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.
Files changed (34) hide show
  1. omids_validator/__init__.py +11 -0
  2. omids_validator/__main__.py +11 -0
  3. omids_validator/acquisition_profiles.py +478 -0
  4. omids_validator/cli.py +185 -0
  5. omids_validator/data/metadata_requirements.json +337 -0
  6. omids_validator/data/rules.json +364 -0
  7. omids_validator/file_types.py +46 -0
  8. omids_validator/inventory.py +133 -0
  9. omids_validator/models.py +60 -0
  10. omids_validator/participant_table.py +128 -0
  11. omids_validator/participants.py +235 -0
  12. omids_validator/patient_metadata.py +150 -0
  13. omids_validator/reporters/__init__.py +1 -0
  14. omids_validator/reporters/html_report.py +607 -0
  15. omids_validator/reporters/json_report.py +74 -0
  16. omids_validator/reporters/markdown_report.py +283 -0
  17. omids_validator/reporters/report_data.py +79 -0
  18. omids_validator/reporters/terminal.py +18 -0
  19. omids_validator/rule_catalog.py +279 -0
  20. omids_validator/rules/__init__.py +5 -0
  21. omids_validator/rules/layer0_structure.py +316 -0
  22. omids_validator/rules/layer1_naming.py +191 -0
  23. omids_validator/rules/layer2_sidecars.py +292 -0
  24. omids_validator/rules/layer3_metadata.py +144 -0
  25. omids_validator/rules/layer4_image_headers.py +152 -0
  26. omids_validator/rules/layer5_dataset.py +261 -0
  27. omids_validator/rules_document.py +192 -0
  28. omids_validator/validator.py +274 -0
  29. omids_validator-0.2.0.dist-info/METADATA +158 -0
  30. omids_validator-0.2.0.dist-info/RECORD +34 -0
  31. omids_validator-0.2.0.dist-info/WHEEL +5 -0
  32. omids_validator-0.2.0.dist-info/entry_points.txt +3 -0
  33. omids_validator-0.2.0.dist-info/licenses/LICENSE +677 -0
  34. omids_validator-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,316 @@
