Example #1
0
def test_create_eddy_correct_pipeline(tmpdir):
    fsl_course_dir = os.path.abspath(os.environ['FSL_COURSE_DATA'])

    dwi_file = os.path.join(fsl_course_dir, "fdt1/subj1/data.nii.gz")

    trim_dwi = pe.Node(fsl.ExtractROI(t_min=0,
                                      t_size=2), name="trim_dwi")
    trim_dwi.inputs.in_file = dwi_file

    nipype_eddycorrect = create_eddy_correct_pipeline("nipype_eddycorrect")
    nipype_eddycorrect.inputs.inputnode.ref_num = 0

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        original_eddycorrect = pe.Node(interface=fsl.EddyCorrect(), name="original_eddycorrect")
    original_eddycorrect.inputs.ref_num = 0

    test = pe.Node(util.AssertEqual(), name="eddy_corrected_dwi_test")

    pipeline = pe.Workflow(name="test_eddycorrect")
    pipeline.base_dir = tmpdir.mkdir("nipype_test_eddycorrect_").strpath

    pipeline.connect([(trim_dwi, original_eddycorrect, [("roi_file", "in_file")]),
                      (trim_dwi, nipype_eddycorrect, [("roi_file", "inputnode.in_file")]),
                      (nipype_eddycorrect, test, [("outputnode.eddy_corrected", "volume1")]),
                      (original_eddycorrect, test, [("eddy_corrected", "volume2")]),
                      ])

    pipeline.run(plugin='Linear')
Example #2
0
def test_create_eddy_correct_pipeline():
    fsl_course_dir = os.path.abspath('fsl_course_data')

    dwi_file = os.path.join(fsl_course_dir, "fdt/subj1/data.nii.gz")

    nipype_eddycorrect = create_eddy_correct_pipeline("nipype_eddycorrect")
    nipype_eddycorrect.inputs.inputnode.in_file = dwi_file
    nipype_eddycorrect.inputs.inputnode.ref_num = 0

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        original_eddycorrect = pe.Node(interface=fsl.EddyCorrect(), name="original_eddycorrect")
    original_eddycorrect.inputs.in_file = dwi_file
    original_eddycorrect.inputs.ref_num = 0

    test = pe.Node(util.AssertEqual(), name="eddy_corrected_dwi_test")

    pipeline = pe.Workflow(name="test_eddycorrect")
    pipeline.base_dir = tempfile.mkdtemp(prefix="nipype_test_eddycorrect_")

    pipeline.connect([(nipype_eddycorrect, test, [("outputnode.eddy_corrected", "volume1")]),
                      (original_eddycorrect, test, [("eddy_corrected", "volume2")]),
                      ])

    pipeline.run(plugin='Linear')
    shutil.rmtree(pipeline.base_dir)
Example #3
0
def test_create_eddy_correct_pipeline():
    fsl_course_dir = os.path.abspath(os.environ['FSL_COURSE_DATA'])

    dwi_file = os.path.join(fsl_course_dir, "fdt1/subj1/data.nii.gz")

    trim_dwi = pe.Node(fsl.ExtractROI(t_min=0, t_size=2), name="trim_dwi")
    trim_dwi.inputs.in_file = dwi_file

    nipype_eddycorrect = create_eddy_correct_pipeline("nipype_eddycorrect")
    nipype_eddycorrect.inputs.inputnode.ref_num = 0

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        original_eddycorrect = pe.Node(interface=fsl.EddyCorrect(),
                                       name="original_eddycorrect")
    original_eddycorrect.inputs.ref_num = 0

    test = pe.Node(util.AssertEqual(), name="eddy_corrected_dwi_test")

    pipeline = pe.Workflow(name="test_eddycorrect")
    pipeline.base_dir = tempfile.mkdtemp(prefix="nipype_test_eddycorrect_")

    pipeline.connect([
        (trim_dwi, original_eddycorrect, [("roi_file", "in_file")]),
        (trim_dwi, nipype_eddycorrect, [("roi_file", "inputnode.in_file")]),
        (nipype_eddycorrect, test, [("outputnode.eddy_corrected", "volume1")]),
        (original_eddycorrect, test, [("eddy_corrected", "volume2")]),
    ])

    pipeline.run(plugin='Linear')
    shutil.rmtree(pipeline.base_dir)
