Ejemplo n.º 1
0
def test_surfxfm(create_surf_file_in_directory):

    xfm = fs.SurfaceTransform()

    # Test underlying command
    assert xfm.cmd == "mri_surf2surf"

    # Test mandatory args exception
    with pytest.raises(ValueError):
        xfm.run()

    # Create testing files
    surf, cwd = create_surf_file_in_directory

    # Test input settings
    xfm.inputs.source_file = surf
    xfm.inputs.source_subject = "my_subject"
    xfm.inputs.target_subject = "fsaverage"
    xfm.inputs.hemi = "lh"

    # Test the command line
    assert xfm.cmdline == \
        ("mri_surf2surf --hemi lh --tval %s/lh.a.fsaverage.nii --sval %s --srcsubject my_subject --trgsubject fsaverage" %
         (cwd, surf))

    # Test identity
    xfmish = fs.SurfaceTransform(source_subject="fsaverage",
                                 target_subject="my_subject",
                                 source_file=surf,
                                 hemi="lh")
    assert xfm != xfmish
Ejemplo n.º 2
0
def test_surfxfm():

    xfm = fs.SurfaceTransform()

    # Test underlying command
    yield assert_equal, xfm.cmd, "mri_surf2surf"

    # Test mandatory args exception
    yield assert_raises, ValueError, xfm.run

    # Create testing files
    surf, cwd, oldwd = create_surf_file()

    # Test input settings
    xfm.inputs.source_file = surf
    xfm.inputs.source_subject = "my_subject"
    xfm.inputs.target_subject = "fsaverage"
    xfm.inputs.hemi = "lh"

    # Test the command line
    yield assert_equal, xfm.cmdline, \
        ("mri_surf2surf --hemi lh --tval %s/lh.a.fsaverage.nii --sval %s --srcsubject my_subject --trgsubject fsaverage" %
         (cwd, surf))

    # Test identity
    xfmish = fs.SurfaceTransform(source_subject="fsaverage",
                                 target_subject="my_subject",
                                 source_file=surf,
                                 hemi="lh")
    yield assert_not_equal, xfm, xfmish

    # Clean up
    clean_directory(cwd, oldwd)
Ejemplo n.º 3
0
def nipype_mri_surf2surf(t1path, hemi):
    splitpath = t1path.split(os.sep)
    fsdir = os.path.join(BIDS_DATA_DIR, "derivatives",
                         "freesurfer_{0}".format(splitpath[-3]))
    outdir = os.path.join(BIDS_DATA_DIR, "derivatives",
                          "freesurfer_projection_{0}".format(splitpath[-3]),
                          splitpath[-4])
    funcpath = os.path.join(
        BIDS_DATA_DIR, "derivatives", "spmpreproc_{0}".format(splitpath[-3]),
        splitpath[-4],
        "wrr{0}_{1}_task-localizer_bold.nii.gz".format(splitpath[-4],
                                                       splitpath[-3]))
    assert os.path.isfile(funcpath), funcpath
    basename = os.path.basename(funcpath).replace(".nii.gz", "")
    texturefile = os.path.join(outdir, basename + ".{0}.gii".format(hemi))
    smoothfile = os.path.join(outdir, basename + ".s5.{0}.gii".format(hemi))
    if not os.path.isfile(texturefile):
        raise ValueError("Oups!: {0}".format(path))
    if not os.path.isfile(smoothfile):
        open(smoothfile, "wt").close()
    surf2surf = freesurfer.SurfaceSmooth(in_file=texturefile,
                                         hemi=hemi,
                                         subject_id=splitpath[-4],
                                         fwhm=5,
                                         out_file=smoothfile,
                                         subjects_dir=fsdir)
    print(surf2surf.cmdline)
    if PROCESS:
        surf2surf.run()
    surf2surf = freesurfer.SurfaceTransform(
        hemi=hemi,
        source_subject=splitpath[-4],
        source_file=smoothfile,
        target_subject="ico",
        target_ico_order=7,
        out_file=os.path.join(outdir,
                              basename + ".ico7.s5.{0}.gii".format(hemi)),
        subjects_dir=fsdir)
    print(surf2surf.cmdline)
    if PROCESS:
        surf2surf.run()
Ejemplo n.º 4
0
def segstats_workflow(c, name='segstats'):
    import nipype.interfaces.fsl as fsl
    import nipype.interfaces.freesurfer as fs
    import nipype.interfaces.io as nio
    import nipype.pipeline.engine as pe
    import nipype.interfaces.utility as niu
    from .scripts.u0a14c5b5899911e1bca80023dfa375f2.modified_nipype_workflows import create_get_stats_flow
    from .scripts.u0a14c5b5899911e1bca80023dfa375f2.utils import tolist, pickidx
    if not c.use_annotation:
        workflow = create_get_stats_flow(name='segstats', withreg=c.use_reg)
    else:
        workflow = create_get_stats_flow(name='segstats')

    workflow.inputs.segstats.avgwf_txt_file = True
    datagrabber = c.datagrabber.create_dataflow()
    merge = pe.Node(fsl.Merge(dimension='t'), name='merge_files')
    inputspec = workflow.get_node('inputspec')
    subject_iterable = datagrabber.get_node("subject_id_iterable")
    # merge files grabbed
    stats = workflow.get_node('segstats')
    print "colortablefile:", c.color_table_file
    if c.color_table_file == "Default":
        stats.inputs.default_color_table = True
    elif c.color_table_file == "Color_Table":
        stats.inputs.color_table_file = c.color_file
    elif c.color_table_file == "GCA_color_table":
        stats.inputs.gca_color_table = c.color_file

    workflow.connect(datagrabber, ('datagrabber.in_files', tolist), merge,
                     'in_files')
    doubler = lambda x: [x, x]
    # This means you're using an annotation from a standard surface
    if c.use_annotation:

        surf2surf = pe.MapNode(fs.SurfaceTransform(
            source_subject=c.annot_space, subjects_dir=c.surf_dir),
                               name="surf2surf",
                               iterfield=['hemi', 'source_annot_file'])
        surf2surf.inputs.source_annot_file = [c.lh_annotation, c.rh_annotation]
        workflow.connect(subject_iterable, "subject_id", surf2surf,
                         "target_subject")
        surf2surf.inputs.hemi = ['lh', 'rh']
        #add = pe.Node(fsl.BinaryMaths(operation='add'),name="add")
        #workflow.connect(add,'out_file',inputspec,"label_file")
        label2vol = pe.MapNode(fs.Label2Vol(subjects_dir=c.surf_dir,
                                            proj=c.proj),
                               name='label2vol',
                               iterfield=["hemi", "annot_file"])
        workflow.connect(surf2surf, "out_file", label2vol, "annot_file")
        workflow.connect(subject_iterable, "subject_id", label2vol,
                         "subject_id")
        workflow.connect(merge, "merged_file", label2vol, "template_file")
        label2vol.inputs.hemi = ['lh', 'rh']
        workflow.connect(datagrabber, 'datagrabber.reg_file', label2vol,
                         'reg_file')
        if c.inverse_reg:
            label2vol.inputs.invert_mtx = c.inverse_reg
        workflow.connect(label2vol, 'vol_label_file', inputspec, 'label_file')
        workflow.connect(merge, ('merged_file', doubler), inputspec,
                         'source_file')

    #This means you're using annotations on the subjects surface
    if c.use_subject_annotation:
        label2vol = pe.MapNode(fs.Label2Vol(subjects_dir=c.surf_dir,
                                            proj=c.proj),
                               name='label2vol',
                               iterfield=["hemi", "annot_file"])
        label2vol.inputs.hemi = ['lh', 'rh']
        workflow.connect(datagrabber, "datagrabber.label_file", label2vol,
                         "annot_file")
        workflow.connect(subject_iterable, "subject_id", label2vol,
                         "subject_id")
        workflow.connect(merge, "merged_file", label2vol, "template_file")
        workflow.connect(label2vol, 'vol_label_file', inputspec, "label_file")
        workflow.connect(merge, ('merged_file', doubler), inputspec,
                         'source_file')
        workflow.connect(datagrabber, 'datagrabber.reg_file', label2vol,
                         'reg_file')
        if c.inverse_reg:
            label2vol.inputs.invert_mtx = c.inverse_reg

    if not c.use_subject_annotation and not c.use_annotation:
        workflow.connect(merge, 'merged_file', inputspec, 'source_file')

    # This means you're using a labeled volume like aparc+aseg
    if c.use_reg and not c.use_annotation and not c.use_subject_annotation:
        workflow.connect(datagrabber, 'datagrabber.reg_file', inputspec,
                         'reg_file')
        workflow.inputs.inputspec.subjects_dir = c.surf_dir
        workflow.inputs.inputspec.inverse = c.inverse_reg

    if c.use_standard_label and not c.use_annotation and not c.use_subject_annotation:
        workflow.inputs.inputspec.label_file = c.label_file
    elif not c.use_standard_label and not c.use_annotation and not c.use_subject_annotation:
        workflow.connect(datagrabber, 'datagrabber.label_file', inputspec,
                         "label_file")

    sinker = pe.Node(nio.DataSink(), name='sinker')
    sinker.inputs.base_directory = os.path.join(c.sink_dir)

    workflow.connect(subject_iterable, 'subject_id', sinker, 'container')

    def get_subs(subject_id, subject_annot):
        subs = [('_subject_id_%s' % subject_id, '')]
        if subject_annot:
            subs.append(('_segstats0/summary.stats', 'lh.summary.stats'))
            subs.append(('_segstats1/summary.stats', 'rh.summary.stats'))
            subs.append(('_segstats0', ''))
            subs.append(('_segstats1', ''))
        else:
            subs.append(('_segstats0', ''))
        return subs

    workflow.connect(
        subject_iterable,
        ('subject_id', get_subs, c.use_subject_annotation or c.use_annotation),
        sinker, 'substitutions')
    outputspec = workflow.get_node('outputspec')
    workflow.connect(outputspec, 'stats_file', sinker,
                     'segstats.%s.@stats' % c.statname)
    workflow.connect(stats, "avgwf_txt_file", sinker,
                     'segstats.%s.@avg' % c.statname)

    return workflow
