Skip to content

Commit

Permalink
remove stuff form other branch, add string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Lindsey committed Apr 15, 2024
1 parent 962f1cd commit 17072ab
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 42 deletions.
1 change: 1 addition & 0 deletions nipype/interfaces/afni/tests/test_auto_Zeropad.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def test_Zeropad_inputs():
out_file=dict(
argstr="-prefix %s",
extensions=None,
name_template="zeropad",
),
outputtype=dict(),
z=dict(
Expand Down
34 changes: 0 additions & 34 deletions nipype/interfaces/afni/tests/test_extra_Zeropad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 1 addition & 7 deletions nipype/interfaces/afni/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -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)}
2 changes: 1 addition & 1 deletion nipype/interfaces/utility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions nipype/interfaces/utility/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions nipype/interfaces/utility/tests/test_auto_StringInterpolate.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions nipype/interfaces/utility/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"

0 comments on commit 17072ab

Please sign in to comment.