-
Notifications
You must be signed in to change notification settings - Fork 520
/
registration.py
286 lines (220 loc) · 8.38 KB
/
registration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- coding: utf-8 -*-
# coding: utf-8
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Interfaces to perform image registrations and to apply the resulting
displacement maps to images and points.
"""
import os.path as op
import re
from ... import logging
from .base import ElastixBaseInputSpec
from ..base import CommandLine, TraitedSpec, File, traits, InputMultiPath
iflogger = logging.getLogger("nipype.interface")
class RegistrationInputSpec(ElastixBaseInputSpec):
fixed_image = File(exists=True, mandatory=True, argstr="-f %s", desc="fixed image")
moving_image = File(
exists=True, mandatory=True, argstr="-m %s", desc="moving image"
)
parameters = InputMultiPath(
File(exists=True),
mandatory=True,
argstr="-p %s...",
desc="parameter file, elastix handles 1 or more -p",
)
fixed_mask = File(exists=True, argstr="-fMask %s", desc="mask for fixed image")
moving_mask = File(exists=True, argstr="-mMask %s", desc="mask for moving image")
initial_transform = File(
exists=True, argstr="-t0 %s", desc="parameter file for initial transform"
)
class RegistrationOutputSpec(TraitedSpec):
transform = InputMultiPath(File(exists=True), desc="output transform")
warped_file = File(desc="input moving image warped to fixed image")
warped_files = InputMultiPath(
File(exists=False),
desc=("input moving image warped to fixed image at each level"),
)
warped_files_flags = traits.List(
traits.Bool(False), desc="flag indicating if warped image was generated"
)
class Registration(CommandLine):
"""
Elastix nonlinear registration interface
Example
-------
>>> from nipype.interfaces.elastix import Registration
>>> reg = Registration()
>>> reg.inputs.fixed_image = 'fixed1.nii'
>>> reg.inputs.moving_image = 'moving1.nii'
>>> reg.inputs.parameters = ['elastix.txt']
>>> reg.cmdline
'elastix -f fixed1.nii -m moving1.nii -threads 1 -out ./ -p elastix.txt'
"""
_cmd = "elastix"
input_spec = RegistrationInputSpec
output_spec = RegistrationOutputSpec
def _list_outputs(self):
outputs = self._outputs().get()
out_dir = op.abspath(self.inputs.output_path)
regex = re.compile(r"^\((\w+)\s(.+)\)$")
outputs["transform"] = []
outputs["warped_files"] = []
outputs["warped_files_flags"] = []
for i, params in enumerate(self.inputs.parameters):
config = {}
with open(params, "r") as f:
for line in f.readlines():
line = line.strip()
if not line.startswith("//") and line:
m = regex.search(line)
if m:
value = self._cast(m.group(2).strip())
config[m.group(1).strip()] = value
outputs["transform"].append(
op.join(out_dir, "TransformParameters.%01d.txt" % i)
)
warped_file = None
if config["WriteResultImage"]:
warped_file = op.join(
out_dir, "result.%01d.%s" % (i, config["ResultImageFormat"])
)
outputs["warped_files"].append(warped_file)
outputs["warped_files_flags"].append(config["WriteResultImage"])
if outputs["warped_files_flags"][-1]:
outputs["warped_file"] = outputs["warped_files"][-1]
return outputs
def _cast(self, val):
if val.startswith('"') and val.endswith('"'):
if val == '"true"':
return True
elif val == '"false"':
return False
else:
return val[1:-1]
try:
return int(val)
except ValueError:
try:
return float(val)
except ValueError:
return val
class ApplyWarpInputSpec(ElastixBaseInputSpec):
transform_file = File(
exists=True,
mandatory=True,
argstr="-tp %s",
desc="transform-parameter file, only 1",
)
moving_image = File(
exists=True, argstr="-in %s", mandatory=True, desc="input image to deform"
)
class ApplyWarpOutputSpec(TraitedSpec):
warped_file = File(desc="input moving image warped to fixed image")
class ApplyWarp(CommandLine):
"""
Use ``transformix`` to apply a transform on an input image.
The transform is specified in the transform-parameter file.
Example
-------
>>> from nipype.interfaces.elastix import ApplyWarp
>>> reg = ApplyWarp()
>>> reg.inputs.moving_image = 'moving1.nii'
>>> reg.inputs.transform_file = 'TransformParameters.0.txt'
>>> reg.cmdline
'transformix -in moving1.nii -threads 1 -out ./ -tp TransformParameters.0.txt'
"""
_cmd = "transformix"
input_spec = ApplyWarpInputSpec
output_spec = ApplyWarpOutputSpec
def _list_outputs(self):
outputs = self._outputs().get()
out_dir = op.abspath(self.inputs.output_path)
outputs["warped_file"] = op.join(out_dir, "result.nii.gz")
return outputs
class AnalyzeWarpInputSpec(ApplyWarpInputSpec):
points = traits.Enum(
"all",
usedefault=True,
position=0,
argstr="-def %s",
desc="transform all points from the input-image, which effectively"
" generates a deformation field.",
)
jac = traits.Enum(
"all",
usedefault=True,
argstr="-jac %s",
desc="generate an image with the determinant of the spatial Jacobian",
)
jacmat = traits.Enum(
"all",
usedefault=True,
argstr="-jacmat %s",
desc="generate an image with the spatial Jacobian matrix at each voxel",
)
moving_image = File(
exists=True, argstr="-in %s", desc="input image to deform (not used)"
)
class AnalyzeWarpOutputSpec(TraitedSpec):
disp_field = File(desc="displacements field")
jacdet_map = File(desc="det(Jacobian) map")
jacmat_map = File(desc="Jacobian matrix map")
class AnalyzeWarp(ApplyWarp):
"""
Use transformix to get details from the input transform (generate
the corresponding deformation field, generate the determinant of the
Jacobian map or the Jacobian map itself)
Example
-------
>>> from nipype.interfaces.elastix import AnalyzeWarp
>>> reg = AnalyzeWarp()
>>> reg.inputs.transform_file = 'TransformParameters.0.txt'
>>> reg.cmdline
'transformix -def all -jac all -jacmat all -threads 1 -out ./ -tp TransformParameters.0.txt'
"""
input_spec = AnalyzeWarpInputSpec
output_spec = AnalyzeWarpOutputSpec
def _list_outputs(self):
outputs = self._outputs().get()
out_dir = op.abspath(self.inputs.output_path)
outputs["disp_field"] = op.join(out_dir, "deformationField.nii.gz")
outputs["jacdet_map"] = op.join(out_dir, "spatialJacobian.nii.gz")
outputs["jacmat_map"] = op.join(out_dir, "fullSpatialJacobian.nii.gz")
return outputs
class PointsWarpInputSpec(ElastixBaseInputSpec):
points_file = File(
exists=True,
argstr="-def %s",
mandatory=True,
desc="input points (accepts .vtk triangular meshes).",
)
transform_file = File(
exists=True,
mandatory=True,
argstr="-tp %s",
desc="transform-parameter file, only 1",
)
class PointsWarpOutputSpec(TraitedSpec):
warped_file = File(desc="input points displaced in fixed image domain")
class PointsWarp(CommandLine):
"""Use ``transformix`` to apply a transform on an input point set.
The transform is specified in the transform-parameter file.
Example
-------
>>> from nipype.interfaces.elastix import PointsWarp
>>> reg = PointsWarp()
>>> reg.inputs.points_file = 'surf1.vtk'
>>> reg.inputs.transform_file = 'TransformParameters.0.txt'
>>> reg.cmdline
'transformix -threads 1 -out ./ -def surf1.vtk -tp TransformParameters.0.txt'
"""
_cmd = "transformix"
input_spec = PointsWarpInputSpec
output_spec = PointsWarpOutputSpec
def _list_outputs(self):
outputs = self._outputs().get()
out_dir = op.abspath(self.inputs.output_path)
fname, ext = op.splitext(op.basename(self.inputs.points_file))
outputs["warped_file"] = op.join(out_dir, "outputpoints%s" % ext)
return outputs