Ejemplo n.º 5
0
def do_pipe3_projection(subject_ID,
                        freesurfer_dir,
                        workflow_dir,
                        output_dir,
                        tract_number,
                        use_sample=False):
    """
    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.ants as ants
    import os.path as op  # system functions

    from nipype.interfaces.utility import Function

    from dmri_pipe_aux import get_connectivity_matrix
    from dmri_pipe_aux import surf2file
    from dmri_pipe_aux import voxels2nii
    from dmri_pipe_aux import normalize_matrix
    from dmri_pipe_aux import interface2surf
    from dmri_pipe_aux import read_voxels
    from dmri_pipe_aux import downsample_matrix
    from dmri_pipe_aux import merge_matrices
    """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """
    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='pipe3_projection')
    """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """
    Use datasource node to perform the actual data grabbing.
    Templates for the associated images are used to obtain the correct images.
    """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """"""

    data_template = subject_ID + "/%s/" + "%s" + "%s"

    info = dict(
        wm=[['fa_masking', subject_ID, '_mask_wm.nii']],
        seeds_left=[['fa_masking', subject_ID, '_interface_left_voxels.txt']],
        seeds_right=[['fa_masking', subject_ID,
                      '_interface_right_voxels.txt']],
        index_left=[['fa_masking', subject_ID, '_interface_left_index.nii']],
        index_right=[['fa_masking', subject_ID, '_interface_right_index.nii']],
        fa=[['fa_masking', subject_ID, '_fa_masked.nii']],
        t1=[['anatomy', subject_ID, '_t1_masked.nii']],
        inv_flirt_mat=[['anatomy', '', 'flirt_t1_2_fa_inv.mat']],
        warp=[['anatomy', '', 'ants_fa_2_regt1_Warp.nii.gz']])

    datasource = pe.Node(interface=io.DataGrabber(outfields=info.keys()),
                         name='datasource')
    datasource.inputs.template = data_template
    datasource.inputs.base_directory = output_dir
    datasource.inputs.template_args = info
    datasource.inputs.sort_filelist = True
    datasource.run_without_submitting = True

    tracts_left_source = pe.Node(
        interface=io.DataGrabber(outfields=['tracts_left']),
        name='tracts_left_source')
    tracts_left_source.inputs.template = subject_ID + '/raw_tracts/lh/probtract_*.nii'
    tracts_left_source.inputs.base_directory = output_dir
    tracts_left_source.inputs.sort_filelist = True
    tracts_left_source.run_without_submitting = True

    tracts_right_source = pe.Node(
        interface=io.DataGrabber(outfields=['tracts_right']),
        name='tracts_right_source')
    tracts_right_source.inputs.template = subject_ID + '/raw_tracts/rh/probtract_*.nii'
    tracts_right_source.inputs.base_directory = output_dir
    tracts_right_source.inputs.sort_filelist = True
    tracts_right_source.run_without_submitting = True
    """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """
    The input node 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=[
        "wm", "seeds_left", "seeds_right", "index_left", "index_right", "fa",
        "t1", "inv_flirt_mat", "warp", "tracts_left", "tracts_right"
    ]),
                        name="inputnode")
    """
    read seed coordinates
    """

    interface_voxels_left = pe.Node(interface=Function(
        input_names=["seed_file", "use_sample"],
        output_names=["seed_list"],
        function=read_voxels),
                                    name='70_interface_voxels_left')
    interface_voxels_left.inputs.use_sample = use_sample
    dmripipeline.connect(inputnode, "seeds_left", interface_voxels_left,
                         "seed_file")

    interface_voxels_right = interface_voxels_left.clone(
        name='70_interface_voxels_right')
    dmripipeline.connect(inputnode, "seeds_right", interface_voxels_right,
                         "seed_file")
    """
    Get the direct connectivity matrix
    """

    connectivity_matrix = pe.Node(interface=Function(
        input_names=[
            "tract_list_left", "tract_list_right", "voxel_list_left",
            "voxel_list_right", "max_value"
        ],
        output_names=[
            "submatrix_left_left", "submatrix_left_right",
            "submatrix_right_left", "submatrix_right_right", "exclusion_list"
        ],
        function=get_connectivity_matrix),
                                  name='71_direct_connect_array')
    connectivity_matrix.inputs.max_value = tract_number
    connectivity_matrix.run_without_submitting = True
    #    connectivity_matrix.plugin_args={'override_specs': 'requirements = Machine == "kalifornien.cbs.mpg.de"'}
    dmripipeline.connect(inputnode, "tracts_left", connectivity_matrix,
                         "tract_list_left")
    dmripipeline.connect(inputnode, "tracts_right", connectivity_matrix,
                         "tract_list_right")
    dmripipeline.connect(interface_voxels_left, "seed_list",
                         connectivity_matrix, "voxel_list_left")
    dmripipeline.connect(interface_voxels_right, "seed_list",
                         connectivity_matrix, "voxel_list_right")

    tract_exclusion_mask = pe.Node(interface=Function(
        input_names=["voxel_list", "ref_image", "outfile"],
        output_names=["outfile"],
        function=voxels2nii),
                                   name='72_tract_exclusion_mask')
    tract_exclusion_mask.inputs.outfile = subject_ID + '_tractseed_exclusion_mask.nii'
    dmripipeline.connect(inputnode, "wm", tract_exclusion_mask, "ref_image")
    dmripipeline.connect(connectivity_matrix, "exclusion_list",
                         tract_exclusion_mask, "voxel_list")

    submatrix_left_left = pe.Node(interface=Function(
        input_names=["in_array", "max_value", "outfile_prefix"],
        output_names=[
            "mat_matrix_nat", "mat_matrix_log", "nii_matrix_nat",
            "nii_matrix_log"
        ],
        function=normalize_matrix),
                                  name='73_submatrix_left_left')
    submatrix_left_left.run_without_submitting = True
    submatrix_left_left.inputs.max_value = tract_number
    submatrix_left_left.inputs.outfile_prefix = 'directconnect_left_left'
    dmripipeline.connect(connectivity_matrix, "submatrix_left_left",
                         submatrix_left_left, "in_array")

    submatrix_left_right = submatrix_left_left.clone(
        name='73_submatrix_left_right')
    submatrix_left_right.inputs.outfile_prefix = 'directconnect_left_right'
    dmripipeline.connect(connectivity_matrix, "submatrix_left_right",
                         submatrix_left_right, "in_array")

    submatrix_right_left = submatrix_left_left.clone(
        name='73_submatrix_right_left')
    submatrix_right_left.inputs.outfile_prefix = 'directconnect_right_left'
    dmripipeline.connect(connectivity_matrix, "submatrix_right_left",
                         submatrix_right_left, "in_array")

    submatrix_right_right = submatrix_left_left.clone(
        name='73_submatrix_right_right')
    submatrix_right_right.inputs.outfile_prefix = 'directconnect_right_right'
    dmripipeline.connect(connectivity_matrix, "submatrix_right_right",
                         submatrix_right_right, "in_array")

    #     full_matrix_nat = pe.Node(interface=Function(input_names=["sm_left_left", "sm_left_right","sm_right_left", "sm_right_right", "out_filename"], output_names=["out_file"], function=merge_matrices), name='73_full_matrix_nat')
    #     full_matrix_nat.inputs.out_filename = 'directconnect_full_nat.mat'
    #     full_matrix_nat.run_without_submitting = True
    #     dmripipeline.connect(submatrix_left_left, "nii_matrix_nat", full_matrix_nat, "sm_left_left")
    #     dmripipeline.connect(submatrix_left_right, "nii_matrix_nat", full_matrix_nat, "sm_left_right")
    #     dmripipeline.connect(submatrix_right_left, "nii_matrix_nat", full_matrix_nat, "sm_right_left")
    #     dmripipeline.connect(submatrix_right_right, "nii_matrix_nat", full_matrix_nat, "sm_right_right")
    #
    #     full_matrix_log = full_matrix_nat.clone(name='73_full_matrix_log')
    #     full_matrix_log.inputs.out_filename = 'directconnect_full_log.mat'
    #     full_matrix_log.run_without_submitting = True
    #     dmripipeline.connect(submatrix_left_left, "nii_matrix_log", full_matrix_log, "sm_left_left")
    #     dmripipeline.connect(submatrix_left_right, "nii_matrix_log", full_matrix_log, "sm_left_right")
    #     dmripipeline.connect(submatrix_right_left, "nii_matrix_log", full_matrix_log, "sm_right_left")
    #     dmripipeline.connect(submatrix_right_right, "nii_matrix_log", full_matrix_log, "sm_right_right")
    """
    # invert and binarize tract exclusion mask and remove those voxels from the index interfaces
    """
    tract_denoise_mask = pe.Node(interface=fsl.maths.MathsCommand(),
                                 name='74_tract_denoise_mask')
    tract_denoise_mask.inputs.args = '-binv'
    tract_denoise_mask.run_without_submitting = True
    dmripipeline.connect(tract_exclusion_mask, "outfile", tract_denoise_mask,
                         "in_file")

    index_pruned_left = pe.Node(interface=fsl.maths.ApplyMask(),
                                name='75_interface_pruned_left')
    index_pruned_left.inputs.out_file = subject_ID + '_interface_pruned_left.nii'
    index_pruned_left.run_without_submitting = True
    dmripipeline.connect(inputnode, "index_left", index_pruned_left, "in_file")
    dmripipeline.connect(tract_denoise_mask, "out_file", index_pruned_left,
                         "mask_file")

    index_pruned_right = index_pruned_left.clone(
        name='75_interface_pruned_right')
    index_pruned_right.inputs.out_file = subject_ID + '_interface_pruned_right.nii'
    dmripipeline.connect(inputnode, "index_right", index_pruned_right,
                         "in_file")
    dmripipeline.connect(tract_denoise_mask, "out_file", index_pruned_right,
                         "mask_file")
    """
    # warp index image to t1 space
    """
    index_warped_2_t1_left = pe.Node(interface=ants.WarpImageMultiTransform(),
                                     name='76_index_warped_2_t1_left')
    index_warped_2_t1_left.inputs.use_nearest = True
    index_warped_2_t1_left.run_without_submitting = True
    dmripipeline.connect([(index_pruned_left, index_warped_2_t1_left,
                           [('out_file', 'input_image')])])
    dmripipeline.connect([(inputnode, index_warped_2_t1_left,
                           [('fa', 'reference_image')])])
    dmripipeline.connect([(inputnode, index_warped_2_t1_left,
                           [('warp', 'transformation_series')])])

    index_warped_2_t1_right = index_warped_2_t1_left.clone(
        name='76_index_warped_2_t1_right')
    dmripipeline.connect([(index_pruned_right, index_warped_2_t1_right,
                           [('out_file', 'input_image')])])
    dmripipeline.connect([(inputnode, index_warped_2_t1_right,
                           [('fa', 'reference_image')])])
    dmripipeline.connect([(inputnode, index_warped_2_t1_right,
                           [('warp', 'transformation_series')])])

    index_final_2_t1_left = pe.Node(interface=fsl.ApplyXfm(),
                                    name='77_index_final_2_t1_left')
    index_final_2_t1_left.inputs.apply_xfm = True
    index_final_2_t1_left.run_without_submitting = True
    index_final_2_t1_left.inputs.interp = 'nearestneighbour'
    index_final_2_t1_left.inputs.out_file = subject_ID + '_index_seedt1_left.nii'
    dmripipeline.connect([(index_warped_2_t1_left, index_final_2_t1_left,
                           [("output_image", "in_file")])])
    dmripipeline.connect([(inputnode, index_final_2_t1_left,
                           [("inv_flirt_mat", "in_matrix_file")])])
    dmripipeline.connect([(inputnode, index_final_2_t1_left, [("t1",
                                                               "reference")])])

    index_final_2_t1_right = index_final_2_t1_left.clone(
        name='77_index_final_2_t1_right')
    index_final_2_t1_right.inputs.out_file = subject_ID + '_index_seedt1_right.nii'
    dmripipeline.connect([(index_warped_2_t1_right, index_final_2_t1_right,
                           [("output_image", "in_file")])])
    dmripipeline.connect([(inputnode, index_final_2_t1_right,
                           [("inv_flirt_mat", "in_matrix_file")])])
    dmripipeline.connect([(inputnode, index_final_2_t1_right,
                           [("t1", "reference")])])
    """
    extra processing
    """

    index_vol2surf_left = pe.Node(interface=fsurf.SampleToSurface(),
                                  name='78_index_vol2surf_left')
    index_vol2surf_left.inputs.hemi = 'lh'
    index_vol2surf_left.inputs.subject_id = subject_ID
    index_vol2surf_left.inputs.reg_header = True
    index_vol2surf_left.inputs.interp_method = 'nearest'
    index_vol2surf_left.inputs.sampling_method = 'point'
    index_vol2surf_left.inputs.sampling_range = 0
    index_vol2surf_left.inputs.sampling_units = 'frac'
    index_vol2surf_left.inputs.surface = 'orig'
    #index_vol2surf_left.inputs.cortex_mask = True
    index_vol2surf_left.inputs.terminal_output = 'file'
    index_vol2surf_left.inputs.out_file = subject_ID + '_index_seedt1_2surf_left.mgz'
    index_vol2surf_left.run_without_submitting = True
    dmripipeline.connect([(index_final_2_t1_left, index_vol2surf_left,
                           [('out_file', 'source_file')])])

    index_vol2surf_right = index_vol2surf_left.clone(
        name='78_index_vol2surf_right')
    index_vol2surf_right.inputs.hemi = 'rh'
    index_vol2surf_right.inputs.out_file = subject_ID + '_index_seedt1_2surf_right.mgz'
    dmripipeline.connect([(index_final_2_t1_right, index_vol2surf_right,
                           [('out_file', 'source_file')])])

    index_2_t1_reorient_left = pe.Node(interface=fsl.Reorient2Std(),
                                       name='79_next_2_t1_reorient_left')
    index_2_t1_reorient_left.inputs.out_file = subject_ID + '_index_seedt1_reorient_left.nii'
    index_2_t1_reorient_left.run_without_submitting = True
    dmripipeline.connect(index_final_2_t1_left, 'out_file',
                         index_2_t1_reorient_left, 'in_file')

    index_2_t1_reorient_right = index_2_t1_reorient_left.clone(
        name='79_next_2_t1_reorient_right')
    index_2_t1_reorient_right.inputs.out_file = subject_ID + '_index_seedt1_reorient_right.nii'
    dmripipeline.connect(index_final_2_t1_right, 'out_file',
                         index_2_t1_reorient_right, 'in_file')

    index_interface2surf_left = pe.Node(interface=Function(
        input_names=[
            "interface_image", "surface_file", "cortex_label", "ref_mgz",
            "out_file"
        ],
        output_names=["out_file"],
        function=interface2surf),
                                        name='80_index_interface2surf_left')
    index_interface2surf_left.inputs.surface_file = subjects_dir + '/' + subject_ID + '/surf/lh.orig'
    index_interface2surf_left.inputs.cortex_label = subjects_dir + '/' + subject_ID + '/label/lh.cortex.label'
    index_interface2surf_left.inputs.out_file = subject_ID + '_index_seedt1_2surf_left.mgz'
    dmripipeline.connect(index_2_t1_reorient_left, 'out_file',
                         index_interface2surf_left, 'interface_image')
    dmripipeline.connect(index_vol2surf_left, 'out_file',
                         index_interface2surf_left, 'ref_mgz')

    index_interface2surf_right = index_interface2surf_left.clone(
        name='80_index_interface2surf_right')
    index_interface2surf_right.inputs.surface_file = subjects_dir + '/' + subject_ID + '/surf/rh.orig'
    index_interface2surf_right.inputs.cortex_label = subjects_dir + '/' + subject_ID + '/label/rh.cortex.label'
    index_interface2surf_right.inputs.out_file = subject_ID + '_index_seedt1_2surf_right.mgz'
    dmripipeline.connect(index_2_t1_reorient_right, 'out_file',
                         index_interface2surf_right, 'interface_image')
    dmripipeline.connect(index_vol2surf_right, 'out_file',
                         index_interface2surf_right, 'ref_mgz')

    fs_indexlist_left = pe.Node(interface=Function(
        input_names=["in_surface_values", "cortex_label", "out_file"],
        output_names=["out_file"],
        function=surf2file),
                                name='81_index_fsnative_left')
    fs_indexlist_left.inputs.cortex_label = op.join(
        freesurfer_dir, subject_ID + '/label/lh.cortex.label')
    fs_indexlist_left.inputs.out_file = subject_ID + '_seed_index_fsnative_left.txt'
    fs_indexlist_left.run_without_submitting = True
    dmripipeline.connect([(index_interface2surf_left, fs_indexlist_left,
                           [("out_file", "in_surface_values")])])

    fs_indexlist_right = fs_indexlist_left.clone(
        name='81_index_fsnative_right')
    fs_indexlist_right.inputs.cortex_label = op.join(
        freesurfer_dir, subject_ID + '/label/rh.cortex.label')
    fs_indexlist_right.inputs.out_file = subject_ID + '_seed_index_fsnative_right.txt'
    dmripipeline.connect([(index_interface2surf_right, fs_indexlist_right,
                           [("out_file", "in_surface_values")])])
    """""" """""" """""" """""" """
    """ """""" """""" """""" """"""

    index_fsaverage5_left = pe.Node(interface=fsurf.SurfaceTransform(),
                                    name='81_index_fsaverage5_left')
    index_fsaverage5_left.inputs.hemi = 'lh'
    index_fsaverage5_left.inputs.source_subject = subject_ID
    index_fsaverage5_left.inputs.target_subject = 'fsaverage5'
    index_fsaverage5_left.inputs.args = '--mapmethod nnf --label-src lh.cortex.label --label-trg lh.cortex.label'
    index_fsaverage5_left.inputs.out_file = subject_ID + '_index_seedt1_fsaverage5_left.mgz'
    #index_fsaverage5_left.run_without_submitting = True
    dmripipeline.connect([(index_interface2surf_left, index_fsaverage5_left,
                           [('out_file', 'source_file')])])

    index_fsaverage5_right = index_fsaverage5_left.clone(
        name='81_index_fsaverage5_right')
    index_fsaverage5_right.inputs.hemi = 'rh'
    index_fsaverage5_left.inputs.args = '--mapmethod nnf --label-src rh.cortex.label --label-trg rh.cortex.label'
    index_fsaverage5_right.inputs.out_file = subject_ID + '_index_seedt1_fsaverage5_right.mgz'
    dmripipeline.connect([(index_interface2surf_right, index_fsaverage5_right,
                           [('out_file', 'source_file')])])

    fs5_indexlist_left = pe.Node(interface=Function(
        input_names=["in_surface_values", "cortex_label", "out_file"],
        output_names=["out_file"],
        function=surf2file),
                                 name='82_index_fsav5_left')
    fs5_indexlist_left.inputs.cortex_label = op.join(
        freesurfer_dir, 'fsaverage5/label/lh.cortex.label')
    fs5_indexlist_left.inputs.out_file = subject_ID + '_seed_index_fs5_left.txt'
    #fs5_indexlist_left.run_without_submitting = True
    dmripipeline.connect([(index_fsaverage5_left, fs5_indexlist_left,
                           [("out_file", "in_surface_values")])])

    fs5_indexlist_right = fs5_indexlist_left.clone(name='82_index_fsav5_right')
    fs5_indexlist_right.inputs.cortex_label = op.join(
        freesurfer_dir, 'fsaverage5/label/rh.cortex.label')
    fs5_indexlist_right.inputs.out_file = subject_ID + '_seed_index_fs5_right.txt'
    dmripipeline.connect([(index_fsaverage5_right, fs5_indexlist_right,
                           [("out_file", "in_surface_values")])])

    index_fsaverage4_left = pe.Node(interface=fsurf.SurfaceTransform(),
                                    name='81_index_fsaverage4_left')
    index_fsaverage4_left.inputs.hemi = 'lh'
    index_fsaverage4_left.inputs.source_subject = subject_ID
    index_fsaverage4_left.inputs.target_subject = 'fsaverage4'
    index_fsaverage4_left.inputs.args = '--mapmethod nnf --label-src lh.cortex.label --label-trg lh.cortex.label'
    index_fsaverage4_left.inputs.out_file = subject_ID + '_index_seedt1_fsaverage4_left.mgz'
    #index_fsaverage4_left.run_without_submitting = True
    dmripipeline.connect([(index_interface2surf_left, index_fsaverage4_left,
                           [('out_file', 'source_file')])])

    index_fsaverage4_right = index_fsaverage4_left.clone(
        name='81_index_fsaverage4_right')
    index_fsaverage4_right.inputs.hemi = 'rh'
    index_fsaverage4_left.inputs.args = '--mapmethod nnf --label-src rh.cortex.label --label-trg rh.cortex.label'
    index_fsaverage4_right.inputs.out_file = subject_ID + '_index_seedt1_fsaverage4_right.mgz'
    dmripipeline.connect([(index_interface2surf_right, index_fsaverage4_right,
                           [('out_file', 'source_file')])])

    fs4_indexlist_left = pe.Node(interface=Function(
        input_names=["in_surface_values", "cortex_label", "out_file"],
        output_names=["out_file"],
        function=surf2file),
                                 name='82_index_fsav4_left')
    fs4_indexlist_left.inputs.cortex_label = op.join(
        freesurfer_dir, 'fsaverage4/label/lh.cortex.label')
    fs4_indexlist_left.inputs.out_file = subject_ID + '_seed_index_fs4_left.txt'
    #fs4_indexlist_left.run_without_submitting = True
    dmripipeline.connect([(index_fsaverage4_left, fs4_indexlist_left,
                           [("out_file", "in_surface_values")])])

    fs4_indexlist_right = fs4_indexlist_left.clone(name='82_index_fsav4_right')
    fs4_indexlist_right.inputs.cortex_label = op.join(
        freesurfer_dir, 'fsaverage4/label/rh.cortex.label')
    fs4_indexlist_right.inputs.out_file = subject_ID + '_seed_index_fs4_right.txt'
    dmripipeline.connect([(index_fsaverage4_right, fs4_indexlist_right,
                           [("out_file", "in_surface_values")])])
    """
    downsample matrices according to fsaverage projections
    """
    if (not use_sample):
        connect_mat_fs4_nat_left_left = pe.Node(
            interface=Function(input_names=[
                "index_row_file", "index_col_file", "matrix_file",
                "out_prefix", "dist2sim"
            ],
                               output_names=["out_mat", "out_nii"],
                               function=downsample_matrix),
            name='83_connect_mat_fs4_nat_left_left')
        connect_mat_fs4_nat_left_left.inputs.out_prefix = subject_ID + '_connect_fs4_nat_left_left'
        connect_mat_fs4_nat_left_left.inputs.dist2sim = False
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_nat_left_left, "index_row_file")
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_nat_left_left, "index_col_file")
        dmripipeline.connect(submatrix_left_left, "mat_matrix_nat",
                             connect_mat_fs4_nat_left_left, "matrix_file")

        connect_mat_fs4_nat_left_right = connect_mat_fs4_nat_left_left.clone(
            name='83_connect_mat_fs4_nat_left_right')
        connect_mat_fs4_nat_left_right.inputs.out_prefix = subject_ID + '_connect_fs4_nat_left_right'
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_nat_left_right, "index_row_file")
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_nat_left_right, "index_col_file")
        dmripipeline.connect(submatrix_left_right, "mat_matrix_nat",
                             connect_mat_fs4_nat_left_right, "matrix_file")

        connect_mat_fs4_nat_right_left = connect_mat_fs4_nat_left_left.clone(
            name='83_connect_mat_fs4_nat_right_left')
        connect_mat_fs4_nat_right_left.inputs.out_prefix = subject_ID + '_connect_fs4_nat_right_left'
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_nat_right_left, "index_row_file")
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_nat_right_left, "index_col_file")
        dmripipeline.connect(submatrix_right_left, "mat_matrix_nat",
                             connect_mat_fs4_nat_right_left, "matrix_file")

        connect_mat_fs4_nat_right_right = connect_mat_fs4_nat_left_left.clone(
            name='83_connect_mat_fs4_nat_right_right')
        connect_mat_fs4_nat_right_right.inputs.out_prefix = subject_ID + '_connect_fs4_nat_right_right'
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_nat_right_right, "index_row_file")
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_nat_right_right, "index_col_file")
        dmripipeline.connect(submatrix_right_right, "mat_matrix_nat",
                             connect_mat_fs4_nat_right_right, "matrix_file")

        connect_mat_fs4_log_left_left = connect_mat_fs4_nat_left_left.clone(
            name='83_connect_mat_fs4_log_left_left')
        connect_mat_fs4_log_left_left.inputs.out_prefix = subject_ID + '_connect_fs4_log_left_left'
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_log_left_left, "index_row_file")
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_log_left_left, "index_col_file")
        dmripipeline.connect(submatrix_left_left, "mat_matrix_log",
                             connect_mat_fs4_log_left_left, "matrix_file")

        connect_mat_fs4_log_left_right = connect_mat_fs4_log_left_left.clone(
            name='83_connect_mat_fs4_log_left_right')
        connect_mat_fs4_log_left_right.inputs.out_prefix = subject_ID + '_connect_fs4_log_left_right'
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_log_left_right, "index_row_file")
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_log_left_right, "index_col_file")
        dmripipeline.connect(submatrix_left_right, "mat_matrix_log",
                             connect_mat_fs4_log_left_right, "matrix_file")

        connect_mat_fs4_log_right_left = connect_mat_fs4_log_left_left.clone(
            name='83_connect_mat_fs4_log_right_left')
        connect_mat_fs4_log_right_left.inputs.out_prefix = subject_ID + '_connect_fs4_log_right_left'
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_log_right_left, "index_row_file")
        dmripipeline.connect(fs4_indexlist_left, "out_file",
                             connect_mat_fs4_log_right_left, "index_col_file")
        dmripipeline.connect(submatrix_right_left, "mat_matrix_log",
                             connect_mat_fs4_log_right_left, "matrix_file")

        connect_mat_fs4_log_right_right = connect_mat_fs4_log_left_left.clone(
            name='83_connect_mat_fs4_log_right_right')
        connect_mat_fs4_log_right_right.inputs.out_prefix = subject_ID + '_connect_fs4_log_right_right'
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_log_right_right, "index_row_file")
        dmripipeline.connect(fs4_indexlist_right, "out_file",
                             connect_mat_fs4_log_right_right, "index_col_file")
        dmripipeline.connect(submatrix_right_right, "mat_matrix_log",
                             connect_mat_fs4_log_right_right, "matrix_file")

        #         connect_mat_fs4_nat_full = pe.Node(interface=Function(input_names=["sm_left_left", "sm_left_right","sm_right_left", "sm_right_right", "out_filename"], output_names=["out_file"], function=merge_matrices), name='83_connect_mat_fs4_nat_full')
        #         connect_mat_fs4_nat_full.inputs.out_filename = subject_ID + '_connect_fs4_nat_full.mat'
        #         connect_mat_fs4_nat_full.run_without_submitting = True
        #         dmripipeline.connect(connect_mat_fs4_nat_left_left, "out_nii", connect_mat_fs4_nat_full, "sm_left_left")
        #         dmripipeline.connect(connect_mat_fs4_nat_left_right, "out_nii", connect_mat_fs4_nat_full, "sm_left_right")
        #         dmripipeline.connect(connect_mat_fs4_nat_right_left, "out_nii", connect_mat_fs4_nat_full, "sm_right_left")
        #         dmripipeline.connect(connect_mat_fs4_nat_right_right, "out_nii", connect_mat_fs4_nat_full, "sm_right_right")
        #
        #         connect_mat_fs4_log_full = connect_mat_fs4_nat_full.clone(name='83_connect_mat_fs4_log_full')
        #         connect_mat_fs4_log_full.inputs.outfile_prefix = subject_ID + '_connect_fs4_log_full.mat'
        #         connect_mat_fs4_log_full.run_without_submitting = True
        #         dmripipeline.connect(connect_mat_fs4_log_left_left, "out_nii", connect_mat_fs4_log_full, "sm_left_left")
        #         dmripipeline.connect(connect_mat_fs4_log_left_right, "out_nii", connect_mat_fs4_log_full, "sm_left_right")
        #         dmripipeline.connect(connect_mat_fs4_log_right_left, "out_nii", connect_mat_fs4_log_full, "sm_right_left")
        #         dmripipeline.connect(connect_mat_fs4_log_right_right, "out_nii", connect_mat_fs4_log_full, "sm_right_right")

        connect_mat_fs5_nat_left_left = connect_mat_fs4_nat_left_left.clone(
            name='83_connect_mat_fs5_nat_left_left')
        connect_mat_fs5_nat_left_left.inputs.out_prefix = subject_ID + '_connect_fs5_nat_left_left'
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_nat_left_left, "index_row_file")
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_nat_left_left, "index_col_file")
        dmripipeline.connect(submatrix_left_left, "mat_matrix_nat",
                             connect_mat_fs5_nat_left_left, "matrix_file")

        connect_mat_fs5_nat_left_right = connect_mat_fs5_nat_left_left.clone(
            name='83_connect_mat_fs5_nat_left_right')
        connect_mat_fs5_nat_left_right.inputs.out_prefix = subject_ID + '_connect_fs5_nat_left_right'
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_nat_left_right, "index_row_file")
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_nat_left_right, "index_col_file")
        dmripipeline.connect(submatrix_left_right, "mat_matrix_nat",
                             connect_mat_fs5_nat_left_right, "matrix_file")

        connect_mat_fs5_nat_right_left = connect_mat_fs5_nat_left_left.clone(
            name='83_connect_mat_fs5_nat_right_left')
        connect_mat_fs5_nat_right_left.inputs.out_prefix = subject_ID + '_connect_fs5_nat_right_left'
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_nat_right_left, "index_row_file")
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_nat_right_left, "index_col_file")
        dmripipeline.connect(submatrix_right_left, "mat_matrix_nat",
                             connect_mat_fs5_nat_right_left, "matrix_file")

        connect_mat_fs5_nat_right_right = connect_mat_fs5_nat_left_left.clone(
            name='83_connect_mat_fs5_nat_right_right')
        connect_mat_fs5_nat_right_right.inputs.out_prefix = subject_ID + '_connect_fs5_nat_right_right'
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_nat_right_right, "index_row_file")
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_nat_right_right, "index_col_file")
        dmripipeline.connect(submatrix_right_right, "mat_matrix_nat",
                             connect_mat_fs5_nat_right_right, "matrix_file")

        connect_mat_fs5_log_left_left = connect_mat_fs5_nat_left_left.clone(
            name='83_connect_mat_fs5_log_left_left')
        connect_mat_fs5_log_left_left.inputs.out_prefix = subject_ID + '_connect_fs5_log_left_left'
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_log_left_left, "index_row_file")
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_log_left_left, "index_col_file")
        dmripipeline.connect(submatrix_left_left, "mat_matrix_log",
                             connect_mat_fs5_log_left_left, "matrix_file")

        connect_mat_fs5_log_left_right = connect_mat_fs5_log_left_left.clone(
            name='83_connect_mat_fs5_log_left_right')
        connect_mat_fs5_log_left_right.inputs.out_prefix = subject_ID + '_connect_fs5_log_left_right'
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_log_left_right, "index_row_file")
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_log_left_right, "index_col_file")
        dmripipeline.connect(submatrix_left_right, "mat_matrix_log",
                             connect_mat_fs5_log_left_right, "matrix_file")

        connect_mat_fs5_log_right_left = connect_mat_fs5_log_left_left.clone(
            name='83_connect_mat_fs5_log_right_left')
        connect_mat_fs5_log_right_left.inputs.out_prefix = subject_ID + '_connect_fs5_log_right_left'
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_log_right_left, "index_row_file")
        dmripipeline.connect(fs5_indexlist_left, "out_file",
                             connect_mat_fs5_log_right_left, "index_col_file")
        dmripipeline.connect(submatrix_right_left, "mat_matrix_log",
                             connect_mat_fs5_log_right_left, "matrix_file")

        connect_mat_fs5_log_right_right = connect_mat_fs5_log_left_left.clone(
            name='83_connect_mat_fs5_log_right_right')
        connect_mat_fs5_log_right_right.inputs.out_prefix = subject_ID + '_connect_fs5_log_right_right'
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_log_right_right, "index_row_file")
        dmripipeline.connect(fs5_indexlist_right, "out_file",
                             connect_mat_fs5_log_right_right, "index_col_file")
        dmripipeline.connect(submatrix_right_right, "mat_matrix_log",
                             connect_mat_fs5_log_right_right, "matrix_file")

