Skip to content

Commit 0b07124

Browse files
committed
TEST: Test xor, requires constraints on name_source traits
1 parent 1d2a5c5 commit 0b07124

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

nipype/interfaces/base/core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,9 @@ def _filename_from_source(self, name, chain=None):
11231123
base = self._filename_from_source(ns, chain)
11241124
if isdefined(base):
11251125
_, _, source_ext = split_filename(base)
1126+
else:
1127+
# Do not generate filename when required fields are missing
1128+
return retval
11261129

11271130
chain = None
11281131
retval = name_template % base

nipype/interfaces/base/tests/test_specs.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,55 @@ class TestCycle(nib.CommandLine):
274274
assert '%s_generated_mootpl' % nme in res
275275

276276

277+
def test_namesource_constraints(setup_file):
278+
tmp_infile = setup_file
279+
tmpd, nme, ext = split_filename(tmp_infile)
280+
281+
class constrained_spec(nib.CommandLineInputSpec):
282+
in_file = nib.File(argstr="%s", position=1)
283+
threshold = traits.Float(
284+
argstr="%g",
285+
xor=['mask_file'],
286+
position=2)
287+
mask_file = nib.File(
288+
argstr="%s",
289+
name_source=['in_file'],
290+
name_template='%s_mask',
291+
keep_extension=True,
292+
xor=['threshold'],
293+
position=2)
294+
out_file1 = nib.File(
295+
argstr="%s",
296+
name_source=['in_file'],
297+
name_template='%s_out1',
298+
keep_extension=True,
299+
position=3)
300+
out_file2 = nib.File(
301+
argstr="%s",
302+
name_source=['in_file'],
303+
name_template='%s_out2',
304+
keep_extension=True,
305+
requires=['threshold'],
306+
position=4)
307+
308+
class TestConstrained(nib.CommandLine):
309+
_cmd = "mycommand"
310+
input_spec = constrained_spec
311+
312+
tc = TestConstrained()
313+
314+
# name_source undefined, so template traits remain undefined
315+
assert tc.cmdline == 'mycommand'
316+
317+
# mask_file and out_file1 enabled by name_source definition
318+
tc.inputs.in_file = os.path.basename(tmp_infile)
319+
assert tc.cmdline == 'mycommand foo.txt foo_mask.txt foo_out1.txt'
320+
321+
# mask_file disabled by threshold, out_file2 enabled by threshold
322+
tc.inputs.threshold = 10.
323+
assert tc.cmdline == 'mycommand foo.txt 10 foo_out1.txt foo_out2.txt'
324+
325+
277326
def test_TraitedSpec_withFile(setup_file):
278327
tmp_infile = setup_file
279328
tmpd, nme = os.path.split(tmp_infile)

0 commit comments

Comments
 (0)