1
+ """Layer 0: check the dataset's folders and how they are arranged.
2
+
3
+ These rules check the dataset path and the expected subject, session, and
4
+ imaging-type folders.
5
+ """
6
+
7
+ import re
8
+ from pathlib import Path
9
+ from typing import Callable
10
+
11
+ from omids_validator.file_types import is_nifti_file
12
+ from omids_validator.inventory import (
13
+ VALID_DATATYPE_DIRECTORIES,
14
+ DatasetInventory,
15
+ build_inventory,
16
+ )
17
+ from omids_validator.models import Finding
18
+ from omids_validator.rule_catalog import finding_for_rule
19
+
20
+
21
+ SUBJECT_PATTERN = re.compile(r"sub-[A-Za-z0-9]+")
22
+ SESSION_PATTERN = re.compile(r"ses-[A-Za-z0-9]+")
23
+
24
+
25
+ def _is_operating_system_artifact(path: Path) -> bool:
26
+ """Return whether a path is a common operating-system artifact."""
27
+
28
+ lower_name = path.name.lower()
29
+ return (
30
+ path.name == ".DS_Store"
31
+ or lower_name in {"thumbs.db", "desktop.ini"}
32
+ or path.name.startswith("._")
33
+ )
34
+
35
+
36
+ def mids_001_invalid_dataset_path(dataset_path: Path) -> list[Finding]:
37
+ """MIDS-001: the supplied path must be a readable directory."""
38
+
39
+ path = dataset_path.expanduser()
40
+
41
+ if not path.exists():
42
+ message = "Dataset path does not exist."
43
+ elif not path.is_dir():
44
+ message = "Dataset path is not a directory."
45
+ else:
46
+ try:
47
+ next(path.iterdir(), None)
48
+ except PermissionError:
49
+ message = "Dataset directory is not readable."
50
+ else:
51
+ return []
52
+
53
+ return [
54
+ finding_for_rule(
55
+ "MIDS-001",
56
+ message=message,
57
+ path=str(path),
58
+ )
59
+ ]
60
+
61
+
62
+ def mids_002_unexpected_root_items(
63
+ inventory: DatasetInventory,
64
+ ) -> list[Finding]:
65
+ """MIDS-002: report unsupported items in the dataset root."""
66
+
67
+ findings: list[Finding] = []
68
+ has_exact_participants_table = any(
69
+ item.is_file() and item.name == "participants.tsv"
70
+ for item in inventory.root_items
71
+ )
72
+
73
+ for item in inventory.root_items:
74
+ is_subject = item.is_dir() and item.name.startswith("sub-")
75
+ is_participants = item.is_file() and item.name == "participants.tsv"
76
+ is_mis_cased_participants = (
77
+ not has_exact_participants_table
78
+ and item.is_file()
79
+ and item.name.casefold() == "participants.tsv"
80
+ )
81
+
82
+ if (
83
+ not is_subject
84
+ and not is_participants
85
+ and not is_mis_cased_participants
86
+ and not _is_operating_system_artifact(item)
87
+ ):
88
+ findings.append(
89
+ finding_for_rule(
90
+ "MIDS-002",
91
+ message=(
92
+ "Dataset root items must be subject directories or "
93
+ "participants.tsv."
94
+ ),
95
+ path=inventory.relative_path(item),
96
+ )
97
+ )
98
+
99
+ return findings
100
+
101
+
102
+ def mids_003_invalid_subject_identifiers(
103
+ inventory: DatasetInventory,
104
+ ) -> list[Finding]:
105
+ """MIDS-003: validate subject directory identifiers."""
106
+
107
+ return [
108
+ finding_for_rule(
109
+ "MIDS-003",
110
+ message="Subject directory must follow sub-<alphanumeric>.",
111
+ path=inventory.relative_path(subject),
112
+ )
113
+ for subject in inventory.subject_directories()
114
+ if SUBJECT_PATTERN.fullmatch(subject.name) is None
115
+ ]
116
+
117
+
118
+ def mids_004_invalid_session_identifiers(
119
+ inventory: DatasetInventory,
120
+ ) -> list[Finding]:
121
+ """MIDS-004: validate optional session directory identifiers."""
122
+
123
+ findings: list[Finding] = []
124
+
125
+ for subject in inventory.subject_directories():
126
+ for session in inventory.session_directories(subject):
127
+ if SESSION_PATTERN.fullmatch(session.name) is None:
128
+ findings.append(
129
+ finding_for_rule(
130
+ "MIDS-004",
131
+ message="Session directory must follow ses-<alphanumeric>.",
132
+ path=inventory.relative_path(session),
133
+ )
134
+ )
135
+
136
+ return findings
137
+
138
+
139
+ def mids_005_mixed_subject_structure(
140
+ inventory: DatasetInventory,
141
+ ) -> list[Finding]:
142
+ """MIDS-005: prevent mixing sessions and direct datatype directories."""
143
+
144
+ findings: list[Finding] = []
145
+
146
+ for subject in inventory.subject_directories():
147
+ sessions = inventory.session_directories(subject)
148
+ direct_datatypes = tuple(
149
+ item
150
+ for item in inventory.children(subject)
151
+ if item.is_dir() and item.name in VALID_DATATYPE_DIRECTORIES
152
+ )
153
+
154
+ if sessions and direct_datatypes:
155
+ findings.append(
156
+ finding_for_rule(
157
+ "MIDS-005",
158
+ message=(
159
+ "Subject must use session directories or direct "
160
+ "datatype directories, not both."
161
+ ),
162
+ path=inventory.relative_path(subject),
163
+ )
164
+ )
165
+
166
+ return findings
167
+
168
+
169
+ def mids_006_unrecognized_datatype_items(
170
+ inventory: DatasetInventory,
171
+ ) -> list[Finding]:
172
+ """MIDS-006: report unsupported items where datatype folders belong."""
173
+
174
+ findings: list[Finding] = []
175
+
176
+ for subject in inventory.subject_directories():
177
+ sessions = inventory.session_directories(subject)
178
+ subject_children = inventory.children(subject)
179
+
180
+ for item in subject_children:
181
+ allowed_subject_item = (
182
+ item in sessions
183
+ or (item.is_dir() and item.name in VALID_DATATYPE_DIRECTORIES)
184
+ or _is_operating_system_artifact(item)
185
+ )
186
+ if not allowed_subject_item:
187
+ findings.append(
188
+ finding_for_rule(
189
+ "MIDS-006",
190
+ message=(
191
+ "Only session or recognized datatype directories "
192
+ "are permitted at this level."
193
+ ),
194
+ path=inventory.relative_path(item),
195
+ )
196
+ )
197
+
198
+ for session in sessions:
199
+ for item in inventory.children(session):
200
+ allowed_session_item = (
201
+ (item.is_dir() and item.name in VALID_DATATYPE_DIRECTORIES)
202
+ or _is_operating_system_artifact(item)
203
+ )
204
+ if not allowed_session_item:
205
+ findings.append(
206
+ finding_for_rule(
207
+ "MIDS-006",
208
+ message=(
209
+ "Only recognized datatype directories are "
210
+ "permitted inside a session."
211
+ ),
212
+ path=inventory.relative_path(item),
213
+ )
214
+ )
215
+
216
+ return findings
217
+
218
+
219
+ def mids_007_empty_datatype_directories(
220
+ inventory: DatasetInventory,
221
+ ) -> list[Finding]:
222
+ """MIDS-007: report datatype directories containing no NIfTI images."""
223
+
224
+ findings: list[Finding] = []
225
+
226
+ for subject in inventory.subject_directories():
227
+ for datatype in inventory.datatype_directories(subject):
228
+ contains_image = any(
229
+ is_nifti_file(item) for item in inventory.children(datatype)
230
+ )
231
+ if not contains_image:
232
+ findings.append(
233
+ finding_for_rule(
234
+ "MIDS-007",
235
+ message="Datatype directory contains no NIfTI images.",
236
+ path=inventory.relative_path(datatype),
237
+ )
238
+ )
239
+
240
+ return findings
241
+
242
+
243
+ def mids_008_subjects_without_imaging_data(
244
+ inventory: DatasetInventory,
245
+ ) -> list[Finding]:
246
+ """MIDS-008: report subjects with no NIfTI images in known datatypes."""
247
+
248
+ findings: list[Finding] = []
249
+
250
+ for subject in inventory.subject_directories():
251
+ datatypes = inventory.datatype_directories(subject)
252
+ contains_image = any(
253
+ is_nifti_file(item)
254
+ for datatype in datatypes
255
+ for item in inventory.children(datatype)
256
+ )
257
+
258
+ if not contains_image:
259
+ findings.append(
260
+ finding_for_rule(
261
+ "MIDS-008",
262
+ message=(
263
+ "Subject contains no NIfTI images in recognized "
264
+ "datatype directories."
265
+ ),
266
+ path=inventory.relative_path(subject),
267
+ )
268
+ )
269
+
270
+ return findings
271
+
272
+
273
+ def mids_009_operating_system_artifacts(
274
+ inventory: DatasetInventory,
275
+ ) -> list[Finding]:
276
+ """MIDS-009: report common operating-system artifacts anywhere in data."""
277
+
278
+ return [
279
+ finding_for_rule(
280
+ "MIDS-009",
281
+ message="Remove operating-system artifacts before sharing data.",
282
+ path=inventory.relative_path(item),
283
+ )
284
+ for item in inventory.all_items
285
+ if _is_operating_system_artifact(item)
286
+ ]
287
+
288
+
289
+ StructureRule = Callable[[DatasetInventory], list[Finding]]
290
+
291
+ STRUCTURE_RULES: tuple[StructureRule, ...] = (
292
+ mids_002_unexpected_root_items,
293
+ mids_003_invalid_subject_identifiers,
294
+ mids_004_invalid_session_identifiers,
295
+ mids_005_mixed_subject_structure,
296
+ mids_006_unrecognized_datatype_items,
297
+ mids_007_empty_datatype_directories,
298
+ mids_008_subjects_without_imaging_data,
299
+ mids_009_operating_system_artifacts,
300
+ )
301
+
302
+
303
+ def validate_structure(dataset_path: Path) -> list[Finding]:
304
+ """Run MIDS-001 through MIDS-009 in rule order."""
305
+
306
+ path_findings = mids_001_invalid_dataset_path(dataset_path)
307
+ if path_findings:
308
+ return path_findings
309
+
310
+ inventory = build_inventory(dataset_path)
311
+ findings: list[Finding] = []
312
+
313
+ for rule in STRUCTURE_RULES:
314
+ findings.extend(rule(inventory))
315
+
316
+ return findings
@@ -0,0 +1,191 @@
1
+ """Layer 1: check file names and where the files are stored.
2
+
3
+ These rules check subject identifiers, recognized scan suffixes, and names that
4
+ differ only by uppercase or lowercase letters.
5
+ """
6
+
7
+ import re
8
+ from pathlib import Path
9
+ from typing import Callable
10
+
11
+ from omids_validator.acquisition_profiles import (
12
+ acquisition_profile_for_suffix,
13
+ image_suffix,
14
+ )
15
+ from omids_validator.file_types import is_supported_file
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_COMPONENT_PATTERN = re.compile(
22
+ r"(?:^|_)(sub-[A-Za-z0-9]+)(?=_|\.|$)"
23
+ )
24
+
25
+
26
+ def mids_101_unsupported_item(inventory: DatasetInventory) -> list[Finding]:
27
+ """MIDS-101: report unsupported items in datatype directories."""
28
+
29
+ findings: list[Finding] = []
30
+
31
+ for subject_directory in inventory.subject_directories():
32
+ for datatype_directory in inventory.datatype_directories(
33
+ subject_directory
34
+ ):
35
+ for item in inventory.children(datatype_directory):
36
+ if not is_supported_file(item):
37
+ findings.append(
38
+ finding_for_rule(
39
+ "MIDS-101",
40
+ message=(
41
+ "Datatype directories may contain only NIfTI "
42
+ "images and JSON sidecars."
43
+ ),
44
+ path=inventory.relative_path(item),
45
+ )
46
+ )
47
+
48
+ return findings
49
+
50
+
51
+ def mids_102_subject_identifier_mismatch(
52
+ inventory: DatasetInventory,
53
+ ) -> list[Finding]:
54
+ """MIDS-102: require filenames to match their subject directory."""
55
+
56
+ findings: list[Finding] = []
57
+
58
+ for subject_directory in inventory.subject_directories():
59
+ expected_subject = subject_directory.name
60
+
61
+ for datatype_directory in inventory.datatype_directories(
62
+ subject_directory
63
+ ):
64
+ for file_path in inventory.children(datatype_directory):
65
+ if not is_supported_file(file_path):
66
+ continue
67
+
68
+ subject_match = SUBJECT_COMPONENT_PATTERN.search(file_path.name)
69
+ actual_subject = (
70
+ subject_match.group(1) if subject_match is not None else None
71
+ )
72
+
73
+ if actual_subject != expected_subject:
74
+ findings.append(
75
+ finding_for_rule(
76
+ "MIDS-102",
77
+ message=(
78
+ f"Filename must contain the subject identifier "
79
+ f"{expected_subject}."
80
+ ),
81
+ path=inventory.relative_path(file_path),
82
+ )
83
+ )
84
+
85
+ return findings
86
+
87
+
88
+ def mids_103_suffix_in_wrong_datatype(
89
+ inventory: DatasetInventory,
90
+ ) -> list[Finding]:
91
+ """MIDS-103: require recognized suffixes to use their datatype."""
92
+
93
+ findings: list[Finding] = []
94
+
95
+ for subject_directory in inventory.subject_directories():
96
+ for datatype_directory in inventory.datatype_directories(
97
+ subject_directory
98
+ ):
99
+ for file_path in inventory.children(datatype_directory):
100
+ suffix = image_suffix(file_path)
101
+ if suffix is None:
102
+ continue
103
+
104
+ profile = acquisition_profile_for_suffix(suffix)
105
+ if profile is None:
106
+ continue
107
+
108
+ expected_datatypes = profile["folders"]
109
+ actual_datatype = datatype_directory.name
110
+
111
+ if actual_datatype not in expected_datatypes:
112
+ expected_text = " or ".join(expected_datatypes)
113
+ findings.append(
114
+ finding_for_rule(
115
+ "MIDS-103",
116
+ message=(
117
+ f"Suffix {suffix} belongs in {expected_text}, "
118
+ f"not {actual_datatype}."
119
+ ),
120
+ path=inventory.relative_path(file_path),
121
+ )
122
+ )
123
+
124
+ return findings
125
+
126
+
127
+ def mids_104_case_conflicting_filenames(
128
+ inventory: DatasetInventory,
129
+ ) -> list[Finding]:
130
+ """MIDS-104: report filenames that differ only by capitalization."""
131
+
132
+ case_groups: dict[tuple[Path, str], list[Path]] = {}
133
+
134
+ for subject_directory in inventory.subject_directories():
135
+ for datatype_directory in inventory.datatype_directories(
136
+ subject_directory
137
+ ):
138
+ for file_path in inventory.children(datatype_directory):
139
+ if not is_supported_file(file_path):
140
+ continue
141
+
142
+ comparison_key = (
143
+ file_path.parent,
144
+ file_path.name.casefold(),
145
+ )
146
+ case_groups.setdefault(comparison_key, []).append(file_path)
147
+
148
+ findings: list[Finding] = []
149
+
150
+ for conflicting_paths in case_groups.values():
151
+ if len(conflicting_paths) < 2:
152
+ continue
153
+
154
+ conflicting_names = ", ".join(
155
+ path.name for path in conflicting_paths
156
+ )
157
+ for file_path in conflicting_paths:
158
+ findings.append(
159
+ finding_for_rule(
160
+ "MIDS-104",
161
+ message=(
162
+ "Filename differs only by capitalization from another "
163
+ f"file in the same directory: {conflicting_names}."
164
+ ),
165
+ path=inventory.relative_path(file_path),
166
+ )
167
+ )
168
+
169
+ return findings
170
+
171
+
172
+ NamingRule = Callable[[DatasetInventory], list[Finding]]
173
+
174
+ NAMING_RULES: tuple[NamingRule, ...] = (
175
+ mids_101_unsupported_item,
176
+ mids_102_subject_identifier_mismatch,
177
+ mids_103_suffix_in_wrong_datatype,
178
+ mids_104_case_conflicting_filenames,
179
+ )
180
+
181
+
182
+ def validate_naming(dataset_path: Path) -> list[Finding]:
183
+ """Run 100 series MIDS rules on the dataset at the given path."""
184
+
185
+ inventory = build_inventory(dataset_path)
186
+ findings: list[Finding] = []
187
+
188
+ for rule in NAMING_RULES:
189
+ findings.extend(rule(inventory))
190
+
191
+ return findings