#         connect_mat_fs5_nat_full = connect_mat_fs4_nat_full.clone(name='83_connect_mat_fs5_nat_full')
#         connect_mat_fs5_nat_full.inputs.outfile_prefix = subject_ID + '_connect_fs5_nat_full.mat'
#         connect_mat_fs5_nat_full.run_without_submitting = True
#         dmripipeline.connect(connect_mat_fs5_nat_left_left, "out_nii", connect_mat_fs5_nat_full, "sm_left_left")
#         dmripipeline.connect(connect_mat_fs5_nat_left_right, "out_nii", connect_mat_fs5_nat_full, "sm_left_right")
#         dmripipeline.connect(connect_mat_fs5_nat_right_left, "out_nii", connect_mat_fs5_nat_full, "sm_right_left")
#         dmripipeline.connect(connect_mat_fs5_nat_right_right, "out_nii", connect_mat_fs5_nat_full, "sm_right_right")
#
#         connect_mat_fs5_log_full = connect_mat_fs5_nat_full.clone(name='83_connect_mat_fs5_log_full')
#         connect_mat_fs5_log_full.inputs.out_filename = subject_ID + '_connect_fs5_log_full.mat'
#         connect_mat_fs5_log_full.run_without_submitting = True
#         dmripipeline.connect(connect_mat_fs5_log_left_left, "out_nii", connect_mat_fs5_log_full, "sm_left_left")
#         dmripipeline.connect(connect_mat_fs5_log_left_right, "out_nii", connect_mat_fs5_log_full, "sm_left_right")
#         dmripipeline.connect(connect_mat_fs5_log_right_left, "out_nii", connect_mat_fs5_log_full, "sm_right_left")
#         dmripipeline.connect(connect_mat_fs5_log_right_right, "out_nii", connect_mat_fs5_log_full, "sm_right_right")
#
    """
    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(index_pruned_left, 'out_file', datasink,
                         'interface_index.@3')
    dmripipeline.connect(index_pruned_right, 'out_file', datasink,
                         'interface_index.@4')
    dmripipeline.connect(index_final_2_t1_left, 'out_file', datasink,
                         'interface_index.@5')
    dmripipeline.connect(index_final_2_t1_right, 'out_file', datasink,
                         'interface_index.@6')
    dmripipeline.connect(index_interface2surf_left, 'out_file', datasink,
                         'interface_index.@7')
    dmripipeline.connect(index_interface2surf_right, 'out_file', datasink,
                         'interface_index.@8')
    dmripipeline.connect(index_fsaverage5_left, 'out_file', datasink,
                         'interface_index.@9')
    dmripipeline.connect(index_fsaverage5_right, 'out_file', datasink,
                         'interface_index.@10')
    dmripipeline.connect(fs5_indexlist_left, 'out_file', datasink,
                         'interface_index.@11')
    dmripipeline.connect(fs5_indexlist_right, 'out_file', datasink,
                         'interface_index.@12')
    dmripipeline.connect(index_fsaverage4_left, 'out_file', datasink,
                         'interface_index.@13')
    dmripipeline.connect(index_fsaverage4_right, 'out_file', datasink,
                         'interface_index.@14')
    dmripipeline.connect(fs4_indexlist_left, 'out_file', datasink,
                         'interface_index.@15')
    dmripipeline.connect(fs4_indexlist_right, 'out_file', datasink,
                         'interface_index.@16')
    dmripipeline.connect(fs_indexlist_left, 'out_file', datasink,
                         'interface_index.@17')
    dmripipeline.connect(fs_indexlist_right, 'out_file', datasink,
                         'interface_index.@18')
    dmripipeline.connect(tract_exclusion_mask, 'outfile', datasink,
                         'interface_index.@19')

    #    dmripipeline.connect(submatrix_left_left, 'mat_matrix_nat', datasink, 'connect_matrix.native.mat')
    #    dmripipeline.connect(submatrix_left_left, 'mat_matrix_log', datasink, 'connect_matrix.native.mat.@2')
    dmripipeline.connect(submatrix_left_left, 'nii_matrix_nat', datasink,
                         'connect_matrix.native.@3')
    dmripipeline.connect(submatrix_left_left, 'nii_matrix_log', datasink,
                         'connect_matrix.native.@4')
    #    dmripipeline.connect(submatrix_right_right, 'mat_matrix_nat', datasink, 'connect_matrix.native.mat.@5')
    #    dmripipeline.connect(submatrix_right_right, 'mat_matrix_log', datasink, 'connect_matrix.native.mat.@6')
    dmripipeline.connect(submatrix_right_right, 'nii_matrix_nat', datasink,
                         'connect_matrix.native.@7')
    dmripipeline.connect(submatrix_right_right, 'nii_matrix_log', datasink,
                         'connect_matrix.native.@8')
    #    dmripipeline.connect(submatrix_left_right, 'mat_matrix_nat', datasink, 'connect_matrix.native.mat')
    #    dmripipeline.connect(submatrix_left_right, 'mat_matrix_log', datasink, 'connect_matrix.native.mat.@2')
    dmripipeline.connect(submatrix_left_right, 'nii_matrix_nat', datasink,
                         'connect_matrix.native.@9')
    dmripipeline.connect(submatrix_left_right, 'nii_matrix_log', datasink,
                         'connect_matrix.native.@10')
    #    dmripipeline.connect(submatrix_right_left, 'mat_matrix_nat', datasink, 'connect_matrix.native.mat')
    #    dmripipeline.connect(submatrix_right_left, 'mat_matrix_log', datasink, 'connect_matrix.native.mat.@2')
    dmripipeline.connect(submatrix_right_left, 'nii_matrix_nat', datasink,
                         'connect_matrix.native.@11')
    dmripipeline.connect(submatrix_right_left, 'nii_matrix_log', datasink,
                         'connect_matrix.native.@12')

    #     dmripipeline.connect(full_matrix_nat, 'out_file', datasink, 'connect_matrix.native.@9')
    #     dmripipeline.connect(full_matrix_log, 'out_file', datasink, 'connect_matrix.native.@11')

    if (not use_sample):
        #        dmripipeline.connect(connect_mat_fs4_nat_left_left, 'out_mat', datasink, 'connect_matrix.fs4.mat.@1')
        #        dmripipeline.connect(connect_mat_fs4_log_left_left, 'out_mat', datasink, 'connect_matrix.fs4.mat.@2')
        #        dmripipeline.connect(connect_mat_fs4_nat_right_right, 'out_mat', datasink, 'connect_matrix.fs4.mat.@3')
        #        dmripipeline.connect(connect_mat_fs4_log_right_right, 'out_mat', datasink, 'connect_matrix.fs4.mat.@4')
        #        dmripipeline.connect(connect_mat_fs4_nat_left_right, 'out_mat', datasink, 'connect_matrix.fs4.mat.@5')
        #        dmripipeline.connect(connect_mat_fs4_log_left_right, 'out_mat', datasink, 'connect_matrix.fs4.mat.@6')
        #        dmripipeline.connect(connect_mat_fs4_nat_right_left, 'out_mat', datasink, 'connect_matrix.fs4.mat.@7')
        #        dmripipeline.connect(connect_mat_fs4_log_right_left, 'out_mat', datasink, 'connect_matrix.fs4.mat.@8')

        dmripipeline.connect(connect_mat_fs4_nat_left_left, 'out_nii',
                             datasink, 'connect_matrix.fs4.@1')
        dmripipeline.connect(connect_mat_fs4_log_left_left, 'out_nii',
                             datasink, 'connect_matrix.fs4.@2')
        dmripipeline.connect(connect_mat_fs4_nat_right_right, 'out_nii',
                             datasink, 'connect_matrix.fs4.@3')
        dmripipeline.connect(connect_mat_fs4_log_right_right, 'out_nii',
                             datasink, 'connect_matrix.fs4.@4')
        dmripipeline.connect(connect_mat_fs4_nat_left_right, 'out_nii',
                             datasink, 'connect_matrix.fs4.@5')
        dmripipeline.connect(connect_mat_fs4_log_left_right, 'out_nii',
                             datasink, 'connect_matrix.fs4.@6')
        dmripipeline.connect(connect_mat_fs4_nat_right_left, 'out_nii',
                             datasink, 'connect_matrix.fs4.@7')
        dmripipeline.connect(connect_mat_fs4_log_right_left, 'out_nii',
                             datasink, 'connect_matrix.fs4.@8')

        #         dmripipeline.connect(connect_mat_fs4_nat_full, 'out_file', datasink, 'connect_matrix.@28')
        #         dmripipeline.connect(connect_mat_fs4_log_full, 'out_file', datasink, 'connect_matrix.@30')

        #        dmripipeline.connect(connect_mat_fs5_nat_left_left, 'out_mat', datasink, 'connect_matrix.fs5.mat.@1')
        #        dmripipeline.connect(connect_mat_fs5_log_left_left, 'out_mat', datasink, 'connect_matrix.fs5.mat.@2')
        #        dmripipeline.connect(connect_mat_fs5_nat_right_right, 'out_mat', datasink, 'connect_matrix.fs5.mat.@3')
        #        dmripipeline.connect(connect_mat_fs5_log_right_right, 'out_mat', datasink, 'connect_matrix.fs5.mat.@4')
        #        dmripipeline.connect(connect_mat_fs5_nat_left_right, 'out_mat', datasink, 'connect_matrix.fs5.mat.@5')
        #        dmripipeline.connect(connect_mat_fs5_log_left_right, 'out_mat', datasink, 'connect_matrix.fs5.mat.@6')
        #        dmripipeline.connect(connect_mat_fs5_nat_right_left, 'out_mat', datasink, 'connect_matrix.fs5.mat.@7')
        #        dmripipeline.connect(connect_mat_fs5_log_right_left, 'out_mat', datasink, 'connect_matrix.fs5.mat.@8')

        dmripipeline.connect(connect_mat_fs5_nat_left_left, 'out_nii',
                             datasink, 'connect_matrix.fs5.@1')
        dmripipeline.connect(connect_mat_fs5_log_left_left, 'out_nii',
                             datasink, 'connect_matrix.fs5.@2')
        dmripipeline.connect(connect_mat_fs5_nat_right_right, 'out_nii',
                             datasink, 'connect_matrix.fs5.@3')
        dmripipeline.connect(connect_mat_fs5_log_right_right, 'out_nii',
                             datasink, 'connect_matrix.fs5.@4')
        dmripipeline.connect(connect_mat_fs5_nat_left_right, 'out_nii',
                             datasink, 'connect_matrix.fs5.@5')
        dmripipeline.connect(connect_mat_fs5_log_left_right, 'out_nii',
                             datasink, 'connect_matrix.fs5.@6')
        dmripipeline.connect(connect_mat_fs5_nat_right_left, 'out_nii',
                             datasink, 'connect_matrix.fs5.@7')
        dmripipeline.connect(connect_mat_fs5_log_right_left, 'out_nii',
                             datasink, 'connect_matrix.fs5.@8')

#         dmripipeline.connect(connect_mat_fs5_nat_full, 'out_file', datasink, 'connect_matrix.@40')
#         dmripipeline.connect(connect_mat_fs5_log_full, 'out_file', datasink, 'connect_matrix.@42')
    """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """
    ===============================================================================
    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_pipe3_projection")

    connectprepro.base_dir = op.abspath(workflow_dir + "/workflow_" +
                                        subject_ID)
    connectprepro.connect([(datasource, dmripipeline,
                            [('wm', 'inputnode.wm'),
                             ('seeds_left', 'inputnode.seeds_left'),
                             ('seeds_right', 'inputnode.seeds_right'),
                             ('t1', 'inputnode.t1'),
                             ('warp', 'inputnode.warp'),
                             ('inv_flirt_mat', 'inputnode.inv_flirt_mat'),
                             ('fa', 'inputnode.fa'),
                             ('index_left', 'inputnode.index_left'),
                             ('index_right', 'inputnode.index_right')]),
                           (tracts_left_source, dmripipeline,
                            [('tracts_left', 'inputnode.tracts_left')]),
                           (tracts_right_source, dmripipeline,
                            [('tracts_right', 'inputnode.tracts_right')])])

    return connectprepro
Ejemplo n.º 6
0
def create_surface_projection_workflow(name="surface_projection"):

    # Define the workflow inputs
    inputnode = pe.Node(util.IdentityInterface(
        fields=["subject_id", "timeseries", "tkreg_affine", "smooth_fwhm"]),
                        name="inputs")

    # Set up a hemisphere iterable
    hemisource = pe.Node(util.IdentityInterface(fields=["hemi"]),
                         iterables=("hemi", ["lh", "rh"]),
                         name="hemisource")

    # Project data onto the surface mesh
    surfproject = pe.MapNode(fs.SampleToSurface(sampling_range=(0, 1, .1),
                                                sampling_units="frac",
                                                cortex_mask=True),
                             iterfield=["source_file", "reg_file"],
                             name="surfproject")
    surfproject.inputs.sampling_method = "average"

    # Apply the spherical warp to the data to bring into fsaverage space
    surftransform = pe.MapNode(fs.SurfaceTransform(target_subject="fsaverage",
                                                   reshape=True),
                               iterfield=["source_file"],
                               name="surftransform")

    # Smooth the data along the surface
    smoothnormsurf = pe.MapNode(fs.SurfaceSmooth(subject_id="fsaverage",
                                                 reshape=True),
                                iterfield=["in_file"],
                                name="smoothnormsurf")

    # Convert the fsaverage surface to nifti
    cvtnormsurf = pe.MapNode(fs.MRIConvert(out_type="niigz"),
                             iterfield=["in_file"],
                             name="convertnormsurf")

    # Rename the timeseries
    rename = pe.MapNode(util.Rename(
        format_string="%(hemi)s.timeseries.fsaverage", keep_ext=True),
                        iterfield=["in_file"],
                        name="rename")

    # Define the outputs
    outputnode = pe.Node(util.IdentityInterface(fields=["timeseries"]),
                         name="outputs")

    # Define and connect the workflow
    tosurf = pe.Workflow(name=name)
    tosurf.connect([
        (inputnode, surfproject, [("timeseries", "source_file"),
                                  ("subject_id", "subject_id"),
                                  ("tkreg_affine", "reg_file")]),
        (hemisource, surfproject, [("hemi", "hemi")]),
        (surfproject, surftransform, [("out_file", "source_file")]),
        (inputnode, surftransform, [("subject_id", "source_subject")]),
        (hemisource, surftransform, [("hemi", "hemi")]),
        (surftransform, smoothnormsurf, [("out_file", "in_file")]),
        (hemisource, smoothnormsurf, [("hemi", "hemi")]),
        (inputnode, smoothnormsurf, [("smooth_fwhm", "fwhm")]),
        (smoothnormsurf, cvtnormsurf, [("out_file", "in_file")]),
        (cvtnormsurf, rename, [("out_file", "in_file")]),
        (hemisource, rename, [("hemi", "hemi")]),
        (rename, outputnode, [("out_file", "timeseries")]),
    ])

    return tosurf
Ejemplo n.º 7
0
def segstats_workflow(c, name='segstats'):
    import nipype.interfaces.fsl as fsl
    import nipype.interfaces.freesurfer as fs
    import nipype.interfaces.io as nio
    import nipype.pipeline.engine as pe
    from .scripts.u0a14c5b5899911e1bca80023dfa375f2.modified_nipype_workflows import create_get_stats_flow
    from .scripts.u0a14c5b5899911e1bca80023dfa375f2.utils import tolist, pickidx
    if not c.use_annotation:
        workflow = create_get_stats_flow(name='segstats', withreg=c.use_reg)
    else:
        workflow = create_get_stats_flow(name='segstats')

    workflow.inputs.segstats.avgwf_txt_file = True
    datagrabber = c.datagrabber.create_dataflow()
    merge = pe.Node(fsl.Merge(dimension='t'), name='merge_files')
    inputspec = workflow.get_node('inputspec')
    subject_iterable = datagrabber.get_node("subject_id_iterable")
    # merge files grabbed
    stats = workflow.get_node('segstats')
    print "colortablefile:", c.color_table_file
    if c.color_table_file == "Default":
        stats.inputs.default_color_table = True
    elif c.color_table_file == "Color_Table":
        stats.inputs.color_table_file = c.color_file
    elif c.color_table_file == "GCA_color_table":
        stats.inputs.gca_color_table = c.color_file

    workflow.connect(datagrabber, ('datagrabber.in_files', tolist), merge,
                     'in_files')

    if c.use_annotation:

        surf2surf = pe.MapNode(fs.SurfaceTransform(
            source_subject=c.annot_space, subjects_dir=c.surf_dir),
                               name="surf2surf",
                               iterfield=['hemi', 'source_annot_file'])
        surf2surf.inputs.source_annot_file = [c.lh_annotation, c.rh_annotation]
        workflow.connect(subject_iterable, "subject_id", surf2surf,
                         "target_subject")
        surf2surf.inputs.hemi = ['lh', 'rh']

        add = pe.Node(fsl.BinaryMaths(operation='add'), name="add")
        workflow.connect(add, 'out_file', inputspec, "label_file")
        label2vol = pe.MapNode(fs.Label2Vol(subjects_dir=c.surf_dir,
                                            proj=c.proj),
                               name='label2vol',
                               iterfield=["hemi", "annot_file"])
        workflow.connect(surf2surf, "out_file", label2vol, "annot_file")
        workflow.connect(subject_iterable, "subject_id", label2vol,
                         "subject_id")
        #fssource = pe.Node(nio.FreeSurferSource(subjects_dir = c.surf_dir),name='fssource')
        #workflow.connect(subject_iterable,"subject_id",fssource,"subject_id")
        #workflow.connect(subject_iterable,"subject_id",label2vol,"reg_header")
        #workflow.connect(fssource,"orig",label2vol,"template_file")
        workflow.connect(merge, "merged_file", label2vol, "template_file")
        label2vol.inputs.hemi = ['lh', 'rh']
        workflow.connect(datagrabber, 'datagrabber.reg_file', label2vol,
                         'reg_file')
        if c.inverse_reg:
            label2vol.inputs.invert_mtx = c.inverse_reg
        workflow.connect(label2vol, ('vol_label_file', pickidx, 0), add,
                         'in_file')
        workflow.connect(label2vol, ('vol_label_file', pickidx, 1), add,
                         'operand_file')

    workflow.connect(merge, 'merged_file', inputspec, 'source_file')

    if c.use_reg and not c.use_annotation:
        workflow.connect(datagrabber, 'datagrabber.reg_file', inputspec,
                         'reg_file')
        workflow.inputs.inputspec.subjects_dir = c.surf_dir
        workflow.inputs.inputspec.inverse = c.inverse_reg

    if c.use_standard_label and not c.use_annotation:
        workflow.inputs.inputspec.label_file = c.label_file
    elif not c.use_standard_label and not c.use_annotation:
        workflow.connect(datagrabber, 'datagrabber.label_file', inputspec,
                         "label_file")

    sinker = pe.Node(nio.DataSink(), name='sinker')
    sinker.inputs.base_directory = c.sink_dir

    workflow.connect(subject_iterable, 'subject_id', sinker, 'container')

    def get_subs(subject_id):
        subs = [('_subject_id_%s' % subject_id, '')]
        return subs

    workflow.connect(subject_iterable, ('subject_id', get_subs), sinker,
                     'substitutions')
    outputspec = workflow.get_node('outputspec')
    workflow.connect(outputspec, 'stats_file', sinker, 'segstats.@stats')
    workflow.connect(stats, "avgwf_txt_file", sinker, 'segstats.@avg')

    return workflow
Ejemplo n.º 8
0
def get_wf():
    import numpy as np

    wf = pe.Workflow(name="main_workflow")
    wf.base_dir = os.path.join(workingdir, "rs_preprocessing/")
    wf.config['execution']['crashdump_dir'] = wf.base_dir + "crash_files/"

    ##Infosource##
    subject_id_infosource = pe.Node(
        util.IdentityInterface(fields=['subject_id']),
        name="subject_id_infosource")
    subject_id_infosource.iterables = ('subject_id', subjects)

    session_infosource = pe.Node(util.IdentityInterface(fields=['session']),
                                 name="session_infosource")
    session_infosource.iterables = ('session', sessions)

    hemi_infosource = pe.Node(util.IdentityInterface(fields=['hemi']),
                              name="hemi_infosource")
    hemi_infosource.iterables = ('hemi', hemispheres)

    fs_infosource = pe.Node(util.IdentityInterface(fields=['fs']),
                            name="fs_infosource")
    fs_infosource.iterables = ('fs', fsaverage)

    ##Datagrabber##
    datagrabber = pe.Node(nio.DataGrabber(
        infields=['subject_id'],
        outfields=['resting_nifti', 't1_nifti', 'dicoms']),
                          name="datagrabber",
                          overwrite=True)
    datagrabber.inputs.base_directory = '/'
    datagrabber.inputs.template = '*'
    datagrabber.inputs.field_template = rs_preprocessing_dg_template
    datagrabber.inputs.template_args = rs_preprocessing_dg_args
    datagrabber.inputs.sort_filelist = True

    wf.connect(subject_id_infosource, 'subject_id', datagrabber, 'subject_id')
    wf.connect(session_infosource, 'session', datagrabber, 'session')

    ##DcmStack & MetaData##
    dicom_filedir = pe.Node(name='dicom_filedir',
                            interface=util.Function(
                                input_names=['subject_id', 'session'],
                                output_names=['filedir'],
                                function=construct_dicomfiledir))

    stack = pe.Node(dcm.DcmStack(), name='stack')
    stack.inputs.embed_meta = True

    tr_lookup = pe.Node(dcm.LookupMeta(), name='tr_lookup')
    tr_lookup.inputs.meta_keys = {'RepetitionTime': 'TR'}

    wf.connect(subject_id_infosource, 'subject_id', dicom_filedir,
               'subject_id')
    wf.connect(session_infosource, 'session', dicom_filedir, 'session')

    wf.connect(dicom_filedir, 'filedir', stack, 'dicom_files')
    wf.connect(stack, 'out_file', tr_lookup, 'in_file')

    ##Preproc from BIPs##
    preproc = create_rest_prep(name="bips_resting_preproc", fieldmap=False)
    zscore = preproc.get_node('z_score')
    preproc.remove_nodes([zscore])
    mod_realign = preproc.get_node('mod_realign')
    mod_realign.plugin_args = {'submit_specs': 'request_memory=4000\n'}
    #workaround for realignment crashing in multiproc environment
    mod_realign.run_without_submitting = True

    # inputs
    preproc.inputs.inputspec.motion_correct_node = 'nipy'
    ad = preproc.get_node('artifactdetect')
    preproc.disconnect(mod_realign, 'parameter_source', ad, 'parameter_source')
    ad.inputs.parameter_source = 'NiPy'
    preproc.inputs.inputspec.realign_parameters = {
        "loops": [5],
        "speedup": [5]
    }
    preproc.inputs.inputspec.do_whitening = False
    preproc.inputs.inputspec.timepoints_to_remove = 4
    preproc.inputs.inputspec.smooth_type = 'susan'
    preproc.inputs.inputspec.do_despike = False
    preproc.inputs.inputspec.surface_fwhm = 0.0
    preproc.inputs.inputspec.num_noise_components = 6
    preproc.inputs.inputspec.regress_before_PCA = False
    preproc.get_node('fwhm_input').iterables = ('fwhm', [0, 5])
    preproc.get_node('take_mean_art').get_node(
        'strict_artifact_detect').inputs.save_plot = True
    preproc.inputs.inputspec.ad_normthresh = 1
    preproc.inputs.inputspec.ad_zthresh = 3
    preproc.inputs.inputspec.do_slicetime = True
    preproc.inputs.inputspec.compcor_select = [True, True]
    preproc.inputs.inputspec.filter_type = 'fsl'
    preproc.get_node('bandpass_filter').iterables = [('highpass_freq', [0.01]),
                                                     ('lowpass_freq', [0.1])]
    preproc.inputs.inputspec.reg_params = [
        True, True, True, False, True, False
    ]
    preproc.inputs.inputspec.fssubject_dir = freesurferdir

    #preproc.inputs.inputspec.tr = 1400/1000
    #preproc.inputs.inputspec.motion_correct_node = 'afni'
    #preproc.inputs.inputspec.sliceorder = slicetime_file
    #preproc.inputs.inputspec.sliceorder = list(np.linspace(0,1.4,64))

    def convert_units(tr):
        mstr = (tr * .001)
        return tr

    def get_sliceorder(in_file):
        import nipype.interfaces.dcmstack as dcm
        import numpy as np
        nii_wrp = dcm.NiftiWrapper.from_filename(in_file)
        sliceorder = np.argsort(
            np.argsort(
                nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes')
                [0])).tolist()
        return sliceorder

    wf.connect(tr_lookup, ("TR", convert_units), preproc, "inputspec.tr")
    wf.connect(stack, ('out_file', get_sliceorder), preproc,
               "inputspec.sliceorder")
    wf.connect(subject_id_infosource, 'subject_id', preproc,
               "inputspec.fssubject_id")
    wf.connect(datagrabber, "resting_nifti", preproc, "inputspec.func")

    ##Sampler##
    sampler = pe.Node(fs.SampleToSurface(), name='sampler')
    sampler.inputs.sampling_method = 'average'
    sampler.inputs.sampling_range = (0.2, 0.8, 0.1)
    sampler.inputs.sampling_units = 'frac'
    sampler.inputs.interp_method = 'nearest'
    sampler.inputs.out_type = 'nii'

    wf.connect(preproc, ('bandpass_filter.out_file', list_to_filename),
               sampler, 'source_file')
    wf.connect(preproc, ('getmask.register.out_reg_file', list_to_filename),
               sampler, 'reg_file')
    wf.connect(hemi_infosource, 'hemi', sampler, 'hemi')

    ##SXFM##
    sxfm = pe.Node(fs.SurfaceTransform(), name='sxfm')
    sxfm.inputs.args = '--cortex --fwhm-src 5 --noreshape'
    sxfm.inputs.target_type = 'nii'

    wf.connect(sampler, 'out_file', sxfm, 'source_file')
    wf.connect(subject_id_infosource, 'subject_id', sxfm, 'source_subject')
    wf.connect(hemi_infosource, 'hemi', sxfm, 'hemi')
    wf.connect(fs_infosource, 'fs', sxfm, 'target_subject')

    ###########

    #report_wf = create_preproc_report_wf(os.path.join(preprocdir, "reports"))
    #report_wf.inputs.inputspec.fssubjects_dir = preproc.inputs.inputspec.fssubject_dir

    def pick_full_brain_ribbon(l):
        import os
        for path in l:
            if os.path.split(path)[1] == "ribbon.mgz":
                return path

    #wf.connect(preproc,"artifactdetect.plot_files", report_wf, "inputspec.art_detect_plot")
    #wf.connect(preproc,"take_mean_art.weighted_mean.mean_image", report_wf, "inputspec.mean_epi")
    #wf.connect(preproc,("getmask.register.out_reg_file", list_to_filename), report_wf, "inputspec.reg_file")
    #wf.connect(preproc,("getmask.fssource.ribbon",pick_full_brain_ribbon), report_wf, "inputspec.ribbon")
    #wf.connect(preproc,("CompCor.tsnr.tsnr_file", list_to_filename), report_wf, "inputspec.tsnr_file")
    #wf.connect(subject_id_infosource, 'subject_id', report_wf, "inputspec.subject_id")


##Datasink##
    ds = pe.Node(nio.DataSink(), name="datasink")
    ds.inputs.base_directory = os.path.join(preprocdir, "aimivolumes")
    wf.connect(preproc, 'bandpass_filter.out_file', ds, "preprocessed_resting")
    wf.connect(preproc, 'getmask.register.out_fsl_file', ds,
               "func2anat_transform")
    wf.connect(sampler, 'out_file', ds, 'sampledtosurf')
    wf.connect(sxfm, 'out_file', ds, 'sxfmout')
    #wf.write_graph()
    return wf
Ejemplo n.º 9
0
def create_ml_preprocess_workflow(
        name,
        project_dir,
        work_dir,
        sessions_file,
        session_template,
        fs_dir,
        annot_template='{subject_id}/label/{hemi}.aparc.a2009s.annot',
        fwhm_vals=[2],
        ico_order_vals=[4],
        do_save_vol_ds=False,
        do_save_smooth_vol_ds=False,
        do_save_surface_smooth_vol_ds=False,
        do_save_surface_ds=False,
        do_save_smooth_surface_ds=False,
        do_save_sphere_nifti=False,
        do_save_sphere_ds=True,
        do_save_join_sessions_ds=True,
        do_save_join_subjects_ds=True):

    #initialize workflow
    workflow = pe.Workflow(name=name)
    workflow.base_dir = work_dir

    sessions_info = ColumnData(sessions_file, dtype=str)
    subject_ids = set(sessions_info['subject_id'])
    session_map = [
        (sid, [s for i, s, r in zip(*sessions_info.values()) if i == sid])
        for sid in subject_ids
    ]

    ##for each subject
    subjects = pe.Node(interface=util.IdentityInterface(fields=['subject_id']),
                       name='subjects')
    subjects.iterables = [('subject_id', subject_ids)]

    ##for each session
    sessions = pe.Node(
        interface=util.IdentityInterface(fields=['subject_id', 'session_dir']),
        name='sessions')
    sessions.itersource = ('subjects', 'subject_id')
    sessions.iterables = [('session_dir', dict(session_map))]
    workflow.connect(subjects, 'subject_id', sessions, 'subject_id')

    #get session directory
    get_session_dir = pe.Node(interface=nio.SelectFiles(session_template),
                              name='get_session_dir')
    workflow.connect(sessions, 'session_dir', get_session_dir, 'session_dir')

    #save outputs
    datasink = pe.Node(nio.DataSink(), name='datasink')
    datasink.inputs.parameterization = False
    workflow.connect(get_session_dir, 'session_dir', datasink,
                     'base_directory')

    template = {
        'nifti_file': 'mri/f.nii.gz',
        'attributes_file': 'attributes.txt',
        'reg_file': 'mri/transforms/functional_to_anatomy.dat'
    }
    get_files = pe.Node(nio.SelectFiles(template), name='get_files')
    workflow.connect(get_session_dir, 'session_dir', get_files,
                     'base_directory')

    vol_to_ds = pe.Node(nmutil.NiftiToDataset(), name='vol_to_ds')
    vol_to_ds.inputs.ds_file = 'vol.hdf5'

    workflow.connect(get_files, 'nifti_file', vol_to_ds, 'nifti_file')
    workflow.connect(get_files, 'attributes_file', vol_to_ds,
                     'attributes_file')
    workflow.connect(subjects, 'subject_id', vol_to_ds, 'subject_id')
    workflow.connect(sessions, 'session_dir', vol_to_ds, 'session_id')

    if do_save_vol_ds:
        workflow.connect(vol_to_ds, 'ds_file', datasink, 'ml.@vol')

    fwhm = pe.Node(util.IdentityInterface(fields=['fwhm']), name='fwhm')
    fwhm.iterables = [('fwhm', fwhm_vals)]

    if do_save_smooth_vol_ds:
        smooth_vol = pe.Node(interface=fs.MRIConvert(), name='smooth_vol')
        workflow.connect(get_files, 'nifti_file', smooth_vol, 'in_file')
        workflow.connect(fwhm, 'fwhm', smooth_vol, 'fwhm')

        smooth_vol_to_ds = pe.Node(nmutil.NiftiToDataset(),
                                   name='smooth_vol_to_ds')
        smooth_vol_to_ds.inputs.ds_file = 'smooth_vol.hdf5'

        workflow.connect(smooth_vol, 'out_file', smooth_vol_to_ds,
                         'nifti_file')
        workflow.connect(get_files, 'attributes_file', smooth_vol_to_ds,
                         'attributes_file')
        workflow.connect(subjects, 'subject_id', smooth_vol_to_ds,
                         'subject_id')
        workflow.connect(sessions, 'session_dir', smooth_vol_to_ds,
                         'session_id')

        workflow.connect(smooth_vol_to_ds, 'ds_file', datasink,
                         'ml.@smooth_vol')

    if do_save_surface_smooth_vol_ds:
        surface_smooth_vol = pe.Node(interface=fs.Smooth(),
                                     name='surface_smooth_vol')
        workflow.connect(get_files, 'reg_file', surface_smooth_vol, 'reg_file')
        workflow.connect(get_files, 'nifti_file', surface_smooth_vol,
                         'in_file')
        workflow.connect(fwhm, 'fwhm', surface_smooth_vol, 'surface_fwhm')

        surface_smooth_vol_to_ds = pe.Node(nmutil.NiftiToDataset(),
                                           name='surface_smooth_vol_to_ds')
        surface_smooth_vol_to_ds.inputs.ds_file = 'surface_smooth_vol.hdf5'

        workflow.connect(surface_smooth_vol, 'out_file',
                         surface_smooth_vol_to_ds, 'nifti_file')
        workflow.connect(get_files, 'attributes_file',
                         surface_smooth_vol_to_ds, 'attributes_file')
        workflow.connect(subjects, 'subject_id', surface_smooth_vol_to_ds,
                         'subject_id')
        workflow.connect(sessions, 'session_dir', surface_smooth_vol_to_ds,
                         'session_id')

        workflow.connect(surface_smooth_vol_to_ds, 'ds_file', datasink,
                         'ml.@surface_smooth_vol')

    hemi = pe.Node(util.IdentityInterface(fields=['hemi']), name='hemi')
    hemi.iterables = [('hemi', ['lh', 'rh'])]

    to_surface = pe.Node(fs.SampleToSurface(), name='to_surface')
    to_surface.inputs.sampling_method = 'average'
    to_surface.inputs.sampling_range = (0., 1., 0.1)
    to_surface.inputs.sampling_units = 'frac'
    to_surface.inputs.subjects_dir = fs_dir
    workflow.connect(hemi, 'hemi', to_surface, 'hemi')
    workflow.connect(get_files, 'nifti_file', to_surface, 'source_file')
    workflow.connect(get_files, 'reg_file', to_surface, 'reg_file')

    if do_save_surface_ds:
        surface_to_ds = pe.Node(nmutil.NiftiToDataset(), name='surface_to_ds')
        workflow.connect(to_surface, 'out_file', surface_to_ds, 'nifti_file')
        workflow.connect(get_files, 'attributes_file', surface_to_ds,
                         'attributes_file')
        workflow.connect(subjects, 'subject_id', surface_to_ds, 'subject_id')
        workflow.connect(sessions, 'session_dir', surface_to_ds, 'session_id')

        join_surfaces = pe.JoinNode(nmutil.JoinDatasets(),
                                    name='join_surfaces',
                                    joinsource='hemi',
                                    joinfield='input_datasets')
        join_surfaces.inputs.joined_dataset = 'surface.hdf5'
        join_surfaces.inputs.join_hemispheres = True
        workflow.connect(surface_to_ds, 'ds_file', join_surfaces,
                         'input_datasets')

        workflow.connect(join_surfaces, 'joined_dataset', datasink,
                         'ml.@surface')

    smooth_surface = pe.Node(fs.SurfaceSmooth(), name='smooth_surface')
    smooth_surface.inputs.subjects_dir = fs_dir
    workflow.connect(to_surface, 'out_file', smooth_surface, 'in_file')
    workflow.connect(sessions, 'subject_id', smooth_surface, 'subject_id')
    workflow.connect(hemi, 'hemi', smooth_surface, 'hemi')
    workflow.connect(fwhm, 'fwhm', smooth_surface, 'fwhm')

    if do_save_smooth_surface_ds:
        smooth_surface_to_ds = pe.Node(nmutil.NiftiToDataset(),
                                       name='smooth_surface_to_ds')
        workflow.connect(smooth_surface, 'out_file', smooth_surface_to_ds,
                         'nifti_file')
        workflow.connect(get_files, 'attributes_file', smooth_surface_to_ds,
                         'attributes_file')
        workflow.connect(subjects, 'subject_id', smooth_surface_to_ds,
                         'subject_id')
        workflow.connect(sessions, 'session_dir', smooth_surface_to_ds,
                         'session_id')

        join_smooth_surfaces = pe.JoinNode(nmutil.JoinDatasets(),
                                           name='join_smooth_surfaces',
                                           joinsource='hemi',
                                           joinfield='input_datasets')
        join_smooth_surfaces.inputs.joined_dataset = 'smooth_surface.hdf5'
        join_smooth_surfaces.inputs.join_hemispheres = True
        workflow.connect(smooth_surface_to_ds, 'ds_file', join_smooth_surfaces,
                         'input_datasets')

        workflow.connect(join_smooth_surfaces, 'joined_dataset', datasink,
                         'ml.@smooth_surface')

    ico_order = pe.Node(util.IdentityInterface(fields=['ico_order']),
                        name='ico_order')
    ico_order.iterables = [('ico_order', ico_order_vals)]

    to_sphere = pe.Node(fs.SurfaceTransform(), name='to_sphere')
    to_sphere.inputs.target_subject = 'ico'
    to_sphere.inputs.subjects_dir = fs_dir
    workflow.connect(hemi, 'hemi', to_sphere, 'hemi')
    workflow.connect(smooth_surface, 'out_file', to_sphere, 'source_file')
    workflow.connect(subjects, 'subject_id', to_sphere, 'source_subject')
    workflow.connect(ico_order, 'ico_order', to_sphere, 'target_ico_order')

    if do_save_sphere_nifti:
        workflow.connect(to_sphere, 'out_file', datasink, 'surf.@sphere')

    template = {'annot_file': annot_template}
    get_annot_file = pe.Node(nio.SelectFiles(template), name='get_annot_file')
    get_annot_file.inputs.base_directory = fs_dir
    get_annot_file.inputs.subject_id = 'fsaverage'
    workflow.connect(hemi, 'hemi', get_annot_file, 'hemi')

    transform_annot = pe.Node(fs.SurfaceTransform(), name='transform_annot')
    transform_annot.inputs.source_subject = 'fsaverage'
    transform_annot.inputs.target_subject = 'ico'
    transform_annot.inputs.subjects_dir = fs_dir
    workflow.connect(hemi, 'hemi', transform_annot, 'hemi')
    workflow.connect(get_annot_file, 'annot_file', transform_annot,
                     'source_annot_file')
    workflow.connect(ico_order, 'ico_order', transform_annot,
                     'target_ico_order')

    sphere_to_ds = pe.Node(nmutil.NiftiToDataset(), name='sphere_to_ds')
    workflow.connect(to_sphere, 'out_file', sphere_to_ds, 'nifti_file')
    workflow.connect(get_files, 'attributes_file', sphere_to_ds,
                     'attributes_file')
    workflow.connect(transform_annot, 'out_file', sphere_to_ds, 'annot_file')
    workflow.connect(subjects, 'subject_id', sphere_to_ds, 'subject_id')
    workflow.connect(sessions, 'session_dir', sphere_to_ds, 'session_id')

    join_hemispheres = pe.JoinNode(nmutil.JoinDatasets(),
                                   name='join_hemispheres',
                                   joinsource='hemi',
                                   joinfield='input_datasets')
    join_hemispheres.inputs.joined_dataset = 'sphere.hdf5'
    join_hemispheres.inputs.join_hemispheres = True

    workflow.connect(sphere_to_ds, 'ds_file', join_hemispheres,
                     'input_datasets')

    if do_save_sphere_ds:
        workflow.connect(join_hemispheres, 'joined_dataset', datasink,
                         'ml.@sphere')

    join_sessions = pe.JoinNode(nmutil.JoinDatasets(),
                                name='join_sessions',
                                joinsource='sessions',
                                joinfield='input_datasets')
    workflow.connect(join_hemispheres, 'joined_dataset', join_sessions,
                     'input_datasets')

    if do_save_join_sessions_ds:
        join_sessions_sink = pe.Node(nio.DataSink(), name='join_sessions_sink')
        join_sessions_sink.inputs.parameterization = False
        join_sessions_sink.inputs.base_directory = os.path.join(
            project_dir, 'ml')
        workflow.connect(subjects, 'subject_id', join_sessions_sink,
                         'container')
        workflow.connect(join_sessions, 'joined_dataset', join_sessions_sink,
                         '@join_sessions')

    join_subjects = pe.JoinNode(nmutil.JoinDatasets(),
                                name='join_subjects',
                                joinsource='subjects',
                                joinfield='input_datasets')
    workflow.connect(join_sessions, 'joined_dataset', join_subjects,
                     'input_datasets')

    if do_save_join_subjects_ds:
        join_subjects_sink = pe.Node(nio.DataSink(), name='join_subjects_sink')
        join_subjects_sink.inputs.parameterization = False
        join_subjects_sink.inputs.base_directory = os.path.join(
            project_dir, 'ml')
        workflow.connect(join_subjects, 'joined_dataset', join_subjects_sink,
                         '@join_subjects')

    return workflow