Skip to content

Commit e59aaa6

Browse files
committed
fix: address edge cases of the copyheader mixin
- Disable ``copy_header`` for the ``PadImage`` operation in ``ImageMath`` - Drop the CopyHeader mixin and update auto test of ResampleImageBySpacing
1 parent 3587533 commit e59aaa6

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

nipype/interfaces/ants/tests/test_auto_ResampleImageBySpacing.py

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ def test_ResampleImageBySpacing_inputs():
77
addvox=dict(argstr="%d", position=6, requires=["apply_smoothing"],),
88
apply_smoothing=dict(argstr="%d", position=5,),
99
args=dict(argstr="%s",),
10-
copy_header=dict(mandatory=True, usedefault=True,),
1110
dimension=dict(argstr="%d", position=1, usedefault=True,),
1211
environ=dict(nohash=True, usedefault=True,),
1312
input_image=dict(argstr="%s", extensions=None, mandatory=True, position=2,),

nipype/interfaces/ants/utils.py

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""ANTs' utilities."""
22
import os
3+
from warnings import warn
34
from ..base import traits, isdefined, TraitedSpec, File, Str, InputMultiObject
45
from ..mixins import CopyHeaderInterface
56
from .base import ANTSCommandInputSpec, ANTSCommand
@@ -103,13 +104,46 @@ class ImageMath(ANTSCommand, CopyHeaderInterface):
103104
... op2='0.005 0.999 256').cmdline
104105
'ImageMath 3 structural_maths.nii TruncateImageIntensity structural.nii 0.005 0.999 256'
105106
107+
>>> pad = ImageMath(
108+
... op1='structural.nii',
109+
... operation='PadImage',
110+
... op2='0.005 0.999 256')
111+
>>> pad.inputs.copy_header
112+
False
113+
114+
>>> pad.inputs.copy_header = True
115+
>>> pad.inputs.copy_header
116+
False
117+
118+
>>> pad.inputs.operation = "ME"
119+
>>> pad.inputs.copy_header = True
120+
>>> pad.inputs.copy_header
121+
True
122+
106123
"""
107124

108125
_cmd = "ImageMath"
109126
input_spec = ImageMathInputSpec
110127
output_spec = ImageMathOuputSpec
111128
_copy_header_map = {"output_image": "op1"}
112129

130+
def __init__(self, **inputs):
131+
super(ImageMath, self).__init__(**inputs)
132+
if self.inputs.operation in ("PadImage", ):
133+
self.inputs.copy_header = False
134+
135+
self.inputs.on_trait_change(self._operation_update, "operation")
136+
self.inputs.on_trait_change(self._copyheader_update, "copy_header")
137+
138+
def _operation_update(self):
139+
if self.inputs.operation in ("PadImage", ):
140+
self.inputs.copy_header = False
141+
142+
def _copyheader_update(self):
143+
if self.inputs.copy_header and self.inputs.operation in ("PadImage", ):
144+
warn("copy_header cannot be updated to True with PadImage as operation.")
145+
self.inputs.copy_header = False
146+
113147

114148
class ResampleImageBySpacingInputSpec(ANTSCommandInputSpec):
115149
dimension = traits.Int(
@@ -147,19 +181,13 @@ class ResampleImageBySpacingInputSpec(ANTSCommandInputSpec):
147181
nn_interp = traits.Bool(
148182
argstr="%d", desc="nn interpolation", position=-1, requires=["addvox"]
149183
)
150-
copy_header = traits.Bool(
151-
True,
152-
mandatory=True,
153-
usedefault=True,
154-
desc="copy headers of the original image into the output (corrected) file",
155-
)
156184

157185

158186
class ResampleImageBySpacingOutputSpec(TraitedSpec):
159187
output_image = File(exists=True, desc="resampled file")
160188

161189

162-
class ResampleImageBySpacing(ANTSCommand, CopyHeaderInterface):
190+
class ResampleImageBySpacing(ANTSCommand):
163191
"""
164192
Resample an image with a given spacing.
165193
@@ -195,7 +223,6 @@ class ResampleImageBySpacing(ANTSCommand, CopyHeaderInterface):
195223
_cmd = "ResampleImageBySpacing"
196224
input_spec = ResampleImageBySpacingInputSpec
197225
output_spec = ResampleImageBySpacingOutputSpec
198-
_copy_header_map = {"output_image": "input_image"}
199226

200227
def _format_arg(self, name, trait_spec, value):
201228
if name == "out_spacing":

0 commit comments

Comments
 (0)