def gsr(epi_data, mask, direction='y', ref_file=None, out_file=None):
    '\n    Computes the :abbr:`GSR (ghost to signal ratio)` [Giannelli2010]_. The\n    procedure is as follows:\n\n      #. Create a Nyquist ghost mask by circle-shifting the original mask by :math:`N/2`.\n\n      #. Rotate by :math:`N/2`\n\n      #. Remove the intersection with the original mask\n\n      #. Generate a non-ghost background\n\n      #. Calculate the :abbr:`GSR (ghost to signal ratio)`\n\n\n    .. warning ::\n\n      This should be used with EPI images for which the phase\n      encoding direction is known.\n\n    :param str epi_file: path to epi file\n    :param str mask_file: path to brain mask\n    :param str direction: the direction of phase encoding (x, y, all)\n    :return: the computed gsr\n\n    '
    direction = direction.lower()
    if (direction[(- 1)] not in ['x', 'y', 'all']):
        raise Exception(('Unknown direction %s, should be one of x, -x, y, -y, all' % direction))
    if (direction == 'all'):
        result = []
        for newdir in ['x', 'y']:
            ofile = None
            if (out_file is not None):
                (fname, ext) = op.splitext(ofile)
                if (ext == '.gz'):
                    (fname, ext2) = op.splitext(fname)
                    ext = (ext2 + ext)
                ofile = ('%s_%s%s' % (fname, newdir, ext))
            result += [gsr(epi_data, mask, newdir, ref_file=ref_file, out_file=ofile)]
        return result
    n2_mask = np.zeros_like(mask)
    if (direction == 'x'):
        n2lim = np.floor((mask.shape[0] / 2))
        n2_mask[:n2lim, :, :] = mask[n2lim:(n2lim * 2), :, :]
        n2_mask[n2lim:(n2lim * 2), :, :] = mask[:n2lim, :, :]
    elif (direction == 'y'):
        n2lim = np.floor((mask.shape[1] / 2))
        n2_mask[:, :n2lim, :] = mask[:, n2lim:(n2lim * 2), :]
        n2_mask[:, n2lim:(n2lim * 2), :] = mask[:, :n2lim, :]
    elif (direction == 'z'):
        n2lim = np.floor((mask.shape[2] / 2))
        n2_mask[:, :, :n2lim] = mask[:, :, n2lim:(n2lim * 2)]
        n2_mask[:, :, n2lim:(n2lim * 2)] = mask[:, :, :n2lim]
    n2_mask = (n2_mask * (1 - mask))
    n2_mask = (n2_mask + (2 * ((1 - n2_mask) - mask)))
    if ((ref_file is not None) and (out_file is not None)):
        ref = nb.load(ref_file)
        out = nb.Nifti1Image(n2_mask, ref.get_affine(), ref.get_header())
        out.to_filename(out_file)
    ghost = (epi_data[(n2_mask == 1)].mean() - epi_data[(n2_mask == 2)].mean())
    signal = epi_data[(n2_mask == 0)].mean()
    return float((ghost / signal))
Computes the :abbr:`GSR (ghost to signal ratio)` [Giannelli2010]_. The
procedure is as follows:

  #. Create a Nyquist ghost mask by circle-shifting the original mask by :math:`N/2`.

  #. Rotate by :math:`N/2`

  #. Remove the intersection with the original mask

  #. Generate a non-ghost background

  #. Calculate the :abbr:`GSR (ghost to signal ratio)`


.. warning ::

  This should be used with EPI images for which the phase
  encoding direction is known.

:param str epi_file: path to epi file
:param str mask_file: path to brain mask
:param str direction: the direction of phase encoding (x, y, all)
:return: the computed gsr
def gsr(epi_data, mask, direction='y', ref_file=None, out_file=None):
    '\n    Computes the :abbr:`GSR (ghost to signal ratio)` [Giannelli2010]_. The\n    procedure is as follows:\n\n      #. Create a Nyquist ghost mask by circle-shifting the original mask by :math:`N/2`.\n\n      #. Rotate by :math:`N/2`\n\n      #. Remove the intersection with the original mask\n\n      #. Generate a non-ghost background\n\n      #. Calculate the :abbr:`GSR (ghost to signal ratio)`\n\n\n    .. warning ::\n\n      This should be used with EPI images for which the phase\n      encoding direction is known.\n\n    :param str epi_file: path to epi file\n    :param str mask_file: path to brain mask\n    :param str direction: the direction of phase encoding (x, y, all)\n    :return: the computed gsr\n\n    '
    direction = direction.lower()
    if (direction[(- 1)] not in ['x', 'y', 'all']):
        raise Exception(('Unknown direction %s, should be one of x, -x, y, -y, all' % direction))
    if (direction == 'all'):
        result = []
        for newdir in ['x', 'y']:
            ofile = None
            if (out_file is not None):
                (fname, ext) = op.splitext(ofile)
                if (ext == '.gz'):
                    (fname, ext2) = op.splitext(fname)
                    ext = (ext2 + ext)
                ofile = ('%s_%s%s' % (fname, newdir, ext))
            result += [gsr(epi_data, mask, newdir, ref_file=ref_file, out_file=ofile)]
        return result
    n2_mask = np.zeros_like(mask)
    if (direction == 'x'):
        n2lim = np.floor((mask.shape[0] / 2))
        n2_mask[:n2lim, :, :] = mask[n2lim:(n2lim * 2), :, :]
        n2_mask[n2lim:(n2lim * 2), :, :] = mask[:n2lim, :, :]
    elif (direction == 'y'):
        n2lim = np.floor((mask.shape[1] / 2))
        n2_mask[:, :n2lim, :] = mask[:, n2lim:(n2lim * 2), :]
        n2_mask[:, n2lim:(n2lim * 2), :] = mask[:, :n2lim, :]
    elif (direction == 'z'):
        n2lim = np.floor((mask.shape[2] / 2))
        n2_mask[:, :, :n2lim] = mask[:, :, n2lim:(n2lim * 2)]
        n2_mask[:, :, n2lim:(n2lim * 2)] = mask[:, :, :n2lim]
    n2_mask = (n2_mask * (1 - mask))
    n2_mask = (n2_mask + (2 * ((1 - n2_mask) - mask)))
    if ((ref_file is not None) and (out_file is not None)):
        ref = nb.load(ref_file)
        out = nb.Nifti1Image(n2_mask, ref.get_affine(), ref.get_header())
        out.to_filename(out_file)
    ghost = (epi_data[(n2_mask == 1)].mean() - epi_data[(n2_mask == 2)].mean())
    signal = epi_data[(n2_mask == 0)].mean()
    return float((ghost / signal))