-
Notifications
You must be signed in to change notification settings - Fork 15
/
bids.py
415 lines (347 loc) · 14.6 KB
/
bids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Helpers for handling BIDS-like neuroimaging structures
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"""
from pathlib import Path
import json
import re
import warnings
from bids import BIDSLayout
from packaging.version import Version
class BIDSError(ValueError):
def __init__(self, message, bids_root):
indent = 10
header = '{sep} BIDS root folder: "{bids_root}" {sep}'.format(
bids_root=bids_root, sep="".join(["-"] * indent)
)
self.msg = "\n{header}\n{indent}{message}\n{footer}".format(
header=header,
indent="".join([" "] * (indent + 1)),
message=message,
footer="".join(["-"] * len(header)),
)
super(BIDSError, self).__init__(self.msg)
self.bids_root = bids_root
class BIDSWarning(RuntimeWarning):
pass
def collect_participants(
bids_dir, participant_label=None, strict=False, bids_validate=False
):
"""
List the participants under the BIDS root and checks that participants
designated with the participant_label argument exist in that folder.
Returns the list of participants to be finally processed.
Requesting all subjects in a BIDS directory root:
#>>> collect_participants(str(datadir / 'ds114'), bids_validate=False)
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
Requesting two subjects, given their IDs:
#>>> collect_participants(str(datadir / 'ds114'), participant_label=['02', '04'],
#... bids_validate=False)
['02', '04']
Requesting two subjects, given their IDs (works with 'sub-' prefixes):
#>>> collect_participants(str(datadir / 'ds114'), participant_label=['sub-02', 'sub-04'],
#... bids_validate=False)
['02', '04']
Requesting two subjects, but one does not exist:
#>>> collect_participants(str(datadir / 'ds114'), participant_label=['02', '14'],
#... bids_validate=False)
['02']
#>>> collect_participants(
#... str(datadir / 'ds114'), participant_label=['02', '14'],
#... strict=True, bids_validate=False) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
BIDSError:
...
"""
if isinstance(bids_dir, BIDSLayout):
layout = bids_dir
else:
layout = BIDSLayout(str(bids_dir), validate=bids_validate)
all_participants = set(layout.get_subjects())
# Error: bids_dir does not contain subjects
if not all_participants:
raise BIDSError(
"Could not find participants. Please make sure the BIDS data "
"structure is present and correct. Datasets can be validated online "
"using the BIDS Validator (http://bids-standard.github.io/bids-validator/).\n"
"If you are using Docker for Mac or Docker for Windows, you "
'may need to adjust your "File sharing" preferences.',
bids_dir,
)
# No --participant-label was set, return all
if not participant_label:
return sorted(all_participants)
if isinstance(participant_label, str):
participant_label = [participant_label]
# Drop sub- prefixes
participant_label = [
sub[4:] if sub.startswith("sub-") else sub for sub in participant_label
]
# Remove duplicates
participant_label = sorted(set(participant_label))
# Remove labels not found
found_label = sorted(set(participant_label) & all_participants)
if not found_label:
raise BIDSError(
"Could not find participants [{}]".format(", ".join(participant_label)),
bids_dir,
)
# Warn if some IDs were not found
notfound_label = sorted(set(participant_label) - all_participants)
if notfound_label:
exc = BIDSError(
"Some participants were not found: {}".format(", ".join(notfound_label)),
bids_dir,
)
if strict:
raise exc
warnings.warn(exc.msg, BIDSWarning)
return found_label
def collect_data(
bids_dir,
participant_label,
task=None,
echo=None,
bids_validate=False,
bids_filters=None,
):
"""
Uses pybids to retrieve the input data for a given participant
#>>> bids_root, _ = collect_data(str(datadir / 'ds054'), '100185',
#... bids_validate=False)
#>>> bids_root['fmap'] # doctest: +ELLIPSIS
['.../ds054/sub-100185/fmap/sub-100185_magnitude1.nii.gz', \
'.../ds054/sub-100185/fmap/sub-100185_magnitude2.nii.gz', \
'.../ds054/sub-100185/fmap/sub-100185_phasediff.nii.gz']
#>>> bids_root['bold'] # doctest: +ELLIPSIS
['.../ds054/sub-100185/func/sub-100185_task-machinegame_run-01_bold.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-02_bold.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-03_bold.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-04_bold.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-05_bold.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-06_bold.nii.gz']
#>>> bids_root['sbref'] # doctest: +ELLIPSIS
['.../ds054/sub-100185/func/sub-100185_task-machinegame_run-01_sbref.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-02_sbref.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-03_sbref.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-04_sbref.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-05_sbref.nii.gz', \
'.../ds054/sub-100185/func/sub-100185_task-machinegame_run-06_sbref.nii.gz']
#>>> bids_root['t1w'] # doctest: +ELLIPSIS
['.../ds054/sub-100185/anat/sub-100185_T1w.nii.gz']
#>>> bids_root['t2w'] # doctest: +ELLIPSIS
[]
#>>> bids_root, _ = collect_data(str(datadir / 'ds051'), '01',
#... bids_validate=False, bids_filters={'t1w':{'run': 1}})
#>>> bids_root['t1w'] # doctest: +ELLIPSIS
['.../ds051/sub-01/anat/sub-01_run-01_T1w.nii.gz']
"""
if isinstance(bids_dir, BIDSLayout):
layout = bids_dir
else:
layout = BIDSLayout(str(bids_dir), validate=bids_validate)
queries = {
'fmap': {'datatype': 'fmap'},
'bold': {'datatype': 'func', 'suffix': 'bold'},
'sbref': {'datatype': 'func', 'suffix': 'sbref'},
'flair': {'datatype': 'anat', 'suffix': 'FLAIR'},
't2w': {'datatype': 'anat', 'suffix': 'T2w'},
't1w': {'datatype': 'anat', 'suffix': 'T1w'},
'roi': {'datatype': 'anat', 'suffix': 'roi'},
'asl': {'datatype': 'perf', 'suffix': 'asl'},
'cbf': {'datatype': 'perf', 'suffix': 'DELTAM'},
'm0z': {'datatype': 'perf', 'suffix': 'MoScan'},
}
bids_filters = bids_filters or {}
for acq, entities in bids_filters.items():
queries[acq].update(entities)
#if task:
#queries["asl"]["task"] = task
if echo:
queries["asl"]["echo"] = echo
subj_data = {
dtype: sorted(
layout.get(
return_type="file",
subject=participant_label,
extension=["nii", "nii.gz"],
**query,
)
)
for dtype, query in queries.items()
}
# Special case: multi-echo BOLD, grouping echos
if any(["_echo-" in bold for bold in subj_data["asl"]]):
subj_data["asl"] = group_multiecho(subj_data["asl"])
return subj_data, layout
def get_metadata_for_nifti(in_file, bids_dir=None, validate=True):
"""Fetch metadata for a given nifti file
#>>> metadata = get_metadata_for_nifti(
#... datadir / 'ds054' / 'sub-100185' / 'fmap' / 'sub-100185_phasediff.nii.gz',
#... validate=False)
#>>> metadata['Manufacturer']
'SIEMENS'
>>>
"""
return _init_layout(in_file, bids_dir, validate).get_metadata(str(in_file))
def _init_layout(in_file=None, bids_dir=None, validate=True):
if isinstance(bids_dir, BIDSLayout):
return bids_dir
if bids_dir is None:
in_file = Path(in_file)
for parent in in_file.parents:
if parent.name.startswith("sub-"):
bids_dir = parent.parent.resolve()
break
if bids_dir is None:
raise RuntimeError("Could not infer BIDS root")
layout = BIDSLayout(str(bids_dir), validate=validate)
return layout
def group_multiecho(bold_sess):
"""
Multiplexes multi-echo EPIs into arrays. Dual-echo is a special
case of multi-echo, which is treated as single-echo data.
>>> bold_sess = ["sub-01_task-rest_echo-1_run-01_bold.nii.gz",
... "sub-01_task-rest_echo-2_run-01_bold.nii.gz",
... "sub-01_task-rest_echo-1_run-02_bold.nii.gz",
... "sub-01_task-rest_echo-2_run-02_bold.nii.gz",
... "sub-01_task-rest_echo-3_run-02_bold.nii.gz",
... "sub-01_task-rest_run-03_bold.nii.gz"]
>>> group_multiecho(bold_sess)
['sub-01_task-rest_echo-1_run-01_bold.nii.gz',
'sub-01_task-rest_echo-2_run-01_bold.nii.gz',
['sub-01_task-rest_echo-1_run-02_bold.nii.gz',
'sub-01_task-rest_echo-2_run-02_bold.nii.gz',
'sub-01_task-rest_echo-3_run-02_bold.nii.gz'],
'sub-01_task-rest_run-03_bold.nii.gz']
>>> bold_sess.insert(2, "sub-01_task-rest_echo-3_run-01_bold.nii.gz")
>>> group_multiecho(bold_sess)
[['sub-01_task-rest_echo-1_run-01_bold.nii.gz',
'sub-01_task-rest_echo-2_run-01_bold.nii.gz',
'sub-01_task-rest_echo-3_run-01_bold.nii.gz'],
['sub-01_task-rest_echo-1_run-02_bold.nii.gz',
'sub-01_task-rest_echo-2_run-02_bold.nii.gz',
'sub-01_task-rest_echo-3_run-02_bold.nii.gz'],
'sub-01_task-rest_run-03_bold.nii.gz']
>>> bold_sess += ["sub-01_task-beh_echo-1_run-01_bold.nii.gz",
... "sub-01_task-beh_echo-2_run-01_bold.nii.gz",
... "sub-01_task-beh_echo-1_run-02_bold.nii.gz",
... "sub-01_task-beh_echo-2_run-02_bold.nii.gz",
... "sub-01_task-beh_echo-3_run-02_bold.nii.gz",
... "sub-01_task-beh_run-03_bold.nii.gz"]
>>> group_multiecho(bold_sess)
[['sub-01_task-rest_echo-1_run-01_bold.nii.gz',
'sub-01_task-rest_echo-2_run-01_bold.nii.gz',
'sub-01_task-rest_echo-3_run-01_bold.nii.gz'],
['sub-01_task-rest_echo-1_run-02_bold.nii.gz',
'sub-01_task-rest_echo-2_run-02_bold.nii.gz',
'sub-01_task-rest_echo-3_run-02_bold.nii.gz'],
'sub-01_task-rest_run-03_bold.nii.gz',
'sub-01_task-beh_echo-1_run-01_bold.nii.gz',
'sub-01_task-beh_echo-2_run-01_bold.nii.gz',
['sub-01_task-beh_echo-1_run-02_bold.nii.gz',
'sub-01_task-beh_echo-2_run-02_bold.nii.gz',
'sub-01_task-beh_echo-3_run-02_bold.nii.gz'],
'sub-01_task-beh_run-03_bold.nii.gz']
Some tests from https://neurostars.org/t/fmriprep-from\
-singularity-unboundlocalerror/3299/7
>>> bold_sess = ['sub-01_task-AudLoc_echo-1_bold.nii',
... 'sub-01_task-AudLoc_echo-2_bold.nii',
... 'sub-01_task-FJT_echo-1_bold.nii',
... 'sub-01_task-FJT_echo-2_bold.nii',
... 'sub-01_task-LDT_echo-1_bold.nii',
... 'sub-01_task-LDT_echo-2_bold.nii',
... 'sub-01_task-MotLoc_echo-1_bold.nii',
... 'sub-01_task-MotLoc_echo-2_bold.nii']
>>> group_multiecho(bold_sess) == bold_sess
True
>>> bold_sess += ['sub-01_task-MotLoc_echo-3_bold.nii']
>>> groups = group_multiecho(bold_sess)
>>> len(groups[:-1])
6
>>> [isinstance(g, list) for g in groups]
[False, False, False, False, False, False, True]
>>> len(groups[-1])
3
"""
from itertools import groupby
def _grp_echos(x):
if "_echo-" not in x:
return x
echo = re.search("_echo-\\d*", x).group(0)
return x.replace(echo, "_echo-?")
ses_uids = []
for _, bold in groupby(bold_sess, key=_grp_echos):
bold = list(bold)
# If single- or dual-echo, flatten list; keep list otherwise.
action = getattr(ses_uids, "append" if len(bold) > 2 else "extend")
action(bold)
return ses_uids
def relative_to_root(path):
"""
Calculate the BIDS root folder given one file path's.
Example
-------
>>> str(relative_to_root(
... "/sub-03/sourcedata/sub-01/anat/sub-01_T1.nii.gz"
... ))
'sub-01/anat/sub-01_T1.nii.gz'
>>> str(relative_to_root(
... "/sub-03/anat/sourcedata/sub-01/ses-preop/anat/sub-01_ses-preop_T1.nii.gz"
... ))
'sub-01/ses-preop/anat/sub-01_ses-preop_T1.nii.gz'
>>> str(relative_to_root(
... "sub-01/anat/sub-01_T1.nii.gz"
... ))
'sub-01/anat/sub-01_T1.nii.gz'
>>> str(relative_to_root("anat/sub-01_T1.nii.gz"))
'anat/sub-01_T1.nii.gz'
"""
path = Path(path)
if path.name.startswith("sub-"):
parents = [path.name]
for p in path.parents:
parents.insert(0, p.name)
if p.name.startswith("sub-"):
return Path(*parents)
return path
raise ValueError(
f"Could not determine the BIDS root of <{path}>. "
"Only files under a subject directory are currently supported."
)
def check_pipeline_version(cvers, data_desc):
"""
Searches for existing BIDS pipeline output and compares against current pipeline version.
.. testsetup::
#>>> import json
#>>> data = {"PipelineDescription": {"Version": "1.1.1rc5"}}
#>>> desc_file = Path(tmpdir) / 'sample_dataset_description.json'
#>>> _ = desc_file.write_text(json.dumps(data))
Parameters
----------
cvers : :obj:`str`
Current pipeline version
data_desc : :obj:`str` or :class:`os.Pathlike`
Path to pipeline output's dataset_description.json
Examples
--------
#>>> check_pipeline_version('1.1.1rc5', 'sample_dataset_description.json') is None
True
#>>> check_pipeline_version('1.1.1rc5+129.gbe0e5158', 'sample_dataset_description.json')
#>>> check_pipeline_version('1.2', 'sample_dataset_description.json') # doctest: +ELLIPSIS
'Previous output generated ...'
Returns
-------
message : :obj:`str` or :obj:`None`
A warning string if there is a difference between versions, otherwise ``None``.
"""
data_desc = Path(data_desc)
if not data_desc.exists():
return
desc = json.loads(data_desc.read_text())
dvers = desc.get("PipelineDescription", {}).get("Version", "0+unknown")
if Version(cvers).public != Version(dvers).public:
return "Previous output generated by version {} found.".format(dvers)