Skip to content

Commit 7b80b00

Browse files
author
erikz
committed
Camino group tutorial runs properly
1 parent 984aa21 commit 7b80b00

File tree

7 files changed

+113
-49
lines changed

7 files changed

+113
-49
lines changed

examples/dmri_group_connectivity_camino.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
We perform this analysis using one healthy subject and two subjects who suffer from Parkinson's disease.
1616
17-
The whole package (754 mb as .tar.gz / 1 gb uncompressed) including the Freesurfer directories for these subjects, can be acquired from here:
17+
The whole package (960 mb as .tar.gz / 1.3 gb uncompressed) including the Freesurfer directories for these subjects, can be acquired from here:
1818
1919
* http://db.tt/b6F1t0QV
2020
@@ -79,7 +79,7 @@
7979

8080
group_list = {}
8181
group_list['controls'] = ['cont17']
82-
group_list['parkinsons'] = ['pat07', 'pat20']
82+
group_list['parkinsons'] = ['pat10', 'pat20']
8383

8484
"""
8585
The output directory must be named as well.
@@ -131,10 +131,10 @@
131131
Define the parcellation scheme to use.
132132
"""
133133

134-
parcellation_name = 'scale500'
134+
parcellation_scheme = 'NativeFreesurfer'
135135
cmp_config = cmp.configuration.PipelineConfiguration()
136-
cmp_config.parcellation_scheme = "Lausanne2008"
137-
l1pipeline.inputs.connectivity.inputnode.resolution_network_file = cmp_config._get_lausanne_parcellation('Lausanne2008')[parcellation_name]['node_information_graphml']
136+
cmp_config.parcellation_scheme = parcellation_scheme
137+
l1pipeline.inputs.connectivity.inputnode.resolution_network_file = cmp_config._get_lausanne_parcellation(parcellation_scheme)['freesurferaparc']['node_information_graphml']
138138

139139
"""
140140
The first level pipeline we have tweaked here is run within the for loop.

examples/dmri_group_connectivity_mrtrix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
We perform this analysis using one healthy subject and two subjects who suffer from Parkinson's disease.
1616
17-
The whole package (754 mb as .tar.gz / 1 gb uncompressed) including the Freesurfer directories for these subjects, can be acquired from here:
17+
The whole package (960 mb as .tar.gz / 1.3 gb uncompressed) including the Freesurfer directories for these subjects, can be acquired from here:
1818
1919
* http://db.tt/b6F1t0QV
2020
@@ -77,7 +77,7 @@
7777

7878
group_list = {}
7979
group_list['controls'] = ['cont17']
80-
group_list['parkinsons'] = ['pat07', 'pat20']
80+
group_list['parkinsons'] = ['pat10', 'pat20']
8181

8282
"""
8383
The output directory must be named as well.

nipype/interfaces/cmtk/cmtk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
212212
# compute a position for the node based on the mean position of the
213213
# ROI in voxel coordinates (segmentation volume )
214214
xyz = tuple(np.mean(np.where(np.flipud(roiData) == int(d["dn_correspondence_id"])) , axis=1))
215-
G.node[int(u)]['dn_position'] = tuple([xyz[0], xyz[1], xyz[2]])
215+
G.node[int(u)]['dn_position'] = tuple([xyz[0], xyz[2], -xyz[1]])
216216

217217
if intersections:
218218
iflogger.info("Filtering tractography from intersections")

nipype/interfaces/cmtk/parcellation.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import shutil
1919
from nipype.utils.misc import package_check
2020
import warnings
21+
import logging
22+
23+
logging.basicConfig()
24+
iflogger = logging.getLogger('interface')
2125

2226
have_cmp = True
2327
try:
@@ -30,8 +34,8 @@
3034
from cmp.util import runCmd
3135

