From 1ef1821c8faab1b64fe4281dc564786eba414894 Mon Sep 17 00:00:00 2001 From: Salma BOUGACHA Date: Wed, 18 Jul 2018 18:14:49 +0200 Subject: [PATCH 1/3] add option to save transform --- nipype/interfaces/afni/preprocess.py | 53 ++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/nipype/interfaces/afni/preprocess.py b/nipype/interfaces/afni/preprocess.py index 19876b1317..d66faf10c6 100644 --- a/nipype/interfaces/afni/preprocess.py +++ b/nipype/interfaces/afni/preprocess.py @@ -2758,7 +2758,8 @@ class WarpInputSpec(AFNICommandInputSpec): name_template='%s_warp', desc='output image file name', argstr='-prefix %s', - name_source='in_file') + name_source='in_file', + keep_extension=True) tta2mni = traits.Bool( desc='transform dataset from Talairach to MNI152', argstr='-tta2mni') mni2tta = traits.Bool( @@ -2789,6 +2790,13 @@ class WarpInputSpec(AFNICommandInputSpec): argstr='-zpad %d') verbose = traits.Bool( desc='Print out some information along the way.', argstr='-verb') + save_warp = traits.Bool( + False, usedefault=True, desc='save warp as .mat file') + + +class WarpOutputSpec(TraitedSpec): + out_file = File(desc='Warped file.', exists=True) + warp_file = File(desc='warp transfrom .mat file') class Warp(AFNICommand): @@ -2820,7 +2828,48 @@ class Warp(AFNICommand): """ _cmd = '3dWarp' input_spec = WarpInputSpec - output_spec = AFNICommandOutputSpec + output_spec = WarpOutputSpec + + def _run_interface(self, runtime): + runtime = super(Warp, self)._run_interface(runtime) + + if self.inputs.save_warp: + import numpy as np + if not self.inputs.out_file: + warp_file = fname_presuffix(self.inputs.in_file, + suffix='_warp_transform.mat', + use_ext=False) + else: + warp_file = fname_presuffix(self.inputs.out_file, + suffix='_transform.mat', + use_ext=False) + np.savetxt(warp_file, [runtime.stdout], fmt=str('%s')) + return runtime + + def _list_outputs(self): + outputs = self.output_spec().get() + if not self.inputs.out_file: + fname, ext = os.path.splitext(self.inputs.in_file) + if '.gz' in ext: + _, ext2 = os.path.splitext(fname) + ext = ext2 + ext + out_file = self._gen_fname(self.inputs.in_file, suffix='_warp', + ext=ext) + outputs['out_file'] = os.path.abspath(out_file) + if self.inputs.save_warp: + warp_file = fname_presuffix(self.inputs.in_file, + suffix='_warp_transform.mat', + use_ext=False) + outputs['warp_file'] = os.path.abspath(warp_file) + else: + outputs['out_file'] = os.path.abspath(self.inputs.out_file) + if self.inputs.save_warp: + warp_file = fname_presuffix(self.inputs.out_file, + suffix='_transform.mat', + use_ext=False) + outputs['warp_file'] = os.path.abspath(warp_file) + + return outputs class QwarpPlusMinusInputSpec(CommandLineInputSpec): From 50f8d81821a6e6facb0a8ac322e6176d32d04db3 Mon Sep 17 00:00:00 2001 From: salma1601 Date: Thu, 19 Jul 2018 15:43:06 +0200 Subject: [PATCH 2/3] update warp interface test --- nipype/interfaces/afni/tests/test_auto_Warp.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/nipype/interfaces/afni/tests/test_auto_Warp.py b/nipype/interfaces/afni/tests/test_auto_Warp.py index 929b7b7dd4..76fa4fd22d 100644 --- a/nipype/interfaces/afni/tests/test_auto_Warp.py +++ b/nipype/interfaces/afni/tests/test_auto_Warp.py @@ -29,10 +29,12 @@ def test_Warp_inputs(): oblique_parent=dict(argstr='-oblique_parent %s', ), out_file=dict( argstr='-prefix %s', + keep_extension=True, name_source='in_file', name_template='%s_warp', ), outputtype=dict(), + save_warp=dict(usedefault=True, ), tta2mni=dict(argstr='-tta2mni', ), verbose=dict(argstr='-verb', ), zpad=dict(argstr='-zpad %d', ), @@ -43,7 +45,10 @@ def test_Warp_inputs(): for metakey, value in list(metadata.items()): assert getattr(inputs.traits()[key], metakey) == value def test_Warp_outputs(): - output_map = dict(out_file=dict(), ) + output_map = dict( + out_file=dict(), + warp_file=dict(), + ) outputs = Warp.output_spec() for key, metadata in list(output_map.items()): From 3dc803c7294f58b1ec9e3cf20088d2dcd6df118a Mon Sep 17 00:00:00 2001 From: salma1601 Date: Wed, 25 Jul 2018 00:04:20 +0200 Subject: [PATCH 3/3] address comments --- nipype/interfaces/afni/preprocess.py | 43 +++++-------------- .../interfaces/afni/tests/test_auto_Warp.py | 2 +- 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/nipype/interfaces/afni/preprocess.py b/nipype/interfaces/afni/preprocess.py index d66faf10c6..35887ddc28 100644 --- a/nipype/interfaces/afni/preprocess.py +++ b/nipype/interfaces/afni/preprocess.py @@ -13,7 +13,7 @@ from ...utils.filemanip import (load_json, save_json, split_filename, fname_presuffix) from ..base import (CommandLineInputSpec, CommandLine, TraitedSpec, traits, - isdefined, File, InputMultiPath, Undefined, Str, + isdefined, File, InputMultiPath, Undefined, Str, InputMultiObject) from .base import (AFNICommandBase, AFNICommand, AFNICommandInputSpec, @@ -2563,7 +2563,7 @@ class TProject(AFNICommand): _cmd = '3dTproject' input_spec = TProjectInputSpec output_spec = AFNICommandOutputSpec - + class TShiftInputSpec(AFNICommandInputSpec): in_file = File( @@ -2791,12 +2791,12 @@ class WarpInputSpec(AFNICommandInputSpec): verbose = traits.Bool( desc='Print out some information along the way.', argstr='-verb') save_warp = traits.Bool( - False, usedefault=True, desc='save warp as .mat file') + desc='save warp as .mat file', requires=['verbose']) class WarpOutputSpec(TraitedSpec): out_file = File(desc='Warped file.', exists=True) - warp_file = File(desc='warp transfrom .mat file') + warp_file = File(desc='warp transform .mat file') class Warp(AFNICommand): @@ -2835,39 +2835,16 @@ def _run_interface(self, runtime): if self.inputs.save_warp: import numpy as np - if not self.inputs.out_file: - warp_file = fname_presuffix(self.inputs.in_file, - suffix='_warp_transform.mat', - use_ext=False) - else: - warp_file = fname_presuffix(self.inputs.out_file, - suffix='_transform.mat', - use_ext=False) + warp_file = self._list_outputs()['warp_file'] np.savetxt(warp_file, [runtime.stdout], fmt=str('%s')) return runtime def _list_outputs(self): - outputs = self.output_spec().get() - if not self.inputs.out_file: - fname, ext = os.path.splitext(self.inputs.in_file) - if '.gz' in ext: - _, ext2 = os.path.splitext(fname) - ext = ext2 + ext - out_file = self._gen_fname(self.inputs.in_file, suffix='_warp', - ext=ext) - outputs['out_file'] = os.path.abspath(out_file) - if self.inputs.save_warp: - warp_file = fname_presuffix(self.inputs.in_file, - suffix='_warp_transform.mat', - use_ext=False) - outputs['warp_file'] = os.path.abspath(warp_file) - else: - outputs['out_file'] = os.path.abspath(self.inputs.out_file) - if self.inputs.save_warp: - warp_file = fname_presuffix(self.inputs.out_file, - suffix='_transform.mat', - use_ext=False) - outputs['warp_file'] = os.path.abspath(warp_file) + outputs = super(Warp, self)._list_outputs() + if self.inputs.save_warp: + outputs['warp_file'] = fname_presuffix(outputs['out_file'], + suffix='_transform.mat', + use_ext=False) return outputs diff --git a/nipype/interfaces/afni/tests/test_auto_Warp.py b/nipype/interfaces/afni/tests/test_auto_Warp.py index 76fa4fd22d..b85692310a 100644 --- a/nipype/interfaces/afni/tests/test_auto_Warp.py +++ b/nipype/interfaces/afni/tests/test_auto_Warp.py @@ -34,7 +34,7 @@ def test_Warp_inputs(): name_template='%s_warp', ), outputtype=dict(), - save_warp=dict(usedefault=True, ), + save_warp=dict(requires=['verbose'], ), tta2mni=dict(argstr='-tta2mni', ), verbose=dict(argstr='-verb', ), zpad=dict(argstr='-zpad %d', ),