Пример #1
0
def create_cmats_to_csv_pipeline(name="cmats_to_csv",
                                 extra_column_heading="subject"):
    """Creates a workflow to convert the outputs from CreateMatrix into a single
    comma-separated value text file. An extra column / field is also added to the
    text file. Typically, the user would connect the subject name to this field.

    Example
    -------

    >>> from nipype.workflows.dmri.connectivity.nx import create_cmats_to_csv_pipeline
    >>> csv = create_cmats_to_csv_pipeline("cmats_to_csv", "subject_id")
    >>> csv.inputs.inputnode.extra_field = 'subj1'
    >>> csv.inputs.inputnode.matlab_matrix_files = ['subj1_cmatrix.mat', 'subj1_mean_fiber_length.mat', 'subj1_median_fiber_length.mat', 'subj1_fiber_length_std.mat']
    >>> csv.run()                 # doctest: +SKIP

    Inputs::

        inputnode.extra_field
        inputnode.matlab_matrix_files

    Outputs::

        outputnode.csv_file

    """
    inputnode = pe.Node(
        interface=util.IdentityInterface(
            fields=["extra_field", "matlab_matrix_files"]),
        name="inputnode")

    pipeline = pe.Workflow(name=name)

    Matlab2CSV = pe.MapNode(
        interface=misc.Matlab2CSV(), name="Matlab2CSV", iterfield=["in_file"])
    MergeCSVFiles = pe.Node(
        interface=misc.MergeCSVFiles(), name="MergeCSVFiles")
    MergeCSVFiles.inputs.extra_column_heading = extra_column_heading

    pipeline.connect([(inputnode, Matlab2CSV, [("matlab_matrix_files",
                                                "in_file")])])
    pipeline.connect([(Matlab2CSV, MergeCSVFiles, [("csv_files",
                                                    "in_files")])])
    pipeline.connect([(inputnode, MergeCSVFiles, [("extra_field",
                                                   "extra_field")])])

    outputnode = pe.Node(
        interface=util.IdentityInterface(fields=["csv_file"]),
        name="outputnode")

    pipeline.connect([(MergeCSVFiles, outputnode, [("csv_file", "csv_file")])])
    return pipeline
