29
29
else :
30
30
have_pybids = True
31
31
32
+ from warnings import warn
32
33
33
34
class BIDSDataGrabberInputSpec (DynamicTraitedSpec ):
34
35
base_dir = traits .Directory (exists = True ,
@@ -37,6 +38,9 @@ class BIDSDataGrabberInputSpec(DynamicTraitedSpec):
37
38
output_query = traits .Dict (key_trait = Str ,
38
39
value_trait = traits .Dict ,
39
40
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' )
40
44
return_type = traits .Enum ('filename' , 'namedtuple' , usedefault = True )
41
45
42
46
@@ -58,7 +62,7 @@ class BIDSDataGrabber(BaseInterface):
58
62
>>> bg.inputs.base_dir = 'ds005/'
59
63
>>> results = bg.run()
60
64
>>> len(results.outputs.outfield) # doctest: +ALLOW_UNICODE
61
- 116
65
+ 135
62
66
63
67
Using dynamically created, user-defined input fields,
64
68
filter files based on BIDS entities.
@@ -99,8 +103,8 @@ def __init__(self, infields=None, outfields=None, **kwargs):
99
103
Indicates the input fields to be dynamically created
100
104
101
105
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.
104
108
"""
105
109
if not outfields :
106
110
outfields = []
@@ -150,7 +154,18 @@ def _list_outputs(self):
150
154
151
155
outputs = {}
152
156
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
156
171
return outputs
0 commit comments