forked from childmindresearch/bids2table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentities.py
More file actions
345 lines (288 loc) · 9.5 KB
/
entities.py
File metadata and controls
345 lines (288 loc) · 9.5 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
"""
A structured representation for BIDS entities.
"""
import re
import warnings
from dataclasses import asdict, dataclass, field, fields, make_dataclass
from functools import lru_cache
from pathlib import Path
from types import MappingProxyType
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
import bidsschematools.schema
import pandas as pd
from elbow.typing import StrOrPath
from typing_extensions import Self, get_args, get_origin
_bids_schema_path: Optional[str] = None
def set_bids_schema_path(path: Optional[str]):
"""
Set the path to the BIDS schema.
"""
global _bids_schema_path
_bids_schema_path = path
def _get_bids_schema() -> Any:
"""
Get the BIDS schema.
"""
global _bids_schema_path
return bidsschematools.schema.load_schema(schema_path=_bids_schema_path)
BIDS_DATATYPES = tuple(o.value for o in _get_bids_schema().objects.datatypes.values())
def bids_field(
name: str,
display_name: str,
required: bool = False,
allowed_values: Optional[Iterable] = None,
default: Optional[Any] = None,
default_factory: Optional[Callable] = None,
):
"""
BIDS entity dataclass field.
"""
if allowed_values is not None:
allowed_values = list(allowed_values)
metadata = {
"name": name,
"display_name": display_name,
"allowed_values": allowed_values,
}
if required:
fld = field(metadata=metadata)
elif default_factory is not None:
fld = field(default_factory=default_factory, metadata=metadata)
else:
fld = field(default=default, metadata=metadata)
return fld
@dataclass
class _BIDSEntitiesBase:
"""
A dataclass representing known BIDS entities.
Note:
Only minimal validation of the entity values is performed. Use `bids_validator`
for thorough validation of BIDS datasets.
Reference:
https://bids-specification.readthedocs.io/en/stable/appendices/entities.html
"""
sub: str = bids_field(name="subject", display_name="Subject", required=True)
ses: Optional[str] = bids_field(name="session", display_name="Session")
datatype: Optional[str] = bids_field(
name="datatype", display_name="Data type", allowed_values=BIDS_DATATYPES
)
suffix: Optional[str] = bids_field(name="suffix", display_name="Suffix")
ext: Optional[str] = bids_field(name="extension", display_name="Extension")
extra_entities: Optional[Dict[str, Union[str, int]]] = bids_field(
name="extra_entities",
display_name="Extra entities",
default_factory=dict,
)
@staticmethod
def special() -> List[str]:
"""
Get list of field keys which are not standard entities.
"""
return ["datatype", "suffix", "ext", "extra_entities"]
@classmethod
def from_dict(cls, entities: Dict[str, Any], valid_only: bool = False):
"""
Initialize from a dict of entities.
"""
filtered = {}
extra_entities: Dict[str, Union[str, int]] = {}
fields_map = {f.name: f for f in fields(cls) if f.name != "extra_entities"}
def add_to_extra(k: Any, v: Any):
if not isinstance(key, str):
raise TypeError(
f"Extra entity {k} has type {type(k)}; only str supported"
)
if isinstance(v, (str, int)):
extra_entities[k] = v
else:
warnings.warn(
f"Value {v} for extra entity {k} has type {type(v)}; "
f"only str, int supported"
)
for key, val in entities.items():
if pd.isna(val):
continue
if key in fields_map:
fld = fields_map[key]
typ = _get_type(fld.type)
try:
val = typ(val)
except ValueError as exc:
raise ValueError(
f"Unable to coerce {repr(val)} to type {typ} for "
f"entity {fld.name}"
) from exc
allowed_values = fld.metadata.get("allowed_values")
if allowed_values and val not in allowed_values:
raise ValueError(
f"Value {val} for entity {fld.name} isn't one of the "
f"allowed values {allowed_values}"
)
filtered[key] = val
# Special handling if the dict already contains 'extra_entities'. This makes
# it easy to reconstruct entities from a df row.
elif key == "extra_entities":
assert isinstance(
val, dict
), "Value for 'extra_entities' key must be a dict"
for k, v in val.items():
add_to_extra(k, v)
elif not valid_only:
add_to_extra(key, val)
return cls(**filtered, extra_entities=extra_entities)
@classmethod
def from_path(cls, path: StrOrPath):
"""
Initialize from a file path.
"""
entities = parse_bids_entities(path)
return cls.from_dict(entities)
def to_dict(self, valid_only: bool = False) -> Dict[str, Any]:
"""
Convert entities to a plain dict.
"""
data = asdict(self)
extra = data.pop("extra_entities")
if not valid_only and extra:
data.update(extra)
return data
def to_path(
self,
prefix: Optional[StrOrPath] = None,
valid_only: bool = False,
int_format: str = "%d",
) -> Path:
"""
Generate a filepath based on the entities.
"""
special = {"datatype", "suffix", "ext"}
data = self.to_dict(valid_only=valid_only)
name = "_".join(
_fmt_ent(k, v, int_format=int_format)
for k, v in data.items()
if k not in special and not pd.isna(v)
)
if self.suffix:
name += f"_{self.suffix}"
if self.ext:
name += self.ext
path = Path(name)
if self.datatype:
path = self.datatype / path
if self.ses:
path = f"ses-{self.ses}" / path
path = f"sub-{self.sub}" / path
if prefix:
path = prefix / path
return path
def with_update(
self, entities: Optional[Dict[str, Any]] = None, **kwargs
) -> Self:
"""
Create a new instance with updated entities.
"""
data = self.to_dict(valid_only=False)
if entities:
data.update(entities)
if kwargs:
data.update(kwargs)
return self.__class__.from_dict(data)
def make_bids_field(
entity_schema: Dict[str, Any],
) -> Tuple[str, Any, Any]:
"""
BIDS entity dataclass field.
"""
metadata = {
"name": entity_schema["name"],
"display_name": entity_schema["display_name"],
"allowed_values": entity_schema.get("enum"),
}
type_ = {"index": int}.get(entity_schema["format"], str)
field_ = field(default=None, metadata=metadata)
return entity_schema["name"], Optional[type_], field_
_sorted_entities = [
_get_bids_schema().objects.entities[field_name]
for field_name in _get_bids_schema().rules.entities
]
BIDSEntities: Any = make_dataclass(
"BIDSEntities",
[
make_bids_field(dict(f))
for f in _sorted_entities
if f.name not in {f.name for f in fields(_BIDSEntitiesBase)}
],
bases=(_BIDSEntitiesBase,),
)
del _sorted_entities
def _get_type(alias: Any) -> type:
"""
Unbox type aliases of the form `Optional[str]`.
"""
if _is_optional(alias):
return get_args(alias)[0]
return alias
def _is_optional(alias: Any) -> bool:
"""
Check if alias is an optional of a primitive type like `Optional[str]`.
"""
return (
get_origin(alias) is Union
and len(get_args(alias)) == 2
and isinstance(None, get_args(alias)[1])
)
def _fmt_ent(
k: str,
v: Union[str, int],
*,
int_format: str = "%d",
):
if isinstance(v, int):
v = int_format % v
ent = f"{k}-{v}" if len(v) > 0 else k
return ent
@lru_cache()
def parse_bids_entities(path: StrOrPath) -> Dict[str, str]:
"""
Parse all BIDS filename ``"{key}-{value}"`` entities as well as special entities:
- datatype
- suffix
- ext (extension)
from the file path.
.. note:: This function does not validate entities.
"""
path = Path(path)
entities = {}
# datatype
match = re.search(
f"/({'|'.join(BIDS_DATATYPES)})/",
path.as_posix(),
)
datatype = match.group(1) if match is not None else None
filename = path.name
parts = filename.split("_")
# suffix and extension
suffix_ext = parts.pop()
idx = suffix_ext.find(".")
if idx < 0:
suffix, ext = suffix_ext, None
else:
suffix, ext = suffix_ext[:idx], suffix_ext[idx:]
# suffix is actually an entity, put back in list
if "-" in suffix:
parts.append(suffix)
suffix = None
# parse entities
for part in parts:
if "-" in part:
key, val = part.split("-", maxsplit=1)
else:
key, val = part, ""
entities[key] = val
for k, v in zip(["datatype", "suffix", "ext"], [datatype, suffix, ext]):
if v is not None:
entities[k] = v
return entities
ENTITY_NAMES_TO_KEYS = MappingProxyType(
{f.metadata["name"]: f.name for f in fields(BIDSEntities)}
)