def do_pipe1_prepro(subject_ID, freesurfer_dir, data_dir, data_template, workflow_dir, output_dir):

    
    """
    Packages and Data Setup
    =======================
    Import necessary modules from nipype.
    """
    
    
    import nipype.interfaces.io as io  # Data i/o
    import nipype.interfaces.utility as util  # utility
    import nipype.pipeline.engine as pe  # pipeline engine
    import nipype.interfaces.fsl as fsl
    import nipype.interfaces.freesurfer as fsurf  # freesurfer
    import nipype.interfaces.mrtrix as mrtrix
    import nipype.interfaces.ants as ants
    import nipype.interfaces.vista as vista
    import os.path as op  # system functions
    
    from nipype.workflows.dmri.fsl.epi import create_eddy_correct_pipeline
    from nipype.interfaces.utility import Function
    
    from dmri_pipe_aux import threshold_bval
    from dmri_pipe_aux import pick_full_ribbon
    from dmri_pipe_aux import get_voxels
    from dmri_pipe_aux import assign_voxel_ids
    from dmri_pipe_aux import get_mean_b0
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Point to the freesurfer subjects directory (Recon-all must have been run on the subjects)
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    subjects_dir = op.abspath(freesurfer_dir)
    fsurf.FSCommand.set_default_subjects_dir(subjects_dir)
    fsl.FSLCommand.set_default_output_type('NIFTI')
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    define the workflow
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    dmripipeline = pe.Workflow(name='pipe1_prepro')
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Use datasource node to perform the actual data grabbing.
    Templates for the associated images are used to obtain the correct images.
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    info = dict(dwi=[['subject_id', 'DTI_mx_137.nii.gz']],
                bvecs=[['subject_id', 'DTI_mx_137.bvecs']],
                bvals=[['subject_id', 'DTI_mx_137.bvals']])
    
    datasource = pe.Node(interface=io.DataGrabber(infields=['subject_id'], outfields=info.keys()), name='datasource')
    datasource.inputs.subject_id =  subject_ID
    datasource.inputs.template = data_template
    datasource.inputs.base_directory = data_dir
    datasource.inputs.template_args = info
    datasource.inputs.sort_filelist = True
    datasource.run_without_submitting = True
    
    auxsource = pe.Node(interface=io.DataGrabber(outfields=['lateral_line']), name='auxsource')
    auxsource.inputs.template = 'lateral_line.nii'
    auxsource.inputs.base_directory = data_dir
    auxsource.inputs.sort_filelist = True
    auxsource.run_without_submitting = True

    
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    The input node and Freesurfer sources declared here will be the main
    conduits for the raw data to the rest of the processing pipeline.
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    inputnode = pe.Node(interface=util.IdentityInterface(fields=["dwi", "bvecs", "bvals", "lateral_line"]), name="inputnode")
    
    FreeSurferSource = pe.Node(interface=io.FreeSurferSource(), name='01_FreeSurferSource')
    FreeSurferSource.inputs.subjects_dir = subjects_dir
    FreeSurferSource.inputs.subject_id = subject_ID
    FreeSurferSource.run_without_submitting = True
    
 
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Define a function that thresholds the bvals and zero's the 'near zero' B images.
    This is a correction for the NKI DWI data. The Bval file has nine 'near zero' B images.
    If this is not corrected, MRTRIX does not read these images as B0's introducing various
    errors throughout the processing
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    corrected_bvalues = pe.Node (name='01_corrected_bvalues', interface=Function (input_names=['in_file', 'thr'], output_names=['out_file'], function=threshold_bval))
    corrected_bvalues.inputs.thr = 100
    corrected_bvalues.run_without_submitting = True
    dmripipeline.connect(inputnode, "bvals", corrected_bvalues, "in_file")
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Diffusion processing nodes
    --------------------------
    
    MRTRIX ENCODING
    Convert FSL format files bvecs and bvals into single encoding file for MRtrix
    invert x axis to get the right convention
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    fsl2mrtrix = pe.Node(interface=mrtrix.FSL2MRTrix(), name='01_fsl2mrtrix')
    fsl2mrtrix.inputs.invert_x = True
    fsl2mrtrix.run_without_submitting = True
    dmripipeline.connect(corrected_bvalues, "out_file", fsl2mrtrix, "bval_file")
    dmripipeline.connect(inputnode, "bvecs", fsl2mrtrix, "bvec_file")
    
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    EDDY CURRENT CORRECTION
    Correct for distortions induced by eddy currents before fitting the tensors.
    The first image  is used as a reference for which to warp the others.
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    eddy_corrected_dmri = create_eddy_correct_pipeline(name='01_eddy_corrected_dmri')
    eddy_corrected_dmri.inputs.inputnode.ref_num = 0
    eddy_corrected_dmri.run_without_submitting = True
    dmripipeline.connect(inputnode, "dwi", eddy_corrected_dmri, "inputnode.in_file")

    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    TENSOR FITTING
    Tensors are fitted to each voxel in the diffusion-weighted image and from these three maps are created:
        * Major eigenvector in each voxel
        * Apparent diffusion coefficient
        * Fractional anisotropy
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    dwi2tensor = pe.Node(interface=mrtrix.DWI2Tensor(), name='02_dwi2tensor')
    dwi2tensor.inputs.out_filename=subject_ID +'_tensor.mif'
    dwi2tensor.run_without_submitting = True
    dmripipeline.connect(eddy_corrected_dmri, "outputnode.eddy_corrected", dwi2tensor, "in_file")
    dmripipeline.connect(fsl2mrtrix, "encoding_file", dwi2tensor, "encoding_file")
    
    
    #tensor2vector = pe.Node(interface=mrtrix.Tensor2Vector(), name='03_tensor2vector')
    #dmripipeline.connect([(dwi2tensor, tensor2vector, [['tensor', 'in_file']])])
    #tensor2adc = pe.Node(interface=mrtrix.Tensor2ApparentDiffusion(), name='03_tensor2adc')
    #dmripipeline.connect([(dwi2tensor, tensor2adc, [['tensor', 'in_file']]   )])
    
    tensor_full_fa = pe.Node(interface=mrtrix.Tensor2FractionalAnisotropy(), name='03_tensor2fa')
    tensor_full_fa.run_without_submitting = True
    #full_fa.inputs.out_filename=subject_ID+'_fa_full.nii'
    dmripipeline.connect(dwi2tensor, 'tensor', tensor_full_fa, 'in_file')
    
    full_fa = pe.Node(interface=mrtrix.MRConvert(), name='04_full_fa')
    full_fa.inputs.out_filename = subject_ID + '_fa_full.nii'
    full_fa.inputs.extension = 'nii'
    full_fa.run_without_submitting = True
    dmripipeline.connect(tensor_full_fa, "FA", full_fa, "in_file")
    
    
    mean_b0 = pe.Node(interface=Function(input_names=["bvals_file","dwi_file","out_filename"],output_names=["out_file"],function=get_mean_b0), name='02_mean_b0')
    mean_b0.inputs.out_filename= subject_ID +'_mean_b0.nii'
    mean_b0.run_without_submitting = True
    dmripipeline.connect(eddy_corrected_dmri, "outputnode.eddy_corrected", mean_b0, "dwi_file")
    dmripipeline.connect(corrected_bvalues, "out_file", mean_b0, "bvals_file")
    
    corrected_b0 = pe.Node(interface=ants.N4BiasFieldCorrection(), name='03_corrected_b0')
    corrected_b0.inputs.output_image = subject_ID + '_corrected_b0.nii'
    corrected_b0.inputs.dimension = 3
    corrected_b0.inputs.bspline_fitting_distance = 300
    corrected_b0.inputs.shrink_factor = 3
    corrected_b0.inputs.n_iterations = [50,50,30,20]
    corrected_b0.inputs.convergence_threshold = 1e-6
    corrected_b0.run_without_submitting = True
    dmripipeline.connect(mean_b0, "out_file", corrected_b0, "input_image")    
    
    ''' DISCONTINUED
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    B0_MASK
    This block creates the rough brain mask with two erosion steps. The mask will be used to generate an Single fiber voxel mask
    for the estimation of the response function and a white matter mask which will serve as a seed for tractography.
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    

    bet_b0 = pe.Node(interface=fsl.BET(mask=False), name='02_bet_b0')
    bet_b0.run_without_submitting = True
    dmripipeline.connect(eddy_corrected_dmri, "pick_ref.out", bet_b0, "in_file")
    
    
    b0_mask = pe.Node(interface=fsl.maths.MathsCommand(), name='03_b0_mask')
    b0_mask.inputs.args = '-bin'
    b0_mask.run_without_submitting = True
    dmripipeline.connect(bet_b0, "out_file", b0_mask , "in_file")
    
    """
    mask fa with b0 ... also erode it to use for estimation of response function
    """
    fa_b0_masked = pe.Node(interface=fsl.maths.ApplyMask(), name='05_fa_B0_masked')
    fa_b0_masked.run_without_submitting = True
    dmripipeline.connect(full_fa, "converted", fa_b0_masked, "in_file")
    dmripipeline.connect(b0_mask, "out_file", fa_b0_masked, "mask_file")
    
    fa_b0_masked_ero = pe.Node(interface=fsl.maths.MathsCommand(), name='06_fa_B0_masked_ero')
    fa_b0_masked_ero.inputs.args = '-ero'
    fa_b0_masked_ero.run_without_submitting = True
    dmripipeline.connect(fa_b0_masked, 'out_file', fa_b0_masked_ero, 'in_file')
    '''
    
    
    """
    CSF mask extraction from B0
    """
    CSF_mask = pe.Node(interface=fsl.maths.MathsCommand(), name='02_CSF_mask')
    CSF_mask.inputs.args = '-thrP 95 -binv'
    CSF_mask.inputs.out_file = subject_ID + '_csf_mask.nii'
    CSF_mask.run_without_submitting = True
    dmripipeline.connect(corrected_b0, 'output_image', CSF_mask, 'in_file')
    
        
    
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Non-linear transformation of Fa map onto T1. This will generate a warp field that will be inverted to warp full t1 onto FA.
    
    
    1- Convert ribbon, T1 freesurfer outputs to nii
    2- Mask T1 with ribbon to extract the brain and get rid of the of the CB and BS.
    4- T1 --> FA ---- Register ribbon masked T1 onto B0 masked FA with 6DOFs, cross corr.
    5- Close holes in T1_2_FA_6DOF (-dil -ero)
    6- Mask original FA map with 6DOF FA registered,closed wholes T1.
    7- Eroded FA -ero with default-kbox 3x3x3
    8- Non-linearly transform t1, wm mask created from ribbon
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    """
    convert t1and ribbon to nii
    """
    t1_nii = pe.Node(interface=fsurf.MRIConvert(), name='05_t1_nii')
    t1_nii.inputs.out_type = 'nii'
    t1_nii.inputs.out_file = subject_ID + '_t1.nii'
    t1_nii.run_without_submitting = True
    dmripipeline.connect([(FreeSurferSource, t1_nii, [("T1", "in_file")])])
    
    """
    convert T1 and ribbon
    """
    ribbon_nii = pe.Node(interface=fsurf.MRIConvert(), name='06_ribbon_nii')
    ribbon_nii.inputs.out_file = subject_ID + '_ribbon.nii'
    ribbon_nii.inputs.out_type = 'nii'
    ribbon_nii.run_without_submitting = True
    dmripipeline.connect([(FreeSurferSource, ribbon_nii, [(("ribbon", pick_full_ribbon), "in_file")])])
    
    
    aseg_nii = pe.Node(interface=fsurf.MRIConvert(), name='07_aseg_nii')
    aseg_nii.inputs.out_file = subject_ID + '_aseg.nii'
    aseg_nii.inputs.out_type = 'nii'
    aseg_nii.run_without_submitting = True
    dmripipeline.connect([(FreeSurferSource, aseg_nii, [("aseg", "in_file")])])
    
    """
    mask t1 with ribbon and aseg (without and with cerebellum)
    """
    t1_ribbon_masked = pe.Node(interface=fsl.maths.ApplyMask(), name='08_t1_ribbon_masked')
    t1_ribbon_masked.run_without_submitting = True
    dmripipeline.connect([(t1_nii, t1_ribbon_masked , [("out_file", "in_file")])])
    dmripipeline.connect([(ribbon_nii, t1_ribbon_masked , [("out_file", "mask_file")])])
    
    t1_aseg_masked = pe.Node(interface=fsl.maths.ApplyMask(), name='09_t1_aseg_masked')
    t1_aseg_masked.run_without_submitting = True
    dmripipeline.connect([(t1_nii, t1_aseg_masked , [("out_file", "in_file")])])
    dmripipeline.connect([(aseg_nii, t1_aseg_masked , [("out_file", "mask_file")])])
    
    """
    register T1_aseg_masked to full b0
    """
    flirt_t1aseg_2_b0 = pe.Node(interface=fsl.FLIRT(), name='10_flirt_t1aseg_2_b0')
    flirt_t1aseg_2_b0.inputs.dof = 6
    flirt_t1aseg_2_b0.inputs.cost_func = 'corratio'
    flirt_t1aseg_2_b0.inputs.bins = 256
    flirt_t1aseg_2_b0.inputs.interp = 'trilinear'
    flirt_t1aseg_2_b0.inputs.out_matrix_file = 'flirt_t1_2_b0.mat'
    flirt_t1aseg_2_b0.run_without_submitting = True
    dmripipeline.connect([(t1_ribbon_masked, flirt_t1aseg_2_b0 , [("out_file", "in_file")])])
    dmripipeline.connect([(corrected_b0, flirt_t1aseg_2_b0 , [("output_image", "reference")])])
    
    """
    use warp to convert ribbon to b0
    """
    ribbon_linear2b0 = pe.Node(interface=fsl.ApplyXfm(), name='11_ribbon_linear2b0')
    ribbon_linear2b0.inputs.apply_xfm = True
    ribbon_linear2b0.inputs.interp = 'nearestneighbour'
    ribbon_linear2b0.run_without_submitting = True
    dmripipeline.connect([(ribbon_nii, ribbon_linear2b0 , [("out_file", "in_file")])])
    dmripipeline.connect([(flirt_t1aseg_2_b0, ribbon_linear2b0 , [("out_matrix_file", "in_matrix_file")])])
    dmripipeline.connect([(corrected_b0, ribbon_linear2b0 , [("output_image", "reference")])])
    
    """
    close holes in registered ribbon2b0
    """
    ribbonmask_linear2b0_rounded = pe.Node(interface=fsl.maths.MathsCommand(), name='11_ribbonmask_linear2b0_rounded')
    ribbonmask_linear2b0_rounded.inputs.args = '-bin -kernel sphere 3 -dilM -dilM  -ero -ero -ero'
    ribbonmask_linear2b0_rounded.run_without_submitting = True
    dmripipeline.connect([(ribbon_linear2b0, ribbonmask_linear2b0_rounded, [('out_file', 'in_file')])])
    
    """
    mask fa with registered ribbon
    """
    fa_rib2b0_masked = pe.Node(interface=fsl.maths.ApplyMask(), name='12_fa_t12B0_masked')
    fa_rib2b0_masked.run_without_submitting = True
    dmripipeline.connect(full_fa, "converted", fa_rib2b0_masked, "in_file")
    dmripipeline.connect(ribbonmask_linear2b0_rounded, "out_file", fa_rib2b0_masked, "mask_file")
    
    
    
    """
    register T1_ribbon_masked to Fa_b0_masked
    # flirt -in T1_masked.nii.gz -ref  fa_brain.nii.gz -out t12fa -omat t12fa.mat -bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 6  -interp trilinear
    """
    flirt_t1masked_2_FAmasked = pe.Node(interface=fsl.FLIRT(), name='13_flirt_t1m_2_FAm')
    flirt_t1masked_2_FAmasked.inputs.dof = 6
    flirt_t1masked_2_FAmasked.inputs.cost_func = 'corratio'
    flirt_t1masked_2_FAmasked.inputs.bins = 256
    flirt_t1masked_2_FAmasked.inputs.interp = 'trilinear'
    flirt_t1masked_2_FAmasked.inputs.out_matrix_file = 'flirt_t1_2_fa.mat'
    flirt_t1masked_2_FAmasked.run_without_submitting = True
    dmripipeline.connect([(t1_ribbon_masked, flirt_t1masked_2_FAmasked , [("out_file", "in_file")])])
    dmripipeline.connect([(fa_rib2b0_masked, flirt_t1masked_2_FAmasked , [("out_file", "reference")])])
    
    """
    inverse the linear T1 to FA transform
    """
    invert_linearxfm_t1_2_fa = pe.Node(interface=fsl.ConvertXFM(), name='13_invertxfm_t1m_2_FAm')
    invert_linearxfm_t1_2_fa.inputs.invert_xfm = True
    invert_linearxfm_t1_2_fa.inputs.out_file = 'flirt_t1_2_fa_inv.mat'
    invert_linearxfm_t1_2_fa.run_without_submitting = True
    dmripipeline.connect([(flirt_t1masked_2_FAmasked, invert_linearxfm_t1_2_fa , [("out_matrix_file", "in_file")])])
    
    """
    use warp to convert ribbon (again)
    """
    ribbon_linear2fa = pe.Node(interface=fsl.ApplyXfm(), name='14_ribbon_linear2fa')
    ribbon_linear2fa.inputs.apply_xfm = True
    ribbon_linear2fa.inputs.interp = 'nearestneighbour'
    ribbon_linear2fa.run_without_submitting = True
    dmripipeline.connect([(ribbon_nii, ribbon_linear2fa , [("out_file", "in_file")])])
    dmripipeline.connect([(flirt_t1masked_2_FAmasked, ribbon_linear2fa , [("out_matrix_file", "in_matrix_file")])])
    dmripipeline.connect([(fa_rib2b0_masked, ribbon_linear2fa , [("out_file", "reference")])])
    
    """
    close holes in registered ribbon2Fa
    """
    ribbonmask_linear2fa_rounded = pe.Node(interface=fsl.maths.MathsCommand(), name='14_ribbonmask_linear2fa_rounded')
    ribbonmask_linear2fa_rounded.inputs.args = '-bin -kernel sphere 3 -dilM -dilM  -ero -ero -ero'
    ribbonmask_linear2fa_rounded.run_without_submitting = True
    dmripipeline.connect([(ribbon_linear2fa, ribbonmask_linear2fa_rounded, [('out_file', 'in_file')])])
    
    """
    mask full fa with T12Fa_closed_holes
    """
    fa_linear_ribbon_masked = pe.Node(interface=fsl.ApplyMask(), name='15_fa_linear_ribbon_masked')
    fa_linear_ribbon_masked.run_without_submitting = True
    dmripipeline.connect([(full_fa, fa_linear_ribbon_masked , [("converted", "in_file")])])
    dmripipeline.connect([(ribbonmask_linear2fa_rounded, fa_linear_ribbon_masked, [("out_file", "mask_file")])])
    
    """
    Non-linearly transform FA_masked T1_ribbon_masked
    ANTS 3 -m PR[T1_masked.nii,FA_2_T1_6DOF.nii.gz,1,3] -i 5x5 -o FA_2_T1_ANTS.nii.gz -t SyN[0.25] -r Gauss[3,0]
    """
    ants_FA2T1m_2_T1_full = pe.Node(interface=ants.ANTS(), name='16_ants_fa_2_t1')
    ants_FA2T1m_2_T1_full.inputs.dimension = 3
    ants_FA2T1m_2_T1_full.inputs.metric = ['PR']
    ants_FA2T1m_2_T1_full.inputs.radius = [3]
    ants_FA2T1m_2_T1_full.inputs.metric_weight = [1.0]
    ants_FA2T1m_2_T1_full.inputs.transformation_model = 'SyN'
    ants_FA2T1m_2_T1_full.inputs.gradient_step_length = 0.25
    ants_FA2T1m_2_T1_full.inputs.number_of_iterations = [ 5, 5]
    ants_FA2T1m_2_T1_full.inputs.regularization = 'Gauss'
    ants_FA2T1m_2_T1_full.inputs.regularization_gradient_field_sigma = 3
    ants_FA2T1m_2_T1_full.inputs.regularization_deformation_field_sigma = 0
    ants_FA2T1m_2_T1_full.inputs.output_transform_prefix = 'ants_fa_2_regt1_'
    ants_FA2T1m_2_T1_full.run_without_submitting = True
    dmripipeline.connect([(fa_linear_ribbon_masked, ants_FA2T1m_2_T1_full, [('out_file', 'moving_image')])])
    dmripipeline.connect([(flirt_t1masked_2_FAmasked, ants_FA2T1m_2_T1_full, [('out_file', 'fixed_image')])])
        
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Creation of masks for seeding
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    """
    # warp T1_masked to Fa_masked
    """
    t1_warped_2_fa = pe.Node (interface=ants.WarpImageMultiTransform(), name='21_t1_warped_2_fa')
    t1_warped_2_fa.run_without_submitting = True
    dmripipeline.connect([(flirt_t1masked_2_FAmasked, t1_warped_2_fa, [('out_file', 'input_image')])])
    dmripipeline.connect([(fa_linear_ribbon_masked, t1_warped_2_fa, [('out_file', 'reference_image')])])
    dmripipeline.connect([(ants_FA2T1m_2_T1_full, t1_warped_2_fa, [('inverse_warp_transform', 'transformation_series')])])
    
    """
    # warp ribbon 2 fa
    """
    ribbon_warped_2_fa = pe.Node (interface=ants.WarpImageMultiTransform(), name='21_ribbon_warped_2_fa')
    ribbon_warped_2_fa.inputs.use_nearest = True
    ribbon_warped_2_fa.run_without_submitting = True
    dmripipeline.connect([(ribbon_linear2fa, ribbon_warped_2_fa, [('out_file', 'input_image')])])
    dmripipeline.connect([(fa_linear_ribbon_masked, ribbon_warped_2_fa, [('out_file', 'reference_image')])])
    dmripipeline.connect([(ants_FA2T1m_2_T1_full, ribbon_warped_2_fa, [('inverse_warp_transform', 'transformation_series')])])
    
    """
    close holes in registered ribbon2Fa
    """
    ribbon_warped_2_fa_shell = pe.Node(interface=fsl.maths.MathsCommand(), name='21_ribbon_warped_2_fa_shell')
    ribbon_warped_2_fa_shell.inputs.args = '-bin -kernel sphere 3 -dilM -dilM  -ero -ero -ero'
    ribbon_warped_2_fa_shell.run_without_submitting = True
    dmripipeline.connect([(ribbon_warped_2_fa, ribbon_warped_2_fa_shell, [('output_image', 'in_file')])])
    
    
    """
    # generate white matter mask and full brain mask from ribbon
    """
    ribbon_wm_right = pe.Node(interface=fsl.maths.MathsCommand(), name='22_ribbon_right_wm_41')
    ribbon_wm_right.inputs.args = '-thr 41 -uthr 41 -bin'
    ribbon_wm_right.run_without_submitting = True
    dmripipeline.connect([(ribbon_warped_2_fa, ribbon_wm_right, [('output_image', 'in_file')])])
    
    ribbon_gm_right = pe.Node(interface=fsl.maths.MathsCommand(), name='22_ribbon_right_gm_42')
    ribbon_gm_right.inputs.args = '-thr 42 -uthr 42 -bin'
    ribbon_gm_right.run_without_submitting = True
    dmripipeline.connect([(ribbon_warped_2_fa, ribbon_gm_right, [('output_image', 'in_file')])])
    
    ribbon_wm_left = pe.Node(interface=fsl.maths.MathsCommand(), name='22_ribbon_left_wm_2')
    ribbon_wm_left.inputs.args = '-thr 2 -uthr 2 -bin'
    ribbon_wm_left.run_without_submitting = True
    dmripipeline.connect([(ribbon_warped_2_fa, ribbon_wm_left, [('output_image', 'in_file')])])
    
    ribbon_gm_left = pe.Node(interface=fsl.maths.MathsCommand(), name='22_ribbon_left_gm_3')
    ribbon_gm_left.inputs.args = '-thr 3 -uthr 3 -bin'
    ribbon_gm_left.run_without_submitting = True
    dmripipeline.connect([(ribbon_warped_2_fa, ribbon_gm_left, [('output_image', 'in_file')])])
    
    """
    # used masks
    """
    ribbon_fullmask = pe.Node(interface=fsl.maths.MathsCommand(), name='23_ribbon_fullmask')
    ribbon_fullmask.inputs.args = '-bin'
    ribbon_fullmask.inputs.out_file = subject_ID + '_mask_fullbrain.nii'
    ribbon_fullmask.run_without_submitting = True
    dmripipeline.connect([(ribbon_warped_2_fa, ribbon_fullmask, [('output_image', 'in_file')])])
    
    ribbon_left_hemi = pe.Node(interface=fsl.maths.BinaryMaths(), name='23_ribbon_left_hemi')
    ribbon_left_hemi.inputs.operation = 'add'
    ribbon_left_hemi.inputs.args = '-bin'
    ribbon_left_hemi.inputs.out_file = subject_ID + '_mask_left_hemi.nii'
    ribbon_left_hemi.run_without_submitting = True
    dmripipeline.connect([(ribbon_gm_left, ribbon_left_hemi, [('out_file', 'in_file')])])
    dmripipeline.connect([(ribbon_wm_left, ribbon_left_hemi, [('out_file', 'operand_file')])])
    
    ribbon_right_hemi = pe.Node(interface=fsl.maths.BinaryMaths(), name='23_ribbon_right_hemi')
    ribbon_right_hemi.inputs.operation = 'add'
    ribbon_right_hemi.inputs.args = '-bin'
    ribbon_right_hemi.inputs.out_file = subject_ID + '_mask_right_hemi.nii'
    ribbon_right_hemi.run_without_submitting = True
    dmripipeline.connect([(ribbon_gm_right, ribbon_right_hemi, [('out_file', 'in_file')])])
    dmripipeline.connect([(ribbon_wm_right, ribbon_right_hemi, [('out_file', 'operand_file')])])
    
    ribbon_wm_mask = pe.Node(interface=fsl.maths.BinaryMaths(), name='23_ribbon_wm_mask')
    ribbon_wm_mask.inputs.operation = 'add'
    ribbon_wm_mask.inputs.args = '-bin'
    ribbon_wm_mask.run_without_submitting = True
    dmripipeline.connect([(ribbon_wm_right, ribbon_wm_mask, [('out_file', 'in_file')])])
    dmripipeline.connect([(ribbon_wm_left, ribbon_wm_mask, [('out_file', 'operand_file')])])
    
    ribbon_wm_mask_ero = pe.Node(interface=fsl.maths.MathsCommand(), name='23_ribbon_wm_mask_ero')
    ribbon_wm_mask_ero.inputs.args = '-kernel sphere 2 -ero'
    ribbon_wm_mask_ero.run_without_submitting = True
    dmripipeline.connect([(ribbon_wm_mask, ribbon_wm_mask_ero, [('out_file', 'in_file')])])
    
    
    """
    # mask fa with new ribbon
    """
    fa_nl_ribbon_masked = pe.Node(interface=fsl.maths.ApplyMask(), name='24_fa_nl_ribbon_masked')
    fa_nl_ribbon_masked.inputs.out_file = subject_ID + '_fa_masked.nii'
    fa_nl_ribbon_masked.run_without_submitting = True
    dmripipeline.connect([(full_fa, fa_nl_ribbon_masked , [("converted", "in_file")])])
    dmripipeline.connect([(ribbon_fullmask, fa_nl_ribbon_masked , [("out_file", "mask_file")])])
    
    """
    # generate white matter mask from FA with 0.2 thresh
    """
    fa_masked_thresh_bin = pe.Node(interface=fsl.maths.MathsCommand(), name='25_fa_masked_thresh_bin')
    fa_masked_thresh_bin.inputs.args = '-thr 0.2 -bin'
    fa_masked_thresh_bin.run_without_submitting = True
    dmripipeline.connect([(fa_nl_ribbon_masked, fa_masked_thresh_bin, [('out_file', 'in_file')])])
    
    """
    # add fa_wm and ribbon_wm
    """
    fa_wm_holes_closed = pe.Node(interface=fsl.maths.BinaryMaths(), name='26_fa_wm_holes_closed')
    fa_wm_holes_closed.inputs.operation = 'add'
    fa_wm_holes_closed.inputs.args = '-bin'
    fa_wm_holes_closed.run_without_submitting = True
    dmripipeline.connect([(fa_masked_thresh_bin, fa_wm_holes_closed, [('out_file', 'in_file')])])
    dmripipeline.connect([(ribbon_wm_mask_ero, fa_wm_holes_closed, [('out_file', 'operand_file')])])
    
    """
    eliminate non-connected voxels from mask
    """
    fa_wm_connectedcomp_mask = pe.Node(interface=fsl.maths.BinaryMaths(), name='27_fa_wm_connectedcomp_mask')
    fa_wm_connectedcomp_mask.inputs.operation = 'add'
    fa_wm_connectedcomp_mask.inputs.args = '-binv -fillh -binv'
    fa_wm_connectedcomp_mask.run_without_submitting = True
    dmripipeline.connect([(fa_wm_holes_closed, fa_wm_connectedcomp_mask, [('out_file', 'in_file')])])
    dmripipeline.connect([(inputnode, fa_wm_connectedcomp_mask, [('lateral_line', 'operand_file')])])
    
    fa_wm_maincomponent = pe.Node(interface=fsl.maths.ApplyMask(), name='28_fa_wm_maincomponent')
    fa_wm_maincomponent.run_without_submitting = True
    dmripipeline.connect([(fa_wm_holes_closed, fa_wm_maincomponent, [('out_file', 'in_file')])])
    dmripipeline.connect([(fa_wm_connectedcomp_mask, fa_wm_maincomponent , [("out_file", "mask_file")])])
    
    """
    fill holes
    """
    fa_wm_filled = pe.Node(interface=fsl.maths.MathsCommand(), name='29_fa_wm_filled')
    fa_wm_filled.inputs.args = '-fillh'
    fa_wm_filled.run_without_submitting = True
    dmripipeline.connect([(fa_wm_maincomponent, fa_wm_filled , [("out_file", "in_file")])])
    
    """
    remove borders
    """
    fa_wm_rounded = pe.Node(interface=fsl.maths.ApplyMask(), name='29b_wm_rounded')
    fa_wm_rounded.inputs.out_file = subject_ID + '_mask_wm_rounded.nii'
    fa_wm_rounded.run_without_submitting = True
    dmripipeline.connect([(fa_wm_filled, fa_wm_rounded , [("out_file", "in_file")])])
    dmripipeline.connect([(ribbon_warped_2_fa_shell, fa_wm_rounded , [("out_file", "mask_file")])])
    
    """
    exclude csf
    """
    fa_wm_final = pe.Node(interface=fsl.maths.ApplyMask(), name='30_wm_final')
    fa_wm_final.inputs.out_file = subject_ID + '_mask_wm.nii'
    fa_wm_final.run_without_submitting = True
    dmripipeline.connect([(fa_wm_rounded, fa_wm_final , [("out_file", "in_file")])])
    dmripipeline.connect([(CSF_mask, fa_wm_final , [("out_file", "mask_file")])])
    
    """
    save wm in vista format
    """
    wm_vista = pe.Node(interface=vista.Vnifti2Image(), name='30b_wm_vista')
    dmripipeline.connect(fa_wm_final, "out_file", wm_vista, "in_file")
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    # INTERFACE
    # create tract seeding interface..
    # ##this is  created by eroding and subtracting this from the non eroded final fa mask
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    fa_wm_ero = pe.Node(interface=fsl.maths.MathsCommand(), name='31_fa_wm_vol_ero')
    fa_wm_ero.inputs.args = '-kernel sphere 2 -ero'
    fa_wm_ero.run_without_submitting = True
    dmripipeline.connect([(fa_wm_rounded, fa_wm_ero, [('out_file', 'in_file')])])
    
    interface_preliminary = pe.Node(interface=fsl.maths.BinaryMaths(), name='32_interface_preliminary')
    interface_preliminary.inputs.operation = 'sub'
    interface_preliminary.run_without_submitting = True
    dmripipeline.connect([(fa_wm_rounded, interface_preliminary, [('out_file', 'in_file')])])
    dmripipeline.connect([(fa_wm_ero, interface_preliminary, [('out_file', 'operand_file')])])
    
    interface_nocsf = pe.Node(interface=fsl.maths.ApplyMask(), name='33_interface_nocsf')
    interface_nocsf.inputs.out_file = subject_ID + '_interface_nocsf.nii'
    interface_nocsf.run_without_submitting = True
    dmripipeline.connect([(interface_preliminary, interface_nocsf , [("out_file", "in_file")])])
    dmripipeline.connect([(fa_wm_final, interface_nocsf , [("out_file", "mask_file")])])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Creation of Single fiber voxel mask for CSD
    # Create a singla fiber voxel mask
    # this is done by thresholding the the FA MAP at 0.7
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    single_fiber_voxel_mask = pe.Node(interface=fsl.maths.ApplyMask(), name='41_single_fiber_voxel_mask')
    single_fiber_voxel_mask.inputs.args = '-thr 0.7 -bin '
    single_fiber_voxel_mask.inputs.out_file = subject_ID + '_mask_singlefiber.nii'
    single_fiber_voxel_mask.run_without_submitting = True
    dmripipeline.connect([(fa_nl_ribbon_masked, single_fiber_voxel_mask, [('out_file', 'in_file')])])
    dmripipeline.connect([(fa_wm_final, single_fiber_voxel_mask , [("out_file", "mask_file")])])
    
    single_fiber_voxel_mask_mult_final_fa_mif = pe.Node(interface=mrtrix.MRConvert(), name='42_single_fiber_voxel_mask_mult_final_fa_mif')
    single_fiber_voxel_mask_mult_final_fa_mif.inputs.extension = 'mif'
    single_fiber_voxel_mask_mult_final_fa_mif.run_without_submitting = True
    dmripipeline.connect([(single_fiber_voxel_mask, single_fiber_voxel_mask_mult_final_fa_mif, [('out_file', 'in_file')])])
    
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
      Estimation of the response function
     Estimation of the constrained spherical deconvolution depends on the estimate of the response function
     ::For damaged or pathological brains one should take care to lower the maximum harmonic order of these steps.
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    estimateresponse = pe.Node(interface=mrtrix.EstimateResponseForSH(), name='43_estimateresponse')
    estimateresponse.inputs.maximum_harmonic_order = 8
    estimateresponse.inputs.out_filename = subject_ID + '_ER.txt'
    dmripipeline.connect([(eddy_corrected_dmri, estimateresponse, [("outputnode.eddy_corrected", "in_file")])])
    dmripipeline.connect([(fsl2mrtrix, estimateresponse, [("encoding_file", "encoding_file")])])
    dmripipeline.connect([(single_fiber_voxel_mask_mult_final_fa_mif, estimateresponse, [("converted", "mask_image")])])
    
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Constrained spherical deconvolution fODF
    - also get direction of the maximum peaks and obtain a f0df max amplitude image, and a threshold of 0.2
    - this will be used to further eliminate noise voxels for the seed image
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    csdeconv = pe.Node(interface=mrtrix.ConstrainedSphericalDeconvolution(), name='44_csdeconv')
    csdeconv.inputs.maximum_harmonic_order = 8
    csdeconv.inputs.out_filename  = subject_ID + '_CSD.mif'
    dmripipeline.connect([(eddy_corrected_dmri, csdeconv, [("outputnode.eddy_corrected", "in_file")])])
    dmripipeline.connect([(fa_wm_final, csdeconv, [("out_file", "mask_image")])])
    dmripipeline.connect([(estimateresponse, csdeconv, [("response", "response_file")])])
    dmripipeline.connect([(fsl2mrtrix, csdeconv, [("encoding_file", "encoding_file")])])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Detect voxels with odf of less than 0.2 amplitude and obtain a mask of voxels above that value
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    direction_prior = pe.Node(interface=mrtrix.GenerateDirections(), name='45_priordirs')
    direction_prior.inputs.num_dirs = 100
