Skip to content

Commit 0803819

Browse files
authored
Merge pull request #2228 from effigies/enh/bids_grabber
ENH: More flexible BIDSDataGrabber creation
2 parents 53d77a9 + 425a570 commit 0803819

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

nipype/interfaces/bids_utils.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
LOGGER = logging.getLogger('workflows')
3636

37+
3738
class BIDSDataGrabberInputSpec(DynamicTraitedSpec):
3839
base_dir = Directory(exists=True,
3940
desc='Path to BIDS Directory.',
@@ -91,7 +92,7 @@ class BIDSDataGrabber(BaseInterface):
9192
output_spec = DynamicTraitedSpec
9293
_always_run = True
9394

94-
def __init__(self, infields=None, outfields=None, **kwargs):
95+
def __init__(self, infields=None, **kwargs):
9596
"""
9697
Parameters
9798
----------
@@ -104,17 +105,13 @@ def __init__(self, infields=None, outfields=None, **kwargs):
104105
"""
105106
super(BIDSDataGrabber, self).__init__(**kwargs)
106107
if not have_pybids:
107-
raise ImportError("The BIDSEventsGrabber interface requires pybids."
108-
" Please make sure it is installed.")
109-
110-
# If outfields is None use anat and func as default
111-
if outfields is None:
112-
outfields = ['func', 'anat']
113-
self.inputs.output_query = {
114-
"func": {"modality": "func"},
115-
"anat": {"modality": "anat"}}
116-
else:
117-
self.inputs.output_query = {}
108+
raise ImportError(
109+
"The BIDSEventsGrabber interface requires pybids."
110+
" Please make sure it is installed.")
111+
112+
if not isdefined(self.inputs.output_query):
113+
self.inputs.output_query = {"func": {"modality": "func"},
114+
"anat": {"modality": "anat"}}
118115

119116
# If infields is None, use all BIDS entities
120117
if infields is None:
@@ -123,13 +120,12 @@ def __init__(self, infields=None, outfields=None, **kwargs):
123120
infields = [i['name'] for i in bids_config['entities']]
124121

125122
self._infields = infields
126-
self._outfields = outfields
127123

128124
# used for mandatory inputs check
129125
undefined_traits = {}
130126
for key in infields:
131127
self.inputs.add_trait(key, traits.Any)
132-
undefined_traits[key] = Undefined
128+
undefined_traits[key] = kwargs[key] if key in kwargs else Undefined
133129

134130
self.inputs.trait_set(trait_change_notify=False, **undefined_traits)
135131

@@ -139,10 +135,6 @@ def _run_interface(self, runtime):
139135
def _list_outputs(self):
140136
layout = gb.BIDSLayout(self.inputs.base_dir)
141137

142-
for key in self._outfields:
143-
if key not in self.inputs.output_query:
144-
raise ValueError("Define query for all outputs")
145-
146138
# If infield is not given nm input value, silently ignore
147139
filters = {}
148140
for key in self._infields:
@@ -154,11 +146,9 @@ def _list_outputs(self):
154146
for key, query in self.inputs.output_query.items():
155147
args = query.copy()
156148
args.update(filters)
157-
filelist = layout.get(return_type=self.inputs.return_type,
158-
**args)
149+
filelist = layout.get(return_type=self.inputs.return_type, **args)
159150
if len(filelist) == 0:
160-
msg = 'Output key: %s returned no files' % (
161-
key)
151+
msg = 'Output key: %s returned no files' % key
162152
if self.inputs.raise_on_empty:
163153
raise IOError(msg)
164154
else:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from __future__ import unicode_literals
3+
from ..bids_utils import BIDSDataGrabber
4+
5+
6+
def test_BIDSDataGrabber_inputs():
7+
input_map = dict(base_dir=dict(mandatory=True,
8+
),
9+
output_query=dict(),
10+
raise_on_empty=dict(usedefault=True,
11+
),
12+
return_type=dict(usedefault=True,
13+
),
14+
)
15+
inputs = BIDSDataGrabber.input_spec()
16+
17+
for key, metadata in list(input_map.items()):
18+
for metakey, value in list(metadata.items()):
19+
assert getattr(inputs.traits()[key], metakey) == value
20+
21+
22+
def test_BIDSDataGrabber_outputs():
23+
output_map = dict()
24+
outputs = BIDSDataGrabber.output_spec()
25+
26+
for key, metadata in list(output_map.items()):
27+
for metakey, value in list(metadata.items()):
28+
assert getattr(outputs.traits()[key], metakey) == value

0 commit comments

Comments
 (0)