Skip to content

Commit 81b9c83

Browse files
committed
RESTORE ants.legacy, removed in 4a84a02
1 parent 4c48a18 commit 81b9c83

File tree

1 file changed

+373
-0
lines changed

1 file changed

+373
-0
lines changed

nipype/interfaces/ants/legacy.py

Lines changed: 373 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,373 @@
1+
# -*- coding: utf-8 -*-
2+
# NOTE: This implementation has been superceeded buy the antsApplyTransform
3+
# implmeentation that more closely follows the strucutre and capabilities
4+
# of the antsApplyTransform program. This implementation is here
5+
# for backwards compatibility.
6+
"""ANTS Apply Transforms interface
7+
"""
8+
9+
from builtins import range
10+
11+
import os
12+
from glob import glob
13+
14+
from .base import ANTSCommand, ANTSCommandInputSpec
15+
from ..base import TraitedSpec, File, traits, isdefined, OutputMultiPath
16+
from ...utils.filemanip import split_filename
17+
18+
19+
class antsIntroductionInputSpec(ANTSCommandInputSpec):
20+
dimension = traits.Enum(
21+
3,
22+
2,
23+
argstr="-d %d",
24+
usedefault=True,
25+
desc="image dimension (2 or 3)",
26+
position=1,
27+
)
28+
reference_image = File(
29+
exists=True,
30+
argstr="-r %s",
31+
desc="template file to warp to",
32+
mandatory=True,
33+
copyfile=True,
34+
)
35+
input_image = File(
36+
exists=True,
37+
argstr="-i %s",
38+
desc="input image to warp to template",
39+
mandatory=True,
40+
copyfile=False,
41+
)
42+
force_proceed = traits.Bool(
43+
argstr="-f 1",
44+
desc=("force script to proceed even if headers " "may be incompatible"),
45+
)
46+
inverse_warp_template_labels = traits.Bool(
47+
argstr="-l",
48+
desc=(
49+
"Applies inverse warp to the template labels "
50+
"to estimate label positions in target space (use "
51+
"for template-based segmentation)"
52+
),
53+
)
54+
max_iterations = traits.List(
55+
traits.Int,
56+
argstr="-m %s",
57+
sep="x",
58+
desc=(
59+
"maximum number of iterations (must be "
60+
"list of integers in the form [J,K,L...]: "
61+
"J = coarsest resolution iterations, K = "
62+
"middle resolution interations, L = fine "
63+
"resolution iterations"
64+
),
65+
)
66+
bias_field_correction = traits.Bool(
67+
argstr="-n 1", desc=("Applies bias field correction to moving " "image")
68+
)
69+
similarity_metric = traits.Enum(
70+
"PR",
71+
"CC",
72+
"MI",
73+
"MSQ",
74+
argstr="-s %s",
75+
desc=(
76+
"Type of similartiy metric used for registration "
77+
"(CC = cross correlation, MI = mutual information, "
78+
"PR = probability mapping, MSQ = mean square difference)"
79+
),
80+
)
81+
transformation_model = traits.Enum(
82+
"GR",
83+
"EL",
84+
"SY",
85+
"S2",
86+
"EX",
87+
"DD",
88+
"RI",
89+
"RA",
90+
argstr="-t %s",
91+
usedefault=True,
92+
desc=(
93+
"Type of transofmration model used for registration "
94+
"(EL = elastic transformation model, SY = SyN with time, "
95+
"arbitrary number of time points, S2 = SyN with time "
96+
"optimized for 2 time points, GR = greedy SyN, EX = "
97+
"exponential, DD = diffeomorphic demons style exponential "
98+
"mapping, RI = purely rigid, RA = affine rigid"
99+
),
100+
)
101+
out_prefix = traits.Str(
102+
"ants_",
103+
argstr="-o %s",
104+
usedefault=True,
105+
desc=("Prefix that is prepended to all output " "files (default = ants_)"),
106+
)
107+
quality_check = traits.Bool(
108+
argstr="-q 1", desc="Perform a quality check of the result"
109+
)
110+
111+
112+
class antsIntroductionOutputSpec(TraitedSpec):
113+
affine_transformation = File(exists=True, desc="affine (prefix_Affine.txt)")
114+
warp_field = File(exists=True, desc="warp field (prefix_Warp.nii)")
115+
inverse_warp_field = File(
116+
exists=True, desc="inverse warp field (prefix_InverseWarp.nii)"
117+
)
118+
input_file = File(exists=True, desc="input image (prefix_repaired.nii)")
119+
output_file = File(exists=True, desc="output image (prefix_deformed.nii)")
120+
121+
122+
class antsIntroduction(ANTSCommand):
123+
"""Uses ANTS to generate matrices to warp data from one space to another.
124+
125+
Examples
126+
--------
127+
128+
>>> from nipype.interfaces.ants.legacy import antsIntroduction
129+
>>> warp = antsIntroduction()
130+
>>> warp.inputs.reference_image = 'Template_6.nii'
131+
>>> warp.inputs.input_image = 'structural.nii'
132+
>>> warp.inputs.max_iterations = [30,90,20]
133+
>>> warp.cmdline
134+
'antsIntroduction.sh -d 3 -i structural.nii -m 30x90x20 -o ants_ -r Template_6.nii -t GR'
135+
136+
"""
137+
138+
_cmd = "antsIntroduction.sh"
139+
input_spec = antsIntroductionInputSpec
140+
output_spec = antsIntroductionOutputSpec
141+
142+
def _list_outputs(self):
143+
outputs = self._outputs().get()
144+
transmodel = self.inputs.transformation_model
145+
146+
# When transform is set as 'RI'/'RA', wrap fields should not be expected
147+
# The default transformation is GR, which outputs the wrap fields
148+
if not isdefined(transmodel) or (
149+
isdefined(transmodel) and transmodel not in ["RI", "RA"]
150+
):
151+
outputs["warp_field"] = os.path.join(
152+
os.getcwd(), self.inputs.out_prefix + "Warp.nii.gz"
153+
)
154+
outputs["inverse_warp_field"] = os.path.join(
155+
os.getcwd(), self.inputs.out_prefix + "InverseWarp.nii.gz"
156+
)
157+
158+
outputs["affine_transformation"] = os.path.join(
159+
os.getcwd(), self.inputs.out_prefix + "Affine.txt"
160+
)
161+
outputs["input_file"] = os.path.join(
162+
os.getcwd(), self.inputs.out_prefix + "repaired.nii.gz"
163+
)
164+
outputs["output_file"] = os.path.join(
165+
os.getcwd(), self.inputs.out_prefix + "deformed.nii.gz"
166+
)
167+
168+
return outputs
169+
170+
171+
# How do we make a pass through so that GenWarpFields is just an alias for antsIntroduction ?
172+
173+
174+
class GenWarpFields(antsIntroduction):
175+
pass
176+
177+
178+
class buildtemplateparallelInputSpec(ANTSCommandInputSpec):
179+
dimension = traits.Enum(
180+
3,
181+
2,
182+
4,
183+
argstr="-d %d",
184+
usedefault=True,
185+
desc="image dimension (2, 3 or 4)",
186+
position=1,
187+
)
188+
out_prefix = traits.Str(
189+
"antsTMPL_",
190+
argstr="-o %s",
191+
usedefault=True,
192+
desc=("Prefix that is prepended to all output " "files (default = antsTMPL_)"),
193+
)
194+
in_files = traits.List(
195+
File(exists=True),
196+
mandatory=True,
197+
desc="list of images to generate template from",
198+
argstr="%s",
199+
position=-1,
200+
)
201+
parallelization = traits.Enum(
202+
0,
203+
1,
204+
2,
205+
argstr="-c %d",
206+
usedefault=True,
207+
desc=(
208+
"control for parallel processing (0 = "
209+
"serial, 1 = use PBS, 2 = use PEXEC, 3 = "
210+
"use Apple XGrid"
211+
),
212+
)
213+
gradient_step_size = traits.Float(
214+
argstr="-g %f",
215+
desc=("smaller magnitude results in " "more cautious steps (default = " ".25)"),
216+
)
217+
iteration_limit = traits.Int(
218+
4, argstr="-i %d", usedefault=True, desc="iterations of template construction"
219+
)
220+
num_cores = traits.Int(
221+
argstr="-j %d",
222+
requires=["parallelization"],
223+
desc=(
224+
"Requires parallelization = 2 (PEXEC). " "Sets number of cpu cores to use"
225+
),
226+
)
227+
max_iterations = traits.List(
228+
traits.Int,
229+
argstr="-m %s",
230+
sep="x",
231+
desc=(
232+
"maximum number of iterations (must be "
233+
"list of integers in the form [J,K,L...]: "
234+
"J = coarsest resolution iterations, K = "
235+
"middle resolution interations, L = fine "
236+
"resolution iterations"
237+
),
238+
)
239+
bias_field_correction = traits.Bool(
240+
argstr="-n 1", desc=("Applies bias field correction to moving " "image")
241+
)
242+
rigid_body_registration = traits.Bool(
243+
argstr="-r 1",
244+
desc=(
245+
"registers inputs before creating template "
246+
"(useful if no initial template available)"
247+
),
248+
)
249+
similarity_metric = traits.Enum(
250+
"PR",
251+
"CC",
252+
"MI",
253+
"MSQ",
254+
argstr="-s %s",
255+
desc=(
256+
"Type of similartiy metric used for registration "
257+
"(CC = cross correlation, MI = mutual information, "
258+
"PR = probability mapping, MSQ = mean square difference)"
259+
),
260+
)
261+
transformation_model = traits.Enum(
262+
"GR",
263+
"EL",
264+
"SY",
265+
"S2",
266+
"EX",
267+
"DD",
268+
argstr="-t %s",
269+
usedefault=True,
270+
desc=(
271+
"Type of transofmration model used for registration "
272+
"(EL = elastic transformation model, SY = SyN with time, "
273+
"arbitrary number of time points, S2 = SyN with time "
274+
"optimized for 2 time points, GR = greedy SyN, EX = "
275+
"exponential, DD = diffeomorphic demons style exponential "
276+
"mapping"
277+
),
278+
)
279+
use_first_as_target = traits.Bool(
280+
desc=(
281+
"uses first volume as target of "
282+
"all inputs. When not used, an "
283+
"unbiased average image is used "
284+
"to start."
285+
)
286+
)
287+
288+
289+
class buildtemplateparallelOutputSpec(TraitedSpec):
290+
final_template_file = File(exists=True, desc="final ANTS template")
291+
template_files = OutputMultiPath(
292+
File(exists=True), desc="Templates from different stages of iteration"
293+
)
294+
subject_outfiles = OutputMultiPath(
295+
File(exists=True),
296+
desc=(
297+
"Outputs for each input image. Includes warp "
298+
"field, inverse warp, Affine, original image "
299+
"(repaired) and warped image (deformed)"
300+
),
301+
)
302+
303+
304+
class buildtemplateparallel(ANTSCommand):
305+
"""Generate a optimal average template
306+
307+
.. warning::
308+
309+
This can take a VERY long time to complete
310+
311+
Examples
312+
--------
313+
314+
>>> from nipype.interfaces.ants.legacy import buildtemplateparallel
315+
>>> tmpl = buildtemplateparallel()
316+
>>> tmpl.inputs.in_files = ['T1.nii', 'structural.nii']
317+
>>> tmpl.inputs.max_iterations = [30, 90, 20]
318+
>>> tmpl.cmdline
319+
'buildtemplateparallel.sh -d 3 -i 4 -m 30x90x20 -o antsTMPL_ -c 0 -t GR T1.nii structural.nii'
320+
321+
"""
322+
323+
_cmd = "buildtemplateparallel.sh"
324+
input_spec = buildtemplateparallelInputSpec
325+
output_spec = buildtemplateparallelOutputSpec
326+
327+
def _format_arg(self, opt, spec, val):
328+
if opt == "num_cores":
329+
if self.inputs.parallelization == 2:
330+
return "-j " + str(val)
331+
else:
332+
return ""
333+
if opt == "in_files":
334+
if self.inputs.use_first_as_target:
335+
start = "-z "
336+
else:
337+
start = ""
338+
return start + " ".join(name for name in val)
339+
return super(buildtemplateparallel, self)._format_arg(opt, spec, val)
340+
341+
def _list_outputs(self):
342+
outputs = self._outputs().get()
343+
outputs["template_files"] = []
344+
for i in range(len(glob(os.path.realpath("*iteration*")))):
345+
temp = os.path.realpath(
346+
"%s_iteration_%d/%stemplate.nii.gz"
347+
% (self.inputs.transformation_model, i, self.inputs.out_prefix)
348+
)
349+
os.rename(
350+
temp,
351+
os.path.realpath(
352+
"%s_iteration_%d/%stemplate_i%d.nii.gz"
353+
% (self.inputs.transformation_model, i, self.inputs.out_prefix, i)
354+
),
355+
)
356+
file_ = "%s_iteration_%d/%stemplate_i%d.nii.gz" % (
357+
self.inputs.transformation_model,
358+
i,
359+
self.inputs.out_prefix,
360+
i,
361+
)
362+
363+
outputs["template_files"].append(os.path.realpath(file_))
364+
outputs["final_template_file"] = os.path.realpath(
365+
"%stemplate.nii.gz" % self.inputs.out_prefix
366+
)
367+
outputs["subject_outfiles"] = []
368+
for filename in self.inputs.in_files:
369+
_, base, _ = split_filename(filename)
370+
temp = glob(os.path.realpath("%s%s*" % (self.inputs.out_prefix, base)))
371+
for file_ in temp:
372+
outputs["subject_outfiles"].append(file_)
373+
return outputs

0 commit comments

Comments
 (0)