Skip to content

Commit a67d480

Browse files
author
Erik Ziegler
committed
Documentation updates, PEP8, reworked output naming
1 parent b9c8775 commit a67d480

File tree

1 file changed

+73
-46
lines changed

1 file changed

+73
-46
lines changed

nipype/interfaces/dipy/tensors.py

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
from nipype.interfaces.base import (TraitedSpec, BaseInterface, BaseInterfaceInputSpec,
3-
File, isdefined, traits)
2+
from nipype.interfaces.base import (
3+
TraitedSpec, BaseInterface, File)
44
from nipype.utils.filemanip import split_filename
55
import os.path as op
66
import nibabel as nb
@@ -21,55 +21,82 @@
2121

2222
class TensorModeInputSpec(TraitedSpec):
2323
in_file = File(exists=True, mandatory=True,
24-
desc='The input diffusion-weighted image file')
24+
desc='The input 4D diffusion-weighted image file')
2525
bvecs = File(exists=True, mandatory=True,
26-
desc='The input b-vector file')
26+
desc='The input b-vector text file')
2727
bvals = File(exists=True, mandatory=True,
28-
desc='The input b-value track file')
29-
out_filename = File('mode.nii', usedefault=True, desc='The output filename for the tracks in TrackVis (.trk) format')
28+
desc='The input b-value text file')
29+
out_filename = File(
30+
genfile=True, desc='The output filename for the Tensor mode image')
31+
3032

3133
class TensorModeOutputSpec(TraitedSpec):
3234
out_file = File(exists=True)
3335

36+
3437
class TensorMode(BaseInterface):
35-
"""
36-
Creates a tract density image from a TrackVis track file using functions from dipy
37-
38-
Example
39-
-------
40-
41-
>>> import nipype.interfaces.dipy as dipy
42-
>>> mode = dipy.TensorMode()
43-
>>> mode.inputs.in_file = 'dwi.nii'
44-
>>> mode.run() # doctest: +SKIP
45-
"""
46-
input_spec = TensorModeInputSpec
47-
output_spec = TensorModeOutputSpec
48-
49-
def _run_interface(self, runtime):
50-
img=nb.load(self.inputs.in_file)
51-
data=img.get_data()
52-
affine=img.get_affine()
53-
54-
bvals=np.loadtxt(self.inputs.bvals)
55-
gradients=np.loadtxt(self.inputs.bvecs).T
56-
57-
gtab = GradientTable(gradients)
58-
gtab.bvals = bvals
59-
60-
mask = data[..., 0] > 50
61-
tenmodel = dti.TensorModel(gtab)
62-
tenfit = tenmodel.fit(data, mask)
63-
64-
mode_data = tenfit.mode
65-
img = nb.Nifti1Image(mode_data,affine)
66-
out_file = op.abspath(self.inputs.out_filename)
67-
nb.save(img, out_file)
68-
iflogger.info('Tensor mode image saved as {i}'.format(i=out_file))
69-
return runtime
70-
71-
def _list_outputs(self):
72-
outputs = self._outputs().get()
73-
outputs['out_file'] = op.abspath(self.inputs.out_filename)
74-
return outputs
38+
"""
39+
Creates a map of the mode of the diffusion tensors given a set of
40+
diffusion-weighted images, as well as their associated b-values and
41+
b-vectors. Fits the diffusion tensors and calculates tensor mode
42+
with Dipy.
43+
44+
Example
45+
-------
46+
47+
>>> import nipype.interfaces.dipy as dipy
48+
>>> mode = dipy.TensorMode()
49+
>>> mode.inputs.in_file = 'dwi.nii'
50+
>>> mode.inputs.bvecs = 'bvecs'
51+
>>> mode.inputs.bvals = 'bvals'
52+
>>> mode.run() # doctest: +SKIP
53+
"""
54+
input_spec = TensorModeInputSpec
55+
output_spec = TensorModeOutputSpec
56+
57+
def _run_interface(self, runtime):
58+
## Load the 4D image files
59+
img = nb.load(self.inputs.in_file)
60+
data = img.get_data()
61+
affine = img.get_affine()
62+
63+
## Load the gradient strengths and directions
64+
bvals = np.loadtxt(self.inputs.bvals)
65+
gradients = np.loadtxt(self.inputs.bvecs).T
66+
67+
## Place in Dipy's preferred format
68+
gtab = GradientTable(gradients)
69+
gtab.bvals = bvals
70+
71+
## Mask the data so that tensors are not fit for
72+
## unnecessary voxels
73+
mask = data[..., 0] > 50
74+
75+
## Fit the tensors to the data
76+
tenmodel = dti.TensorModel(gtab)
77+
tenfit = tenmodel.fit(data, mask)
78+
79+
## Calculate the mode of each voxel's tensor
80+
mode_data = tenfit.mode
81+
82+
## Write as a 3D Nifti image with the original affine
83+
img = nb.Nifti1Image(mode_data, affine)
84+
out_file = op.abspath(self._gen_outfilename())
85+
nb.save(img, out_file)
86+
iflogger.info('Tensor mode image saved as {i}'.format(i=out_file))
87+
return runtime
88+
89+
def _list_outputs(self):
90+
outputs = self._outputs().get()
91+
outputs['out_file'] = op.abspath(self._gen_outfilename())
92+
return outputs
93+
94+
def _gen_filename(self, name):
95+
if name is 'out_filename':
96+
return self._gen_outfilename()
97+
else:
98+
return None
7599

100+
def _gen_outfilename(self):
101+
_, name, _ = split_filename(self.inputs.in_file)
102+
return name + '_mode.nii'

0 commit comments

Comments
 (0)