Skip to content

Commit 3d7c459

Browse files
committed
RF: Remove some code duplication.
1 parent 84c360f commit 3d7c459

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

nipype/interfaces/dipy/tensors.py

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,42 @@
2929
from dipy.io.utils import nifti1_symmat
3030

3131

32+
def tensor_fitting(in_file, bvals, bvecs, mask_file=None)
33+
"""
34+
Use dipy to fit DTI
35+
36+
Parameters
37+
----------
38+
in_file : str
39+
Full path to a DWI data file
40+
bvals : str
41+
Full path to a file containing gradient magnitude information (b-values)
42+
bvecs : str
43+
Full path to a file containing gradient direction information (b-vectors)
44+
mask_file : str, optional
45+
Full path to a file containing a binary mask. Defaults to use the entire volume.
46+
47+
Returns
48+
-------
49+
TensorFit object
50+
"""
51+
img = nb.load(in_file)
52+
data = img.get_data()
53+
affine = img.get_affine()
54+
if mask_file is not None:
55+
mask = nb.load(self.inputs.mask_file).get_data()
56+
else:
57+
mask=None
58+
59+
# Load information about the gradients:
60+
gtab = grad.gradient_table(self.inputs.bvals, self.inputs.bvecs)
61+
gtab.bvals = bvals
62+
63+
# Fit it
64+
tenmodel = dti.TensorModel(gtab)
65+
return tenmodel.fit(data, mask)
66+
67+
3268
class DTIInputSpec(TraitedSpec):
3369
in_file = File(exists=True, mandatory=True,
3470
desc='The input 4D diffusion-weighted image file')
@@ -65,22 +101,9 @@ class DTI(BaseInterface):
65101
output_spec = DTIOutputSpec
66102

67103
def _run_interface(self, runtime):
68-
## Load the 4D image files
69-
img = nb.load(self.inputs.in_file)
70-
data = img.get_data()
71-
affine = img.get_affine()
72-
mask = nb.load(self.inputs.mask_file).get_data()
73-
74-
# Load information about the gradients:
75-
gtab = grad.gradient_table(self.inputs.bvals, self.inputs.bvecs)
76-
gtab.bvals = bvals
77-
78-
# Fit it
79-
tenmodel = dti.TensorModel(gtab)
80-
tenfit = tenmodel.fit(data, mask)
81-
104+
ten_fit = tensor_fitting(self.inputs.in_file, self.inputs.bvals, self.inputs.bvecs,
105+
self.inputs.mask_file)
82106
lower_triangular = tenfit.lower_triangular()
83-
lower_triangular *= opts.scale
84107
img = nifti1_symmat(lower_triangular, affine)
85108
out_file = op.abspath(self._gen_outfilename())
86109
nb.save(img, out_file)
@@ -147,24 +170,11 @@ class TensorMode(BaseInterface):
147170
output_spec = TensorModeOutputSpec
148171

149172
def _run_interface(self, runtime):
150-
## Load the 4D image files
151-
img = nb.load(self.inputs.in_file)
152-
data = img.get_data()
153-
affine = img.get_affine()
154-
mask = nb.load(self.inputs.mask_file).get_data()
155-
156-
## Load the gradients
157-
bvals = grad.gradient_table(self.inputs.bvals, self.inputs.bvecs)
158-
159-
## Fit the tensors to the data
160-
tenmodel = dti.TensorModel(gtab)
161-
tenfit = tenmodel.fit(data, mask)
162-
163-
## Calculate the mode of each voxel's tensor
164-
mode_data = tenfit.mode
173+
ten_fit = tensor_fitting(self.inputs.in_file, self.inputs.bvals, self.inputs.bvecs,
174+
self.inputs.mask_file)
165175

166176
## Write as a 3D Nifti image with the original affine
167-
img = nb.Nifti1Image(mode_data, affine)
177+
img = nb.Nifti1Image(tenfit.mode, affine)
168178
out_file = op.abspath(self._gen_outfilename())
169179
nb.save(img, out_file)
170180
iflogger.info('Tensor mode image saved as {i}'.format(i=out_file))

0 commit comments

Comments
 (0)