From a97b291bda338633110097f99e3cfb240ed23aaa Mon Sep 17 00:00:00 2001 From: Marc Modat Date: Tue, 28 Mar 2017 10:44:12 +0100 Subject: [PATCH 01/20] Initial commit adding the interfaces to the niftyreg executables as well as one workflow for groupwise registration --- nipype/interfaces/niftyreg/__init__.py | 14 + nipype/interfaces/niftyreg/base.py | 122 +++ nipype/interfaces/niftyreg/reg.py | 429 +++++++++++ nipype/interfaces/niftyreg/regutils.py | 727 ++++++++++++++++++ nipype/interfaces/niftyreg/tests/__init__.py | 0 .../niftyreg/tests/test_Reg_Aladin.py | 43 ++ .../niftyreg/tests/test_Reg_Average.py | 203 +++++ .../interfaces/niftyreg/tests/test_Reg_F3D.py | 45 ++ .../niftyreg/tests/test_Reg_Jacobian.py | 100 +++ .../niftyreg/tests/test_Reg_Measure.py | 39 + .../niftyreg/tests/test_Reg_Resample.py | 79 ++ .../niftyreg/tests/test_Reg_Tools.py | 63 ++ .../niftyreg/tests/test_Reg_Transform.py | 164 ++++ .../tests/test_auto_NiftyRegCommand.py | 23 + .../niftyreg/tests/test_auto_RegAladin.py | 92 +++ .../niftyreg/tests/test_auto_RegAverage.py | 71 ++ .../niftyreg/tests/test_auto_RegF3D.py | 142 ++++ .../niftyreg/tests/test_auto_RegJacobian.py | 47 ++ .../niftyreg/tests/test_auto_RegMeasure.py | 46 ++ .../niftyreg/tests/test_auto_RegResample.py | 62 ++ .../niftyreg/tests/test_auto_RegTools.py | 68 ++ .../niftyreg/tests/test_auto_RegTransform.py | 98 +++ nipype/interfaces/setup.py | 5 +- nipype/workflows/smri/__init__.py | 3 +- nipype/workflows/smri/niftyreg/__init__.py | 5 + nipype/workflows/smri/niftyreg/groupwise.py | 385 ++++++++++ 26 files changed, 3072 insertions(+), 3 deletions(-) create mode 100644 nipype/interfaces/niftyreg/__init__.py create mode 100644 nipype/interfaces/niftyreg/base.py create mode 100644 nipype/interfaces/niftyreg/reg.py create mode 100644 nipype/interfaces/niftyreg/regutils.py create mode 100644 nipype/interfaces/niftyreg/tests/__init__.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Average.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_F3D.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Measure.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Resample.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Tools.py create mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Transform.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_NiftyRegCommand.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegResample.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegTools.py create mode 100644 nipype/interfaces/niftyreg/tests/test_auto_RegTransform.py create mode 100644 nipype/workflows/smri/niftyreg/__init__.py create mode 100644 nipype/workflows/smri/niftyreg/groupwise.py diff --git a/nipype/interfaces/niftyreg/__init__.py b/nipype/interfaces/niftyreg/__init__.py new file mode 100644 index 0000000000..9981262fb5 --- /dev/null +++ b/nipype/interfaces/niftyreg/__init__.py @@ -0,0 +1,14 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +""" +The niftyreg module provides classes for interfacing with the `NiftyReg +`_ command line tools. + +Top-level namespace for niftyreg. +""" + +from .base import no_niftyreg, get_custom_path, PositiveInt +from .reg import RegAladin, RegF3D +from .regutils import (RegResample, RegJacobian, RegAverage, RegTools, + RegTransform, RegMeasure) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py new file mode 100644 index 0000000000..b7e443e484 --- /dev/null +++ b/nipype/interfaces/niftyreg/base.py @@ -0,0 +1,122 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +""" +The niftyreg module provides classes for interfacing with `niftyreg +`_ command line tools. + +These are the base tools for working with niftyreg. + +Registration tools are found in niftyreg/reg.py +Every other tool is found in niftyreg/regutils.py + +Examples +-------- +See the docstrings of the individual classes for examples. + +""" + +from distutils.version import StrictVersion +import os +import subprocess +import warnings + +from ..base import CommandLine, isdefined, traits +from ...utils.filemanip import split_filename + + +warn = warnings.warn +warnings.filterwarnings('always', category=UserWarning) + + +def get_custom_path(command): + try: + specific_dir = os.environ['NIFTYREGDIR'] + command = os.path.join(specific_dir, command) + return command + except KeyError: + return command + + +def no_niftyreg(cmd='reg_f3d'): + if True in [os.path.isfile(os.path.join(path, cmd)) and + os.access(os.path.join(path, cmd), os.X_OK) + for path in os.environ["PATH"].split(os.pathsep)]: + return False + return True + + +class PositiveInt(traits.BaseInt): + # Define the default value + default_value = 0 + # Describe the trait type + info_text = 'A positive integer' + + def validate(self, obj, name, value): + value = super(PositiveInt, self).validate(obj, name, value) + if (value >= 0) == 1: + return value + self.error(obj, name, value) + + +class NiftyRegCommand(CommandLine): + """ + Base support interface for NiftyReg commands. + """ + _suffix = '_nr' + _min_version = '1.5.10' + + def __init__(self, required_version=None, **inputs): + super(NiftyRegCommand, self).__init__(**inputs) + cur_version = self.get_version() + if StrictVersion(cur_version) < StrictVersion(self._min_version): + err = 'A later version of Niftyreg is required (%s < %s)' + raise ValueError(err % (cur_version, self._min_version)) + if required_version is not None: + if StrictVersion(cur_version) != StrictVersion(required_version): + err = 'The version of NiftyReg differs from the required' + err += '(%s != %s)' + raise ValueError(err % (cur_version, required_version)) + + def get_version(self): + if no_niftyreg(cmd=self.cmd): + return None + exec_cmd = ''.join((self.cmd, ' -v')) + return subprocess.check_output(exec_cmd, shell=True).strip('\n') + + @property + def version(self): + return self.get_version() + + def exists(self): + if self.get_version() is None: + return False + return True + + def _gen_fname(self, basename, out_dir=None, suffix=None, ext=None): + if basename == '': + msg = 'Unable to generate filename for command %s. ' % self.cmd + msg += 'basename is not set!' + raise ValueError(msg) + _, final_bn, final_ext = split_filename(basename) + if out_dir is None: + out_dir = os.getcwd() + if ext is not None: + final_ext = ext + if suffix is not None: + final_bn = ''.join((final_bn, suffix)) + return os.path.abspath(os.path.join(out_dir, final_bn + final_ext)) + + def _gen_filename(self, name): + if name == 'out_file': + return self._gen_fname(self.inputs.in_file, + suffix=self._suffix, ext='.nii.gz') + return None + + def _list_outputs(self): + outputs = self.output_spec().get() + if isdefined(self.inputs.out_file): + outputs['out_file'] = self.inputs.out_file + else: + outputs['out_file'] = self._gen_filename('out_file') + return outputs diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py new file mode 100644 index 0000000000..55afc80794 --- /dev/null +++ b/nipype/interfaces/niftyreg/reg.py @@ -0,0 +1,429 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +""" +The reg module provides classes for interfacing with the `niftyreg +`_ registration command line tools. + +The interfaces were written to work with niftyreg version 1.5.10 +""" + +import os +import warnings + +from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec +from .base import get_custom_path, NiftyRegCommand, PositiveInt +from ...utils.filemanip import split_filename + + +warn = warnings.warn +warnings.filterwarnings('always', category=UserWarning) + + +class RegAladinInputSpec(CommandLineInputSpec): + """ Input Spec for RegAladin. """ + # Input reference file + ref_file = File(exists=True, + desc='The input reference/target image', + argstr='-ref %s', + mandatory=True) + # Input floating file + flo_file = File(exists=True, + desc='The input floating/source image', + argstr='-flo %s', + mandatory=True) + # No symmetric flag + nosym_flag = traits.Bool(argstr='-noSym', + desc='Turn off symmetric registration') + # Rigid only registration + rig_only_flag = traits.Bool(argstr='-rigOnly', + desc='Do only a rigid registration') + # Directly optimise affine flag + desc = 'Directly optimise the affine parameters' + aff_direct_flag = traits.Bool(argstr='-affDirect', + desc=desc) + # Input affine + in_aff_file = File(exists=True, + desc='The input affine transformation', + argstr='-inaff %s') + # Input reference mask + rmask_file = File(exists=True, + desc='The input reference mask', + argstr='-rmask %s') + # Input floating mask + fmask_file = File(exists=True, + desc='The input floating mask', + argstr='-fmask %s') + # Maximum number of iterations + maxit_val = PositiveInt(desc='Maximum number of iterations', + argstr='-maxit %d') + # Multiresolution levels + ln_val = PositiveInt(desc='Number of resolution levels to create', + argstr='-ln %d') + # Number of resolution levels to process + lp_val = PositiveInt(desc='Number of resolution levels to perform', + argstr='-lp %d') + # Smoothing to apply on reference image + desc = 'Amount of smoothing to apply to reference image' + smoo_r_val = traits.Float(desc=desc, + argstr='-smooR %f') + # Smoothing to apply on floating image + desc = 'Amount of smoothing to apply to floating image' + smoo_f_val = traits.Float(desc=desc, + argstr='-smooF %f') + # Use nifti header to initialise transformation + desc = 'Use nifti header to initialise transformation' + nac_flag = traits.Bool(desc=desc, + argstr='-nac') + # Use the input masks centre of mass to initialise the transformation + desc = 'Use the masks centre of mass to initialise the transformation' + cog_flag = traits.Bool(desc=desc, + argstr='-cog') + # Percent of blocks that are considered active. + v_val = PositiveInt(desc='Percent of blocks that are active', + argstr='-pv %d') + # Percent of inlier blocks + i_val = PositiveInt(desc='Percent of inlier blocks', argstr='-pi %d') + # Lower threshold on reference image + ref_low_val = traits.Float(desc='Lower threshold value on reference image', + argstr='-refLowThr %f') + # Upper threshold on reference image + ref_up_val = traits.Float(desc='Upper threshold value on reference image', + argstr='-refUpThr %f') + # Lower threshold on floating image + flo_low_val = traits.Float(desc='Lower threshold value on floating image', + argstr='-floLowThr %f') + # Upper threshold on floating image + flo_up_val = traits.Float(desc='Upper threshold value on floating image', + argstr='-floUpThr %f') + # Platform to use + platform_val = traits.Int(desc='Platform index', + argstr='-platf %i') + # Platform to use + gpuid_val = traits.Int(desc='Device to use id', + argstr='-gpuid %i') + # Verbosity off + verbosity_off_flag = traits.Bool(argstr='-voff', + desc='Turn off verbose output') + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %i') + + # Affine output transformation matrix file + aff_file = File(genfile=True, + desc='The output affine matrix file', + argstr='-aff %s') + # Result warped image file + res_file = File(genfile=True, + desc='The affine transformed floating image', + argstr='-res %s') + + +class RegAladinOutputSpec(TraitedSpec): + """ Output Spec for RegAladin. """ + aff_file = File(desc='The output affine file') + res_file = File(desc='The output transformed image') + desc = 'Output string in the format for reg_average' + avg_output = traits.String(desc=desc) + + +class RegAladin(NiftyRegCommand): + """Interface for executable reg_aladin from NiftyReg platform. + + Block Matching algorithm for symmetric global registration. + Based on Modat et al., "Global image registration using + asymmetric block-matching approach" + J. Med. Img. 1(2) 024003, 2014, doi: 10.1117/1.JMI.1.2.024003 + + For source code, see https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegAladin + >>> node = RegAladin() + >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP + >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP + >>> node.inputs.rmask_file = 'rmask.nii.gz' # doctest: +SKIP + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +SKIP + 'reg_aladin -aff flo_aff.txt -flo flo.nii.gz -omp 4 -ref ref.nii.gz \ +-rmask rmask.nii.gz -res flo_res.nii.gz' + + """ + _cmd = get_custom_path('reg_aladin') + input_spec = RegAladinInputSpec + output_spec = RegAladinOutputSpec + + def _gen_filename(self, name): + if name == 'aff_file': + return self._gen_fname(self.inputs.flo_file, + suffix='_aff', ext='.txt') + if name == 'res_file': + return self._gen_fname(self.inputs.flo_file, + suffix='_res', ext='.nii.gz') + return None + + def _list_outputs(self): + outputs = self.output_spec().get() + + if isdefined(self.inputs.aff_file): + outputs['aff_file'] = self.inputs.aff_file + else: + outputs['aff_file'] = self._gen_filename('aff_file') + if isdefined(self.inputs.res_file): + outputs['res_file'] = self.inputs.aff_file + else: + outputs['res_file'] = self._gen_filename('res_file') + + # Make a list of the linear transformation file and the input image + aff = os.path.abspath(outputs['aff_file']) + flo = os.path.abspath(self.inputs.flo_file) + outputs['avg_output'] = '%s %s' % (aff, flo) + return outputs + + +class RegF3DInputSpec(CommandLineInputSpec): + """ Input Spec for RegF3D. """ + # Input reference file + ref_file = File(exists=True, + desc='The input reference/target image', + argstr='-ref %s', + mandatory=True) + # Input floating file + flo_file = File(exists=True, + desc='The input floating/source image', + argstr='-flo %s', + mandatory=True) + + # Input Affine file + aff_file = File(exists=True, + desc='The input affine transformation file', + argstr='-aff %s') + + # Input cpp file + incpp_file = File(exists=True, + desc='The input cpp transformation file', + argstr='-incpp %s') + + # Reference mask + rmask_file = File(exists=True, + desc='Reference image mask', + argstr='-rmask %s') + + # Smoothing kernel for reference + desc = 'Smoothing kernel width for reference image' + ref_smooth_val = traits.Float(desc=desc, argstr='-smooR %f') + # Smoothing kernel for floating + desc = 'Smoothing kernel width for floating image' + flo_smooth_val = traits.Float(desc=desc, argstr='-smooF %f') + + # Lower threshold for reference image + rlwth_thr_val = traits.Float(desc='Lower threshold for reference image', + argstr='--rLwTh %f') + # Upper threshold for reference image + rupth_thr_val = traits.Float(desc='Upper threshold for reference image', + argstr='--rUpTh %f') + # Lower threshold for reference image + flwth_thr_val = traits.Float(desc='Lower threshold for floating image', + argstr='--fLwTh %f') + # Upper threshold for reference image + fupth_thr_val = traits.Float(desc='Upper threshold for floating image', + argstr='--fUpTh %f') + + # Lower threshold for reference image + desc = 'Lower threshold for reference image at the specified time point' + rlwth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + desc=desc, argstr='-rLwTh %d %f') + # Upper threshold for reference image + desc = 'Upper threshold for reference image at the specified time point' + rupth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + desc=desc, argstr='-rUpTh %d %f') + # Lower threshold for reference image + desc = 'Lower threshold for floating image at the specified time point' + flwth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + desc=desc, argstr='-fLwTh %d %f') + # Upper threshold for reference image + desc = 'Upper threshold for floating image at the specified time point' + fupth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + desc=desc, argstr='-fUpTh %d %f') + + # Final grid spacing along the 3 axes + sx_val = traits.Float(desc='Final grid spacing along the x axes', + argstr='-sx %f') + sy_val = traits.Float(desc='Final grid spacing along the y axes', + argstr='-sy %f') + sz_val = traits.Float(desc='Final grid spacing along the z axes', + argstr='-sz %f') + + # Regularisation options + be_val = traits.Float(desc='Bending energy value', argstr='-be %f') + le_val = traits.Float(desc='Linear elasticity penalty term', + argstr='-le %f') + jl_val = traits.Float(desc='Log of jacobian of deformation penalty value', + argstr='-jl %f') + desc = 'Do not approximate the log of jacobian penalty at control points \ +only' + no_app_jl_flag = traits.Bool(argstr='-noAppJL', desc=desc) + + # Similarity measure options + desc = 'use NMI even when other options are specified' + nmi_flag = traits.Bool(argstr='--nmi', desc=desc) + desc = 'Number of bins in the histogram for reference image' + rbn_val = PositiveInt(desc=desc, argstr='--rbn %d') + desc = 'Number of bins in the histogram for reference image' + fbn_val = PositiveInt(desc=desc, argstr='--fbn %d') + desc = 'Number of bins in the histogram for reference image for given \ +time point' + rbn2_val = traits.Tuple(PositiveInt, PositiveInt, + desc=desc, argstr='-rbn %d %d') + + desc = 'Number of bins in the histogram for reference image for given \ +time point' + fbn2_val = traits.Tuple(PositiveInt, PositiveInt, + desc=desc, argstr='-fbn %d %d') + + lncc_val = traits.Float(desc='SD of the Gaussian for computing LNCC', + argstr='--lncc %f') + desc = 'SD of the Gaussian for computing LNCC for a given time point' + lncc2_val = traits.Tuple(PositiveInt, traits.Float, + desc=desc, argstr='-lncc %d %f') + + ssd_flag = traits.Bool(desc='Use SSD as the similarity measure', + argstr='--ssd') + desc = 'Use SSD as the similarity measure for a given time point' + ssd2_flag = PositiveInt(desc=desc, argstr='-ssd %d') + kld_flag = traits.Bool(desc='Use KL divergence as the similarity measure', + argstr='--kld') + desc = 'Use KL divergence as the similarity measure for a given time point' + kld2_flag = PositiveInt(desc=desc, argstr='-kld %d') + amc_flag = traits.Bool(desc='Use additive NMI', argstr='-amc') + + nox_flag = traits.Bool(desc="Don't optimise in x direction", + argstr='-nox') + noy_flag = traits.Bool(desc="Don't optimise in y direction", + argstr='-noy') + noz_flag = traits.Bool(desc="Don't optimise in z direction", + argstr='-noz') + + # Optimization options + maxit_val = PositiveInt(desc='Maximum number of iterations per level', + argstr='-maxit %d') + ln_val = PositiveInt(desc='Number of resolution levels to create', + argstr='-ln %d') + lp_val = PositiveInt(desc='Number of resolution levels to perform', + argstr='-lp %d') + nopy_flag = traits.Bool(desc='Do not use the multiresolution approach', + argstr='-nopy') + noconj_flag = traits.Bool(desc='Use simple GD optimization', + argstr='-noConj') + desc = 'Add perturbation steps after each optimization step' + pert_val = PositiveInt(desc=desc, argstr='-pert %d') + + # F3d2 options + vel_flag = traits.Bool(desc='Use velocity field integration', + argstr='-vel') + fmask_file = File(exists=True, + desc='Floating image mask', + argstr='-fmask %s') + + # Other options + desc = 'Kernel width for smoothing the metric gradient' + smooth_grad_val = traits.Float(desc=desc, argstr='-smoothGrad %f') + # Padding value + pad_val = traits.Float(desc='Padding value', argstr='-pad %f') + # verbosity off + verbosity_off_flag = traits.Bool(argstr='-voff', + desc='Turn off verbose output') + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %i') + + # Output CPP image file + cpp_file = File(genfile=True, + desc='The output CPP file', + argstr='-cpp %s') + # Output warped image file + res_file = File(genfile=True, + desc='The output resampled image', + argstr='-res %s') + + +class RegF3DOutputSpec(TraitedSpec): + """ Output Spec for RegF3D. """ + cpp_file = File(desc='The output CPP file') + res_file = File(desc='The output resampled image') + invcpp_file = File(desc='The output inverse CPP file') + invres_file = File(desc='The output inverse res file') + desc = 'Output string in the format for reg_average' + avg_output = traits.String(desc=desc) + + +class RegF3D(NiftyRegCommand): + """Interface for executable reg_f3d from NiftyReg platform. + + Fast Free-Form Deformation (F3D) algorithm for non-rigid registration. + Initially based on Modat et al., "Fast Free-Form Deformation using + graphics processing units", CMPB, 2010 + + For source code, see https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegF3D + >>> node = RegF3D() + >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP + >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP + >>> node.inputs.rmask_file = 'rmask.nii.gz' # doctest: +SKIP + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +SKIP + 'reg_f3d -be 0.100000 -cpp flo_cpp.nii.gz -flo flo.nii.gz -le 0.100000 \ +-omp 4 -ref ref.nii.gz -res flo_res.nii.gz -rmask rmask.nii.gz -vel' + + """ + _cmd = get_custom_path('reg_f3d') + input_spec = RegF3DInputSpec + output_spec = RegF3DOutputSpec + + @staticmethod + def _remove_extension(in_file): + dn, bn, _ = split_filename(in_file) + return os.path.join(dn, bn) + + def _gen_filename(self, name): + if name == 'res_file': + return self._gen_fname(self.inputs.flo_file, + suffix='_res', ext='.nii.gz') + if name == 'cpp_file': + return self._gen_fname(self.inputs.flo_file, + suffix='_cpp', ext='.nii.gz') + + def _list_outputs(self): + outputs = self.output_spec().get() + + if isdefined(self.inputs.res_file): + outputs['res_file'] = self.inputs.res_file + else: + outputs['res_file'] = self._gen_filename('res_file') + + if isdefined(self.inputs.cpp_file): + outputs['cpp_file'] = self.inputs.cpp_file + else: + outputs['cpp_file'] = self._gen_filename('cpp_file') + + if self.inputs.vel_flag is True: + res_name = self._remove_extension(outputs['res_file']) + cpp_name = self._remove_extension(outputs['cpp_file']) + outputs['invres_file'] = '%s_backward.nii.gz' % res_name + outputs['invcpp_file'] = '%s_backward.nii.gz' % cpp_name + + # Make a list of the linear transformation file and the input image + if self.inputs.vel_flag is True and isdefined(self.inputs.aff_file): + cpp_file = os.path.abspath(outputs['cpp_file']) + flo_file = os.path.abspath(self.inputs.flo_file) + outputs['avg_output'] = '%s %s %s' % (self.inputs.aff_file, + cpp_file, flo_file) + else: + cpp_file = os.path.abspath(outputs['cpp_file']) + flo_file = os.path.abspath(self.inputs.flo_file) + outputs['avg_output'] = '%s %s' % (cpp_file, flo_file) + return outputs diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py new file mode 100644 index 0000000000..2b40b97621 --- /dev/null +++ b/nipype/interfaces/niftyreg/regutils.py @@ -0,0 +1,727 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: +"""The regutils module provides classes for interfacing with the `niftyreg +`_ utility command line tools. +The interfaces were written to work with niftyreg version 1.4 +""" + +import warnings +import os + +from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec +from .base import get_custom_path, NiftyRegCommand +from ...utils.filemanip import split_filename + + +warn = warnings.warn +warnings.filterwarnings('always', category=UserWarning) + + +class RegResampleInputSpec(CommandLineInputSpec): + """ Input Spec for RegResample. """ + # Input reference file + ref_file = File(exists=True, + desc='The input reference/target image', + argstr='-ref %s', + mandatory=True) + # Input floating file + flo_file = File(exists=True, + desc='The input floating/source image', + argstr='-flo %s', + mandatory=True) + # Input deformation field + trans_file = File(exists=True, + desc='The input transformation file', + argstr='-trans %s') + + type = traits.Enum('res', 'blank', + argstr='-%s', + position=-2, + usedefault=True, + desc='Type of output') + + # Output file name + out_file = File(genfile=True, + argstr='%s', + position=-1, + desc='The output filename of the transformed image') + + # Interpolation type + inter_val = traits.Enum('NN', 'LIN', 'CUB', 'SINC', + desc='Interpolation type', + argstr='-inter %d') + + # Padding value + pad_val = traits.Float(desc='Padding value', argstr='-pad %f') + + # Tensor flag + tensor_flag = traits.Bool(desc='Resample Tensor Map', argstr='-tensor ') + + # Verbosity off + verbosity_off_flag = traits.Bool(argstr='-voff', + desc='Turn off verbose output') + # PSF flag + desc = 'Perform the resampling in two steps to resample an image to a \ +lower resolution' + psf_flag = traits.Bool(argstr='-psf', desc=desc) + desc = 'Minimise the matrix metric (0) or the determinant (1) when \ +estimating the PSF [0]' + psf_alg = traits.Enum(0, 1, argstr='-psf_alg %d', desc=desc) + + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %d') + + +class RegResampleOutputSpec(TraitedSpec): + """ Output Spec for RegResample. """ + out_file = File(desc='The output filename of the transformed image') + + +class RegResample(NiftyRegCommand): + """Interface for executable reg_resample from NiftyReg platform. + + Tool to resample floating image in the space of a defined reference image + given a transformation parametrisation generated by reg_aladin, reg_f3d or + reg_transform + + For complete documentation, see https://cmiclab.cs.ucl.ac.uk/mmodat/\ + niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegResample + >>> node = RegResample() + >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP + >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP + >>> node.inputs.trans_file = 'warpfield.nii.gz' # doctest: +SKIP + >>> node.inputs.inter_val = 'LIN' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +SKIP + 'reg_resample -flo flo.nii.gz -inter 1 -omp 4 -ref ref.nii.gz -trans \ +warpfield.nii.gz -res flo_res.nii.gz' + + """ + _cmd = get_custom_path('reg_resample') + input_spec = RegResampleInputSpec + output_spec = RegResampleOutputSpec + + # Need this overload to properly constraint the interpolation type input + def _format_arg(self, name, spec, value): + if name == 'inter_val': + inter_val = {'NN': 0, 'LIN': 1, 'CUB': 3, 'SINC': 5} + return spec.argstr % inter_val[value] + else: + return super(RegResample, self)._format_arg(name, spec, value) + + def _gen_filename(self, name): + if name == 'out_file': + return self._gen_fname(self.inputs.flo_file, + suffix='_%s' % self.inputs.type, + ext='.nii.gz') + return None + + +class RegJacobianInputSpec(CommandLineInputSpec): + """ Input Spec for RegJacobian. """ + # Reference file name + desc = 'Reference/target file (required if specifying CPP transformations.' + ref_file = File(exists=True, + desc=desc, + argstr='-ref %s') + # Input transformation file + trans_file = File(exists=True, + desc='The input non-rigid transformation', + argstr='-trans %s', + mandatory=True) + type = traits.Enum('jac', 'jacL', 'jacM', + usedefault=True, + argstr='-%s', + position=-2, + desc='Type of jacobian outcome') + out_file = File(genfile=True, + desc='The output jacobian determinant file name', + argstr='%s', + position=-1) + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %i') + + +class RegJacobianOutputSpec(TraitedSpec): + """ Output Spec for RegJacobian. """ + out_file = File(desc='The output file') + + +class RegJacobian(NiftyRegCommand): + """Interface for executable reg_resample from NiftyReg platform. + + Tool to generate Jacobian determinant maps from transformation + parametrisation generated by reg_f3d + + For source code, see https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegJacobian + >>> node = RegJacobian() + >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP + >>> node.inputs.trans_file = 'warpfield.nii.gz' # doctest: +SKIP + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +SKIP + 'reg_jacobian -omp 4 -ref ref.nii.gz -trans warpfield.nii.gz -jac \ +warpfield_jac.nii.gz' + + """ + _cmd = get_custom_path('reg_jacobian') + input_spec = RegJacobianInputSpec + output_spec = RegJacobianOutputSpec + + def _gen_filename(self, name): + if name == 'out_file': + return self._gen_fname(self.inputs.trans_file, + suffix='_%s' % self.inputs.type, + ext='.nii.gz') + return None + + +class RegToolsInputSpec(CommandLineInputSpec): + """ Input Spec for RegTools. """ + # Input image file + in_file = File(exists=True, + desc='The input image file path', + argstr='-in %s', + mandatory=True) + + # Output file path + out_file = File(genfile=True, + desc='The output file name', + argstr='-out %s') + + # Make the output image isotropic + iso_flag = traits.Bool(argstr='-iso', desc='Make output image isotropic') + + # Set scale, slope to 0 and 1. + noscl_flag = traits.Bool(argstr='-noscl', + desc='Set scale, slope to 0 and 1') + + # Values outside the mask are set to NaN + mask_file = File(exists=True, + desc='Values outside the mask are set to NaN', + argstr='-nan %s') + + # Threshold the input image + desc = 'Binarise the input image with the given threshold' + thr_val = traits.Float(desc=desc, argstr='-thr %f') + + # Binarise the input image + bin_flag = traits.Bool(argstr='-bin', desc='Binarise the input image') + + # Compute the mean RMS between the two images + rms_val = File(exists=True, + desc='Compute the mean RMS between the images', + argstr='-rms %s') + + # Perform division by image or value + div_val = traits.Either(traits.Float, File(exists=True), + desc='Divide the input by image or value', + argstr='-div %s') + + # Perform multiplication by image or value + mul_val = traits.Either(traits.Float, File(exists=True), + desc='Multiply the input by image or value', + argstr='-mul %s') + + # Perform addition by image or value + add_val = traits.Either(traits.Float, File(exists=True), + desc='Add to the input image or value', + argstr='-add %s') + + # Perform subtraction by image or value + sub_val = traits.Either(traits.Float, File(exists=True), + desc='Add to the input image or value', + argstr='-sub %s') + + # Downsample the image by a factor of 2. + down_flag = traits.Bool(desc='Downsample the image by a factor of 2', + argstr='-down') + + # Smoothing using spline kernel + desc = 'Smooth the input image using a cubic spline kernel' + smo_s_val = traits.Tuple(traits.Float, traits.Float, traits.Float, + desc=desc, + argstr='-smoS %f %f %f') + + # Change the resolution of the input image + chg_res_val = traits.Tuple(traits.Float, traits.Float, traits.Float, + desc='Change the resolution of the input image', + argstr='-chgres %f %f %f') + + # Smoothing using Gaussian kernel + desc = 'Smooth the input image using a Gaussian kernel' + smo_g_val = traits.Tuple(traits.Float, traits.Float, traits.Float, + desc=desc, + argstr='-smoG %f %f %f') + + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %i') + + +class RegToolsOutputSpec(TraitedSpec): + """ Output Spec for RegTools. """ + out_file = File(desc='The output file', exists=True) + + +class RegTools(NiftyRegCommand): + """Interface for executable reg_tools from NiftyReg platform. + + Tool delivering various actions related to registration such as + resampling the input image to a chosen resolution or remove the nan and + inf in the input image by a specified value. + + For source code, see https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegTools + >>> node = RegTools() + >>> node.inputs.in_file = 'im1.nii.gz' # doctest: +SKIP + >>> node.inputs.mul_val = 4 + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +SKIP + 'reg_tools -in im1.nii.gz -mul 4.0 -omp 4 -out im1_tools.nii.gz' + + """ + _cmd = get_custom_path('reg_tools') + input_spec = RegToolsInputSpec + output_spec = RegToolsOutputSpec + _suffix = '_tools' + + +class RegAverageInputSpec(CommandLineInputSpec): + """ Input Spec for RegAverage. """ + avg_files = traits.List(File(exist=True), + position=1, + argstr='-avg %s', + sep=' ', + xor=['avg_lts_files', 'avg_ref_file', + 'demean1_ref_file', 'demean2_ref_file', + 'demean3_ref_file', 'warp_files'], + desc='Averaging of images/affine transformations') + + desc = 'Robust average of affine transformations' + avg_lts_files = traits.List(File(exist=True), + position=1, + argstr='-avg_lts %s', + sep=' ', + xor=['avg_files', 'avg_ref_file', + 'demean1_ref_file', 'demean2_ref_file', + 'demean3_ref_file', 'warp_files'], + desc=desc) + + desc = 'All input images are resampled into the space of \ + and averaged. A cubic spline interpolation scheme is used for resampling' + avg_ref_file = File(position=1, + argstr='-avg_tran %s', + xor=['avg_files', 'avg_lts_files', 'demean1_ref_file', + 'demean2_ref_file', 'demean3_ref_file'], + requires=['warp_files'], + desc=desc) + + desc = 'Average images and demean average image that have affine \ +transformations to a common space' + demean1_ref_file = File(position=1, + argstr='-demean1 %s', + xor=['avg_files', 'avg_lts_files', 'avg_ref_file', + 'demean2_ref_file', 'demean3_ref_file'], + requires=['warp_files'], + desc=desc) + + desc = 'Average images and demean average image that have non-rigid \ +transformations to a common space' + demean2_ref_file = File(position=1, + argstr='-demean2 %s', + xor=['avg_files', 'avg_lts_files', 'avg_ref_file', + 'demean1_ref_file', 'demean3_ref_file'], + requires=['warp_files'], + desc=desc) + + desc = 'Average images and demean average image that have linear and \ +non-rigid transformations to a common space' + demean3_ref_file = File(position=1, + argstr='-demean3 %s', + xor=['avg_files', 'avg_lts_files', 'avg_ref_file', + 'demean1_ref_file', 'demean2_ref_file'], + requires=['warp_files'], + desc=desc) + + desc = 'transformation files and floating image pairs/triplets to the \ +reference space' + warp_files = traits.List(File(exist=True), + position=-1, + argstr='%s', + sep=' ', + xor=['avg_files', 'avg_lts_files'], + desc=desc) + + out_file = File(genfile=True, + position=0, + desc='Output file name', + argstr='%s') + + +class RegAverageOutputSpec(TraitedSpec): + """ Output Spec for RegAverage. """ + out_file = File(desc='Output file name') + + +class RegAverage(NiftyRegCommand): + """Interface for executable reg_average from NiftyReg platform. + + Compute average matrix or image from a list of matrices or image. + The tool can be use to resample images given input transformation + parametrisation as well as to demean transformations in Euclidean or + log-Euclidean space. + + This interface is different than the others in the way that the options + will be written in a command file that is given as a parameter. + + For source code, see https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegAverage + >>> node = RegAverage() + >>> one_file = 'im1.nii' + >>> two_file = 'im2.nii' + >>> three_file = 'im3.nii' + >>> node.inputs.avg_files = [one_file, two_file, three_file] \ + # doctest: +SKIP + >>> node.cmdline # doctest: +SKIP + 'reg_average --cmd_file reg_average_cmd' + + """ + _cmd = get_custom_path('reg_average') + input_spec = RegAverageInputSpec + output_spec = RegAverageOutputSpec + _suffix = 'avg_out' + + def _gen_filename(self, name): + if name == 'out_file': + if isdefined(self.inputs.avg_lts_files): + return self._gen_fname(self._suffix, ext='.txt') + elif isdefined(self.inputs.avg_files): + _, _, ext = split_filename(self.inputs.avg_files[0]) + if ext not in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz']: + return self._gen_fname(self._suffix, ext=ext) + return self._gen_fname(self._suffix, ext='.nii.gz') + return None + + @property + def cmdline(self): + """ Rewrite the cmdline to write options in text_file.""" + argv = super(RegAverage, self).cmdline + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'w') as f: + f.write(argv) + return '%s --cmd_file %s' % (self.cmd, reg_average_cmd) + + +class RegTransformInputSpec(CommandLineInputSpec): + """ Input Spec for RegTransform. """ + ref1_file = File(exists=True, + desc='The input reference/target image', + argstr='-ref %s', + position=0) + + ref2_file = File(exists=True, + desc='The input second reference/target image', + argstr='-ref2 %s', + position=1, + requires=['ref1_file']) + + def_input = File(exists=True, + argstr='-def %s', + position=-2, + desc='Compute deformation field from transformation', + xor=['disp_input', 'flow_input', 'comp_input', + 'upd_s_form_input', 'inv_aff_input', + 'inv_nrr_input', 'half_input', 'make_aff_input', + 'aff_2_rig_input', 'flirt_2_nr_input']) + + disp_input = File(exists=True, + argstr='-disp %s', + position=-2, + desc='Compute displacement field from transformation', + xor=['def_input', 'flow_input', 'comp_input', + 'upd_s_form_input', 'inv_aff_input', + 'inv_nrr_input', 'half_input', 'make_aff_input', + 'aff_2_rig_input', 'flirt_2_nr_input']) + + flow_input = File(exists=True, + argstr='-flow %s', + position=-2, + desc='Compute flow field from spline SVF', + xor=['def_input', 'disp_input', 'comp_input', + 'upd_s_form_input', 'inv_aff_input', + 'inv_nrr_input', 'half_input', 'make_aff_input', + 'aff_2_rig_input', 'flirt_2_nr_input']) + + comp_input = File(exists=True, + argstr='-comp %s', + position=-3, + desc='compose two transformations', + xor=['def_input', 'disp_input', 'flow_input', + 'upd_s_form_input', 'inv_aff_input', + 'inv_nrr_input', 'half_input', 'make_aff_input', + 'aff_2_rig_input', 'flirt_2_nr_input'], + requires=['comp_input2']) + + comp_input2 = File(exists=True, + argstr='%s', + position=-2, + desc='compose two transformations') + + desc = 'Update s-form using the affine transformation' + upd_s_form_input = File(exists=True, + argstr='-updSform %s', + position=-3, + desc=desc, + xor=['def_input', 'disp_input', 'flow_input', + 'comp_input', 'inv_aff_input', + 'inv_nrr_input', 'half_input', + 'make_aff_input', 'aff_2_rig_input', + 'flirt_2_nr_input'], + requires=['upd_s_form_input2']) + + desc = 'Update s-form using the affine transformation' + upd_s_form_input2 = File(exists=True, + argstr='%s', + position=-2, + desc=desc, + requires=['upd_s_form_input']) + + inv_aff_input = File(exists=True, + argstr='-invAff %s', + position=-2, + desc='Invert an affine transformation', + xor=['def_input', 'disp_input', 'flow_input', + 'comp_input', 'upd_s_form_input', + 'inv_nrr_input', 'half_input', 'make_aff_input', + 'aff_2_rig_input', 'flirt_2_nr_input']) + + inv_nrr_input = traits.Tuple(File(exists=True), File(exists=True), + desc='Invert a non-linear transformation', + argstr='-invNrr %s %s', + position=-2, + xor=['def_input', 'disp_input', 'flow_input', + 'comp_input', 'upd_s_form_input', + 'inv_aff_input', 'half_input', + 'make_aff_input', 'aff_2_rig_input', + 'flirt_2_nr_input']) + + half_input = File(exists=True, + argstr='-half %s', + position=-2, + desc='Half way to the input transformation', + xor=['def_input', 'disp_input', 'flow_input', + 'comp_input', 'upd_s_form_input', + 'inv_aff_input', 'inv_nrr_input', 'make_aff_input', + 'aff_2_rig_input', 'flirt_2_nr_input']) + + argstr_tmp = '-makeAff %f %f %f %f %f %f %f %f %f %f %f %f' + make_aff_input = traits.Tuple(traits.Float, traits.Float, traits.Float, + traits.Float, traits.Float, traits.Float, + traits.Float, traits.Float, traits.Float, + traits.Float, traits.Float, traits.Float, + argstr=argstr_tmp, + position=-2, + desc='Make an affine transformation matrix', + xor=['def_input', 'disp_input', 'flow_input', + 'comp_input', 'upd_s_form_input', + 'inv_aff_input', 'inv_nrr_input', + 'half_input', 'aff_2_rig_input', + 'flirt_2_nr_input']) + + desc = 'Extract the rigid component from affine transformation' + aff_2_rig_input = File(exists=True, + argstr='-aff2rig %s', + position=-2, + desc=desc, + xor=['def_input', 'disp_input', 'flow_input', + 'comp_input', 'upd_s_form_input', + 'inv_aff_input', 'inv_nrr_input', 'half_input', + 'make_aff_input', 'flirt_2_nr_input']) + + desc = 'Convert a FLIRT affine transformation to niftyreg affine \ +transformation' + flirt_2_nr_input = traits.Tuple(File(exists=True), File(exists=True), + File(exists=True), + argstr='-flirtAff2NR %s %s %s', + position=-2, + desc=desc, + xor=['def_input', 'disp_input', + 'flow_input', 'comp_input', + 'upd_s_form_input', 'inv_aff_input', + 'inv_nrr_input', 'half_input', + 'make_aff_input', 'aff_2_rig_input']) + + out_file = File(genfile=True, + position=-1, + argstr='%s', + desc='transformation file to write') + + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %i') + + +class RegTransformOutputSpec(TraitedSpec): + """ Output Spec for RegTransform. """ + desc = 'Output File (transformation in any format)' + out_file = File(exists=True, desc=desc) + + +class RegTransform(NiftyRegCommand): + """Interface for executable reg_transform from NiftyReg platform. + + Tools to convert transformation parametrisation from one type to another + as well as to compose, inverse or half transformations. + + For source code, see https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegResample + >>> node = RegTransform() + >>> node.inputs.def_input = 'warpfield.nii' # doctest: +SKIP + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +SKIP + 'reg_transform -omp 4 -def warpfield.nii warpfield_trans.nii.gz' + + """ + _cmd = get_custom_path('reg_transform') + input_spec = RegTransformInputSpec + output_spec = RegTransformOutputSpec + _suffix = '_trans' + + def _find_input(self): + inputs = [self.inputs.def_input, self.inputs.disp_input, + self.inputs.flow_input, self.inputs.comp_input, + self.inputs.comp_input2, self.inputs.upd_s_form_input, + self.inputs.inv_aff_input, self.inputs.inv_nrr_input, + self.inputs.half_input, self.inputs.make_aff_input, + self.inputs.aff_2_rig_input, self.inputs.flirt_2_nr_input] + entries = [] + for entry in inputs: + if isdefined(entry): + entries.append(entry) + _, _, ext = split_filename(entry) + if ext == '.nii' or ext == '.nii.gz' or ext == '.hdr': + return entry + if len(entries): + return entries[0] + return None + + def _gen_filename(self, name): + if name == 'out_file': + if isdefined(self.inputs.make_aff_input): + return self._gen_fname('matrix', suffix=self._suffix, + ext='.txt') + + if isdefined(self.inputs.comp_input) and \ + isdefined(self.inputs.comp_input2): + _, bn1, ext1 = split_filename(self.inputs.comp_input) + _, _, ext2 = split_filename(self.inputs.comp_input2) + if ext1 in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz'] or \ + ext2 in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz']: + return self._gen_fname(bn1, suffix=self._suffix, + ext='.nii.gz') + else: + return self._gen_fname(bn1, suffix=self._suffix, ext=ext1) + + if isdefined(self.inputs.flirt_2_nr_input): + return self._gen_fname(self.inputs.flirt_2_nr_input[0], + suffix=self._suffix, ext='.txt') + + input_to_use = self._find_input() + _, _, ext = split_filename(input_to_use) + if ext not in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz']: + return self._gen_fname(input_to_use, suffix=self._suffix, + ext=ext) + else: + return self._gen_fname(input_to_use, suffix=self._suffix, + ext='.nii.gz') + + return None + + def _list_outputs(self): + outputs = self.output_spec().get() + + if isdefined(self.inputs.out_file): + outputs['out_file'] = self.inputs.out_file + else: + outputs['out_file'] = self._gen_filename('out_file') + + return outputs + + +class RegMeasureInputSpec(CommandLineInputSpec): + """ Input Spec for RegMeasure. """ + # Input reference file + ref_file = File(exists=True, + desc='The input reference/target image', + argstr='-ref %s', + mandatory=True) + # Input floating file + flo_file = File(exists=True, + desc='The input floating/source image', + argstr='-flo %s', + mandatory=True) + measure_type = traits.Enum('ncc', 'lncc', 'nmi', 'ssd', + mandatory=True, + argstr='-%s', + desc='Measure of similarity to compute') + out_file = File(genfile=True, + argstr='-out %s', + desc='The output text file containing the measure') + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %i') + + +class RegMeasureOutputSpec(TraitedSpec): + """ Output Spec for RegMeasure. """ + out_file = File(desc='The output text file containing the measure') + + +class RegMeasure(NiftyRegCommand): + """Interface for executable reg_measure from NiftyReg platform. + + Given two input images, compute the specified measure(s) of similarity + + For source code, see https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg + + Examples + -------- + >>> from nipype.interfaces.niftyreg import RegMeasure + >>> node = RegMeasure() + >>> node.inputs.ref_file = 'im1.nii' # doctest: +SKIP + >>> node.inputs.flo_file = 'im2.nii' # doctest: +SKIP + >>> node.inputs.measure_type = 'lncc' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +SKIP + 'reg_measure -flo {flo} -lncc -omp 4 -out {out} -ref {ref}' + + """ + _cmd = get_custom_path('reg_measure') + input_spec = RegMeasureInputSpec + output_spec = RegMeasureOutputSpec + + def _gen_filename(self, name): + if name == 'out_file': + return self._gen_fname(self.inputs.flo_file, + suffix='_%s' % self.inputs.measure_type, + ext='.txt') + return None diff --git a/nipype/interfaces/niftyreg/tests/__init__.py b/nipype/interfaces/niftyreg/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py b/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py new file mode 100644 index 0000000000..5a42a72a2a --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py @@ -0,0 +1,43 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegAladin) +from nipype.testing import skipif, example_data +import os +import pytest + + +@skipif(no_niftyreg(cmd='reg_aladin')) +def test_reg_aladin(): + + # Create a reg_aladin object + nr = RegAladin() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_aladin') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + rmask_file = example_data('mask.nii') + nr.inputs.ref_file = ref_file + nr.inputs.flo_file = flo_file + nr.inputs.rmask_file = rmask_file + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -aff {aff} -flo {flo} -omp 4 -ref {ref} -res {res} \ +-rmask {rmask}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_aladin'), + aff=os.path.join(os.getcwd(), 'im2_aff.txt'), + flo=flo_file, + ref=ref_file, + res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), + rmask=rmask_file) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Average.py b/nipype/interfaces/niftyreg/tests/test_Reg_Average.py new file mode 100644 index 0000000000..8bebbc4e09 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Average.py @@ -0,0 +1,203 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegAverage) +from nipype.testing import skipif, example_data +import os + + +@skipif(no_niftyreg(cmd='reg_average')) +def test_reg_average_avg_nii(): + + # Create a reg_average object + nr = RegAverage() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_average') + + # Assign some input data + one_file = example_data('im1.nii') + two_file = example_data('im2.nii') + three_file = example_data('im3.nii') + nr.inputs.avg_files = [one_file, two_file, three_file] + nr.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f: + argv = f.read() + os.remove(reg_average_cmd) + + expected_argv = '%s %s -avg %s %s %s' % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), + 'avg_out.nii.gz'), + one_file, two_file, three_file) + + assert argv == expected_argv + + expected_cmd = ('%s --cmd_file %s' + % (get_custom_path('reg_average'), reg_average_cmd)) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_average')) +def test_reg_average_avg_txt(): + + # Create a reg_average object + nr = RegAverage() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_average') + + # Assign some input data + one_file = example_data('TransformParameters.0.txt') + two_file = example_data('ants_Affine.txt') + three_file = example_data('elastix.txt') + nr.inputs.avg_files = [one_file, two_file, three_file] + nr.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f: + argv = f.read() + os.remove(reg_average_cmd) + + expected_argv = '%s %s -avg %s %s %s' % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), + 'avg_out.txt'), + one_file, two_file, three_file) + + assert argv == expected_argv + + expected_cmd = ('%s --cmd_file %s' + % (get_custom_path('reg_average'), reg_average_cmd)) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_average')) +def test_reg_average_avg_lts(): + + # Create a reg_average object + nr = RegAverage() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_average') + + # Assign some input data + one_file = example_data('TransformParameters.0.txt') + two_file = example_data('ants_Affine.txt') + three_file = example_data('elastix.txt') + nr.inputs.avg_lts_files = [one_file, two_file, three_file] + nr.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f: + argv = f.read() + os.remove(reg_average_cmd) + + expected_argv = ('%s %s -avg_lts %s %s %s' + % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), 'avg_out.txt'), + one_file, two_file, three_file)) + + assert argv == expected_argv + + expected_cmd = ('%s --cmd_file %s' + % (get_custom_path('reg_average'), reg_average_cmd)) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_average')) +def test_reg_average_avg_ref(): + + # Create a reg_average object + nr = RegAverage() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_average') + + # Assign some input data + ref_file = example_data('anatomical.nii') + one_file = example_data('im1.nii') + two_file = example_data('im2.nii') + three_file = example_data('im3.nii') + trans1_file = example_data('roi01.nii') + trans2_file = example_data('roi02.nii') + trans3_file = example_data('roi03.nii') + nr.inputs.warp_files = [trans1_file, one_file, + trans2_file, two_file, + trans3_file, three_file] + nr.inputs.avg_ref_file = ref_file + nr.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f: + argv = f.read() + os.remove(reg_average_cmd) + + expected_argv = ('%s %s -avg_tran %s %s %s %s %s %s %s' + % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), 'avg_out.nii.gz'), + ref_file, trans1_file, one_file, trans2_file, two_file, + trans3_file, three_file)) + + assert argv == expected_argv + + expected_cmd = ('%s --cmd_file %s' + % (get_custom_path('reg_average'), reg_average_cmd)) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_average')) +def test_reg_average_demean3(): + + # Create a reg_average object + nr = RegAverage() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_average') + + # Assign some input data + ref_file = example_data('anatomical.nii') + one_file = example_data('im1.nii') + two_file = example_data('im2.nii') + three_file = example_data('im3.nii') + aff1_file = example_data('TransformParameters.0.txt') + aff2_file = example_data('ants_Affine.txt') + aff3_file = example_data('elastix.txt') + trans1_file = example_data('roi01.nii') + trans2_file = example_data('roi02.nii') + trans3_file = example_data('roi03.nii') + nr.inputs.warp_files = [aff1_file, trans1_file, one_file, + aff2_file, trans2_file, two_file, + aff3_file, trans3_file, three_file] + nr.inputs.demean3_ref_file = ref_file + nr.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f: + argv = f.read() + os.remove(reg_average_cmd) + + expected_argv = ('%s %s -demean3 %s %s %s %s %s %s %s %s %s %s' + % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), 'avg_out.nii.gz'), + ref_file, + aff1_file, trans1_file, one_file, + aff2_file, trans2_file, two_file, + aff3_file, trans3_file, three_file)) + + assert argv == expected_argv + + expected_cmd = ('%s --cmd_file %s' + % (get_custom_path('reg_average'), reg_average_cmd)) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py b/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py new file mode 100644 index 0000000000..d0ac31eee1 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py @@ -0,0 +1,45 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import no_niftyreg, get_custom_path, RegF3D +from nipype.testing import skipif, example_data +import os +import pytest + + +@skipif(no_niftyreg(cmd='reg_f3d')) +def test_reg_f3d(): + + # Create a reg_f3d object + nr = RegF3D() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_f3d') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + rmask_file = example_data('mask.nii') + nr.inputs.ref_file = ref_file + nr.inputs.flo_file = flo_file + nr.inputs.rmask_file = rmask_file + nr.inputs.omp_core_val = 4 + nr.inputs.vel_flag = True + nr.inputs.be_val = 0.1 + nr.inputs.le_val = 0.1 + + cmd_tmp = '{cmd} -be 0.100000 -cpp {cpp} -flo {flo} -le 0.100000 -omp 4 \ +-ref {ref} -res {res} -rmask {rmask} -vel' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_f3d'), + cpp=os.path.join(os.getcwd(), 'im2_cpp.nii.gz'), + flo=flo_file, + ref=ref_file, + res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), + rmask=rmask_file) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py b/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py new file mode 100644 index 0000000000..b3a1e55b2e --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py @@ -0,0 +1,100 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegJacobian) +from nipype.testing import skipif, example_data +import os +import pytest + + +@skipif(no_niftyreg(cmd='reg_jacobian')) +def test_reg_jacobian_jac(): + + # Create a reg_jacobian object + nr = RegJacobian() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_jacobian') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr.inputs.ref_file = ref_file + nr.inputs.trans_file = trans_file + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jac {jac}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_jacobian'), + ref=ref_file, + trans=trans_file, + jac=os.path.join(os.getcwd(), 'warpfield_jac.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_jacobian')) +def test_reg_jacobian_jac_m(): + + # Create a reg_jacobian object + nr = RegJacobian() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_jacobian') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr.inputs.ref_file = ref_file + nr.inputs.trans_file = trans_file + nr.inputs.type = 'jacM' + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jacM {jac}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_jacobian'), + ref=ref_file, + trans=trans_file, + jac=os.path.join(os.getcwd(), 'warpfield_jacM.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_jacobian')) +def test_reg_jacobian_jac_l(): + + # Create a reg_jacobian object + nr = RegJacobian() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_jacobian') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr.inputs.ref_file = ref_file + nr.inputs.trans_file = trans_file + nr.inputs.type = 'jacL' + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jacL {jac}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_jacobian'), + ref=ref_file, + trans=trans_file, + jac=os.path.join(os.getcwd(), 'warpfield_jacL.nii.gz')) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py b/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py new file mode 100644 index 0000000000..8fa1445946 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py @@ -0,0 +1,39 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegMeasure) +from nipype.testing import skipif, example_data +import pytest +import os + + +@skipif(no_niftyreg(cmd='reg_measure')) +def test_reg_measure(): + + # Create a reg_measure object + nr = RegMeasure() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_measure') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + nr.inputs.ref_file = ref_file + nr.inputs.flo_file = flo_file + nr.inputs.measure_type = 'lncc' + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -flo {flo} -lncc -omp 4 -out {out} -ref {ref}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_measure'), + flo=flo_file, + out=os.path.join(os.getcwd(), 'im2_lncc.txt'), + ref=ref_file) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py b/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py new file mode 100644 index 0000000000..e626528148 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py @@ -0,0 +1,79 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegResample) +from nipype.testing import skipif, example_data +import os +import pytest + + +@skipif(no_niftyreg(cmd='reg_resample')) +def test_reg_resample_res(): + + # Create a reg_resample object + nr = RegResample() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_resample') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + trans_file = example_data('warpfield.nii') + nr.inputs.ref_file = ref_file + nr.inputs.flo_file = flo_file + nr.inputs.trans_file = trans_file + nr.inputs.inter_val = 'LIN' + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -flo {flo} -inter 1 -omp 4 -ref {ref} -trans {trans} \ +-res {res}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_resample'), + flo=flo_file, + ref=ref_file, + trans=trans_file, + res=os.path.join(os.getcwd(), 'im2_res.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_resample')) +def test_reg_resample_blank(): + + # Create a reg_resample object + nr = RegResample() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_resample') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + trans_file = example_data('warpfield.nii') + nr.inputs.ref_file = ref_file + nr.inputs.flo_file = flo_file + nr.inputs.trans_file = trans_file + nr.inputs.type = 'blank' + nr.inputs.inter_val = 'LIN' + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -flo {flo} -inter 1 -omp 4 -ref {ref} -trans {trans} \ +-blank {blank}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_resample'), + flo=flo_file, + ref=ref_file, + trans=trans_file, + blank=os.path.join(os.getcwd(), 'im2_blank.nii.gz')) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py b/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py new file mode 100644 index 0000000000..d8b9553b6a --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py @@ -0,0 +1,63 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import no_niftyreg, get_custom_path, RegTools +from nipype.testing import skipif, example_data +import os +import pytest + + +@skipif(no_niftyreg(cmd='reg_tools')) +def test_reg_tools_mul(): + + # Create a reg_tools object + nr = RegTools() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_tools') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + in_file = example_data('im1.nii') + nr.inputs.in_file = in_file + nr.inputs.mul_val = 4 + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -in {in_file} -mul 4.0 -omp 4 -out {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_tools'), + in_file=in_file, + out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_tools')) +def test_reg_tools_iso(): + + # Create a reg_tools object + nr = RegTools() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_tools') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr.run() + + # Assign some input data + in_file = example_data('im1.nii') + nr.inputs.in_file = in_file + nr.inputs.iso_flag = True + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -in {in_file} -iso -omp 4 -out {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_tools'), + in_file=in_file, + out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py b/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py new file mode 100644 index 0000000000..97f9a9dbbc --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py @@ -0,0 +1,164 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegTransform) +from nipype.testing import skipif, example_data +import os + + +@skipif(no_niftyreg(cmd='reg_transform')) +def test_reg_transform_def(): + + # Create a reg_transform object + nr = RegTransform() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_transform') + + # Assign some input data + trans_file = example_data('warpfield.nii') + nr.inputs.def_input = trans_file + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -def {trans_file} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + trans_file=trans_file, + out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_transform')) +def test_reg_transform_def_ref(): + + # Create a reg_transform object + nr = RegTransform() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_transform') + + # Assign some input data + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr.inputs.ref1_file = ref_file + nr.inputs.def_input = trans_file + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -ref {ref_file} -omp 4 -def {trans_file} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + ref_file=ref_file, + trans_file=trans_file, + out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_transform')) +def test_reg_transform_comp_nii(): + + # Create a reg_transform object + nr = RegTransform() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_transform') + + # Assign some input data + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + trans2_file = example_data('anatomical.nii') + nr.inputs.ref1_file = ref_file + nr.inputs.comp_input2 = trans2_file + nr.inputs.comp_input = trans_file + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -ref {ref_file} -omp 4 -comp {trans1} {trans2} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + ref_file=ref_file, + trans1=trans_file, + trans2=trans2_file, + out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_transform')) +def test_reg_transform_comp_txt(): + + # Create a reg_transform object + nr = RegTransform() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_transform') + + # Assign some input data + aff1_file = example_data('ants_Affine.txt') + aff2_file = example_data('elastix.txt') + nr.inputs.comp_input2 = aff2_file + nr.inputs.comp_input = aff1_file + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -comp {aff1} {aff2} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + aff1=aff1_file, + aff2=aff2_file, + out_file=os.path.join(os.getcwd(), 'ants_Affine_trans.txt')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_transform')) +def test_reg_transform_comp(): + + # Create a reg_transform object + nr = RegTransform() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_transform') + + # Assign some input data + trans_file = example_data('warpfield.nii') + aff_file = example_data('elastix.txt') + nr.inputs.comp_input2 = trans_file + nr.inputs.comp_input = aff_file + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -comp {aff} {trans} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + aff=aff_file, + trans=trans_file, + out_file=os.path.join(os.getcwd(), 'elastix_trans.nii.gz')) + + assert nr.cmdline == expected_cmd + + +@skipif(no_niftyreg(cmd='reg_transform')) +def test_reg_transform_flirt(): + + # Create a reg_transform object + nr = RegTransform() + + # Check if the command is properly defined + assert nr.cmd == get_custom_path('reg_transform') + + # Assign some input data + aff_file = example_data('elastix.txt') + ref_file = example_data('im1.nii') + in_file = example_data('im2.nii') + nr.inputs.flirt_2_nr_input = (aff_file, ref_file, in_file) + nr.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -flirtAff2NR {aff} {ref} {in_file} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + aff=aff_file, + ref=ref_file, + in_file=in_file, + out_file=os.path.join(os.getcwd(), 'elastix_trans.txt')) + + assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_auto_NiftyRegCommand.py b/nipype/interfaces/niftyreg/tests/test_auto_NiftyRegCommand.py new file mode 100644 index 0000000000..c231aa52ac --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_NiftyRegCommand.py @@ -0,0 +1,23 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..base import NiftyRegCommand + + +def test_NiftyRegCommand_inputs(): + input_map = dict(args=dict(argstr='%s', + ), + environ=dict(nohash=True, + usedefault=True, + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + terminal_output=dict(nohash=True, + ), + ) + inputs = NiftyRegCommand.input_spec() + + for key, metadata in list(input_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(inputs.traits()[key], metakey) == value + diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py b/nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py new file mode 100644 index 0000000000..cc9ebfdaa2 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py @@ -0,0 +1,92 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..reg import RegAladin + + +def test_RegAladin_inputs(): + input_map = dict(aff_direct_flag=dict(argstr='-affDirect', + ), + aff_file=dict(argstr='-aff %s', + genfile=True, + ), + args=dict(argstr='%s', + ), + cog_flag=dict(argstr='-cog', + ), + environ=dict(nohash=True, + usedefault=True, + ), + flo_file=dict(argstr='-flo %s', + mandatory=True, + ), + flo_low_val=dict(argstr='-floLowThr %f', + ), + flo_up_val=dict(argstr='-floUpThr %f', + ), + fmask_file=dict(argstr='-fmask %s', + ), + gpuid_val=dict(argstr='-gpuid %i', + ), + i_val=dict(argstr='-pi %d', + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + in_aff_file=dict(argstr='-inaff %s', + ), + ln_val=dict(argstr='-ln %d', + ), + lp_val=dict(argstr='-lp %d', + ), + maxit_val=dict(argstr='-maxit %d', + ), + nac_flag=dict(argstr='-nac', + ), + nosym_flag=dict(argstr='-noSym', + ), + omp_core_val=dict(argstr='-omp %i', + ), + platform_val=dict(argstr='-platf %i', + ), + ref_file=dict(argstr='-ref %s', + mandatory=True, + ), + ref_low_val=dict(argstr='-refLowThr %f', + ), + ref_up_val=dict(argstr='-refUpThr %f', + ), + res_file=dict(argstr='-res %s', + genfile=True, + ), + rig_only_flag=dict(argstr='-rigOnly', + ), + rmask_file=dict(argstr='-rmask %s', + ), + smoo_f_val=dict(argstr='-smooF %f', + ), + smoo_r_val=dict(argstr='-smooR %f', + ), + terminal_output=dict(nohash=True, + ), + v_val=dict(argstr='-pv %d', + ), + verbosity_off_flag=dict(argstr='-voff', + ), + ) + inputs = RegAladin.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_RegAladin_outputs(): + output_map = dict(aff_file=dict(), + avg_output=dict(), + res_file=dict(), + ) + outputs = RegAladin.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py b/nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py new file mode 100644 index 0000000000..6a32598cc7 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py @@ -0,0 +1,71 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..regutils import RegAverage + + +def test_RegAverage_inputs(): + input_map = dict(args=dict(argstr='%s', + ), + avg_files=dict(argstr='-avg %s', + position=1, + sep=' ', + xor=['avg_lts_files', 'avg_ref_file', 'demean1_ref_file', 'demean2_ref_file', 'demean3_ref_file', 'warp_files'], + ), + avg_lts_files=dict(argstr='-avg_lts %s', + position=1, + sep=' ', + xor=['avg_files', 'avg_ref_file', 'demean1_ref_file', 'demean2_ref_file', 'demean3_ref_file', 'warp_files'], + ), + avg_ref_file=dict(argstr='-avg_tran %s', + position=1, + requires=['warp_files'], + xor=['avg_files', 'avg_lts_files', 'demean1_ref_file', 'demean2_ref_file', 'demean3_ref_file'], + ), + demean1_ref_file=dict(argstr='-demean1 %s', + position=1, + requires=['warp_files'], + xor=['avg_files', 'avg_lts_files', 'avg_ref_file', 'demean2_ref_file', 'demean3_ref_file'], + ), + demean2_ref_file=dict(argstr='-demean2 %s', + position=1, + requires=['warp_files'], + xor=['avg_files', 'avg_lts_files', 'avg_ref_file', 'demean1_ref_file', 'demean3_ref_file'], + ), + demean3_ref_file=dict(argstr='-demean3 %s', + position=1, + requires=['warp_files'], + xor=['avg_files', 'avg_lts_files', 'avg_ref_file', 'demean1_ref_file', 'demean2_ref_file'], + ), + environ=dict(nohash=True, + usedefault=True, + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + out_file=dict(argstr='%s', + genfile=True, + position=0, + ), + terminal_output=dict(nohash=True, + ), + warp_files=dict(argstr='%s', + position=-1, + sep=' ', + xor=['avg_files', 'avg_lts_files'], + ), + ) + inputs = RegAverage.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_RegAverage_outputs(): + output_map = dict(out_file=dict(), + ) + outputs = RegAverage.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py b/nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py new file mode 100644 index 0000000000..0f4483cbce --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py @@ -0,0 +1,142 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..reg import RegF3D + + +def test_RegF3D_inputs(): + input_map = dict(aff_file=dict(argstr='-aff %s', + ), + amc_flag=dict(argstr='-amc', + ), + args=dict(argstr='%s', + ), + be_val=dict(argstr='-be %f', + ), + cpp_file=dict(argstr='-cpp %s', + genfile=True, + ), + environ=dict(nohash=True, + usedefault=True, + ), + fbn2_val=dict(argstr='-fbn %d %d', + ), + fbn_val=dict(argstr='--fbn %d', + ), + flo_file=dict(argstr='-flo %s', + mandatory=True, + ), + flo_smooth_val=dict(argstr='-smooF %f', + ), + flwth2_thr_val=dict(argstr='-fLwTh %d %f', + ), + flwth_thr_val=dict(argstr='--fLwTh %f', + ), + fmask_file=dict(argstr='-fmask %s', + ), + fupth2_thr_val=dict(argstr='-fUpTh %d %f', + ), + fupth_thr_val=dict(argstr='--fUpTh %f', + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + incpp_file=dict(argstr='-incpp %s', + ), + jl_val=dict(argstr='-jl %f', + ), + kld2_flag=dict(argstr='-kld %d', + ), + kld_flag=dict(argstr='--kld', + ), + le_val=dict(argstr='-le %f', + ), + ln_val=dict(argstr='-ln %d', + ), + lncc2_val=dict(argstr='-lncc %d %f', + ), + lncc_val=dict(argstr='--lncc %f', + ), + lp_val=dict(argstr='-lp %d', + ), + maxit_val=dict(argstr='-maxit %d', + ), + nmi_flag=dict(argstr='--nmi', + ), + no_app_jl_flag=dict(argstr='-noAppJL', + ), + noconj_flag=dict(argstr='-noConj', + ), + nopy_flag=dict(argstr='-nopy', + ), + nox_flag=dict(argstr='-nox', + ), + noy_flag=dict(argstr='-noy', + ), + noz_flag=dict(argstr='-noz', + ), + omp_core_val=dict(argstr='-omp %i', + ), + pad_val=dict(argstr='-pad %f', + ), + pert_val=dict(argstr='-pert %d', + ), + rbn2_val=dict(argstr='-rbn %d %d', + ), + rbn_val=dict(argstr='--rbn %d', + ), + ref_file=dict(argstr='-ref %s', + mandatory=True, + ), + ref_smooth_val=dict(argstr='-smooR %f', + ), + res_file=dict(argstr='-res %s', + genfile=True, + ), + rlwth2_thr_val=dict(argstr='-rLwTh %d %f', + ), + rlwth_thr_val=dict(argstr='--rLwTh %f', + ), + rmask_file=dict(argstr='-rmask %s', + ), + rupth2_thr_val=dict(argstr='-rUpTh %d %f', + ), + rupth_thr_val=dict(argstr='--rUpTh %f', + ), + smooth_grad_val=dict(argstr='-smoothGrad %f', + ), + ssd2_flag=dict(argstr='-ssd %d', + ), + ssd_flag=dict(argstr='--ssd', + ), + sx_val=dict(argstr='-sx %f', + ), + sy_val=dict(argstr='-sy %f', + ), + sz_val=dict(argstr='-sz %f', + ), + terminal_output=dict(nohash=True, + ), + vel_flag=dict(argstr='-vel', + ), + verbosity_off_flag=dict(argstr='-voff', + ), + ) + inputs = RegF3D.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_RegF3D_outputs(): + output_map = dict(avg_output=dict(), + cpp_file=dict(), + invcpp_file=dict(), + invres_file=dict(), + res_file=dict(), + ) + outputs = RegF3D.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py b/nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py new file mode 100644 index 0000000000..badf703863 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py @@ -0,0 +1,47 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..regutils import RegJacobian + + +def test_RegJacobian_inputs(): + input_map = dict(args=dict(argstr='%s', + ), + environ=dict(nohash=True, + usedefault=True, + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + omp_core_val=dict(argstr='-omp %i', + ), + out_file=dict(argstr='%s', + genfile=True, + position=-1, + ), + ref_file=dict(argstr='-ref %s', + ), + terminal_output=dict(nohash=True, + ), + trans_file=dict(argstr='-trans %s', + mandatory=True, + ), + type=dict(argstr='-%s', + position=-2, + usedefault=True, + ), + ) + inputs = RegJacobian.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_RegJacobian_outputs(): + output_map = dict(out_file=dict(), + ) + outputs = RegJacobian.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py b/nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py new file mode 100644 index 0000000000..adc4e7e6e9 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py @@ -0,0 +1,46 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..regutils import RegMeasure + + +def test_RegMeasure_inputs(): + input_map = dict(args=dict(argstr='%s', + ), + environ=dict(nohash=True, + usedefault=True, + ), + flo_file=dict(argstr='-flo %s', + mandatory=True, + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + measure_type=dict(argstr='-%s', + mandatory=True, + ), + omp_core_val=dict(argstr='-omp %i', + ), + out_file=dict(argstr='-out %s', + genfile=True, + ), + ref_file=dict(argstr='-ref %s', + mandatory=True, + ), + terminal_output=dict(nohash=True, + ), + ) + inputs = RegMeasure.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_RegMeasure_outputs(): + output_map = dict(out_file=dict(), + ) + outputs = RegMeasure.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegResample.py b/nipype/interfaces/niftyreg/tests/test_auto_RegResample.py new file mode 100644 index 0000000000..e6fb2442eb --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegResample.py @@ -0,0 +1,62 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..regutils import RegResample + + +def test_RegResample_inputs(): + input_map = dict(args=dict(argstr='%s', + ), + environ=dict(nohash=True, + usedefault=True, + ), + flo_file=dict(argstr='-flo %s', + mandatory=True, + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + inter_val=dict(argstr='-inter %d', + ), + omp_core_val=dict(argstr='-omp %d', + ), + out_file=dict(argstr='%s', + genfile=True, + position=-1, + ), + pad_val=dict(argstr='-pad %f', + ), + psf_alg=dict(argstr='-psf_alg %d', + ), + psf_flag=dict(argstr='-psf', + ), + ref_file=dict(argstr='-ref %s', + mandatory=True, + ), + tensor_flag=dict(argstr='-tensor ', + ), + terminal_output=dict(nohash=True, + ), + trans_file=dict(argstr='-trans %s', + ), + type=dict(argstr='-%s', + position=-2, + usedefault=True, + ), + verbosity_off_flag=dict(argstr='-voff', + ), + ) + inputs = RegResample.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_RegResample_outputs(): + output_map = dict(out_file=dict(), + ) + outputs = RegResample.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegTools.py b/nipype/interfaces/niftyreg/tests/test_auto_RegTools.py new file mode 100644 index 0000000000..08c85363aa --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegTools.py @@ -0,0 +1,68 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..regutils import RegTools + + +def test_RegTools_inputs(): + input_map = dict(add_val=dict(argstr='-add %s', + ), + args=dict(argstr='%s', + ), + bin_flag=dict(argstr='-bin', + ), + chg_res_val=dict(argstr='-chgres %f %f %f', + ), + div_val=dict(argstr='-div %s', + ), + down_flag=dict(argstr='-down', + ), + environ=dict(nohash=True, + usedefault=True, + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + in_file=dict(argstr='-in %s', + mandatory=True, + ), + iso_flag=dict(argstr='-iso', + ), + mask_file=dict(argstr='-nan %s', + ), + mul_val=dict(argstr='-mul %s', + ), + noscl_flag=dict(argstr='-noscl', + ), + omp_core_val=dict(argstr='-omp %i', + ), + out_file=dict(argstr='-out %s', + genfile=True, + ), + rms_val=dict(argstr='-rms %s', + ), + smo_g_val=dict(argstr='-smoG %f %f %f', + ), + smo_s_val=dict(argstr='-smoS %f %f %f', + ), + sub_val=dict(argstr='-sub %s', + ), + terminal_output=dict(nohash=True, + ), + thr_val=dict(argstr='-thr %f', + ), + ) + inputs = RegTools.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_RegTools_outputs(): + output_map = dict(out_file=dict(), + ) + outputs = RegTools.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegTransform.py b/nipype/interfaces/niftyreg/tests/test_auto_RegTransform.py new file mode 100644 index 0000000000..67b8426129 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegTransform.py @@ -0,0 +1,98 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..regutils import RegTransform + + +def test_RegTransform_inputs(): + input_map = dict(aff_2_rig_input=dict(argstr='-aff2rig %s', + position=-2, + xor=['def_input', 'disp_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'flirt_2_nr_input'], + ), + args=dict(argstr='%s', + ), + comp_input=dict(argstr='-comp %s', + position=-3, + requires=['comp_input2'], + xor=['def_input', 'disp_input', 'flow_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + comp_input2=dict(argstr='%s', + position=-2, + ), + def_input=dict(argstr='-def %s', + position=-2, + xor=['disp_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + disp_input=dict(argstr='-disp %s', + position=-2, + xor=['def_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + environ=dict(nohash=True, + usedefault=True, + ), + flirt_2_nr_input=dict(argstr='-flirtAff2NR %s %s %s', + position=-2, + xor=['def_input', 'disp_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'aff_2_rig_input'], + ), + flow_input=dict(argstr='-flow %s', + position=-2, + xor=['def_input', 'disp_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + half_input=dict(argstr='-half %s', + position=-2, + xor=['def_input', 'disp_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + inv_aff_input=dict(argstr='-invAff %s', + position=-2, + xor=['def_input', 'disp_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + inv_nrr_input=dict(argstr='-invNrr %s %s', + position=-2, + xor=['def_input', 'disp_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'half_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + make_aff_input=dict(argstr='-makeAff %f %f %f %f %f %f %f %f %f %f %f %f', + position=-2, + xor=['def_input', 'disp_input', 'flow_input', 'comp_input', 'upd_s_form_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + omp_core_val=dict(argstr='-omp %i', + ), + out_file=dict(argstr='%s', + genfile=True, + position=-1, + ), + ref1_file=dict(argstr='-ref %s', + position=0, + ), + ref2_file=dict(argstr='-ref2 %s', + position=1, + requires=['ref1_file'], + ), + terminal_output=dict(nohash=True, + ), + upd_s_form_input=dict(argstr='-updSform %s', + position=-3, + requires=['upd_s_form_input2'], + xor=['def_input', 'disp_input', 'flow_input', 'comp_input', 'inv_aff_input', 'inv_nrr_input', 'half_input', 'make_aff_input', 'aff_2_rig_input', 'flirt_2_nr_input'], + ), + upd_s_form_input2=dict(argstr='%s', + position=-2, + requires=['upd_s_form_input'], + ), + ) + inputs = RegTransform.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_RegTransform_outputs(): + output_map = dict(out_file=dict(), + ) + outputs = RegTransform.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/setup.py b/nipype/interfaces/setup.py index 4c79456824..72f48f6039 100644 --- a/nipype/interfaces/setup.py +++ b/nipype/interfaces/setup.py @@ -19,14 +19,15 @@ def configuration(parent_package='', top_path=None): config.add_subpackage('elastix') config.add_subpackage('freesurfer') config.add_subpackage('fsl') + config.add_subpackage('minc') + config.add_subpackage('mipav') config.add_subpackage('mne') config.add_subpackage('mrtrix') config.add_subpackage('mrtrix3') + config.add_subpackage('niftyreg') config.add_subpackage('nipy') config.add_subpackage('spm') config.add_subpackage('slicer') - config.add_subpackage('minc') - config.add_subpackage('mipav') config.add_data_dir('script_templates') config.add_data_dir('tests') diff --git a/nipype/workflows/smri/__init__.py b/nipype/workflows/smri/__init__.py index 64030857a9..3359fbdc6c 100644 --- a/nipype/workflows/smri/__init__.py +++ b/nipype/workflows/smri/__init__.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import print_function, division, unicode_literals, absolute_import -from . import freesurfer from . import ants +from . import freesurfer +from . import niftyreg diff --git a/nipype/workflows/smri/niftyreg/__init__.py b/nipype/workflows/smri/niftyreg/__init__.py new file mode 100644 index 0000000000..b9d0c9c85b --- /dev/null +++ b/nipype/workflows/smri/niftyreg/__init__.py @@ -0,0 +1,5 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from .groupwise import (create_groupwise_average, create_nonlinear_gw_step, + create_linear_gw_step) diff --git a/nipype/workflows/smri/niftyreg/groupwise.py b/nipype/workflows/smri/niftyreg/groupwise.py new file mode 100644 index 0000000000..9e36d8451f --- /dev/null +++ b/nipype/workflows/smri/niftyreg/groupwise.py @@ -0,0 +1,385 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +''' +This file provides some common registration routines useful for a variety of +pipelines. + +Including linear and non-linear image co-registration +''' + +import nipype.interfaces.utility as niu +import nipype.interfaces.niftyreg as niftyreg +import nipype.pipeline.engine as pe + + +def create_linear_gw_step(name="linear_gw_niftyreg", + demean=True, + linear_options_hash=None, + use_mask=False, + verbose=False): + """ + Creates a workflow that perform linear co-registration of a set of images + using RegAladin, producing an average image and a set of affine + transformation matrices linking each of the floating images to the average. + + Inputs:: + + inputspec.in_files - The input files to be registered + inputspec.ref_file - The initial reference image that the input files + are registered to + inputspec.rmask_file - Mask of the reference image + inputspec.in_aff_files - Initial affine transformation files + + Outputs:: + + outputspec.average_image - The average image + outputspec.aff_files - The affine transformation files + + Optional arguments:: + linear_options_hash - An options dictionary containing a list of + parameters for RegAladin that take + the same form as given in the interface (default None) + demean - Selects whether to demean the transformation matrices when + performing the averaging (default True) + initial_affines - Selects whether to iterate over initial affine + images, which we generally won't have (default False) + + Example + ------- + + >>> from nipype.workflows.smri.niftyreg import create_linear_gw_step + >>> lgw = create_linear_gw_step('my_linear_coreg') + >>> lgw.inputs.inputspec.in_files = ['file1.nii.gz', 'file2.nii.gz'] \ +# doctest: +SKIP + >>> lgw.inputs.inputspec.ref_file = ['ref.nii.gz'] # doctest: +SKIP + >>> lgw.run() # doctest: +SKIP + + """ + # Create the sub workflow + workflow = pe.Workflow(name=name) + workflow.base_output_dir = name + + # We need to create an input node for the workflow + inputnode = pe.Node(niu.IdentityInterface( + fields=['in_files', 'ref_file', 'rmask_file']), + name='inputspec') + + if linear_options_hash is None: + linear_options_hash = dict() + + # Rigidly register each of the images to the average + lin_reg = pe.MapNode(interface=niftyreg.RegAladin(**linear_options_hash), + name="lin_reg", iterfield=['flo_file']) + + if verbose is False: + lin_reg.inputs.verbosity_off_flag = True + + # Average the images + ave_ims = pe.Node(interface=niftyreg.RegAverage(), name="ave_ims") + + # We have a new average image and the affine + # transformations, which are returned as an output node. + outputnode = pe.Node(niu.IdentityInterface( + fields=['average_image', 'trans_files']), name='outputspec') + + # Connect the inputs to the lin_reg node + workflow.connect([ + (inputnode, lin_reg, [('ref_file', 'ref_file')]), + (inputnode, lin_reg, [('in_files', 'flo_file')]) + ]) + if use_mask: + workflow.connect(inputnode, 'rmask_file', lin_reg, 'rmask_file') + + if demean: + workflow.connect([ + (inputnode, ave_ims, [('ref_file', 'demean1_ref_file')]), + (lin_reg, ave_ims, [('avg_output', 'warp_files')]) + ]) + else: + workflow.connect(lin_reg, 'res_file', ave_ims, 'avg_files') + + # Connect up the output node + workflow.connect([ + (lin_reg, outputnode, [('aff_file', 'trans_files')]), + (ave_ims, outputnode, [('out_file', 'average_image')]) + ]) + + return workflow + + +def create_nonlinear_gw_step(name="nonlinear_gw_niftyreg", + demean=True, + nonlinear_options_hash=None, + initial_affines=False, + use_mask=False, + verbose=False): + """ + Creates a workflow that perform non-linear co-registrations of a set of + images using RegF3d, producing an non-linear average image and a set of + cpp transformation linking each of the floating images to the average. + + Inputs:: + inputspec.in_files - The input files to be registered + inputspec.ref_file - The initial reference image that the input files + are registered to + inputspec.rmask_file - Mask of the reference image + inputspec.in_trans_files - Initial transformation files (affine or + cpps) + + Outputs:: + + outputspec.average_image - The average image + outputspec.cpp_files - The bspline transformation files + + Optional arguments:: + nonlinear_options_hash - An options dictionary containing a list of + parameters for RegAladin that take the + same form as given in the interface (default None) + initial_affines - Selects whether to iterate over initial affine + images, which we generally won't have (default False) + + Example + ------- + >>> from nipype.workflows.smri.niftyreg import create_nonlinear_gw_step + >>> nlc = create_nonlinear_gw_step('nonlinear_coreg') # doctest: +SKIP + >>> nlc.inputs.inputspec.in_files = ['file1.nii.gz', 'file2.nii.gz'] \ +# doctest: +SKIP + >>> nlc.inputs.inputspec.ref_file = ['ref.nii.gz'] # doctest: +SKIP + >>> nlc.run() # doctest: +SKIP + + """ + + # Create the workflow + workflow = pe.Workflow(name=name) + workflow.base_output_dir = name + + # We need to create an input node for the workflow + inputnode = pe.Node(niu.IdentityInterface( + fields=['in_files', + 'ref_file', + 'rmask_file', + 'input_aff_files']), + name='inputspec') + + if nonlinear_options_hash is None: + nonlinear_options_hash = dict() + + # non-rigidly register each of the images to the average + # flo_file can take a list of files + # Need to be able to iterate over input affine files, but what about the + # cases where we have no input affine files? + # Passing empty strings are not valid filenames, and undefined fields can + # not be iterated over. + # Current simple solution, as this is not generally required, is to use a + # flag which specifies wherther to iterate + if initial_affines: + nonlin_reg = pe.MapNode(interface=niftyreg.RegF3D( + **nonlinear_options_hash), name="nonlin_reg", + iterfield=['flo_file', 'aff_file']) + else: + nonlin_reg = pe.MapNode(interface=niftyreg.RegF3D( + **nonlinear_options_hash), name="nonlin_reg", + iterfield=['flo_file']) + + if verbose is False: + nonlin_reg.inputs.verbosity_off_flag = True + + # Average the images + ave_ims = pe.Node(interface=niftyreg.RegAverage(), name="ave_ims") + + # We have a new centered average image, the resampled original images and + # the affine transformations, which are returned as an output node. + outputnode = pe.Node(niu.IdentityInterface( + fields=['average_image', + 'trans_files']), + name='outputspec') + + # Connect the inputs to the lin_reg node, which is split over in_files + workflow.connect([ + (inputnode, nonlin_reg, [('in_files', 'flo_file')]), + (inputnode, nonlin_reg, [('ref_file', 'ref_file')]) + ]) + + if use_mask: + workflow.connect(inputnode, 'rmask_file', nonlin_reg, 'rmask_file') + + # If we have initial affine transforms, we need to connect them in + if initial_affines: + workflow.connect(inputnode, 'input_aff_files', nonlin_reg, 'aff_file') + + if demean: + if 'vel_flag' in nonlinear_options_hash.keys() and \ + nonlinear_options_hash['vel_flag'] is True and \ + initial_affines: + workflow.connect( + inputnode, 'ref_file', ave_ims, 'demean3_ref_file') + else: + workflow.connect( + inputnode, 'ref_file', ave_ims, 'demean2_ref_file') + workflow.connect(nonlin_reg, 'avg_output', ave_ims, 'warp_files') + else: + workflow.connect(nonlin_reg, 'res_file', ave_ims, 'avg_files') + + # Connect up the output node + workflow.connect([ + (nonlin_reg, outputnode, [('cpp_file', 'trans_files')]), + (ave_ims, outputnode, [('out_file', 'average_image')]) + ]) + + return workflow + + +# Creates an atlas image by iterative registration. An initial reference image +# can be provided, otherwise one will be made. +def create_groupwise_average(name="atlas_creation", + itr_rigid=3, + itr_affine=3, + itr_non_lin=5, + linear_options_hash=None, + nonlinear_options_hash=None, + use_mask=False, + verbose=False): + """ + Create the overall workflow that embeds all the rigid, affine and + non-linear components. + + Inputs:: + inputspec.in_files - The input files to be registered + inputspec.ref_file - The initial reference image that the input files + are registered to + inputspec.rmask_file - Mask of the reference image + inputspec.in_trans_files - Initial transformation files (affine or + cpps) + + Outputs:: + + outputspec.average_image - The average image + outputspec.cpp_files - The bspline transformation files + + Example + ------- + >>> from nipype.workflows.smri.niftyreg import create_groupwise_average + >>> node = create_groupwise_average('groupwise_av') # doctest: +SKIP + >>> node.inputs.inputspec.in_files = ['file1.nii.gz', 'file2.nii.gz'] \ +# doctest: +SKIP + >>> node.inputs.inputspec.ref_file = ['ref.nii.gz'] # doctest: +SKIP + >>> node.inputs.inputspec.rmask_file = ['mask.nii.gz'] # doctest: +SKIP + >>> node.run() # doctest: +SKIP + + """ + # Create workflow + workflow = pe.Workflow(name=name) + + if linear_options_hash is None: + linear_options_hash = dict() + + if nonlinear_options_hash is None: + nonlinear_options_hash = dict() + + # Create the input and output node + inputnode = pe.Node(niu.IdentityInterface( + fields=['in_files', + 'ref_file', + 'rmask_file']), + name='inputspec') + + outputnode = pe.Node(niu.IdentityInterface( + fields=['average_image', + 'trans_files']), + name='outputspec') + + # Create lists to store the rigid, affine and non-linear sub-workflow + lin_workflows = [] + nonlin_workflows = [] + + # Create the linear groupwise registration sub-workflows + for i in range(itr_rigid + itr_affine): + # Define is the sub-workflow is rigid or affine + if i >= itr_rigid: + linear_options_hash['rig_only_flag'] = False + else: + linear_options_hash['rig_only_flag'] = True + + # Define if the average image should be demean to ensure we have a + # barycenter + if (i < itr_rigid) or (i == (itr_rigid + itr_affine - 1)): + demean_arg = False + else: + demean_arg = True + + # Create the rigid or affine sub-workflow and add it to the relevant + # list + wf = create_linear_gw_step(name='lin_reg' + str(i), + linear_options_hash=linear_options_hash, + demean=demean_arg, verbose=verbose) + lin_workflows.append(wf) + + # Connect up the input data to the workflow + workflow.connect(inputnode, 'in_files', wf, 'inputspec.in_files') + if use_mask: + workflow.connect( + inputnode, 'rmask_file', wf, 'inputspec.rmask_file') + # If it exist, connect the previous workflow to the current one + if i == 0: + workflow.connect(inputnode, 'ref_file', wf, 'inputspec.ref_file') + else: + workflow.connect(lin_workflows[i - 1], 'outputspec.average_image', + wf, 'inputspec.ref_file') + + demean_arg = True + + # Create the nonlinear groupwise registration sub-workflows + for i in range(itr_non_lin): + + if len(lin_workflows) > 0: + initial_affines_arg = True + if i == (itr_non_lin - 1): + demean_arg = False + + wf = create_nonlinear_gw_step( + name='nonlin' + str(i), demean=demean_arg, + initial_affines=initial_affines_arg, + nonlinear_options_hash=nonlinear_options_hash, verbose=verbose) + + # Connect up the input data to the workflows + workflow.connect(inputnode, 'in_files', wf, 'inputspec.in_files') + if use_mask: + workflow.connect( + inputnode, 'rmask_file', wf, 'inputspec.rmask_file') + + if initial_affines_arg: + # Take the final linear registration results and use them to + # initialise the NR + workflow.connect(lin_workflows[-1], 'outputspec.trans_files', + wf, 'inputspec.input_aff_files') + + if i == 0: + if len(lin_workflows) > 0: + workflow.connect( + lin_workflows[-1], 'outputspec.average_image', + wf, 'inputspec.ref_file') + else: + workflow.connect(inputnode, 'ref_file', + wf, 'inputspec.ref_file') + else: + workflow.connect( + nonlin_workflows[i - 1], 'outputspec.average_image', + wf, 'inputspec.ref_file') + + nonlin_workflows.append(wf) + + # Set up the last workflow + lw = None + if len(nonlin_workflows) > 0: + lw = nonlin_workflows[-1] + elif len(lin_workflows) > 0: + lw = lin_workflows[-1] + + # Connect the data to return + workflow.connect([ + (lw, outputnode, [('outputspec.average_image', 'average_image')]), + (lw, outputnode, [('outputspec.trans_files', 'trans_files')]) + ]) + + return workflow From 62134cfbca78272747bcdf150ceeb451d9811af3 Mon Sep 17 00:00:00 2001 From: Marc Modat Date: Wed, 29 Mar 2017 12:20:47 +0100 Subject: [PATCH 02/20] Modifications towards python3 --- nipype/interfaces/niftyreg/__init__.py | 1 + nipype/interfaces/niftyreg/base.py | 9 ++++++-- nipype/interfaces/niftyreg/reg.py | 4 ++++ nipype/interfaces/niftyreg/regutils.py | 4 ++++ nipype/interfaces/niftyreg/tests/__init__.py | 1 + .../niftyreg/tests/test_Reg_Aladin.py | 1 + .../niftyreg/tests/test_Reg_Average.py | 21 ++++++++++--------- .../interfaces/niftyreg/tests/test_Reg_F3D.py | 1 + .../niftyreg/tests/test_Reg_Jacobian.py | 1 + .../niftyreg/tests/test_Reg_Measure.py | 1 + .../niftyreg/tests/test_Reg_Resample.py | 1 + .../niftyreg/tests/test_Reg_Tools.py | 1 + .../niftyreg/tests/test_Reg_Transform.py | 1 + 13 files changed, 35 insertions(+), 12 deletions(-) diff --git a/nipype/interfaces/niftyreg/__init__.py b/nipype/interfaces/niftyreg/__init__.py index 9981262fb5..2d54870a9d 100644 --- a/nipype/interfaces/niftyreg/__init__.py +++ b/nipype/interfaces/niftyreg/__init__.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index b7e443e484..05112dc54d 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: @@ -16,6 +17,9 @@ """ +from __future__ import print_function, division, \ + unicode_literals, absolute_import +from builtins import property, super from distutils.version import StrictVersion import os import subprocess @@ -82,7 +86,7 @@ def get_version(self): if no_niftyreg(cmd=self.cmd): return None exec_cmd = ''.join((self.cmd, ' -v')) - return subprocess.check_output(exec_cmd, shell=True).strip('\n') + return subprocess.check_output(exec_cmd, shell=True).strip() @property def version(self): @@ -110,7 +114,8 @@ def _gen_fname(self, basename, out_dir=None, suffix=None, ext=None): def _gen_filename(self, name): if name == 'out_file': return self._gen_fname(self.inputs.in_file, - suffix=self._suffix, ext='.nii.gz') + suffix=self._suffix, + ext='.nii.gz') return None def _list_outputs(self): diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 55afc80794..e7067e83a9 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: @@ -8,6 +9,9 @@ The interfaces were written to work with niftyreg version 1.5.10 """ +from __future__ import print_function, division, \ + unicode_literals, absolute_import +from builtins import staticmethod import os import warnings diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index 2b40b97621..67c23eb3a1 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: """The regutils module provides classes for interfacing with the `niftyreg @@ -5,6 +6,9 @@ The interfaces were written to work with niftyreg version 1.4 """ +from __future__ import print_function, division, \ + unicode_literals, absolute_import +from builtins import len, open, property, super import warnings import os diff --git a/nipype/interfaces/niftyreg/tests/__init__.py b/nipype/interfaces/niftyreg/tests/__init__.py index e69de29bb2..40a96afc6f 100644 --- a/nipype/interfaces/niftyreg/tests/__init__.py +++ b/nipype/interfaces/niftyreg/tests/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py b/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py index 5a42a72a2a..d0f0311e09 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Average.py b/nipype/interfaces/niftyreg/tests/test_Reg_Average.py index 8bebbc4e09..1d983da902 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Average.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Average.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: @@ -21,7 +22,7 @@ def test_reg_average_avg_nii(): two_file = example_data('im2.nii') three_file = example_data('im3.nii') nr.inputs.avg_files = [one_file, two_file, three_file] - nr.cmdline + generated_cmd = nr.cmdline # Read the reg_average_cmd reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') @@ -39,7 +40,7 @@ def test_reg_average_avg_nii(): expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) - assert nr.cmdline == expected_cmd + assert generated_cmd == expected_cmd @skipif(no_niftyreg(cmd='reg_average')) @@ -56,7 +57,7 @@ def test_reg_average_avg_txt(): two_file = example_data('ants_Affine.txt') three_file = example_data('elastix.txt') nr.inputs.avg_files = [one_file, two_file, three_file] - nr.cmdline + generated_cmd = nr.cmdline # Read the reg_average_cmd reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') @@ -74,7 +75,7 @@ def test_reg_average_avg_txt(): expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) - assert nr.cmdline == expected_cmd + assert generated_cmd == expected_cmd @skipif(no_niftyreg(cmd='reg_average')) @@ -91,7 +92,7 @@ def test_reg_average_avg_lts(): two_file = example_data('ants_Affine.txt') three_file = example_data('elastix.txt') nr.inputs.avg_lts_files = [one_file, two_file, three_file] - nr.cmdline + generated_cmd = nr.cmdline # Read the reg_average_cmd reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') @@ -109,7 +110,7 @@ def test_reg_average_avg_lts(): expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) - assert nr.cmdline == expected_cmd + assert generated_cmd == expected_cmd @skipif(no_niftyreg(cmd='reg_average')) @@ -133,7 +134,7 @@ def test_reg_average_avg_ref(): trans2_file, two_file, trans3_file, three_file] nr.inputs.avg_ref_file = ref_file - nr.cmdline + generated_cmd = nr.cmdline # Read the reg_average_cmd reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') @@ -152,7 +153,7 @@ def test_reg_average_avg_ref(): expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) - assert nr.cmdline == expected_cmd + assert generated_cmd == expected_cmd @skipif(no_niftyreg(cmd='reg_average')) @@ -179,7 +180,7 @@ def test_reg_average_demean3(): aff2_file, trans2_file, two_file, aff3_file, trans3_file, three_file] nr.inputs.demean3_ref_file = ref_file - nr.cmdline + generated_cmd = nr.cmdline # Read the reg_average_cmd reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') @@ -200,4 +201,4 @@ def test_reg_average_demean3(): expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) - assert nr.cmdline == expected_cmd + assert generated_cmd == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py b/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py index d0ac31eee1..35e1b53383 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py b/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py index b3a1e55b2e..396baa6762 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py b/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py index 8fa1445946..3d8483f21b 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py b/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py index e626528148..7909a4ce15 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py b/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py index d8b9553b6a..7b3e019738 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py b/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py index 97f9a9dbbc..e95a6da371 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: From 44d01b639f89948009ccabe20bee5042bbe44191 Mon Sep 17 00:00:00 2001 From: byvernault Date: Wed, 29 Mar 2017 14:39:06 +0100 Subject: [PATCH 03/20] Fixing bytes data to string for comparaison for python 3 compatibility --- nipype/interfaces/niftyreg/base.py | 8 ++++---- nipype/interfaces/niftyreg/tests/test_Reg_Average.py | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 05112dc54d..45d0333f42 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -17,9 +17,9 @@ """ -from __future__ import print_function, division, \ - unicode_literals, absolute_import -from builtins import property, super +from __future__ import (print_function, division, unicode_literals, + absolute_import) +from builtins import property, super, str from distutils.version import StrictVersion import os import subprocess @@ -72,7 +72,7 @@ class NiftyRegCommand(CommandLine): def __init__(self, required_version=None, **inputs): super(NiftyRegCommand, self).__init__(**inputs) - cur_version = self.get_version() + cur_version = self.get_version().decode("utf-8") if StrictVersion(cur_version) < StrictVersion(self._min_version): err = 'A later version of Niftyreg is required (%s < %s)' raise ValueError(err % (cur_version, self._min_version)) diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Average.py b/nipype/interfaces/niftyreg/tests/test_Reg_Average.py index 1d983da902..4fc06ef5b7 100644 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Average.py +++ b/nipype/interfaces/niftyreg/tests/test_Reg_Average.py @@ -35,7 +35,7 @@ def test_reg_average_avg_nii(): 'avg_out.nii.gz'), one_file, two_file, three_file) - assert argv == expected_argv + assert argv.decode('utf-8') == expected_argv expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) @@ -70,7 +70,7 @@ def test_reg_average_avg_txt(): 'avg_out.txt'), one_file, two_file, three_file) - assert argv == expected_argv + assert argv.decode('utf-8') == expected_argv expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) @@ -105,7 +105,7 @@ def test_reg_average_avg_lts(): os.path.join(os.getcwd(), 'avg_out.txt'), one_file, two_file, three_file)) - assert argv == expected_argv + assert argv.decode('utf-8') == expected_argv expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) @@ -148,7 +148,7 @@ def test_reg_average_avg_ref(): ref_file, trans1_file, one_file, trans2_file, two_file, trans3_file, three_file)) - assert argv == expected_argv + assert argv.decode('utf-8') == expected_argv expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) @@ -196,7 +196,7 @@ def test_reg_average_demean3(): aff2_file, trans2_file, two_file, aff3_file, trans3_file, three_file)) - assert argv == expected_argv + assert argv.decode('utf-8') == expected_argv expected_cmd = ('%s --cmd_file %s' % (get_custom_path('reg_average'), reg_average_cmd)) From 37411dda5597636d2a968a27863ecb9d8551a1cf Mon Sep 17 00:00:00 2001 From: byvernault Date: Wed, 29 Mar 2017 14:50:59 +0100 Subject: [PATCH 04/20] Editing the workflow to be compatible python 2.7/3.x --- nipype/workflows/smri/niftyreg/groupwise.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nipype/workflows/smri/niftyreg/groupwise.py b/nipype/workflows/smri/niftyreg/groupwise.py index 9e36d8451f..5a8c8cdc86 100644 --- a/nipype/workflows/smri/niftyreg/groupwise.py +++ b/nipype/workflows/smri/niftyreg/groupwise.py @@ -8,6 +8,7 @@ Including linear and non-linear image co-registration ''' +from builtins import str, range import nipype.interfaces.utility as niu import nipype.interfaces.niftyreg as niftyreg import nipype.pipeline.engine as pe @@ -209,7 +210,7 @@ def create_nonlinear_gw_step(name="nonlinear_gw_niftyreg", workflow.connect(inputnode, 'input_aff_files', nonlin_reg, 'aff_file') if demean: - if 'vel_flag' in nonlinear_options_hash.keys() and \ + if 'vel_flag' in list(nonlinear_options_hash.keys()) and \ nonlinear_options_hash['vel_flag'] is True and \ initial_affines: workflow.connect( From 9e754566476624d6cbf33b5d4abdf1274e9b31a1 Mon Sep 17 00:00:00 2001 From: Benjamin Yvernault Date: Wed, 29 Mar 2017 16:36:52 +0100 Subject: [PATCH 05/20] Edit version control Raising Exception if Niftyreg not found --- nipype/interfaces/niftyreg/base.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 45d0333f42..7c5c604fa5 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -72,7 +72,11 @@ class NiftyRegCommand(CommandLine): def __init__(self, required_version=None, **inputs): super(NiftyRegCommand, self).__init__(**inputs) - cur_version = self.get_version().decode("utf-8") + cur_version = self.get_version() + if not cur_version: + raise Exception('Niftyreg not found') + # Decoding to string: + cur_version = cur_version.decode("utf-8") if StrictVersion(cur_version) < StrictVersion(self._min_version): err = 'A later version of Niftyreg is required (%s < %s)' raise ValueError(err % (cur_version, self._min_version)) From b9e37e400b7738492d78b4e952c42fa93f0249e4 Mon Sep 17 00:00:00 2001 From: byvernault Date: Thu, 30 Mar 2017 09:37:59 +0100 Subject: [PATCH 06/20] Skipping some test in docstring --- nipype/interfaces/niftyreg/reg.py | 2 +- nipype/interfaces/niftyreg/regutils.py | 28 ++++++++++----------- nipype/workflows/smri/niftyreg/groupwise.py | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index e7067e83a9..652e0b28f9 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -144,7 +144,7 @@ class RegAladin(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegAladin - >>> node = RegAladin() + >>> node = RegAladin() # doctest: +SKIP >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP >>> node.inputs.rmask_file = 'rmask.nii.gz' # doctest: +SKIP diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index 67c23eb3a1..8bd528cf66 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -95,12 +95,12 @@ class RegResample(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegResample - >>> node = RegResample() + >>> node = RegResample() # doctest: +SKIP >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP >>> node.inputs.trans_file = 'warpfield.nii.gz' # doctest: +SKIP - >>> node.inputs.inter_val = 'LIN' - >>> node.inputs.omp_core_val = 4 + >>> node.inputs.inter_val = 'LIN' # doctest: +SKIP + >>> node.inputs.omp_core_val = 4 # doctest: +SKIP >>> node.cmdline # doctest: +SKIP 'reg_resample -flo flo.nii.gz -inter 1 -omp 4 -ref ref.nii.gz -trans \ warpfield.nii.gz -res flo_res.nii.gz' @@ -168,10 +168,10 @@ class RegJacobian(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegJacobian - >>> node = RegJacobian() + >>> node = RegJacobian() # doctest: +SKIP >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP >>> node.inputs.trans_file = 'warpfield.nii.gz' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 + >>> node.inputs.omp_core_val = 4 # doctest: +SKIP >>> node.cmdline # doctest: +SKIP 'reg_jacobian -omp 4 -ref ref.nii.gz -trans warpfield.nii.gz -jac \ warpfield_jac.nii.gz' @@ -289,10 +289,10 @@ class RegTools(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegTools - >>> node = RegTools() + >>> node = RegTools() # doctest: +SKIP >>> node.inputs.in_file = 'im1.nii.gz' # doctest: +SKIP - >>> node.inputs.mul_val = 4 - >>> node.inputs.omp_core_val = 4 + >>> node.inputs.mul_val = 4 # doctest: +SKIP + >>> node.inputs.omp_core_val = 4 # doctest: +SKIP >>> node.cmdline # doctest: +SKIP 'reg_tools -in im1.nii.gz -mul 4.0 -omp 4 -out im1_tools.nii.gz' @@ -396,7 +396,7 @@ class RegAverage(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegAverage - >>> node = RegAverage() + >>> node = RegAverage() # doctest: +SKIP >>> one_file = 'im1.nii' >>> two_file = 'im2.nii' >>> three_file = 'im3.nii' @@ -598,9 +598,9 @@ class RegTransform(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegResample - >>> node = RegTransform() + >>> node = RegTransform() # doctest: +SKIP >>> node.inputs.def_input = 'warpfield.nii' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 + >>> node.inputs.omp_core_val = 4 # doctest: +SKIP >>> node.cmdline # doctest: +SKIP 'reg_transform -omp 4 -def warpfield.nii warpfield_trans.nii.gz' @@ -710,11 +710,11 @@ class RegMeasure(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegMeasure - >>> node = RegMeasure() + >>> node = RegMeasure() # doctest: +SKIP >>> node.inputs.ref_file = 'im1.nii' # doctest: +SKIP >>> node.inputs.flo_file = 'im2.nii' # doctest: +SKIP - >>> node.inputs.measure_type = 'lncc' - >>> node.inputs.omp_core_val = 4 + >>> node.inputs.measure_type = 'lncc' # doctest: +SKIP + >>> node.inputs.omp_core_val = 4 # doctest: +SKIP >>> node.cmdline # doctest: +SKIP 'reg_measure -flo {flo} -lncc -omp 4 -out {out} -ref {ref}' diff --git a/nipype/workflows/smri/niftyreg/groupwise.py b/nipype/workflows/smri/niftyreg/groupwise.py index 5a8c8cdc86..125a017866 100644 --- a/nipype/workflows/smri/niftyreg/groupwise.py +++ b/nipype/workflows/smri/niftyreg/groupwise.py @@ -50,7 +50,7 @@ def create_linear_gw_step(name="linear_gw_niftyreg", ------- >>> from nipype.workflows.smri.niftyreg import create_linear_gw_step - >>> lgw = create_linear_gw_step('my_linear_coreg') + >>> lgw = create_linear_gw_step('my_linear_coreg') # doctest: +SKIP >>> lgw.inputs.inputspec.in_files = ['file1.nii.gz', 'file2.nii.gz'] \ # doctest: +SKIP >>> lgw.inputs.inputspec.ref_file = ['ref.nii.gz'] # doctest: +SKIP From 403947bad89c47360f1f952f6ad491dd5b423f8e Mon Sep 17 00:00:00 2001 From: Benjamin Yvernault Date: Thu, 30 Mar 2017 10:04:09 +0100 Subject: [PATCH 07/20] Adding # doctest: +SKIP --- nipype/interfaces/niftyreg/reg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 652e0b28f9..6c10d9606c 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -148,7 +148,7 @@ class RegAladin(NiftyRegCommand): >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP >>> node.inputs.rmask_file = 'rmask.nii.gz' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 + >>> node.inputs.omp_core_val = 4 # doctest: +SKIP >>> node.cmdline # doctest: +SKIP 'reg_aladin -aff flo_aff.txt -flo flo.nii.gz -omp 4 -ref ref.nii.gz \ -rmask rmask.nii.gz -res flo_res.nii.gz' @@ -374,11 +374,11 @@ class RegF3D(NiftyRegCommand): Examples -------- >>> from nipype.interfaces.niftyreg import RegF3D - >>> node = RegF3D() + >>> node = RegF3D() # doctest: +SKIP >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP >>> node.inputs.rmask_file = 'rmask.nii.gz' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 + >>> node.inputs.omp_core_val = 4 # doctest: +SKIP >>> node.cmdline # doctest: +SKIP 'reg_f3d -be 0.100000 -cpp flo_cpp.nii.gz -flo flo.nii.gz -le 0.100000 \ -omp 4 -ref ref.nii.gz -res flo_res.nii.gz -rmask rmask.nii.gz -vel' From 33ebab8ac96fd7de3a6887cfb45d1cf0a1f329e3 Mon Sep 17 00:00:00 2001 From: Benjamin Yvernault Date: Fri, 31 Mar 2017 16:43:35 +0200 Subject: [PATCH 08/20] Editing docstring to follow nipype template and add some tests. --- nipype/interfaces/niftyreg/base.py | 2 +- nipype/interfaces/niftyreg/reg.py | 6 + nipype/interfaces/niftyreg/regutils.py | 10 +- .../niftyreg/tests/test_Reg_Aladin.py | 44 -- .../niftyreg/tests/test_Reg_Average.py | 204 -------- .../interfaces/niftyreg/tests/test_Reg_F3D.py | 46 -- .../niftyreg/tests/test_Reg_Jacobian.py | 101 ---- .../niftyreg/tests/test_Reg_Measure.py | 40 -- .../niftyreg/tests/test_Reg_Resample.py | 80 --- .../niftyreg/tests/test_Reg_Tools.py | 64 --- .../niftyreg/tests/test_Reg_Transform.py | 165 ------- nipype/interfaces/niftyreg/tests/test_reg.py | 86 ++++ .../niftyreg/tests/test_regutils.py | 463 ++++++++++++++++++ 13 files changed, 565 insertions(+), 746 deletions(-) delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Average.py delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_F3D.py delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Measure.py delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Resample.py delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Tools.py delete mode 100644 nipype/interfaces/niftyreg/tests/test_Reg_Transform.py create mode 100644 nipype/interfaces/niftyreg/tests/test_reg.py create mode 100644 nipype/interfaces/niftyreg/tests/test_regutils.py diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 7c5c604fa5..46b2cf126b 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -19,7 +19,7 @@ from __future__ import (print_function, division, unicode_literals, absolute_import) -from builtins import property, super, str +from builtins import property, super from distutils.version import StrictVersion import os import subprocess diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 6c10d9606c..6fb730ac03 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -7,6 +7,12 @@ `_ registration command line tools. The interfaces were written to work with niftyreg version 1.5.10 + +Change directory to provide relative paths for doctests + >>> import os + >>> filepath = os.path.dirname( os.path.realpath( __file__ ) ) + >>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data')) + >>> os.chdir(datadir) """ from __future__ import print_function, division, \ diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index 8bd528cf66..217be8c9fb 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -1,9 +1,17 @@ # -*- coding: utf-8 -*- # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: + """The regutils module provides classes for interfacing with the `niftyreg `_ utility command line tools. -The interfaces were written to work with niftyreg version 1.4 + +The interfaces were written to work with niftyreg version 1.5.10 + +Change directory to provide relative paths for doctests + >>> import os + >>> filepath = os.path.dirname( os.path.realpath( __file__ ) ) + >>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data')) + >>> os.chdir(datadir) """ from __future__ import print_function, division, \ diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py b/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py deleted file mode 100644 index d0f0311e09..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Aladin.py +++ /dev/null @@ -1,44 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, - RegAladin) -from nipype.testing import skipif, example_data -import os -import pytest - - -@skipif(no_niftyreg(cmd='reg_aladin')) -def test_reg_aladin(): - - # Create a reg_aladin object - nr = RegAladin() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_aladin') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - flo_file = example_data('im2.nii') - rmask_file = example_data('mask.nii') - nr.inputs.ref_file = ref_file - nr.inputs.flo_file = flo_file - nr.inputs.rmask_file = rmask_file - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -aff {aff} -flo {flo} -omp 4 -ref {ref} -res {res} \ --rmask {rmask}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_aladin'), - aff=os.path.join(os.getcwd(), 'im2_aff.txt'), - flo=flo_file, - ref=ref_file, - res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), - rmask=rmask_file) - - assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Average.py b/nipype/interfaces/niftyreg/tests/test_Reg_Average.py deleted file mode 100644 index 4fc06ef5b7..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Average.py +++ /dev/null @@ -1,204 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, - RegAverage) -from nipype.testing import skipif, example_data -import os - - -@skipif(no_niftyreg(cmd='reg_average')) -def test_reg_average_avg_nii(): - - # Create a reg_average object - nr = RegAverage() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_average') - - # Assign some input data - one_file = example_data('im1.nii') - two_file = example_data('im2.nii') - three_file = example_data('im3.nii') - nr.inputs.avg_files = [one_file, two_file, three_file] - generated_cmd = nr.cmdline - - # Read the reg_average_cmd - reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') - with open(reg_average_cmd, 'rb') as f: - argv = f.read() - os.remove(reg_average_cmd) - - expected_argv = '%s %s -avg %s %s %s' % (get_custom_path('reg_average'), - os.path.join(os.getcwd(), - 'avg_out.nii.gz'), - one_file, two_file, three_file) - - assert argv.decode('utf-8') == expected_argv - - expected_cmd = ('%s --cmd_file %s' - % (get_custom_path('reg_average'), reg_average_cmd)) - - assert generated_cmd == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_average')) -def test_reg_average_avg_txt(): - - # Create a reg_average object - nr = RegAverage() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_average') - - # Assign some input data - one_file = example_data('TransformParameters.0.txt') - two_file = example_data('ants_Affine.txt') - three_file = example_data('elastix.txt') - nr.inputs.avg_files = [one_file, two_file, three_file] - generated_cmd = nr.cmdline - - # Read the reg_average_cmd - reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') - with open(reg_average_cmd, 'rb') as f: - argv = f.read() - os.remove(reg_average_cmd) - - expected_argv = '%s %s -avg %s %s %s' % (get_custom_path('reg_average'), - os.path.join(os.getcwd(), - 'avg_out.txt'), - one_file, two_file, three_file) - - assert argv.decode('utf-8') == expected_argv - - expected_cmd = ('%s --cmd_file %s' - % (get_custom_path('reg_average'), reg_average_cmd)) - - assert generated_cmd == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_average')) -def test_reg_average_avg_lts(): - - # Create a reg_average object - nr = RegAverage() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_average') - - # Assign some input data - one_file = example_data('TransformParameters.0.txt') - two_file = example_data('ants_Affine.txt') - three_file = example_data('elastix.txt') - nr.inputs.avg_lts_files = [one_file, two_file, three_file] - generated_cmd = nr.cmdline - - # Read the reg_average_cmd - reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') - with open(reg_average_cmd, 'rb') as f: - argv = f.read() - os.remove(reg_average_cmd) - - expected_argv = ('%s %s -avg_lts %s %s %s' - % (get_custom_path('reg_average'), - os.path.join(os.getcwd(), 'avg_out.txt'), - one_file, two_file, three_file)) - - assert argv.decode('utf-8') == expected_argv - - expected_cmd = ('%s --cmd_file %s' - % (get_custom_path('reg_average'), reg_average_cmd)) - - assert generated_cmd == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_average')) -def test_reg_average_avg_ref(): - - # Create a reg_average object - nr = RegAverage() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_average') - - # Assign some input data - ref_file = example_data('anatomical.nii') - one_file = example_data('im1.nii') - two_file = example_data('im2.nii') - three_file = example_data('im3.nii') - trans1_file = example_data('roi01.nii') - trans2_file = example_data('roi02.nii') - trans3_file = example_data('roi03.nii') - nr.inputs.warp_files = [trans1_file, one_file, - trans2_file, two_file, - trans3_file, three_file] - nr.inputs.avg_ref_file = ref_file - generated_cmd = nr.cmdline - - # Read the reg_average_cmd - reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') - with open(reg_average_cmd, 'rb') as f: - argv = f.read() - os.remove(reg_average_cmd) - - expected_argv = ('%s %s -avg_tran %s %s %s %s %s %s %s' - % (get_custom_path('reg_average'), - os.path.join(os.getcwd(), 'avg_out.nii.gz'), - ref_file, trans1_file, one_file, trans2_file, two_file, - trans3_file, three_file)) - - assert argv.decode('utf-8') == expected_argv - - expected_cmd = ('%s --cmd_file %s' - % (get_custom_path('reg_average'), reg_average_cmd)) - - assert generated_cmd == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_average')) -def test_reg_average_demean3(): - - # Create a reg_average object - nr = RegAverage() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_average') - - # Assign some input data - ref_file = example_data('anatomical.nii') - one_file = example_data('im1.nii') - two_file = example_data('im2.nii') - three_file = example_data('im3.nii') - aff1_file = example_data('TransformParameters.0.txt') - aff2_file = example_data('ants_Affine.txt') - aff3_file = example_data('elastix.txt') - trans1_file = example_data('roi01.nii') - trans2_file = example_data('roi02.nii') - trans3_file = example_data('roi03.nii') - nr.inputs.warp_files = [aff1_file, trans1_file, one_file, - aff2_file, trans2_file, two_file, - aff3_file, trans3_file, three_file] - nr.inputs.demean3_ref_file = ref_file - generated_cmd = nr.cmdline - - # Read the reg_average_cmd - reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') - with open(reg_average_cmd, 'rb') as f: - argv = f.read() - os.remove(reg_average_cmd) - - expected_argv = ('%s %s -demean3 %s %s %s %s %s %s %s %s %s %s' - % (get_custom_path('reg_average'), - os.path.join(os.getcwd(), 'avg_out.nii.gz'), - ref_file, - aff1_file, trans1_file, one_file, - aff2_file, trans2_file, two_file, - aff3_file, trans3_file, three_file)) - - assert argv.decode('utf-8') == expected_argv - - expected_cmd = ('%s --cmd_file %s' - % (get_custom_path('reg_average'), reg_average_cmd)) - - assert generated_cmd == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py b/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py deleted file mode 100644 index 35e1b53383..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_F3D.py +++ /dev/null @@ -1,46 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import no_niftyreg, get_custom_path, RegF3D -from nipype.testing import skipif, example_data -import os -import pytest - - -@skipif(no_niftyreg(cmd='reg_f3d')) -def test_reg_f3d(): - - # Create a reg_f3d object - nr = RegF3D() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_f3d') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - flo_file = example_data('im2.nii') - rmask_file = example_data('mask.nii') - nr.inputs.ref_file = ref_file - nr.inputs.flo_file = flo_file - nr.inputs.rmask_file = rmask_file - nr.inputs.omp_core_val = 4 - nr.inputs.vel_flag = True - nr.inputs.be_val = 0.1 - nr.inputs.le_val = 0.1 - - cmd_tmp = '{cmd} -be 0.100000 -cpp {cpp} -flo {flo} -le 0.100000 -omp 4 \ --ref {ref} -res {res} -rmask {rmask} -vel' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_f3d'), - cpp=os.path.join(os.getcwd(), 'im2_cpp.nii.gz'), - flo=flo_file, - ref=ref_file, - res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), - rmask=rmask_file) - - assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py b/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py deleted file mode 100644 index 396baa6762..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Jacobian.py +++ /dev/null @@ -1,101 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, - RegJacobian) -from nipype.testing import skipif, example_data -import os -import pytest - - -@skipif(no_niftyreg(cmd='reg_jacobian')) -def test_reg_jacobian_jac(): - - # Create a reg_jacobian object - nr = RegJacobian() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_jacobian') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - trans_file = example_data('warpfield.nii') - nr.inputs.ref_file = ref_file - nr.inputs.trans_file = trans_file - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jac {jac}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_jacobian'), - ref=ref_file, - trans=trans_file, - jac=os.path.join(os.getcwd(), 'warpfield_jac.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_jacobian')) -def test_reg_jacobian_jac_m(): - - # Create a reg_jacobian object - nr = RegJacobian() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_jacobian') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - trans_file = example_data('warpfield.nii') - nr.inputs.ref_file = ref_file - nr.inputs.trans_file = trans_file - nr.inputs.type = 'jacM' - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jacM {jac}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_jacobian'), - ref=ref_file, - trans=trans_file, - jac=os.path.join(os.getcwd(), 'warpfield_jacM.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_jacobian')) -def test_reg_jacobian_jac_l(): - - # Create a reg_jacobian object - nr = RegJacobian() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_jacobian') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - trans_file = example_data('warpfield.nii') - nr.inputs.ref_file = ref_file - nr.inputs.trans_file = trans_file - nr.inputs.type = 'jacL' - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jacL {jac}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_jacobian'), - ref=ref_file, - trans=trans_file, - jac=os.path.join(os.getcwd(), 'warpfield_jacL.nii.gz')) - - assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py b/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py deleted file mode 100644 index 3d8483f21b..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Measure.py +++ /dev/null @@ -1,40 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, - RegMeasure) -from nipype.testing import skipif, example_data -import pytest -import os - - -@skipif(no_niftyreg(cmd='reg_measure')) -def test_reg_measure(): - - # Create a reg_measure object - nr = RegMeasure() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_measure') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - flo_file = example_data('im2.nii') - nr.inputs.ref_file = ref_file - nr.inputs.flo_file = flo_file - nr.inputs.measure_type = 'lncc' - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -flo {flo} -lncc -omp 4 -out {out} -ref {ref}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_measure'), - flo=flo_file, - out=os.path.join(os.getcwd(), 'im2_lncc.txt'), - ref=ref_file) - - assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py b/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py deleted file mode 100644 index 7909a4ce15..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Resample.py +++ /dev/null @@ -1,80 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, - RegResample) -from nipype.testing import skipif, example_data -import os -import pytest - - -@skipif(no_niftyreg(cmd='reg_resample')) -def test_reg_resample_res(): - - # Create a reg_resample object - nr = RegResample() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_resample') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - flo_file = example_data('im2.nii') - trans_file = example_data('warpfield.nii') - nr.inputs.ref_file = ref_file - nr.inputs.flo_file = flo_file - nr.inputs.trans_file = trans_file - nr.inputs.inter_val = 'LIN' - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -flo {flo} -inter 1 -omp 4 -ref {ref} -trans {trans} \ --res {res}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_resample'), - flo=flo_file, - ref=ref_file, - trans=trans_file, - res=os.path.join(os.getcwd(), 'im2_res.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_resample')) -def test_reg_resample_blank(): - - # Create a reg_resample object - nr = RegResample() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_resample') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - ref_file = example_data('im1.nii') - flo_file = example_data('im2.nii') - trans_file = example_data('warpfield.nii') - nr.inputs.ref_file = ref_file - nr.inputs.flo_file = flo_file - nr.inputs.trans_file = trans_file - nr.inputs.type = 'blank' - nr.inputs.inter_val = 'LIN' - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -flo {flo} -inter 1 -omp 4 -ref {ref} -trans {trans} \ --blank {blank}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_resample'), - flo=flo_file, - ref=ref_file, - trans=trans_file, - blank=os.path.join(os.getcwd(), 'im2_blank.nii.gz')) - - assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py b/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py deleted file mode 100644 index 7b3e019738..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Tools.py +++ /dev/null @@ -1,64 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import no_niftyreg, get_custom_path, RegTools -from nipype.testing import skipif, example_data -import os -import pytest - - -@skipif(no_niftyreg(cmd='reg_tools')) -def test_reg_tools_mul(): - - # Create a reg_tools object - nr = RegTools() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_tools') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - in_file = example_data('im1.nii') - nr.inputs.in_file = in_file - nr.inputs.mul_val = 4 - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -in {in_file} -mul 4.0 -omp 4 -out {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_tools'), - in_file=in_file, - out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_tools')) -def test_reg_tools_iso(): - - # Create a reg_tools object - nr = RegTools() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_tools') - - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr.run() - - # Assign some input data - in_file = example_data('im1.nii') - nr.inputs.in_file = in_file - nr.inputs.iso_flag = True - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -in {in_file} -iso -omp 4 -out {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_tools'), - in_file=in_file, - out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) - - assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py b/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py deleted file mode 100644 index e95a6da371..0000000000 --- a/nipype/interfaces/niftyreg/tests/test_Reg_Transform.py +++ /dev/null @@ -1,165 +0,0 @@ -# -*- coding: utf-8 -*- -# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- -# vi: set ft=python sts=4 ts=4 sw=4 et: - -from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, - RegTransform) -from nipype.testing import skipif, example_data -import os - - -@skipif(no_niftyreg(cmd='reg_transform')) -def test_reg_transform_def(): - - # Create a reg_transform object - nr = RegTransform() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_transform') - - # Assign some input data - trans_file = example_data('warpfield.nii') - nr.inputs.def_input = trans_file - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -omp 4 -def {trans_file} {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_transform'), - trans_file=trans_file, - out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_transform')) -def test_reg_transform_def_ref(): - - # Create a reg_transform object - nr = RegTransform() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_transform') - - # Assign some input data - ref_file = example_data('im1.nii') - trans_file = example_data('warpfield.nii') - nr.inputs.ref1_file = ref_file - nr.inputs.def_input = trans_file - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -ref {ref_file} -omp 4 -def {trans_file} {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_transform'), - ref_file=ref_file, - trans_file=trans_file, - out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_transform')) -def test_reg_transform_comp_nii(): - - # Create a reg_transform object - nr = RegTransform() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_transform') - - # Assign some input data - ref_file = example_data('im1.nii') - trans_file = example_data('warpfield.nii') - trans2_file = example_data('anatomical.nii') - nr.inputs.ref1_file = ref_file - nr.inputs.comp_input2 = trans2_file - nr.inputs.comp_input = trans_file - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -ref {ref_file} -omp 4 -comp {trans1} {trans2} {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_transform'), - ref_file=ref_file, - trans1=trans_file, - trans2=trans2_file, - out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_transform')) -def test_reg_transform_comp_txt(): - - # Create a reg_transform object - nr = RegTransform() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_transform') - - # Assign some input data - aff1_file = example_data('ants_Affine.txt') - aff2_file = example_data('elastix.txt') - nr.inputs.comp_input2 = aff2_file - nr.inputs.comp_input = aff1_file - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -omp 4 -comp {aff1} {aff2} {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_transform'), - aff1=aff1_file, - aff2=aff2_file, - out_file=os.path.join(os.getcwd(), 'ants_Affine_trans.txt')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_transform')) -def test_reg_transform_comp(): - - # Create a reg_transform object - nr = RegTransform() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_transform') - - # Assign some input data - trans_file = example_data('warpfield.nii') - aff_file = example_data('elastix.txt') - nr.inputs.comp_input2 = trans_file - nr.inputs.comp_input = aff_file - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -omp 4 -comp {aff} {trans} {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_transform'), - aff=aff_file, - trans=trans_file, - out_file=os.path.join(os.getcwd(), 'elastix_trans.nii.gz')) - - assert nr.cmdline == expected_cmd - - -@skipif(no_niftyreg(cmd='reg_transform')) -def test_reg_transform_flirt(): - - # Create a reg_transform object - nr = RegTransform() - - # Check if the command is properly defined - assert nr.cmd == get_custom_path('reg_transform') - - # Assign some input data - aff_file = example_data('elastix.txt') - ref_file = example_data('im1.nii') - in_file = example_data('im2.nii') - nr.inputs.flirt_2_nr_input = (aff_file, ref_file, in_file) - nr.inputs.omp_core_val = 4 - - cmd_tmp = '{cmd} -omp 4 -flirtAff2NR {aff} {ref} {in_file} {out_file}' - expected_cmd = cmd_tmp.format( - cmd=get_custom_path('reg_transform'), - aff=aff_file, - ref=ref_file, - in_file=in_file, - out_file=os.path.join(os.getcwd(), 'elastix_trans.txt')) - - assert nr.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_reg.py b/nipype/interfaces/niftyreg/tests/test_reg.py new file mode 100644 index 0000000000..153f46518a --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_reg.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegAladin, RegF3D) +from nipype.testing import example_data +import os +import pytest + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_aladin'), + reason="niftyreg is not installed. reg_aladin not found.") +def test_reg_aladin(): + """ tests for reg_aladin interface""" + # Create a reg_aladin object + nr_aladin = RegAladin() + + # Check if the command is properly defined + assert nr_aladin.cmd == get_custom_path('reg_aladin') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr_aladin.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + rmask_file = example_data('mask.nii') + nr_aladin.inputs.ref_file = ref_file + nr_aladin.inputs.flo_file = flo_file + nr_aladin.inputs.rmask_file = rmask_file + nr_aladin.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -aff {aff} -flo {flo} -omp 4 -ref {ref} -res {res} \ +-rmask {rmask}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_aladin'), + aff=os.path.join(os.getcwd(), 'im2_aff.txt'), + flo=flo_file, + ref=ref_file, + res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), + rmask=rmask_file) + + assert nr_aladin.cmdline == expected_cmd + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_f3d'), + reason="niftyreg is not installed. reg_f3d not found.") +def test_reg_f3d(): + """ tests for reg_f3d interface""" + # Create a reg_f3d object + nr_f3d = RegF3D() + + # Check if the command is properly defined + assert nr_f3d.cmd == get_custom_path('reg_f3d') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr_f3d.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + rmask_file = example_data('mask.nii') + nr_f3d.inputs.ref_file = ref_file + nr_f3d.inputs.flo_file = flo_file + nr_f3d.inputs.rmask_file = rmask_file + nr_f3d.inputs.omp_core_val = 4 + nr_f3d.inputs.vel_flag = True + nr_f3d.inputs.be_val = 0.1 + nr_f3d.inputs.le_val = 0.1 + + cmd_tmp = '{cmd} -be 0.100000 -cpp {cpp} -flo {flo} -le 0.100000 -omp 4 \ +-ref {ref} -res {res} -rmask {rmask} -vel' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_f3d'), + cpp=os.path.join(os.getcwd(), 'im2_cpp.nii.gz'), + flo=flo_file, + ref=ref_file, + res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), + rmask=rmask_file) + + assert nr_f3d.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_regutils.py b/nipype/interfaces/niftyreg/tests/test_regutils.py new file mode 100644 index 0000000000..7608dd5075 --- /dev/null +++ b/nipype/interfaces/niftyreg/tests/test_regutils.py @@ -0,0 +1,463 @@ +# -*- coding: utf-8 -*- +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: + +from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, + RegAverage, RegResample, RegJacobian, + RegTools, RegMeasure, RegTransform) +from nipype.testing import example_data +import os +import pytest + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_resample'), + reason="niftyreg is not installed. reg_resample not found.") +def test_reg_resample_res(): + """ tests for reg_resample interface """ + # Create a reg_resample object + nr_resample = RegResample() + + # Check if the command is properly defined + assert nr_resample.cmd == get_custom_path('reg_resample') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr_resample.run() + + # Resample res + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + trans_file = example_data('warpfield.nii') + nr_resample.inputs.ref_file = ref_file + nr_resample.inputs.flo_file = flo_file + nr_resample.inputs.trans_file = trans_file + nr_resample.inputs.inter_val = 'LIN' + nr_resample.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -flo {flo} -inter 1 -omp 4 -ref {ref} -trans {trans} \ +-res {res}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_resample'), + flo=flo_file, + ref=ref_file, + trans=trans_file, + res=os.path.join(os.getcwd(), 'im2_res.nii.gz')) + + assert nr_resample.cmdline == expected_cmd + + # test_reg_resample_blank() + nr_resample_2 = RegResample(type='blank', inter_val='LIN', omp_core_val=4) + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + trans_file = example_data('warpfield.nii') + nr_resample_2.inputs.ref_file = ref_file + nr_resample_2.inputs.flo_file = flo_file + nr_resample_2.inputs.trans_file = trans_file + + cmd_tmp = '{cmd} -flo {flo} -inter 1 -omp 4 -ref {ref} -trans {trans} \ +-blank {blank}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_resample'), + flo=flo_file, + ref=ref_file, + trans=trans_file, + blank=os.path.join(os.getcwd(), 'im2_blank.nii.gz')) + + assert nr_resample_2.cmdline == expected_cmd + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_jacobian'), + reason="niftyreg is not installed. reg_jacobian not found.") +def test_reg_jacobian_jac(): + """ Test interface for RegJacobian """ + # Create a reg_jacobian object + nr_jacobian = RegJacobian() + + # Check if the command is properly defined + assert nr_jacobian.cmd == get_custom_path('reg_jacobian') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr_jacobian.run() + + # Test Reg Jacobian: jac + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr_jacobian.inputs.ref_file = ref_file + nr_jacobian.inputs.trans_file = trans_file + nr_jacobian.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jac {jac}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_jacobian'), + ref=ref_file, + trans=trans_file, + jac=os.path.join(os.getcwd(), 'warpfield_jac.nii.gz')) + + assert nr_jacobian.cmdline == expected_cmd + + # Test Reg Jacobian: jac m + nr_jacobian_2 = RegJacobian(type='jacM', omp_core_val=4) + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr_jacobian_2.inputs.ref_file = ref_file + nr_jacobian_2.inputs.trans_file = trans_file + + cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jacM {jac}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_jacobian'), + ref=ref_file, + trans=trans_file, + jac=os.path.join(os.getcwd(), 'warpfield_jacM.nii.gz')) + + assert nr_jacobian_2.cmdline == expected_cmd + + # Test Reg Jacobian: jac l + nr_jacobian_3 = RegJacobian(type='jacL', omp_core_val=4) + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr_jacobian_3.inputs.ref_file = ref_file + nr_jacobian_3.inputs.trans_file = trans_file + + cmd_tmp = '{cmd} -omp 4 -ref {ref} -trans {trans} -jacL {jac}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_jacobian'), + ref=ref_file, + trans=trans_file, + jac=os.path.join(os.getcwd(), 'warpfield_jacL.nii.gz')) + + assert nr_jacobian_3.cmdline == expected_cmd + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_tools'), + reason="niftyreg is not installed. reg_tools not found.") +def test_reg_tools_mul(): + """ tests for reg_tools interface """ + # Create a reg_tools object + nr_tools = RegTools() + + # Check if the command is properly defined + assert nr_tools.cmd == get_custom_path('reg_tools') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr_tools.run() + + # Test reg_tools: mul + in_file = example_data('im1.nii') + nr_tools.inputs.in_file = in_file + nr_tools.inputs.mul_val = 4 + nr_tools.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -in {in_file} -mul 4.0 -omp 4 -out {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_tools'), + in_file=in_file, + out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) + + assert nr_tools.cmdline == expected_cmd + + # Test reg_tools: iso + nr_tools_2 = RegTools(iso_flag=True, omp_core_val=4) + in_file = example_data('im1.nii') + nr_tools_2.inputs.in_file = in_file + + cmd_tmp = '{cmd} -in {in_file} -iso -omp 4 -out {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_tools'), + in_file=in_file, + out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) + + assert nr_tools_2.cmdline == expected_cmd + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_average'), + reason="niftyreg is not installed. reg_average not found.") +def test_reg_average(): + """ tests for reg_average interface """ + # Create a reg_average object + nr_average = RegAverage() + + # Check if the command is properly defined + assert nr_average.cmd == get_custom_path('reg_average') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr_average.run() + + # Average niis + one_file = example_data('im1.nii') + two_file = example_data('im2.nii') + three_file = example_data('im3.nii') + nr_average.inputs.avg_files = [one_file, two_file, three_file] + generated_cmd = nr_average.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f_obj: + argv = f_obj.read() + os.remove(reg_average_cmd) + + expected_argv = '%s %s -avg %s %s %s' % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), + 'avg_out.nii.gz'), + one_file, two_file, three_file) + + assert argv.decode('utf-8') == expected_argv + + # Test command line with text file + expected_cmd = ('%s --cmd_file %s' + % (get_custom_path('reg_average'), reg_average_cmd)) + + assert generated_cmd == expected_cmd + + # Test Reg Average: average txt + nr_average_2 = RegAverage() + one_file = example_data('TransformParameters.0.txt') + two_file = example_data('ants_Affine.txt') + three_file = example_data('elastix.txt') + nr_average_2.inputs.avg_files = [one_file, two_file, three_file] + generated_cmd = nr_average_2.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f_obj: + argv = f_obj.read() + os.remove(reg_average_cmd) + + expected_argv = '%s %s -avg %s %s %s' % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), + 'avg_out.txt'), + one_file, two_file, three_file) + + assert argv.decode('utf-8') == expected_argv + + # Test Reg Average: average list + nr_average_3 = RegAverage() + one_file = example_data('TransformParameters.0.txt') + two_file = example_data('ants_Affine.txt') + three_file = example_data('elastix.txt') + nr_average_3.inputs.avg_lts_files = [one_file, two_file, three_file] + generated_cmd = nr_average_3.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f_obj: + argv = f_obj.read() + os.remove(reg_average_cmd) + + expected_argv = ('%s %s -avg_lts %s %s %s' + % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), 'avg_out.txt'), + one_file, two_file, three_file)) + + assert argv.decode('utf-8') == expected_argv + + # Test Reg Average: average ref + nr_average_4 = RegAverage() + ref_file = example_data('anatomical.nii') + one_file = example_data('im1.nii') + two_file = example_data('im2.nii') + three_file = example_data('im3.nii') + trans1_file = example_data('roi01.nii') + trans2_file = example_data('roi02.nii') + trans3_file = example_data('roi03.nii') + nr_average_4.inputs.warp_files = [trans1_file, one_file, + trans2_file, two_file, + trans3_file, three_file] + nr_average_4.inputs.avg_ref_file = ref_file + generated_cmd = nr_average_4.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f_obj: + argv = f_obj.read() + os.remove(reg_average_cmd) + + expected_argv = ('%s %s -avg_tran %s %s %s %s %s %s %s' + % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), 'avg_out.nii.gz'), + ref_file, trans1_file, one_file, trans2_file, two_file, + trans3_file, three_file)) + + assert argv.decode('utf-8') == expected_argv + + # Test Reg Average: demean3 + nr_average_5 = RegAverage() + ref_file = example_data('anatomical.nii') + one_file = example_data('im1.nii') + two_file = example_data('im2.nii') + three_file = example_data('im3.nii') + aff1_file = example_data('TransformParameters.0.txt') + aff2_file = example_data('ants_Affine.txt') + aff3_file = example_data('elastix.txt') + trans1_file = example_data('roi01.nii') + trans2_file = example_data('roi02.nii') + trans3_file = example_data('roi03.nii') + nr_average_5.inputs.warp_files = [aff1_file, trans1_file, one_file, + aff2_file, trans2_file, two_file, + aff3_file, trans3_file, three_file] + nr_average_5.inputs.demean3_ref_file = ref_file + generated_cmd = nr_average_5.cmdline + + # Read the reg_average_cmd + reg_average_cmd = os.path.join(os.getcwd(), 'reg_average_cmd') + with open(reg_average_cmd, 'rb') as f_obj: + argv = f_obj.read() + os.remove(reg_average_cmd) + + expected_argv = ('%s %s -demean3 %s %s %s %s %s %s %s %s %s %s' + % (get_custom_path('reg_average'), + os.path.join(os.getcwd(), 'avg_out.nii.gz'), + ref_file, + aff1_file, trans1_file, one_file, + aff2_file, trans2_file, two_file, + aff3_file, trans3_file, three_file)) + + assert argv.decode('utf-8') == expected_argv + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_transform'), + reason="niftyreg is not installed. reg_transform not found.") +def test_reg_transform_def(): + """ tests for reg_transform interface """ + # Create a reg_transform object + nr_transform = RegTransform() + + # Check if the command is properly defined + assert nr_transform.cmd == get_custom_path('reg_transform') + + # Assign some input data + trans_file = example_data('warpfield.nii') + nr_transform.inputs.def_input = trans_file + nr_transform.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -omp 4 -def {trans_file} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + trans_file=trans_file, + out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) + + assert nr_transform.cmdline == expected_cmd + + # Test reg_transform: def ref + nr_transform_2 = RegTransform(omp_core_val=4) + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + nr_transform_2.inputs.ref1_file = ref_file + nr_transform_2.inputs.def_input = trans_file + + cmd_tmp = '{cmd} -ref {ref_file} -omp 4 -def {trans_file} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + ref_file=ref_file, + trans_file=trans_file, + out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) + + assert nr_transform_2.cmdline == expected_cmd + + # Test reg_transform: comp nii + nr_transform_3 = RegTransform(omp_core_val=4) + ref_file = example_data('im1.nii') + trans_file = example_data('warpfield.nii') + trans2_file = example_data('anatomical.nii') + nr_transform_3.inputs.ref1_file = ref_file + nr_transform_3.inputs.comp_input2 = trans2_file + nr_transform_3.inputs.comp_input = trans_file + + cmd_tmp = '{cmd} -ref {ref_file} -omp 4 -comp {trans1} {trans2} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + ref_file=ref_file, + trans1=trans_file, + trans2=trans2_file, + out_file=os.path.join(os.getcwd(), 'warpfield_trans.nii.gz')) + + assert nr_transform_3.cmdline == expected_cmd + + # Test reg_transform: comp txt + nr_transform_4 = RegTransform(omp_core_val=4) + aff1_file = example_data('ants_Affine.txt') + aff2_file = example_data('elastix.txt') + nr_transform_4.inputs.comp_input2 = aff2_file + nr_transform_4.inputs.comp_input = aff1_file + + cmd_tmp = '{cmd} -omp 4 -comp {aff1} {aff2} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + aff1=aff1_file, + aff2=aff2_file, + out_file=os.path.join(os.getcwd(), 'ants_Affine_trans.txt')) + + assert nr_transform_4.cmdline == expected_cmd + + # Test reg_transform: comp + nr_transform_5 = RegTransform(omp_core_val=4) + trans_file = example_data('warpfield.nii') + aff_file = example_data('elastix.txt') + nr_transform_5.inputs.comp_input2 = trans_file + nr_transform_5.inputs.comp_input = aff_file + + cmd_tmp = '{cmd} -omp 4 -comp {aff} {trans} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + aff=aff_file, + trans=trans_file, + out_file=os.path.join(os.getcwd(), 'elastix_trans.nii.gz')) + + assert nr_transform_5.cmdline == expected_cmd + + # Test reg_transform: flirt + nr_transform_6 = RegTransform(omp_core_val=4) + aff_file = example_data('elastix.txt') + ref_file = example_data('im1.nii') + in_file = example_data('im2.nii') + nr_transform_6.inputs.flirt_2_nr_input = (aff_file, ref_file, in_file) + + cmd_tmp = '{cmd} -omp 4 -flirtAff2NR {aff} {ref} {in_file} {out_file}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_transform'), + aff=aff_file, + ref=ref_file, + in_file=in_file, + out_file=os.path.join(os.getcwd(), 'elastix_trans.txt')) + + assert nr_transform_6.cmdline == expected_cmd + + +@pytest.mark.skipif( + no_niftyreg(cmd='reg_measure'), + reason="niftyreg is not installed. reg_measure not found.") +def test_reg_measure(): + """ tests for reg_measure interface """ + # Create a reg_measure object + nr_measure = RegMeasure() + + # Check if the command is properly defined + assert nr_measure.cmd == get_custom_path('reg_measure') + + # test raising error with mandatory args absent + with pytest.raises(ValueError): + nr_measure.run() + + # Assign some input data + ref_file = example_data('im1.nii') + flo_file = example_data('im2.nii') + nr_measure.inputs.ref_file = ref_file + nr_measure.inputs.flo_file = flo_file + nr_measure.inputs.measure_type = 'lncc' + nr_measure.inputs.omp_core_val = 4 + + cmd_tmp = '{cmd} -flo {flo} -lncc -omp 4 -out {out} -ref {ref}' + expected_cmd = cmd_tmp.format( + cmd=get_custom_path('reg_measure'), + flo=flo_file, + out=os.path.join(os.getcwd(), 'im2_lncc.txt'), + ref=ref_file) + + assert nr_measure.cmdline == expected_cmd From 6800ccecdcd7870f8c61ea628e823a18e0395bd9 Mon Sep 17 00:00:00 2001 From: byvernault Date: Mon, 3 Apr 2017 11:23:05 +0100 Subject: [PATCH 09/20] Removing code to raise exceptions for version control and switch to warning/add function --- nipype/interfaces/niftyreg/base.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 46b2cf126b..737783c41b 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -72,19 +72,33 @@ class NiftyRegCommand(CommandLine): def __init__(self, required_version=None, **inputs): super(NiftyRegCommand, self).__init__(**inputs) - cur_version = self.get_version() - if not cur_version: + self.required_version = required_version + _version = self.get_version() + if _version: + _version = _version.decode("utf-8") + if StrictVersion(_version) < StrictVersion(self._min_version): + warn = 'A later version of Niftyreg is required (%s < %s)' + print(warn % (_version, self._min_version)) + if required_version is not None: + if StrictVersion(_version) != StrictVersion(required_version): + warn = 'The version of NiftyReg differs from the required' + warn += '(%s != %s)' + print(warn % (_version, required_version)) + + def check_version(self): + _version = self.get_version() + if not _version: raise Exception('Niftyreg not found') # Decoding to string: - cur_version = cur_version.decode("utf-8") - if StrictVersion(cur_version) < StrictVersion(self._min_version): + _version = _version.decode("utf-8") + if StrictVersion(_version) < StrictVersion(self._min_version): err = 'A later version of Niftyreg is required (%s < %s)' - raise ValueError(err % (cur_version, self._min_version)) - if required_version is not None: - if StrictVersion(cur_version) != StrictVersion(required_version): + raise ValueError(err % (_version, self._min_version)) + if self.required_version: + if StrictVersion(_version) != StrictVersion(self.required_version): err = 'The version of NiftyReg differs from the required' err += '(%s != %s)' - raise ValueError(err % (cur_version, required_version)) + raise ValueError(err % (_version, self.required_version)) def get_version(self): if no_niftyreg(cmd=self.cmd): From cc1c6d06615aa089a74f678759783c1dd4fa9f80 Mon Sep 17 00:00:00 2001 From: byvernault Date: Mon, 3 Apr 2017 11:24:55 +0100 Subject: [PATCH 10/20] Editing the tests and docstring --- nipype/interfaces/niftyreg/reg.py | 41 ++++----- nipype/interfaces/niftyreg/regutils.py | 87 +++++++++---------- .../niftyreg/tests/test_regutils.py | 4 - 3 files changed, 64 insertions(+), 68 deletions(-) diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 6fb730ac03..4f1f272480 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -15,8 +15,8 @@ >>> os.chdir(datadir) """ -from __future__ import print_function, division, \ - unicode_literals, absolute_import +from __future__ import (print_function, division, unicode_literals, + absolute_import) from builtins import staticmethod import os import warnings @@ -149,15 +149,15 @@ class RegAladin(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegAladin - >>> node = RegAladin() # doctest: +SKIP - >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP - >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP - >>> node.inputs.rmask_file = 'rmask.nii.gz' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP - 'reg_aladin -aff flo_aff.txt -flo flo.nii.gz -omp 4 -ref ref.nii.gz \ --rmask rmask.nii.gz -res flo_res.nii.gz' + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegAladin() + >>> node.inputs.ref_file = 'ref.nii.gz' + >>> node.inputs.flo_file = 'flo.nii.gz' + >>> node.inputs.rmask_file = 'rmask.nii.gz' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + 'reg_aladin -aff .../flo_aff.txt -flo flo.nii.gz -omp 4 -ref ref.nii.gz \ +-rmask rmask.nii.gz -res .../flo_res.nii.gz' """ _cmd = get_custom_path('reg_aladin') @@ -379,15 +379,16 @@ class RegF3D(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegF3D - >>> node = RegF3D() # doctest: +SKIP - >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP - >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP - >>> node.inputs.rmask_file = 'rmask.nii.gz' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP - 'reg_f3d -be 0.100000 -cpp flo_cpp.nii.gz -flo flo.nii.gz -le 0.100000 \ --omp 4 -ref ref.nii.gz -res flo_res.nii.gz -rmask rmask.nii.gz -vel' + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegF3D() + >>> node.inputs.ref_file = 'ref.nii.gz' + >>> node.inputs.flo_file = 'flo.nii.gz' + >>> node.inputs.rmask_file = 'rmask.nii.gz' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + 'reg_f3d -be 0.100000 -cpp .../flo_cpp.nii.gz -flo flo.nii.gz -le \ +0.100000 -omp 4 -ref ref.nii.gz -res .../flo_res.nii.gz -rmask rmask.nii.gz \ +-vel' """ _cmd = get_custom_path('reg_f3d') diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index 217be8c9fb..a57ae7cc5d 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -14,8 +14,8 @@ >>> os.chdir(datadir) """ -from __future__ import print_function, division, \ - unicode_literals, absolute_import +from __future__ import (print_function, division, unicode_literals, + absolute_import) from builtins import len, open, property, super import warnings import os @@ -102,16 +102,16 @@ class RegResample(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegResample - >>> node = RegResample() # doctest: +SKIP - >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP - >>> node.inputs.flo_file = 'flo.nii.gz' # doctest: +SKIP - >>> node.inputs.trans_file = 'warpfield.nii.gz' # doctest: +SKIP - >>> node.inputs.inter_val = 'LIN' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegResample() + >>> node.inputs.ref_file = 'ref.nii.gz' + >>> node.inputs.flo_file = 'flo.nii.gz' + >>> node.inputs.trans_file = 'warpfield.nii.gz' + >>> node.inputs.inter_val = 'LIN' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE 'reg_resample -flo flo.nii.gz -inter 1 -omp 4 -ref ref.nii.gz -trans \ -warpfield.nii.gz -res flo_res.nii.gz' +warpfield.nii.gz -res .../flo_res.nii.gz' """ _cmd = get_custom_path('reg_resample') @@ -175,14 +175,14 @@ class RegJacobian(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegJacobian - >>> node = RegJacobian() # doctest: +SKIP - >>> node.inputs.ref_file = 'ref.nii.gz' # doctest: +SKIP - >>> node.inputs.trans_file = 'warpfield.nii.gz' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegJacobian() + >>> node.inputs.ref_file = 'ref.nii.gz' + >>> node.inputs.trans_file = 'warpfield.nii.gz' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE 'reg_jacobian -omp 4 -ref ref.nii.gz -trans warpfield.nii.gz -jac \ -warpfield_jac.nii.gz' +.../warpfield_jac.nii.gz' """ _cmd = get_custom_path('reg_jacobian') @@ -296,13 +296,13 @@ class RegTools(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegTools - >>> node = RegTools() # doctest: +SKIP - >>> node.inputs.in_file = 'im1.nii.gz' # doctest: +SKIP - >>> node.inputs.mul_val = 4 # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP - 'reg_tools -in im1.nii.gz -mul 4.0 -omp 4 -out im1_tools.nii.gz' + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegTools() + >>> node.inputs.in_file = 'im1.nii.gz' + >>> node.inputs.mul_val = 4 + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + 'reg_tools -in im1.nii.gz -mul 4.0 -omp 4 -out .../im1_tools.nii.gz' """ _cmd = get_custom_path('reg_tools') @@ -403,14 +403,13 @@ class RegAverage(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegAverage - >>> node = RegAverage() # doctest: +SKIP + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegAverage() >>> one_file = 'im1.nii' >>> two_file = 'im2.nii' >>> three_file = 'im3.nii' - >>> node.inputs.avg_files = [one_file, two_file, three_file] \ - # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP + >>> node.inputs.avg_files = [one_file, two_file, three_file] + >>> node.cmdline 'reg_average --cmd_file reg_average_cmd' """ @@ -605,12 +604,12 @@ class RegTransform(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegResample - >>> node = RegTransform() # doctest: +SKIP - >>> node.inputs.def_input = 'warpfield.nii' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP - 'reg_transform -omp 4 -def warpfield.nii warpfield_trans.nii.gz' + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegTransform() + >>> node.inputs.def_input = 'warpfield.nii' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + 'reg_transform -omp 4 -def warpfield.nii .../warpfield_trans.nii.gz' """ _cmd = get_custom_path('reg_transform') @@ -717,14 +716,14 @@ class RegMeasure(NiftyRegCommand): Examples -------- - >>> from nipype.interfaces.niftyreg import RegMeasure - >>> node = RegMeasure() # doctest: +SKIP - >>> node.inputs.ref_file = 'im1.nii' # doctest: +SKIP - >>> node.inputs.flo_file = 'im2.nii' # doctest: +SKIP - >>> node.inputs.measure_type = 'lncc' # doctest: +SKIP - >>> node.inputs.omp_core_val = 4 # doctest: +SKIP - >>> node.cmdline # doctest: +SKIP - 'reg_measure -flo {flo} -lncc -omp 4 -out {out} -ref {ref}' + >>> from nipype.interfaces import niftyreg + >>> node = niftyreg.RegMeasure() + >>> node.inputs.ref_file = 'im1.nii' + >>> node.inputs.flo_file = 'im2.nii' + >>> node.inputs.measure_type = 'lncc' + >>> node.inputs.omp_core_val = 4 + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + 'reg_measure -flo im2.nii -lncc -omp 4 -out .../im2_lncc.txt -ref im1.nii' """ _cmd = get_custom_path('reg_measure') diff --git a/nipype/interfaces/niftyreg/tests/test_regutils.py b/nipype/interfaces/niftyreg/tests/test_regutils.py index 7608dd5075..d0d676cc11 100644 --- a/nipype/interfaces/niftyreg/tests/test_regutils.py +++ b/nipype/interfaces/niftyreg/tests/test_regutils.py @@ -185,10 +185,6 @@ def test_reg_average(): # Check if the command is properly defined assert nr_average.cmd == get_custom_path('reg_average') - # test raising error with mandatory args absent - with pytest.raises(ValueError): - nr_average.run() - # Average niis one_file = example_data('im1.nii') two_file = example_data('im2.nii') From 18994c64e35640a567c83d05780bdfc0f2c55958 Mon Sep 17 00:00:00 2001 From: byvernault Date: Mon, 3 Apr 2017 11:29:33 +0100 Subject: [PATCH 11/20] Adding # doctest: +ALLOW_UNICODE for unicode --- nipype/interfaces/niftyreg/regutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index a57ae7cc5d..b3bc5eb7be 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -409,7 +409,7 @@ class RegAverage(NiftyRegCommand): >>> two_file = 'im2.nii' >>> three_file = 'im3.nii' >>> node.inputs.avg_files = [one_file, two_file, three_file] - >>> node.cmdline + >>> node.cmdline # doctest: +ALLOW_UNICODE 'reg_average --cmd_file reg_average_cmd' """ From 1ed121d3c0600f31785e7a1984ad3ec81997606d Mon Sep 17 00:00:00 2001 From: byvernault Date: Mon, 3 Apr 2017 15:15:32 +0100 Subject: [PATCH 12/20] using files present in testing/data --- nipype/interfaces/niftyreg/reg.py | 20 ++++++++++---------- nipype/interfaces/niftyreg/regutils.py | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 4f1f272480..98a0eafd6d 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -151,13 +151,13 @@ class RegAladin(NiftyRegCommand): -------- >>> from nipype.interfaces import niftyreg >>> node = niftyreg.RegAladin() - >>> node.inputs.ref_file = 'ref.nii.gz' - >>> node.inputs.flo_file = 'flo.nii.gz' - >>> node.inputs.rmask_file = 'rmask.nii.gz' + >>> node.inputs.ref_file = 'im1.nii' + >>> node.inputs.flo_file = 'im2.nii' + >>> node.inputs.rmask_file = 'mask.nii' >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_aladin -aff .../flo_aff.txt -flo flo.nii.gz -omp 4 -ref ref.nii.gz \ --rmask rmask.nii.gz -res .../flo_res.nii.gz' + 'reg_aladin -aff .../im2_aff.txt -flo im2.nii -omp 4 -ref im1.nii \ +-rmask mask.nii -res .../im2_res.nii.gz' """ _cmd = get_custom_path('reg_aladin') @@ -381,13 +381,13 @@ class RegF3D(NiftyRegCommand): -------- >>> from nipype.interfaces import niftyreg >>> node = niftyreg.RegF3D() - >>> node.inputs.ref_file = 'ref.nii.gz' - >>> node.inputs.flo_file = 'flo.nii.gz' - >>> node.inputs.rmask_file = 'rmask.nii.gz' + >>> node.inputs.ref_file = 'im1.nii' + >>> node.inputs.flo_file = 'im2.nii' + >>> node.inputs.rmask_file = 'mask.nii' >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_f3d -be 0.100000 -cpp .../flo_cpp.nii.gz -flo flo.nii.gz -le \ -0.100000 -omp 4 -ref ref.nii.gz -res .../flo_res.nii.gz -rmask rmask.nii.gz \ + 'reg_f3d -be 0.100000 -cpp .../im2_cpp.nii.gz -flo im2.nii -le \ +0.100000 -omp 4 -ref im1.nii -res .../flo_res.nii.gz -rmask mask.nii \ -vel' """ diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index b3bc5eb7be..5c78f9b177 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -104,14 +104,14 @@ class RegResample(NiftyRegCommand): -------- >>> from nipype.interfaces import niftyreg >>> node = niftyreg.RegResample() - >>> node.inputs.ref_file = 'ref.nii.gz' - >>> node.inputs.flo_file = 'flo.nii.gz' - >>> node.inputs.trans_file = 'warpfield.nii.gz' + >>> node.inputs.ref_file = 'im1.nii' + >>> node.inputs.flo_file = 'im2.nii' + >>> node.inputs.trans_file = 'warpfield.nii' >>> node.inputs.inter_val = 'LIN' >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_resample -flo flo.nii.gz -inter 1 -omp 4 -ref ref.nii.gz -trans \ -warpfield.nii.gz -res .../flo_res.nii.gz' + 'reg_resample -flo im2.nii.gz -inter 1 -omp 4 -ref im1.nii -trans \ +warpfield.nii -res .../im2_res.nii.gz' """ _cmd = get_custom_path('reg_resample') @@ -177,11 +177,11 @@ class RegJacobian(NiftyRegCommand): -------- >>> from nipype.interfaces import niftyreg >>> node = niftyreg.RegJacobian() - >>> node.inputs.ref_file = 'ref.nii.gz' - >>> node.inputs.trans_file = 'warpfield.nii.gz' + >>> node.inputs.ref_file = 'im1.nii' + >>> node.inputs.trans_file = 'warpfield.nii' >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_jacobian -omp 4 -ref ref.nii.gz -trans warpfield.nii.gz -jac \ + 'reg_jacobian -omp 4 -ref im1.nii -trans warpfield.nii -jac \ .../warpfield_jac.nii.gz' """ @@ -298,11 +298,11 @@ class RegTools(NiftyRegCommand): -------- >>> from nipype.interfaces import niftyreg >>> node = niftyreg.RegTools() - >>> node.inputs.in_file = 'im1.nii.gz' + >>> node.inputs.in_file = 'im1.nii' >>> node.inputs.mul_val = 4 >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_tools -in im1.nii.gz -mul 4.0 -omp 4 -out .../im1_tools.nii.gz' + 'reg_tools -in im1.nii -mul 4.0 -omp 4 -out .../im1_tools.nii.gz' """ _cmd = get_custom_path('reg_tools') From 81b738b32d6ccc75136c8b13e3fff24a69c12931 Mon Sep 17 00:00:00 2001 From: byvernault Date: Tue, 4 Apr 2017 10:08:48 +0100 Subject: [PATCH 13/20] moving print to warning and editing docstring --- nipype/interfaces/niftyreg/base.py | 16 ++++++---------- nipype/interfaces/niftyreg/reg.py | 7 +++---- nipype/interfaces/niftyreg/regutils.py | 6 +++--- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 737783c41b..29f5908258 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -23,16 +23,12 @@ from distutils.version import StrictVersion import os import subprocess -import warnings +from warnings import warn from ..base import CommandLine, isdefined, traits from ...utils.filemanip import split_filename -warn = warnings.warn -warnings.filterwarnings('always', category=UserWarning) - - def get_custom_path(command): try: specific_dir = os.environ['NIFTYREGDIR'] @@ -77,13 +73,13 @@ def __init__(self, required_version=None, **inputs): if _version: _version = _version.decode("utf-8") if StrictVersion(_version) < StrictVersion(self._min_version): - warn = 'A later version of Niftyreg is required (%s < %s)' - print(warn % (_version, self._min_version)) + msg = 'A later version of Niftyreg is required (%s < %s)' + warn(msg % (_version, self._min_version)) if required_version is not None: if StrictVersion(_version) != StrictVersion(required_version): - warn = 'The version of NiftyReg differs from the required' - warn += '(%s != %s)' - print(warn % (_version, required_version)) + msg = 'The version of NiftyReg differs from the required' + msg += '(%s != %s)' + warn(msg % (_version, self.required_version)) def check_version(self): _version = self.get_version() diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 98a0eafd6d..f03cbcd935 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -157,7 +157,7 @@ class RegAladin(NiftyRegCommand): >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE 'reg_aladin -aff .../im2_aff.txt -flo im2.nii -omp 4 -ref im1.nii \ --rmask mask.nii -res .../im2_res.nii.gz' +-res .../im2_res.nii.gz -rmask mask.nii' """ _cmd = get_custom_path('reg_aladin') @@ -386,9 +386,8 @@ class RegF3D(NiftyRegCommand): >>> node.inputs.rmask_file = 'mask.nii' >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_f3d -be 0.100000 -cpp .../im2_cpp.nii.gz -flo im2.nii -le \ -0.100000 -omp 4 -ref im1.nii -res .../flo_res.nii.gz -rmask mask.nii \ --vel' + 'reg_f3d -cpp .../im2_cpp.nii.gz -flo im2.nii -omp 4 -ref im1.nii \ +-res .../flo_res.nii.gz -rmask mask.nii' """ _cmd = get_custom_path('reg_f3d') diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index 5c78f9b177..e46ecdabe0 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -110,7 +110,7 @@ class RegResample(NiftyRegCommand): >>> node.inputs.inter_val = 'LIN' >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_resample -flo im2.nii.gz -inter 1 -omp 4 -ref im1.nii -trans \ + 'reg_resample -flo im2.nii -inter 1 -omp 4 -ref im1.nii -trans \ warpfield.nii -res .../im2_res.nii.gz' """ @@ -409,8 +409,8 @@ class RegAverage(NiftyRegCommand): >>> two_file = 'im2.nii' >>> three_file = 'im3.nii' >>> node.inputs.avg_files = [one_file, two_file, three_file] - >>> node.cmdline # doctest: +ALLOW_UNICODE - 'reg_average --cmd_file reg_average_cmd' + >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + 'reg_average --cmd_file .../reg_average_cmd' """ _cmd = get_custom_path('reg_average') From 4171b1928e629615c188d1bd5a85bc3c652f5c3b Mon Sep 17 00:00:00 2001 From: byvernault Date: Tue, 4 Apr 2017 10:42:39 +0100 Subject: [PATCH 14/20] fixing the last docstring --- nipype/interfaces/niftyreg/reg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index f03cbcd935..99c84f85a2 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -387,7 +387,7 @@ class RegF3D(NiftyRegCommand): >>> node.inputs.omp_core_val = 4 >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE 'reg_f3d -cpp .../im2_cpp.nii.gz -flo im2.nii -omp 4 -ref im1.nii \ --res .../flo_res.nii.gz -rmask mask.nii' +-res .../im2_res.nii.gz -rmask mask.nii' """ _cmd = get_custom_path('reg_f3d') From f12762ab387e74c97629651cc639d5bf44738505 Mon Sep 17 00:00:00 2001 From: byvernault Date: Thu, 20 Apr 2017 18:38:25 +0100 Subject: [PATCH 15/20] Symplifying interfaces using name_source/name_template when possible --- nipype/interfaces/niftyreg/reg.py | 65 +++++-------------- nipype/interfaces/niftyreg/regutils.py | 7 +- .../niftyreg/tests/test_auto_RegAladin.py | 6 +- .../niftyreg/tests/test_auto_RegF3D.py | 6 +- .../niftyreg/tests/test_auto_RegTools.py | 3 +- nipype/interfaces/niftyreg/tests/test_reg.py | 18 ++--- .../niftyreg/tests/test_regutils.py | 4 +- 7 files changed, 43 insertions(+), 66 deletions(-) diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 99c84f85a2..15c2c855fe 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -120,11 +120,13 @@ class RegAladinInputSpec(CommandLineInputSpec): argstr='-omp %i') # Affine output transformation matrix file - aff_file = File(genfile=True, + aff_file = File(name_source=['flo_file'], + name_template='%s_aff.txt', desc='The output affine matrix file', argstr='-aff %s') # Result warped image file - res_file = File(genfile=True, + res_file = File(name_source=['flo_file'], + name_template='%s_res.nii.gz', desc='The affine transformed floating image', argstr='-res %s') @@ -155,35 +157,17 @@ class RegAladin(NiftyRegCommand): >>> node.inputs.flo_file = 'im2.nii' >>> node.inputs.rmask_file = 'mask.nii' >>> node.inputs.omp_core_val = 4 - >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_aladin -aff .../im2_aff.txt -flo im2.nii -omp 4 -ref im1.nii \ --res .../im2_res.nii.gz -rmask mask.nii' + >>> node.cmdline # doctest: +ALLOW_UNICODE + 'reg_aladin -aff im2_aff.txt -flo im2.nii -omp 4 -ref im1.nii \ +-res im2_res.nii.gz -rmask mask.nii' """ _cmd = get_custom_path('reg_aladin') input_spec = RegAladinInputSpec output_spec = RegAladinOutputSpec - def _gen_filename(self, name): - if name == 'aff_file': - return self._gen_fname(self.inputs.flo_file, - suffix='_aff', ext='.txt') - if name == 'res_file': - return self._gen_fname(self.inputs.flo_file, - suffix='_res', ext='.nii.gz') - return None - def _list_outputs(self): - outputs = self.output_spec().get() - - if isdefined(self.inputs.aff_file): - outputs['aff_file'] = self.inputs.aff_file - else: - outputs['aff_file'] = self._gen_filename('aff_file') - if isdefined(self.inputs.res_file): - outputs['res_file'] = self.inputs.aff_file - else: - outputs['res_file'] = self._gen_filename('res_file') + outputs = super(RegAladin, self)._list_outputs() # Make a list of the linear transformation file and the input image aff = os.path.abspath(outputs['aff_file']) @@ -349,11 +333,13 @@ class RegF3DInputSpec(CommandLineInputSpec): argstr='-omp %i') # Output CPP image file - cpp_file = File(genfile=True, + cpp_file = File(name_source=['flo_file'], + name_template='%s_cpp.nii.gz', desc='The output CPP file', argstr='-cpp %s') # Output warped image file - res_file = File(genfile=True, + res_file = File(name_source=['flo_file'], + name_template='%s_res.nii.gz', desc='The output resampled image', argstr='-res %s') @@ -385,9 +371,9 @@ class RegF3D(NiftyRegCommand): >>> node.inputs.flo_file = 'im2.nii' >>> node.inputs.rmask_file = 'mask.nii' >>> node.inputs.omp_core_val = 4 - >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_f3d -cpp .../im2_cpp.nii.gz -flo im2.nii -omp 4 -ref im1.nii \ --res .../im2_res.nii.gz -rmask mask.nii' + >>> node.cmdline # doctest: +ALLOW_UNICODE + 'reg_f3d -cpp im2_cpp.nii.gz -flo im2.nii -omp 4 -ref im1.nii \ +-res im2_res.nii.gz -rmask mask.nii' """ _cmd = get_custom_path('reg_f3d') @@ -399,26 +385,8 @@ def _remove_extension(in_file): dn, bn, _ = split_filename(in_file) return os.path.join(dn, bn) - def _gen_filename(self, name): - if name == 'res_file': - return self._gen_fname(self.inputs.flo_file, - suffix='_res', ext='.nii.gz') - if name == 'cpp_file': - return self._gen_fname(self.inputs.flo_file, - suffix='_cpp', ext='.nii.gz') - def _list_outputs(self): - outputs = self.output_spec().get() - - if isdefined(self.inputs.res_file): - outputs['res_file'] = self.inputs.res_file - else: - outputs['res_file'] = self._gen_filename('res_file') - - if isdefined(self.inputs.cpp_file): - outputs['cpp_file'] = self.inputs.cpp_file - else: - outputs['cpp_file'] = self._gen_filename('cpp_file') + outputs = super(RegF3D, self)._list_outputs() if self.inputs.vel_flag is True: res_name = self._remove_extension(outputs['res_file']) @@ -436,4 +404,5 @@ def _list_outputs(self): cpp_file = os.path.abspath(outputs['cpp_file']) flo_file = os.path.abspath(self.inputs.flo_file) outputs['avg_output'] = '%s %s' % (cpp_file, flo_file) + return outputs diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index e46ecdabe0..c51421077b 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -206,7 +206,8 @@ class RegToolsInputSpec(CommandLineInputSpec): mandatory=True) # Output file path - out_file = File(genfile=True, + out_file = File(name_source=['in_file'], + name_template='%s_tools.nii.gz', desc='The output file name', argstr='-out %s') @@ -301,8 +302,8 @@ class RegTools(NiftyRegCommand): >>> node.inputs.in_file = 'im1.nii' >>> node.inputs.mul_val = 4 >>> node.inputs.omp_core_val = 4 - >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_tools -in im1.nii -mul 4.0 -omp 4 -out .../im1_tools.nii.gz' + >>> node.cmdline # doctest: +ALLOW_UNICODE + 'reg_tools -in im1.nii -mul 4.0 -omp 4 -out im1_tools.nii.gz' """ _cmd = get_custom_path('reg_tools') diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py b/nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py index cc9ebfdaa2..6865be7e4c 100644 --- a/nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py @@ -7,7 +7,8 @@ def test_RegAladin_inputs(): input_map = dict(aff_direct_flag=dict(argstr='-affDirect', ), aff_file=dict(argstr='-aff %s', - genfile=True, + name_source=['flo_file'], + name_template='%s_aff.txt', ), args=dict(argstr='%s', ), @@ -56,7 +57,8 @@ def test_RegAladin_inputs(): ref_up_val=dict(argstr='-refUpThr %f', ), res_file=dict(argstr='-res %s', - genfile=True, + name_source=['flo_file'], + name_template='%s_res.nii.gz', ), rig_only_flag=dict(argstr='-rigOnly', ), diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py b/nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py index 0f4483cbce..6e2a145eec 100644 --- a/nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py @@ -13,7 +13,8 @@ def test_RegF3D_inputs(): be_val=dict(argstr='-be %f', ), cpp_file=dict(argstr='-cpp %s', - genfile=True, + name_source=['flo_file'], + name_template='%s_cpp.nii.gz', ), environ=dict(nohash=True, usedefault=True, @@ -90,7 +91,8 @@ def test_RegF3D_inputs(): ref_smooth_val=dict(argstr='-smooR %f', ), res_file=dict(argstr='-res %s', - genfile=True, + name_source=['flo_file'], + name_template='%s_res.nii.gz', ), rlwth2_thr_val=dict(argstr='-rLwTh %d %f', ), diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegTools.py b/nipype/interfaces/niftyreg/tests/test_auto_RegTools.py index 08c85363aa..4fe0f1e7b1 100644 --- a/nipype/interfaces/niftyreg/tests/test_auto_RegTools.py +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegTools.py @@ -36,7 +36,8 @@ def test_RegTools_inputs(): omp_core_val=dict(argstr='-omp %i', ), out_file=dict(argstr='-out %s', - genfile=True, + name_source=['in_file'], + name_template='%s_tools.nii.gz', ), rms_val=dict(argstr='-rms %s', ), diff --git a/nipype/interfaces/niftyreg/tests/test_reg.py b/nipype/interfaces/niftyreg/tests/test_reg.py index 153f46518a..8f9a8eb886 100644 --- a/nipype/interfaces/niftyreg/tests/test_reg.py +++ b/nipype/interfaces/niftyreg/tests/test_reg.py @@ -2,11 +2,11 @@ # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: +import pytest + from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path, RegAladin, RegF3D) from nipype.testing import example_data -import os -import pytest @pytest.mark.skipif( @@ -37,11 +37,12 @@ def test_reg_aladin(): -rmask {rmask}' expected_cmd = cmd_tmp.format( cmd=get_custom_path('reg_aladin'), - aff=os.path.join(os.getcwd(), 'im2_aff.txt'), + aff='im2_aff.txt', flo=flo_file, ref=ref_file, - res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), - rmask=rmask_file) + res='im2_res.nii.gz', + rmask=rmask_file, + ) assert nr_aladin.cmdline == expected_cmd @@ -77,10 +78,11 @@ def test_reg_f3d(): -ref {ref} -res {res} -rmask {rmask} -vel' expected_cmd = cmd_tmp.format( cmd=get_custom_path('reg_f3d'), - cpp=os.path.join(os.getcwd(), 'im2_cpp.nii.gz'), + cpp='im2_cpp.nii.gz', flo=flo_file, ref=ref_file, - res=os.path.join(os.getcwd(), 'im2_res.nii.gz'), - rmask=rmask_file) + res='im2_res.nii.gz', + rmask=rmask_file, + ) assert nr_f3d.cmdline == expected_cmd diff --git a/nipype/interfaces/niftyreg/tests/test_regutils.py b/nipype/interfaces/niftyreg/tests/test_regutils.py index d0d676cc11..e79e38ef7e 100644 --- a/nipype/interfaces/niftyreg/tests/test_regutils.py +++ b/nipype/interfaces/niftyreg/tests/test_regutils.py @@ -156,7 +156,7 @@ def test_reg_tools_mul(): expected_cmd = cmd_tmp.format( cmd=get_custom_path('reg_tools'), in_file=in_file, - out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) + out_file='im1_tools.nii.gz') assert nr_tools.cmdline == expected_cmd @@ -169,7 +169,7 @@ def test_reg_tools_mul(): expected_cmd = cmd_tmp.format( cmd=get_custom_path('reg_tools'), in_file=in_file, - out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz')) + out_file='im1_tools.nii.gz') assert nr_tools_2.cmdline == expected_cmd From 1527855298a19d25ff7c4e427fcaa98dc9064601 Mon Sep 17 00:00:00 2001 From: byvernault Date: Fri, 21 Apr 2017 15:14:10 +0100 Subject: [PATCH 16/20] Seeting omp to 1 if not set and removing PositiveInt to use traits.Range --- nipype/interfaces/niftyreg/__init__.py | 2 +- nipype/interfaces/niftyreg/base.py | 24 ++++------- nipype/interfaces/niftyreg/reg.py | 57 +++++++++++++------------- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/nipype/interfaces/niftyreg/__init__.py b/nipype/interfaces/niftyreg/__init__.py index 2d54870a9d..9c0feeb2fb 100644 --- a/nipype/interfaces/niftyreg/__init__.py +++ b/nipype/interfaces/niftyreg/__init__.py @@ -9,7 +9,7 @@ Top-level namespace for niftyreg. """ -from .base import no_niftyreg, get_custom_path, PositiveInt +from .base import no_niftyreg, get_custom_path from .reg import RegAladin, RegF3D from .regutils import (RegResample, RegJacobian, RegAverage, RegTools, RegTransform, RegMeasure) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 29f5908258..752fe5c909 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -25,7 +25,7 @@ import subprocess from warnings import warn -from ..base import CommandLine, isdefined, traits +from ..base import CommandLine, isdefined from ...utils.filemanip import split_filename @@ -46,25 +46,12 @@ def no_niftyreg(cmd='reg_f3d'): return True -class PositiveInt(traits.BaseInt): - # Define the default value - default_value = 0 - # Describe the trait type - info_text = 'A positive integer' - - def validate(self, obj, name, value): - value = super(PositiveInt, self).validate(obj, name, value) - if (value >= 0) == 1: - return value - self.error(obj, name, value) - - class NiftyRegCommand(CommandLine): """ Base support interface for NiftyReg commands. """ _suffix = '_nr' - _min_version = '1.5.10' + _min_version = '1.5.30' def __init__(self, required_version=None, **inputs): super(NiftyRegCommand, self).__init__(**inputs) @@ -111,6 +98,13 @@ def exists(self): return False return True + def _run_interface(self, runtime): + # Update num threads estimate from OMP_NUM_THREADS env var + # Default to 1 if not set + if not isdefined(self.inputs.environ['OMP_NUM_THREADS']): + self.inputs.environ['OMP_NUM_THREADS'] = self.num_threads + return super(NiftyRegCommand, self)._run_interface(runtime) + def _gen_fname(self, basename, out_dir=None, suffix=None, ext=None): if basename == '': msg = 'Unable to generate filename for command %s. ' % self.cmd diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index 15c2c855fe..fef7f99ff6 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -22,7 +22,7 @@ import warnings from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec -from .base import get_custom_path, NiftyRegCommand, PositiveInt +from .base import get_custom_path, NiftyRegCommand from ...utils.filemanip import split_filename @@ -65,14 +65,14 @@ class RegAladinInputSpec(CommandLineInputSpec): desc='The input floating mask', argstr='-fmask %s') # Maximum number of iterations - maxit_val = PositiveInt(desc='Maximum number of iterations', - argstr='-maxit %d') + maxit_val = traits.Range(desc='Maximum number of iterations', + argstr='-maxit %d', low=0) # Multiresolution levels - ln_val = PositiveInt(desc='Number of resolution levels to create', - argstr='-ln %d') + ln_val = traits.Range(desc='Number of resolution levels to create', + argstr='-ln %d', low=0) # Number of resolution levels to process - lp_val = PositiveInt(desc='Number of resolution levels to perform', - argstr='-lp %d') + lp_val = traits.Range(desc='Number of resolution levels to perform', + argstr='-lp %d', low=0) # Smoothing to apply on reference image desc = 'Amount of smoothing to apply to reference image' smoo_r_val = traits.Float(desc=desc, @@ -90,10 +90,11 @@ class RegAladinInputSpec(CommandLineInputSpec): cog_flag = traits.Bool(desc=desc, argstr='-cog') # Percent of blocks that are considered active. - v_val = PositiveInt(desc='Percent of blocks that are active', - argstr='-pv %d') + v_val = traits.Range(desc='Percent of blocks that are active', + argstr='-pv %d', low=0) # Percent of inlier blocks - i_val = PositiveInt(desc='Percent of inlier blocks', argstr='-pi %d') + i_val = traits.Range(desc='Percent of inlier blocks', argstr='-pi %d', + low=0) # Lower threshold on reference image ref_low_val = traits.Float(desc='Lower threshold value on reference image', argstr='-refLowThr %f') @@ -226,19 +227,19 @@ class RegF3DInputSpec(CommandLineInputSpec): # Lower threshold for reference image desc = 'Lower threshold for reference image at the specified time point' - rlwth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + rlwth2_thr_val = traits.Tuple(traits.Range(low=0), traits.Float, desc=desc, argstr='-rLwTh %d %f') # Upper threshold for reference image desc = 'Upper threshold for reference image at the specified time point' - rupth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + rupth2_thr_val = traits.Tuple(traits.Range(low=0), traits.Float, desc=desc, argstr='-rUpTh %d %f') # Lower threshold for reference image desc = 'Lower threshold for floating image at the specified time point' - flwth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + flwth2_thr_val = traits.Tuple(traits.Range(low=0), traits.Float, desc=desc, argstr='-fLwTh %d %f') # Upper threshold for reference image desc = 'Upper threshold for floating image at the specified time point' - fupth2_thr_val = traits.Tuple(PositiveInt, traits.Float, + fupth2_thr_val = traits.Tuple(traits.Range(low=0), traits.Float, desc=desc, argstr='-fUpTh %d %f') # Final grid spacing along the 3 axes @@ -263,33 +264,33 @@ class RegF3DInputSpec(CommandLineInputSpec): desc = 'use NMI even when other options are specified' nmi_flag = traits.Bool(argstr='--nmi', desc=desc) desc = 'Number of bins in the histogram for reference image' - rbn_val = PositiveInt(desc=desc, argstr='--rbn %d') + rbn_val = traits.Range(low=0, desc=desc, argstr='--rbn %d') desc = 'Number of bins in the histogram for reference image' - fbn_val = PositiveInt(desc=desc, argstr='--fbn %d') + fbn_val = traits.Range(low=0, desc=desc, argstr='--fbn %d') desc = 'Number of bins in the histogram for reference image for given \ time point' - rbn2_val = traits.Tuple(PositiveInt, PositiveInt, + rbn2_val = traits.Tuple(traits.Range(low=0), traits.Range(low=0), desc=desc, argstr='-rbn %d %d') desc = 'Number of bins in the histogram for reference image for given \ time point' - fbn2_val = traits.Tuple(PositiveInt, PositiveInt, + fbn2_val = traits.Tuple(traits.Range(low=0), traits.Range(low=0), desc=desc, argstr='-fbn %d %d') lncc_val = traits.Float(desc='SD of the Gaussian for computing LNCC', argstr='--lncc %f') desc = 'SD of the Gaussian for computing LNCC for a given time point' - lncc2_val = traits.Tuple(PositiveInt, traits.Float, + lncc2_val = traits.Tuple(traits.Range(low=0), traits.Float, desc=desc, argstr='-lncc %d %f') ssd_flag = traits.Bool(desc='Use SSD as the similarity measure', argstr='--ssd') desc = 'Use SSD as the similarity measure for a given time point' - ssd2_flag = PositiveInt(desc=desc, argstr='-ssd %d') + ssd2_flag = traits.Range(low=0, desc=desc, argstr='-ssd %d') kld_flag = traits.Bool(desc='Use KL divergence as the similarity measure', argstr='--kld') desc = 'Use KL divergence as the similarity measure for a given time point' - kld2_flag = PositiveInt(desc=desc, argstr='-kld %d') + kld2_flag = traits.Range(low=0, desc=desc, argstr='-kld %d') amc_flag = traits.Bool(desc='Use additive NMI', argstr='-amc') nox_flag = traits.Bool(desc="Don't optimise in x direction", @@ -300,18 +301,18 @@ class RegF3DInputSpec(CommandLineInputSpec): argstr='-noz') # Optimization options - maxit_val = PositiveInt(desc='Maximum number of iterations per level', - argstr='-maxit %d') - ln_val = PositiveInt(desc='Number of resolution levels to create', - argstr='-ln %d') - lp_val = PositiveInt(desc='Number of resolution levels to perform', - argstr='-lp %d') + maxit_val = traits.Range(low=0, argstr='-maxit %d', + desc='Maximum number of iterations per level') + ln_val = traits.Range(low=0, argstr='-ln %d', + desc='Number of resolution levels to create') + lp_val = traits.Range(low=0, argstr='-lp %d', + desc='Number of resolution levels to perform') nopy_flag = traits.Bool(desc='Do not use the multiresolution approach', argstr='-nopy') noconj_flag = traits.Bool(desc='Use simple GD optimization', argstr='-noConj') desc = 'Add perturbation steps after each optimization step' - pert_val = PositiveInt(desc=desc, argstr='-pert %d') + pert_val = traits.Range(low=0, desc=desc, argstr='-pert %d') # F3d2 options vel_flag = traits.Bool(desc='Use velocity field integration', From b84f21733fc40acc658c6ff629e3ffd3a995cdb1 Mon Sep 17 00:00:00 2001 From: byvernault Date: Mon, 24 Apr 2017 16:34:30 +0100 Subject: [PATCH 17/20] Adding NiftyRegCommandInputSpec and using overload_extension --- nipype/interfaces/niftyreg/base.py | 24 ++--- nipype/interfaces/niftyreg/reg.py | 14 +-- nipype/interfaces/niftyreg/regutils.py | 95 ++++++++----------- .../niftyreg/tests/test_auto_RegAverage.py | 2 + .../niftyreg/tests/test_auto_RegJacobian.py | 3 +- .../niftyreg/tests/test_auto_RegMeasure.py | 3 +- .../niftyreg/tests/test_auto_RegResample.py | 5 +- .../niftyreg/tests/test_regutils.py | 12 +-- 8 files changed, 64 insertions(+), 94 deletions(-) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 752fe5c909..f13e94ad39 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -25,7 +25,7 @@ import subprocess from warnings import warn -from ..base import CommandLine, isdefined +from ..base import CommandLine, isdefined, CommandLineInputSpec, traits from ...utils.filemanip import split_filename @@ -46,6 +46,13 @@ def no_niftyreg(cmd='reg_f3d'): return True +class NiftyRegCommandInputSpec(CommandLineInputSpec): + """Input Spec for niftyreg interfaces.""" + # Set the number of omp thread to use + omp_core_val = traits.Int(desc='Number of openmp thread to use', + argstr='-omp %i') + + class NiftyRegCommand(CommandLine): """ Base support interface for NiftyReg commands. @@ -118,18 +125,3 @@ def _gen_fname(self, basename, out_dir=None, suffix=None, ext=None): if suffix is not None: final_bn = ''.join((final_bn, suffix)) return os.path.abspath(os.path.join(out_dir, final_bn + final_ext)) - - def _gen_filename(self, name): - if name == 'out_file': - return self._gen_fname(self.inputs.in_file, - suffix=self._suffix, - ext='.nii.gz') - return None - - def _list_outputs(self): - outputs = self.output_spec().get() - if isdefined(self.inputs.out_file): - outputs['out_file'] = self.inputs.out_file - else: - outputs['out_file'] = self._gen_filename('out_file') - return outputs diff --git a/nipype/interfaces/niftyreg/reg.py b/nipype/interfaces/niftyreg/reg.py index fef7f99ff6..4abc2b78c9 100644 --- a/nipype/interfaces/niftyreg/reg.py +++ b/nipype/interfaces/niftyreg/reg.py @@ -21,8 +21,8 @@ import os import warnings -from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec -from .base import get_custom_path, NiftyRegCommand +from ..base import TraitedSpec, File, traits, isdefined +from .base import get_custom_path, NiftyRegCommand, NiftyRegCommandInputSpec from ...utils.filemanip import split_filename @@ -30,7 +30,7 @@ warnings.filterwarnings('always', category=UserWarning) -class RegAladinInputSpec(CommandLineInputSpec): +class RegAladinInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegAladin. """ # Input reference file ref_file = File(exists=True, @@ -116,9 +116,6 @@ class RegAladinInputSpec(CommandLineInputSpec): # Verbosity off verbosity_off_flag = traits.Bool(argstr='-voff', desc='Turn off verbose output') - # Set the number of omp thread to use - omp_core_val = traits.Int(desc='Number of openmp thread to use', - argstr='-omp %i') # Affine output transformation matrix file aff_file = File(name_source=['flo_file'], @@ -177,7 +174,7 @@ def _list_outputs(self): return outputs -class RegF3DInputSpec(CommandLineInputSpec): +class RegF3DInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegF3D. """ # Input reference file ref_file = File(exists=True, @@ -329,9 +326,6 @@ class RegF3DInputSpec(CommandLineInputSpec): # verbosity off verbosity_off_flag = traits.Bool(argstr='-voff', desc='Turn off verbose output') - # Set the number of omp thread to use - omp_core_val = traits.Int(desc='Number of openmp thread to use', - argstr='-omp %i') # Output CPP image file cpp_file = File(name_source=['flo_file'], diff --git a/nipype/interfaces/niftyreg/regutils.py b/nipype/interfaces/niftyreg/regutils.py index c51421077b..14dc0592cf 100644 --- a/nipype/interfaces/niftyreg/regutils.py +++ b/nipype/interfaces/niftyreg/regutils.py @@ -20,8 +20,8 @@ import warnings import os -from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec -from .base import get_custom_path, NiftyRegCommand +from ..base import TraitedSpec, File, traits, isdefined +from .base import get_custom_path, NiftyRegCommand, NiftyRegCommandInputSpec from ...utils.filemanip import split_filename @@ -29,7 +29,7 @@ warnings.filterwarnings('always', category=UserWarning) -class RegResampleInputSpec(CommandLineInputSpec): +class RegResampleInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegResample. """ # Input reference file ref_file = File(exists=True, @@ -53,7 +53,8 @@ class RegResampleInputSpec(CommandLineInputSpec): desc='Type of output') # Output file name - out_file = File(genfile=True, + out_file = File(name_source=['flo_file'], + name_template='%s', argstr='%s', position=-1, desc='The output filename of the transformed image') @@ -80,10 +81,6 @@ class RegResampleInputSpec(CommandLineInputSpec): estimating the PSF [0]' psf_alg = traits.Enum(0, 1, argstr='-psf_alg %d', desc=desc) - # Set the number of omp thread to use - omp_core_val = traits.Int(desc='Number of openmp thread to use', - argstr='-omp %d') - class RegResampleOutputSpec(TraitedSpec): """ Output Spec for RegResample. """ @@ -109,9 +106,9 @@ class RegResample(NiftyRegCommand): >>> node.inputs.trans_file = 'warpfield.nii' >>> node.inputs.inter_val = 'LIN' >>> node.inputs.omp_core_val = 4 - >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + >>> node.cmdline # doctest: +ALLOW_UNICODE 'reg_resample -flo im2.nii -inter 1 -omp 4 -ref im1.nii -trans \ -warpfield.nii -res .../im2_res.nii.gz' +warpfield.nii -res im2_res.nii.gz' """ _cmd = get_custom_path('reg_resample') @@ -126,15 +123,13 @@ def _format_arg(self, name, spec, value): else: return super(RegResample, self)._format_arg(name, spec, value) - def _gen_filename(self, name): - if name == 'out_file': - return self._gen_fname(self.inputs.flo_file, - suffix='_%s' % self.inputs.type, - ext='.nii.gz') - return None + def _overload_extension(self, value, name=None): + path, base, _ = split_filename(value) + suffix = self.inputs.type + return os.path.join(path, '{0}_{1}.nii.gz'.format(base, suffix)) -class RegJacobianInputSpec(CommandLineInputSpec): +class RegJacobianInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegJacobian. """ # Reference file name desc = 'Reference/target file (required if specifying CPP transformations.' @@ -151,13 +146,11 @@ class RegJacobianInputSpec(CommandLineInputSpec): argstr='-%s', position=-2, desc='Type of jacobian outcome') - out_file = File(genfile=True, + out_file = File(name_source=['trans_file'], + name_template='%s', desc='The output jacobian determinant file name', argstr='%s', position=-1) - # Set the number of omp thread to use - omp_core_val = traits.Int(desc='Number of openmp thread to use', - argstr='-omp %i') class RegJacobianOutputSpec(TraitedSpec): @@ -180,24 +173,22 @@ class RegJacobian(NiftyRegCommand): >>> node.inputs.ref_file = 'im1.nii' >>> node.inputs.trans_file = 'warpfield.nii' >>> node.inputs.omp_core_val = 4 - >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE + >>> node.cmdline # doctest: +ALLOW_UNICODE 'reg_jacobian -omp 4 -ref im1.nii -trans warpfield.nii -jac \ -.../warpfield_jac.nii.gz' +warpfield_jac.nii.gz' """ _cmd = get_custom_path('reg_jacobian') input_spec = RegJacobianInputSpec output_spec = RegJacobianOutputSpec - def _gen_filename(self, name): - if name == 'out_file': - return self._gen_fname(self.inputs.trans_file, - suffix='_%s' % self.inputs.type, - ext='.nii.gz') - return None + def _overload_extension(self, value, name=None): + path, base, _ = split_filename(value) + suffix = self.inputs.type + return os.path.join(path, '{0}_{1}.nii.gz'.format(base, suffix)) -class RegToolsInputSpec(CommandLineInputSpec): +class RegToolsInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegTools. """ # Input image file in_file = File(exists=True, @@ -276,10 +267,6 @@ class RegToolsInputSpec(CommandLineInputSpec): desc=desc, argstr='-smoG %f %f %f') - # Set the number of omp thread to use - omp_core_val = traits.Int(desc='Number of openmp thread to use', - argstr='-omp %i') - class RegToolsOutputSpec(TraitedSpec): """ Output Spec for RegTools. """ @@ -312,7 +299,7 @@ class RegTools(NiftyRegCommand): _suffix = '_tools' -class RegAverageInputSpec(CommandLineInputSpec): +class RegAverageInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegAverage. """ avg_files = traits.List(File(exist=True), position=1, @@ -424,10 +411,11 @@ def _gen_filename(self, name): if isdefined(self.inputs.avg_lts_files): return self._gen_fname(self._suffix, ext='.txt') elif isdefined(self.inputs.avg_files): - _, _, ext = split_filename(self.inputs.avg_files[0]) - if ext not in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz']: - return self._gen_fname(self._suffix, ext=ext) + _, _, _ext = split_filename(self.inputs.avg_files[0]) + if _ext not in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz']: + return self._gen_fname(self._suffix, ext=_ext) return self._gen_fname(self._suffix, ext='.nii.gz') + return None @property @@ -440,7 +428,7 @@ def cmdline(self): return '%s --cmd_file %s' % (self.cmd, reg_average_cmd) -class RegTransformInputSpec(CommandLineInputSpec): +class RegTransformInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegTransform. """ ref1_file = File(exists=True, desc='The input reference/target image', @@ -584,15 +572,10 @@ class RegTransformInputSpec(CommandLineInputSpec): argstr='%s', desc='transformation file to write') - # Set the number of omp thread to use - omp_core_val = traits.Int(desc='Number of openmp thread to use', - argstr='-omp %i') - class RegTransformOutputSpec(TraitedSpec): """ Output Spec for RegTransform. """ - desc = 'Output File (transformation in any format)' - out_file = File(exists=True, desc=desc) + out_file = File(desc='Output File (transformation in any format)') class RegTransform(NiftyRegCommand): @@ -679,7 +662,7 @@ def _list_outputs(self): return outputs -class RegMeasureInputSpec(CommandLineInputSpec): +class RegMeasureInputSpec(NiftyRegCommandInputSpec): """ Input Spec for RegMeasure. """ # Input reference file ref_file = File(exists=True, @@ -695,12 +678,10 @@ class RegMeasureInputSpec(CommandLineInputSpec): mandatory=True, argstr='-%s', desc='Measure of similarity to compute') - out_file = File(genfile=True, + out_file = File(name_source=['flo_file'], + name_template='%s', argstr='-out %s', desc='The output text file containing the measure') - # Set the number of omp thread to use - omp_core_val = traits.Int(desc='Number of openmp thread to use', - argstr='-omp %i') class RegMeasureOutputSpec(TraitedSpec): @@ -723,17 +704,15 @@ class RegMeasure(NiftyRegCommand): >>> node.inputs.flo_file = 'im2.nii' >>> node.inputs.measure_type = 'lncc' >>> node.inputs.omp_core_val = 4 - >>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE - 'reg_measure -flo im2.nii -lncc -omp 4 -out .../im2_lncc.txt -ref im1.nii' + >>> node.cmdline # doctest: +ALLOW_UNICODE + 'reg_measure -flo im2.nii -lncc -omp 4 -out im2_lncc.txt -ref im1.nii' """ _cmd = get_custom_path('reg_measure') input_spec = RegMeasureInputSpec output_spec = RegMeasureOutputSpec - def _gen_filename(self, name): - if name == 'out_file': - return self._gen_fname(self.inputs.flo_file, - suffix='_%s' % self.inputs.measure_type, - ext='.txt') - return None + def _overload_extension(self, value, name=None): + path, base, _ = split_filename(value) + suffix = self.inputs.measure_type + return os.path.join(path, '{0}_{1}.txt'.format(base, suffix)) diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py b/nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py index 6a32598cc7..616279e706 100644 --- a/nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py @@ -42,6 +42,8 @@ def test_RegAverage_inputs(): ignore_exception=dict(nohash=True, usedefault=True, ), + omp_core_val=dict(argstr='-omp %i', + ), out_file=dict(argstr='%s', genfile=True, position=0, diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py b/nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py index badf703863..513f94388d 100644 --- a/nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py @@ -15,7 +15,8 @@ def test_RegJacobian_inputs(): omp_core_val=dict(argstr='-omp %i', ), out_file=dict(argstr='%s', - genfile=True, + name_source=['trans_file'], + name_template='%s', position=-1, ), ref_file=dict(argstr='-ref %s', diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py b/nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py index adc4e7e6e9..47d4d77049 100644 --- a/nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py @@ -21,7 +21,8 @@ def test_RegMeasure_inputs(): omp_core_val=dict(argstr='-omp %i', ), out_file=dict(argstr='-out %s', - genfile=True, + name_source=['flo_file'], + name_template='%s', ), ref_file=dict(argstr='-ref %s', mandatory=True, diff --git a/nipype/interfaces/niftyreg/tests/test_auto_RegResample.py b/nipype/interfaces/niftyreg/tests/test_auto_RegResample.py index e6fb2442eb..2d26ae4379 100644 --- a/nipype/interfaces/niftyreg/tests/test_auto_RegResample.py +++ b/nipype/interfaces/niftyreg/tests/test_auto_RegResample.py @@ -17,10 +17,11 @@ def test_RegResample_inputs(): ), inter_val=dict(argstr='-inter %d', ), - omp_core_val=dict(argstr='-omp %d', + omp_core_val=dict(argstr='-omp %i', ), out_file=dict(argstr='%s', - genfile=True, + name_source=['flo_file'], + name_template='%s', position=-1, ), pad_val=dict(argstr='-pad %f', diff --git a/nipype/interfaces/niftyreg/tests/test_regutils.py b/nipype/interfaces/niftyreg/tests/test_regutils.py index e79e38ef7e..b2e1357811 100644 --- a/nipype/interfaces/niftyreg/tests/test_regutils.py +++ b/nipype/interfaces/niftyreg/tests/test_regutils.py @@ -42,7 +42,7 @@ def test_reg_resample_res(): flo=flo_file, ref=ref_file, trans=trans_file, - res=os.path.join(os.getcwd(), 'im2_res.nii.gz')) + res='im2_res.nii.gz') assert nr_resample.cmdline == expected_cmd @@ -62,7 +62,7 @@ def test_reg_resample_res(): flo=flo_file, ref=ref_file, trans=trans_file, - blank=os.path.join(os.getcwd(), 'im2_blank.nii.gz')) + blank='im2_blank.nii.gz') assert nr_resample_2.cmdline == expected_cmd @@ -94,7 +94,7 @@ def test_reg_jacobian_jac(): cmd=get_custom_path('reg_jacobian'), ref=ref_file, trans=trans_file, - jac=os.path.join(os.getcwd(), 'warpfield_jac.nii.gz')) + jac='warpfield_jac.nii.gz') assert nr_jacobian.cmdline == expected_cmd @@ -110,7 +110,7 @@ def test_reg_jacobian_jac(): cmd=get_custom_path('reg_jacobian'), ref=ref_file, trans=trans_file, - jac=os.path.join(os.getcwd(), 'warpfield_jacM.nii.gz')) + jac='warpfield_jacM.nii.gz') assert nr_jacobian_2.cmdline == expected_cmd @@ -126,7 +126,7 @@ def test_reg_jacobian_jac(): cmd=get_custom_path('reg_jacobian'), ref=ref_file, trans=trans_file, - jac=os.path.join(os.getcwd(), 'warpfield_jacL.nii.gz')) + jac='warpfield_jacL.nii.gz') assert nr_jacobian_3.cmdline == expected_cmd @@ -453,7 +453,7 @@ def test_reg_measure(): expected_cmd = cmd_tmp.format( cmd=get_custom_path('reg_measure'), flo=flo_file, - out=os.path.join(os.getcwd(), 'im2_lncc.txt'), + out='im2_lncc.txt', ref=ref_file) assert nr_measure.cmdline == expected_cmd From 319d6e3e51cf4c6b7101965fe49bf01c954fdf00 Mon Sep 17 00:00:00 2001 From: byvernault Date: Wed, 26 Apr 2017 17:10:55 +0100 Subject: [PATCH 18/20] Edit for changes suggested by oesteban --- nipype/interfaces/niftyreg/base.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index f13e94ad39..291eca3ae8 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -22,6 +22,7 @@ from builtins import property, super from distutils.version import StrictVersion import os +import shutil import subprocess from warnings import warn @@ -30,20 +31,17 @@ def get_custom_path(command): - try: - specific_dir = os.environ['NIFTYREGDIR'] - command = os.path.join(specific_dir, command) - return command - except KeyError: - return command + return os.path.join(os.getenv('NIFTYREGDIR', ''), command) def no_niftyreg(cmd='reg_f3d'): - if True in [os.path.isfile(os.path.join(path, cmd)) and - os.access(os.path.join(path, cmd), os.X_OK) - for path in os.environ["PATH"].split(os.pathsep)]: - return False - return True + try: + return shutil.which(cmd) is None + except AttributeError: # Python < 3.3 + return not any( + [os.path.isfile(os.path.join(path, cmd)) and + os.access(os.path.join(path, cmd), os.X_OK) + for path in os.environ["PATH"].split(os.pathsep)]) class NiftyRegCommandInputSpec(CommandLineInputSpec): @@ -101,9 +99,7 @@ def version(self): return self.get_version() def exists(self): - if self.get_version() is None: - return False - return True + return not self.get_version() is None def _run_interface(self, runtime): # Update num threads estimate from OMP_NUM_THREADS env var From 61bfeb4d4b1e914e1634c2c2144ab81ae7f589cb Mon Sep 17 00:00:00 2001 From: byvernault Date: Thu, 27 Apr 2017 13:07:14 +0100 Subject: [PATCH 19/20] fixing flake8 issue --- nipype/interfaces/niftyreg/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index 291eca3ae8..b36283a398 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -99,7 +99,7 @@ def version(self): return self.get_version() def exists(self): - return not self.get_version() is None + return self.get_version() is not None def _run_interface(self, runtime): # Update num threads estimate from OMP_NUM_THREADS env var From 215f5f07f4b03d4e8bfc30551a739d4df777305e Mon Sep 17 00:00:00 2001 From: byvernault Date: Fri, 28 Apr 2017 16:59:37 +0100 Subject: [PATCH 20/20] setting self.numthreads to omp_core_val if set --- nipype/interfaces/niftyreg/base.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nipype/interfaces/niftyreg/base.py b/nipype/interfaces/niftyreg/base.py index b36283a398..9a21fcfd2e 100644 --- a/nipype/interfaces/niftyreg/base.py +++ b/nipype/interfaces/niftyreg/base.py @@ -108,6 +108,11 @@ def _run_interface(self, runtime): self.inputs.environ['OMP_NUM_THREADS'] = self.num_threads return super(NiftyRegCommand, self)._run_interface(runtime) + def _format_arg(self, name, spec, value): + if name == 'omp_core_val': + self.numthreads = value + return super(NiftyRegCommand, self)._format_arg(name, spec, value) + def _gen_fname(self, basename, out_dir=None, suffix=None, ext=None): if basename == '': msg = 'Unable to generate filename for command %s. ' % self.cmd