Skip to content

Commit 456cb99

Browse files
committed
Merge pull request #3 from chrisfilo/fix/mrtrix
Fix/mrtrix
2 parents a0112fb + a933a7c commit 456cb99

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

examples/mrtrix_dti_tutorial.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@
2323
import nipype.pipeline.engine as pe # pypeline engine
2424
import nipype.interfaces.mrtrix as mrtrix #<---- The important new part!
2525
import nipype.interfaces.fsl as fsl
26-
import nibabel as nb
26+
import nipype.algorithms.misc as misc
2727
import os, os.path as op # system functions
2828

29-
"""
30-
We import the voxel-, data-, and affine-grabbing functions from the Camino DTI processing workflow
31-
"""
32-
33-
from nipype.workflows.camino.diffusion import get_vox_dims, get_data_dims, get_affine
34-
3529
"""
3630
This needs to point to the fdt folder you can find after extracting
3731
@@ -50,7 +44,7 @@
5044
infosource = pe.Node(interface=util.IdentityInterface(fields=['subject_id']), name="infosource")
5145
infosource.iterables = ('subject_id', subject_list)
5246

53-
info = dict(dwi=[['subject_id', 'dwi']],
47+
info = dict(dwi=[['subject_id', 'data']],
5448
bvecs=[['subject_id','bvecs']],
5549
bvals=[['subject_id','bvals']])
5650

@@ -65,7 +59,7 @@
6559

6660
datasource.inputs.template = "%s/%s"
6761
datasource.inputs.base_directory = data_dir
68-
datasource.inputs.field_template = dict(dwi='%s/%s.nii')
62+
datasource.inputs.field_template = dict(dwi='%s/%s.nii.gz')
6963
datasource.inputs.template_args = info
7064

7165
"""
@@ -99,7 +93,7 @@
9993
* Apparent diffusion coefficient
10094
* Fractional anisotropy
10195
"""
102-
96+
gunzip = pe.Node(interface=misc.Gunzip(), name='gunzip')
10397
dwi2tensor = pe.Node(interface=mrtrix.DWI2Tensor(),name='dwi2tensor')
10498
tensor2vector = pe.Node(interface=mrtrix.Tensor2Vector(),name='tensor2vector')
10599
tensor2adc = pe.Node(interface=mrtrix.Tensor2ApparentDiffusion(),name='tensor2adc')
@@ -179,7 +173,8 @@
179173

180174
tractography.connect([(inputnode, fsl2mrtrix, [("bvecs", "bvec_file"),
181175
("bvals", "bval_file")])])
182-
tractography.connect([(inputnode, dwi2tensor,[("dwi","in_file")])])
176+
tractography.connect([(inputnode, gunzip,[("dwi","in_file")])])
177+
tractography.connect([(gunzip, dwi2tensor,[("out_file","in_file")])])
183178
tractography.connect([(fsl2mrtrix, dwi2tensor,[("encoding_file","encoding_file")])])
184179

185180
tractography.connect([(dwi2tensor, tensor2vector,[['tensor','in_file']]),
@@ -193,7 +188,7 @@
193188
fractional anisotropy image, and thresholds it to get the single-fiber voxels.
194189
"""
195190

196-
tractography.connect([(inputnode, MRconvert,[("dwi","in_file")])])
191+
tractography.connect([(gunzip, MRconvert,[("out_file","in_file")])])
197192
tractography.connect([(MRconvert, threshold_b0,[("converted","in_file")])])
198193
tractography.connect([(threshold_b0, median3d,[("out_file","in_file")])])
199194
tractography.connect([(median3d, erode_mask_firstpass,[("out_file","in_file")])])
@@ -206,8 +201,8 @@
206201
Here the thresholded white matter mask is created for seeding the tractography.
207202
"""
208203

209-
tractography.connect([(inputnode, bet,[("dwi","in_file")])])
210-
tractography.connect([(inputnode, gen_WM_mask,[("dwi","in_file")])])
204+
tractography.connect([(gunzip, bet,[("out_file","in_file")])])
205+
tractography.connect([(gunzip, gen_WM_mask,[("out_file","in_file")])])
211206
tractography.connect([(bet, gen_WM_mask,[("mask_file","binary_mask")])])
212207
tractography.connect([(fsl2mrtrix, gen_WM_mask,[("encoding_file","encoding_file")])])
213208
tractography.connect([(gen_WM_mask, threshold_wmmask,[("WMprobabilitymap","in_file")])])
@@ -216,15 +211,15 @@
216211
Next we estimate the fiber response distribution.
217212
"""
218213

