Skip to content

Commit 3aeefe8

Browse files
committed
Interface to elastix
Still necessary to add interfaces to transformix and the parameters file.
1 parent cba8e41 commit 3aeefe8

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ before_install:
1212
- sudo rm -rf /dev/shm
1313
- sudo ln -s /run/shm /dev/shm
1414
- bash <(wget -q -O- http://neuro.debian.net/_files/neurodebian-travis.sh)
15-
- travis_retry sudo apt-get install -qq --no-install-recommends fsl afni
15+
- travis_retry sudo apt-get install -qq --no-install-recommends fsl afni elastix
1616
- travis_retry sudo apt-get install -qq fsl-atlases
1717
- source /etc/fsl/fsl.sh
1818

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Next Release
22
============
33

4+
* ENH: New elastix interface
45
* ENH: New ANTs interface: ApplyTransformsToPoints
56
* FIX: MRTrix tracking algorithms were ignoring mask parameters.
67

nipype/interfaces/elastix/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
4+
# vi: set ft=python sts=4 ts=4 sw=4 et:
5+
#
6+
# @Author: oesteban - [email protected]
7+
# @Date: 2014-06-02 12:06:07
8+
# @Last Modified by: oesteban
9+
# @Last Modified time: 2014-06-02 12:40:10
10+
"""Top-level namespace for elastix."""
11+
12+
from registration import Registration
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
4+
# vi: set ft=python sts=4 ts=4 sw=4 et:
5+
#
6+
# @Author: oesteban - [email protected]
7+
# @Date: 2014-06-02 12:06:50
8+
# @Last Modified by: oesteban
9+
# @Last Modified time: 2014-06-02 14:09:50
10+
"""The :py:mod:`nipype.interfaces.elastix` provides the interface to
11+
the elastix registration software.
12+
13+
.. note:: http://elastix.isi.uu.nl/
14+
15+
16+
"""
17+
18+
import os.path as op
19+
import re
20+
21+
from ..base import (CommandLine, CommandLineInputSpec, isdefined,
22+
TraitedSpec, File, traits, InputMultiPath)
23+
24+
25+
from ... import logging
26+
logger = logging.getLogger('interface')
27+
28+
29+
class RegistrationInputSpec(CommandLineInputSpec):
30+
fixed_image = File(exists=True, mandatory=True, argstr='-f %s',
31+
desc='fixed image')
32+
moving_image = File(exists=True, mandatory=True, argstr='-m %s',
33+
desc='moving image')
34+
35+
output_path = traits.Directory('./', exists=True, mandatory=True, usedefault=True,
36+
argstr='-out %s', desc='output directory')
37+
38+
parameters = InputMultiPath(File(exists=True), mandatory=True, argstr='-p %s...',
39+
desc='parameter file, elastix handles 1 or more -p')
40+
41+
fixed_mask = File(exists=True, argstr='-fMask %s', desc='mask for fixed image')
42+
moving_mask = File(exists=True, argstr='-mMask %s', desc='mask for moving image')
43+
initial_transform = File(exists=True, argstr='-t0 %s',
44+
desc='parameter file for initial transform')
45+
num_threads = traits.Int(1, argstr='-threads %01d',
46+
desc='set the maximum number of threads of elastix')
47+
48+
49+
class RegistrationOutputSpec(TraitedSpec):
50+
transform = InputMultiPath(File(exists=True), desc='output transform')
51+
warped_file = File(desc='input moving image warped to fixed image')
52+
warped_files = InputMultiPath(File(), desc=('input moving image warped to'
53+
' fixed image at each level'))
54+
warped_files_flags = traits.List(traits.Bool(False),
55+
desc='flag indicating if warped image was generated')
56+
57+
58+
class Registration(CommandLine):
59+
"""Elastix nonlinear registration interface
60+
61+
Example
62+
-------
63+
64+
>>> from nipype.interfaces.elastix import Registration
65+
>>> reg = Registration()
66+
>>> reg.inputs.fixed_image = 'fixed1.nii'
67+
>>> reg.inputs.moving_image = 'moving1.nii'
68+
>>> reg.inputs.parameters = ['elastix.txt']
69+
>>> reg.cmdline
70+
'elastix -f fixed1.nii -m moving1.nii -p elastix.txt -out ./'
71+
"""
72+
73+
_cmd = 'elastix'
74+
input_spec = RegistrationInputSpec
75+
output_spec = RegistrationOutputSpec
76+
77+
def _list_outputs(self):
78+
outputs = self._outputs().get()
79+
80+
out_dir = op.abspath(self.inputs.output_path)
81+
82+
opts = [ 'WriteResultImage', 'ResultImageFormat' ]
83+
regex = re.compile(r'^\((\w+)\s(.+)\)$')
84+
85+
outputs['transform'] = []
86+
outputs['warped_files'] = []
87+
outputs['warped_files_flags'] = []
88+
89+
for i,params in enumerate(self.inputs.parameters):
90+
config = {}
91+
92+
with open(params, 'r') as f:
93+
for line in f.readlines():
94+
line = line.strip()
95+
if not line.startswith('//') and line:
96+
m = regex.search(line)
97+
if m:
98+
value = self._cast(m.group(2).strip())
99+
config[m.group(1).strip()] = value
100+
101+
outputs['transform'].append(op.join(out_dir,
102+
'TransformParameters.%01d.txt' % i ))
103+
104+
warped_file = None
105+
if config['WriteResultImage']:
106+
warped_file = op.join(out_dir,
107+
'result.%01d.%s' %(i,config['ResultImageFormat']))
108+
109+
outputs['warped_files'].append(warped_file)
110+
outputs['warped_files_flags'].append(config['WriteResultImage'])
111+
112+
if outputs['warped_files_flags'][-1]:
113+
outputs['warped_file'] = outputs['warped_files'][-1]
114+
115+
return outputs
116+
117+
118+
def _cast(self,val):
119+
if val.startswith('"') and val.endswith('"'):
120+
if val == '"true"':
121+
return True
122+
elif val == '"false"':
123+
return False
124+
else:
125+
return val[1:-1]
126+
127+
try:
128+
return int(val)
129+
except ValueError:
130+
try:
131+
return float(val)
132+
except ValueError:
133+
return val
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from nipype.testing import assert_equal
3+
from nipype.interfaces.elastix.registration import Registration
4+
5+
def test_Registration_inputs():
6+
input_map = dict(args=dict(argstr='%s',
7+
),
8+
environ=dict(nohash=True,
9+
usedefault=True,
10+
),
11+
fixed_image=dict(argstr='-f %s',
12+
mandatory=True,
13+
),
14+
fixed_mask=dict(argstr='-fMask %s',
15+
),
16+
ignore_exception=dict(nohash=True,
17+
usedefault=True,
18+
),
19+
initial_transform=dict(argstr='-t0 %s',
20+
),
21+
moving_image=dict(argstr='-m %s',
22+
mandatory=True,
23+
),
24+
moving_mask=dict(argstr='-mMask %s',
25+
),
26+
num_threads=dict(argstr='-threads %01d',
27+
),
28+
output_path=dict(argstr='-out %s',
29+
mandatory=True,
30+
usedefault=True,
31+
),
32+
parameters=dict(argstr='-p %s...',
33+
mandatory=True,
34+
),
35+
terminal_output=dict(mandatory=True,
36+
nohash=True,
37+
),
38+
)
39+
inputs = Registration.input_spec()
40+
41+
for key, metadata in input_map.items():
42+
for metakey, value in metadata.items():
43+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
44+
45+
def test_Registration_outputs():
46+
output_map = dict(transform=dict(),
47+
warped_file=dict(),
48+
warped_files=dict(),
49+
warped_files_flags=dict(),
50+
)
51+
outputs = Registration.output_spec()
52+
53+
for key, metadata in output_map.items():
54+
for metakey, value in metadata.items():
55+
yield assert_equal, getattr(outputs.traits()[key], metakey), value
56+

nipype/testing/data/elastix.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)