|
22 | 22 | #
|
23 | 23 | """Helpers for handling BIDS-like neuroimaging structures."""
|
24 | 24 |
|
| 25 | +import copy |
25 | 26 | import json
|
26 | 27 | import re
|
27 | 28 | import warnings
|
28 | 29 | from pathlib import Path
|
29 | 30 |
|
30 | 31 | from bids import BIDSLayout
|
31 | 32 | from bids.layout import Query
|
| 33 | +from bids.utils import listify |
32 | 34 | from packaging.version import Version
|
33 | 35 |
|
34 | 36 | DEFAULT_BIDS_QUERIES = {
|
@@ -226,22 +228,43 @@ def collect_data(
|
226 | 228 | >>> bids_root['t1w'] # doctest: +ELLIPSIS
|
227 | 229 | ['.../ds051/sub-01/anat/sub-01_run-01_T1w.nii.gz']
|
228 | 230 |
|
| 231 | + >>> bids_root, _ = collect_data( |
| 232 | + ... str(datadir / 'ds114'), |
| 233 | + ... '01', |
| 234 | + ... bids_validate=False, |
| 235 | + ... session_id='retest', |
| 236 | + ... bids_filters={'bold': {'session': 'madeup'}}) # doctest: +ELLIPSIS |
| 237 | + Traceback (most recent call last): |
| 238 | + ... |
| 239 | + ValueError: Conflicting entities for "session" found: madeup // retest |
| 240 | +
|
229 | 241 | """
|
230 | 242 | if isinstance(bids_dir, BIDSLayout):
|
231 | 243 | layout = bids_dir
|
232 | 244 | else:
|
233 | 245 | layout = BIDSLayout(str(bids_dir), validate=bids_validate)
|
234 | 246 |
|
| 247 | + if not queries: |
| 248 | + queries = copy.deepcopy(DEFAULT_BIDS_QUERIES) |
| 249 | + |
235 | 250 | layout_get_kwargs = {
|
236 | 251 | 'return_type': 'file',
|
237 | 252 | 'subject': participant_label,
|
238 | 253 | 'extension': ['.nii', '.nii.gz'],
|
239 | 254 | 'session': session_id or Query.OPTIONAL,
|
240 | 255 | }
|
241 | 256 |
|
242 |
| - queries = queries or DEFAULT_BIDS_QUERIES |
| 257 | + reserved_entities = [('subject', participant_label), ('session', session_id)] |
| 258 | + |
243 | 259 | bids_filters = bids_filters or {}
|
244 | 260 | for acq, entities in bids_filters.items():
|
| 261 | + # BIDS filters will not be able to override subject / session entities |
| 262 | + for entity, param in reserved_entities: |
| 263 | + if entity in entities and listify(param) != listify(entities[entity]): |
| 264 | + raise ValueError( |
| 265 | + f'Conflicting entities for "{entity}" found: {entities[entity]} // {param}' |
| 266 | + ) |
| 267 | + |
245 | 268 | queries[acq].update(entities)
|
246 | 269 | for entity in list(layout_get_kwargs.keys()):
|
247 | 270 | if entity in entities:
|
|
0 commit comments