def gen_motion_parameters(subject_id, scan_id, movement_parameters,
    max_displacement):
    """  Method to calculate all the movement parameters.

    Parameters
    ----------
    subject_id : string
        subject name or id

    scan_id : string
        scan name or id

    max_displacement : string
        path of file with maximum displacement (in mm) for brain voxels in each volume

    movement_parameters : string
        path of 1D file containing six movement/motion parameters(3 Translation,
        3 Rotations) in different columns (roll pitch yaw dS  dL  dP)

    Returns
    -------
    out_file : string
        path to csv file contianing various motion parameters
    """
    import os
    import numpy as np
    import re
    out_file = 'motion_parameters.txt'
    with open(out_file, 'w') as f:
        header = """Subject,Scan,Mean_Relative_RMS_Displacement,Max_Relative_RMS_Displacement,Movements_gt_threshold,Mean_Relative_Mean_Rotation,Mean_Relative_Maxdisp,Max_Relative_Maxdisp,Max_Abs_Maxdisp,Max Relative_Roll,Max_Relative_Pitch,Max_Relative_Yaw,Max_Relative_dS-I,Max_Relative_dL-R,Max_Relative_dP-A,Mean_Relative_Roll,Mean_Relative_Pitch,Mean_Relative_Yaw,Mean_Relative_dS-I,Mean_Relative_dL-R,Mean_Relative_dP-A,Max_Abs_Roll,Max_Abs_Pitch,Max_Abs_Yaw,Max_Abs_dS-I,Max_Abs_dL-R,Max_Abs_dP-A,Mean_Abs_Roll,Mean_Abs_Pitch,Mean_Abs_Yaw,Mean_Abs_dS-I,Mean_Abs_dL-R,Mean_Abs_dP-A
"""
        f.write(header)
        f.write('%s,' % subject_id)
        f.write('%s,' % scan_id)
        arr = np.genfromtxt(movement_parameters)
        arr = arr.T
        rms = np.sqrt(arr[3] * arr[3] + arr[4] * arr[4] + arr[5] * arr[5])
        diff = np.diff(rms)
        MEANrms = np.mean(abs(diff))
        f.write('%.3f,' % MEANrms)
        MAXrms = np.max(abs(diff))
        f.write('%.3f,' % MAXrms)
        NUMmove = np.sum(abs(diff) > 0.1)
        f.write('%.3f,' % NUMmove)
        MEANrot = np.mean(np.abs(np.diff((abs(arr[0]) + abs(arr[1]) + abs(
            arr[2])) / 3)))
        f.write('%.3f,' % MEANrot)
        with open(max_displacement, 'r') as disp_f:
            lines = disp_f.readlines()
        list1 = []
        for l in lines:
            if re.match('^\\d+?\\.\\d+?$', l.strip()):
                list1.append(float(l.strip()))
        arr2 = np.array(list1, dtype='float')
        mean = np.mean(np.diff(arr2))
        f.write('%.3f,' % mean)
        relMAX = np.max(abs(np.diff(arr2)))
        f.write('%.3f,' % relMAX)
        MAX = np.max(arr2)
        f.write('%.3f,' % MAX)
        for i in range(6):
            f.write('%.6f,' % np.max(abs(np.diff(arr[i]))))
        for i in range(6):
            f.write('%.6f,' % np.mean(np.diff(arr[i])))
        for i in range(6):
            f.write('%.6f,' % np.max(abs(arr[i])))
        for i in range(6):
            f.write('%.6f,' % np.mean(abs(arr[i])))
    return os.path.abspath(out_file)
