Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAINT] check format 'IntendedFor' fields in json files #408

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add script to detect deprecated bids URI in intended for
  • Loading branch information
Remi-Gau committed Sep 2, 2023
commit ee9836790139eec41b98a42deb3a1a46ffc856bd
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ env/

*.asv

site/
site/

deprecated_intended_for.log
5 changes: 5 additions & 0 deletions ieeg_epilepsy/derivatives/brainvisa/dataset_description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "Brainvisa dertivatives",
"DatasetType": "derivative",
"BIDSVersion": "1.7.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "Brainvisa dertivatives",
"DatasetType": "derivative",
"BIDSVersion": "1.7.0"
}
71 changes: 71 additions & 0 deletions tools/check_intended_for.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Check jsons of all datasets for IntendedFor field.

Make sure that the format is not one of the deprecated ones.

https://bids-specification.readthedocs.io/en/latest/common-principles.html#bids-uri

Example:

- bad

.. code-block:: json

"IntendedFor": ["anat/sub-01_T1w.nii.gz"]


- good

.. code-block:: json

"IntendedFor": ["bids::sub-01/anat/sub-01_T1w.nii.gz"]
"""

from pathlib import Path
import json
from warnings import warn
from rich import print

VERBOSE = False

root_dir = Path(__file__).parent.parent

deprecated_formats = {}

for json_path in root_dir.glob("**/*.json"):

if VERBOSE:
print(f"Checking {json_path.relative_to(root_dir)}")

with open(json_path) as f:
content = json.load(f)

if "IntendedFor" in content:
intended_for = content["IntendedFor"]
if isinstance(intended_for, str):
intended_for = [intended_for]

for intended_for_path in intended_for:
if not intended_for_path.startswith("bids"):
if json_path not in deprecated_formats:
deprecated_formats[json_path] = []
deprecated_formats[json_path].append(intended_for_path)

if deprecated_formats:

log_file = root_dir / "deprecated_intended_for.log"

with open(log_file, "w") as f:
for json_path, deprecated_paths in deprecated_formats.items():
f.write(f"{json_path.relative_to(root_dir)}\n")
print(f"{json_path.relative_to(root_dir)}")
for deprecated_path in deprecated_paths:
f.write(f" {deprecated_path}\n")
print(f" {deprecated_path}")

raise(ValueError)(
f"Found {len(deprecated_formats)} jsons with deprecated IntendedFor formats.\n"
f"See {log_file}\n"
"Please update them to the new format.\n"
"See https://bids-specification.readthedocs.io/en/latest/common-principles.html#bids-uri"
)

3 changes: 2 additions & 1 deletion tools/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pybids
pandas
tabulate
tabulate
rich