#     direction_prior.inputs.out_file = "directions_100.txt"

    csd_peaks = pe.Node(interface=mrtrix.FindShPeaks(), name='46_csd_peaks')
    csd_peaks.inputs.num_peaks = 1
    csd_peaks.inputs.out_file = subject_ID + '_CSD_peaks.mif'
    dmripipeline.connect(csdeconv, "spherical_harmonics_image", csd_peaks, "in_file")
    dmripipeline.connect(direction_prior, "out_file", csd_peaks, "directions_file")
    
    csd_amplitude = pe.Node(interface=mrtrix.Directions2Amplitude(), name='47_csd_amplitude')
    csd_amplitude.inputs.out_file = subject_ID + '_CSD_amplitude.mif'
    csd_amplitude.run_without_submitting = True
    dmripipeline.connect(csd_peaks, "out_file", csd_amplitude, "in_file")
    
    csd_amplitude_nii = pe.Node(interface=mrtrix.MRConvert(), name='47_csd_amplitude_nii')
    csd_amplitude_nii.inputs.out_filename = subject_ID + '_CSD_amplitude.nii'
    csd_amplitude_nii.inputs.extension = 'nii'
    csd_amplitude_nii.run_without_submitting = True
    dmripipeline.connect(csd_amplitude, "out_file", csd_amplitude_nii, "in_file")
    
    csd_amplitude_mask = pe.Node(interface=fsl.maths.MathsCommand(), name='48_csd_amplitude_mask')
    csd_amplitude_mask.inputs.args = '-thr 0.25 -bin'
    csd_amplitude_mask.inputs.out_file = subject_ID + '_CSD_amplitude_mask025.nii'
    csd_amplitude_mask.run_without_submitting = True
    dmripipeline.connect(csd_amplitude_nii, "converted", csd_amplitude_mask, "in_file")
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Eliminate noisy csf voxels from the mask, eliminate unconnected vioxels and divide the interface for left and right hemisphere
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    interface_denoised = pe.Node(interface=fsl.maths.ApplyMask(), name='50_interface_denoised')
    interface_denoised.inputs.out_file = subject_ID + '_interface_denoised.nii'
    interface_denoised.run_without_submitting = True
    dmripipeline.connect(interface_nocsf, "out_file", interface_denoised, "in_file")
    dmripipeline.connect(csd_amplitude_mask, "out_file", interface_denoised, "mask_file")

    interface_connectedcomp_mask = pe.Node(interface=fsl.maths.BinaryMaths(), name='51_interface_connectedcomp_mask')
    interface_connectedcomp_mask.inputs.operation = 'add'
    interface_connectedcomp_mask.inputs.args = '-binv -fillh26 -binv'
    interface_connectedcomp_mask.run_without_submitting = True
    dmripipeline.connect(interface_denoised,'out_file', interface_connectedcomp_mask,'in_file')
    dmripipeline.connect(inputnode,'lateral_line', interface_connectedcomp_mask, 'operand_file')
    
    interface_all = pe.Node(interface=fsl.maths.ApplyMask(), name='52_interface_all')
    interface_all.inputs.out_file = subject_ID + '_interface_all.nii'
    interface_all.run_without_submitting = True
    dmripipeline.connect(interface_denoised, "out_file", interface_all, "in_file")
    dmripipeline.connect(interface_connectedcomp_mask, "out_file", interface_all, "mask_file")
    
    interface_left = pe.Node(interface=fsl.maths.ApplyMask(), name='53_interface_left')
    interface_left.inputs.out_file = subject_ID + '_interface_left.nii'
    interface_left.run_without_submitting = True
    dmripipeline.connect([(interface_all, interface_left , [("out_file", "in_file")])])
    dmripipeline.connect([(ribbon_left_hemi, interface_left , [("out_file", "mask_file")])])
    
    interface_right = pe.Node(interface=fsl.maths.ApplyMask(), name='53_interface_right')
    interface_right.inputs.out_file = subject_ID + '_interface_right.nii'
    interface_right.run_without_submitting = True
    dmripipeline.connect([(interface_all, interface_right , [("out_file", "in_file")])])
    dmripipeline.connect([(ribbon_right_hemi, interface_right , [("out_file", "mask_file")])])
    
    
    """
    Get the voxel coordinates from the file, and transfrom them to mm coordinates for mrtrix
    text fiels with the coordinates are saved.
    Also, the voxels are ordered by z,y,x. that is, all the voxels in one slice are contigous
    """
    
    interface_voxels_left = pe.Node(interface=Function(input_names=["interface_file","outfile_prefix","return_sample"], output_names=["voxel_file","mm_file","mrtrix_file","voxel_list"], function=get_voxels), name='54_interface_voxels_left')
    interface_voxels_left.inputs.outfile_prefix = subject_ID + '_interface_left'
    interface_voxels_left.run_without_submitting = True
    dmripipeline.connect([(interface_left, interface_voxels_left, [("out_file", "interface_file")])])
    
    interface_voxels_right = interface_voxels_left.clone(name='54_interface_voxels_right')
    interface_voxels_right.inputs.outfile_prefix = subject_ID + '_interface_right'
    dmripipeline.connect([(interface_right, interface_voxels_right, [("out_file", "interface_file")])])
    
    """
    create an image where each seed voxel has a value equal to its id#
    """
    index_image_left = pe.Node(interface=Function(input_names=["in_seed_file","seedvoxel_list","outfile_prefix"],output_names=["out_index_file","index_list"],function=assign_voxel_ids), name='55_index_image_left')
    index_image_left.inputs.outfile_prefix = subject_ID + '_interface_left'
    index_image_left.run_without_submitting = True
    dmripipeline.connect([(interface_left, index_image_left, [("out_file", "in_seed_file")])])
    dmripipeline.connect([(interface_voxels_left, index_image_left, [("voxel_list", "seedvoxel_list")])])
    
    index_image_right = index_image_left.clone(name='55_index_image_right')
    index_image_right.inputs.outfile_prefix = subject_ID + '_interface_right'
    dmripipeline.connect([(interface_right, index_image_right, [("out_file", "in_seed_file")])])
    dmripipeline.connect([(interface_voxels_right, index_image_right, [("voxel_list", "seedvoxel_list")])])

    
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Probabilistic tracking using the obtained fODF
    Pay Attention to the number of tracts sampled from each voxel. Good values are usally in the millions (overall).
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    """
    overal tracking of a small 10000 use_sample in whole white matter to verify data is correct
    """
    probCSDstreamtrack_overall = pe.Node(interface=mrtrix.ProbabilisticSphericallyDeconvolutedStreamlineTrack(), name='61_probCSDstreamtrack_overall')
    probCSDstreamtrack_overall.inputs.inputmodel = 'SD_PROB'
    probCSDstreamtrack_overall.inputs.desired_number_of_tracks = 50000
    probCSDstreamtrack_overall.inputs.maximum_number_of_tracks = 75000
    dmripipeline.connect([(fa_wm_final, probCSDstreamtrack_overall, [("out_file", "mask_file")])])
    dmripipeline.connect([(fa_wm_final, probCSDstreamtrack_overall, [("out_file", "seed_file")])])
    dmripipeline.connect([(csdeconv, probCSDstreamtrack_overall, [("spherical_harmonics_image", "in_file")])])
    
    tracks2prob_overall = pe.Node(interface=mrtrix.Tracks2Prob(), name='62_tracks2prob_overall')
    tracks2prob_overall.inputs.out_filename = subject_ID + '_tract_wm_50000.nii'
    tracks2prob_overall.inputs.voxel_dims= [0.5,0.5,0.5]
    dmripipeline.connect([(probCSDstreamtrack_overall, tracks2prob_overall, [("tracked", "in_file")])])
    #dmripipeline.connect([(fa_wm_final, tracks2prob_overall, [("out_file", "template_file")])])
    

    """
    use a sink to save outputs
    """
    
    datasink = pe.Node(io.DataSink(), name='99_datasink')
    datasink.inputs.base_directory = output_dir
    datasink.inputs.container = subject_ID
    datasink.inputs.parameterization = True
    datasink.run_without_submitting = True
     
    dmripipeline.connect(eddy_corrected_dmri, 'outputnode.eddy_corrected', datasink, 'diff_data')
    dmripipeline.connect(corrected_b0, 'output_image', datasink, 'diff_data.@2')
    dmripipeline.connect(corrected_bvalues, 'out_file', datasink, 'diff_data.@3')
    dmripipeline.connect(fsl2mrtrix, 'encoding_file', datasink, 'diff_data.@4')
    dmripipeline.connect(dwi2tensor, 'tensor', datasink, 'diff_data.@5')
     
    dmripipeline.connect(t1_nii, 'out_file', datasink, 'anatomy')
    dmripipeline.connect(ribbon_nii, 'out_file', datasink, 'anatomy.@2')
    dmripipeline.connect(t1_ribbon_masked, 'out_file', datasink, 'anatomy.@3')
    dmripipeline.connect(flirt_t1masked_2_FAmasked, 'out_file', datasink, 'anatomy.@4')
    dmripipeline.connect(flirt_t1masked_2_FAmasked, 'out_matrix_file', datasink, 'anatomy.@5')
    dmripipeline.connect(ants_FA2T1m_2_T1_full, 'warp_transform', datasink, 'anatomy.@6')
    dmripipeline.connect(ants_FA2T1m_2_T1_full, 'inverse_warp_transform', datasink, 'anatomy.@7')
    dmripipeline.connect(ribbon_warped_2_fa, 'output_image', datasink, 'anatomy.@8')
    dmripipeline.connect(t1_warped_2_fa, 'output_image', datasink, 'anatomy.@9')
    dmripipeline.connect(invert_linearxfm_t1_2_fa, 'out_file', datasink, 'anatomy.@10')
    
     
    dmripipeline.connect(full_fa, 'converted', datasink, 'fa_masking')
    dmripipeline.connect(ribbon_fullmask, 'out_file', datasink, 'fa_masking.@2')
    dmripipeline.connect(ribbon_left_hemi, 'out_file', datasink, 'fa_masking.@3')
    dmripipeline.connect(ribbon_right_hemi, 'out_file', datasink, 'fa_masking.@4')
    dmripipeline.connect(fa_nl_ribbon_masked, 'out_file', datasink, 'fa_masking.@5')
    dmripipeline.connect(fa_wm_final, 'out_file', datasink, 'fa_masking.@6')
    dmripipeline.connect(interface_all, 'out_file', datasink, 'fa_masking.@7')
    dmripipeline.connect(interface_left, 'out_file', datasink, 'fa_masking.@8')
    dmripipeline.connect(interface_right, 'out_file', datasink, 'fa_masking.@9')
    dmripipeline.connect(interface_voxels_left, 'voxel_file', datasink, 'fa_masking.@10')
    dmripipeline.connect(interface_voxels_left, 'mm_file', datasink, 'fa_masking.@11')
    dmripipeline.connect(interface_voxels_left, 'mrtrix_file', datasink, 'fa_masking.@12')
    dmripipeline.connect(interface_voxels_right, 'voxel_file', datasink, 'fa_masking.@13')
    dmripipeline.connect(interface_voxels_right, 'mm_file', datasink, 'fa_masking.@14')
    dmripipeline.connect(interface_voxels_right, 'mrtrix_file', datasink, 'fa_masking.@15') 
    dmripipeline.connect(index_image_left, 'out_index_file', datasink, 'fa_masking.@16')
    dmripipeline.connect(index_image_right, 'out_index_file', datasink, 'fa_masking.@17')
    dmripipeline.connect(wm_vista, 'out_file', datasink, 'fa_masking.@18')
    dmripipeline.connect(CSF_mask, 'out_file', datasink, 'fa_masking.@19')
         
    dmripipeline.connect(single_fiber_voxel_mask, 'out_file', datasink, 'diff_model')
    dmripipeline.connect(estimateresponse, 'response', datasink, 'diff_model.@2')
    dmripipeline.connect(csdeconv, 'spherical_harmonics_image', datasink, 'diff_model.@3') 
    dmripipeline.connect(probCSDstreamtrack_overall, 'tracked', datasink, 'diff_model.@4')
    dmripipeline.connect(tracks2prob_overall, 'tract_image', datasink, 'diff_model.@5')
    dmripipeline.connect(csd_amplitude_nii, 'converted', datasink, 'diff_model.@6')
    dmripipeline.connect(csd_amplitude_mask, 'out_file', datasink, 'diff_model.@7')
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    ===============================================================================
    Connecting the workflow
    ===============================================================================
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
    
    """
    Create a higher-level workflow
    ------------------------------
    Finally, we create another higher-level workflow to connect our dmripipeline workflow with the info and datagrabbing nodes
    declared at the beginning. Our tutorial is now extensible to any arbitrary number of subjects by simply adding
    their names to the subject list and their data to the proper folders.
    """
    
    connectprepro = pe.Workflow(name="dmri_pipe1_prepro")
    
    connectprepro.base_dir = op.abspath(workflow_dir + "/workflow_"+subject_ID )
    connectprepro.connect([(datasource, dmripipeline, [('dwi', 'inputnode.dwi'),('bvals', 'inputnode.bvals'),('bvecs', 'inputnode.bvecs')]),
                           (auxsource, dmripipeline, [('lateral_line', 'inputnode.lateral_line')])])


    return connectprepro
