18
18
import shutil
19
19
from nipype .utils .misc import package_check
20
20
import warnings
21
+ import logging
22
+
23
+ logging .basicConfig ()
24
+ iflogger = logging .getLogger ('interface' )
21
25
22
26
have_cmp = True
23
27
try :
30
34
from cmp .util import runCmd
31
35
32
36
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 ( "=================================================" )
35
39
fs_label_dir = op .join (op .join (subjects_dir ,subject_id ), 'label' )
36
40
output_dir = op .abspath (op .curdir )
37
41
paths = []
@@ -68,15 +72,15 @@ def create_annot_label(subject_id, subjects_dir, fs_dir, parcellation_name):
68
72
mris_cmd = 'mris_ca_label %s %s "%s/surf/%s.sphere.reg" "%s" "%s" ' % (subject_id , out [0 ],
69
73
op .join (subjects_dir ,subject_id ), out [0 ], cmp_config .get_lausanne_atlas (out [1 ]), op .join (fs_label_dir , out [2 ]))
70
74
runCmd ( mris_cmd , log )
71
- print '-----------'
75
+ iflogger . info ( '-----------' )
72
76
73
77
annot = '--annotation "%s"' % out [4 ]
74
78
75
79
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 )
77
81
runCmd ( mri_an_cmd , log )
78
- print '-----------'
79
- print os .environ ['SUBJECTS_DIR' ]
82
+ iflogger . info ( '-----------' )
83
+ iflogger . info ( os .environ ['SUBJECTS_DIR' ])
80
84
# extract cc and unknown to add to tractography mask, we do not want this as a region of interest
81
85
# in FS 5.0, unknown and corpuscallosum are not available for the 35 scale (why?),
82
86
# 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):
97
101
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 ))
98
102
runCmd ( mri_cmd , log )
99
103
100
- print "[ DONE ]"
104
+ iflogger . info ( "[ DONE ]" )
101
105
102
106
def create_roi (subject_id , subjects_dir , fs_dir , parcellation_name ):
103
107
""" Creates the ROI_%s.nii.gz files using the given parcellation information
104
108
from networks. Iteratively create volume. """
105
- print "Create the ROIs:"
109
+ iflogger . info ( "Create the ROIs:" )
106
110
output_dir = op .abspath (op .curdir )
107
111
fs_dir = op .join (subjects_dir , subject_id )
108
112
cmp_config = cmp .configuration .PipelineConfiguration ()
@@ -113,9 +117,9 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name):
113
117
aseg = nb .load (op .join (fs_dir , 'mri' , 'aseg.nii.gz' ))
114
118
asegd = aseg .get_data ()
115
119
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 ( "========================" )
119
123
pg = nx .read_graphml (pgpath )
120
124
# each node represents a brain region
121
125
# 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):
124
128
count = 0
125
129
for brk , brv in pg .nodes_iter (data = True ):
126
130
count = count + 1
127
- print brv
128
- print brk
131
+ iflogger . info ( brv )
132
+ iflogger . info ( brk )
129
133
if brv ['dn_hemisphere' ] == 'left' :
130
134
hemi = 'lh'
131
135
elif brv ['dn_hemisphere' ] == 'right' :
132
136
hemi = 'rh'
133
137
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 ( "---------------------" )
140
144
# if it is subcortical, retrieve roi from aseg
141
145
idx = np .where (asegd == int (brv ['dn_fs_aseg_val' ]))
142
146
rois [idx ] = int (brv ['dn_correspondence_id' ])
143
147
144
148
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 ( "---------------------" )
151
155
152
156
labelpath = op .join (output_dir , parval ['fs_label_subdir_name' ] % hemi )
153
157
# construct .label file name
@@ -180,11 +184,11 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name):
180
184
img = nb .Nifti1Image (rois , aseg .get_affine (), hdr2 )
181
185
nb .save (img , out_roi )
182
186
183
- print "[ DONE ]"
187
+ iflogger . info ( "[ DONE ]" )
184
188
185
189
186
190
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" )
188
192
fs_dir = op .join (subjects_dir , subject_id )
189
193
cmp_config = cmp .configuration .PipelineConfiguration ()
190
194
cmp_config .parcellation_scheme = "Lausanne2008"
@@ -291,32 +295,32 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name):
291
295
# now remove all the structures from the white matter
292
296
idx = np .where ( (csfA != 0 ) | (csfB != 0 ) | (gr_ncl != 0 ) | (remaining != 0 ) )
293
297
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" )
295
299
296
300
# ADD voxels from 'cc_unknown.nii.gz' dataset
297
301
ccun = nb .load (op .join (fs_dir , 'label' , 'cc_unknown.nii.gz' ))
298
302
ccund = ccun .get_data ()
299
303
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" )
301
305
wmmask [idx ] = 1
302
306
303
307
# check if we should subtract the cortical rois from this parcellation
304
308
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 ) )
306
310
roi = nb .load (op .join (op .curdir , 'ROI_%s.nii.gz' % parcellation_name ))
307
311
roid = roi .get_data ()
308
312
assert roid .shape [0 ] == wmmask .shape [0 ]
309
313
pg = nx .read_graphml (pgpath )
310
314
for brk , brv in pg .nodes_iter (data = True ):
311
315
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' ]) )
313
317
idx = np .where (roid == int (brv ['dn_correspondence_id' ]))
314
318
wmmask [idx ] = 0
315
319
316
320
# output white matter mask. crop and move it afterwards
317
321
wm_out = op .join (fs_dir , 'mri' , 'fsmask_1mm.nii.gz' )
318
322
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 )
320
324
nb .save (img , wm_out )
321
325
322
326
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,
328
332
reg_path = out_roi_file
329
333
output_dir = op .abspath (op .curdir )
330
334
331
- print "Cropping and moving datasets to %s" % output_dir
335
+ iflogger . info ( "Cropping and moving datasets to %s" % output_dir )
332
336
ds = [
333
337
(op .join (fs_dir , 'mri' , 'aseg.nii.gz' ), op .join (output_dir , 'aseg.nii.gz' ) ),
334
338
(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,
339
343
ds .append ( (op .join (op .curdir , 'ROI_%s.nii.gz' % parcellation_name ), op .join (op .curdir , 'ROI_HR_th.nii.gz' )) )
340
344
orig = op .join (fs_dir , 'mri' , 'orig' , '001.mgz' )
341
345
for d in ds :
342
- print "Processing %s:" % d [0 ]
346
+ iflogger . info ( "Processing %s:" % d [0 ])
343
347
if not op .exists (d [0 ]):
344
348
raise Exception ('File %s does not exist.' % d [0 ])
345
349
# reslice to original volume because the roi creation with freesurfer
@@ -382,8 +386,8 @@ class Parcellate(BaseInterface):
382
386
def _run_interface (self , runtime ):
383
387
if self .inputs .subjects_dir :
384
388
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 ( "=============================================" )
387
391
create_annot_label (self .inputs .subject_id , self .inputs .subjects_dir , self .inputs .freesurfer_dir , self .inputs .parcellation_name )
388
392
create_roi (self .inputs .subject_id , self .inputs .subjects_dir , self .inputs .freesurfer_dir , self .inputs .parcellation_name )
389
393
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):
396
400
outputs ['roi_file' ] = op .abspath (self .inputs .out_roi_file )
397
401
else :
398
402
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' )
399
408
return outputs
400
409
401
410
def _gen_outfilename (self , ext , prefix = 'ROI' ):
0 commit comments