bidsschematools.schema

Schema loading- and processing-related functions.

Functions

bidsschematools.schema.dereference(namespace, inplace=True)

Replace references in namespace with the contents of the referred object.

Parameters:
  • namespace (Namespace) – Namespace for which to dereference.

  • inplace (bool, optional) – Whether to modify the namespace in place or create a copy, by default True.

Returns:

namespace – Dereferenced namespace

Return type:

Namespace

bidsschematools.schema.export_schema(schema)

Export the schema to JSON format.

Parameters:

schema (dict) – The schema object, in dictionary form.

Returns:

json – The schema serialized as a JSON string.

Return type:

str

bidsschematools.schema.filter_schema(schema, **kwargs)

Filter the schema based on a set of keyword arguments.

Parameters:
  • schema (dict) – The schema object, which is a dictionary with nested dictionaries and lists stored within it.

  • **kwargs (dict) – Keyword arguments used to filter the schema. Example kwargs that may be used include: “suffixes”, “datatypes”, “extensions”.

Returns:

new_schema – The filtered version of the schema.

Return type:

dict

Notes

This function calls itself recursively, in order to apply filters at arbitrary depth.

Warning

This function employs a very simple filter. It is very limited.

bidsschematools.schema.flatten_enums(namespace, inplace=True)

Replace enum collections with a single enum, merging enums contents.

The function helps reducing the complexity of the schema by assuming that the values in the conditions (anyOf) are mutually exclusive.

Parameters:

namespace (dict) – Schema in dictionary form to be flattened.

Returns:

schema – Schema with flattened enums.

Return type:

dict

Examples

>>> struct = {
...   "anyOf": [
...      {"type": "string", "enum": ["A", "B", "C"]},
...      {"type": "string", "enum": ["D", "E", "F"]},
...   ]
... }
>>> flatten_enums(struct)
{'type': 'string', 'enum': ['A', 'B', 'C', 'D', 'E', 'F']}
bidsschematools.schema.load_schema(schema_path=None)

Load the schema into a dictionary.

This function allows the schema, like BIDS itself, to be specified in a hierarchy of directories and files. Filenames (minus extensions) and directory names become keys in the associative array (dict) of entries composed from content of files and entire directories.

Parameters:

schema_path (str, optional) – Directory containing yaml files or yaml file. If None, use the default schema packaged with bidsschematools.

Returns:

Schema in dictionary form.

Return type:

dict

Notes

This function is cached, so it will only be called once per schema path.

bidsschematools.schema.validate_schema(schema: Namespace)

Validate a schema against the BIDS metaschema.

Exceptions

exception bidsschematools.schema.BIDSSchemaError

Errors indicating invalid values in the schema itself