omids-validator 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omids_validator/__init__.py +11 -0
- omids_validator/__main__.py +11 -0
- omids_validator/acquisition_profiles.py +478 -0
- omids_validator/cli.py +185 -0
- omids_validator/data/metadata_requirements.json +337 -0
- omids_validator/data/rules.json +364 -0
- omids_validator/file_types.py +46 -0
- omids_validator/inventory.py +133 -0
- omids_validator/models.py +60 -0
- omids_validator/participant_table.py +128 -0
- omids_validator/participants.py +235 -0
- omids_validator/patient_metadata.py +150 -0
- omids_validator/reporters/__init__.py +1 -0
- omids_validator/reporters/html_report.py +607 -0
- omids_validator/reporters/json_report.py +74 -0
- omids_validator/reporters/markdown_report.py +283 -0
- omids_validator/reporters/report_data.py +79 -0
- omids_validator/reporters/terminal.py +18 -0
- omids_validator/rule_catalog.py +279 -0
- omids_validator/rules/__init__.py +5 -0
- omids_validator/rules/layer0_structure.py +316 -0
- omids_validator/rules/layer1_naming.py +191 -0
- omids_validator/rules/layer2_sidecars.py +292 -0
- omids_validator/rules/layer3_metadata.py +144 -0
- omids_validator/rules/layer4_image_headers.py +152 -0
- omids_validator/rules/layer5_dataset.py +261 -0
- omids_validator/rules_document.py +192 -0
- omids_validator/validator.py +274 -0
- omids_validator-0.2.0.dist-info/METADATA +158 -0
- omids_validator-0.2.0.dist-info/RECORD +34 -0
- omids_validator-0.2.0.dist-info/WHEEL +5 -0
- omids_validator-0.2.0.dist-info/entry_points.txt +3 -0
- omids_validator-0.2.0.dist-info/licenses/LICENSE +677 -0
- omids_validator-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"""Layer 2: check the JSON sidecar files that accompany each image.
|
|
2
|
+
|
|
3
|
+
These rules find missing, unreadable, incorrectly named, and unpaired primary,
|
|
4
|
+
patient, and extra JSON files.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Callable
|
|
10
|
+
|
|
11
|
+
from omids_validator.file_types import is_json_file, is_nifti_file, nifti_stem
|
|
12
|
+
from omids_validator.inventory import DatasetInventory, build_inventory
|
|
13
|
+
from omids_validator.models import Finding
|
|
14
|
+
from omids_validator.rule_catalog import finding_for_rule
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _image_stem_for_json_sidecar(json_path: Path) -> str:
|
|
18
|
+
"""Return the image stem represented by a primary or optional sidecar."""
|
|
19
|
+
|
|
20
|
+
json_stem = json_path.name[:-5]
|
|
21
|
+
normalized_stem = json_stem.casefold()
|
|
22
|
+
|
|
23
|
+
if normalized_stem.endswith("_patient"):
|
|
24
|
+
return json_stem[:-8]
|
|
25
|
+
if normalized_stem.endswith("_extra"):
|
|
26
|
+
return json_stem[:-6]
|
|
27
|
+
|
|
28
|
+
return json_stem
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def mids_201_missing_primary_json(inventory: DatasetInventory) -> list[Finding]:
|
|
32
|
+
"""MIDS-201: require one primary JSON sidecar for every NIfTI image."""
|
|
33
|
+
|
|
34
|
+
findings: list[Finding] = []
|
|
35
|
+
|
|
36
|
+
for subject_directory in inventory.subject_directories():
|
|
37
|
+
for datatype_directory in inventory.datatype_directories(
|
|
38
|
+
subject_directory
|
|
39
|
+
):
|
|
40
|
+
datatype_items = inventory.children(datatype_directory)
|
|
41
|
+
json_names = {
|
|
42
|
+
item.name.casefold()
|
|
43
|
+
for item in datatype_items
|
|
44
|
+
if is_json_file(item)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for image_path in datatype_items:
|
|
48
|
+
if not is_nifti_file(image_path):
|
|
49
|
+
continue
|
|
50
|
+
|
|
51
|
+
image_stem = nifti_stem(image_path)
|
|
52
|
+
if image_stem is None:
|
|
53
|
+
continue
|
|
54
|
+
|
|
55
|
+
expected_json_name = f"{image_stem}.json"
|
|
56
|
+
if expected_json_name.casefold() not in json_names:
|
|
57
|
+
findings.append(
|
|
58
|
+
finding_for_rule(
|
|
59
|
+
"MIDS-201",
|
|
60
|
+
message=(
|
|
61
|
+
"NIfTI image requires a primary JSON sidecar "
|
|
62
|
+
f"named {expected_json_name}."
|
|
63
|
+
),
|
|
64
|
+
path=inventory.relative_path(image_path),
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
return findings
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def mids_202_and_203_malformed_json_sidecars(
|
|
72
|
+
inventory: DatasetInventory,
|
|
73
|
+
) -> list[Finding]:
|
|
74
|
+
"""MIDS-202/203: report malformed primary and optional JSON sidecars."""
|
|
75
|
+
|
|
76
|
+
findings: list[Finding] = []
|
|
77
|
+
|
|
78
|
+
for subject_directory in inventory.subject_directories():
|
|
79
|
+
for datatype_directory in inventory.datatype_directories(
|
|
80
|
+
subject_directory
|
|
81
|
+
):
|
|
82
|
+
for json_path in inventory.children(datatype_directory):
|
|
83
|
+
if not is_json_file(json_path):
|
|
84
|
+
continue
|
|
85
|
+
|
|
86
|
+
try:
|
|
87
|
+
with json_path.open(encoding="utf-8") as json_file:
|
|
88
|
+
json.load(json_file)
|
|
89
|
+
except json.JSONDecodeError:
|
|
90
|
+
problem = "JSON syntax is invalid."
|
|
91
|
+
except UnicodeDecodeError:
|
|
92
|
+
problem = "JSON file is not valid UTF-8 text."
|
|
93
|
+
except OSError:
|
|
94
|
+
problem = "JSON file could not be read."
|
|
95
|
+
else:
|
|
96
|
+
continue
|
|
97
|
+
|
|
98
|
+
normalized_name = json_path.name.casefold()
|
|
99
|
+
is_optional = normalized_name.endswith(
|
|
100
|
+
("_patient.json", "_extra.json")
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
if is_optional:
|
|
104
|
+
rule_id = "MIDS-203"
|
|
105
|
+
else:
|
|
106
|
+
rule_id = "MIDS-202"
|
|
107
|
+
|
|
108
|
+
findings.append(
|
|
109
|
+
finding_for_rule(
|
|
110
|
+
rule_id,
|
|
111
|
+
message=problem,
|
|
112
|
+
path=inventory.relative_path(json_path),
|
|
113
|
+
)
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
return findings
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def mids_204_and_205_missing_optional_sidecars(
|
|
120
|
+
inventory: DatasetInventory,
|
|
121
|
+
) -> list[Finding]:
|
|
122
|
+
"""MIDS-204/205: report absent patient and extra JSON sidecars."""
|
|
123
|
+
|
|
124
|
+
findings: list[Finding] = []
|
|
125
|
+
optional_sidecars = (
|
|
126
|
+
("patient", "MIDS-204"),
|
|
127
|
+
("extra", "MIDS-205"),
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
for subject_directory in inventory.subject_directories():
|
|
131
|
+
for datatype_directory in inventory.datatype_directories(
|
|
132
|
+
subject_directory
|
|
133
|
+
):
|
|
134
|
+
datatype_items = inventory.children(datatype_directory)
|
|
135
|
+
json_names = {
|
|
136
|
+
item.name.casefold()
|
|
137
|
+
for item in datatype_items
|
|
138
|
+
if is_json_file(item)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
for image_path in datatype_items:
|
|
142
|
+
if not is_nifti_file(image_path):
|
|
143
|
+
continue
|
|
144
|
+
|
|
145
|
+
image_stem = nifti_stem(image_path)
|
|
146
|
+
if image_stem is None:
|
|
147
|
+
continue
|
|
148
|
+
|
|
149
|
+
for qualifier, rule_id in optional_sidecars:
|
|
150
|
+
expected_json_name = f"{image_stem}_{qualifier}.json"
|
|
151
|
+
if expected_json_name.casefold() in json_names:
|
|
152
|
+
continue
|
|
153
|
+
|
|
154
|
+
findings.append(
|
|
155
|
+
finding_for_rule(
|
|
156
|
+
rule_id,
|
|
157
|
+
message=(
|
|
158
|
+
"Optional JSON sidecar is absent; expected "
|
|
159
|
+
f"{expected_json_name}."
|
|
160
|
+
),
|
|
161
|
+
path=inventory.relative_path(image_path),
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
return findings
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def mids_206_orphan_json_sidecars(
|
|
169
|
+
inventory: DatasetInventory,
|
|
170
|
+
) -> list[Finding]:
|
|
171
|
+
"""MIDS-206: report JSON sidecar groups without a NIfTI image."""
|
|
172
|
+
|
|
173
|
+
findings: list[Finding] = []
|
|
174
|
+
|
|
175
|
+
for subject_directory in inventory.subject_directories():
|
|
176
|
+
for datatype_directory in inventory.datatype_directories(
|
|
177
|
+
subject_directory
|
|
178
|
+
):
|
|
179
|
+
datatype_items = inventory.children(datatype_directory)
|
|
180
|
+
nifti_stems: set[str] = set()
|
|
181
|
+
|
|
182
|
+
for item in datatype_items:
|
|
183
|
+
image_stem = nifti_stem(item)
|
|
184
|
+
|
|
185
|
+
if image_stem is not None:
|
|
186
|
+
nifti_stems.add(image_stem.casefold())
|
|
187
|
+
|
|
188
|
+
json_groups: dict[str, list[Path]] = {}
|
|
189
|
+
|
|
190
|
+
for json_path in datatype_items:
|
|
191
|
+
if not is_json_file(json_path):
|
|
192
|
+
continue
|
|
193
|
+
|
|
194
|
+
image_stem = _image_stem_for_json_sidecar(json_path)
|
|
195
|
+
normalized_stem = image_stem.casefold()
|
|
196
|
+
json_groups.setdefault(normalized_stem, []).append(json_path)
|
|
197
|
+
|
|
198
|
+
for normalized_stem, json_paths in json_groups.items():
|
|
199
|
+
if normalized_stem in nifti_stems:
|
|
200
|
+
continue
|
|
201
|
+
|
|
202
|
+
primary_name = f"{normalized_stem}.json"
|
|
203
|
+
representative_path = next(
|
|
204
|
+
(
|
|
205
|
+
path
|
|
206
|
+
for path in json_paths
|
|
207
|
+
if path.name.casefold() == primary_name
|
|
208
|
+
),
|
|
209
|
+
json_paths[0],
|
|
210
|
+
)
|
|
211
|
+
orphaned_names = ", ".join(path.name for path in json_paths)
|
|
212
|
+
|
|
213
|
+
findings.append(
|
|
214
|
+
finding_for_rule(
|
|
215
|
+
"MIDS-206",
|
|
216
|
+
message=(
|
|
217
|
+
"JSON sidecars have no corresponding NIfTI image: "
|
|
218
|
+
f"{orphaned_names}."
|
|
219
|
+
),
|
|
220
|
+
path=inventory.relative_path(representative_path),
|
|
221
|
+
)
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
return findings
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def mids_207_invalid_extra_json_structure(
|
|
228
|
+
inventory: DatasetInventory,
|
|
229
|
+
) -> list[Finding]:
|
|
230
|
+
"""MIDS-207: require exact meta and extra top-level structures."""
|
|
231
|
+
|
|
232
|
+
findings: list[Finding] = []
|
|
233
|
+
expected_keys = {"meta", "extra"}
|
|
234
|
+
|
|
235
|
+
for subject_directory in inventory.subject_directories():
|
|
236
|
+
for datatype_directory in inventory.datatype_directories(
|
|
237
|
+
subject_directory
|
|
238
|
+
):
|
|
239
|
+
for json_path in inventory.children(datatype_directory):
|
|
240
|
+
if not is_json_file(json_path):
|
|
241
|
+
continue
|
|
242
|
+
if not json_path.name.casefold().endswith("_extra.json"):
|
|
243
|
+
continue
|
|
244
|
+
|
|
245
|
+
try:
|
|
246
|
+
with json_path.open(encoding="utf-8") as json_file:
|
|
247
|
+
json_data = json.load(json_file)
|
|
248
|
+
except (json.JSONDecodeError, UnicodeDecodeError, OSError):
|
|
249
|
+
continue
|
|
250
|
+
|
|
251
|
+
is_json_object = isinstance(json_data, dict)
|
|
252
|
+
has_expected_keys = (
|
|
253
|
+
is_json_object
|
|
254
|
+
and set(json_data.keys()) == expected_keys
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
if not has_expected_keys:
|
|
258
|
+
findings.append(
|
|
259
|
+
finding_for_rule(
|
|
260
|
+
"MIDS-207",
|
|
261
|
+
message=(
|
|
262
|
+
"Extra JSON sidecar must be an object with "
|
|
263
|
+
"exactly the top-level keys meta and extra."
|
|
264
|
+
),
|
|
265
|
+
path=inventory.relative_path(json_path),
|
|
266
|
+
)
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
return findings
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
SidecarRule = Callable[[DatasetInventory], list[Finding]]
|
|
273
|
+
|
|
274
|
+
SIDECAR_RULES: tuple[SidecarRule, ...] = (
|
|
275
|
+
mids_201_missing_primary_json,
|
|
276
|
+
mids_202_and_203_malformed_json_sidecars,
|
|
277
|
+
mids_204_and_205_missing_optional_sidecars,
|
|
278
|
+
mids_206_orphan_json_sidecars,
|
|
279
|
+
mids_207_invalid_extra_json_structure,
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def validate_sidecars(dataset_path: Path) -> list[Finding]:
|
|
284
|
+
"""Run 200-series MIDS rules on the dataset at the given path."""
|
|
285
|
+
|
|
286
|
+
inventory = build_inventory(dataset_path)
|
|
287
|
+
findings: list[Finding] = []
|
|
288
|
+
|
|
289
|
+
for rule in SIDECAR_RULES:
|
|
290
|
+
findings.extend(rule(inventory))
|
|
291
|
+
|
|
292
|
+
return findings
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Layer 3: check the metadata expected for each recognized scan type.
|
|
2
|
+
|
|
3
|
+
These rules read primary JSON sidecars and report missing or unusable metadata
|
|
4
|
+
fields based on the scan suffix in the matching image name.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Callable
|
|
10
|
+
|
|
11
|
+
from omids_validator.acquisition_profiles import (
|
|
12
|
+
image_suffix,
|
|
13
|
+
metadata_fields_for_suffix,
|
|
14
|
+
)
|
|
15
|
+
from omids_validator.file_types import is_json_file, is_nifti_file, nifti_stem
|
|
16
|
+
from omids_validator.inventory import DatasetInventory, build_inventory
|
|
17
|
+
from omids_validator.models import Finding
|
|
18
|
+
from omids_validator.rule_catalog import finding_for_rule
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _is_empty_metadata_value(value: object) -> bool:
|
|
22
|
+
"""Return whether a present metadata value contains no usable value."""
|
|
23
|
+
|
|
24
|
+
if value is None:
|
|
25
|
+
return True
|
|
26
|
+
if isinstance(value, str):
|
|
27
|
+
return value.strip() == ""
|
|
28
|
+
if isinstance(value, (list, dict)):
|
|
29
|
+
return len(value) == 0
|
|
30
|
+
|
|
31
|
+
return False
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def mids_301_303_304_metadata_fields(
|
|
35
|
+
inventory: DatasetInventory,
|
|
36
|
+
) -> list[Finding]:
|
|
37
|
+
"""Check missing and empty fields without performing MIDS-302."""
|
|
38
|
+
|
|
39
|
+
findings: list[Finding] = []
|
|
40
|
+
|
|
41
|
+
for subject_directory in inventory.subject_directories():
|
|
42
|
+
for datatype_directory in inventory.datatype_directories(
|
|
43
|
+
subject_directory
|
|
44
|
+
):
|
|
45
|
+
datatype_items = inventory.children(datatype_directory)
|
|
46
|
+
json_paths = {
|
|
47
|
+
item.name.casefold(): item
|
|
48
|
+
for item in datatype_items
|
|
49
|
+
if is_json_file(item)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for image_path in datatype_items:
|
|
53
|
+
if not is_nifti_file(image_path):
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
suffix = image_suffix(image_path)
|
|
57
|
+
if suffix is None:
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
metadata_fields = metadata_fields_for_suffix(suffix)
|
|
61
|
+
if metadata_fields is None:
|
|
62
|
+
continue
|
|
63
|
+
|
|
64
|
+
image_stem = nifti_stem(image_path)
|
|
65
|
+
if image_stem is None:
|
|
66
|
+
continue
|
|
67
|
+
|
|
68
|
+
expected_json_name = f"{image_stem}.json".casefold()
|
|
69
|
+
json_path = json_paths.get(expected_json_name)
|
|
70
|
+
if json_path is None:
|
|
71
|
+
continue
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
with json_path.open(encoding="utf-8") as json_file:
|
|
75
|
+
json_data = json.load(json_file)
|
|
76
|
+
except (json.JSONDecodeError, UnicodeDecodeError, OSError):
|
|
77
|
+
continue
|
|
78
|
+
|
|
79
|
+
if not isinstance(json_data, dict):
|
|
80
|
+
continue
|
|
81
|
+
|
|
82
|
+
for field in metadata_fields:
|
|
83
|
+
field_name = field["name"]
|
|
84
|
+
requirement_status = field["requirement_status"]
|
|
85
|
+
|
|
86
|
+
if requirement_status == "mandatory":
|
|
87
|
+
if field_name not in json_data:
|
|
88
|
+
findings.append(
|
|
89
|
+
finding_for_rule(
|
|
90
|
+
"MIDS-301",
|
|
91
|
+
message=(
|
|
92
|
+
f"Mandatory field {field_name} is "
|
|
93
|
+
"absent from the primary JSON sidecar."
|
|
94
|
+
),
|
|
95
|
+
path=inventory.relative_path(json_path),
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
elif _is_empty_metadata_value(json_data[field_name]):
|
|
99
|
+
findings.append(
|
|
100
|
+
finding_for_rule(
|
|
101
|
+
"MIDS-303",
|
|
102
|
+
message=(
|
|
103
|
+
f"Mandatory field {field_name} is "
|
|
104
|
+
"present but empty."
|
|
105
|
+
),
|
|
106
|
+
path=inventory.relative_path(json_path),
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
elif (
|
|
111
|
+
requirement_status == "optional"
|
|
112
|
+
and field_name not in json_data
|
|
113
|
+
):
|
|
114
|
+
findings.append(
|
|
115
|
+
finding_for_rule(
|
|
116
|
+
"MIDS-304",
|
|
117
|
+
message=(
|
|
118
|
+
f"Optional field {field_name} is absent "
|
|
119
|
+
"from the primary JSON sidecar."
|
|
120
|
+
),
|
|
121
|
+
path=inventory.relative_path(json_path),
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
return findings
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
MetadataRule = Callable[[DatasetInventory], list[Finding]]
|
|
129
|
+
|
|
130
|
+
METADATA_RULES: tuple[MetadataRule, ...] = (
|
|
131
|
+
mids_301_303_304_metadata_fields,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def validate_metadata(dataset_path: Path) -> list[Finding]:
|
|
136
|
+
"""Run standard 300-series metadata rules for a dataset."""
|
|
137
|
+
|
|
138
|
+
inventory = build_inventory(dataset_path)
|
|
139
|
+
findings: list[Finding] = []
|
|
140
|
+
|
|
141
|
+
for rule in METADATA_RULES:
|
|
142
|
+
findings.extend(rule(inventory))
|
|
143
|
+
|
|
144
|
+
return findings
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""Layer 4: optionally check NIfTI image headers.
|
|
2
|
+
|
|
3
|
+
When requested, these rules open each image header and report unreadable images
|
|
4
|
+
or unusual dimensions among images of the same type.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from collections import Counter
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
import re
|
|
11
|
+
|
|
12
|
+
import nibabel as nib
|
|
13
|
+
from nibabel.filebasedimages import ImageFileError
|
|
14
|
+
|
|
15
|
+
from omids_validator.file_types import nifti_stem
|
|
16
|
+
from omids_validator.inventory import DatasetInventory, build_inventory
|
|
17
|
+
from omids_validator.models import Finding
|
|
18
|
+
from omids_validator.rule_catalog import finding_for_rule
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
SUBJECT_LABEL_PATTERN = re.compile(r"(?:^|_)(sub-[A-Za-z0-9]+)_(.+)$")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class ImageHeader:
|
|
26
|
+
"""Header values retained for one NIfTI image that opened successfully."""
|
|
27
|
+
|
|
28
|
+
path: Path
|
|
29
|
+
shape: tuple[int, ...]
|
|
30
|
+
image_type: str | None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class HeaderCollection:
|
|
35
|
+
"""Valid image headers and MIDS-401 findings from one inspection pass."""
|
|
36
|
+
|
|
37
|
+
headers: tuple[ImageHeader, ...]
|
|
38
|
+
findings: tuple[Finding, ...]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def image_type_key(image_path: Path) -> str | None:
|
|
42
|
+
"""Return the normalized full image label after ``sub-<ID>_``.
|
|
43
|
+
|
|
44
|
+
This is deliberately distinct from the final acquisition suffix used for
|
|
45
|
+
profile lookup. For example, ``sub-01_part-phase_MEGRE.nii.gz`` has the
|
|
46
|
+
image type ``part-phase_megre``.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
image_stem = nifti_stem(image_path)
|
|
50
|
+
if image_stem is None:
|
|
51
|
+
return None
|
|
52
|
+
|
|
53
|
+
subject_match = SUBJECT_LABEL_PATTERN.search(image_stem)
|
|
54
|
+
if subject_match is None:
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
return subject_match.group(2).casefold()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def collect_image_headers(
|
|
61
|
+
inventory: DatasetInventory,
|
|
62
|
+
) -> HeaderCollection:
|
|
63
|
+
"""Open each discovered NIfTI once and collect its shape or MIDS-401."""
|
|
64
|
+
|
|
65
|
+
headers: list[ImageHeader] = []
|
|
66
|
+
findings: list[Finding] = []
|
|
67
|
+
|
|
68
|
+
for image_path in inventory.nifti_image_paths():
|
|
69
|
+
try:
|
|
70
|
+
image = nib.load(image_path, mmap=False)
|
|
71
|
+
shape = tuple(int(dimension) for dimension in image.shape)
|
|
72
|
+
except (ImageFileError, OSError, EOFError, ValueError) as error:
|
|
73
|
+
findings.append(
|
|
74
|
+
finding_for_rule(
|
|
75
|
+
"MIDS-401",
|
|
76
|
+
message=(
|
|
77
|
+
"NIfTI header could not be opened and interpreted: "
|
|
78
|
+
f"{error}."
|
|
79
|
+
),
|
|
80
|
+
path=inventory.relative_path(image_path),
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
headers.append(
|
|
86
|
+
ImageHeader(
|
|
87
|
+
path=image_path,
|
|
88
|
+
shape=shape,
|
|
89
|
+
image_type=image_type_key(image_path),
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
return HeaderCollection(
|
|
94
|
+
headers=tuple(headers),
|
|
95
|
+
findings=tuple(findings),
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def mids_402_acquisition_dimension_outliers(
|
|
100
|
+
inventory: DatasetInventory,
|
|
101
|
+
headers: tuple[ImageHeader, ...],
|
|
102
|
+
) -> list[Finding]:
|
|
103
|
+
"""Report shapes outside an image type's 80 percent modal shape."""
|
|
104
|
+
|
|
105
|
+
headers_by_image_type: dict[str, list[ImageHeader]] = {}
|
|
106
|
+
for header in headers:
|
|
107
|
+
if header.image_type is not None:
|
|
108
|
+
headers_by_image_type.setdefault(header.image_type, []).append(header)
|
|
109
|
+
|
|
110
|
+
findings: list[Finding] = []
|
|
111
|
+
for image_type, image_headers in headers_by_image_type.items():
|
|
112
|
+
if len(image_headers) < 2:
|
|
113
|
+
continue
|
|
114
|
+
|
|
115
|
+
shape_counts = Counter(header.shape for header in image_headers)
|
|
116
|
+
common_shape, common_count = max(
|
|
117
|
+
shape_counts.items(),
|
|
118
|
+
key=lambda item: (item[1], item[0]),
|
|
119
|
+
)
|
|
120
|
+
total_count = len(image_headers)
|
|
121
|
+
if common_count * 5 < total_count * 4:
|
|
122
|
+
continue
|
|
123
|
+
|
|
124
|
+
for header in image_headers:
|
|
125
|
+
if header.shape == common_shape:
|
|
126
|
+
continue
|
|
127
|
+
|
|
128
|
+
findings.append(
|
|
129
|
+
finding_for_rule(
|
|
130
|
+
"MIDS-402",
|
|
131
|
+
message=(
|
|
132
|
+
f"Image type {image_type!r} usually has shape "
|
|
133
|
+
f"{common_shape} ({common_count} of {total_count} "
|
|
134
|
+
"readable images), but this image has shape "
|
|
135
|
+
f"{header.shape}."
|
|
136
|
+
),
|
|
137
|
+
path=inventory.relative_path(header.path),
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
return findings
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def validate_image_headers(dataset_path: Path) -> list[Finding]:
|
|
145
|
+
"""Run the implemented MIDS-401 and MIDS-402 checks for one dataset."""
|
|
146
|
+
|
|
147
|
+
inventory = build_inventory(dataset_path)
|
|
148
|
+
collection = collect_image_headers(inventory)
|
|
149
|
+
return [
|
|
150
|
+
*collection.findings,
|
|
151
|
+
*mids_402_acquisition_dimension_outliers(inventory, collection.headers),
|
|
152
|
+
]
|