|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -"""Change directory to provide relative paths for doctests |
| 2 | +""" |
| 3 | +Change directory to provide relative paths for doctests |
3 | 4 | >>> import os
|
4 | 5 | >>> filepath = os.path.dirname( os.path.realpath( __file__ ) )
|
5 | 6 | >>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data'))
|
6 | 7 | >>> os.chdir(datadir)
|
7 | 8 | """
|
8 |
| -from nipype.interfaces.base import (TraitedSpec, BaseInterface, BaseInterfaceInputSpec, |
9 |
| - File, isdefined, traits) |
| 9 | +from nipype.interfaces.base import ( |
| 10 | + TraitedSpec, BaseInterface, BaseInterfaceInputSpec, |
| 11 | + File, isdefined, traits) |
10 | 12 | from nipype.utils.filemanip import split_filename
|
11 | 13 | import os.path as op
|
12 |
| -import nibabel as nb, nibabel.trackvis as trk |
| 14 | +import nibabel as nb |
| 15 | +import nibabel.trackvis as nbt |
13 | 16 | from nipype.utils.misc import package_check
|
14 | 17 | import warnings
|
15 | 18 |
|
|
27 | 30 |
|
28 | 31 | class TrackDensityMapInputSpec(TraitedSpec):
|
29 | 32 | in_file = File(exists=True, mandatory=True,
|
30 |
| - desc='The input TrackVis track file') |
| 33 | + desc='The input TrackVis track file') |
| 34 | + reference = File(exists=True, |
| 35 | + desc='A reference file to define RAS coordinates space') |
| 36 | + points_space = traits.Enum('rasmm', 'voxel', None, usedefault=True, |
| 37 | + desc='coordinates of trk file') |
| 38 | + |
31 | 39 | voxel_dims = traits.List(traits.Float, minlen=3, maxlen=3,
|
32 |
| - desc='The size of each voxel in mm.') |
| 40 | + desc='The size of each voxel in mm.') |
33 | 41 | data_dims = traits.List(traits.Int, minlen=3, maxlen=3,
|
34 |
| - desc='The size of the image in voxels.') |
35 |
| - out_filename = File('tdi.nii', usedefault=True, desc='The output filename for the tracks in TrackVis (.trk) format') |
| 42 | + desc='The size of the image in voxels.') |
| 43 | + out_filename = File('tdi.nii', usedefault=True, |
| 44 | + desc=('The output filename for the tracks in TrackVis ' |
| 45 | + '(.trk) format')) |
| 46 | + |
36 | 47 |
|
37 | 48 | class TrackDensityMapOutputSpec(TraitedSpec):
|
38 | 49 | out_file = File(exists=True)
|
39 | 50 |
|
| 51 | + |
40 | 52 | class TrackDensityMap(BaseInterface):
|
41 |
| - """ |
42 |
| - Creates a tract density image from a TrackVis track file using functions from dipy |
43 |
| -
|
44 |
| - Example |
45 |
| - ------- |
46 |
| -
|
47 |
| - >>> import nipype.interfaces.dipy as dipy |
48 |
| - >>> trk2tdi = dipy.TrackDensityMap() |
49 |
| - >>> trk2tdi.inputs.in_file = 'converted.trk' |
50 |
| - >>> trk2tdi.run() # doctest: +SKIP |
51 |
| - """ |
52 |
| - input_spec = TrackDensityMapInputSpec |
53 |
| - output_spec = TrackDensityMapOutputSpec |
54 |
| - |
55 |
| - def _run_interface(self, runtime): |
56 |
| - tracks, header = trk.read(self.inputs.in_file) |
57 |
| - if not isdefined(self.inputs.data_dims): |
58 |
| - data_dims = header['dim'] |
59 |
| - else: |
60 |
| - data_dims = self.inputs.data_dims |
61 |
| - |
62 |
| - if not isdefined(self.inputs.voxel_dims): |
63 |
| - voxel_size = header['voxel_size'] |
64 |
| - else: |
65 |
| - voxel_size = self.inputs.voxel_dims |
66 |
| - |
67 |
| - affine = header['vox_to_ras'] |
68 |
| - |
69 |
| - streams = ((ii[0]) for ii in tracks) |
70 |
| - data = density_map(streams, data_dims, voxel_size) |
71 |
| - if data.max() < 2**15: |
72 |
| - data = data.astype('int16') |
73 |
| - |
74 |
| - img = nb.Nifti1Image(data,affine) |
75 |
| - out_file = op.abspath(self.inputs.out_filename) |
76 |
| - nb.save(img, out_file) |
77 |
| - iflogger.info('Track density map saved as {i}'.format(i=out_file)) |
78 |
| - iflogger.info('Data Dimensions {d}'.format(d=data_dims)) |
79 |
| - iflogger.info('Voxel Dimensions {v}'.format(v=voxel_size)) |
80 |
| - return runtime |
81 |
| - |
82 |
| - def _list_outputs(self): |
83 |
| - outputs = self._outputs().get() |
84 |
| - outputs['out_file'] = op.abspath(self.inputs.out_filename) |
85 |
| - return outputs |
86 | 53 |
|
| 54 | + """ |
| 55 | + Creates a tract density image from a TrackVis track file using functions |
| 56 | + from dipy |
| 57 | +
|
| 58 | +
|
| 59 | + Example |
| 60 | + ------- |
| 61 | +
|
| 62 | + >>> import nipype.interfaces.dipy as dipy |
| 63 | + >>> trk2tdi = dipy.TrackDensityMap() |
| 64 | + >>> trk2tdi.inputs.in_file = 'converted.trk' |
| 65 | + >>> trk2tdi.run() # doctest: +SKIP |
| 66 | +
|
| 67 | + """ |
| 68 | + input_spec = TrackDensityMapInputSpec |
| 69 | + output_spec = TrackDensityMapOutputSpec |
| 70 | + |
| 71 | + def _run_interface(self, runtime): |
| 72 | + tracks, header = nbt.read(self.inputs.in_file) |
| 73 | + streams = ((ii[0]) for ii in tracks) |
| 74 | + |
| 75 | + if isdefined(self.inputs.reference): |
| 76 | + refnii = nb.load(self.inputs.reference) |
| 77 | + affine = refnii.get_affine() |
| 78 | + data_dims = refnii.get_shape()[:3] |
| 79 | + kwargs = dict(affine=affine) |
| 80 | + else: |
| 81 | + iflogger.warn(('voxel_dims and data_dims are deprecated' |
| 82 | + 'as of dipy 0.7.1. Please use reference ' |
| 83 | + 'input instead')) |
| 84 | + |
| 85 | + if not isdefined(self.inputs.data_dims): |
| 86 | + data_dims = header['dim'] |
| 87 | + else: |
| 88 | + data_dims = self.inputs.data_dims |
| 89 | + if not isdefined(self.inputs.voxel_dims): |
| 90 | + voxel_size = header['voxel_size'] |
| 91 | + else: |
| 92 | + voxel_size = self.inputs.voxel_dims |
| 93 | + |
| 94 | + affine = header['vox_to_ras'] |
| 95 | + kwargs = dict(voxel_size=voxel_size) |
| 96 | + |
| 97 | + data = density_map(streams, data_dims, **kwargs) |
| 98 | + data = data.astype(np.min_scalar_type(data.max())) |
| 99 | + img = nb.Nifti1Image(data, affine) |
| 100 | + out_file = op.abspath(self.inputs.out_filename) |
| 101 | + nb.save(img, out_file) |
| 102 | + |
| 103 | + iflogger.info( |
| 104 | + ('Track density map saved as {i}, size={d}, ' |
| 105 | + 'dimensions={v}').format( |
| 106 | + i=out_file, |
| 107 | + d=img.get_shape(), |
| 108 | + v=img.get_header().get_zooms())) |
| 109 | + return runtime |
| 110 | + |
| 111 | + def _list_outputs(self): |
| 112 | + outputs = self._outputs().get() |
| 113 | + outputs['out_file'] = op.abspath(self.inputs.out_filename) |
| 114 | + return outputs |
0 commit comments