Skip to content

Commit fde07fa

Browse files
authored
Merge pull request #2549 from effigies/fix/name_source
FIX: Do not generate filename when required fields are missing
2 parents a2d0f57 + 0b07124 commit fde07fa

File tree

4 files changed

+71
-18
lines changed

4 files changed

+71
-18
lines changed

nipype/interfaces/base/core.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,13 @@ def _filename_from_source(self, name, chain=None):
10741074
return retval
10751075

10761076
# Do not generate filename when excluded by other inputs
1077-
if trait_spec.xor and any(isdefined(getattr(self.inputs, field))
1078-
for field in trait_spec.xor):
1077+
if any(isdefined(getattr(self.inputs, field))
1078+
for field in trait_spec.xor or ()):
1079+
return retval
1080+
1081+
# Do not generate filename when required fields are missing
1082+
if not all(isdefined(getattr(self.inputs, field))
1083+
for field in trait_spec.requires or ()):
10791084
return retval
10801085

10811086
if isdefined(retval) and "%s" in retval:
@@ -1118,6 +1123,9 @@ def _filename_from_source(self, name, chain=None):
11181123
base = self._filename_from_source(ns, chain)
11191124
if isdefined(base):
11201125
_, _, source_ext = split_filename(base)
1126+
else:
1127+
# Do not generate filename when required fields are missing
1128+
return retval
11211129

11221130
chain = None
11231131
retval = name_template % base
@@ -1144,8 +1152,9 @@ def _list_outputs(self):
11441152
out_name = name
11451153
if trait_spec.output_name is not None:
11461154
out_name = trait_spec.output_name
1147-
outputs[out_name] = \
1148-
os.path.abspath(self._filename_from_source(name))
1155+
fname = self._filename_from_source(name)
1156+
if isdefined(fname):
1157+
outputs[out_name] = os.path.abspath(fname)
11491158
return outputs
11501159

11511160
def _parse_inputs(self, skip=None):

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)

nipype/interfaces/fsl/tests/test_preprocess.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def test_flirt(setup_flirt):
313313
os.path.join(os.getcwd(), flirter.inputs.out_file)
314314
assert outs['out_matrix_file'] == \
315315
os.path.join(os.getcwd(), flirter.inputs.out_matrix_file)
316+
assert not isdefined(flirter.inputs.out_log)
316317

317318

318319
# Mcflirt

nipype/interfaces/niftyfit/dwi.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ class FitDwiInputSpec(CommandLineInputSpec):
6666
argstr='-nodiff %s')
6767

6868
# Output options, with templated output names based on the source image
69-
desc = 'Filename of multi-compartment model parameter map \
70-
(-ivim,-ball,-nod)'
71-
7269
mcmap_file = traits.File(
7370
name_source=['source_file'],
7471
name_template='%s_mcmap.nii.gz',
75-
desc=desc,
72+
desc='Filename of multi-compartment model parameter map '
73+
'(-ivim,-ball,-nod)',
7674
argstr='-mcmap %s',
7775
requires=['nodv_flag'])
7876

@@ -239,10 +237,7 @@ class FitDwiInputSpec(CommandLineInputSpec):
239237

240238
class FitDwiOutputSpec(TraitedSpec):
241239
""" Output Spec for FitDwi. """
242-
desc = 'Filename of multi-compartment model parameter map \
243-
(-ivim,-ball,-nod)'
244240

245-
mcmap_file = traits.File(desc=desc)
246241
error_file = traits.File(desc='Filename of parameter error maps')
247242
res_file = traits.File(desc='Filename of model residual map')
248243
syn_file = traits.File(desc='Filename of synthetic image')
@@ -253,10 +248,9 @@ class FitDwiOutputSpec(TraitedSpec):
253248
rgbmap_file = traits.File(desc='Filename of colour FA map')
254249
tenmap_file = traits.File(desc='Filename of tensor map')
255250
tenmap2_file = traits.File(desc='Filename of tensor map [lower tri]')
256-
desc = 'Filename of multi-compartment model parameter map \
257-
(-ivim,-ball,-nod).'
258251

259-
mcmap_file = traits.File(desc=desc)
252+
mcmap_file = traits.File(desc='Filename of multi-compartment model '
253+
'parameter map (-ivim,-ball,-nod).')
260254
mcout = traits.File(desc='Filename of mc samples (ascii text file)')
261255

262256

@@ -281,10 +275,10 @@ class FitDwi(NiftyFitCommand):
281275
>>> fit_dwi.inputs.rgbmap_file = 'rgb.nii.gz'
282276
>>> fit_dwi.cmdline
283277
'fit_dwi -source dwi.nii.gz -bval bvals -bvec bvecs -dti \
284-
-error dwi_error.nii.gz -famap dwi_famap.nii.gz -mcmap dwi_mcmap.nii.gz \
285-
-mcout dwi_mcout.txt -mdmap dwi_mdmap.nii.gz -nodiff dwi_no_diff.nii.gz \
286-
-res dwi_resmap.nii.gz -rgbmap rgb.nii.gz -syn dwi_syn.nii.gz \
287-
-tenmap2 dwi_tenmap2.nii.gz -v1map dwi_v1map.nii.gz'
278+
-error dwi_error.nii.gz -famap dwi_famap.nii.gz -mcout dwi_mcout.txt \
279+
-mdmap dwi_mdmap.nii.gz -nodiff dwi_no_diff.nii.gz -res dwi_resmap.nii.gz \
280+
-rgbmap rgb.nii.gz -syn dwi_syn.nii.gz -tenmap2 dwi_tenmap2.nii.gz \
281+
-v1map dwi_v1map.nii.gz'
288282
289283
"""
290284
_cmd = get_custom_path('fit_dwi', env_dir='NIFTYFITDIR')

0 commit comments

Comments
 (0)