3236
def create_annot_label(subject_id, subjects_dir, fs_dir, parcellation_name):
33-
print "Create the cortical labels necessary for our ROIs"
34-
print "================================================="
37+
iflogger.info("Create the cortical labels necessary for our ROIs")
38+
iflogger.info("=================================================")
3539
fs_label_dir = op.join(op.join(subjects_dir,subject_id), 'label')
3640
output_dir = op.abspath(op.curdir)
3741
paths = []
@@ -68,15 +72,15 @@ def create_annot_label(subject_id, subjects_dir, fs_dir, parcellation_name):
6872
mris_cmd = 'mris_ca_label %s %s "%s/surf/%s.sphere.reg" "%s" "%s" ' % (subject_id, out[0],
6973
op.join(subjects_dir,subject_id), out[0], cmp_config.get_lausanne_atlas(out[1]), op.join(fs_label_dir, out[2]))
7074
runCmd( mris_cmd, log )
71-
print '-----------'
75+
iflogger.info('-----------')
7276

7377
annot = '--annotation "%s"' % out[4]
7478

7579
mri_an_cmd = 'mri_annotation2label --subject %s --hemi %s --outdir "%s" %s' % (subject_id, out[0], op.join(output_dir, out[3]), annot)
76-
print mri_an_cmd
80+
iflogger.info(mri_an_cmd)
7781
runCmd( mri_an_cmd, log )
78-
print '-----------'
79-
print os.environ['SUBJECTS_DIR']
82+
iflogger.info('-----------')
83+
iflogger.info(os.environ['SUBJECTS_DIR'])
8084
# extract cc and unknown to add to tractography mask, we do not want this as a region of interest
8185
# in FS 5.0, unknown and corpuscallosum are not available for the 35 scale (why?),
8286
# but for the other scales only, take the ones from _60
@@ -97,12 +101,12 @@ def create_annot_label(subject_id, subjects_dir, fs_dir, parcellation_name):
97101
mri_cmd = 'mri_convert -i "%s/mri/aseg.mgz" -o "%s/mri/aseg.nii.gz"' % (op.join(subjects_dir,subject_id), op.join(subjects_dir,subject_id))
98102
runCmd( mri_cmd, log )
99103

100-
print "[ DONE ]"
104+
iflogger.info("[ DONE ]")
101105

102106
def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name):
103107
""" Creates the ROI_%s.nii.gz files using the given parcellation information
104108
from networks. Iteratively create volume. """
105-
print "Create the ROIs:"
109+
iflogger.info("Create the ROIs:")
106110
output_dir = op.abspath(op.curdir)
107111
fs_dir = op.join(subjects_dir, subject_id)
108112
cmp_config = cmp.configuration.PipelineConfiguration()
@@ -113,9 +117,9 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name):
113117
aseg = nb.load(op.join(fs_dir, 'mri', 'aseg.nii.gz'))
114118
asegd = aseg.get_data()
115119

116-
print "Working on parcellation: "
117-
print cmp_config._get_lausanne_parcellation('Lausanne2008')[parcellation_name]
118-
print "========================"
120+
iflogger.info("Working on parcellation: ")
121+
iflogger.info(cmp_config._get_lausanne_parcellation('Lausanne2008')[parcellation_name])
122+
iflogger.info("========================")
119123
pg = nx.read_graphml(pgpath)
120124
# each node represents a brain region
121125
# create a big 256^3 volume for storage of all ROIs
@@ -124,30 +128,30 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name):
124128
count = 0
125129
for brk, brv in pg.nodes_iter(data=True):
126130
count = count + 1
127-
print brv
128-
print brk
131+
iflogger.info(brv)
132+
iflogger.info(brk)
129133
if brv['dn_hemisphere'] == 'left':
130134
hemi = 'lh'
131135
elif brv['dn_hemisphere'] == 'right':
132136
hemi = 'rh'
133137
if brv['dn_region'] == 'subcortical':
134-
print brv
135-
print "---------------------"
136-
print "Work on brain region: %s" % (brv['dn_region'])
137-
print "Freesurfer Name: %s" % brv['dn_fsname']
138-
print "Region %s of %s " % (count, pg.number_of_nodes())
139-
print "---------------------"
138+
iflogger.info(brv)
139+
iflogger.info("---------------------")
140+
iflogger.info("Work on brain region: %s" % (brv['dn_region']))
141+
iflogger.info("Freesurfer Name: %s" % brv['dn_fsname'])
142+
iflogger.info("Region %s of %s " % (count, pg.number_of_nodes()))
143+
iflogger.info("---------------------")
140144
# if it is subcortical, retrieve roi from aseg
141145
idx = np.where(asegd == int(brv['dn_fs_aseg_val']))
142146
rois[idx] = int(brv['dn_correspondence_id'])
143147