Пример #2
0
def create_networkx_pipeline(name="networkx", extra_column_heading="subject"):
    """Creates a workflow to calculate various graph measures (via NetworkX) on
    an input network. The output measures are then converted to comma-separated value
    text files, and an extra column / field is also added. Typically, the user would
    connect the subject name to this field.

    Example
    -------

    >>> from nipype.workflows.dmri.connectivity.nx import create_networkx_pipeline
    >>> nx = create_networkx_pipeline("networkx", "subject_id")
    >>> nx.inputs.inputnode.extra_field = 'subj1'
    >>> nx.inputs.inputnode.network_file = 'subj1.pck'
    >>> nx.run()                 # doctest: +SKIP

    Inputs::

        inputnode.extra_field
        inputnode.network_file

    Outputs::

        outputnode.network_files
        outputnode.csv_files
        outputnode.matlab_files

    """
    inputnode = pe.Node(interface=util.IdentityInterface(
        fields=["extra_field", "network_file"]),
                        name="inputnode")

    pipeline = pe.Workflow(name=name)

    ntwkMetrics = pe.Node(interface=cmtk.NetworkXMetrics(),
                          name="NetworkXMetrics")
    Matlab2CSV_node = pe.Node(interface=misc.Matlab2CSV(),
                              name="Matlab2CSV_node")
    MergeCSVFiles_node = pe.Node(interface=misc.MergeCSVFiles(),
                                 name="MergeCSVFiles_node")
    MergeCSVFiles_node.inputs.extra_column_heading = extra_column_heading

    Matlab2CSV_global = Matlab2CSV_node.clone(name="Matlab2CSV_global")

    mergeNetworks = pe.Node(interface=util.Merge(2), name="mergeNetworks")
    mergeCSVs = mergeNetworks.clone("mergeCSVs")

    pipeline.connect([(inputnode, ntwkMetrics, [("network_file", "in_file")])])
    pipeline.connect([(ntwkMetrics, Matlab2CSV_node, [("node_measures_matlab",
                                                       "in_file")])])
    pipeline.connect([(ntwkMetrics, Matlab2CSV_global,
                       [("global_measures_matlab", "in_file")])])

    pipeline.connect([(Matlab2CSV_node, MergeCSVFiles_node, [("csv_files",
                                                              "in_files")])])
    pipeline.connect([(inputnode, MergeCSVFiles_node, [("extra_field",
                                                        "out_file")])])
    pipeline.connect([(inputnode, MergeCSVFiles_node, [("extra_field",
                                                        "extra_field")])])
    pipeline.connect([(inputnode, MergeCSVFiles_node,
                       [(("network_file", pullnodeIDs), "row_headings")])])

    pipeline.connect([(inputnode, mergeNetworks, [("network_file", "in1")])])
    pipeline.connect([(ntwkMetrics, mergeNetworks, [("gpickled_network_files",
                                                     "in2")])])

    outputnode = pe.Node(interface=util.IdentityInterface(fields=[
        "network_files", "csv_files", "matlab_files", "node_csv", "global_csv"
    ]),
                         name="outputnode")

    pipeline.connect([(MergeCSVFiles_node, outputnode, [("csv_file",
                                                         "node_csv")])])
    pipeline.connect([(Matlab2CSV_global, outputnode, [("csv_files",
                                                        "global_csv")])])

    pipeline.connect([(MergeCSVFiles_node, mergeCSVs, [("csv_file", "in1")])])
    pipeline.connect([(Matlab2CSV_global, mergeCSVs, [("csv_files", "in2")])])
    pipeline.connect([(mergeNetworks, outputnode, [("out", "network_files")])])
    pipeline.connect([(mergeCSVs, outputnode, [("out", "csv_files")])])
    pipeline.connect([(ntwkMetrics, outputnode, [("matlab_matrix_files",
                                                  "matlab_files")])])
    return pipeline
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
def create_fmri_graphs(name="functional",
                       with_simple_timecourse_correlation=False):
    try:
        coma_rest_lib_path = os.environ['COMA_REST_LIB_ROOT']
    except KeyError:
        print 'COMA_REST_LIB_ROOT environment variable not set.'

    inputnode_within = pe.Node(interface=util.IdentityInterface(fields=[
        "subject_id", "functional_images", "segmentation_file",
        "repetition_time", "resolution_network_file"
    ]),
                               name="inputnode_within")

    ica = pe.Node(interface=SingleSubjectICA(), name='ica')
    # Create the resampling nodes. Functional images and ICA maps must have the same dimensions as the segmentation file
    resampleFunctional = pe.MapNode(interface=fs.MRIConvert(),
                                    name='resampleFunctional',
                                    iterfield=['in_file'])
    resampleFunctional.inputs.out_type = 'nii'
    resampleICAmaps = pe.MapNode(interface=fs.MRIConvert(),
                                 name='resampleICAmaps',
                                 iterfield=['in_file'])
    resampleICAmaps.inputs.out_type = 'nii'
    resample_neuronal = pe.MapNode(interface=fs.MRIConvert(),
                                   name='resample_neuronal',
                                   iterfield=['in_file'])
    resample_neuronal.inputs.out_type = 'nii'
    if with_simple_timecourse_correlation:
        resample_non_neuronal = pe.MapNode(interface=fs.MRIConvert(),
                                           name='resample_non_neuronal',
                                           iterfield=['in_file'])
        resample_non_neuronal.inputs.out_type = 'nii'

    # Create the ComaRestLib nodes
    denoised_image = pe.Node(interface=CreateDenoisedImage(),
                             name='denoised_image')
    denoised_image.inputs.coma_rest_lib_path = coma_rest_lib_path
    matching_classification = pe.Node(interface=MatchingClassification(),
                                      name='matching_classification')
    matching_classification.inputs.coma_rest_lib_path = coma_rest_lib_path
    compute_fingerprints = pe.MapNode(interface=ComputeFingerprint(),
                                      name='compute_fingerprints',
                                      iterfield=['in_file', 'component_index'])
    compute_fingerprints.inputs.coma_rest_lib_path = coma_rest_lib_path

    # Create the functional connectivity thresholding and mapping nodes
    createnodes = pe.Node(interface=cmtk.CreateNodes(), name="CreateNodes")
    connectivity_threshold = pe.Node(
        interface=cmtk.CreateConnectivityThreshold(),
        name='connectivity_threshold')
    connectivity_graph = pe.MapNode(interface=cmtk.ConnectivityGraph(),
                                    name='connectivity_graph',
                                    iterfield=['in_file', 'component_index'])
    neuronal_regional_timecourses = pe.Node(
        interface=cmtk.RegionalValues(), name="neuronal_regional_timecourses")

    # Define the CFF Converter, NetworkX MATLAB -> CommaSeparatedValue nodes
    graphCFFConverter = pe.Node(interface=cmtk.CFFConverter(),
                                name="graphCFFConverter")
    neuronalCFFConverter = pe.Node(interface=cmtk.CFFConverter(),
                                   name="neuronalCFFConverter")
    neuronalCFFConverter.inputs.out_file = 'neuronal.cff'

    correlationCFFConverter = pe.Node(interface=cmtk.CFFConverter(),
                                      name="correlationCFFConverter")
    correlationCFFConverter.inputs.out_file = 'correlation.cff'
    ConnectivityGraphNetworkXMetrics_correlation = pe.MapNode(
        interface=cmtk.NetworkXMetrics(),
        name="cor_fMRIConnectivityGraphNetworkXMetrics",
        iterfield=['in_file'])
    Matlab2CSV_nx_cor = pe.MapNode(interface=misc.Matlab2CSV(),
                                   name="Matlab2CSV_nx_cor",
                                   iterfield=['in_file'])

    anticorrelationCFFConverter = pe.Node(interface=cmtk.CFFConverter(),
                                          name="anticorrelationCFFConverter")
    anticorrelationCFFConverter.inputs.out_file = 'anticorrelation.cff'
    ConnectivityGraphNetworkXMetrics_anticorrelation = pe.MapNode(
        interface=cmtk.NetworkXMetrics(),
        name="anticor_fMRIConnectivityGraphNetworkXMetrics",
        iterfield=['in_file'])
    Matlab2CSV_nx_anticor = pe.MapNode(interface=misc.Matlab2CSV(),
                                       name="Matlab2CSV_nx_anticor",
                                       iterfield=['in_file'])

    MergeCSVFiles_cor = pe.MapNode(interface=misc.MergeCSVFiles(),
                                   name="MergeCSVFiles_cor",
                                   iterfield=['in_files', 'extra_field'])
    MergeCSVFiles_cor.inputs.extra_column_heading = 'template_name'
    MergeCSVFiles_anticor = MergeCSVFiles_cor.clone('MergeCSVFiles_anticor')

    # Uses a Function interface to group the fMRI graphs and save the neuronal graphs with more detailed names
    group_fmri_graphs_interface = Function(input_names=[
        "subject_id", "in_file", "component_index", "matching_stats"
    ],
                                           output_names=["out_file"],
                                           function=group_fmri_graphs)

    # Create lists of the neuronal, correlation, and anticorrelation graphs
    group_graphs = pe.MapNode(interface=group_fmri_graphs_interface,
                              name='group_graphs',
                              iterfield=['in_file', 'component_index'])
    group_graphs_corr = pe.MapNode(interface=group_fmri_graphs_interface,
                                   name='group_graphs_corr',
                                   iterfield=['in_file', 'component_index'])
    group_graphs_anticorr = pe.MapNode(
        interface=group_fmri_graphs_interface,
        name='group_graphs_anticorr',
        iterfield=['in_file', 'component_index'])

    # Define a simple interface for a function which removes 'None' values from the graph grouping above
    removeNoneValues_interface = Function(input_names=["in_files"],
                                          output_names=["out_files"],
                                          function=removeNoneValues)

    # Create nodes for the removing 'None' values from the neuronal, correlation, and anticorrelation lists
    grouped_graphs = pe.Node(interface=removeNoneValues_interface,
                             name='grouped_graphs')
    grouped_graphs_corr = pe.Node(interface=removeNoneValues_interface,
                                  name='grouped_graphs_corr')
    grouped_graphs_anticorr = pe.Node(interface=removeNoneValues_interface,
                                      name='grouped_graphs_anticorr')

    remove_unconnected_graphs_interface = Function(
        input_names=["in_files"],
        output_names=["out_files"],
        function=remove_unconnected_graphs)

    remove_unconnected_corr = pe.Node(
        interface=remove_unconnected_graphs_interface,
        name='remove_unconnected_corr')
    remove_unconnected_anticorr = remove_unconnected_corr.clone(
        name='remove_unconnected_anticorr')

    concat_csv_interface = Function(input_names=["in_files"],
                                    output_names=["out_name"],
                                    function=concatcsv)

    concatcsv_cor = pe.Node(interface=concat_csv_interface,
                            name='concatcsv_cor')
    concatcsv_anticor = pe.Node(interface=concat_csv_interface,
                                name='concatcsv_anticor')
    add_subjid_to_csv_cor = pe.Node(interface=misc.AddCSVColumn(),
                                    name='add_subjid_to_csv_cor')
    add_subjid_to_csv_cor.inputs.extra_column_heading = 'Subject'

    add_subjid_to_csv_anticor = pe.Node(interface=misc.AddCSVColumn(),
                                        name='add_subjid_to_csv_anticor')
    add_subjid_to_csv_anticor.inputs.extra_column_heading = 'Subject'

    split_neuronal = pe.Node(interface=fsl.Split(), name='split_neuronal')
    split_neuronal.inputs.dimension = 't'
    TCcorrCFFConverter = pe.Node(interface=cmtk.CFFConverter(),
                                 name="TCcorrCFFConverter")
    if with_simple_timecourse_correlation:
        split_non_neuronal = split_neuronal.clone(name='split_non_neuronal')
        neuronal_time_course_correlation = pe.Node(
            interface=cmtk.SimpleTimeCourseCorrelationGraph(),
            name='neuronal_time_course_correlation')
        neuronal_time_course_correlation.inputs.out_network_file = 'neuronal.pck'
        non_neuronal_time_course_correlation = pe.Node(
            interface=cmtk.SimpleTimeCourseCorrelationGraph(),
            name='non_neuronal_time_course_correlation')
        non_neuronal_time_course_correlation.inputs.out_network_file = 'non_neuronal.pck'
        TCcorrCFFConverter.inputs.out_file = 'time_course_correlation.cff'
        mergeSTCC = pe.Node(interface=util.Merge(2), name='mergeSTCC')
    else:
        TCcorrCFFConverter.inputs.out_file = 'time_courses.cff'

    ### Create the workflow ###
    func_ntwk = pe.Workflow(name='func_ntwk')

    func_ntwk.connect([(inputnode_within, ica, [('functional_images',
                                                 'in_files')])])
    func_ntwk.connect([(inputnode_within, ica, [('subject_id', 'prefix')])])

    # Create the denoised image
    func_ntwk.connect([(inputnode_within, denoised_image, [('subject_id',
                                                            'prefix')])])
    func_ntwk.connect([(inputnode_within, denoised_image,
                        [('repetition_time', 'repetition_time')])])
    func_ntwk.connect([(ica, denoised_image, [('independent_component_images',
                                               'in_files')])])
    func_ntwk.connect([(ica, denoised_image, [('mask_image', 'ica_mask_image')
                                              ])])
    func_ntwk.connect([(ica, denoised_image, [
        ('independent_component_timecourse', 'time_course_image')
    ])])

    # Runs the matching classification
    func_ntwk.connect([(inputnode_within, matching_classification,
                        [('subject_id', 'prefix')])])
    func_ntwk.connect([(inputnode_within, matching_classification,
                        [('repetition_time', 'repetition_time')])])
    func_ntwk.connect([(ica, matching_classification,
                        [('independent_component_images', 'in_files')])])
    func_ntwk.connect([(ica, matching_classification, [('mask_image',
                                                        'ica_mask_image')])])
    func_ntwk.connect([(ica, matching_classification, [
        ('independent_component_timecourse', 'time_course_image')
    ])])

    # Computes and saves the fingerprint for each IC

    func_ntwk.connect([(inputnode_within, compute_fingerprints,
                        [('repetition_time', 'repetition_time')])])
    func_ntwk.connect([(ica, compute_fingerprints,
                        [('independent_component_images', 'in_file')])])
    func_ntwk.connect([(ica, compute_fingerprints, [('mask_image',
                                                     'ica_mask_image')])])
    func_ntwk.connect([(ica, compute_fingerprints, [
        ('independent_component_timecourse', 'time_course_image')
    ])])
    func_ntwk.connect([(ica, compute_fingerprints,
                        [(('independent_component_images',
                           get_component_index), 'component_index')])])

    # Calculates the the t-value threshold for each node/IC
    func_ntwk.connect([(inputnode_within, resampleFunctional,
                        [('functional_images', 'in_file')])])
    func_ntwk.connect([(inputnode_within, resampleFunctional,
                        [('segmentation_file', 'reslice_like')])])
    func_ntwk.connect([(resampleFunctional, connectivity_threshold,
                        [('out_file', 'in_files')])])
    func_ntwk.connect([(ica, connectivity_threshold, [
        ('independent_component_timecourse', 'time_course_file')
    ])])
    func_ntwk.connect([(inputnode_within, connectivity_threshold,
                        [('segmentation_file', 'segmentation_file')])])

    # Resamples the ICA z-score maps to the same dimensions as the segmentation file
    func_ntwk.connect([(ica, resampleICAmaps, [('independent_component_images',
                                                'in_file')])])
    func_ntwk.connect([(inputnode_within, resampleICAmaps,
                        [('segmentation_file', 'reslice_like')])])

    # Splits the 4d neuronal and non-neuronal images, resamples them, and creates the time-course correlation graph
    func_ntwk.connect([(denoised_image, split_neuronal, [('neuronal_image',
                                                          'in_file')])])
    func_ntwk.connect([(split_neuronal, resample_neuronal, [('out_files',
                                                             'in_file')])])
    func_ntwk.connect([(inputnode_within, resample_neuronal,
                        [('segmentation_file', 'reslice_like')])])

    # Calculates the fmri timecourse
    func_ntwk.connect([(inputnode_within, neuronal_regional_timecourses,
                        [('segmentation_file', 'segmentation_file')])])
    func_ntwk.connect([(createnodes, neuronal_regional_timecourses,
                        [('node_network', 'resolution_network_file')])])
    func_ntwk.connect([(resample_neuronal, neuronal_regional_timecourses,
                        [('out_file', 'in_files')])])
    neuronal_regional_timecourses.inputs.out_stats_file = 'denoised_fmri_timecourse.mat'

    if with_simple_timecourse_correlation:
        func_ntwk.connect([(inputnode_within, neuronal_time_course_correlation,
                            [('segmentation_file', 'segmentation_file')])])
        func_ntwk.connect([(createnodes, neuronal_time_course_correlation,
                            [('node_network', 'structural_network')])])
        func_ntwk.connect([(resample_neuronal,
                            neuronal_time_course_correlation, [('out_file',
                                                                'in_files')])])
        func_ntwk.connect([(neuronal_time_course_correlation, mergeSTCC,
                            [('network_file', 'in1')])])

        func_ntwk.connect([(denoised_image, split_non_neuronal,
                            [('non_neuronal_image', 'in_file')])])
        func_ntwk.connect([(split_non_neuronal, resample_non_neuronal,
                            [('out_files', 'in_file')])])
        func_ntwk.connect([(inputnode_within, resample_non_neuronal,
                            [('segmentation_file', 'reslice_like')])])
        func_ntwk.connect([
            (inputnode_within, non_neuronal_time_course_correlation,
             [('segmentation_file', 'segmentation_file')])
        ])
        func_ntwk.connect([(createnodes, non_neuronal_time_course_correlation,
                            [('node_network', 'structural_network')])])
        func_ntwk.connect([
            (resample_non_neuronal, non_neuronal_time_course_correlation,
             [('out_file', 'in_files')])
        ])
        func_ntwk.connect([(non_neuronal_time_course_correlation, mergeSTCC,
                            [('network_file', 'in2')])])
        func_ntwk.connect([(mergeSTCC, TCcorrCFFConverter,
                            [('out', 'gpickled_networks')])])

    # Creates the nodes for the graph from the input segmentation file and resolution network file
    func_ntwk.connect([(inputnode_within, createnodes, [('segmentation_file',
                                                         'roi_file')])])
    func_ntwk.connect([(inputnode_within, createnodes, [
        ('resolution_network_file', 'resolution_network_file')
    ])])
    func_ntwk.connect([(inputnode_within, MergeCSVFiles_cor, [
        (('resolution_network_file', pullnodeIDs), 'row_headings')
    ])])
    func_ntwk.connect([(inputnode_within, MergeCSVFiles_anticor, [
        (('resolution_network_file', pullnodeIDs), 'row_headings')
    ])])

    # Creates a connectivity graph for each IC and stores all of the graphs in a CFF file
    func_ntwk.connect([(inputnode_within, connectivity_graph,
                        [('segmentation_file', 'segmentation_file')])])
    func_ntwk.connect([(createnodes, connectivity_graph,
                        [('node_network', 'resolution_network_file')])])
    func_ntwk.connect([(resampleICAmaps, connectivity_graph, [('out_file',
                                                               'in_file')])])
    func_ntwk.connect([(resampleICAmaps, connectivity_graph, [
        (('out_file', get_component_index_resampled), 'component_index')
    ])])
    func_ntwk.connect([(connectivity_threshold, connectivity_graph, [
        ('t_value_threshold_file', 't_value_threshold_file')
    ])])
    func_ntwk.connect([(connectivity_graph, graphCFFConverter,
                        [('network_file', 'gpickled_networks')])])

    # Uses the matching classification to separate the neuronal connectivity graphs
    func_ntwk.connect([(inputnode_within, group_graphs, [('subject_id',
                                                          'subject_id')])])
    func_ntwk.connect([(connectivity_graph, group_graphs, [('network_file',
                                                            'in_file')])])
    func_ntwk.connect([(resampleICAmaps, group_graphs, [
        (('out_file', get_component_index_resampled), 'component_index')
    ])])
    func_ntwk.connect([(matching_classification, group_graphs,
                        [('stats_file', 'matching_stats')])])
    func_ntwk.connect([(group_graphs, grouped_graphs, [('out_file', 'in_files')
                                                       ])])
    func_ntwk.connect([(grouped_graphs, neuronalCFFConverter,
                        [('out_files', 'gpickled_networks')])])

    # Groups the correlation graphs as above, calculates NetworkX measures, outputs to a CSV file
    func_ntwk.connect([(inputnode_within, group_graphs_corr,
                        [('subject_id', 'subject_id')])])
    func_ntwk.connect([(connectivity_graph, group_graphs_corr,
                        [('correlation_network', 'in_file')])])
    func_ntwk.connect([(resampleICAmaps, group_graphs_corr, [
        (('out_file', get_component_index_resampled), 'component_index')
    ])])
    func_ntwk.connect([(matching_classification, group_graphs_corr,
                        [('stats_file', 'matching_stats')])])
    func_ntwk.connect([(group_graphs_corr, grouped_graphs_corr,
                        [('out_file', 'in_files')])])
    func_ntwk.connect([(grouped_graphs_corr, correlationCFFConverter,
                        [('out_files', 'gpickled_networks')])])
    func_ntwk.connect([(grouped_graphs_corr, remove_unconnected_corr,
                        [('out_files', 'in_files')])])
    func_ntwk.connect([
        (grouped_graphs_corr, ConnectivityGraphNetworkXMetrics_correlation,
         [('out_files', 'in_file')])
    ])
    func_ntwk.connect([(grouped_graphs_corr, MergeCSVFiles_cor,
                        [(('out_files', pull_template_name), 'extra_field')])])
    func_ntwk.connect([(ConnectivityGraphNetworkXMetrics_correlation,
                        Matlab2CSV_nx_cor, [("node_measures_matlab", "in_file")
                                            ])])
    func_ntwk.connect([(Matlab2CSV_nx_cor, MergeCSVFiles_cor, [("csv_files",
                                                                "in_files")])])
    func_ntwk.connect([(MergeCSVFiles_cor, concatcsv_cor, [("csv_file",
                                                            "in_files")])])
    func_ntwk.connect([(concatcsv_cor, add_subjid_to_csv_cor, [("out_name",
                                                                "in_file")])])
    func_ntwk.connect([(inputnode_within, add_subjid_to_csv_cor,
                        [("subject_id", "extra_field")])])
    func_ntwk.connect([(inputnode_within, add_subjid_to_csv_cor,
                        [("subject_id", "out_file")])])

    # Groups the anticorrelation graphs as above, calculates NetworkX measures, outputs to a CSV file
    func_ntwk.connect([(inputnode_within, group_graphs_anticorr,
                        [('subject_id', 'subject_id')])])
    func_ntwk.connect([(connectivity_graph, group_graphs_anticorr,
                        [('anticorrelation_network', 'in_file')])])
    func_ntwk.connect([(resampleICAmaps, group_graphs_anticorr, [
        (('out_file', get_component_index_resampled), 'component_index')
    ])])
    func_ntwk.connect([(matching_classification, group_graphs_anticorr,
                        [('stats_file', 'matching_stats')])])
    func_ntwk.connect([(group_graphs_anticorr, grouped_graphs_anticorr,
                        [('out_file', 'in_files')])])
    func_ntwk.connect([(grouped_graphs_anticorr, anticorrelationCFFConverter,
                        [('out_files', 'gpickled_networks')])])
    func_ntwk.connect([(grouped_graphs_anticorr, remove_unconnected_anticorr,
                        [('out_files', 'in_files')])])
    func_ntwk.connect([(grouped_graphs_anticorr,
                        ConnectivityGraphNetworkXMetrics_anticorrelation, [
                            ('out_files', 'in_file')
                        ])])
    func_ntwk.connect([(grouped_graphs_anticorr, MergeCSVFiles_anticor,
                        [(('out_files', pull_template_name), 'extra_field')])])
    func_ntwk.connect([(ConnectivityGraphNetworkXMetrics_anticorrelation,
                        Matlab2CSV_nx_anticor, [("node_measures_matlab",
                                                 "in_file")])])
    func_ntwk.connect([(Matlab2CSV_nx_anticor, MergeCSVFiles_anticor,
                        [("csv_files", "in_files")])])
    func_ntwk.connect([(MergeCSVFiles_anticor, concatcsv_anticor,
                        [("csv_file", "in_files")])])
    func_ntwk.connect([(concatcsv_anticor, add_subjid_to_csv_anticor,
                        [("out_name", "in_file")])])
    func_ntwk.connect([(inputnode_within, add_subjid_to_csv_anticor,
                        [("subject_id", "extra_field")])])
    func_ntwk.connect([(inputnode_within, add_subjid_to_csv_anticor,
                        [("subject_id", "out_file")])])
    ConnectivityGraphNetworkXMetrics_correlation.inputs.ignore_exception = True
    ConnectivityGraphNetworkXMetrics_anticorrelation.inputs.ignore_exception = True

    # Create a higher-level workflow
    inputnode = pe.Node(interface=util.IdentityInterface(fields=[
        "subject_id", "functional_images", "fmri_ICA_maps", "ica_mask_image",
        "fmri_ICA_timecourse", "segmentation_file", "repetition_time",
        "resolution_network_file"
    ]),
                        name="inputnode")

    if with_simple_timecourse_correlation:
        outputnode = pe.Node(interface=util.IdentityInterface(fields=[
            "matching_stats", "neuronal_ntwks", "neuronal_cff",
            "neuronal_regional_timecourse_stats", "correlation_ntwks",
            "correlation_cff", "anticorrelation_ntwks", "anticorrelation_cff",
            "correlation_stats", "anticorrelation_stats",
            "simple_correlation_ntwks", "simple_correlation_cff"
        ]),
                             name="outputnode")
    else:
        outputnode = pe.Node(interface=util.IdentityInterface(fields=[
            "matching_stats", "neuronal_ntwks", "neuronal_cff",
            "neuronal_regional_timecourse_stats", "correlation_ntwks",
            "correlation_cff", "correlation_stats", "anticorrelation_stats",
            "anticorrelation_ntwks", "anticorrelation_cff"
        ]),
                             name="outputnode")

    functional = pe.Workflow(name=name)
    functional.base_output_dir = name
    functional.base_dir = name
    functional.connect([(inputnode, func_ntwk, [
        ("subject_id", "inputnode_within.subject_id"),
        ("functional_images", "inputnode_within.functional_images"),
        ("segmentation_file", "inputnode_within.segmentation_file"),
        ("repetition_time", "inputnode_within.repetition_time"),
        ("resolution_network_file", "inputnode_within.resolution_network_file")
    ])])

    functional.connect([(func_ntwk, outputnode, [('grouped_graphs.out_files',
                                                  'neuronal_ntwks')])])
    functional.connect([(func_ntwk, outputnode, [
        ('neuronalCFFConverter.connectome_file', 'neuronal_cff')
    ])])
    functional.connect([(func_ntwk, outputnode,
                         [('neuronal_regional_timecourses.stats_file',
                           'neuronal_regional_timecourse_stats')])])

    functional.connect([(func_ntwk, outputnode, [
        ('remove_unconnected_corr.out_files', 'correlation_ntwks')
    ])])
    functional.connect([(func_ntwk, outputnode, [
        ('correlationCFFConverter.connectome_file', 'correlation_cff')
    ])])
    functional.connect([(func_ntwk, outputnode, [
        ('add_subjid_to_csv_cor.csv_file', 'correlation_stats')
    ])])

    functional.connect([(func_ntwk, outputnode, [
        ('remove_unconnected_anticorr.out_files', 'anticorrelation_ntwks')
    ])])
    functional.connect([(func_ntwk, outputnode, [
        ('anticorrelationCFFConverter.connectome_file', 'anticorrelation_cff')
    ])])
    functional.connect([(func_ntwk, outputnode, [
        ('add_subjid_to_csv_anticor.csv_file', 'anticorrelation_stats')
    ])])

    functional.connect([(func_ntwk, outputnode, [
        ('matching_classification.stats_file', 'matching_stats')
    ])])

    if with_simple_timecourse_correlation:
        functional.connect([(func_ntwk, outputnode,
                             [('mergeSTCC.out', 'simple_correlation_ntwks')])])
        functional.connect([(func_ntwk, outputnode, [
            ('TCcorrCFFConverter.connectome_file', 'simple_correlation_cff')
        ])])
    return functional
