forked from cpp-lln-lab/bidsMReye
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbids_utils.py
More file actions
360 lines (280 loc) · 10.8 KB
/
bids_utils.py
File metadata and controls
360 lines (280 loc) · 10.8 KB
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
from __future__ import annotations
import json
import re
from pathlib import Path
from typing import Any
from bids import BIDSLayout # type: ignore
from bids.layout import BIDSFile
from bidsmreye._version import __version__
from bidsmreye.configuration import (
Config,
config_to_dict,
get_bids_filter_config,
get_bidsname_config,
get_pybids_config,
)
from bidsmreye.logger import bidsmreye_log
from bidsmreye.methods import methods
from bidsmreye.utils import copy_license, create_dir_if_absent, return_regex
log = bidsmreye_log("bidsmreye")
def check_layout(cfg: Config, layout: BIDSLayout, for_file: str = "bold") -> None:
"""Check layout.
:param cfg: Configuration object
:type cfg: Config
:param layout: BIDSLayout of the dataset.
:type layout: BIDSLayout
:raises RuntimeError: _description_
:raises RuntimeError: _description_
"""
desc = layout.get_dataset_description()
if ("DatasetType" not in desc and "PipelineDescription" not in desc) or (
"DatasetType" in desc and desc["DatasetType"] != "derivative"
):
raise RuntimeError(
"DatasetType must be 'derivative' in dataset_description.json\n."
"Check the FAQ for more information: "
"https://bidsmreye.readthedocs.io/en/latest/FAQ.html"
)
this_filter = get_bids_filter_config()[for_file]
if "GeneratedBy" in desc:
generated_by = desc["GeneratedBy"]
elif "PipelineDescription" in desc:
generated_by = desc["PipelineDescription"]
if isinstance(generated_by, list):
generated_by = generated_by[0]
if generated_by["Name"].lower() == "bidsmreye":
this_filter = get_bids_filter_config()["mask"]
this_filter["task"] = return_regex(cfg.task)
this_filter["space"] = cfg.space
if cfg.run:
this_filter["run"] = cfg.run
log.debug(f"Looking for files with filter\n{this_filter}")
bf = layout.get(
return_type="filename",
regex_search=True,
**this_filter,
)
if bf == []:
subjects = layout.get(return_type="id", target="subject", suffix="bold")
tasks = layout.get(return_type="id", target="task", suffix="bold")
raise RuntimeError(
f"Input dataset {layout.root} does not have "
f"any data to process for filter\n{this_filter}.\n"
f"This dataset contains subjects: {subjects}.\n"
f"This dataset contains tasks: {tasks}.\n"
"Is your dataset a BIDS derivative dataset?\n"
"Check the FAQ for more information: "
"https://bidsmreye.readthedocs.io/en/latest/FAQ.html"
)
def create_bidsname(
layout: BIDSLayout,
filename: dict[str, str] | str | Path,
filetype: str,
extra_entities: dict[str, str] | None = None,
) -> Path:
"""Return a BIDS valid filename for layout and a filename or a dict of BIDS entities.
:param layout: BIDSLayout of the dataset.
:type layout: BIDSLayout
:param filename: Dictionary of BIDS entities or a Path to a file.
:type filename: Union[dict, str, Path]
:param filetype: One of the file type available in the BIDS name config.
:type filetype: str
:raises TypeError:
:return:
:rtype: Path
"""
if isinstance(filename, dict):
entities = filename
elif isinstance(filename, (Path, str)):
entities = layout.parse_file_entities(filename)
if "run" in entities:
tokens = str(filename).split("_")
for x in tokens:
if x.startswith("run-"):
padding = len(x.split("-")[1])
entities["run"] = f"{entities['run']:0{padding}d}"
if extra_entities is not None:
for key in extra_entities:
entities[key] = extra_entities[key]
bids_name_config = get_bidsname_config()
output_file = layout.build_path(entities, bids_name_config[filetype], validate=False)
output_file = Path(layout.root) / output_file
return output_file.absolute()
def return_desc_entity(model_filename: Path):
model_name = sanitize_filename(model_filename).replace("Dataset", "")
if model_name in ["1GuidedFixations", "5FreeViewing"]:
return model_name[1:]
elif model_name == "3Openclosed":
return "OpenClosed"
elif model_name in ["4Pursuit", "2Pursuit", "3Pursuit"]:
return model_name[1:] + model_name[0]
elif model_name in ["1to5", "1to6"]:
return model_name
else:
return sanitize_filename(model_filename)
def sanitize_filename(filename: Path):
"""Turn filename stem into its alphanumeric CamelCase equivalent.
To use as a BIDS entity label.
"""
# Remove non-alphanumeric characters and split into words
words = re.sub(r"[^a-zA-Z0-9]", " ", filename.stem).split()
# Capitalize the first letter of each word and join them
camelcase_name = "".join(word.capitalize() for word in words)
return camelcase_name
def create_sidecar(
layout: BIDSLayout,
filename: str,
SamplingFrequency: float | None = None,
source: str | None = None,
) -> None:
"""Create sidecar for the eye motion timeseries."""
if SamplingFrequency is None:
SamplingFrequency = 0
content = {
"SamplingFrequency": SamplingFrequency,
}
if source is not None:
content["Sources"] = [source] # type: ignore
sidecar_name = create_bidsname(layout, filename, "no_label_json")
json.dump(content, open(sidecar_name, "w"), indent=4)
log.debug(f"Sidecar saved to {sidecar_name}")
def save_sampling_frequency_to_json(
layout_out: BIDSLayout, img: BIDSFile, source: str
) -> None:
metadata = img.get_metadata()
repetition_time = metadata["RepetitionTime"]
# TODO handle rare edge case where preprocessed data
# does not contain RepetitionTime metadata
if repetition_time <= 1:
log.warning(f"Found a repetition time of {repetition_time} seconds.")
create_sidecar(
layout_out, img.path, SamplingFrequency=1 / float(repetition_time), source=source
)
def get_dataset_layout(
dataset_path: str | Path,
config: str | list[str] | dict[str, str] | None = None,
use_database: bool = False,
reset_database: bool = False,
) -> BIDSLayout:
"""Return a BIDSLayout object for the dataset at the given path.
:param dataset_path: Path to the dataset.
:type dataset_path: Union[str, Path]
:param config: Pybids config to use. Defaults to None.
:type config: Optional[dict], optional
:param use_database: Defaults to False
:type use_database: bool, optional
:return: _description_
:rtype: BIDSLayout
"""
if isinstance(dataset_path, str):
dataset_path = Path(dataset_path)
create_dir_if_absent(dataset_path)
dataset_path = dataset_path.absolute()
pybids_config = config
if config is None:
pybids_config = get_pybids_config()
log.info(f"Indexing {dataset_path}")
if not use_database:
return BIDSLayout(
dataset_path, validate=False, derivatives=False, config=pybids_config
)
database_path = dataset_path / "pybids_db"
return BIDSLayout(
dataset_path,
validate=False,
derivatives=False,
config=pybids_config,
database_path=database_path,
reset_database=reset_database,
)
def init_dataset(cfg: Config, qc_only: bool = False) -> BIDSLayout:
layout_out = init_derivatives_layout(cfg)
citation_file = methods(cfg.output_dir, qc_only=qc_only)
log.info(f"Method section generated: {citation_file}")
license_file = copy_license(cfg.output_dir)
log.info(f"License file added: {license_file}")
return layout_out
def init_derivatives_layout(cfg: Config) -> BIDSLayout:
"""Initialize a derivatives dataset and returns its layout.
:param output_dir:
:type output_dir: Path
:return:
:rtype: BIDSLayout
"""
create_dir_if_absent(cfg.output_dir)
layout_out = get_dataset_layout(cfg.output_dir)
layout_out = set_dataset_description(layout_out)
layout_out.dataset_description["DatasetType"] = "derivative"
layout_out.dataset_description["GeneratedBy"][0]["Name"] = "bidsmreye"
layout_out.dataset_description["GeneratedBy"][0]["config"] = config_to_dict(cfg)
write_dataset_description(layout_out)
return layout_out
def list_subjects(cfg: Config, layout: BIDSLayout) -> list[str]:
"""List subject in a BIDS dataset for a given Config.
:param cfg: Configuration object
:type cfg: Config
:param layout: BIDSLayout of the dataset.
:type layout: BIDSLayout
:raises RuntimeError: _description_
:return: _description_
:rtype: list
"""
subjects = layout.get(return_type="id", target="subject", subject=cfg.subjects)
if subjects == [] or subjects is None:
raise RuntimeError(f"No subject found in layout:\n\t{layout.root}")
if cfg.debug:
subjects = [subjects[0]]
log.debug("Running first subject only.")
log.info(f"Processing subjects: {subjects}")
return subjects
def set_dataset_description(layout: BIDSLayout, is_derivative: bool = True) -> BIDSLayout:
"""Add dataset description to a layout.
:param layout: _description_
:type layout: BIDSLayout
:param is_derivative: Defaults to True
:type is_derivative: bool, optional
:return: Updated BIDSLayout of the dataset
:rtype: BIDSLayout
"""
data: dict[str, Any] = {
"Name": "dataset name",
"BIDSVersion": "1.7.0",
"DatasetType": "raw",
"License": "CCO",
"Authors": ["", ""],
"Acknowledgements": "Special thanks to ",
"HowToAcknowledge": """Please cite this paper: Frey, M., Nau, M. & Doeller, C.F.
Magnetic resonance-based eye tracking using deep neural networks.
Nat Neurosci 24, 1772-1779 (2021).
https://doi.org/10.1038/s41593-021-00947-w""",
"Funding": ["", ""],
"ReferencesAndLinks": ["https://doi.org/10.1038/s41593-021-00947-w"],
"DatasetDOI": "doi:",
}
if is_derivative:
data["GeneratedBy"] = [
{
"Name": "bidsMReye",
"Version": __version__,
"Container": {"Type": "", "Tag": ""},
"Description": "",
"CodeURL": "https://github.com/cpp-lln-lab/bidsMReye.git",
},
]
data["SourceDatasets"] = [
{
"DOI": "doi:",
"URL": "",
"Version": "",
}
]
layout.dataset_description = data
return layout
def write_dataset_description(layout: BIDSLayout) -> None:
"""Add a dataset_description.json to a BIDS dataset.
:param layout: BIDSLayout of the dataset to update.
:type layout: BIDSLayout
"""
output_file = Path(layout.root) / "dataset_description.json"
with open(output_file, "w") as ff:
json.dump(layout.dataset_description, ff, indent=4)