Skip to content

Commit ee98367

Browse files
committed
add script to detect deprecated bids URI in intended for
1 parent 1c30c6e commit ee98367

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ env/
1212

1313
*.asv
1414

15-
site/
15+
site/
16+
17+
deprecated_intended_for.log
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Name": "Brainvisa dertivatives",
3+
"DatasetType": "derivative",
4+
"BIDSVersion": "1.7.0"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Name": "Brainvisa dertivatives",
3+
"DatasetType": "derivative",
4+
"BIDSVersion": "1.7.0"
5+
}

tools/check_intended_for.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Check jsons of all datasets for IntendedFor field.
2+
3+
Make sure that the format is not one of the deprecated ones.
4+
5+
https://bids-specification.readthedocs.io/en/latest/common-principles.html#bids-uri
6+
7+
Example:
8+
9+
- bad
10+
11+
.. code-block:: json
12+
13+
"IntendedFor": ["anat/sub-01_T1w.nii.gz"]
14+
15+
16+
- good
17+
18+
.. code-block:: json
19+
20+
"IntendedFor": ["bids::sub-01/anat/sub-01_T1w.nii.gz"]
21+
"""
22+
23+
from pathlib import Path
24+
import json
25+
from warnings import warn
26+
from rich import print
27+
28+
VERBOSE = False
29+
30+
root_dir = Path(__file__).parent.parent
31+
32+
deprecated_formats = {}
33+
34+
for json_path in root_dir.glob("**/*.json"):
35+
36+
if VERBOSE:
37+
print(f"Checking {json_path.relative_to(root_dir)}")
38+
39+
with open(json_path) as f:
40+
content = json.load(f)
41+
42+
if "IntendedFor" in content:
43+
intended_for = content["IntendedFor"]
44+
if isinstance(intended_for, str):
45+
intended_for = [intended_for]
46+
47+
for intended_for_path in intended_for:
48+
if not intended_for_path.startswith("bids"):
49+
if json_path not in deprecated_formats:
50+
deprecated_formats[json_path] = []
51+
deprecated_formats[json_path].append(intended_for_path)
52+
53+
if deprecated_formats:
54+
55+
log_file = root_dir / "deprecated_intended_for.log"
56+
57+
with open(log_file, "w") as f:
58+
for json_path, deprecated_paths in deprecated_formats.items():
59+
f.write(f"{json_path.relative_to(root_dir)}\n")
60+
print(f"{json_path.relative_to(root_dir)}")
61+
for deprecated_path in deprecated_paths:
62+
f.write(f" {deprecated_path}\n")
63+
print(f" {deprecated_path}")
64+
65+
raise(ValueError)(
66+
f"Found {len(deprecated_formats)} jsons with deprecated IntendedFor formats.\n"
67+
f"See {log_file}\n"
68+
"Please update them to the new format.\n"
69+
"See https://bids-specification.readthedocs.io/en/latest/common-principles.html#bids-uri"
70+
)
71+

tools/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pybids
22
pandas
3-
tabulate
3+
tabulate
4+
rich

0 commit comments

Comments
 (0)