def create_connectivity_pipeline(name="connectivity",
                                 parcellation_name='scale500'):
    inputnode_within = pe.Node(util.IdentityInterface(fields=[
        "subject_id", "dwi", "bvecs", "bvals", "subjects_dir",
        "resolution_network_file"
    ]),
                               name="inputnode_within")

    FreeSurferSource = pe.Node(interface=nio.FreeSurferSource(),
                               name='fssource')
    FreeSurferSourceLH = pe.Node(interface=nio.FreeSurferSource(),
                                 name='fssourceLH')
    FreeSurferSourceLH.inputs.hemi = 'lh'

    FreeSurferSourceRH = pe.Node(interface=nio.FreeSurferSource(),
                                 name='fssourceRH')
    FreeSurferSourceRH.inputs.hemi = 'rh'
    """
    Creating the workflow's nodes
    =============================
    """
    """
    Conversion nodes
    ----------------
    """
    """
    A number of conversion operations are required to obtain NIFTI files from the FreesurferSource for each subject.
    Nodes are used to convert the following:
        * Original structural image to NIFTI
        * Pial, white, inflated, and spherical surfaces for both the left and right hemispheres are converted to GIFTI for visualization in ConnectomeViewer
        * Parcellated annotation files for the left and right hemispheres are also converted to GIFTI

    """

    mri_convert_Brain = pe.Node(interface=fs.MRIConvert(),
                                name='mri_convert_Brain')
    mri_convert_Brain.inputs.out_type = 'nii'
    mri_convert_ROI_scale500 = mri_convert_Brain.clone(
        'mri_convert_ROI_scale500')

    mris_convertLH = pe.Node(interface=fs.MRIsConvert(), name='mris_convertLH')
    mris_convertLH.inputs.out_datatype = 'gii'
    mris_convertRH = mris_convertLH.clone('mris_convertRH')
    mris_convertRHwhite = mris_convertLH.clone('mris_convertRHwhite')
    mris_convertLHwhite = mris_convertLH.clone('mris_convertLHwhite')
    mris_convertRHinflated = mris_convertLH.clone('mris_convertRHinflated')
    mris_convertLHinflated = mris_convertLH.clone('mris_convertLHinflated')
    mris_convertRHsphere = mris_convertLH.clone('mris_convertRHsphere')
    mris_convertLHsphere = mris_convertLH.clone('mris_convertLHsphere')
    mris_convertLHlabels = mris_convertLH.clone('mris_convertLHlabels')
    mris_convertRHlabels = mris_convertLH.clone('mris_convertRHlabels')
    """
    Diffusion processing nodes
    --------------------------

    .. seealso::

        dmri_mrtrix_dti.py
            Tutorial that focuses solely on the MRtrix diffusion processing

        http://www.brain.org.au/software/mrtrix/index.html
            MRtrix's online documentation
    """
    """
    b-values and b-vectors stored in FSL's format are converted into a single encoding file for MRTrix.
    """

    fsl2mrtrix = pe.Node(interface=mrtrix.FSL2MRTrix(), name='fsl2mrtrix')
    """
    Distortions induced by eddy currents are corrected prior to fitting the tensors.
    The first image is used as a reference for which to warp the others.
    """

    eddycorrect = create_eddy_correct_pipeline(name='eddycorrect')
    eddycorrect.inputs.inputnode.ref_num = 1
    """
    Tensors are fitted to each voxel in the diffusion-weighted image and from these three maps are created:
        * Major eigenvector in each voxel
        * Apparent diffusion coefficient
        * Fractional anisotropy
    """

    dwi2tensor = pe.Node(interface=mrtrix.DWI2Tensor(), name='dwi2tensor')
    tensor2vector = pe.Node(interface=mrtrix.Tensor2Vector(),
                            name='tensor2vector')
    tensor2adc = pe.Node(interface=mrtrix.Tensor2ApparentDiffusion(),
                         name='tensor2adc')
    tensor2fa = pe.Node(interface=mrtrix.Tensor2FractionalAnisotropy(),
                        name='tensor2fa')
    MRconvert_fa = pe.Node(interface=mrtrix.MRConvert(), name='MRconvert_fa')
    MRconvert_fa.inputs.extension = 'nii'
    """

    These nodes are used to create a rough brain mask from the b0 image.
    The b0 image is extracted from the original diffusion-weighted image,
    put through a simple thresholding routine, and smoothed using a 3x3 median filter.
    """

    MRconvert = pe.Node(interface=mrtrix.MRConvert(), name='MRconvert')
    MRconvert.inputs.extract_at_axis = 3
    MRconvert.inputs.extract_at_coordinate = [0]
    threshold_b0 = pe.Node(interface=mrtrix.Threshold(), name='threshold_b0')
    median3d = pe.Node(interface=mrtrix.MedianFilter3D(), name='median3d')
    """
    The brain mask is also used to help identify single-fiber voxels.
    This is done by passing the brain mask through two erosion steps,
    multiplying the remaining mask with the fractional anisotropy map, and
    thresholding the result to obtain some highly anisotropic within-brain voxels.
    """

    erode_mask_firstpass = pe.Node(interface=mrtrix.Erode(),
                                   name='erode_mask_firstpass')
    erode_mask_secondpass = pe.Node(interface=mrtrix.Erode(),
                                    name='erode_mask_secondpass')
    MRmultiply = pe.Node(interface=mrtrix.MRMultiply(), name='MRmultiply')
    MRmult_merge = pe.Node(interface=util.Merge(2), name='MRmultiply_merge')
    threshold_FA = pe.Node(interface=mrtrix.Threshold(), name='threshold_FA')
    threshold_FA.inputs.absolute_threshold_value = 0.7
    """
    For whole-brain tracking we also require a broad white-matter seed mask.
    This is created by generating a white matter mask, given a brainmask, and
    thresholding it at a reasonably high level.
    """

    bet = pe.Node(interface=fsl.BET(mask=True), name='bet_b0')
    gen_WM_mask = pe.Node(interface=mrtrix.GenerateWhiteMatterMask(),
                          name='gen_WM_mask')
    threshold_wmmask = pe.Node(interface=mrtrix.Threshold(),
                               name='threshold_wmmask')
    threshold_wmmask.inputs.absolute_threshold_value = 0.4
    """
    The spherical deconvolution step depends on the estimate of the response function
    in the highly anisotropic voxels we obtained above.

    .. warning::

        For damaged or pathological brains one should take care to lower the maximum harmonic order of these steps.

    """

    estimateresponse = pe.Node(interface=mrtrix.EstimateResponseForSH(),
                               name='estimateresponse')
    estimateresponse.inputs.maximum_harmonic_order = 6
    csdeconv = pe.Node(interface=mrtrix.ConstrainedSphericalDeconvolution(),
                       name='csdeconv')
    csdeconv.inputs.maximum_harmonic_order = 6
    """
    Finally, we track probabilistically using the orientation distribution functions obtained earlier.
    The tracts are then used to generate a tract-density image, and they are also converted to TrackVis format.
    """

    probCSDstreamtrack = pe.Node(
        interface=mrtrix.ProbabilisticSphericallyDeconvolutedStreamlineTrack(),
        name='probCSDstreamtrack')
    probCSDstreamtrack.inputs.inputmodel = 'SD_PROB'
    probCSDstreamtrack.inputs.desired_number_of_tracks = 150000
    tracks2prob = pe.Node(interface=mrtrix.Tracks2Prob(), name='tracks2prob')
    tracks2prob.inputs.colour = True
    MRconvert_tracks2prob = MRconvert_fa.clone(name='MRconvert_tracks2prob')
    tck2trk = pe.Node(interface=mrtrix.MRTrix2TrackVis(), name='tck2trk')
    """
    Structural segmentation nodes
    -----------------------------
    """
    """
    The following node identifies the transformation between the diffusion-weighted
    image and the structural image. This transformation is then applied to the tracts
    so that they are in the same space as the regions of interest.
    """

    coregister = pe.Node(interface=fsl.FLIRT(dof=6), name='coregister')
    coregister.inputs.cost = ('normmi')
    """
    Parcellation is performed given the aparc+aseg image from Freesurfer.
    The CMTK Parcellation step subdivides these regions to return a higher-resolution parcellation scheme.
    The parcellation used here is entitled "scale500" and returns 1015 regions.
    """

    parcellate = pe.Node(interface=cmtk.Parcellate(), name="Parcellate")
    parcellate.inputs.parcellation_name = parcellation_name
    """
    The CreateMatrix interface takes in the remapped aparc+aseg image as well as the label dictionary and fiber tracts
    and outputs a number of different files. The most important of which is the connectivity network itself, which is stored
    as a 'gpickle' and can be loaded using Python's NetworkX package (see CreateMatrix docstring). Also outputted are various
    NumPy arrays containing detailed tract information, such as the start and endpoint regions, and statistics on the mean and
    standard deviation for the fiber length of each connection. These matrices can be used in the ConnectomeViewer to plot the
    specific tracts that connect between user-selected regions.

    Here we choose the Lausanne2008 parcellation scheme, since we are incorporating the CMTK parcellation step.
    """

    creatematrix = pe.Node(interface=cmtk.CreateMatrix(), name="CreateMatrix")
    creatematrix.inputs.count_region_intersections = True
    """
    Next we define the endpoint of this tutorial, which is the CFFConverter node, as well as a few nodes which use
    the Nipype Merge utility. These are useful for passing lists of the files we want packaged in our CFF file.
    The inspect.getfile command is used to package this script into the resulting CFF file, so that it is easy to
    look back at the processing parameters that were used.
    """

    CFFConverter = pe.Node(interface=cmtk.CFFConverter(), name="CFFConverter")
    CFFConverter.inputs.script_files = op.abspath(
        inspect.getfile(inspect.currentframe()))
    giftiSurfaces = pe.Node(interface=util.Merge(8), name="GiftiSurfaces")
    giftiLabels = pe.Node(interface=util.Merge(2), name="GiftiLabels")
    niftiVolumes = pe.Node(interface=util.Merge(3), name="NiftiVolumes")
    fiberDataArrays = pe.Node(interface=util.Merge(4), name="FiberDataArrays")
    """
    We also create a node to calculate several network metrics on our resulting file, and another CFF converter
    which will be used to package these networks into a single file.
    """

    networkx = create_networkx_pipeline(name='networkx')
    cmats_to_csv = create_cmats_to_csv_pipeline(name='cmats_to_csv')
    nfibs_to_csv = pe.Node(interface=misc.Matlab2CSV(), name='nfibs_to_csv')
    merge_nfib_csvs = pe.Node(interface=misc.MergeCSVFiles(),
                              name='merge_nfib_csvs')
    merge_nfib_csvs.inputs.extra_column_heading = 'Subject'
    merge_nfib_csvs.inputs.out_file = 'fibers.csv'
    NxStatsCFFConverter = pe.Node(interface=cmtk.CFFConverter(),
                                  name="NxStatsCFFConverter")
    NxStatsCFFConverter.inputs.script_files = op.abspath(
        inspect.getfile(inspect.currentframe()))
    """
    Connecting the workflow
    =======================
    Here we connect our processing pipeline.
    """
    """
    Connecting the inputs, FreeSurfer nodes, and conversions
    --------------------------------------------------------
    """

    mapping = pe.Workflow(name='mapping')
    """
    First, we connect the input node to the FreeSurfer input nodes.
    """

    mapping.connect([(inputnode_within, FreeSurferSource, [("subjects_dir",
                                                            "subjects_dir")])])
    mapping.connect([(inputnode_within, FreeSurferSource, [("subject_id",
                                                            "subject_id")])])

    mapping.connect([(inputnode_within, FreeSurferSourceLH,
                      [("subjects_dir", "subjects_dir")])])
    mapping.connect([(inputnode_within, FreeSurferSourceLH, [("subject_id",
                                                              "subject_id")])])

    mapping.connect([(inputnode_within, FreeSurferSourceRH,
                      [("subjects_dir", "subjects_dir")])])
    mapping.connect([(inputnode_within, FreeSurferSourceRH, [("subject_id",
                                                              "subject_id")])])

    mapping.connect([(inputnode_within, parcellate, [("subjects_dir",
                                                      "subjects_dir")])])
    mapping.connect([(inputnode_within, parcellate, [("subject_id",
                                                      "subject_id")])])
    mapping.connect([(parcellate, mri_convert_ROI_scale500, [('roi_file',
                                                              'in_file')])])
    """
    Nifti conversion for subject's stripped brain image from Freesurfer:
    """

    mapping.connect([(FreeSurferSource, mri_convert_Brain, [('brain',
                                                             'in_file')])])
    """
    Surface conversions to GIFTI (pial, white, inflated, and sphere for both hemispheres)
    """

    mapping.connect([(FreeSurferSourceLH, mris_convertLH, [('pial', 'in_file')
                                                           ])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRH, [('pial', 'in_file')
                                                           ])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHwhite, [('white',
                                                                 'in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHwhite, [('white',
                                                                 'in_file')])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHinflated,
                      [('inflated', 'in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHinflated,
                      [('inflated', 'in_file')])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHsphere,
                      [('sphere', 'in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHsphere,
                      [('sphere', 'in_file')])])
    """
    The annotation files are converted using the pial surface as a map via the MRIsConvert interface.
    One of the functions defined earlier is used to select the lh.aparc.annot and rh.aparc.annot files
    specifically (rather than e.g. rh.aparc.a2009s.annot) from the output list given by the FreeSurferSource.
    """

    mapping.connect([(FreeSurferSourceLH, mris_convertLHlabels,
                      [('pial', 'in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHlabels,
                      [('pial', 'in_file')])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHlabels,
                      [(('annot', select_aparc_annot), 'annot_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHlabels,
                      [(('annot', select_aparc_annot), 'annot_file')])])
    """
    Diffusion Processing
    --------------------
    Now we connect the tensor computations:
    """

    mapping.connect([(inputnode_within, fsl2mrtrix, [("bvecs", "bvec_file"),
                                                     ("bvals", "bval_file")])])
    mapping.connect([(inputnode_within, eddycorrect, [("dwi",
                                                       "inputnode.in_file")])])
    mapping.connect([(eddycorrect, dwi2tensor, [("outputnode.eddy_corrected",
                                                 "in_file")])])
    mapping.connect([(fsl2mrtrix, dwi2tensor, [("encoding_file",
                                                "encoding_file")])])

    mapping.connect([
        (dwi2tensor, tensor2vector, [['tensor', 'in_file']]),
        (dwi2tensor, tensor2adc, [['tensor', 'in_file']]),
        (dwi2tensor, tensor2fa, [['tensor', 'in_file']]),
    ])
    mapping.connect([(tensor2fa, MRmult_merge, [("FA", "in1")])])
    mapping.connect([(tensor2fa, MRconvert_fa, [("FA", "in_file")])])
    """

    This block creates the rough brain mask to be multiplied, mulitplies it with the
    fractional anisotropy image, and thresholds it to get the single-fiber voxels.
    """

    mapping.connect([(eddycorrect, MRconvert, [("outputnode.eddy_corrected",
                                                "in_file")])])
    mapping.connect([(MRconvert, threshold_b0, [("converted", "in_file")])])
    mapping.connect([(threshold_b0, median3d, [("out_file", "in_file")])])
    mapping.connect([(median3d, erode_mask_firstpass, [("out_file", "in_file")
                                                       ])])
    mapping.connect([(erode_mask_firstpass, erode_mask_secondpass,
                      [("out_file", "in_file")])])
    mapping.connect([(erode_mask_secondpass, MRmult_merge, [("out_file", "in2")
                                                            ])])
    mapping.connect([(MRmult_merge, MRmultiply, [("out", "in_files")])])
    mapping.connect([(MRmultiply, threshold_FA, [("out_file", "in_file")])])
    """
    Here the thresholded white matter mask is created for seeding the tractography.
    """

    mapping.connect([(eddycorrect, bet, [("outputnode.eddy_corrected",
                                          "in_file")])])
    mapping.connect([(eddycorrect, gen_WM_mask, [("outputnode.eddy_corrected",
                                                  "in_file")])])
    mapping.connect([(bet, gen_WM_mask, [("mask_file", "binary_mask")])])
    mapping.connect([(fsl2mrtrix, gen_WM_mask, [("encoding_file",
                                                 "encoding_file")])])
    mapping.connect([(gen_WM_mask, threshold_wmmask, [("WMprobabilitymap",
                                                       "in_file")])])
    """
    Next we estimate the fiber response distribution.
    """

    mapping.connect([(eddycorrect, estimateresponse,
                      [("outputnode.eddy_corrected", "in_file")])])
    mapping.connect([(fsl2mrtrix, estimateresponse, [("encoding_file",
                                                      "encoding_file")])])
    mapping.connect([(threshold_FA, estimateresponse, [("out_file",
                                                        "mask_image")])])
    """
    Run constrained spherical deconvolution.
    """

    mapping.connect([(eddycorrect, csdeconv, [("outputnode.eddy_corrected",
                                               "in_file")])])
    mapping.connect([(gen_WM_mask, csdeconv, [("WMprobabilitymap",
                                               "mask_image")])])
    mapping.connect([(estimateresponse, csdeconv, [("response",
                                                    "response_file")])])
    mapping.connect([(fsl2mrtrix, csdeconv, [("encoding_file", "encoding_file")
                                             ])])
    """
    Connect the tractography and compute the tract density image.
    """

    mapping.connect([(threshold_wmmask, probCSDstreamtrack, [("out_file",
                                                              "seed_file")])])
    mapping.connect([(csdeconv, probCSDstreamtrack,
                      [("spherical_harmonics_image", "in_file")])])
    mapping.connect([(probCSDstreamtrack, tracks2prob, [("tracked", "in_file")
                                                        ])])
    mapping.connect([(eddycorrect, tracks2prob, [("outputnode.eddy_corrected",
                                                  "template_file")])])
    mapping.connect([(tracks2prob, MRconvert_tracks2prob, [("tract_image",
                                                            "in_file")])])
    """
    Structural Processing
    ---------------------
    First, we coregister the diffusion image to the structural image
    """

    mapping.connect([(eddycorrect, coregister, [("outputnode.eddy_corrected",
                                                 "in_file")])])
    mapping.connect([(mri_convert_Brain, coregister, [('out_file', 'reference')
                                                      ])])
    """
    The MRtrix-tracked fibers are converted to TrackVis format (with voxel and data dimensions grabbed from the DWI).
    The connectivity matrix is created with the transformed .trk fibers and the parcellation file.
    """

    mapping.connect([(eddycorrect, tck2trk, [("outputnode.eddy_corrected",
                                              "image_file")])])
    mapping.connect([(mri_convert_Brain, tck2trk,
                      [("out_file", "registration_image_file")])])
    mapping.connect([(coregister, tck2trk, [("out_matrix_file", "matrix_file")
                                            ])])
    mapping.connect([(probCSDstreamtrack, tck2trk, [("tracked", "in_file")])])
    mapping.connect([(tck2trk, creatematrix, [("out_file", "tract_file")])])
    mapping.connect(inputnode_within, 'resolution_network_file', creatematrix,
                    'resolution_network_file')
    mapping.connect([(inputnode_within, creatematrix, [("subject_id",
                                                        "out_matrix_file")])])
    mapping.connect([(inputnode_within, creatematrix,
                      [("subject_id", "out_matrix_mat_file")])])
    mapping.connect([(parcellate, creatematrix, [("roi_file", "roi_file")])])
    """
    The merge nodes defined earlier are used here to create lists of the files which are
    destined for the CFFConverter.
    """

    mapping.connect([(mris_convertLH, giftiSurfaces, [("converted", "in1")])])
    mapping.connect([(mris_convertRH, giftiSurfaces, [("converted", "in2")])])
    mapping.connect([(mris_convertLHwhite, giftiSurfaces, [("converted", "in3")
                                                           ])])
    mapping.connect([(mris_convertRHwhite, giftiSurfaces, [("converted", "in4")
                                                           ])])
    mapping.connect([(mris_convertLHinflated, giftiSurfaces, [("converted",
                                                               "in5")])])
    mapping.connect([(mris_convertRHinflated, giftiSurfaces, [("converted",
                                                               "in6")])])
    mapping.connect([(mris_convertLHsphere, giftiSurfaces, [("converted",
                                                             "in7")])])
    mapping.connect([(mris_convertRHsphere, giftiSurfaces, [("converted",
                                                             "in8")])])

    mapping.connect([(mris_convertLHlabels, giftiLabels, [("converted", "in1")
                                                          ])])
    mapping.connect([(mris_convertRHlabels, giftiLabels, [("converted", "in2")
                                                          ])])

    mapping.connect([(parcellate, niftiVolumes, [("roi_file", "in1")])])
    mapping.connect([(eddycorrect, niftiVolumes, [("outputnode.eddy_corrected",
                                                   "in2")])])
    mapping.connect([(mri_convert_Brain, niftiVolumes, [("out_file", "in3")])])

    mapping.connect([(creatematrix, fiberDataArrays, [("endpoint_file", "in1")
                                                      ])])
    mapping.connect([(creatematrix, fiberDataArrays, [("endpoint_file_mm",
                                                       "in2")])])
    mapping.connect([(creatematrix, fiberDataArrays, [("fiber_length_file",
                                                       "in3")])])
    mapping.connect([(creatematrix, fiberDataArrays, [("fiber_label_file",
                                                       "in4")])])
    """
    This block actually connects the merged lists to the CFF converter. We pass the surfaces
    and volumes that are to be included, as well as the tracts and the network itself. The currently
    running pipeline (dmri_connectivity_advanced.py) is also scraped and included in the CFF file. This
    makes it easy for the user to examine the entire processing pathway used to generate the end
    product.
    """

    mapping.connect([(giftiSurfaces, CFFConverter, [("out", "gifti_surfaces")])
                     ])
    mapping.connect([(giftiLabels, CFFConverter, [("out", "gifti_labels")])])
    mapping.connect([(creatematrix, CFFConverter, [("matrix_files",
                                                    "gpickled_networks")])])
    mapping.connect([(niftiVolumes, CFFConverter, [("out", "nifti_volumes")])])
    mapping.connect([(fiberDataArrays, CFFConverter, [("out", "data_files")])])
    mapping.connect([(creatematrix, CFFConverter, [("filtered_tractography",
                                                    "tract_files")])])
    mapping.connect([(inputnode_within, CFFConverter, [("subject_id", "title")
                                                       ])])
    """
    The graph theoretical metrics which have been generated are placed into another CFF file.
    """

    mapping.connect([(inputnode_within, networkx,
                      [("subject_id", "inputnode.extra_field")])])
    mapping.connect([(creatematrix, networkx, [("intersection_matrix_file",
                                                "inputnode.network_file")])])

    mapping.connect([(networkx, NxStatsCFFConverter,
                      [("outputnode.network_files", "gpickled_networks")])])
    mapping.connect([(giftiSurfaces, NxStatsCFFConverter,
                      [("out", "gifti_surfaces")])])
    mapping.connect([(giftiLabels, NxStatsCFFConverter, [("out",
                                                          "gifti_labels")])])
    mapping.connect([(niftiVolumes, NxStatsCFFConverter, [("out",
                                                           "nifti_volumes")])])
    mapping.connect([(fiberDataArrays, NxStatsCFFConverter, [("out",
                                                              "data_files")])])
    mapping.connect([(inputnode_within, NxStatsCFFConverter, [("subject_id",
                                                               "title")])])

    mapping.connect([(inputnode_within, cmats_to_csv,
                      [("subject_id", "inputnode.extra_field")])])
    mapping.connect([(creatematrix, cmats_to_csv, [
        ("matlab_matrix_files", "inputnode.matlab_matrix_files")
    ])])
    mapping.connect([(creatematrix, nfibs_to_csv, [("stats_file", "in_file")])
                     ])
    mapping.connect([(nfibs_to_csv, merge_nfib_csvs, [("csv_files", "in_files")
                                                      ])])
    mapping.connect([(inputnode_within, merge_nfib_csvs, [("subject_id",
                                                           "extra_field")])])
    """
    Create a higher-level workflow
    --------------------------------------
    Finally, we create another higher-level workflow to connect our mapping workflow with the info and datagrabbing nodes
    declared at the beginning. Our tutorial can is now extensible to any arbitrary number of subjects by simply adding
    their names to the subject list and their data to the proper folders.
    """

    inputnode = pe.Node(interface=util.IdentityInterface(
        fields=["subject_id", "dwi", "bvecs", "bvals", "subjects_dir"]),
                        name="inputnode")

    outputnode = pe.Node(interface=util.IdentityInterface(fields=[
        "fa", "struct", "tracts", "tracks2prob", "connectome", "nxstatscff",
        "nxmatlab", "nxcsv", "fiber_csv", "cmatrices_csv", "nxmergedcsv",
        "cmatrix", "networks", "filtered_tracts", "rois", "odfs", "tdi",
        "mean_fiber_length", "median_fiber_length", "fiber_length_std"
    ]),
                         name="outputnode")

    connectivity = pe.Workflow(name="connectivity")
    connectivity.base_output_dir = name
    connectivity.base_dir = name

    connectivity.connect([(inputnode, mapping, [
        ("dwi", "inputnode_within.dwi"), ("bvals", "inputnode_within.bvals"),
        ("bvecs", "inputnode_within.bvecs"),
        ("subject_id", "inputnode_within.subject_id"),
        ("subjects_dir", "inputnode_within.subjects_dir")
    ])])

    connectivity.connect([(mapping, outputnode, [
        ("tck2trk.out_file", "tracts"),
        ("CFFConverter.connectome_file", "connectome"),
        ("NxStatsCFFConverter.connectome_file", "nxstatscff"),
        ("CreateMatrix.matrix_mat_file", "cmatrix"),
        ("CreateMatrix.mean_fiber_length_matrix_mat_file",
         "mean_fiber_length"),
        ("CreateMatrix.median_fiber_length_matrix_mat_file",
         "median_fiber_length"),
        ("CreateMatrix.fiber_length_std_matrix_mat_file", "fiber_length_std"),
        ("CreateMatrix.matrix_files", "networks"),
        ("CreateMatrix.filtered_tractographies", "filtered_tracts"),
        ("merge_nfib_csvs.csv_file", "fiber_csv"),
        ("mri_convert_ROI_scale500.out_file", "rois"),
        ("csdeconv.spherical_harmonics_image", "odfs"),
        ("mri_convert_Brain.out_file", "struct"),
        ("MRconvert_fa.converted", "fa"),
        ("MRconvert_tracks2prob.converted", "tracks2prob")
    ])])

    connectivity.connect([(cmats_to_csv, outputnode, [("outputnode.csv_file",
                                                       "cmatrices_csv")])])
    connectivity.connect([(networkx, outputnode, [("outputnode.csv_files",
                                                   "nxcsv")])])
    return connectivity
get_subject_data_interface = util.Function(input_names=["subject_id", "data_file"],
  output_names=["dose", "weight", "delay", "glycemie", "scan_time"], function=return_subject_data)
grab_subject_data = pe.Node(interface=get_subject_data_interface, name='grab_subject_data')
grab_subject_data.inputs.data_file = op.join(data_path, "SubjectData.csv")

datasink_step1 = pe.Node(interface=nio.DataSink(), name="datasink")
datasink_step1.inputs.base_directory = output_dir
datasink_step1.overwrite = True

workflow = pe.Workflow(name='ex_precoth1')
workflow.base_dir = output_dir
workflow.connect([(infosource, datasource_step1,[('subject_id', 'subject_id')])])

motioncorrect = create_motion_correct_pipeline(name='motioncorrect')
motioncorrect.inputs.inputnode.ref_num = 0
eddycorrect = create_eddy_correct_pipeline(name='eddycorrect')
eddycorrect.inputs.inputnode.ref_num = 0

workflow.connect([(datasource_step1, motioncorrect,[('dwi', 'inputnode.in_file')])])
workflow.connect([(motioncorrect, eddycorrect,[('outputnode.motion_corrected', 'inputnode.in_file')])])
workflow.connect([(eddycorrect, step1,[('outputnode.eddy_corrected', 'inputnode.dwi')])])
workflow.connect([(eddycorrect, datasink_step1,[('outputnode.eddy_corrected', '@subject_id.corrected_dwi')])])
workflow.write_graph()
#workflow.run()
#workflow.run(plugin='MultiProc', plugin_args={'n_procs' : 4})




workflow2 = pe.Workflow(name='ex_precoth2')
workflow2.base_dir = output_dir
Example #7
0
grab_subject_data = pe.Node(interface=get_subject_data_interface,
                            name='grab_subject_data')
grab_subject_data.inputs.data_file = op.join(data_path, "SubjectData.csv")

datasink_step1 = pe.Node(interface=nio.DataSink(), name="datasink")
datasink_step1.inputs.base_directory = output_dir
datasink_step1.overwrite = True

workflow = pe.Workflow(name='ex_precoth1')
workflow.base_dir = output_dir
workflow.connect([(infosource, datasource_step1, [('subject_id', 'subject_id')
                                                  ])])

motioncorrect = create_motion_correct_pipeline(name='motioncorrect')
motioncorrect.inputs.inputnode.ref_num = 0
eddycorrect = create_eddy_correct_pipeline(name='eddycorrect')
eddycorrect.inputs.inputnode.ref_num = 0

workflow.connect([(datasource_step1, motioncorrect, [('dwi',
                                                      'inputnode.in_file')])])
workflow.connect([(motioncorrect, eddycorrect, [('outputnode.motion_corrected',
                                                 'inputnode.in_file')])])
workflow.connect([(eddycorrect, step1, [('outputnode.eddy_corrected',
                                         'inputnode.dwi')])])
workflow.connect([(eddycorrect, datasink_step1, [
    ('outputnode.eddy_corrected', '@subject_id.corrected_dwi')
])])
workflow.write_graph()
#workflow.run()
#workflow.run(plugin='MultiProc', plugin_args={'n_procs' : 4})
def create_connectivity_pipeline(name="connectivity", parcellation_name='scale500'):
    inputnode_within = pe.Node(util.IdentityInterface(fields=["subject_id",
                                                              "dwi",
                                                              "bvecs",
                                                              "bvals",
                                                              "subjects_dir",
                                                              "resolution_network_file"]),
                               name="inputnode_within")

    FreeSurferSource = pe.Node(interface=nio.FreeSurferSource(), name='fssource')
    FreeSurferSourceLH = pe.Node(interface=nio.FreeSurferSource(), name='fssourceLH')
    FreeSurferSourceLH.inputs.hemi = 'lh'

    FreeSurferSourceRH = pe.Node(interface=nio.FreeSurferSource(), name='fssourceRH')
    FreeSurferSourceRH.inputs.hemi = 'rh'

    """
    Creating the workflow's nodes
    =============================
    """

    """
    Conversion nodes
    ----------------
    """

    """
    A number of conversion operations are required to obtain NIFTI files from the FreesurferSource for each subject.
    Nodes are used to convert the following:
        * Original structural image to NIFTI
        * Pial, white, inflated, and spherical surfaces for both the left and right hemispheres are converted to GIFTI for visualization in ConnectomeViewer
        * Parcellated annotation files for the left and right hemispheres are also converted to GIFTI

    """

    mri_convert_Brain = pe.Node(interface=fs.MRIConvert(), name='mri_convert_Brain')
    mri_convert_Brain.inputs.out_type = 'nii'
    mri_convert_ROI_scale500 = mri_convert_Brain.clone('mri_convert_ROI_scale500')

    mris_convertLH = pe.Node(interface=fs.MRIsConvert(), name='mris_convertLH')
    mris_convertLH.inputs.out_datatype = 'gii'
    mris_convertRH = mris_convertLH.clone('mris_convertRH')
    mris_convertRHwhite = mris_convertLH.clone('mris_convertRHwhite')
    mris_convertLHwhite = mris_convertLH.clone('mris_convertLHwhite')
    mris_convertRHinflated = mris_convertLH.clone('mris_convertRHinflated')
    mris_convertLHinflated = mris_convertLH.clone('mris_convertLHinflated')
    mris_convertRHsphere = mris_convertLH.clone('mris_convertRHsphere')
    mris_convertLHsphere = mris_convertLH.clone('mris_convertLHsphere')
    mris_convertLHlabels = mris_convertLH.clone('mris_convertLHlabels')
    mris_convertRHlabels = mris_convertLH.clone('mris_convertRHlabels')

    """
    Diffusion processing nodes
    --------------------------

    .. seealso::

        dmri_mrtrix_dti.py
            Tutorial that focuses solely on the MRtrix diffusion processing

        http://www.brain.org.au/software/mrtrix/index.html
            MRtrix's online documentation
    """

    """
    b-values and b-vectors stored in FSL's format are converted into a single encoding file for MRTrix.
    """

    fsl2mrtrix = pe.Node(interface=mrtrix.FSL2MRTrix(),name='fsl2mrtrix')

    """
    Distortions induced by eddy currents are corrected prior to fitting the tensors.
    The first image is used as a reference for which to warp the others.
    """

    eddycorrect = create_eddy_correct_pipeline(name='eddycorrect')
    eddycorrect.inputs.inputnode.ref_num = 1

    """
    Tensors are fitted to each voxel in the diffusion-weighted image and from these three maps are created:
        * Major eigenvector in each voxel
        * Apparent diffusion coefficient
        * Fractional anisotropy
    """

    dwi2tensor = pe.Node(interface=mrtrix.DWI2Tensor(),name='dwi2tensor')
    tensor2vector = pe.Node(interface=mrtrix.Tensor2Vector(),name='tensor2vector')
    tensor2adc = pe.Node(interface=mrtrix.Tensor2ApparentDiffusion(),name='tensor2adc')
    tensor2fa = pe.Node(interface=mrtrix.Tensor2FractionalAnisotropy(),name='tensor2fa')
    MRconvert_fa = pe.Node(interface=mrtrix.MRConvert(),name='MRconvert_fa')
    MRconvert_fa.inputs.extension = 'nii'

    """

    These nodes are used to create a rough brain mask from the b0 image.
    The b0 image is extracted from the original diffusion-weighted image,
    put through a simple thresholding routine, and smoothed using a 3x3 median filter.
    """

    MRconvert = pe.Node(interface=mrtrix.MRConvert(),name='MRconvert')
    MRconvert.inputs.extract_at_axis = 3
    MRconvert.inputs.extract_at_coordinate = [0]
    threshold_b0 = pe.Node(interface=mrtrix.Threshold(),name='threshold_b0')
    median3d = pe.Node(interface=mrtrix.MedianFilter3D(),name='median3d')

    """
    The brain mask is also used to help identify single-fiber voxels.
    This is done by passing the brain mask through two erosion steps,
    multiplying the remaining mask with the fractional anisotropy map, and
    thresholding the result to obtain some highly anisotropic within-brain voxels.
    """

    erode_mask_firstpass = pe.Node(interface=mrtrix.Erode(),name='erode_mask_firstpass')
    erode_mask_secondpass = pe.Node(interface=mrtrix.Erode(),name='erode_mask_secondpass')
    MRmultiply = pe.Node(interface=mrtrix.MRMultiply(),name='MRmultiply')
    MRmult_merge = pe.Node(interface=util.Merge(2), name='MRmultiply_merge')
    threshold_FA = pe.Node(interface=mrtrix.Threshold(),name='threshold_FA')
    threshold_FA.inputs.absolute_threshold_value = 0.7

    """
    For whole-brain tracking we also require a broad white-matter seed mask.
    This is created by generating a white matter mask, given a brainmask, and
    thresholding it at a reasonably high level.
    """

    bet = pe.Node(interface=fsl.BET(mask = True), name = 'bet_b0')
    gen_WM_mask = pe.Node(interface=mrtrix.GenerateWhiteMatterMask(),name='gen_WM_mask')
    threshold_wmmask = pe.Node(interface=mrtrix.Threshold(),name='threshold_wmmask')
    threshold_wmmask.inputs.absolute_threshold_value = 0.4

    """
    The spherical deconvolution step depends on the estimate of the response function
    in the highly anisotropic voxels we obtained above.

    .. warning::

        For damaged or pathological brains one should take care to lower the maximum harmonic order of these steps.

    """

    estimateresponse = pe.Node(interface=mrtrix.EstimateResponseForSH(),name='estimateresponse')
    estimateresponse.inputs.maximum_harmonic_order = 6
    csdeconv = pe.Node(interface=mrtrix.ConstrainedSphericalDeconvolution(),name='csdeconv')
    csdeconv.inputs.maximum_harmonic_order = 6

    """
    Finally, we track probabilistically using the orientation distribution functions obtained earlier.
    The tracts are then used to generate a tract-density image, and they are also converted to TrackVis format.
    """

    probCSDstreamtrack = pe.Node(interface=mrtrix.ProbabilisticSphericallyDeconvolutedStreamlineTrack(),name='probCSDstreamtrack')
    probCSDstreamtrack.inputs.inputmodel = 'SD_PROB'
    probCSDstreamtrack.inputs.desired_number_of_tracks = 150000
    tracks2prob = pe.Node(interface=mrtrix.Tracks2Prob(),name='tracks2prob')
    tracks2prob.inputs.colour = True
    MRconvert_tracks2prob = MRconvert_fa.clone(name='MRconvert_tracks2prob')
    tck2trk = pe.Node(interface=mrtrix.MRTrix2TrackVis(),name='tck2trk')

    """
    Structural segmentation nodes
    -----------------------------
    """

    """
    The following node identifies the transformation between the diffusion-weighted
    image and the structural image. This transformation is then applied to the tracts
    so that they are in the same space as the regions of interest.
    """

    coregister = pe.Node(interface=fsl.FLIRT(dof=6), name = 'coregister')
    coregister.inputs.cost = ('normmi')

    """
    Parcellation is performed given the aparc+aseg image from Freesurfer.
    The CMTK Parcellation step subdivides these regions to return a higher-resolution parcellation scheme.
    The parcellation used here is entitled "scale500" and returns 1015 regions.
    """

    parcellate = pe.Node(interface=cmtk.Parcellate(), name="Parcellate")
    parcellate.inputs.parcellation_name = parcellation_name

    """
    The CreateMatrix interface takes in the remapped aparc+aseg image as well as the label dictionary and fiber tracts
    and outputs a number of different files. The most important of which is the connectivity network itself, which is stored
    as a 'gpickle' and can be loaded using Python's NetworkX package (see CreateMatrix docstring). Also outputted are various
    NumPy arrays containing detailed tract information, such as the start and endpoint regions, and statistics on the mean and
    standard deviation for the fiber length of each connection. These matrices can be used in the ConnectomeViewer to plot the
    specific tracts that connect between user-selected regions.

    Here we choose the Lausanne2008 parcellation scheme, since we are incorporating the CMTK parcellation step.
    """

    creatematrix = pe.Node(interface=cmtk.CreateMatrix(), name="CreateMatrix")
    creatematrix.inputs.count_region_intersections = True

    """
    Next we define the endpoint of this tutorial, which is the CFFConverter node, as well as a few nodes which use
    the Nipype Merge utility. These are useful for passing lists of the files we want packaged in our CFF file.
    The inspect.getfile command is used to package this script into the resulting CFF file, so that it is easy to
    look back at the processing parameters that were used.
    """

    CFFConverter = pe.Node(interface=cmtk.CFFConverter(), name="CFFConverter")
    CFFConverter.inputs.script_files = op.abspath(inspect.getfile(inspect.currentframe()))
    giftiSurfaces = pe.Node(interface=util.Merge(8), name="GiftiSurfaces")
    giftiLabels = pe.Node(interface=util.Merge(2), name="GiftiLabels")
    niftiVolumes = pe.Node(interface=util.Merge(3), name="NiftiVolumes")
    fiberDataArrays = pe.Node(interface=util.Merge(4), name="FiberDataArrays")

    """
    We also create a node to calculate several network metrics on our resulting file, and another CFF converter
    which will be used to package these networks into a single file.
    """

    networkx = create_networkx_pipeline(name='networkx')
    cmats_to_csv = create_cmats_to_csv_pipeline(name='cmats_to_csv')
    nfibs_to_csv = pe.Node(interface=misc.Matlab2CSV(), name='nfibs_to_csv')
    merge_nfib_csvs = pe.Node(interface=misc.MergeCSVFiles(), name='merge_nfib_csvs')
    merge_nfib_csvs.inputs.extra_column_heading = 'Subject'
    merge_nfib_csvs.inputs.out_file = 'fibers.csv'
    NxStatsCFFConverter = pe.Node(interface=cmtk.CFFConverter(), name="NxStatsCFFConverter")
    NxStatsCFFConverter.inputs.script_files = op.abspath(inspect.getfile(inspect.currentframe()))

    """
    Connecting the workflow
    =======================
    Here we connect our processing pipeline.
    """


    """
    Connecting the inputs, FreeSurfer nodes, and conversions
    --------------------------------------------------------
    """

    mapping = pe.Workflow(name='mapping')

    """
    First, we connect the input node to the FreeSurfer input nodes.
    """

    mapping.connect([(inputnode_within, FreeSurferSource,[("subjects_dir","subjects_dir")])])
    mapping.connect([(inputnode_within, FreeSurferSource,[("subject_id","subject_id")])])

    mapping.connect([(inputnode_within, FreeSurferSourceLH,[("subjects_dir","subjects_dir")])])
    mapping.connect([(inputnode_within, FreeSurferSourceLH,[("subject_id","subject_id")])])

    mapping.connect([(inputnode_within, FreeSurferSourceRH,[("subjects_dir","subjects_dir")])])
    mapping.connect([(inputnode_within, FreeSurferSourceRH,[("subject_id","subject_id")])])

    mapping.connect([(inputnode_within, parcellate,[("subjects_dir","subjects_dir")])])
    mapping.connect([(inputnode_within, parcellate,[("subject_id","subject_id")])])
    mapping.connect([(parcellate, mri_convert_ROI_scale500,[('roi_file','in_file')])])

    """
    Nifti conversion for subject's stripped brain image from Freesurfer:
    """

    mapping.connect([(FreeSurferSource, mri_convert_Brain,[('brain','in_file')])])

    """
    Surface conversions to GIFTI (pial, white, inflated, and sphere for both hemispheres)
    """

    mapping.connect([(FreeSurferSourceLH, mris_convertLH,[('pial','in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRH,[('pial','in_file')])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHwhite,[('white','in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHwhite,[('white','in_file')])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHinflated,[('inflated','in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHinflated,[('inflated','in_file')])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHsphere,[('sphere','in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHsphere,[('sphere','in_file')])])

    """
    The annotation files are converted using the pial surface as a map via the MRIsConvert interface.
    One of the functions defined earlier is used to select the lh.aparc.annot and rh.aparc.annot files
    specifically (rather than e.g. rh.aparc.a2009s.annot) from the output list given by the FreeSurferSource.
    """

    mapping.connect([(FreeSurferSourceLH, mris_convertLHlabels,[('pial','in_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHlabels,[('pial','in_file')])])
    mapping.connect([(FreeSurferSourceLH, mris_convertLHlabels, [(('annot', select_aparc_annot), 'annot_file')])])
    mapping.connect([(FreeSurferSourceRH, mris_convertRHlabels, [(('annot', select_aparc_annot), 'annot_file')])])


    """
    Diffusion Processing
    --------------------
    Now we connect the tensor computations:
    """

    mapping.connect([(inputnode_within, fsl2mrtrix, [("bvecs", "bvec_file"),
                                                    ("bvals", "bval_file")])])
    mapping.connect([(inputnode_within, eddycorrect,[("dwi","inputnode.in_file")])])
    mapping.connect([(eddycorrect, dwi2tensor,[("outputnode.eddy_corrected","in_file")])])
    mapping.connect([(fsl2mrtrix, dwi2tensor,[("encoding_file","encoding_file")])])

    mapping.connect([(dwi2tensor, tensor2vector,[['tensor','in_file']]),
                           (dwi2tensor, tensor2adc,[['tensor','in_file']]),
                           (dwi2tensor, tensor2fa,[['tensor','in_file']]),
                          ])
    mapping.connect([(tensor2fa, MRmult_merge,[("FA","in1")])])
    mapping.connect([(tensor2fa, MRconvert_fa,[("FA","in_file")])])

    """

    This block creates the rough brain mask to be multiplied, mulitplies it with the
    fractional anisotropy image, and thresholds it to get the single-fiber voxels.
    """

    mapping.connect([(eddycorrect, MRconvert,[("outputnode.eddy_corrected","in_file")])])
    mapping.connect([(MRconvert, threshold_b0,[("converted","in_file")])])
    mapping.connect([(threshold_b0, median3d,[("out_file","in_file")])])
    mapping.connect([(median3d, erode_mask_firstpass,[("out_file","in_file")])])
    mapping.connect([(erode_mask_firstpass, erode_mask_secondpass,[("out_file","in_file")])])
    mapping.connect([(erode_mask_secondpass, MRmult_merge,[("out_file","in2")])])
    mapping.connect([(MRmult_merge, MRmultiply,[("out","in_files")])])
    mapping.connect([(MRmultiply, threshold_FA,[("out_file","in_file")])])

    """
    Here the thresholded white matter mask is created for seeding the tractography.
    """

    mapping.connect([(eddycorrect, bet,[("outputnode.eddy_corrected","in_file")])])
    mapping.connect([(eddycorrect, gen_WM_mask,[("outputnode.eddy_corrected","in_file")])])
    mapping.connect([(bet, gen_WM_mask,[("mask_file","binary_mask")])])
    mapping.connect([(fsl2mrtrix, gen_WM_mask,[("encoding_file","encoding_file")])])
    mapping.connect([(gen_WM_mask, threshold_wmmask,[("WMprobabilitymap","in_file")])])

    """
    Next we estimate the fiber response distribution.
    """

    mapping.connect([(eddycorrect, estimateresponse,[("outputnode.eddy_corrected","in_file")])])
    mapping.connect([(fsl2mrtrix, estimateresponse,[("encoding_file","encoding_file")])])
    mapping.connect([(threshold_FA, estimateresponse,[("out_file","mask_image")])])

    """
    Run constrained spherical deconvolution.
    """

    mapping.connect([(eddycorrect, csdeconv,[("outputnode.eddy_corrected","in_file")])])
    mapping.connect([(gen_WM_mask, csdeconv,[("WMprobabilitymap","mask_image")])])
    mapping.connect([(estimateresponse, csdeconv,[("response","response_file")])])
    mapping.connect([(fsl2mrtrix, csdeconv,[("encoding_file","encoding_file")])])

    """
    Connect the tractography and compute the tract density image.
    """

    mapping.connect([(threshold_wmmask, probCSDstreamtrack,[("out_file","seed_file")])])
    mapping.connect([(csdeconv, probCSDstreamtrack,[("spherical_harmonics_image","in_file")])])
    mapping.connect([(probCSDstreamtrack, tracks2prob,[("tracked","in_file")])])
    mapping.connect([(eddycorrect, tracks2prob,[("outputnode.eddy_corrected","template_file")])])
    mapping.connect([(tracks2prob, MRconvert_tracks2prob,[("tract_image","in_file")])])

    """
    Structural Processing
    ---------------------
    First, we coregister the diffusion image to the structural image
    """

    mapping.connect([(eddycorrect, coregister,[("outputnode.eddy_corrected","in_file")])])
    mapping.connect([(mri_convert_Brain, coregister,[('out_file','reference')])])

    """
    The MRtrix-tracked fibers are converted to TrackVis format (with voxel and data dimensions grabbed from the DWI).
    The connectivity matrix is created with the transformed .trk fibers and the parcellation file.
    """

    mapping.connect([(eddycorrect, tck2trk,[("outputnode.eddy_corrected","image_file")])])
    mapping.connect([(mri_convert_Brain, tck2trk,[("out_file","registration_image_file")])])
    mapping.connect([(coregister, tck2trk,[("out_matrix_file","matrix_file")])])
    mapping.connect([(probCSDstreamtrack, tck2trk,[("tracked","in_file")])])
    mapping.connect([(tck2trk, creatematrix,[("out_file","tract_file")])])
    mapping.connect(inputnode_within, 'resolution_network_file',
                    creatematrix, 'resolution_network_file')
    mapping.connect([(inputnode_within, creatematrix,[("subject_id","out_matrix_file")])])
    mapping.connect([(inputnode_within, creatematrix,[("subject_id","out_matrix_mat_file")])])
    mapping.connect([(parcellate, creatematrix,[("roi_file","roi_file")])])

    """
    The merge nodes defined earlier are used here to create lists of the files which are
    destined for the CFFConverter.
    """

    mapping.connect([(mris_convertLH, giftiSurfaces,[("converted","in1")])])
    mapping.connect([(mris_convertRH, giftiSurfaces,[("converted","in2")])])
    mapping.connect([(mris_convertLHwhite, giftiSurfaces,[("converted","in3")])])
    mapping.connect([(mris_convertRHwhite, giftiSurfaces,[("converted","in4")])])
    mapping.connect([(mris_convertLHinflated, giftiSurfaces,[("converted","in5")])])
    mapping.connect([(mris_convertRHinflated, giftiSurfaces,[("converted","in6")])])
    mapping.connect([(mris_convertLHsphere, giftiSurfaces,[("converted","in7")])])
    mapping.connect([(mris_convertRHsphere, giftiSurfaces,[("converted","in8")])])

    mapping.connect([(mris_convertLHlabels, giftiLabels,[("converted","in1")])])
    mapping.connect([(mris_convertRHlabels, giftiLabels,[("converted","in2")])])

    mapping.connect([(parcellate, niftiVolumes,[("roi_file","in1")])])
    mapping.connect([(eddycorrect, niftiVolumes,[("outputnode.eddy_corrected","in2")])])
    mapping.connect([(mri_convert_Brain, niftiVolumes,[("out_file","in3")])])

    mapping.connect([(creatematrix, fiberDataArrays,[("endpoint_file","in1")])])
    mapping.connect([(creatematrix, fiberDataArrays,[("endpoint_file_mm","in2")])])
    mapping.connect([(creatematrix, fiberDataArrays,[("fiber_length_file","in3")])])
    mapping.connect([(creatematrix, fiberDataArrays,[("fiber_label_file","in4")])])

    """
    This block actually connects the merged lists to the CFF converter. We pass the surfaces
    and volumes that are to be included, as well as the tracts and the network itself. The currently
    running pipeline (dmri_connectivity_advanced.py) is also scraped and included in the CFF file. This
    makes it easy for the user to examine the entire processing pathway used to generate the end
    product.
    """

    mapping.connect([(giftiSurfaces, CFFConverter,[("out","gifti_surfaces")])])
    mapping.connect([(giftiLabels, CFFConverter,[("out","gifti_labels")])])
    mapping.connect([(creatematrix, CFFConverter,[("matrix_files","gpickled_networks")])])
    mapping.connect([(niftiVolumes, CFFConverter,[("out","nifti_volumes")])])
    mapping.connect([(fiberDataArrays, CFFConverter,[("out","data_files")])])
    mapping.connect([(creatematrix, CFFConverter,[("filtered_tractography","tract_files")])])
    mapping.connect([(inputnode_within, CFFConverter,[("subject_id","title")])])

    """
    The graph theoretical metrics which have been generated are placed into another CFF file.
    """

    mapping.connect([(inputnode_within, networkx,[("subject_id","inputnode.extra_field")])])
    mapping.connect([(creatematrix, networkx,[("intersection_matrix_file","inputnode.network_file")])])

    mapping.connect([(networkx, NxStatsCFFConverter,[("outputnode.network_files","gpickled_networks")])])
    mapping.connect([(giftiSurfaces, NxStatsCFFConverter,[("out","gifti_surfaces")])])
    mapping.connect([(giftiLabels, NxStatsCFFConverter,[("out","gifti_labels")])])
    mapping.connect([(niftiVolumes, NxStatsCFFConverter,[("out","nifti_volumes")])])
    mapping.connect([(fiberDataArrays, NxStatsCFFConverter,[("out","data_files")])])
    mapping.connect([(inputnode_within, NxStatsCFFConverter,[("subject_id","title")])])

    mapping.connect([(inputnode_within, cmats_to_csv,[("subject_id","inputnode.extra_field")])])
    mapping.connect([(creatematrix, cmats_to_csv,[("matlab_matrix_files","inputnode.matlab_matrix_files")])])
    mapping.connect([(creatematrix, nfibs_to_csv,[("stats_file","in_file")])])
    mapping.connect([(nfibs_to_csv, merge_nfib_csvs,[("csv_files","in_files")])])
    mapping.connect([(inputnode_within, merge_nfib_csvs,[("subject_id","extra_field")])])


    """
    Create a higher-level workflow
    --------------------------------------
    Finally, we create another higher-level workflow to connect our mapping workflow with the info and datagrabbing nodes
    declared at the beginning. Our tutorial can is now extensible to any arbitrary number of subjects by simply adding
    their names to the subject list and their data to the proper folders.
    """

    inputnode = pe.Node(interface=util.IdentityInterface(fields=["subject_id", "dwi", "bvecs", "bvals", "subjects_dir"]), name="inputnode")

    outputnode = pe.Node(interface = util.IdentityInterface(fields=["fa",
                                                                "struct",
                                                                "tracts",
                                                                "tracks2prob",
                                                                "connectome",
                                                                "nxstatscff",
                                                                "nxmatlab",
                                                                "nxcsv",
                                                                "fiber_csv",
                                                                "cmatrices_csv",
                                                                "nxmergedcsv",
                                                                "cmatrix",
                                                                "networks",
                                                                "filtered_tracts",
                                                                "rois",
                                                                "odfs",
                                                                "tdi",
                                                                "mean_fiber_length",
                                                                "median_fiber_length",
                                                                "fiber_length_std"]),
                                        name="outputnode")

    connectivity = pe.Workflow(name="connectivity")
    connectivity.base_output_dir=name
    connectivity.base_dir=name

    connectivity.connect([(inputnode, mapping, [("dwi", "inputnode_within.dwi"),
                                              ("bvals", "inputnode_within.bvals"),
                                              ("bvecs", "inputnode_within.bvecs"),
                                              ("subject_id", "inputnode_within.subject_id"),
                                              ("subjects_dir", "inputnode_within.subjects_dir")])
                                              ])

    connectivity.connect([(mapping, outputnode, [("tck2trk.out_file", "tracts"),
        ("CFFConverter.connectome_file", "connectome"),
        ("NxStatsCFFConverter.connectome_file", "nxstatscff"),
        ("CreateMatrix.matrix_mat_file", "cmatrix"),
        ("CreateMatrix.mean_fiber_length_matrix_mat_file", "mean_fiber_length"),
        ("CreateMatrix.median_fiber_length_matrix_mat_file", "median_fiber_length"),
        ("CreateMatrix.fiber_length_std_matrix_mat_file", "fiber_length_std"),
        ("CreateMatrix.matrix_files", "networks"),
        ("CreateMatrix.filtered_tractographies", "filtered_tracts"),
        ("merge_nfib_csvs.csv_file", "fiber_csv"),
        ("mri_convert_ROI_scale500.out_file", "rois"),
        ("csdeconv.spherical_harmonics_image", "odfs"),
        ("mri_convert_Brain.out_file", "struct"),
        ("MRconvert_fa.converted", "fa"),
        ("MRconvert_tracks2prob.converted", "tracks2prob")])
        ])

    connectivity.connect([(cmats_to_csv, outputnode,[("outputnode.csv_file","cmatrices_csv")])])
    connectivity.connect([(networkx, outputnode,[("outputnode.csv_files","nxcsv")])])
    return connectivity