144148
elif brv['dn_region'] == 'cortical':
145-
print brv
146-
print "---------------------"
147-
print "Work on brain region: %s" % (brv['dn_region'])
148-
print "Freesurfer Name: %s" % brv['dn_fsname']
149-
print "Region %s of %s " % (count, pg.number_of_nodes())
150-
print "---------------------"
149+
iflogger.info(brv)
150+
iflogger.info("---------------------")
151+
iflogger.info("Work on brain region: %s" % (brv['dn_region']))
152+
iflogger.info("Freesurfer Name: %s" % brv['dn_fsname'])
153+
iflogger.info("Region %s of %s " % (count, pg.number_of_nodes()))
154+
iflogger.info("---------------------")
151155

152156
labelpath = op.join(output_dir, parval['fs_label_subdir_name'] % hemi)
153157
# construct .label file name
@@ -180,11 +184,11 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name):
180184
img = nb.Nifti1Image(rois, aseg.get_affine(), hdr2)
181185
nb.save(img, out_roi)
182186

183-
print "[ DONE ]"
187+
iflogger.info("[ DONE ]")
184188

185189

186190
def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name):
187-
print "Create white matter mask"
191+
iflogger.info("Create white matter mask")
188192
fs_dir = op.join(subjects_dir, subject_id)
189193
cmp_config = cmp.configuration.PipelineConfiguration()
190194
cmp_config.parcellation_scheme = "Lausanne2008"
@@ -291,32 +295,32 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name):
291295
# now remove all the structures from the white matter
292296
idx = np.where( (csfA != 0) | (csfB != 0) | (gr_ncl != 0) | (remaining != 0) )
293297
wmmask[idx] = 0
294-
print "Removing lateral ventricles and eroded grey nuclei and brainstem from white matter mask"
298+
iflogger.info("Removing lateral ventricles and eroded grey nuclei and brainstem from white matter mask")
295299

296300
# ADD voxels from 'cc_unknown.nii.gz' dataset
297301
ccun = nb.load(op.join(fs_dir, 'label', 'cc_unknown.nii.gz'))
298302
ccund = ccun.get_data()
299303
idx = np.where(ccund != 0)
300-
print "Add corpus callosum and unknown to wm mask"
304+
iflogger.info("Add corpus callosum and unknown to wm mask")
301305
wmmask[idx] = 1
302306

303307
# check if we should subtract the cortical rois from this parcellation
304308
parval = cmp_config._get_lausanne_parcellation('Lausanne2008')[parcellation_name]
305-
print "Loading %s to subtract cortical ROIs from white matter mask" % ('ROI_%s.nii.gz' % parcellation_name)
309+
iflogger.info("Loading %s to subtract cortical ROIs from white matter mask" % ('ROI_%s.nii.gz' % parcellation_name))
306310
roi = nb.load(op.join(op.curdir, 'ROI_%s.nii.gz' % parcellation_name))
307311
roid = roi.get_data()
308312
assert roid.shape[0] == wmmask.shape[0]
309313
pg = nx.read_graphml(pgpath)
310314
for brk, brv in pg.nodes_iter(data=True):
311315
if brv['dn_region'] == 'cortical':
312-
print "Subtracting region %s with intensity value %s" % (brv['dn_region'], brv['dn_correspondence_id'])
316+
iflogger.info("Subtracting region %s with intensity value %s" % (brv['dn_region'], brv['dn_correspondence_id']))
313317
idx = np.where(roid == int(brv['dn_correspondence_id']))
314318
wmmask[idx] = 0
315319

316320
# output white matter mask. crop and move it afterwards
317321
wm_out = op.join(fs_dir, 'mri', 'fsmask_1mm.nii.gz')
318322
img = nb.Nifti1Image(wmmask, fsmask.get_affine(), fsmask.get_header() )
319-
print "Save white matter mask: %s" % wm_out
323+
iflogger.info("Save white matter mask: %s" % wm_out)
320324
nb.save(img, wm_out)
321325

