|
29 | 29 | from dipy.io.utils import nifti1_symmat
|
30 | 30 |
|
31 | 31 |
|
| 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 | + |
32 | 68 | class DTIInputSpec(TraitedSpec):
|
33 | 69 | in_file = File(exists=True, mandatory=True,
|
34 | 70 | desc='The input 4D diffusion-weighted image file')
|
@@ -65,22 +101,9 @@ class DTI(BaseInterface):
|
65 | 101 | output_spec = DTIOutputSpec
|
66 | 102 |
|
67 | 103 | 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) |
82 | 106 | lower_triangular = tenfit.lower_triangular()
|
83 |
| - lower_triangular *= opts.scale |
84 | 107 | img = nifti1_symmat(lower_triangular, affine)
|
85 | 108 | out_file = op.abspath(self._gen_outfilename())
|
86 | 109 | nb.save(img, out_file)
|
@@ -147,24 +170,11 @@ class TensorMode(BaseInterface):
|
147 | 170 | output_spec = TensorModeOutputSpec
|
148 | 171 |
|
149 | 172 | 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) |
165 | 175 |
|
166 | 176 | ## Write as a 3D Nifti image with the original affine
|
167 |
| - img = nb.Nifti1Image(mode_data, affine) |
| 177 | + img = nb.Nifti1Image(tenfit.mode, affine) |
168 | 178 | out_file = op.abspath(self._gen_outfilename())
|
169 | 179 | nb.save(img, out_file)
|
170 | 180 | iflogger.info('Tensor mode image saved as {i}'.format(i=out_file))
|
|
0 commit comments