diff --git a/nipype/interfaces/ants/tests/test_auto_ResampleImageBySpacing.py b/nipype/interfaces/ants/tests/test_auto_ResampleImageBySpacing.py index dce64b8930..a2834bf36c 100644 --- a/nipype/interfaces/ants/tests/test_auto_ResampleImageBySpacing.py +++ b/nipype/interfaces/ants/tests/test_auto_ResampleImageBySpacing.py @@ -7,7 +7,6 @@ def test_ResampleImageBySpacing_inputs(): addvox=dict(argstr="%d", position=6, requires=["apply_smoothing"],), apply_smoothing=dict(argstr="%d", position=5,), args=dict(argstr="%s",), - copy_header=dict(mandatory=True, usedefault=True,), dimension=dict(argstr="%d", position=1, usedefault=True,), environ=dict(nohash=True, usedefault=True,), input_image=dict(argstr="%s", extensions=None, mandatory=True, position=2,), diff --git a/nipype/interfaces/ants/utils.py b/nipype/interfaces/ants/utils.py index a158aa83d4..6ca6af2d98 100644 --- a/nipype/interfaces/ants/utils.py +++ b/nipype/interfaces/ants/utils.py @@ -1,5 +1,6 @@ """ANTs' utilities.""" import os +from warnings import warn from ..base import traits, isdefined, TraitedSpec, File, Str, InputMultiObject from ..mixins import CopyHeaderInterface from .base import ANTSCommandInputSpec, ANTSCommand @@ -47,6 +48,10 @@ class ImageMathInputSpec(ANTSCommandInputSpec): "GO", "GC", "TruncateImageIntensity", + "Laplacian", + "GetLargestComponent", + "FillHoles", + "PadImage", mandatory=True, position=3, argstr="%s", @@ -73,8 +78,8 @@ class ImageMath(ANTSCommand, CopyHeaderInterface): """ Operations over images. - Example - ------- + Examples + -------- >>> ImageMath( ... op1='structural.nii', ... operation='+', @@ -99,6 +104,31 @@ class ImageMath(ANTSCommand, CopyHeaderInterface): ... op2='0.005 0.999 256').cmdline 'ImageMath 3 structural_maths.nii TruncateImageIntensity structural.nii 0.005 0.999 256' + By default, Nipype copies headers from the first input image (``op1``) + to the output image. + For the ``PadImage`` operation, the header cannot be copied from inputs to + outputs, and so ``copy_header`` option is automatically set to ``False``. + + >>> pad = ImageMath( + ... op1='structural.nii', + ... operation='PadImage') + >>> pad.inputs.copy_header + False + + While the operation is set to ``PadImage``, + setting ``copy_header = True`` will have no effect. + + >>> pad.inputs.copy_header = True + >>> pad.inputs.copy_header + False + + For any other operation, ``copy_header`` can be enabled/disabled normally: + + >>> pad.inputs.operation = "ME" + >>> pad.inputs.copy_header = True + >>> pad.inputs.copy_header + True + """ _cmd = "ImageMath" @@ -106,6 +136,23 @@ class ImageMath(ANTSCommand, CopyHeaderInterface): output_spec = ImageMathOuputSpec _copy_header_map = {"output_image": "op1"} + def __init__(self, **inputs): + super(ImageMath, self).__init__(**inputs) + if self.inputs.operation in ("PadImage", ): + self.inputs.copy_header = False + + self.inputs.on_trait_change(self._operation_update, "operation") + self.inputs.on_trait_change(self._copyheader_update, "copy_header") + + def _operation_update(self): + if self.inputs.operation in ("PadImage", ): + self.inputs.copy_header = False + + def _copyheader_update(self): + if self.inputs.copy_header and self.inputs.operation in ("PadImage", ): + warn("copy_header cannot be updated to True with PadImage as operation.") + self.inputs.copy_header = False + class ResampleImageBySpacingInputSpec(ANTSCommandInputSpec): dimension = traits.Int( @@ -143,19 +190,13 @@ class ResampleImageBySpacingInputSpec(ANTSCommandInputSpec): nn_interp = traits.Bool( argstr="%d", desc="nn interpolation", position=-1, requires=["addvox"] ) - copy_header = traits.Bool( - True, - mandatory=True, - usedefault=True, - desc="copy headers of the original image into the output (corrected) file", - ) class ResampleImageBySpacingOutputSpec(TraitedSpec): output_image = File(exists=True, desc="resampled file") -class ResampleImageBySpacing(ANTSCommand, CopyHeaderInterface): +class ResampleImageBySpacing(ANTSCommand): """ Resample an image with a given spacing. @@ -191,7 +232,6 @@ class ResampleImageBySpacing(ANTSCommand, CopyHeaderInterface): _cmd = "ResampleImageBySpacing" input_spec = ResampleImageBySpacingInputSpec output_spec = ResampleImageBySpacingOutputSpec - _copy_header_map = {"output_image": "input_image"} def _format_arg(self, name, trait_spec, value): if name == "out_spacing":