|
1 | 1 | # -*- 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) |
4 | 4 | from nipype.utils.filemanip import split_filename
|
5 | 5 | import os.path as op
|
6 | 6 | import nibabel as nb
|
|
21 | 21 |
|
22 | 22 | class TensorModeInputSpec(TraitedSpec):
|
23 | 23 | in_file = File(exists=True, mandatory=True,
|
24 |
| - desc='The input diffusion-weighted image file') |
| 24 | + desc='The input 4D diffusion-weighted image file') |
25 | 25 | bvecs = File(exists=True, mandatory=True,
|
26 |
| - desc='The input b-vector file') |
| 26 | + desc='The input b-vector text file') |
27 | 27 | 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 | + |
30 | 32 |
|
31 | 33 | class TensorModeOutputSpec(TraitedSpec):
|
32 | 34 | out_file = File(exists=True)
|
33 | 35 |
|
| 36 | + |
34 | 37 | 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 |
75 | 99 |
|
| 100 | + def _gen_outfilename(self): |
| 101 | + _, name, _ = split_filename(self.inputs.in_file) |
| 102 | + return name + '_mode.nii' |
0 commit comments