|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Change directory to provide relative paths for doctests |
| 3 | + >>> import os |
| 4 | + >>> filepath = os.path.dirname( os.path.realpath( __file__ ) ) |
| 5 | + >>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data')) |
| 6 | + >>> os.chdir(datadir) |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import print_function, division, unicode_literals, absolute_import |
| 10 | + |
| 11 | +import nibabel as nb |
| 12 | + |
| 13 | +from ... import logging |
| 14 | +from ..base import TraitedSpec, File, isdefined |
| 15 | +from .base import DipyDiffusionInterface, DipyBaseInterfaceInputSpec |
| 16 | + |
| 17 | +IFLOGGER = logging.getLogger('interface') |
| 18 | + |
| 19 | +class APMQballInputSpec(DipyBaseInterfaceInputSpec): |
| 20 | + mask_file = File(exists=True, |
| 21 | + desc='An optional brain mask') |
| 22 | + |
| 23 | + |
| 24 | +class APMQballOutputSpec(TraitedSpec): |
| 25 | + out_file = File(exists=True) |
| 26 | + |
| 27 | +class APMQball(DipyDiffusionInterface): |
| 28 | + """ |
| 29 | + Calculates the anisotropic power map |
| 30 | +
|
| 31 | + Example |
| 32 | + ------- |
| 33 | +
|
| 34 | + >>> import nipype.interfaces.dipy as dipy |
| 35 | + >>> apm = dipy.APMQball() |
| 36 | + >>> apm.inputs.in_file = 'diffusion.nii' |
| 37 | + >>> apm.inputs.in_bvec = 'bvecs' |
| 38 | + >>> apm.inputs.in_bval = 'bvals' |
| 39 | + >>> apm.run() # doctest: +SKIP |
| 40 | + """ |
| 41 | + input_spec = APMQballInputSpec |
| 42 | + output_spec = APMQballOutputSpec |
| 43 | + |
| 44 | + def _run_interface(self, runtime): |
| 45 | + from dipy.reconst import shm |
| 46 | + from dipy.io.utils import nifti1_symmat |
| 47 | + from dipy.data import get_sphere |
| 48 | + from dipy.reconst.peaks import peaks_from_model |
| 49 | + |
| 50 | + gtab = self._get_gradient_table() |
| 51 | + |
| 52 | + img = nb.load(self.inputs.in_file) |
| 53 | + data = img.get_data() |
| 54 | + affine = img.affine |
| 55 | + mask = None |
| 56 | + if isdefined(self.inputs.mask_file): |
| 57 | + mask = nb.load(self.inputs.mask_file).get_data() |
| 58 | + |
| 59 | + # Fit it |
| 60 | + model = shm.QballModel(gtab,8) |
| 61 | + sphere = get_sphere('symmetric724') |
| 62 | + fit = model.fit(data, mask) |
| 63 | + peaks = peaks_from_model(model=model, data=data, relative_peak_threshold=.5, min_separation_angle=25, sphere=sphere, mask=mask) |
| 64 | + apm = shm.anisotropic_power(peaks.shm_coeff) |
| 65 | + out_file = self._gen_filename('apm') |
| 66 | + nb.Nifti1Image(apm.astype("float32"), affine).to_filename(out_file) |
| 67 | + IFLOGGER.info('APM qball image saved as {i}'.format(i=out_file)) |
| 68 | + |
| 69 | + |
| 70 | + return runtime |
| 71 | + |
| 72 | + def _list_outputs(self): |
| 73 | + outputs = self._outputs().get() |
| 74 | + outputs['out_file'] = self._gen_filename('apm') |
| 75 | + |
| 76 | + return outputs |
0 commit comments