Skip to content

Commit b2d3a81

Browse files
committed
Merge pull request #783 from chrisfilo/enh/ants_apply_to_points
Enh/ants apply to points
2 parents cb0dd44 + 9d7f307 commit b2d3a81

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Release 0.9.2 (January 31, 2014)
22
===========
33

4+
* ENH: New ANTs interface: ApplyTransformsToPoints
5+
46
* FIX: DataFinder was broken due to a typo
57
* FIX: Order of DataFinder outputs was not guaranteed, it's human sorted now
68
* ENH: New interfaces: Vnifti2Image, VtoMat

nipype/interfaces/ants/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .registration import ANTS, Registration
88

99
# Resampling Programs
10-
from resampling import ApplyTransforms, WarpImageMultiTransform, WarpTimeSeriesImageMultiTransform
10+
from .resampling import ApplyTransforms, ApplyTransformsToPoints, WarpImageMultiTransform, WarpTimeSeriesImageMultiTransform
1111

1212

1313
# Segmentation Programs

nipype/interfaces/ants/resampling.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,78 @@ def _list_outputs(self):
329329
outputs['output_image'] = os.path.abspath(
330330
self._gen_filename('output_image'))
331331
return outputs
332+
333+
334+
class ApplyTransformsToPointsInputSpec(ANTSCommandInputSpec):
335+
dimension = traits.Enum(2, 3, 4, argstr='--dimensionality %d',
336+
desc=('This option forces the image to be treated '
337+
'as a specified-dimensional image. If not '
338+
'specified, antsWarp tries to infer the '
339+
'dimensionality from the input image.'))
340+
input_file = File(argstr='--input %s', mandatory=True,
341+
desc=("Currently, the only input supported is a csv file with "
342+
"columns including x,y (2D), x,y,z (3D) or x,y,z,t,label (4D) column headers."
343+
"The points should be defined in physical space."
344+
"If in doubt how to convert coordinates from your files to the space"
345+
"required by antsApplyTransformsToPoints try creating/drawing a simple"
346+
"label volume with only one voxel set to 1 and all others set to 0."
347+
"Write down the voxel coordinates. Then use ImageMaths LabelStats to find"
348+
"out what coordinates for this voxel antsApplyTransformsToPoints is"
349+
"expecting."),
350+
exists=True)
351+
output_file = traits.Str(argstr='--output %s',
352+
desc=('Name of the output CSV file'), name_source=['input_file'],
353+
hash_files=False, name_template='%s_transformed.csv')
354+
transforms = traits.List(File(exists=True), argstr='%s', mandatory=True,
355+
desc=('transforms that will be applied to the points'))
356+
invert_transform_flags = traits.List(traits.Bool(),
357+
desc=('list indicating if a transform should be reversed'))
358+
359+
360+
class ApplyTransformsToPointsOutputSpec(TraitedSpec):
361+
output_file = File(exists=True, desc='csv file with transformed coordinates')
362+
363+
364+
class ApplyTransformsToPoints(ANTSCommand):
365+
"""ApplyTransformsToPoints, applied to an CSV file, transforms coordinates
366+
using provided transform (or a set of transforms).
367+
368+
Examples
369+
--------
370+
371+
>>> from nipype.interfaces.ants import ApplyTransforms
372+
>>> at = ApplyTransformsToPoints()
373+
>>> at.inputs.dimension = 3
374+
>>> at.inputs.input_file = 'moving.csv'
375+
>>> at.inputs.transforms = ['trans.mat', 'ants_Warp.nii.gz']
376+
>>> at.inputs.invert_transform_flags = [False, False]
377+
>>> at.cmdline
378+
'antsApplyTransformsToPoints --dimensionality 3 --input moving.csv --output moving_transformed.csv --transform [trans.mat,0] --transform [ants_Warp.nii.gz,0]'
379+
380+
381+
"""
382+
_cmd = 'antsApplyTransformsToPoints'
383+
input_spec = ApplyTransformsToPointsInputSpec
384+
output_spec = ApplyTransformsToPointsOutputSpec
385+
386+
387+
def _getTransformFileNames(self):
388+
retval = []
389+
for ii in range(len(self.inputs.transforms)):
390+
if isdefined(self.inputs.invert_transform_flags):
391+
if len(self.inputs.transforms) == len(self.inputs.invert_transform_flags):
392+
invert_code = 1 if self.inputs.invert_transform_flags[
393+
ii] else 0
394+
retval.append("--transform [%s,%d]" %
395+
(self.inputs.transforms[ii], invert_code))
396+
else:
397+
raise Exception("ERROR: The useInverse list must have the same number of entries as the transformsFileName list.")
398+
else:
399+
retval.append("--transform %s" % self.inputs.transforms[ii])
400+
return " ".join(retval)
401+
402+
def _format_arg(self, opt, spec, val):
403+
if opt == "transforms":
404+
return self._getTransformFileNames()
405+
return super(ApplyTransformsToPoints, self)._format_arg(opt, spec, val)
406+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from nipype.testing import assert_equal
3+
from nipype.interfaces.ants.resampling import ApplyTransformsToPoints
4+
5+
def test_ApplyTransformsToPoints_inputs():
6+
input_map = dict(args=dict(argstr='%s',
7+
),
8+
dimension=dict(argstr='--dimensionality %d',
9+
),
10+
environ=dict(nohash=True,
11+
usedefault=True,
12+
),
13+
ignore_exception=dict(nohash=True,
14+
usedefault=True,
15+
),
16+
input_file=dict(argstr='--input %s',
17+
mandatory=True,
18+
),
19+
invert_transform_flags=dict(),
20+
num_threads=dict(nohash=True,
21+
usedefault=True,
22+
),
23+
output_file=dict(argstr='--output %s',
24+
hash_files=False,
25+
name_source=['input_file'],
26+
name_template='%s_transformed.csv',
27+
),
28+
terminal_output=dict(mandatory=True,
29+
nohash=True,
30+
),
31+
transforms=dict(argstr='%s',
32+
mandatory=True,
33+
),
34+
)
35+
inputs = ApplyTransformsToPoints.input_spec()
36+
37+
for key, metadata in input_map.items():
38+
for metakey, value in metadata.items():
39+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
40+
41+
def test_ApplyTransformsToPoints_outputs():
42+
output_map = dict(output_file=dict(),
43+
)
44+
outputs = ApplyTransformsToPoints.output_spec()
45+
46+
for key, metadata in output_map.items():
47+
for metakey, value in metadata.items():
48+
yield assert_equal, getattr(outputs.traits()[key], metakey), value
49+

nipype/testing/data/moving.csv

Whitespace-only changes.

0 commit comments

Comments
 (0)