Пример #5
0
def create_fsconnectivity_pipeline(name="fsconnectivity",
                                   manual_seg_rois=False,
                                   parcellation_name="scale33"):
    inputfields = [
        "subjects_dir", "subject_id", "dwi", "bvecs", "bvals",
        "resolution_network_file"
    ]

    inputnode = pe.Node(interface=util.IdentityInterface(fields=inputfields),
                        name="inputnode")

    outputnode = pe.Node(
        interface=util.IdentityInterface(
            fields=[  # Outputs from the DWI workflow
                "single_fiber_mask",
                "fa",
                "rgb_fa",
                "md",
                "mode",
                "t1",
                "t1_brain",
                "wm_mask",
                "term_mask",
                "aparc_aseg",
                "tissue_class_files",
                "gm_prob",
                "wm_prob",
                "csf_prob",

                # Outputs from registration and labelling
                "rois_to_dwi",
                "wmmask_to_dwi",
                "termmask_to_dwi",
                "dwi_to_t1_matrix",
                "highres_t1_to_dwi_matrix",

                # T1 in DWI space for reference
                "t1_to_dwi",

                # Outputs from tracking
                "fiber_odfs",
                "fiber_tracks_tck_dwi",
                "fiber_tracks_trk_t1",

                # Outputs from connectivity mapping
                "connectome",
                "nxstatscff",
                "nxmatlab",
                "nxcsv",
                "cmatrix",
                "matrix_file",
            ]),
        name="outputnode")

    t1_to_dwi = pe.Node(interface=fsl.ApplyXfm(), name='t1_to_dwi')

    termmask_to_dwi = t1_to_dwi.clone("termmask_to_dwi")

    dtiproc = damaged_brain_dti_processing("dtiproc", use_FAST_masks=True)
    reg_label = create_reg_and_label_wf("reg_label", manual_seg_rois=True)

    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_convertLHlabels = mris_convertLH.clone('mris_convertLHlabels')
    mris_convertRHlabels = mris_convertLH.clone('mris_convertRHlabels')
    """
    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(6), 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()))

    workflow = pe.Workflow(name=name)
    workflow.base_output_dir = name

    workflow.connect([(inputnode, dtiproc,
                       [("subjects_dir", "inputnode.subjects_dir"),
                        ("subject_id", "inputnode.subject_id"),
                        ("dwi", "inputnode.dwi"), ("bvecs", "inputnode.bvecs"),
                        ("bvals", "inputnode.bvals")])])

    workflow.connect([(inputnode, reg_label, [("subject_id",
                                               "inputnode.subject_id")])])
    #workflow.connect([(mri_convert_ROI_scale500, reg_label, [("out_file", "inputnode.manual_seg_rois")])])
    workflow.connect([(dtiproc, reg_label, [("outputnode.aparc_aseg",
                                             "inputnode.manual_seg_rois")])])

    workflow.connect([(dtiproc, reg_label, [
        ("outputnode.wm_mask", "inputnode.wm_mask"),
        ("outputnode.term_mask", "inputnode.termination_mask"),
        ("outputnode.fa", "inputnode.fa"),
        ("outputnode.aparc_aseg", "inputnode.aparc_aseg"),
    ])])

    workflow.connect([(reg_label, t1_to_dwi, [("outputnode.t1_to_dwi_matrix",
                                               "in_matrix_file")])])
    workflow.connect([(dtiproc, t1_to_dwi, [("outputnode.t1", "in_file")])])
    workflow.connect([(dtiproc, t1_to_dwi, [("outputnode.fa", "reference")])])
    workflow.connect([(inputnode, t1_to_dwi, [
        (('subject_id', add_subj_name_to_T1_dwi), 'out_file')
    ])])

    workflow.connect([(reg_label, termmask_to_dwi,
                       [("outputnode.t1_to_dwi_matrix", "in_matrix_file")])])
    workflow.connect([(dtiproc, termmask_to_dwi, [("outputnode.term_mask",
                                                   "in_file")])])
    workflow.connect([(dtiproc, termmask_to_dwi, [("outputnode.fa",
                                                   "reference")])])
    '''
    Connect outputnode
    '''

    workflow.connect([(t1_to_dwi, outputnode, [("out_file", "t1_to_dwi")])])

    workflow.connect([(dtiproc, outputnode, [
        ("outputnode.t1", "t1"),
        ("outputnode.wm_prob", "wm_prob"),
        ("outputnode.gm_prob", "gm_prob"),
        ("outputnode.csf_prob", "csf_prob"),
        ("outputnode.single_fiber_mask", "single_fiber_mask"),
        ("outputnode.fa", "fa"),
        ("outputnode.rgb_fa", "rgb_fa"),
        ("outputnode.md", "md"),
        ("outputnode.mode", "mode"),
        ("outputnode.t1_brain", "t1_brain"),
        ("outputnode.wm_mask", "wm_mask"),
        ("outputnode.term_mask", "term_mask"),
        ("outputnode.aparc_aseg", "aparc_aseg"),
        ("outputnode.tissue_class_files", "tissue_class_files"),
    ])])

    workflow.connect([(reg_label, outputnode, [
        ("outputnode.rois_to_dwi", "rois_to_dwi"),
        ("outputnode.wmmask_to_dwi", "wmmask_to_dwi"),
        ("outputnode.termmask_to_dwi", "termmask_to_dwi"),
        ("outputnode.dwi_to_t1_matrix", "dwi_to_t1_matrix"),
        ("outputnode.highres_t1_to_dwi_matrix", "highres_t1_to_dwi_matrix"),
    ])])

    workflow.connect([(dtiproc, outputnode, [("outputnode.aparc_aseg", "rois")
                                             ])])

    tracking = anatomically_constrained_tracking("tracking")

    workflow.connect([(inputnode, tracking, [
        ("subject_id", "inputnode.subject_id"),
        ("dwi", "inputnode.dwi"),
        ("bvecs", "inputnode.bvecs"),
        ("bvals", "inputnode.bvals"),
    ])])

    workflow.connect([(reg_label, tracking, [
        ("outputnode.wmmask_to_dwi", "inputnode.wm_mask"),
        ("outputnode.termmask_to_dwi", "inputnode.termination_mask"),
        ("outputnode.dwi_to_t1_matrix", "inputnode.registration_matrix_file"),
    ])])

    workflow.connect([(dtiproc, tracking, [
        ("outputnode.t1", "inputnode.registration_image_file")
    ])])

    workflow.connect([(dtiproc, tracking, [("outputnode.single_fiber_mask",
                                            "inputnode.single_fiber_mask")])])

    workflow.connect([(tracking, outputnode, [
        ("outputnode.fiber_odfs", "fiber_odfs"),
        ("outputnode.fiber_tracks_tck_dwi", "fiber_tracks_tck_dwi"),
        ("outputnode.fiber_tracks_trk_t1", "fiber_tracks_trk_t1"),
    ])])

    workflow.connect([(tracking, creatematrix,
                       [("outputnode.fiber_tracks_trk_t1", "tract_file")])])

    workflow.connect([(inputnode, FreeSurferSource, [("subjects_dir",
                                                      "subjects_dir")])])
    workflow.connect([(inputnode, FreeSurferSource, [("subject_id",
                                                      "subject_id")])])

    workflow.connect([(inputnode, FreeSurferSourceLH, [("subjects_dir",
                                                        "subjects_dir")])])
    workflow.connect([(inputnode, FreeSurferSourceLH, [("subject_id",
                                                        "subject_id")])])

    workflow.connect([(inputnode, FreeSurferSourceRH, [("subjects_dir",
                                                        "subjects_dir")])])
    workflow.connect([(inputnode, FreeSurferSourceRH, [("subject_id",
                                                        "subject_id")])])

    workflow.connect([(inputnode, parcellate, [("subjects_dir", "subjects_dir")
                                               ])])
    workflow.connect([(inputnode, parcellate, [("subject_id", "subject_id")])])
    workflow.connect([(parcellate, mri_convert_ROI_scale500, [('roi_file',
                                                               'in_file')])])
    """
    Surface conversions to GIFTI (pial, white, inflated, and sphere for both hemispheres)
    """

    workflow.connect([(FreeSurferSourceLH, mris_convertLH, [('pial', 'in_file')
                                                            ])])
    workflow.connect([(FreeSurferSourceRH, mris_convertRH, [('pial', 'in_file')
                                                            ])])
    workflow.connect([(FreeSurferSourceLH, mris_convertLHwhite,
                       [('white', 'in_file')])])
    workflow.connect([(FreeSurferSourceRH, mris_convertRHwhite,
                       [('white', 'in_file')])])
    workflow.connect([(FreeSurferSourceLH, mris_convertLHinflated,
                       [('inflated', 'in_file')])])
    workflow.connect([(FreeSurferSourceRH, mris_convertRHinflated,
                       [('inflated', '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.
    """

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

    workflow.connect(inputnode, 'resolution_network_file', creatematrix,
                     'resolution_network_file')
    workflow.connect([(inputnode, creatematrix, [("subject_id",
                                                  "out_matrix_file")])])
    workflow.connect([(inputnode, creatematrix, [("subject_id",
                                                  "out_matrix_mat_file")])])
    workflow.connect([(parcellate, creatematrix, [("roi_file", "roi_file")])])

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

    workflow.connect([(mris_convertLH, giftiSurfaces, [("converted", "in1")])])
    workflow.connect([(mris_convertRH, giftiSurfaces, [("converted", "in2")])])
    workflow.connect([(mris_convertLHwhite, giftiSurfaces, [("converted",
                                                             "in3")])])
    workflow.connect([(mris_convertRHwhite, giftiSurfaces, [("converted",
                                                             "in4")])])
    workflow.connect([(mris_convertLHinflated, giftiSurfaces, [("converted",
                                                                "in5")])])
    workflow.connect([(mris_convertRHinflated, giftiSurfaces, [("converted",
                                                                "in6")])])

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

    workflow.connect([(giftiSurfaces, CFFConverter, [("out", "gifti_surfaces")
                                                     ])])
    workflow.connect([(giftiLabels, CFFConverter, [("out", "gifti_labels")])])
    workflow.connect([(creatematrix, CFFConverter, [("matrix_files",
                                                     "gpickled_networks")])])
    workflow.connect([(fiberDataArrays, CFFConverter, [("out", "data_files")])
                      ])
    workflow.connect([(inputnode, CFFConverter, [("subject_id", "title")])])
    workflow.connect([(inputnode, CFFConverter, [
        (('subject_id', add_subj_name_to_Connectome), 'out_file')
    ])])
    """
    The graph theoretical metrics which have been generated are placed into another CFF file.
    """

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

    workflow.connect([(networkx, NxStatsCFFConverter,
                       [("outputnode.network_files", "gpickled_networks")])])
    workflow.connect([(giftiSurfaces, NxStatsCFFConverter,
                       [("out", "gifti_surfaces")])])
    workflow.connect([(giftiLabels, NxStatsCFFConverter, [("out",
                                                           "gifti_labels")])])
    workflow.connect([(fiberDataArrays, NxStatsCFFConverter,
                       [("out", "data_files")])])
    workflow.connect([(inputnode, NxStatsCFFConverter, [("subject_id", "title")
                                                        ])])
    workflow.connect([(inputnode, NxStatsCFFConverter, [
        (('subject_id', add_subj_name_to_nxConnectome), 'out_file')
    ])])

    workflow.connect([(CFFConverter, outputnode, [("connectome_file",
                                                   "connectome")])])
    workflow.connect([(NxStatsCFFConverter, outputnode, [("connectome_file",
                                                          "nxstatscff")])])
    workflow.connect([(creatematrix, outputnode, [("intersection_matrix_file",
                                                   "matrix_file")])])
    workflow.connect([(creatematrix, outputnode, [("matrix_mat_file",
                                                   "cmatrix")])])
    workflow.connect([(networkx, outputnode, [("outputnode.csv_files", "nxcsv")
                                              ])])
    return workflow
