Skip to content

Commit cb0efcb

Browse files
author
Erik Ziegler
committed
Single-subject connectivity tutorials run properly
1 parent 7b80b00 commit cb0efcb

File tree

3 files changed

+30
-79
lines changed

3 files changed

+30
-79
lines changed

examples/dmri_connectivity.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -279,24 +279,6 @@ def select_aparc_annot(list_of_files):
279279
track = pe.Node(interface=camino.TrackPICo(), name="track")
280280
track.inputs.iterations = 1
281281

282-
"""
283-
This tutorial demonstrates two methods of connectivity mapping. The first uses the conmap function of Camino.
284-
The problem with this method is that it can be very memory intensive. To deal with this, we add a "shredding" node
285-
which removes roughly half of the tracts in the file.
286-
"""
287-
288-
tractshred = pe.Node(interface=camino.TractShredder(), name='tractshred')
289-
tractshred.inputs.offset = 0
290-
tractshred.inputs.bunchsize = 2
291-
tractshred.inputs.space = 1
292-
293-
"""
294-
Here we create the Camino-based connectivity mapping node. For visualization, it can be beneficial to set a
295-
threshold for the minimum number of fiber connections that are required for an edge to be drawn on the graph.
296-
"""
297-
298-
conmap = pe.Node(interface=camino.Conmap(), name='conmap')
299-
conmap.inputs.threshold = 100
300282

301283
"""
302284
Currently, the best program for visualizing tracts is TrackVis. For this reason, a node is included to
@@ -350,6 +332,7 @@ def select_aparc_annot(list_of_files):
350332
cmp_config = cmp.configuration.PipelineConfiguration(parcellation_scheme = "NativeFreesurfer")
351333
cmp_config.parcellation_scheme = "NativeFreesurfer"
352334
roigen.inputs.LUT_file = cmp_config.get_freeview_lut("NativeFreesurfer")['freesurferaparc']
335+
roigen_structspace = roigen.clone('ROIGen_structspace')
353336

354337
"""
355338
The CreateMatrix interface takes in the remapped aparc+aseg image as well as the label dictionary and fiber tracts
@@ -361,7 +344,9 @@ def select_aparc_annot(list_of_files):
361344
"""
362345

363346
creatematrix = pe.Node(interface=cmtk.CreateMatrix(), name="CreateMatrix")
364-
creatematrix.inputs.resolution_network_file = cmp_config.parcellation['freesurferaparc']['node_information_graphml']
347+
creatematrix.inputs.count_region_intersections = True
348+
createnodes = pe.Node(interface=cmtk.CreateNodes(), name="CreateNodes")
349+
createnodes.inputs.resolution_network_file = cmp_config.parcellation['freesurferaparc']['node_information_graphml']
365350

366351
"""
367352
Here we define the endpoint of this tutorial, which is the CFFConverter node, as well as a few nodes which use
@@ -447,7 +432,6 @@ def select_aparc_annot(list_of_files):
447432
mapping.connect([(b0Strip, inverse,[('out_file','reference')])])
448433
mapping.connect([(convertxfm, inverse,[('out_file','in_matrix_file')])])
449434
mapping.connect([(mri_convert_WMParc, inverse,[('out_file','in_file')])])
450-
#mapping.connect([(inverse, conmap,[('out_file','roi_file')])])
451435

