-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.py
More file actions
375 lines (298 loc) · 12.7 KB
/
rules.py
File metadata and controls
375 lines (298 loc) · 12.7 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
rules.py
--------
BIDS dataset discovery rules, modelled after the Rule/RuleSet pattern
from idiscore (see rules_idiscore.py).
Each Rule encapsulates a single structural check on a candidate directory.
A RuleSet groups rules and evaluates them all at once, returning a structured
list of RuleResults rather than bare strings.
Severity levels:
HARD — a failed hard rule immediately disqualifies the directory (skipped).
SOFT — a failed soft rule is recorded as a warning; the dataset is still indexed.
Usage::
from rules import DEFAULT_BIDS_RULESET
qualifies, results = DEFAULT_BIDS_RULESET.evaluate(Path("/data/my_study"))
for r in results:
print(r)
"""
from __future__ import annotations
import json
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Callable, Iterable, Optional
# ─── Severity & Result ────────────────────────────────────────────────────────
class Severity(Enum):
"""Controls whether a failing rule blocks indexing or only warns."""
HARD = "hard"
SOFT = "soft"
@dataclass
class RuleResult:
"""Outcome of evaluating a single BIDSRule against a directory."""
rule_name: str
passed: bool
severity: Severity
message: str
@property
def is_blocker(self) -> bool:
"""True when this result should prevent the dataset from being indexed."""
return not self.passed and self.severity == Severity.HARD
def __str__(self) -> str:
status = "PASS" if self.passed else ("FAIL" if self.severity == Severity.HARD else "WARN")
return f"[{status}] {self.rule_name}: {self.message}"
# ─── Rule ─────────────────────────────────────────────────────────────────────
class BIDSRule:
"""
A single structural check applied to a candidate BIDS directory.
Analogous to `Rule` in rules_idiscore.py — it pairs an identifier
(the rule name / what is being checked) with an operation (the callable
that does the actual check).
Parameters
----------
name:
Short machine-readable identifier, e.g. ``"has_description_json"``.
description:
Human-readable explanation of what this rule checks.
severity:
HARD rules block indexing on failure; SOFT rules only warn.
check:
Callable ``(Path) -> (passed: bool, message: str)``.
"""
def __init__(
self,
name: str,
description: str,
severity: Severity,
check: Callable[[Path], tuple[bool, str]],
) -> None:
self.name = name
self.description = description
self.severity = severity
self._check = check
def evaluate(self, path: Path) -> RuleResult:
"""Run the check against `path` and return a RuleResult."""
passed, message = self._check(path)
return RuleResult(
rule_name=self.name,
passed=passed,
severity=self.severity,
message=message,
)
def __str__(self) -> str:
return f"{self.name} [{self.severity.value.upper()}]"
def as_human_readable(self) -> str:
return f"{self.name} [{self.severity.value.upper()}] — {self.description}"
# ─── RuleSet ──────────────────────────────────────────────────────────────────
class BIDSRuleSet:
"""
A named collection of BIDSRules.
Analogous to `RuleSet` in rules_idiscore.py. Evaluates all contained
rules against a path and aggregates the results. Hard rules are always
evaluated before soft rules so that blockers are surfaced first.
Parameters
----------
rules:
Iterable of BIDSRule objects.
name:
Human-readable label for this set (used in logs and repr).
"""
def __init__(self, rules: Iterable[BIDSRule], name: str = "BIDSRuleSet") -> None:
# Store hard rules first so blockers surface before warnings
all_rules = list(rules)
self._hard_rules = [r for r in all_rules if r.severity == Severity.HARD]
self._soft_rules = [r for r in all_rules if r.severity == Severity.SOFT]
self.name = name
@property
def rules(self) -> list[BIDSRule]:
"""All rules in this set, hard rules first."""
return self._hard_rules + self._soft_rules
@property
def hard_rules(self) -> list[BIDSRule]:
return list(self._hard_rules)
@property
def soft_rules(self) -> list[BIDSRule]:
return list(self._soft_rules)
def evaluate(self, path: Path) -> tuple[bool, list[RuleResult]]:
"""
Evaluate all rules against `path`.
Returns
-------
qualifies : bool
False if any HARD rule failed (dataset should be skipped).
results : list[RuleResult]
One result per rule, in hard-first order.
Notes
-----
Evaluation short-circuits after the first blocking (HARD + failed) result
so that later rules are not run against a directory that already can't
possibly qualify.
"""
results: list[RuleResult] = []
for rule in self._hard_rules:
result = rule.evaluate(path)
results.append(result)
if result.is_blocker:
# No point running remaining rules on an unqualified path
return False, results
for rule in self._soft_rules:
results.append(rule.evaluate(path))
qualifies = not any(r.is_blocker for r in results)
return qualifies, results
def get_rule(self, name: str) -> Optional[BIDSRule]:
"""Return the rule with the given name, or None if not found."""
for rule in self.rules:
if rule.name == name:
return rule
return None
def remove(self, name: str) -> None:
"""
Remove a rule by name.
Raises
------
KeyError
If no rule with that name exists in this set.
"""
for collection in (self._hard_rules, self._soft_rules):
for rule in collection:
if rule.name == name:
collection.remove(rule)
return
raise KeyError(f"No rule named '{name}' in {self}")
def as_human_readable_list(self) -> str:
"""All rules formatted for display, hard rules first."""
return "\n".join(r.as_human_readable() for r in self.rules)
def __str__(self) -> str:
return f'BIDSRuleSet "{self.name}" ({len(self._hard_rules)} hard, {len(self._soft_rules)} soft)'
def __len__(self) -> int:
return len(self._hard_rules) + len(self._soft_rules)
def __repr__(self) -> str:
return (
f"BIDSRuleSet(name={self.name!r}, "
f"hard={len(self._hard_rules)}, soft={len(self._soft_rules)})"
)
# ─── Check functions ──────────────────────────────────────────────────────────
# Each function has signature (Path) -> (bool, str) and is kept private;
# public access is through the pre-built BIDSRule instances below.
def _has_description_json(path: Path) -> tuple[bool, str]:
if (path / "dataset_description.json").exists():
return True, "dataset_description.json found"
return False, "Missing dataset_description.json"
def _description_is_valid_json(path: Path) -> tuple[bool, str]:
desc_file = path / "dataset_description.json"
if not desc_file.exists():
return False, "dataset_description.json not present (checked by prior rule)"
try:
content = json.loads(desc_file.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError) as exc:
return False, f"dataset_description.json parse error: {exc}"
if not isinstance(content, dict):
return False, "dataset_description.json must be a JSON object, not a list or scalar"
return True, "dataset_description.json is valid JSON"
def _has_name_field(path: Path) -> tuple[bool, str]:
try:
desc = json.loads((path / "dataset_description.json").read_text(encoding="utf-8"))
except Exception:
return False, "Cannot read dataset_description.json"
if "Name" in desc:
return True, f"Name = '{desc['Name']}'"
return False, "dataset_description.json missing required 'Name' field"
def _has_bids_version(path: Path) -> tuple[bool, str]:
try:
desc = json.loads((path / "dataset_description.json").read_text(encoding="utf-8"))
except Exception:
return False, "Cannot read dataset_description.json"
if "BIDSVersion" in desc:
return True, f"BIDSVersion = '{desc['BIDSVersion']}'"
return False, "dataset_description.json missing required 'BIDSVersion' field"
def _has_subject_directories(path: Path) -> tuple[bool, str]:
subject_dirs = [d for d in path.iterdir() if d.is_dir() and d.name.startswith("sub-")]
n = len(subject_dirs)
if n:
return True, f"{n} subject director{'y' if n == 1 else 'ies'} found"
return False, "No subject directories (sub-XX/) found — dataset may be empty"
def _has_participants_tsv(path: Path) -> tuple[bool, str]:
if (path / "participants.tsv").exists():
return True, "participants.tsv present"
return False, "participants.tsv not found (recommended for multi-subject datasets)"
def _has_readme(path: Path) -> tuple[bool, str]:
for candidate in ("README", "README.md", "README.txt", "README.rst"):
if (path / candidate).exists():
return True, f"{candidate} present"
return False, "No README file found (recommended)"
def _not_inside_derivatives(path: Path) -> tuple[bool, str]:
if "derivatives" in path.parts:
return False, "Path is inside a 'derivatives/' subtree — this is a derivative dataset"
return True, "Dataset is not nested inside a derivatives folder"
# ─── Pre-built rule instances ─────────────────────────────────────────────────
RULE_HAS_DESCRIPTION_JSON = BIDSRule(
name="has_description_json",
description="Directory must contain a dataset_description.json file",
severity=Severity.HARD,
check=_has_description_json,
)
RULE_DESCRIPTION_IS_VALID_JSON = BIDSRule(
name="description_json_is_valid",
description="dataset_description.json must be parseable as a JSON object",
severity=Severity.HARD,
check=_description_is_valid_json,
)
RULE_HAS_NAME_FIELD = BIDSRule(
name="has_name_field",
description="dataset_description.json must contain the required 'Name' field",
severity=Severity.SOFT,
check=_has_name_field,
)
RULE_HAS_BIDS_VERSION = BIDSRule(
name="has_bids_version",
description="dataset_description.json must contain the required 'BIDSVersion' field",
severity=Severity.SOFT,
check=_has_bids_version,
)
RULE_HAS_SUBJECT_DIRECTORIES = BIDSRule(
name="has_subject_directories",
description="Dataset root must contain at least one sub-XX/ directory",
severity=Severity.SOFT,
check=_has_subject_directories,
)
RULE_HAS_PARTICIPANTS_TSV = BIDSRule(
name="has_participants_tsv",
description="A participants.tsv file is recommended at the dataset root",
severity=Severity.SOFT,
check=_has_participants_tsv,
)
RULE_HAS_README = BIDSRule(
name="has_readme",
description="A README file is recommended at the dataset root",
severity=Severity.SOFT,
check=_has_readme,
)
RULE_NOT_INSIDE_DERIVATIVES = BIDSRule(
name="not_inside_derivatives",
description="Dataset should not be nested inside a derivatives/ folder",
severity=Severity.SOFT,
check=_not_inside_derivatives,
)
# ─── Pre-built rule sets ──────────────────────────────────────────────────────
HARD_RULESET = BIDSRuleSet(
rules=[
RULE_HAS_DESCRIPTION_JSON,
RULE_DESCRIPTION_IS_VALID_JSON,
],
name="BIDS Hard Rules",
)
SOFT_RULESET = BIDSRuleSet(
rules=[
RULE_HAS_NAME_FIELD,
RULE_HAS_BIDS_VERSION,
RULE_HAS_SUBJECT_DIRECTORIES,
RULE_HAS_PARTICIPANTS_TSV,
RULE_HAS_README,
RULE_NOT_INSIDE_DERIVATIVES,
],
name="BIDS Soft Rules",
)
# The default ruleset used by the discovery pipeline
DEFAULT_BIDS_RULESET = BIDSRuleSet(
rules=[*HARD_RULESET.rules, *SOFT_RULESET.rules],
name="Default BIDS Discovery Rules",
)