Skip to content

Commit 81dfefe

Browse files
committed
Enhance flexibiliy of SelectFiles output list coercion
1 parent 95c74e6 commit 81dfefe

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

nipype/interfaces/io.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,12 @@ class SelectFilesInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
562562
desc="When matching mutliple files, return them in sorted order.")
563563
raise_on_empty = traits.Bool(True, usedefault=True,
564564
desc="Raise an exception if a template pattern matches no files.")
565-
force_lists = traits.Bool(False, usedefault=True,
566-
desc="Return all values as lists even when matching a single file.")
565+
force_lists = traits.Either(traits.Bool(), traits.List(traits.Str()),
566+
default=False, usedefault=True,
567+
desc=("Whether to return outputs as a list even when only one file "
568+
"matches the template. Either a boolean that applies to all "
569+
"output fields or a list of output field names to coerce to "
570+
" a list"))
567571

568572

569573
class SelectFiles(IOBase):
@@ -646,6 +650,10 @@ def _list_outputs(self):
646650
info = dict([(k, v) for k, v in self.inputs.__dict__.items()
647651
if k in self._infields])
648652

653+
force_lists = self.inputs.force_lists
654+
if isinstance(force_lists, bool):
655+
force_lists = self._outfields if force_lists else []
656+
649657
for field, template in self._templates.iteritems():
650658

651659
# Build the full template path
@@ -673,7 +681,7 @@ def _list_outputs(self):
673681
filelist.sort()
674682

675683
# Handle whether this must be a list or not
676-
if not self.inputs.force_lists:
684+
if field not in force_lists:
677685
filelist = list_to_filename(filelist)
678686

679687
outputs[field] = filelist

nipype/interfaces/tests/test_io.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ def test_selectfiles():
4343
"interfaces/spm/preprocess.py")
4444
yield assert_equal, res.outputs.preprocess, [wanted]
4545

46+
dg.inputs.package = "fsl"
47+
dg.inputs.force_lists = ["model"]
48+
res = dg.run()
49+
preproc = op.join(op.dirname(nipype.__file__),
50+
"interfaces/fsl/preprocess.py")
51+
model = [op.join(op.dirname(nipype.__file__),
52+
"interfaces/fsl/model.py")]
53+
yield assert_equal, res.outputs.preprocess, preproc
54+
yield assert_equal, res.outputs.model, model
55+
4656
templates = {"converter": "interfaces/dcm{to!s}nii.py"}
4757
dg = nio.SelectFiles(templates, base_directory=base_dir)
4858
dg.inputs.to = 2

0 commit comments

Comments
 (0)