def fd_jenkinson(in_file, rmax=80.0, out_file=None):
    '\n    Compute the :abbr:`FD (framewise displacement)` [Jenkinson2002]_\n    on a 4D dataset, after ``3dvolreg`` has been executed\n    (generally a file named ``*.affmat12.1D``).\n\n    :param str in_file: path to epi file\n    :param float rmax: the default radius (as in FSL) of a sphere represents\n      the brain in which the angular displacements are projected.\n    :param str out_file: a path for the output file with the FD\n\n    :return: the output file with the FD, and the average FD along\n      the time series\n    :rtype: tuple(str, float)\n\n\n    .. note ::\n\n      :code:`infile` should have one 3dvolreg affine matrix in one row -\n      NOT the motion parameters\n\n\n    '
    import sys
    import math
    if (out_file is None):
        (fname, ext) = op.splitext(op.basename(in_file))
        out_file = op.abspath(('%s_fdfile%s' % (fname, ext)))
    if ('rel.rms' in in_file):
        return in_file
    pm_ = np.genfromtxt(in_file)
    original_shape = pm_.shape
    pm = np.zeros((pm_.shape[0], (pm_.shape[1] + 4)))
    pm[:, :original_shape[1]] = pm_
    pm[:, original_shape[1]:] = [0.0, 0.0, 0.0, 1.0]
    T_rb_prev = np.matrix(np.eye(4))
    flag = 0
    X = [0]
    for i in range(0, pm.shape[0]):
        T_rb = np.matrix(pm[i].reshape(4, 4))
        if (flag == 0):
            flag = 1
        else:
            M = (np.dot(T_rb, T_rb_prev.I) - np.eye(4))
            A = M[0:3, 0:3]
            b = M[0:3, 3]
            FD_J = math.sqrt(((((rmax * rmax) / 5) * np.trace(np.dot(A.T, A))) + np.dot(b.T, b)))
            X.append(FD_J)
        T_rb_prev = T_rb
    np.savetxt(out_file, X)
    return out_file
Compute the :abbr:`FD (framewise displacement)` [Jenkinson2002]_
on a 4D dataset, after ``3dvolreg`` has been executed
(generally a file named ``*.affmat12.1D``).

:param str in_file: path to epi file
:param float rmax: the default radius (as in FSL) of a sphere represents
  the brain in which the angular displacements are projected.
:param str out_file: a path for the output file with the FD

:return: the output file with the FD, and the average FD along
  the time series
:rtype: tuple(str, float)


.. note ::

  :code:`infile` should have one 3dvolreg affine matrix in one row -
  NOT the motion parameters
def fd_jenkinson(in_file, rmax=80.0, out_file=None):
    '\n    Compute the :abbr:`FD (framewise displacement)` [Jenkinson2002]_\n    on a 4D dataset, after ``3dvolreg`` has been executed\n    (generally a file named ``*.affmat12.1D``).\n\n    :param str in_file: path to epi file\n    :param float rmax: the default radius (as in FSL) of a sphere represents\n      the brain in which the angular displacements are projected.\n    :param str out_file: a path for the output file with the FD\n\n    :return: the output file with the FD, and the average FD along\n      the time series\n    :rtype: tuple(str, float)\n\n\n    .. note ::\n\n      :code:`infile` should have one 3dvolreg affine matrix in one row -\n      NOT the motion parameters\n\n\n    '
    import sys
    import math
    if (out_file is None):
        (fname, ext) = op.splitext(op.basename(in_file))
        out_file = op.abspath(('%s_fdfile%s' % (fname, ext)))
    if ('rel.rms' in in_file):
        return in_file
    pm_ = np.genfromtxt(in_file)
    original_shape = pm_.shape
    pm = np.zeros((pm_.shape[0], (pm_.shape[1] + 4)))
    pm[:, :original_shape[1]] = pm_
    pm[:, original_shape[1]:] = [0.0, 0.0, 0.0, 1.0]
    T_rb_prev = np.matrix(np.eye(4))
    flag = 0
    X = [0]
    for i in range(0, pm.shape[0]):
        T_rb = np.matrix(pm[i].reshape(4, 4))
        if (flag == 0):
            flag = 1
        else:
            M = (np.dot(T_rb, T_rb_prev.I) - np.eye(4))
            A = M[0:3, 0:3]
            b = M[0:3, 3]
            FD_J = math.sqrt(((((rmax * rmax) / 5) * np.trace(np.dot(A.T, A))) + np.dot(b.T, b)))
            X.append(FD_J)
        T_rb_prev = T_rb
    np.savetxt(out_file, X)
    return out_file