Skip to content

FIX: ANTs' utilities revision - bug fixes and add more operations to ants.ImageMath #3236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,),
Expand Down
60 changes: 50 additions & 10 deletions nipype/interfaces/ants/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -47,6 +48,10 @@ class ImageMathInputSpec(ANTSCommandInputSpec):
"GO",
"GC",
"TruncateImageIntensity",
"Laplacian",
"GetLargestComponent",
"FillHoles",
"PadImage",
mandatory=True,
position=3,
argstr="%s",
Expand All @@ -73,8 +78,8 @@ class ImageMath(ANTSCommand, CopyHeaderInterface):
"""
Operations over images.

Example
-------
Examples
--------
>>> ImageMath(
... op1='structural.nii',
... operation='+',
Expand All @@ -99,13 +104,55 @@ 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"
input_spec = ImageMathInputSpec
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(
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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":
Expand Down