1
1
"""ANTs' utilities."""
2
2
import os
3
+ from warnings import warn
3
4
from ..base import traits , isdefined , TraitedSpec , File , Str , InputMultiObject
4
5
from ..mixins import CopyHeaderInterface
5
6
from .base import ANTSCommandInputSpec , ANTSCommand
@@ -47,6 +48,10 @@ class ImageMathInputSpec(ANTSCommandInputSpec):
47
48
"GO" ,
48
49
"GC" ,
49
50
"TruncateImageIntensity" ,
51
+ "Laplacian" ,
52
+ "GetLargestComponent" ,
53
+ "FillHoles" ,
54
+ "PadImage" ,
50
55
mandatory = True ,
51
56
position = 3 ,
52
57
argstr = "%s" ,
@@ -73,8 +78,8 @@ class ImageMath(ANTSCommand, CopyHeaderInterface):
73
78
"""
74
79
Operations over images.
75
80
76
- Example
77
- -------
81
+ Examples
82
+ --------
78
83
>>> ImageMath(
79
84
... op1='structural.nii',
80
85
... operation='+',
@@ -99,13 +104,55 @@ class ImageMath(ANTSCommand, CopyHeaderInterface):
99
104
... op2='0.005 0.999 256').cmdline
100
105
'ImageMath 3 structural_maths.nii TruncateImageIntensity structural.nii 0.005 0.999 256'
101
106
107
+ By default, Nipype copies headers from the first input image (``op1``)
108
+ to the output image.
109
+ For the ``PadImage`` operation, the header cannot be copied from inputs to
110
+ outputs, and so ``copy_header`` option is automatically set to ``False``.
111
+
112
+ >>> pad = ImageMath(
113
+ ... op1='structural.nii',
114
+ ... operation='PadImage')
115
+ >>> pad.inputs.copy_header
116
+ False
117
+
118
+ While the operation is set to ``PadImage``,
119
+ setting ``copy_header = True`` will have no effect.
120
+
121
+ >>> pad.inputs.copy_header = True
122
+ >>> pad.inputs.copy_header
123
+ False
124
+
125
+ For any other operation, ``copy_header`` can be enabled/disabled normally:
126
+
127
+ >>> pad.inputs.operation = "ME"
128
+ >>> pad.inputs.copy_header = True
129
+ >>> pad.inputs.copy_header
130
+ True
131
+
102
132
"""
103
133
104
134
_cmd = "ImageMath"
105
135
input_spec = ImageMathInputSpec
106
136
output_spec = ImageMathOuputSpec
107
137
_copy_header_map = {"output_image" : "op1" }
108
138
139
+ def __init__ (self , ** inputs ):
140
+ super (ImageMath , self ).__init__ (** inputs )
141
+ if self .inputs .operation in ("PadImage" , ):
142
+ self .inputs .copy_header = False
143
+
144
+ self .inputs .on_trait_change (self ._operation_update , "operation" )
145
+ self .inputs .on_trait_change (self ._copyheader_update , "copy_header" )
146
+
147
+ def _operation_update (self ):
148
+ if self .inputs .operation in ("PadImage" , ):
149
+ self .inputs .copy_header = False
150
+
151
+ def _copyheader_update (self ):
152
+ if self .inputs .copy_header and self .inputs .operation in ("PadImage" , ):
153
+ warn ("copy_header cannot be updated to True with PadImage as operation." )
154
+ self .inputs .copy_header = False
155
+
109
156
110
157
class ResampleImageBySpacingInputSpec (ANTSCommandInputSpec ):
111
158
dimension = traits .Int (
@@ -143,19 +190,13 @@ class ResampleImageBySpacingInputSpec(ANTSCommandInputSpec):
143
190
nn_interp = traits .Bool (
144
191
argstr = "%d" , desc = "nn interpolation" , position = - 1 , requires = ["addvox" ]
145
192
)
146
- copy_header = traits .Bool (
147
- True ,
148
- mandatory = True ,
149
- usedefault = True ,
150
- desc = "copy headers of the original image into the output (corrected) file" ,
151
- )
152
193
153
194
154
195
class ResampleImageBySpacingOutputSpec (TraitedSpec ):
155
196
output_image = File (exists = True , desc = "resampled file" )
156
197
157
198
158
- class ResampleImageBySpacing (ANTSCommand , CopyHeaderInterface ):
199
+ class ResampleImageBySpacing (ANTSCommand ):
159
200
"""
160
201
Resample an image with a given spacing.
161
202
@@ -191,7 +232,6 @@ class ResampleImageBySpacing(ANTSCommand, CopyHeaderInterface):
191
232
_cmd = "ResampleImageBySpacing"
192
233
input_spec = ResampleImageBySpacingInputSpec
193
234
output_spec = ResampleImageBySpacingOutputSpec
194
- _copy_header_map = {"output_image" : "input_image" }
195
235
196
236
def _format_arg (self , name , trait_spec , value ):
197
237
if name == "out_spacing" :
0 commit comments