|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from nipype.interfaces.base import (TraitedSpec, BaseInterface, BaseInterfaceInputSpec, |
| 3 | + File, isdefined, traits) |
| 4 | +from nipype.utils.filemanip import split_filename |
| 5 | +import os.path as op |
| 6 | +import nibabel as nb |
| 7 | +import numpy as np |
| 8 | +from nipype.utils.misc import package_check |
| 9 | +import warnings |
| 10 | + |
| 11 | +from ... import logging |
| 12 | +iflogger = logging.getLogger('interface') |
| 13 | + |
| 14 | +try: |
| 15 | + package_check('dipy') |
| 16 | + import dipy.reconst.dti as dti |
| 17 | +except Exception, e: |
| 18 | + warnings.warn('dipy not installed') |
| 19 | + |
| 20 | + |
| 21 | +class TensorModeInputSpec(TraitedSpec): |
| 22 | + in_file = File(exists=True, mandatory=True, |
| 23 | + desc='The input diffusion-weighted image file') |
| 24 | + bvecs = File(exists=True, mandatory=True, |
| 25 | + desc='The input b-vector file') |
| 26 | + bvals = File(exists=True, mandatory=True, |
| 27 | + desc='The input b-value track file') |
| 28 | + out_filename = File('mode.nii', usedefault=True, desc='The output filename for the tracks in TrackVis (.trk) format') |
| 29 | + |
| 30 | +class TensorModeOutputSpec(TraitedSpec): |
| 31 | + out_file = File(exists=True) |
| 32 | + |
| 33 | +class TensorMode(BaseInterface): |
| 34 | + """ |
| 35 | + Creates a tract density image from a TrackVis track file using functions from dipy |
| 36 | +
|
| 37 | + Example |
| 38 | + ------- |
| 39 | +
|
| 40 | + >>> import nipype.interfaces.dipy as dipy |
| 41 | + >>> mode = dipy.TensorMode() |
| 42 | + >>> mode.inputs.in_file = 'dwi.nii' |
| 43 | + >>> mode.run() # doctest: +SKIP |
| 44 | + """ |
| 45 | + input_spec = TensorModeInputSpec |
| 46 | + output_spec = TensorModeOutputSpec |
| 47 | + |
| 48 | + def _run_interface(self, runtime): |
| 49 | + img=nb.load(self.inputs.in_file) |
| 50 | + data=img.get_data() |
| 51 | + affine=img.get_affine() |
| 52 | + |
| 53 | + bvals=np.loadtxt(self.inputs.bvals) |
| 54 | + gradients=np.loadtxt(self.inputs.bvecs).T |
| 55 | + |
| 56 | + tensor = dti.Tensor(data,bvals,gradients,thresh=50) |
| 57 | + |
| 58 | + mode_data = tensor.mode |
| 59 | + img = nb.Nifti1Image(mode_data,affine) |
| 60 | + out_file = op.abspath(self.inputs.out_filename) |
| 61 | + nb.save(img, out_file) |
| 62 | + iflogger.info('Tensor mode image saved as {i}'.format(i=out_file)) |
| 63 | + return runtime |
| 64 | + |
| 65 | + def _list_outputs(self): |
| 66 | + outputs = self._outputs().get() |
| 67 | + outputs['out_file'] = op.abspath(self.inputs.out_filename) |
| 68 | + return outputs |
| 69 | + |
0 commit comments