def create_rsfmri_correlation_network(name="functional",
                                      have_nodes_already=False):
    inputnode_within = pe.Node(interface=util.IdentityInterface(fields=[
        "subject_id", "functional_images", "segmentation_file",
        "resolution_network_file"
    ]),
                               name="inputnode_within")

    # Create the resampling nodes. Functional images must have the same
    # dimensions as the segmentation file
    resampleFunctional = pe.MapNode(interface=fs.MRIConvert(),
                                    name='resampleFunctional',
                                    iterfield=['in_file'])
    resampleFunctional.inputs.out_type = 'nii'

    # Create the nodes
    if not have_nodes_already:
        createnodes = pe.Node(interface=cmtk.CreateNodes(), name="CreateNodes")

    # Define the correlation mapping node, the CFF Converter, and NetworkX
    # MATLAB -> CommaSeparatedValue node
    time_course_correlation = pe.Node(
        interface=ci.SimpleTimeCourseCorrelationGraph(),
        name='time_course_correlation')
    correlationCFFConverter = pe.Node(interface=cmtk.CFFConverter(),
                                      name="correlationCFFConverter")
    correlationCFFConverter.inputs.out_file = 'correlation.cff'
    correlationNetworkXMetrics = pe.Node(interface=cmtk.NetworkXMetrics(),
                                         name="correlationNetworkXMetrics")
    correlationMatlab2CSV_nx = pe.Node(interface=misc.Matlab2CSV(),
                                       name="correlationMatlab2CSV_nx")

    # Create the workflow ###
    cor_ntwk = pe.Workflow(name='cor_ntwk')

    # Calculates the the t-value threshold for each node/IC
    cor_ntwk.connect([(inputnode_within, resampleFunctional,
                       [('functional_images', 'in_file')])])
    cor_ntwk.connect([(inputnode_within, resampleFunctional,
                       [('segmentation_file', 'reslice_like')])])
    cor_ntwk.connect([(resampleFunctional, time_course_correlation,
                       [('out_file', 'in_files')])])

    # Creates the nodes for the graph from the input segmentation file and
    # resolution network file
    if not have_nodes_already:
        cor_ntwk.connect([(inputnode_within, createnodes,
                           [('segmentation_file', 'roi_file')])])
        cor_ntwk.connect([(inputnode_within, createnodes, [
            ('resolution_network_file', 'resolution_network_file')
        ])])
        cor_ntwk.connect([(createnodes, time_course_correlation,
                           [('node_network', 'structural_network')])])
    else:
        cor_ntwk.connect([(inputnode_within, time_course_correlation, [
            ('resolution_network_file', 'structural_network')
        ])])

    # Creates a connectivity graph for each IC and stores all of the graphs in
    # a CFF file
    cor_ntwk.connect([(inputnode_within, time_course_correlation,
                       [('segmentation_file', 'segmentation_file')])])
    cor_ntwk.connect([(time_course_correlation, correlationCFFConverter,
                       [('network_file', 'gpickled_networks')])])
    cor_ntwk.connect([(time_course_correlation, correlationNetworkXMetrics,
                       [('network_file', 'in_file')])])
    cor_ntwk.connect([(correlationNetworkXMetrics, correlationMatlab2CSV_nx,
                       [("node_measures_matlab", "in_file")])])

    # Create a higher-level workflow
    inputnode = pe.Node(interface=util.IdentityInterface(fields=[
        "subject_id", "functional_images", "segmentation_file",
        "resolution_network_file"
    ]),
                        name="inputnode")

    outputnode = pe.Node(interface=util.IdentityInterface(
        fields=["correlation_ntwk", "correlation_cff"]),
                         name="outputnode")

    correlation = pe.Workflow(name=name)
    correlation.base_output_dir = name
    correlation.base_dir = name
    correlation.connect([(inputnode, cor_ntwk, [
        ("subject_id", "inputnode_within.subject_id"),
        ("functional_images", "inputnode_within.functional_images"),
        ("segmentation_file", "inputnode_within.segmentation_file"),
        ("resolution_network_file", "inputnode_within.resolution_network_file")
    ])])

    correlation.connect([(cor_ntwk, outputnode, [
        ('time_course_correlation.network_file', 'correlation_ntwk')
    ])])
    correlation.connect([(cor_ntwk, outputnode, [
        ('correlationCFFConverter.connectome_file', 'correlation_cff')
    ])])
    return correlation