-
Notifications
You must be signed in to change notification settings - Fork 15
/
bids.py
275 lines (237 loc) · 9.46 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
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Utilities to handle BIDS inputs."""
import json
import os
import sys
from pathlib import Path
import yaml
from bids.layout import BIDSLayout
from aslprep import config
def collect_data(
bids_dir,
participant_label,
bids_validate=False,
bids_filters=None,
):
"""Use pybids to retrieve the input data for a given participant."""
if isinstance(bids_dir, BIDSLayout):
layout = bids_dir
else:
layout = BIDSLayout(str(bids_dir), validate=bids_validate)
queries = {
"fmap": {"datatype": "fmap"},
"flair": {"datatype": "anat", "suffix": "FLAIR"},
"t2w": {"datatype": "anat", "suffix": "T2w"},
"t1w": {"datatype": "anat", "suffix": "T1w"},
"roi": {"datatype": "anat", "suffix": "roi"},
"sbref": {"datatype": "perf", "suffix": "sbref"},
"asl": {"datatype": "perf", "suffix": "asl"},
}
bids_filters = bids_filters or {}
for acq, entities in bids_filters.items():
queries[acq].update(entities)
subj_data = {
dtype: sorted(
layout.get(
return_type="file",
subject=participant_label,
extension=["nii", "nii.gz"],
**query,
)
)
for dtype, query in queries.items()
}
return subj_data, layout
def collect_run_data(layout, asl_file):
"""Use pybids to retrieve the input data for a given participant."""
queries = {
"aslcontext": {"suffix": "aslcontext", "extension": ".tsv"},
"sbref": {"suffix": "sbref"},
}
bids_file = layout.get_file(asl_file)
run_data = {
dtype: layout.get_nearest(
bids_file.path,
return_type="file",
strict=False, # aslcontext files aren't grabbed when strict=True, for some reason
**query,
)
for dtype, query in queries.items()
}
if "sbref" in config.workflow.ignore:
config.loggers.workflow.info("Single-band reference files ignored.")
run_data["sbref"] = None
# The aslcontext file is required
if not run_data["aslcontext"]:
raise FileNotFoundError(f"aslcontext file for {asl_file} not found.")
# Now let's look for an m0scan
m0scan_candidates = [
f
for f in bids_file.get_associations(kind="InformedBy")
if f.entities["suffix"] == "m0scan"
]
if m0scan_candidates:
if len(m0scan_candidates) > 1:
config.loggers.workflow.warning(
f"More than one M0 file found for {asl_file}. "
f"Using the first one ({m0scan_candidates[0]})"
)
run_data["m0scan"] = m0scan_candidates[0]
else:
run_data["m0scan"] = None
m0scan_metadata = None
asl_metadata = layout.get_metadata(asl_file)
if (asl_metadata["M0Type"] == "Separate") and not run_data["m0scan"]:
raise FileNotFoundError(f"M0 file for {asl_file} not found.")
elif asl_metadata["M0Type"] == "Separate":
m0scan_metadata = layout.get_file(run_data["m0scan"]).get_metadata()
if not m0scan_metadata:
raise Exception(f"No metadata for m0scan: {run_data['m0scan']}")
elif run_data["m0scan"]:
raise ValueError(
f"M0Type is {run_data['asl_metadata']['M0Type']}, "
f"but an M0 scan was found at {run_data['m0scan']}"
)
config.loggers.workflow.info(
(
f"Collected run data for {asl_file}:\n"
f"{yaml.dump(run_data, default_flow_style=False, indent=4)}"
),
)
# Add metadata to dictionary now (we don't want to print these with the logger).
run_data["asl_metadata"] = asl_metadata
run_data["m0scan_metadata"] = m0scan_metadata
return run_data
def write_derivative_description(bids_dir, deriv_dir):
"""Write derivative dataset_description file."""
from aslprep.__about__ import DOWNLOAD_URL, __url__, __version__
bids_dir = Path(bids_dir)
deriv_dir = Path(deriv_dir)
desc = {
"Name": "ASLPrep - ASL PREProcessing workflow",
"BIDSVersion": "1.1.1",
"PipelineDescription": {
"Name": "ASLPrep",
"Version": __version__,
"CodeURL": DOWNLOAD_URL,
},
"CodeURL": __url__,
"HowToAcknowledge": "Please cite our paper coming :), "
"and include the generated citation boilerplate within the Methods "
"section of the text.",
}
# Keys that can only be set by environment
if "ASLPREP_DOCKER_TAG" in os.environ:
desc["DockerHubContainerTag"] = os.environ["ASLPREP_DOCKER_TAG"]
if "ASLPREP_SINGULARITY_URL" in os.environ:
singularity_url = os.environ["ASLPREP_SINGULARITY_URL"]
desc["SingularityContainerURL"] = singularity_url
singularity_md5 = _get_shub_version(singularity_url)
if singularity_md5 and singularity_md5 is not NotImplemented:
desc["SingularityContainerMD5"] = _get_shub_version(singularity_url)
# Keys deriving from source dataset
orig_desc = {}
fname = bids_dir / "dataset_description.json"
if fname.exists():
with fname.open() as fobj:
orig_desc = json.load(fobj)
if "DatasetDOI" in orig_desc:
desc["SourceDatasetsURLs"] = [f"https://doi.org/{orig_desc['DatasetDOI']}"]
if "License" in orig_desc:
desc["License"] = orig_desc["License"]
with (deriv_dir / "dataset_description.json").open("w") as fobj:
json.dump(desc, fobj, indent=4)
def validate_input_dir(exec_env, bids_dir, participant_label):
"""Validate input directory.
This function checks necessary validator requirements,
but skips ones that should not influence the pipeline.
"""
import subprocess
import tempfile
validator_config_dict = {
"ignore": [
"EVENTS_COLUMN_ONSET",
"EVENTS_COLUMN_DURATION",
"TSV_EQUAL_ROWS",
"TSV_EMPTY_CELL",
"TSV_IMPROPER_NA",
"VOLUME_COUNT_MISMATCH",
"BVAL_MULTIPLE_ROWS",
"BVEC_NUMBER_ROWS",
"DWI_MISSING_BVAL",
"INCONSISTENT_SUBJECTS",
"INCONSISTENT_PARAMETERS",
"BVEC_ROW_LENGTH",
"B_FILE",
"PARTICIPANT_ID_COLUMN",
"PARTICIPANT_ID_MISMATCH",
"TASK_NAME_MUST_DEFINE",
"PHENOTYPE_SUBJECTS_MISSING",
"STIMULUS_FILE_MISSING",
"DWI_MISSING_BVEC",
"EVENTS_TSV_MISSING",
"TSV_IMPROPER_NA",
"ACQTIME_FMT",
"Participants age 89 or higher",
"DATASET_DESCRIPTION_JSON_MISSING",
"FILENAME_COLUMN",
"WRONG_NEW_LINE",
"MISSING_TSV_COLUMN_CHANNELS",
"MISSING_TSV_COLUMN_IEEG_CHANNELS",
"MISSING_TSV_COLUMN_IEEG_ELECTRODES",
"UNUSED_STIMULUS",
"CHANNELS_COLUMN_SFREQ",
"CHANNELS_COLUMN_LOWCUT",
"CHANNELS_COLUMN_HIGHCUT",
"CHANNELS_COLUMN_NOTCH",
"CUSTOM_COLUMN_WITHOUT_DESCRIPTION",
"ACQTIME_FMT",
"SUSPICIOUSLY_LONG_EVENT_DESIGN",
"SUSPICIOUSLY_SHORT_EVENT_DESIGN",
"MALFORMED_BVEC",
"MALFORMED_BVAL",
"MISSING_TSV_COLUMN_EEG_ELECTRODES",
"MISSING_SESSION",
],
"error": ["NO_T1W"],
"ignoredFiles": ["/dataset_description.json", "/participants.tsv"],
}
# Limit validation only to data from requested participants
if participant_label:
all_subs = {s.name[4:] for s in bids_dir.glob("sub-*")}
selected_subs = {s[4:] if s.startswith("sub-") else s for s in participant_label}
if bad_labels := selected_subs.difference(all_subs):
error_msg = (
"Data for requested participant(s) label(s) not found. "
f"Could not find data for participant(s): {','.join(bad_labels)}. "
"Please verify the requested participant labels."
)
if exec_env == "docker":
error_msg += (
" This error can be caused by the input data not being "
"accessible inside the docker container. "
"Please make sure all volumes are mounted properly "
"(see https://docs.docker.com/engine/reference/commandline/"
"run/#mount-volume--v---read-only)"
)
elif exec_env == "singularity":
error_msg += (
" This error can be caused by the input data not being "
"accessible inside the singularity container. "
"Please make sure all paths are mapped properly "
"(see https://www.sylabs.io/guides/3.0/user-guide/bind_paths_and_mounts.html)"
)
raise RuntimeError(error_msg)
if ignored_subs := all_subs.difference(selected_subs):
for sub in ignored_subs:
validator_config_dict["ignoredFiles"].append(f"/sub-{sub}/**")
with tempfile.NamedTemporaryFile("w+") as temp:
temp.write(json.dumps(validator_config_dict))
temp.flush()
try:
subprocess.check_call(["bids-validator", bids_dir, "-c", temp.name])
except FileNotFoundError:
print("bids-validator does not appear to be installed", file=sys.stderr)
def _get_shub_version(singularity_url): # noqa: U100
return NotImplemented