Skip to content

Commit 72bd8e2

Browse files
committed
Added mandatory option for outfieds
1 parent 6a4ce64 commit 72bd8e2

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

nipype/interfaces/bids.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
else:
3030
have_pybids = True
3131

32+
from warnings import warn
3233

3334
class BIDSDataGrabberInputSpec(DynamicTraitedSpec):
3435
base_dir = traits.Directory(exists=True,
@@ -37,6 +38,9 @@ class BIDSDataGrabberInputSpec(DynamicTraitedSpec):
3738
output_query = traits.Dict(key_trait=Str,
3839
value_trait=traits.Dict,
3940
desc='Queries for outfield outputs')
41+
raise_on_empty = traits.Bool(True, usedefault=True,
42+
desc='Generate exception if list is empty '
43+
'for a given field')
4044
return_type = traits.Enum('filename', 'namedtuple', usedefault=True)
4145

4246

@@ -58,7 +62,7 @@ class BIDSDataGrabber(BaseInterface):
5862
>>> bg.inputs.base_dir = 'ds005/'
5963
>>> results = bg.run()
6064
>>> len(results.outputs.outfield) # doctest: +ALLOW_UNICODE
61-
116
65+
135
6266
6367
Using dynamically created, user-defined input fields,
6468
filter files based on BIDS entities.
@@ -99,8 +103,8 @@ def __init__(self, infields=None, outfields=None, **kwargs):
99103
Indicates the input fields to be dynamically created
100104
101105
outfields: list of str
102-
Indicates output fields to be dynamically created
103-
106+
Indicates output fields to be dynamically created.
107+
If no matching items, returns Undefined.
104108
"""
105109
if not outfields:
106110
outfields = []
@@ -150,7 +154,18 @@ def _list_outputs(self):
150154

151155
outputs = {}
152156
for key, query in self.inputs.output_query.items():
153-
outputs[key] = layout.get(
154-
**dict(query.items() | filters.items()),
155-
return_type='file')
157+
args = query.copy()
158+
args.update(filters)
159+
filelist = layout.get(return_type='file',
160+
**args)
161+
if len(filelist) == 0:
162+
msg = 'Output key: %s returned no files' % (
163+
key)
164+
if self.inputs.raise_on_empty:
165+
raise IOError(msg)
166+
else:
167+
warn(msg)
168+
filelist = Undefined
169+
else:
170+
outputs[key] = filelist
156171
return outputs

0 commit comments

Comments
 (0)