|
| 1 | +"""Handling the gradient table.""" |
| 2 | +from pathlib import Path |
| 3 | +import numpy as np |
| 4 | +from nipype.utils.filemanip import fname_presuffix |
| 5 | +from nipype.interfaces.base import ( |
| 6 | + SimpleInterface, BaseInterfaceInputSpec, TraitedSpec, |
| 7 | + File, traits, isdefined |
| 8 | +) |
| 9 | +from ..utils.vectors import DiffusionGradientTable, B0_THRESHOLD, BVEC_NORM_EPSILON |
| 10 | + |
| 11 | + |
| 12 | +class _CheckGradientTableInputSpec(BaseInterfaceInputSpec): |
| 13 | + dwi_file = File(exists=True, mandatory=True) |
| 14 | + in_bvec = File(exists=True, xor=['in_rasb']) |
| 15 | + in_bval = File(exists=True, xor=['in_rasb']) |
| 16 | + in_rasb = File(exists=True, xor=['in_bval', 'in_bvec']) |
| 17 | + b0_threshold = traits.Float(B0_THRESHOLD, usedefault=True) |
| 18 | + bvec_norm_epsilon = traits.Float(BVEC_NORM_EPSILON, usedefault=True) |
| 19 | + b_scale = traits.Bool(True, usedefault=True) |
| 20 | + |
| 21 | + |
| 22 | +class _CheckGradientTableOutputSpec(TraitedSpec): |
| 23 | + out_rasb = File(exists=True) |
| 24 | + out_bval = File(exists=True) |
| 25 | + out_bvec = File(exists=True) |
| 26 | + full_sphere = traits.Bool() |
| 27 | + pole = traits.Tuple(traits.Float, traits.Float, traits.Float) |
| 28 | + b0_ixs = traits.List(traits.Int) |
| 29 | + |
| 30 | + |
| 31 | +class CheckGradientTable(SimpleInterface): |
| 32 | + """ |
| 33 | + Ensure the correctness of the gradient table. |
| 34 | +
|
| 35 | + Example |
| 36 | + ------- |
| 37 | +
|
| 38 | + >>> os.chdir(tmpdir) |
| 39 | + >>> check = CheckGradientTable( |
| 40 | + ... dwi_file=str(data_dir / 'dwi.nii.gz'), |
| 41 | + ... in_rasb=str(data_dir / 'dwi.tsv')).run() |
| 42 | + >>> check.outputs.pole |
| 43 | + (0.0, 0.0, 0.0) |
| 44 | + >>> check.outputs.full_sphere |
| 45 | + True |
| 46 | +
|
| 47 | + >>> check = CheckGradientTable( |
| 48 | + ... dwi_file=str(data_dir / 'dwi.nii.gz'), |
| 49 | + ... in_bvec=str(data_dir / 'bvec'), |
| 50 | + ... in_bval=str(data_dir / 'bval')).run() |
| 51 | + >>> check.outputs.pole |
| 52 | + (0.0, 0.0, 0.0) |
| 53 | + >>> check.outputs.full_sphere |
| 54 | + True |
| 55 | + >>> newrasb = np.loadtxt(check.outputs.out_rasb, skiprows=1) |
| 56 | + >>> oldrasb = np.loadtxt(str(data_dir / 'dwi.tsv'), skiprows=1) |
| 57 | + >>> np.allclose(newrasb, oldrasb, rtol=1.e-3) |
| 58 | + True |
| 59 | +
|
| 60 | + """ |
| 61 | + |
| 62 | + input_spec = _CheckGradientTableInputSpec |
| 63 | + output_spec = _CheckGradientTableOutputSpec |
| 64 | + |
| 65 | + def _run_interface(self, runtime): |
| 66 | + rasb_file = _undefined(self.inputs, 'in_rasb') |
| 67 | + |
| 68 | + table = DiffusionGradientTable( |
| 69 | + self.inputs.dwi_file, |
| 70 | + bvecs=_undefined(self.inputs, 'in_bvec'), |
| 71 | + bvals=_undefined(self.inputs, 'in_bval'), |
| 72 | + rasb_file=rasb_file, |
| 73 | + b_scale=self.inputs.b_scale, |
| 74 | + bvec_norm_epsilon=self.inputs.bvec_norm_epsilon, |
| 75 | + b0_threshold=self.inputs.b0_threshold, |
| 76 | + ) |
| 77 | + pole = table.pole |
| 78 | + self._results['pole'] = tuple(pole) |
| 79 | + self._results['full_sphere'] = np.all(pole == 0.0) |
| 80 | + self._results['b0_ixs'] = np.where(table.b0mask)[0].tolist() |
| 81 | + |
| 82 | + cwd = Path(runtime.cwd).absolute() |
| 83 | + if rasb_file is None: |
| 84 | + rasb_file = fname_presuffix( |
| 85 | + self.inputs.dwi_file, use_ext=False, suffix='.tsv', |
| 86 | + newpath=str(cwd)) |
| 87 | + table.to_filename(rasb_file) |
| 88 | + self._results['out_rasb'] = rasb_file |
| 89 | + table.to_filename('%s/dwi' % cwd, filetype='fsl') |
| 90 | + self._results['out_bval'] = str(cwd / 'dwi.bval') |
| 91 | + self._results['out_bvec'] = str(cwd / 'dwi.bvec') |
| 92 | + return runtime |
| 93 | + |
| 94 | + |
| 95 | +def _undefined(objekt, name, default=None): |
| 96 | + value = getattr(objekt, name) |
| 97 | + if not isdefined(value): |
| 98 | + return default |
| 99 | + return value |
0 commit comments