|
1 | 1 | """ANTS interfaces.""" |
2 | 2 |
|
3 | 3 | import logging |
4 | | -import os |
5 | 4 |
|
6 | | -from nipype.interfaces.ants.base import ANTSCommand, ANTSCommandInputSpec |
7 | | -from nipype.interfaces.base import ( |
8 | | - CommandLine, |
9 | | - CommandLineInputSpec, |
10 | | - File, |
11 | | - InputMultiPath, |
12 | | - Str, |
13 | | - TraitedSpec, |
14 | | - isdefined, |
15 | | - traits, |
16 | | -) |
| 5 | +from nipype.interfaces.base import isdefined, traits |
17 | 6 | from niworkflows.interfaces.fixes import ( |
18 | 7 | FixHeaderApplyTransforms, |
19 | 8 | _FixTraitApplyTransformsInputSpec, |
|
24 | 13 | LOGGER = logging.getLogger('nipype.interface') |
25 | 14 |
|
26 | 15 |
|
27 | | -class _ConvertTransformFileInputSpec(CommandLineInputSpec): |
28 | | - dimension = traits.Enum(3, 2, usedefault=True, argstr='%d', position=0) |
29 | | - in_transform = traits.File(exists=True, argstr='%s', mandatory=True, position=1) |
30 | | - out_transform = traits.File( |
31 | | - argstr='%s', |
32 | | - name_source='in_transform', |
33 | | - name_template='%s.txt', |
34 | | - keep_extension=False, |
35 | | - position=2, |
36 | | - exists=False, |
37 | | - ) |
38 | | - |
39 | | - |
40 | | -class _ConvertTransformFileOutputSpec(TraitedSpec): |
41 | | - out_transform = traits.File(exists=True) |
42 | | - |
43 | | - |
44 | | -class ConvertTransformFile(CommandLine): |
45 | | - """Wrapper for the ANTS ConvertTransformFile command. |
46 | | -
|
47 | | - Utility to read in a transform file (presumed to be in binary format) and output it in various |
48 | | - formats. |
49 | | - Default output is legacy human-readable text format. |
50 | | - Without any options, the output filename extension must be .txt or .tfm to signify a |
51 | | - text-formatted transform file. |
52 | | - """ |
53 | | - |
54 | | - _cmd = 'ConvertTransformFile' |
55 | | - input_spec = _ConvertTransformFileInputSpec |
56 | | - output_spec = _ConvertTransformFileOutputSpec |
57 | | - |
58 | | - |
59 | | -class _CompositeTransformUtilInputSpec(ANTSCommandInputSpec): |
60 | | - """Input specification for CompositeTransformUtil.""" |
61 | | - |
62 | | - process = traits.Enum( |
63 | | - 'assemble', |
64 | | - 'disassemble', |
65 | | - argstr='--%s', |
66 | | - position=1, |
67 | | - usedefault=True, |
68 | | - desc='What to do with the transform inputs (assemble or disassemble)', |
69 | | - ) |
70 | | - inverse = traits.Bool( |
71 | | - False, |
72 | | - usedefault=True, |
73 | | - desc='Whether to invert the order of the transform components. Not used by the command.', |
74 | | - ) |
75 | | - out_file = File( |
76 | | - exists=False, |
77 | | - argstr='%s', |
78 | | - position=2, |
79 | | - desc='Output file path (only used for disassembly).', |
80 | | - ) |
81 | | - in_file = InputMultiPath( |
82 | | - File(exists=True), |
83 | | - mandatory=True, |
84 | | - argstr='%s...', |
85 | | - position=3, |
86 | | - desc='Input transform file(s)', |
87 | | - ) |
88 | | - output_prefix = Str( |
89 | | - 'transform', |
90 | | - usedefault=True, |
91 | | - argstr='%s', |
92 | | - position=4, |
93 | | - desc='A prefix that is prepended to all output files (only used for assembly).', |
94 | | - ) |
95 | | - |
96 | | - |
97 | | -class _CompositeTransformUtilOutputSpec(TraitedSpec): |
98 | | - """Output specification for CompositeTransformUtil.""" |
99 | | - |
100 | | - affine_transform = File(desc='Affine transform component') |
101 | | - displacement_field = File(desc='Displacement field component') |
102 | | - out_file = File(desc='Compound transformation file') |
103 | | - |
104 | | - |
105 | | -class CompositeTransformUtil(ANTSCommand): |
106 | | - """Wrapper for the ANTS CompositeTransformUtil command. |
107 | | -
|
108 | | - ANTs utility which can combine or break apart transform files into their individual |
109 | | - constituent components. |
110 | | -
|
111 | | - Examples |
112 | | - -------- |
113 | | - >>> from nipype.interfaces.ants import CompositeTransformUtil |
114 | | - >>> tran = CompositeTransformUtil() |
115 | | - >>> tran.inputs.process = 'disassemble' |
116 | | - >>> tran.inputs.in_file = 'output_Composite.h5' |
117 | | - >>> tran.cmdline |
118 | | - 'CompositeTransformUtil --disassemble output_Composite.h5 transform' |
119 | | - >>> tran.run() # doctest: +SKIP |
120 | | - example for assembling transformation files |
121 | | - >>> from nipype.interfaces.ants import CompositeTransformUtil |
122 | | - >>> tran = CompositeTransformUtil(inverse=False) |
123 | | - >>> tran.inputs.process = 'assemble' |
124 | | - >>> tran.inputs.out_file = 'my.h5' |
125 | | - >>> tran.inputs.in_file = ['AffineTransform.mat', 'DisplacementFieldTransform.nii.gz'] |
126 | | - >>> tran.cmdline |
127 | | - 'CompositeTransformUtil --assemble my.h5 AffineTransform.mat |
128 | | - DisplacementFieldTransform.nii.gz ' |
129 | | - >>> tran.run() # doctest: +SKIP |
130 | | - """ |
131 | | - |
132 | | - _cmd = 'CompositeTransformUtil' |
133 | | - input_spec = _CompositeTransformUtilInputSpec |
134 | | - output_spec = _CompositeTransformUtilOutputSpec |
135 | | - |
136 | | - def _num_threads_update(self): |
137 | | - """Do not update the number of threads environment variable. |
138 | | -
|
139 | | - CompositeTransformUtil ignores environment variables, |
140 | | - so override environment update from ANTSCommand class. |
141 | | - """ |
142 | | - pass |
143 | | - |
144 | | - def _format_arg(self, name, spec, value): |
145 | | - if name == 'output_prefix' and self.inputs.process == 'assemble': |
146 | | - return '' |
147 | | - if name == 'out_file' and self.inputs.process == 'disassemble': |
148 | | - return '' |
149 | | - return super()._format_arg(name, spec, value) |
150 | | - |
151 | | - def _list_outputs(self): |
152 | | - outputs = self.output_spec().get() |
153 | | - if self.inputs.inverse: |
154 | | - if self.inputs.process == 'disassemble': |
155 | | - outputs['affine_transform'] = os.path.abspath( |
156 | | - f'{self.inputs.output_prefix}_01_AffineTransform.mat' |
157 | | - ) |
158 | | - outputs['displacement_field'] = os.path.abspath( |
159 | | - f'{self.inputs.output_prefix}_00_DisplacementFieldTransform.nii.gz' |
160 | | - ) |
161 | | - elif self.inputs.process == 'assemble': |
162 | | - outputs['out_file'] = os.path.abspath(self.inputs.out_file) |
163 | | - else: |
164 | | - if self.inputs.process == 'disassemble': |
165 | | - outputs['affine_transform'] = os.path.abspath( |
166 | | - f'{self.inputs.output_prefix}_00_AffineTransform.mat' |
167 | | - ) |
168 | | - outputs['displacement_field'] = os.path.abspath( |
169 | | - f'{self.inputs.output_prefix}_01_DisplacementFieldTransform.nii.gz' |
170 | | - ) |
171 | | - elif self.inputs.process == 'assemble': |
172 | | - outputs['out_file'] = os.path.abspath(self.inputs.out_file) |
173 | | - |
174 | | - return outputs |
175 | | - |
176 | | - |
177 | 16 | class _ApplyTransformsInputSpec(_FixTraitApplyTransformsInputSpec): |
178 | 17 | # Nipype's version doesn't have GenericLabel |
179 | 18 | interpolation = traits.Enum( |
|
0 commit comments