Skip to content

Commit 5992452

Browse files
committed
Merge remote-tracking branch 'oesteban/enh/DipyImproveTDI'
Conflicts: CHANGES
2 parents 6d4d493 + 14179a0 commit 5992452

File tree

3 files changed

+85
-53
lines changed

3 files changed

+85
-53
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Next release
66
* FIX: ants.Registration composite transform outputs are no longer returned as lists (https://github.com/nipy/nipype/pull/1183)
77
* BUG: ANTs Registration interface failed with multi-modal inputs
88
(https://github.com/nipy/nipype/pull/1176) (https://github.com/nipy/nipype/issues/1175)
9+
* ENH: dipy.TrackDensityMap interface now accepts a reference image (https://github.com/nipy/nipype/pull/1091)
910
* FIX: Bug in XFibres5 (https://github.com/nipy/nipype/pull/1168)
1011
* ENH: Attempt to use hard links for data sink.
1112
(https://github.com/nipy/nipype/pull/1161)

nipype/interfaces/dipy/tests/test_auto_TrackDensityMap.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ def test_TrackDensityMap_inputs():
88
),
99
out_filename=dict(usedefault=True,
1010
),
11+
points_space=dict(usedefault=True,
12+
),
13+
reference=dict(),
1114
voxel_dims=dict(),
1215
)
1316
inputs = TrackDensityMap.input_spec()

nipype/interfaces/dipy/tracks.py

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# -*- coding: utf-8 -*-
2-
"""Change directory to provide relative paths for doctests
2+
"""
3+
Change directory to provide relative paths for doctests
34
>>> import os
45
>>> filepath = os.path.dirname( os.path.realpath( __file__ ) )
56
>>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data'))
67
>>> os.chdir(datadir)
78
"""
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)
1012
from nipype.utils.filemanip import split_filename
1113
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
1316
from nipype.utils.misc import package_check
1417
import warnings
1518

@@ -27,60 +30,85 @@
2730

2831
class TrackDensityMapInputSpec(TraitedSpec):
2932
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+
3139
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.')
3341
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+
3647

3748
class TrackDensityMapOutputSpec(TraitedSpec):
3849
out_file = File(exists=True)
3950

51+
4052
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
8653

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

Comments
 (0)