Skip to content

Commit

Permalink
Merge branch 'main' into list-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Feb 5, 2025
2 parents 0441113 + 7650e19 commit 6ede27a
Show file tree
Hide file tree
Showing 8 changed files with 17,191 additions and 32 deletions.
49 changes: 48 additions & 1 deletion cubids/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def _parse_validate():
allow_abbrev=False,
)
PathExists = partial(_path_exists, parser=parser)
IsFile = partial(_is_file, parser=parser)

parser.add_argument(
"bids_dir",
Expand Down Expand Up @@ -177,6 +178,17 @@ def _parse_validate():
help="Lets user run a locally installed BIDS validator. Default is set to False ",
required=False,
)
parser.add_argument(
"--schema",
type=IsFile,
action="store",
default=None,
help=(
"Path to a BIDS schema JSON file. "
"Default is None, which uses the latest schema available to the validator."
),
required=False,
)
return parser


Expand Down Expand Up @@ -243,6 +255,7 @@ def _parse_bids_version():
allow_abbrev=False,
)
PathExists = partial(_path_exists, parser=parser)
IsFile = partial(_is_file, parser=parser)

parser.add_argument(
"bids_dir",
Expand All @@ -263,6 +276,17 @@ def _parse_bids_version():
"By default, `cubids bids-version /bids/path` prints to the terminal."
),
)
parser.add_argument(
"--schema",
type=IsFile,
action="store",
default=None,
help=(
"Path to a BIDS schema JSON file. "
"Default is None, which uses the latest schema available to the validator."
),
required=False,
)
return parser


Expand Down Expand Up @@ -412,6 +436,7 @@ def _parse_group():
allow_abbrev=False,
)
PathExists = partial(_path_exists, parser=parser)
IsFile = partial(_is_file, parser=parser)

parser.add_argument(
"bids_dir",
Expand Down Expand Up @@ -446,13 +471,24 @@ def _parse_group():
parser.add_argument(
"--config",
action="store",
type=PathExists,
type=IsFile,
default=None,
help=(
"Path to a config file for grouping. "
"If not provided, then the default config file from CuBIDS will be used."
),
)
parser.add_argument(
"--schema",
type=IsFile,
action="store",
default=None,
help=(
"Path to a BIDS schema JSON file. "
"Default is None, which uses the latest schema available to the validator."
),
required=False,
)
return parser


Expand Down Expand Up @@ -601,6 +637,17 @@ def _parse_apply():
"If not provided, then the default config file from CuBIDS will be used."
),
)
parser.add_argument(
"--schema",
type=IsFile,
action="store",
default=None,
help=(
"Path to a BIDS schema JSON file. "
"Default is None, which uses the latest schema available to the validator."
),
required=False,
)

return parser

Expand Down
29 changes: 29 additions & 0 deletions cubids/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,32 @@ def load_config(config_file):
config = yaml.safe_load(f)

return config


def load_schema(schema_file):
"""Load a JSON file containing the BIDS schema.
Parameters
----------
schema_file : str or pathlib.Path, optional
The path to the schema file. If None, the default schema file is used.
Returns
-------
dict
The schema loaded from the YAML file.
"""
import json

if schema_file is None:
schema_file = Path(importlib.resources.files("cubids") / "data/schema.json")

with schema_file.open() as f:
schema = json.load(f)

print(
f"Loading BIDS schema version: {schema['schema_version']}. "
f"BIDS version: {schema['bids_version']}"
)

return schema
12 changes: 10 additions & 2 deletions cubids/cubids.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from tqdm import tqdm

from cubids import utils
from cubids.config import load_config
from cubids.config import load_config, load_schema
from cubids.constants import NON_KEY_ENTITIES
from cubids.metadata_merge import check_merging_operations, group_by_acquisition_sets

Expand All @@ -51,6 +51,9 @@ class CuBIDS(object):
force_unlock : :obj:`bool`, optional
If True, force unlock all files in the BIDS dataset.
Default is False.
schema_json : :obj:`str`, optional
Path to a BIDS schema JSON file.
Default is None, in which case the default schema in CuBIDS is used.
Attributes
----------
Expand Down Expand Up @@ -88,6 +91,8 @@ class CuBIDS(object):
A data dictionary for TSV outputs.
use_datalad : :obj:`bool`
If True, use datalad to track changes to the BIDS dataset.
schema : :obj:`dict`
The BIDS schema dictionary.
is_longitudinal : :obj:`bool`
If True, adds "ses" in filepath.
"""
Expand All @@ -99,6 +104,7 @@ def __init__(
acq_group_level="subject",
grouping_config=None,
force_unlock=False,
schema_json=None,
):
self.path = os.path.abspath(data_root)
self._layout = None
Expand All @@ -116,6 +122,7 @@ def __init__(
self.cubids_code_dir = Path(self.path + "/code/CuBIDS").is_dir()
self.data_dict = {} # data dictionary for TSV outputs
self.use_datalad = use_datalad # True if flag set, False if flag unset
self.schema = load_schema(schema_json)
self.is_longitudinal = self._infer_longitudinal() # inferred from dataset structure

if self.use_datalad:
Expand Down Expand Up @@ -546,8 +553,9 @@ def change_filename(self, filepath, entities):
"""
new_path = utils.build_path(
filepath=filepath,
entities=entities,
out_entities=entities,
out_dir=str(self.path),
schema=self.schema,
is_longitudinal=self.is_longitudinal,
)

Expand Down
Loading

0 comments on commit 6ede27a

Please sign in to comment.