Skip to content

Commit 9185ba7

Browse files
committed
add and use global mapping for datatype to modality
1 parent 0d427f4 commit 9185ba7

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/bids_validator/context.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ class ValidationError(Exception):
4646
"""TODO: Add issue structure."""
4747

4848

49+
_DATATYPE_MAP = {}
50+
51+
52+
def datatype_to_modality(datatype: str, schema: Namespace) -> str:
53+
"""Generate a global map for datatype to modality."""
54+
if not _DATATYPE_MAP:
55+
for mod_name, mod_dtypes in schema.rules.modalities.items():
56+
_DATATYPE_MAP |= dict.from_keys(mod_dtypes, mod_name)
57+
return _DATATYPE_MAP[datatype]
58+
59+
4960
@cache
5061
def load_tsv(file: FileTree, *, max_rows=0) -> Namespace:
5162
"""Load TSV contents into a Namespace."""
@@ -130,14 +141,7 @@ def dataset_description(self) -> Namespace:
130141
@cached_property
131142
def modalities(self) -> list[str]:
132143
"""List of modalities found in the dataset."""
133-
result = set()
134-
135-
modalities = self.schema.rules.modalities
136-
for datatype in self.datatypes:
137-
for mod_name, mod_dtypes in modalities.items():
138-
if datatype in mod_dtypes.datatypes:
139-
result.add(mod_name)
140-
144+
result = {datatype_to_modality(datatype, self.schema) for datatype in self.datatypes}
141145
return list(result)
142146

143147
@cached_property
@@ -344,10 +348,7 @@ def extension(self) -> str | None:
344348
@property
345349
def modality(self) -> str | None:
346350
"""Modality of current file, for examples, MRI."""
347-
modalities = self.schema.rules.modalities
348-
for mod_name, mod_dtypes in modalities.items():
349-
if self.datatype in mod_dtypes.datatypes:
350-
return mod_name
351+
return datatype_to_modality(self.datatype, self.schema)
351352

352353
@property
353354
def size(self) -> int:

0 commit comments

Comments
 (0)