322326
def crop_and_move_datasets(subject_id, subjects_dir, fs_dir, parcellation_name, out_roi_file):
@@ -328,7 +332,7 @@ def crop_and_move_datasets(subject_id, subjects_dir, fs_dir, parcellation_name,
328332
reg_path = out_roi_file
329333
output_dir = op.abspath(op.curdir)
330334

331-
print "Cropping and moving datasets to %s" % output_dir
335+
iflogger.info("Cropping and moving datasets to %s" % output_dir)
332336
ds = [
333337
(op.join(fs_dir, 'mri', 'aseg.nii.gz'), op.join(output_dir, 'aseg.nii.gz') ),
334338
(op.join(fs_dir, 'mri', 'ribbon.nii.gz'), op.join(output_dir, 'ribbon.nii.gz') ),
@@ -339,7 +343,7 @@ def crop_and_move_datasets(subject_id, subjects_dir, fs_dir, parcellation_name,
339343
ds.append( (op.join(op.curdir, 'ROI_%s.nii.gz' % parcellation_name), op.join(op.curdir, 'ROI_HR_th.nii.gz')) )
340344
orig = op.join(fs_dir, 'mri', 'orig', '001.mgz')
341345
for d in ds:
342-
print "Processing %s:" % d[0]
346+
iflogger.info("Processing %s:" % d[0])
343347
if not op.exists(d[0]):
344348
raise Exception('File %s does not exist.' % d[0])
345349
# reslice to original volume because the roi creation with freesurfer
@@ -382,8 +386,8 @@ class Parcellate(BaseInterface):
382386
def _run_interface(self, runtime):
383387
if self.inputs.subjects_dir:
384388
os.environ.update({'SUBJECTS_DIR': self.inputs.subjects_dir})
385-
print "ROI_HR_th.nii.gz / fsmask_1mm.nii.gz CREATION"
386-
print "============================================="
389+
iflogger.info("ROI_HR_th.nii.gz / fsmask_1mm.nii.gz CREATION")
390+
iflogger.info("=============================================")
387391
create_annot_label(self.inputs.subject_id, self.inputs.subjects_dir, self.inputs.freesurfer_dir, self.inputs.parcellation_name)
388392
create_roi(self.inputs.subject_id, self.inputs.subjects_dir, self.inputs.freesurfer_dir, self.inputs.parcellation_name)
389393
create_wm_mask(self.inputs.subject_id, self.inputs.subjects_dir, self.inputs.freesurfer_dir, self.inputs.parcellation_name)
@@ -396,6 +400,11 @@ def _list_outputs(self):
396400
outputs['roi_file'] = op.abspath(self.inputs.out_roi_file)
397401
else:
398402
outputs['roi_file'] = op.abspath(self._gen_outfilename('nii.gz', 'ROI'))
403+
outputs['white_matter_mask_file'] = op.abspath('fsmask_1mm.nii.gz')
404+
outputs['cc_unknown_file'] = op.abspath('cc_unknown.nii.gz')
405+
outputs['ribbon_file'] = op.abspath('ribbon.nii.gz')
406+
outputs['aseg_file'] = op.abspath('aseg.nii.gz')
407+
outputs['ROI_HR_th_file'] = op.abspath('ROI_HR_th.nii.gz')
399408
return outputs
400409

401410
def _gen_outfilename(self, ext, prefix='ROI'):

nipype/workflows/dmri/camino/connectivity_mapping.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def create_connectivity_pipeline(name="connectivity"):
221221
"""
222222

223223
roigen = pe.Node(interface=cmtk.ROIGen(), name="ROIGen")
224+
roigen_structspace = roigen.clone("ROIGen_structspace")
224225

225226
"""
226227
The CreateMatrix interface takes in the remapped aparc+aseg image as well as the label dictionary and fiber tracts
@@ -231,6 +232,7 @@ def create_connectivity_pipeline(name="connectivity"):
231232
specific tracts that connect between user-selected regions.
232233
"""
233234

235+
createnodes = pe.Node(interface=cmtk.CreateNodes(), name="CreateNodes")
234236
creatematrix = pe.Node(interface=cmtk.CreateMatrix(), name="CreateMatrix")
235237
creatematrix.inputs.count_region_intersections = True
236238

@@ -382,12 +384,16 @@ def create_connectivity_pipeline(name="connectivity"):
382384
"""
383385

384386
mapping.connect(inputnode_within, 'resolution_network_file',
387+
createnodes, 'resolution_network_file')
388+
mapping.connect(createnodes, 'node_network',
385389
creatematrix, 'resolution_network_file')
386390
mapping.connect([(FreeSurferSource, mri_convert_AparcAseg, [(('aparc_aseg', select_aparc), 'in_file')])])
387391

388392
mapping.connect([(b0Strip, inverse_AparcAseg,[('out_file','reference')])])
389393
mapping.connect([(convertxfm, inverse_AparcAseg,[('out_file','in_matrix_file')])])
390394
mapping.connect([(mri_convert_AparcAseg, inverse_AparcAseg,[('out_file','in_file')])])
395+
mapping.connect([(mri_convert_AparcAseg, roigen_structspace,[('out_file','aparc_aseg_file')])])
396+
mapping.connect([(roigen_structspace, createnodes,[("roi_file","roi_file")])])
391397

392398
mapping.connect([(inverse_AparcAseg, roigen,[("out_file","aparc_aseg_file")])])
393399
mapping.connect([(roigen, creatematrix,[("roi_file","roi_file")])])
@@ -432,13 +438,13 @@ def create_connectivity_pipeline(name="connectivity"):
432438
CFFConverter.inputs.script_files = op.abspath(inspect.getfile(inspect.currentframe()))
433439
mapping.connect([(giftiSurfaces, CFFConverter,[("out","gifti_surfaces")])])
434440
mapping.connect([(giftiLabels, CFFConverter,[("out","gifti_labels")])])
435-
mapping.connect([(creatematrix, CFFConverter,[("matrix_file","gpickled_networks")])])
441+
mapping.connect([(creatematrix, CFFConverter,[("matrix_files","gpickled_networks")])])
436442

437443
mapping.connect([(niftiVolumes, CFFConverter,[("out","nifti_volumes")])])
438444
mapping.connect([(fiberDataArrays, CFFConverter,[("out","data_files")])])
439445
mapping.connect([(camino2trackvis, CFFConverter,[("trackvis","tract_files")])])
440446
mapping.connect([(inputnode_within, CFFConverter,[("subject_id","title")])])
441-
447+
442448
"""
443449
Finally, we create another higher-level workflow to connect our mapping workflow with the info and datagrabbing nodes
444450
declared at the beginning. Our tutorial can is now extensible to any arbitrary number of subjects by simply adding
@@ -453,7 +459,7 @@ def create_connectivity_pipeline(name="connectivity"):
453459
"tracts",
454460
"connectome",
455461
"cmatrix",
456-
"gpickled_network",
462+
"networks",
457463
"rois",
458464
"mean_fiber_length",
459465
"fiber_length_std",
@@ -477,7 +483,7 @@ def create_connectivity_pipeline(name="connectivity"):
477483
("CreateMatrix.mean_fiber_length_matrix_mat_file", "mean_fiber_length"),
478484
("CreateMatrix.fiber_length_std_matrix_mat_file", "fiber_length_std"),
479485
("fa2nii.nifti_file", "fa"),
480-
("CreateMatrix.matrix_file", "gpickled_network"),
486+
("CreateMatrix.matrix_files", "networks"),
481487
("ROIGen.roi_file", "rois"),
482488
("mri_convert_Brain.out_file", "struct"),
483489
("trace2nii.nifti_file", "trace"),

nipype/workflows/dmri/camino/group_connectivity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def create_group_connectivity_pipeline(group_list, group_id, data_dir, subjects_
9090
("outputnode.cmatrix", "@l1output.cmatrix"),
9191
("outputnode.rois", "@l1output.rois"),
9292
("outputnode.struct", "@l1output.struct"),
93-
("outputnode.gpickled_network", "@l1output.gpickled_network"),
93+
("outputnode.networks", "@l1output.networks"),
9494
("outputnode.mean_fiber_length", "@l1output.mean_fiber_length"),
9595
("outputnode.fiber_length_std", "@l1output.fiber_length_std"),
9696
])])

0 commit comments

Comments
 (0)