Skip to content

ENH: Add mrrtrix3.MRResize interface #3031

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 7 commits into from
Sep 16, 2019
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
3 changes: 2 additions & 1 deletion nipype/interfaces/mrtrix3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# -*- coding: utf-8 -*-

from .utils import (Mesh2PVE, Generate5tt, BrainMask, TensorMetrics,
ComputeTDI, TCK2VTK, MRMath, MRConvert, DWIExtract)
ComputeTDI, TCK2VTK, MRMath, MRConvert, MRResize,
DWIExtract)
from .preprocess import (ResponseSD, ACTPrepareFSL, ReplaceFSwithFIRST,
DWIDenoise, MRDeGibbs, DWIBiasCorrect)
from .tracking import Tractography
Expand Down
77 changes: 77 additions & 0 deletions nipype/interfaces/mrtrix3/tests/test_auto_MRResize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
from __future__ import unicode_literals
from ..utils import MRResize


def test_MRResize_inputs():
input_map = dict(
args=dict(argstr='%s', ),
bval_scale=dict(argstr='-bvalue_scaling %s', ),
environ=dict(
nohash=True,
usedefault=True,
),
grad_file=dict(
argstr='-grad %s',
extensions=None,
xor=['grad_fsl'],
),
grad_fsl=dict(
argstr='-fslgrad %s %s',
xor=['grad_file'],
),
image_size=dict(
argstr='-size %d,%d,%d',
mandatory=True,
xor=['voxel_size', 'scale_factor'],
),
in_bval=dict(extensions=None, ),
in_bvec=dict(
argstr='-fslgrad %s %s',
extensions=None,
),
in_file=dict(
argstr='%s',
extensions=None,
mandatory=True,
position=-2,
),
interpolation=dict(
argstr='-interp %s',
usedefault=True,
),
nthreads=dict(
argstr='-nthreads %d',
nohash=True,
),
out_file=dict(
argstr='%s',
extensions=None,
keep_extension=True,
name_source=['in_file'],
name_template='%s_resized',
position=-1,
),
scale_factor=dict(
argstr='-scale %g,%g,%g',
mandatory=True,
xor=['image_size', 'voxel_size'],
),
voxel_size=dict(
argstr='-voxel %g,%g,%g',
mandatory=True,
xor=['image_size', 'scale_factor'],
),
)
inputs = MRResize.input_spec()

for key, metadata in list(input_map.items()):
for metakey, value in list(metadata.items()):
assert getattr(inputs.traits()[key], metakey) == value
def test_MRResize_outputs():
output_map = dict(out_file=dict(extensions=None, ), )
outputs = MRResize.output_spec()

for key, metadata in list(output_map.items()):
for metakey, value in list(metadata.items()):
assert getattr(outputs.traits()[key], metakey) == value
96 changes: 96 additions & 0 deletions nipype/interfaces/mrtrix3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,99 @@ def _list_outputs(self):
outputs = self.output_spec().get()
outputs['out_file'] = op.abspath(self.inputs.out_file)
return outputs


class MRResizeInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True,
argstr='%s',
position=-2,
mandatory=True,
desc='input DWI image'
)
image_size = traits.Tuple(
(traits.Int, traits.Int, traits.Int),
argstr='-size %d,%d,%d',
mandatory=True,
desc='Number of voxels in each dimension of output image',
xor=['voxel_size', 'scale_factor'],
)
voxel_size = traits.Tuple(
(traits.Float, traits.Float, traits.Float),
argstr='-voxel %g,%g,%g',
mandatory=True,
desc='Desired voxel size in mm for the output image',
xor=['image_size', 'scale_factor'],
)
scale_factor = traits.Tuple(
(traits.Float, traits.Float, traits.Float),
argstr='-scale %g,%g,%g',
mandatory=True,
desc='Scale factors to rescale the image by in each dimension',
xor=['image_size', 'voxel_size'],
)
interpolation = traits.Enum(
'cubic',
'nearest',
'linear',
'sinc',
argstr='-interp %s',
usedefault=True,
desc='set the interpolation method to use when resizing (choices: '
'nearest, linear, cubic, sinc. Default: cubic).',
)
out_file = File(
argstr='%s',
name_template='%s_resized',
name_source=['in_file'],
keep_extension=True,
position=-1,
desc='the output resized DWI image',
)


class MRResizeOutputSpec(TraitedSpec):
out_file = File(desc='the output resized DWI image', exists=True)


class MRResize(MRTrix3Base):
"""
Resize an image by defining the new image resolution, voxel size or a
scale factor. If the image is 4D, then only the first 3 dimensions can be
resized. Also, if the image is down-sampled, the appropriate smoothing is
automatically applied using Gaussian smoothing.
For more information, see
<https://mrtrix.readthedocs.io/en/latest/reference/commands/mrresize.html>

Example
-------
>>> import nipype.interfaces.mrtrix3 as mrt

Defining the new image resolution:
>>> image_resize = mrt.MRResize()
>>> image_resize.inputs.in_file = 'dwi.mif'
>>> image_resize.inputs.image_size = (256, 256, 144)
>>> image_resize.cmdline # doctest: +ELLIPSIS
'mrresize -size 256,256,144 -interp cubic dwi.mif dwi_resized.mif'
>>> image_resize.run() # doctest: +SKIP

Defining the new image's voxel size:
>>> voxel_resize = mrt.MRResize()
>>> voxel_resize.inputs.in_file = 'dwi.mif'
>>> voxel_resize.inputs.voxel_size = (1, 1, 1)
>>> voxel_resize.cmdline # doctest: +ELLIPSIS
'mrresize -interp cubic -voxel 1,1,1 dwi.mif dwi_resized.mif'
>>> voxel_resize.run() # doctest: +SKIP

Defining the scale factor of each image dimension:
>>> scale_resize = mrt.MRResize()
>>> scale_resize.inputs.in_file = 'dwi.mif'
>>> scale_resize.inputs.scale_factor = (0.5,0.5,0.5)
>>> scale_resize.cmdline # doctest: +ELLIPSIS
'mrresize -interp cubic -scale 0.5,0.5,0.5 dwi.mif dwi_resized.mif'
>>> scale_resize.run() # doctest: +SKIP
"""

_cmd = 'mrresize'
input_spec = MRResizeInputSpec
output_spec = MRResizeOutputSpec