219-
tractography.connect([(inputnode, estimateresponse,[("dwi","in_file")])])
214+
tractography.connect([(gunzip, estimateresponse,[("out_file","in_file")])])
220215
tractography.connect([(fsl2mrtrix, estimateresponse,[("encoding_file","encoding_file")])])
221216
tractography.connect([(threshold_FA, estimateresponse,[("out_file","mask_image")])])
222217

223218
"""
224219
Run constrained spherical deconvolution.
225220
"""
226221

227-
tractography.connect([(inputnode, csdeconv,[("dwi","in_file")])])
222+
tractography.connect([(gunzip, csdeconv,[("out_file","in_file")])])
228223
tractography.connect([(gen_WM_mask, csdeconv,[("WMprobabilitymap","mask_image")])])
229224
tractography.connect([(estimateresponse, csdeconv,[("response","response_file")])])
230225
tractography.connect([(fsl2mrtrix, csdeconv,[("encoding_file","encoding_file")])])
@@ -236,7 +231,10 @@
236231
tractography.connect([(threshold_wmmask, probCSDstreamtrack,[("out_file","seed_file")])])
237232
tractography.connect([(csdeconv, probCSDstreamtrack,[("spherical_harmonics_image","in_file")])])
238233
tractography.connect([(probCSDstreamtrack, tracks2prob,[("tracked","in_file")])])
239-
tractography.connect([(inputnode, tracks2prob,[("dwi","template_file")])])
234+
tractography.connect([(gunzip, tracks2prob,[("out_file","template_file")])])
235+
236+
tractography.connect([(gunzip, tck2trk,[("out_file","image_file")])])
237+
tractography.connect([(probCSDstreamtrack, tck2trk,[("tracked","in_file")])])
240238

241239
"""
242240
Finally, we create another higher-level workflow to connect our tractography workflow with the info and datagrabbing nodes

nipype/algorithms/misc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,36 @@ def _list_outputs(self):
456456
if isdefined(self.inputs.regress_poly):
457457
outputs['detrended_file'] = self._gen_output_file_name('detrended')
458458
return outputs
459+
460+
class GunzipInputSpec(BaseInterfaceInputSpec):
461+
in_file = File(exists=True, mandatory=True)
462+
463+
class GunzipOutputSpec(TraitedSpec):
464+
out_file = File(exists=True)
465+
466+
class Gunzip(BaseInterface):
467+
"""
468+
469+
"""
470+
input_spec = GunzipInputSpec
471+
output_spec = GunzipOutputSpec
472+
473+
def _gen_output_file_name(self):
474+
_, base, ext = split_filename(self.inputs.in_file)
475+
if ext[-2:].lower() == ".gz":
476+
ext = ext[:-3]
477+
return os.path.abspath(base + ext[:-3])
478+
479+
def _run_interface(self, runtime):
480+
import gzip
481+
in_file = gzip.open(self.inputs.in_file, 'rb')
482+
out_file = open(self._gen_output_file_name(), 'wb')
483+
out_file.write(in_file.read())
484+
out_file.close()
485+
in_file.close()
486+
return runtime
487+
488+
def _list_outputs(self):
489+
outputs = self._outputs().get()
490+
outputs['out_file'] = self._gen_output_file_name()
491+
return outputs

nipype/interfaces/mrtrix/convert.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ def _run_interface(self, runtime):
328328
# as a commandline argument to the matlab executable (without creating any files).
329329
# This, however, is less reliable and harder to debug (code will be reduced to
330330
# a single line and stripped of any comments).
331-
print script
332331
result = MatlabCommand(script=script, mfile=True)
333332
r = result.run()
334333
return runtime

0 commit comments

Comments
 (0)