452436
"""
453437
The tractography pipeline consists of the following nodes. Further information about the tractography
@@ -460,14 +444,6 @@ def select_aparc_annot(list_of_files):
460444
mapping.connect([(dtifit, picopdfs,[("tensor_fitted","in_file")])])
461445
mapping.connect([(picopdfs, track,[("pdfs","in_file")])])
462446

463-
"""
464-
The tractography is passed to the shredder in preparation for connectivity mapping.
465-
Again, the required conmap connection is left commented out.
466-
"""
467-
468-
mapping.connect([(track, tractshred,[("tracked","in_file")])])
469-
#mapping.connect([(tractshred, conmap,[("shredded","in_file")])])
470-
471447
"""
472448
Connecting the Fractional Anisotropy and Trace nodes is simple, as they obtain their input from the
473449
tensor fitting. This is also where our voxel- and data-grabbing functions come in. We pass these functions,
@@ -513,23 +489,28 @@ def select_aparc_annot(list_of_files):
513489
original tracts, and label file are then given to CreateMatrix.
514490
"""
515491

492+
mapping.connect(createnodes, 'node_network',
493+
creatematrix, 'resolution_network_file')
516494
mapping.connect([(FreeSurferSource, mri_convert_AparcAseg, [(('aparc_aseg', select_aparc), 'in_file')])])
517495

518496
mapping.connect([(b0Strip, inverse_AparcAseg,[('out_file','reference')])])
519497
mapping.connect([(convertxfm, inverse_AparcAseg,[('out_file','in_matrix_file')])])
520498
mapping.connect([(mri_convert_AparcAseg, inverse_AparcAseg,[('out_file','in_file')])])
499+
mapping.connect([(mri_convert_AparcAseg, roigen_structspace,[('out_file','aparc_aseg_file')])])
500+
mapping.connect([(roigen_structspace, createnodes,[("roi_file","roi_file")])])
521501

522502
mapping.connect([(inverse_AparcAseg, roigen,[("out_file","aparc_aseg_file")])])
523503
mapping.connect([(roigen, creatematrix,[("roi_file","roi_file")])])
524504
mapping.connect([(camino2trackvis, creatematrix,[("trackvis","tract_file")])])
525505
mapping.connect([(inputnode, creatematrix,[("subject_id","out_matrix_file")])])
506+
mapping.connect([(inputnode, creatematrix,[("subject_id","out_matrix_mat_file")])])
526507

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

532-
mapping.connect([(creatematrix, gpickledNetworks,[("matrix_file","in1")])])
513+
mapping.connect([(creatematrix, gpickledNetworks,[("matrix_files","in1")])])
533514

534515
mapping.connect([(mris_convertLH, giftiSurfaces,[("converted","in1")])])
535516
mapping.connect([(mris_convertRH, giftiSurfaces,[("converted","in2")])])
@@ -576,7 +557,7 @@ def select_aparc_annot(list_of_files):
576557
"""
577558

