BIDS Schema Tools
bidsschematools is a Python library providing utilities for programmatic interaction with the Brain Imaging Data Structure (BIDS) schema. It enables loading, parsing, and validating BIDS datasets against the official specification. The current version is 1.2.2, with releases typically coinciding with major BIDS specification updates or essential bug fixes and enhancements.
Common errors
-
AttributeError: module 'bidsschematools.schema' has no attribute 'load_schema'
cause Attempting to use the `load_schema()` function which was removed in `bidsschematools` v1.2.0.fixUse `from bidsschematools import schema; bids_schema = schema.BIDS_SCHEMA.from_path('/path/to/schema')` instead. -
ModuleNotFoundError: No module named 'bidsschematools.schema.rules'
cause Trying to import a module (`rules` or `objects`) that was removed in `bidsschematools` v1.1.0.fixAccess schema properties directly from the main schema object, e.g., `bids_schema.rules` or `bids_schema.entities` after loading the schema. -
jsonschema.exceptions.ValidationError: '<value>' is not one of [<expected_values>]
cause This is a generic JSON schema validation error indicating that a value in your BIDS metadata or filename does not conform to the expected values or format defined in the BIDS schema.fixReview the BIDS specification for the field or entity causing the error and adjust your data to match the required format or allowed values. The error message usually provides specific context on what was expected.
Warnings
- breaking The `bidsschematools.schema.load_schema()` function was removed and replaced by `BIDS_SCHEMA.from_path()` for loading custom or specific schema versions.
- breaking The modules `bidsschematools.schema.rules` (previously `rules_schema`) and `bidsschematools.schema.objects` were removed as part of internal refactoring to simplify schema access.
- gotcha The library, by default, loads the latest stable version of the BIDS specification available at the time of its own release. If you need to validate against an older or a custom BIDS specification, you must explicitly load it using `BIDS_SCHEMA.from_path()`.
Install
-
pip install bidsschematools
Imports
- BIDS_SCHEMA
from bidsschematools.schema import BIDS_SCHEMA
from bidsschematools import schema bids_schema = schema.BIDS_SCHEMA
- load_schema
from bidsschematools.schema import load_schema
from bidsschematools import schema bids_schema = schema.BIDS_SCHEMA.from_path('/path/to/schema')
Quickstart
from bidsschematools import schema
import pprint
# Load the default, current BIDS schema
bids_schema = schema.BIDS_SCHEMA
print(f"BIDS Schema Version: {bids_schema.version}")
print(f"Number of entities in schema: {len(bids_schema.entities)}")
# Accessing a specific entity definition
entity_sub = bids_schema.entities.get("subject")
if entity_sub:
print("\n'subject' entity definition:")
pprint.pprint(entity_sub)
# Example: Check if a filename pattern is valid according to schema rules
# Note: Full file validation requires a dataset context.
filename_pattern = "sub-<label>[_ses-<label>][_task-<label>]_bold.nii.gz"
is_valid_pattern = bids_schema.validate_pattern_string(filename_pattern)
print(f"\nIs example filename pattern valid according to schema? {is_valid_pattern}")
# To load a specific schema from a path:
# custom_schema = schema.BIDS_SCHEMA.from_path("path/to/your/custom_bids_schema_directory")