From 5b4e9bb288214a05c0d479a2e934b236f0bcb770 Mon Sep 17 00:00:00 2001 From: Salma BOUGACHA Date: Fri, 2 Feb 2018 10:43:41 +0100 Subject: [PATCH 1/7] add afni nwarpadjust --- nipype/interfaces/afni/utils.py | 86 +++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/nipype/interfaces/afni/utils.py b/nipype/interfaces/afni/utils.py index 6cc187a973..9ae7ae5083 100644 --- a/nipype/interfaces/afni/utils.py +++ b/nipype/interfaces/afni/utils.py @@ -1511,6 +1511,92 @@ def _list_outputs(self): return outputs +class NwarpAdjustInputSpec(CommandLineInputSpec): + warps = InputMultiPath( + File(exists=True), + minlen=5, + mandatory=True, + argstr='-nwarp %s', + desc='List of input 3D warp datasets') + in_files = InputMultiPath( + File(exists=True), + minlen=5, + mandatory=False, + argstr='-source %s', + desc='List of input 3D datasets to be warped by the adjusted warp ' + 'datasets. There must be exactly as many of these datasets as ' + 'there are input warps.') + out_file = File( + desc='Output mean dataset, only needed if in_files are also given. ' + 'The output dataset will be on the common grid shared by the ' + 'source datasets.', + argstr='-prefix %s', + mandatory=False, + name_source='in_files', + name_template='%s_NwarpAdjust', + keep_extension=True, + xand=['in_files']) + + +class NwarpAdjust(AFNICommandBase): + """This program takes as input a bunch of 3D warps, averages them, + and computes the inverse of this average warp. It then composes + each input warp with this inverse average to 'adjust' the set of + warps. Optionally, it can also read in a set of 1-brick datasets + corresponding to the input warps, and warp each of them, and average + those. + + For complete details, see the `3dNwarpAdjust Documentation. + `_ + + Examples + ======== + + >>> from nipype.interfaces import afni + >>> adjust = afni.NwarpAdjust() + >>> adjust.inputs.warps = [Fred_WARP+tlrc, Fred.Xaff12.1D, Fred.Xaff12.1D, Fred.Xaff12.1D, Fred.Xaff12.1D] + >>> adjust.cmdline + "NwarpAdjust -nwarp Fred_WARP+tlrc Fred.Xaff12.1D Fred.Xaff12.1D Fred.Xaff12.1D Fred.Xaff12.1D" + >>> res = adjust.run() # doctest: +SKIP + + """ + _cmd = '3dNwarpAdjust' + input_spec = NwarpAdjustInputSpec + output_spec = AFNICommandOutputSpec + + def _parse_inputs(self, skip=None): + if not self.inputs.in_files: + if skip is None: + skip = [] + skip += ['out_file'] + return super(NwarpAdjust, self)._parse_inputs(skip=skip) + + def _gen_filename(self, name): + if name == 'out_file': + return self._list_outputs()[name] + return None + + def _list_outputs(self): + outputs = self.output_spec().get() + + if self.inputs.out_file: + outputs['out_file'] = os.path.abspath(self.inputs.out_file) + + if self.inputs.in_files: + if self.inputs.out_file: + outputs['out_file'] = os.path.abspath(self.inputs.out_file) + else: + basename = os.path.basename(self.inputs.in_files[0]) + basename_noext, ext = op.splitext(basename) + if '.gz' in ext: + basename_noext, ext2 = op.splitext(basename_noext) + ext = ext2 + ext + outputs['out_file'] = os.path.abspath( + basename_noext + '_NwarpAdjust' + ext) + + return outputs + + class NwarpApplyInputSpec(CommandLineInputSpec): in_file = traits.Either( File(exists=True), From 711e3de0c6226c879d51c88f5a18292561f33994 Mon Sep 17 00:00:00 2001 From: salma1601 Date: Sun, 18 Feb 2018 18:37:50 +0100 Subject: [PATCH 2/7] add auto test --- .../afni/tests/test_auto_NwarpAdjust.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py diff --git a/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py new file mode 100644 index 0000000000..98264ddce2 --- /dev/null +++ b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py @@ -0,0 +1,50 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from __future__ import unicode_literals +from ..utils import NwarpAdjust + + +def test_NwarpAdjust_inputs(): + input_map = dict( + args=dict(argstr='%s', ), + environ=dict( + nohash=True, + usedefault=True, + ), + ignore_exception=dict( + deprecated='1.0.0', + nohash=True, + usedefault=True, + ), + in_files=dict( + argstr='-source %s', + mandatory=False, + ), + out_file=dict( + argstr='-prefix %s', + keep_extension=True, + mandatory=False, + name_source='in_files', + name_template='%s_NwarpAdjust', + xand=['in_files'], + ), + terminal_output=dict( + deprecated='1.0.0', + nohash=True, + ), + warps=dict( + argstr='-nwarp %s', + mandatory=True, + ), + ) + inputs = NwarpAdjust.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_NwarpAdjust_outputs(): + output_map = dict(out_file=dict(), ) + outputs = NwarpAdjust.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value From 2512fd2a0bb1b9c62ad6c22800da0afb6b1aaea7 Mon Sep 17 00:00:00 2001 From: salma1601 Date: Sun, 18 Feb 2018 20:03:54 +0100 Subject: [PATCH 3/7] add nwarpadjust to __init__ --- nipype/interfaces/afni/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nipype/interfaces/afni/__init__.py b/nipype/interfaces/afni/__init__.py index b28e5961f7..925011a19a 100644 --- a/nipype/interfaces/afni/__init__.py +++ b/nipype/interfaces/afni/__init__.py @@ -18,7 +18,7 @@ from .utils import ( ABoverlap, AFNItoNIFTI, Autobox, Axialize, BrickStat, Bucket, Calc, Cat, CatMatvec, CenterMass, ConvertDset, Copy, Dot, Edge3, Eval, FWHMx, - MaskTool, Merge, Notes, NwarpApply, NwarpCat, OneDToolPy, Refit, Resample, - TCat, TCatSubBrick, TStat, To3D, Unifize, Undump, ZCutUp, GCOR, Zcat, - Zeropad) + MaskTool, Merge, Notes, NwarpApply, NwarpAdjust, NwarpCat, OneDToolPy, + Refit, Resample, TCat, TCatSubBrick, TStat, To3D, Unifize, Undump, ZCutUp, + GCOR, Zcat, Zeropad) from .model import (Deconvolve, Remlfit, Synthesize) From 3c358521c419d246b4a0d6de7e41ae09aed5f5b1 Mon Sep 17 00:00:00 2001 From: salma1601 Date: Sun, 18 Feb 2018 21:23:05 +0100 Subject: [PATCH 4/7] fix failing doctests --- nipype/interfaces/afni/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nipype/interfaces/afni/utils.py b/nipype/interfaces/afni/utils.py index 9ae7ae5083..820d2ee1d7 100644 --- a/nipype/interfaces/afni/utils.py +++ b/nipype/interfaces/afni/utils.py @@ -1554,9 +1554,9 @@ class NwarpAdjust(AFNICommandBase): >>> from nipype.interfaces import afni >>> adjust = afni.NwarpAdjust() - >>> adjust.inputs.warps = [Fred_WARP+tlrc, Fred.Xaff12.1D, Fred.Xaff12.1D, Fred.Xaff12.1D, Fred.Xaff12.1D] + >>> adjust.inputs.warps = ['func2anat_InverseWarp.nii.gz', 'func2anat_InverseWarp.nii.gz', 'func2anat_InverseWarp.nii.gz', 'func2anat_InverseWarp.nii.gz', 'func2anat_InverseWarp.nii.gz'] >>> adjust.cmdline - "NwarpAdjust -nwarp Fred_WARP+tlrc Fred.Xaff12.1D Fred.Xaff12.1D Fred.Xaff12.1D Fred.Xaff12.1D" + '3dNwarpAdjust -nwarp func2anat_InverseWarp.nii.gz func2anat_InverseWarp.nii.gz func2anat_InverseWarp.nii.gz func2anat_InverseWarp.nii.gz func2anat_InverseWarp.nii.gz' >>> res = adjust.run() # doctest: +SKIP """ From 555f35d4f528716b5f18b358402497f15f761ada Mon Sep 17 00:00:00 2001 From: salma1601 Date: Fri, 9 Mar 2018 08:22:21 +0100 Subject: [PATCH 5/7] address Satra's comments --- nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py | 8 ++------ nipype/interfaces/afni/utils.py | 4 +--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py index 98264ddce2..9acee15e8d 100644 --- a/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py +++ b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py @@ -15,17 +15,13 @@ def test_NwarpAdjust_inputs(): nohash=True, usedefault=True, ), - in_files=dict( - argstr='-source %s', - mandatory=False, - ), + in_files=dict(argstr='-source %s', ), out_file=dict( argstr='-prefix %s', keep_extension=True, - mandatory=False, name_source='in_files', name_template='%s_NwarpAdjust', - xand=['in_files'], + xand=['requires'], ), terminal_output=dict( deprecated='1.0.0', diff --git a/nipype/interfaces/afni/utils.py b/nipype/interfaces/afni/utils.py index 820d2ee1d7..cd0e864903 100644 --- a/nipype/interfaces/afni/utils.py +++ b/nipype/interfaces/afni/utils.py @@ -1521,7 +1521,6 @@ class NwarpAdjustInputSpec(CommandLineInputSpec): in_files = InputMultiPath( File(exists=True), minlen=5, - mandatory=False, argstr='-source %s', desc='List of input 3D datasets to be warped by the adjusted warp ' 'datasets. There must be exactly as many of these datasets as ' @@ -1531,11 +1530,10 @@ class NwarpAdjustInputSpec(CommandLineInputSpec): 'The output dataset will be on the common grid shared by the ' 'source datasets.', argstr='-prefix %s', - mandatory=False, name_source='in_files', name_template='%s_NwarpAdjust', keep_extension=True, - xand=['in_files']) + xand=['requires']) class NwarpAdjust(AFNICommandBase): From 942e3428c4902c909305c84af80895a96ba7ec12 Mon Sep 17 00:00:00 2001 From: salma1601 Date: Fri, 9 Mar 2018 11:29:48 +0100 Subject: [PATCH 6/7] fix typo --- nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py | 2 +- nipype/interfaces/afni/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py index 9acee15e8d..cd041165ae 100644 --- a/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py +++ b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py @@ -21,7 +21,7 @@ def test_NwarpAdjust_inputs(): keep_extension=True, name_source='in_files', name_template='%s_NwarpAdjust', - xand=['requires'], + requires=['in_files'], ), terminal_output=dict( deprecated='1.0.0', diff --git a/nipype/interfaces/afni/utils.py b/nipype/interfaces/afni/utils.py index cd0e864903..5208595ca8 100644 --- a/nipype/interfaces/afni/utils.py +++ b/nipype/interfaces/afni/utils.py @@ -1533,7 +1533,7 @@ class NwarpAdjustInputSpec(CommandLineInputSpec): name_source='in_files', name_template='%s_NwarpAdjust', keep_extension=True, - xand=['requires']) + requires=['in_files']) class NwarpAdjust(AFNICommandBase): From 3a27625a57d2b1d8d7b1cb3538673c9afb3a3d76 Mon Sep 17 00:00:00 2001 From: salma1601 Date: Fri, 9 Mar 2018 22:55:56 +0100 Subject: [PATCH 7/7] remove _gen_filename --- nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py | 5 +++++ nipype/interfaces/afni/utils.py | 11 +---------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py index cd041165ae..cf427dd41a 100644 --- a/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py +++ b/nipype/interfaces/afni/tests/test_auto_NwarpAdjust.py @@ -16,6 +16,10 @@ def test_NwarpAdjust_inputs(): usedefault=True, ), in_files=dict(argstr='-source %s', ), + num_threads=dict( + nohash=True, + usedefault=True, + ), out_file=dict( argstr='-prefix %s', keep_extension=True, @@ -23,6 +27,7 @@ def test_NwarpAdjust_inputs(): name_template='%s_NwarpAdjust', requires=['in_files'], ), + outputtype=dict(), terminal_output=dict( deprecated='1.0.0', nohash=True, diff --git a/nipype/interfaces/afni/utils.py b/nipype/interfaces/afni/utils.py index 5208595ca8..9f306c7b85 100644 --- a/nipype/interfaces/afni/utils.py +++ b/nipype/interfaces/afni/utils.py @@ -1511,7 +1511,7 @@ def _list_outputs(self): return outputs -class NwarpAdjustInputSpec(CommandLineInputSpec): +class NwarpAdjustInputSpec(AFNICommandInputSpec): warps = InputMultiPath( File(exists=True), minlen=5, @@ -1569,17 +1569,9 @@ def _parse_inputs(self, skip=None): skip += ['out_file'] return super(NwarpAdjust, self)._parse_inputs(skip=skip) - def _gen_filename(self, name): - if name == 'out_file': - return self._list_outputs()[name] - return None - def _list_outputs(self): outputs = self.output_spec().get() - if self.inputs.out_file: - outputs['out_file'] = os.path.abspath(self.inputs.out_file) - if self.inputs.in_files: if self.inputs.out_file: outputs['out_file'] = os.path.abspath(self.inputs.out_file) @@ -1591,7 +1583,6 @@ def _list_outputs(self): ext = ext2 + ext outputs['out_file'] = os.path.abspath( basename_noext + '_NwarpAdjust' + ext) - return outputs