578559
connectivity = pe.Workflow(name="connectivity")
579-
connectivity.base_dir = op.abspath('connectivity_tutorial')
560+
connectivity.base_dir = op.abspath('dmri_connectivity')
580561
connectivity.connect([
581562
(infosource,datasource,[('subject_id', 'subject_id')]),
582563
(datasource,mapping,[('dwi','inputnode.dwi'),

examples/dmri_connectivity_advanced.py

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
from nipype.workflows.dmri.camino.connectivity_mapping import select_aparc_annot
5959
from nipype.utils.misc import package_check
6060
import warnings
61-
from nipype.workflows.dmri.connectivity.group_connectivity import pullnodeIDs
61+
from nipype.workflows.dmri.connectivity.nx import create_networkx_pipeline, create_cmats_to_csv_pipeline
6262

6363
try:
6464
package_check('cmp')
@@ -307,12 +307,15 @@
307307
Here we choose the Lausanne2008 parcellation scheme, since we are incorporating the CMTK parcellation step.
308308
"""
309309

310-
creatematrix = pe.Node(interface=cmtk.CreateMatrix(), name="CreateMatrix")
310+
parcellation_name = 'scale500'
311311
cmp_config = cmp.configuration.PipelineConfiguration()
312312
cmp_config.parcellation_scheme = "Lausanne2008"
313313
createnodes = pe.Node(interface=cmtk.CreateNodes(), name="CreateNodes")
314314
createnodes.inputs.resolution_network_file = cmp_config._get_lausanne_parcellation('Lausanne2008')[parcellation_name]['node_information_graphml']
315315

316+
creatematrix = pe.Node(interface=cmtk.CreateMatrix(), name="CreateMatrix")
317+
creatematrix.inputs.count_region_intersections = True
318+
316319
"""
317320
Next we define the endpoint of this tutorial, which is the CFFConverter node, as well as a few nodes which use
318321
the Nipype Merge utility. These are useful for passing lists of the files we want packaged in our CFF file.
@@ -329,29 +332,15 @@
329332
gpickledNetworks = pe.Node(interface=util.Merge(2), name="NetworkFiles")
330333

331334
"""
332-
We also create a node to calculate several network metrics on our resulting file, and another CFF converter
335+
We also create a workflow to calculate several network metrics on our resulting file, and another CFF converter
333336
which will be used to package these networks into a single file.
334337
"""
335338

336-
ntwkMetrics = pe.Node(interface=cmtk.NetworkXMetrics(), name="NetworkXMetrics")
339+
networkx = create_networkx_pipeline(name='networkx')
340+
cmats_to_csv = create_cmats_to_csv_pipeline(name='cmats_to_csv')
337341
NxStatsCFFConverter = pe.Node(interface=cmtk.CFFConverter(), name="NxStatsCFFConverter")
338342
NxStatsCFFConverter.inputs.script_files = op.abspath(inspect.getfile(inspect.currentframe()))
339343

340-
Matlab2CSV_node = pe.Node(interface=misc.Matlab2CSV(), name="Matlab2CSV_node")
341-
Matlab2CSV_global = Matlab2CSV_node.clone(name="Matlab2CSV_global")
342-
Matlab2CSV_cmatrix = Matlab2CSV_node.clone(name="Matlab2CSV_cmatrix")
343-
Matlab2CSV_meanfib = Matlab2CSV_node.clone(name="Matlab2CSV_meanfib")
344-
Matlab2CSV_medianfib = Matlab2CSV_node.clone(name="Matlab2CSV_medianfib")
345-
Matlab2CSV_fibstd = Matlab2CSV_node.clone(name="Matlab2CSV_fibstd")
346-
347-
MergeCSVFiles_node = pe.Node(interface=misc.MergeCSVFiles(), name="MergeCSVFiles_node")
348-
rowIDs = pullnodeIDs(op.abspath(cmp_config._get_lausanne_parcellation('Lausanne2008')[parcellation_name]['node_information_graphml']))
349-
MergeCSVFiles_node.inputs.row_headings = rowIDs
350-
MergeCSVFiles_node.inputs.extra_column_heading = 'subject'
351-
mergeCSVMatrices = pe.Node(interface=util.Merge(4), name="mergeCSVMatrices")
352-
MergeCSVFiles_cmatrices = pe.Node(interface=misc.MergeCSVFiles(), name="MergeCSVFiles_cmatrices")
353-
MergeCSVFiles_cmatrices.inputs.extra_column_heading = 'subject'
354-
355344
"""
356345
Connecting the workflow
357346
=======================
@@ -547,44 +536,28 @@
547536

548537
mapping.connect([(giftiSurfaces, CFFConverter,[("out","gifti_surfaces")])])
549538
mapping.connect([(giftiLabels, CFFConverter,[("out","gifti_labels")])])
550-
mapping.connect([(creatematrix, CFFConverter,[("matrix_file","gpickled_networks")])])
539+
mapping.connect([(creatematrix, CFFConverter,[("matrix_files","gpickled_networks")])])
551540
mapping.connect([(niftiVolumes, CFFConverter,[("out","nifti_volumes")])])
552541
mapping.connect([(fiberDataArrays, CFFConverter,[("out","data_files")])])
553542
mapping.connect([(creatematrix, CFFConverter,[("filtered_tractography","tract_files")])])
554543
mapping.connect([(inputnode, CFFConverter,[("subject_id","title")])])
555544

556545
"""
557-
The graph theoretical metrics which have been generated are placed into another CFF file.
546+
The graph theoretical metrics are computed using the networkx workflow and placed in another CFF file
558547
"""
559548

560-
mapping.connect([(creatematrix, ntwkMetrics,[("matrix_file","in_file")])])
561-
mapping.connect([(creatematrix, gpickledNetworks,[("matrix_file","in1")])])
562-
mapping.connect([(ntwkMetrics, gpickledNetworks,[("gpickled_network_files","in2")])])
563-
mapping.connect([(gpickledNetworks, NxStatsCFFConverter,[("out","gpickled_networks")])])
549+
mapping.connect([(inputnode, networkx,[("subject_id","inputnode.extra_field")])])
550+
mapping.connect([(creatematrix, networkx,[("intersection_matrix_file","inputnode.network_file")])])
564551

552+
mapping.connect([(networkx, NxStatsCFFConverter,[("outputnode.network_files","gpickled_networks")])])
565553
mapping.connect([(giftiSurfaces, NxStatsCFFConverter,[("out","gifti_surfaces")])])
566554
mapping.connect([(giftiLabels, NxStatsCFFConverter,[("out","gifti_labels")])])
567555
mapping.connect([(niftiVolumes, NxStatsCFFConverter,[("out","nifti_volumes")])])
568556
mapping.connect([(fiberDataArrays, NxStatsCFFConverter,[("out","data_files")])])
569557
mapping.connect([(inputnode, NxStatsCFFConverter,[("subject_id","title")])])
570558

571-
mapping.connect([(ntwkMetrics, Matlab2CSV_node,[("node_measures_matlab","in_file")])])
572-
mapping.connect([(ntwkMetrics, Matlab2CSV_global,[("global_measures_matlab","in_file")])])
573-
mapping.connect([(creatematrix, Matlab2CSV_cmatrix,[("matrix_mat_file","in_file")])])
574-
mapping.connect([(creatematrix, Matlab2CSV_meanfib,[("mean_fiber_length_matrix_mat_file","in_file")])])
575-
mapping.connect([(creatematrix, Matlab2CSV_medianfib,[("median_fiber_length_matrix_mat_file","in_file")])])
576-
mapping.connect([(creatematrix, Matlab2CSV_fibstd,[("fiber_length_std_matrix_mat_file","in_file")])])
577-
mapping.connect([(Matlab2CSV_node, MergeCSVFiles_node,[("csv_files","in_files")])])
578-
mapping.connect([(inputnode, MergeCSVFiles_node,[("subject_id","out_file")])])
579-
mapping.connect([(inputnode, MergeCSVFiles_node,[("subject_id","extra_field")])])
580-
581-
mapping.connect([(Matlab2CSV_cmatrix, mergeCSVMatrices,[("csv_files","in1")])])
582-
mapping.connect([(Matlab2CSV_meanfib, mergeCSVMatrices,[("csv_files","in2")])])
583-
mapping.connect([(Matlab2CSV_medianfib, mergeCSVMatrices,[("csv_files","in3")])])
584-
mapping.connect([(Matlab2CSV_fibstd, mergeCSVMatrices,[("csv_files","in4")])])
585-
mapping.connect([(mergeCSVMatrices, MergeCSVFiles_cmatrices,[("out","in_files")])])
586-
mapping.connect([(inputnode, MergeCSVFiles_cmatrices,[("subject_id","out_file")])])
587-
mapping.connect([(inputnode, MergeCSVFiles_cmatrices,[("subject_id","extra_field")])])
559+
mapping.connect([(inputnode, cmats_to_csv,[("subject_id","inputnode.extra_field")])])
560+
mapping.connect([(creatematrix, cmats_to_csv,[("matlab_matrix_files","inputnode.matlab_matrix_files")])])
588561

589562
"""
590563
Create a higher-level workflow
@@ -596,7 +569,7 @@
596569

597570
connectivity = pe.Workflow(name="connectivity")
598571

599-
connectivity.base_dir = op.abspath('connectivity_tutorial_advanced')
572+
connectivity.base_dir = op.abspath('dmri_connectivity_advanced')
600573
connectivity.connect([
601574
(infosource,datasource,[('subject_id', 'subject_id')]),
602575
(datasource,mapping,[('dwi','inputnode.dwi'),

nipype/workflows/dmri/mrtrix/connectivity_mapping.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,20 @@ def create_connectivity_pipeline(name="connectivity", parcellation_name='scale50
4646
4747
outputnode.connectome
4848
outputnode.cmatrix
49-
outputnode.gpickled_network
49+
outputnode.networks
5050
outputnode.fa
5151
outputnode.struct
5252
outputnode.tracts
5353
outputnode.rois
5454
outputnode.odfs
5555
outputnode.filtered_tractography
56+
outputnode.tdi
5657
outputnode.nxstatscff
57-
outputnode.nxmatlab
5858
outputnode.nxcsv
59-
outputnode.nxmergedcsv
60-
outputnode.cmatrix_csv
6159
outputnode.cmatrices_csv
6260
outputnode.mean_fiber_length
63-
outputnode.meanfib_csv
61+
outputnode.median_fiber_length
6462
outputnode.fiber_length_std
65-
outputnode.fibstd_csv
6663
"""
6764

6865
inputnode_within = pe.Node(util.IdentityInterface(fields=["subject_id",

0 commit comments

Comments
 (0)