|
| 1 | +--- |
| 2 | +jupytext: |
| 3 | + formats: md:myst |
| 4 | + text_representation: |
| 5 | + extension: .md |
| 6 | + format_name: myst |
| 7 | + format_version: 0.13 |
| 8 | + jupytext_version: 1.17.2 |
| 9 | +kernelspec: |
| 10 | + name: python3 |
| 11 | + display_name: Python 3 (ipykernel) |
| 12 | + language: python |
| 13 | +--- |
| 14 | + |
| 15 | ++++ {"editable": true, "slideshow": {"slide_type": "slide"}} |
| 16 | + |
| 17 | +# BIDS Schema Tools Quick Start |
| 18 | + |
| 19 | +The `bidsschematools` package is a Python package that is bundled with the [BIDS specification](https://github.com/bids-standard/bids-specification/) in order to render the components of the specification document from the BIDS schema. |
| 20 | + |
| 21 | +```{code-cell} ipython3 |
| 22 | +--- |
| 23 | +editable: true |
| 24 | +slideshow: |
| 25 | + slide_type: '' |
| 26 | +--- |
| 27 | +import bidsschematools as bst |
| 28 | +import bidsschematools.schema |
| 29 | +``` |
| 30 | + |
| 31 | +```{code-cell} ipython3 |
| 32 | +--- |
| 33 | +editable: true |
| 34 | +slideshow: |
| 35 | + slide_type: skip |
| 36 | +tags: [hide-cell, remove-input] |
| 37 | +--- |
| 38 | +# Hidden cell to import useful tools |
| 39 | +import os |
| 40 | +from upath import UPath |
| 41 | +from pprint import pprint |
| 42 | +``` |
| 43 | + |
| 44 | ++++ {"editable": true, "slideshow": {"slide_type": "slide"}} |
| 45 | + |
| 46 | +## Schema loading |
| 47 | + |
| 48 | +Schemas are loaded with the `load_schema()` function. The default schema is the one that is bundled as part of the package: |
| 49 | + |
| 50 | +```{code-cell} ipython3 |
| 51 | +--- |
| 52 | +editable: true |
| 53 | +slideshow: |
| 54 | + slide_type: '' |
| 55 | +--- |
| 56 | +schema = bst.schema.load_schema() |
| 57 | +``` |
| 58 | + |
| 59 | ++++ {"editable": true, "slideshow": {"slide_type": "subslide"}} |
| 60 | + |
| 61 | +In the BIDS repository, the schema source is a subdirectory of YAML documents that need to be compiled before they can be used. Passing such a directory into `load_schema()` will perform the compilation: |
| 62 | + |
| 63 | +```{code-cell} ipython3 |
| 64 | +--- |
| 65 | +editable: true |
| 66 | +slideshow: |
| 67 | + slide_type: '' |
| 68 | +--- |
| 69 | +# Build from local repository, or else set `BIDS_SPEC_REPO` environment variable |
| 70 | +spec = UPath(os.getenv('BIDS_SPEC_REPO', UPath(os.getcwd()).parent.parent.parent)) |
| 71 | +schema_from_directory = bst.schema.load_schema(spec / 'src' / 'schema') |
| 72 | +``` |
| 73 | + |
| 74 | ++++ {"editable": true, "slideshow": {"slide_type": "subslide"}} |
| 75 | + |
| 76 | +`load_schema()` can also load a precompiled JSON schema document, such as can be found at <https://bids-specification.readthedocs.io/en/latest/schema.json>. |
| 77 | + |
| 78 | +```{code-cell} ipython3 |
| 79 | +--- |
| 80 | +editable: true |
| 81 | +slideshow: |
| 82 | + slide_type: '' |
| 83 | +--- |
| 84 | +schema_path = UPath('https://bids-specification.readthedocs.io/en/latest/schema.json') |
| 85 | +schema_from_json = bst.schema.load_schema(schema_path) |
| 86 | +``` |
| 87 | + |
| 88 | ++++ {"editable": true, "slideshow": {"slide_type": "slide"}} |
| 89 | + |
| 90 | +## Schema organization |
| 91 | + |
| 92 | +The schema has three top-level subdivisions (`objects`, `rules`, and `meta`) and also contains its own version and the version of the BIDS standard that it encodes: |
| 93 | + |
| 94 | +```{code-cell} ipython3 |
| 95 | +--- |
| 96 | +editable: true |
| 97 | +slideshow: |
| 98 | + slide_type: '' |
| 99 | +--- |
| 100 | +print(list(schema.keys())) |
| 101 | +print(f'BIDS-Schema version: {schema.schema_version}') |
| 102 | +print(f'BIDS version: {schema.bids_version}') |
| 103 | +``` |
| 104 | + |
| 105 | ++++ {"editable": true, "slideshow": {"slide_type": ""}} |
| 106 | + |
| 107 | +Note that the schema provided by `load_schema()` is a [Namespace](#bidsschematools.types.namespace.Namespace) object, which is able to access fields with dot notation (`schema.bids_version`) as well as index notation (`schema['bids_version'])`. |
| 108 | + |
| 109 | ++++ {"editable": true, "slideshow": {"slide_type": "subslide"}} |
| 110 | + |
| 111 | +We can see the general structure of the schema by listing keys at the second level: |
| 112 | + |
| 113 | +```{code-cell} ipython3 |
| 114 | +--- |
| 115 | +editable: true |
| 116 | +slideshow: |
| 117 | + slide_type: '' |
| 118 | +--- |
| 119 | +list(schema.keys(level=2)) |
| 120 | +``` |
| 121 | + |
| 122 | +```{code-cell} ipython3 |
| 123 | +schema['meta.associations'] |
| 124 | +``` |
| 125 | + |
| 126 | ++++ {"editable": true, "slideshow": {"slide_type": "fragment"}} |
| 127 | + |
| 128 | +The `objects` subschema contains definitions of data and metadata used throughout BIDS, while the `rules` subschema contains definitions of constraints that may be validated. `meta` contains definitions that are essential for validation. |
| 129 | + |
| 130 | ++++ {"editable": true, "slideshow": {"slide_type": "subslide"}} |
| 131 | + |
| 132 | +To see how these sections interact, consider `rules.files.raw.func.func`, which contains the valid entities, datatypes, extensions and suffixes for functional neuroimaging files: |
| 133 | + |
| 134 | +```{code-cell} ipython3 |
| 135 | +--- |
| 136 | +editable: true |
| 137 | +slideshow: |
| 138 | + slide_type: '' |
| 139 | +--- |
| 140 | +pprint(schema.rules.files.raw.func.func.to_dict()) |
| 141 | +``` |
| 142 | + |
| 143 | ++++ {"editable": true, "slideshow": {"slide_type": ""}} |
| 144 | + |
| 145 | +We can look up the suffix definitions in `objects.suffixes`: |
| 146 | + |
| 147 | +```{code-cell} ipython3 |
| 148 | +--- |
| 149 | +editable: true |
| 150 | +slideshow: |
| 151 | + slide_type: '' |
| 152 | +--- |
| 153 | +pprint({ |
| 154 | + suffix: schema.objects.suffixes[suffix].to_dict() |
| 155 | + for suffix in schema.rules.files.raw.func.func.suffixes |
| 156 | +}) |
| 157 | +``` |
| 158 | + |
| 159 | ++++ {"editable": true, "slideshow": {"slide_type": ""}} |
| 160 | + |
| 161 | +Note that suffixes are duplicated in the `value` field. Because suffixes are valid identifiers, this generally does not cause problems, and `objects.suffixes[suffix]` is a shorthand lookup. |
| 162 | + |
| 163 | ++++ {"editable": true, "slideshow": {"slide_type": "subslide"}} |
| 164 | + |
| 165 | +This isn't true in all fields, for example, in extensions: |
| 166 | + |
| 167 | +```{code-cell} ipython3 |
| 168 | +--- |
| 169 | +editable: true |
| 170 | +slideshow: |
| 171 | + slide_type: '' |
| 172 | +--- |
| 173 | +schema.objects.extensions.nii_gz |
| 174 | +``` |
| 175 | + |
| 176 | ++++ {"editable": true, "slideshow": {"slide_type": ""}} |
| 177 | + |
| 178 | +In this case, a lookup table needs to be built on the fly: |
| 179 | + |
| 180 | +```{code-cell} ipython3 |
| 181 | +--- |
| 182 | +editable: true |
| 183 | +slideshow: |
| 184 | + slide_type: '' |
| 185 | +--- |
| 186 | +ext_lookup = {ext.value: ext for ext in schema.objects.extensions.values()} |
| 187 | +pprint({ |
| 188 | + ext: ext_lookup[ext].to_dict() |
| 189 | + for ext in schema.rules.files.raw.func.func.extensions |
| 190 | +}) |
| 191 | +``` |
0 commit comments