From 17072ab133fd5275903ca23d8ad4334e655c979c Mon Sep 17 00:00:00 2001 From: Zach Lindsey Date: Mon, 15 Apr 2024 13:33:52 -0500 Subject: [PATCH] remove stuff form other branch, add string interpolation --- .../afni/tests/test_auto_Zeropad.py | 1 + .../afni/tests/test_extra_Zeropad.py | 34 ------------------- nipype/interfaces/afni/utils.py | 8 +---- nipype/interfaces/utility/__init__.py | 2 +- nipype/interfaces/utility/base.py | 23 +++++++++++++ .../tests/test_auto_StringInterpolate.py | 26 ++++++++++++++ nipype/interfaces/utility/tests/test_base.py | 13 +++++++ 7 files changed, 65 insertions(+), 42 deletions(-) create mode 100644 nipype/interfaces/utility/tests/test_auto_StringInterpolate.py diff --git a/nipype/interfaces/afni/tests/test_auto_Zeropad.py b/nipype/interfaces/afni/tests/test_auto_Zeropad.py index bd5cd1eb34..4d6742f21e 100644 --- a/nipype/interfaces/afni/tests/test_auto_Zeropad.py +++ b/nipype/interfaces/afni/tests/test_auto_Zeropad.py @@ -70,6 +70,7 @@ def test_Zeropad_inputs(): out_file=dict( argstr="-prefix %s", extensions=None, + name_template="zeropad", ), outputtype=dict(), z=dict( diff --git a/nipype/interfaces/afni/tests/test_extra_Zeropad.py b/nipype/interfaces/afni/tests/test_extra_Zeropad.py index 086d65bbf9..5da1ffe8bb 100644 --- a/nipype/interfaces/afni/tests/test_extra_Zeropad.py +++ b/nipype/interfaces/afni/tests/test_extra_Zeropad.py @@ -3,37 +3,3 @@ from nipype.interfaces.afni import Zeropad from nipype.testing.fixtures import create_files_in_directory - -def test_zeropad_handles_outfile_default(create_files_in_directory): - filelist, outdir = create_files_in_directory - zp = Zeropad(I=1) - zp.inputs.in_files = filelist[0] - - result = zp.run() - - assert (Path(outdir) / "zeropad+tlrc.BRIK").exists() - assert Path(result.outputs.out_file).name == "zeropad+tlrc.BRIK" - - -def test_zeropad_handles_outfile_specified_nii_gz(create_files_in_directory): - filelist, outdir = create_files_in_directory - zp = Zeropad(I=1, out_file="padded.nii.gz") - zp.inputs.in_files = filelist[0] - - result = zp.run() - - assert (Path(outdir) / "padded.nii.gz").exists() - assert Path(result.outputs.out_file).name == "padded.nii.gz" - - -def test_zeropad_keeps_file_after_node_run(create_files_in_directory): - filelist, outdir = create_files_in_directory - - zp = Node( - Zeropad(I=1, out_file="padded.nii.gz"), name="test_zeropad", base_dir=outdir - ) - zp.inputs.in_files = Path(outdir) / filelist[0] - - result = zp.run() - assert (Path(zp.output_dir()) / "padded.nii.gz").exists() - assert Path(result.outputs.out_file).name == "padded.nii.gz" diff --git a/nipype/interfaces/afni/utils.py b/nipype/interfaces/afni/utils.py index f10ca43048..5fb1cd1e64 100644 --- a/nipype/interfaces/afni/utils.py +++ b/nipype/interfaces/afni/utils.py @@ -3391,6 +3391,7 @@ class ZeropadInputSpec(AFNICommandInputSpec): copyfile=False, ) out_file = File( + name_template="zeropad", desc="output dataset prefix name (default 'zeropad')", argstr="-prefix %s", ) @@ -3496,10 +3497,3 @@ class Zeropad(AFNICommand): _cmd = "3dZeropad" input_spec = ZeropadInputSpec output_spec = AFNICommandOutputSpec - - def _list_outputs(self): - out_file = getattr(self.inputs, "out_file") - - if not isdefined(out_file): - out_file = "zeropad+tlrc.BRIK" - return {"out_file": op.abspath(out_file)} diff --git a/nipype/interfaces/utility/__init__.py b/nipype/interfaces/utility/__init__.py index b4df1c2afb..544641bcbc 100644 --- a/nipype/interfaces/utility/__init__.py +++ b/nipype/interfaces/utility/__init__.py @@ -6,6 +6,6 @@ Requires Packages to be installed """ -from .base import IdentityInterface, Rename, Select, Split, Merge, AssertEqual +from .base import IdentityInterface, Rename, Select, Split, Merge, AssertEqual, StringInterpolate from .csv import CSVReader from .wrappers import Function diff --git a/nipype/interfaces/utility/base.py b/nipype/interfaces/utility/base.py index 564966cb5b..0c3643546f 100644 --- a/nipype/interfaces/utility/base.py +++ b/nipype/interfaces/utility/base.py @@ -430,3 +430,26 @@ def _run_interface(self, runtime): if not np.array_equal(data1, data2): raise RuntimeError("Input images are not exactly equal") return runtime + + +class StringInterpolateInputSpec(DynamicTraitedSpec): + input_string = Str(mandatory = True) + +class StringInterpolateOutputSpec(TraitedSpec): + output_string = Str() + +class StringInterpolate(BaseInterface): + input_spec = StringInterpolateInputSpec + output_spec = StringInterpolateOutputSpec + + def _run_interface(self, runtime): + + subs = { + k: getattr(self.inputs, k) for k,_ in self.inputs.items() if k != "input_string" + } + self._output_string = self.inputs.input_string.format(**subs) + + def _list_outputs(self): + return { + "output_string": self._output_string + } \ No newline at end of file diff --git a/nipype/interfaces/utility/tests/test_auto_StringInterpolate.py b/nipype/interfaces/utility/tests/test_auto_StringInterpolate.py new file mode 100644 index 0000000000..cc9d3355ca --- /dev/null +++ b/nipype/interfaces/utility/tests/test_auto_StringInterpolate.py @@ -0,0 +1,26 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from ..base import StringInterpolate + + +def test_StringInterpolate_inputs(): + input_map = dict( + input_string=dict( + mandatory=True, + ), + ) + inputs = StringInterpolate.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_StringInterpolate_outputs(): + output_map = dict( + output_string=dict(), + ) + outputs = StringInterpolate.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/utility/tests/test_base.py b/nipype/interfaces/utility/tests/test_base.py index 4a4e6d8899..eaa727b354 100644 --- a/nipype/interfaces/utility/tests/test_base.py +++ b/nipype/interfaces/utility/tests/test_base.py @@ -78,3 +78,16 @@ def test_merge(tmpdir, args, kwargs, in_lists, expected): assert not isdefined(res.outputs.out) else: assert res.outputs.out == expected + + +def test_string_interpolate(): + + interpolate = utility.StringInterpolate() + + interpolate.inputs.input_string = "{greeting}, {name}!" + interpolate.inputs.greeting = "Hello" + interpolate.inputs.name = "Nipype" + + res = interpolate.run() + + assert res.outputs.output_string == "Hello, Nipype!" \ No newline at end of file