Skip to content

Commit bfba949

Browse files
bendhousearteffigiesjulia-pfarr
authored
[SCHEMA][MISC][WIP] Update schema docs with BEP Google doc to schema examples. (#2178)
* start on bep to schema, add make commands for building schema docs * started back up on docs, using BEP 009 as example * push for Yahya * Apply suggestions from code review Co-authored-by: Julia-Katharina Pfarr <[email protected]> * doc: Expand file name section * Explain discrepancy between doc and schema * fix: Use explicit reference to link to description section * Update tools/schemacode/docs/doc_to_schema.md Co-authored-by: Julia-Katharina Pfarr <[email protected]> * fix: Resolve issues link * sty: prettier * Update table --------- Co-authored-by: Chris Markiewicz <[email protected]> Co-authored-by: Julia-Katharina Pfarr <[email protected]>
1 parent 870afc6 commit bfba949

File tree

9 files changed

+294
-1
lines changed

9 files changed

+294
-1
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ commitschema:
3333
formatschema: runprettier commitschema
3434

3535
.PHONY: runprettier commitschema
36+
37+
schemacodedocs_clean:
38+
uv run --group=doc sphinx-build -M clean tools/schemacode/docs tools/schemacode/docs/_build
39+
40+
schemacodedocs_build: schemacodedocs_clean
41+
uv run --group=doc sphinx-build -M html tools/schemacode/docs tools/schemacode/docs/_build
42+
43+
schemacodedocs_serve: schemacodedocs_build
44+
uv run python -m http.server -d tools/schemacode/docs/_build

src/schema/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ as part of a larger rule.
650650
In these cases, the `level` field should be omitted from the issue
651651
to avoid duplication or conflict.
652652

653+
(filename-construction-rules)=
653654
### Filename construction rules
654655

655656
A significant portion of BIDS is devoted to the naming of files,
@@ -985,7 +986,7 @@ They additionally have a `checks` list, and an explicit issue.
985986

986987
| Field | Description |
987988
| ----------- | ---------------------------------------------------------------------------------------------- |
988-
| `issue` | Issue code object (see [Issues](#issues) |
989+
| `issue` | Issue code object (see [Issues](#issues)) |
989990
| `selectors` | List of expressions; any evaluating false indicate rule does not apply |
990991
| `checks` | List of expressions; any evaluating false indicate rule is violated and issue should be raised |
991992

45.7 KB
Loading
77.2 KB
Loading
533 KB
Loading
488 KB
Loading

tools/schemacode/docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,5 @@
7777
"light_logo": "black_logo.svg",
7878
"dark_logo": "white_logo.svg",
7979
}
80+
81+
myst_heading_anchors = 4
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
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+
# Translating a BEP Document into Schema Code
18+
19+
One of the most important steps of a BEP is translating it from a plain text (google/microsoft document) as
20+
a yaml schema. The yaml schema necessary for the following steps:
21+
22+
1. render the spec into its [ultimate compiled form](https://bids-specification.readthedocs.io/en/stable/)
23+
2. apply and validate the new rules and requirements introduced by the BEP
24+
3. merge their BEP into the BIDS Spec
25+
26+
For the purpose of this guide we'll be referencing a completed BEP starting from its initial Google doc form
27+
and finishing with its translation into schema.
28+
29+
## The Google Doc
30+
31+
![Title Page of BEP 009](_static/pet_google_doc_first_page.png)
32+
33+
### Defining new file names
34+
35+
BEP 009 introduced files of the form
36+
37+
```
38+
sub-<label>/[ses-<label>/]pet/sub-<label>[_ses-<label>][_task-<label>][_trc-<label>][_rec-<label>][_run-<index>]_pet.nii[.gz]
39+
```
40+
41+
This path consists of the following objects, which are defined in the schema:
42+
43+
| Object type | Description | Example | Defined in | Introduced by BEP009 |
44+
| ----------- | --------------------------------------- | ------------- | --------------------------------------------------- | -------------------- |
45+
| entity | key-value pairs | `sub-<label>` | [`schema/objects/entities.yaml`][entities.yaml] | `trc-<label>` |
46+
| data type | directory describing data | `pet/` | [`schema/objects/datatypes.yaml`][datatypes.yaml] | `pet/` |
47+
| suffix | end of the file stem (before extension) | `_pet` | [`schema/objects/suffixes.yaml`][suffixes.yaml] | `_pet` |
48+
| extension | file type indicator | `.nii.gz` | [`schema/objects/extensions.yaml`][extensions.yaml] | none |
49+
50+
BIDS also has the concept of a "modality", which does not always appear in the path,
51+
but is determined by the data type.
52+
53+
| Object type | Description | Example | Defined in | Introduced by BEP009 |
54+
| ----------- | --------------------------- | ------- | -------------------------------------------------- | -------------------- |
55+
| modality | primary recording technique | `pet` | [`schema/objects/modalities.yaml`][datatypes.yaml] | `pet` |
56+
57+
#### Datatypes and modalities
58+
59+
PET introduces a new datatype (`pet/`) and modality (`pet`).
60+
For new modalities the first step should be to add that modality to [modalities.yaml][].
61+
62+
```diff
63+
# src/schema/objects/modalities.yaml
64+
---
65+
# This file describes modalities supported by BIDS.
66+
mri:
67+
display_name: Magnetic Resonance Imaging
68+
description: |
69+
Data acquired with an MRI scanner.
70+
[...]
71+
+pet:
72+
+ display_name: Positron Emission Tomography
73+
+ description: |
74+
+ Data acquired with PET.
75+
[...]
76+
mrs:
77+
display_name: Magnetic Resonance Spectroscopy
78+
description: Data acquired with MRS.
79+
```
80+
81+
Likewise, update [datatypes.yaml][]:
82+
83+
```diff
84+
# src/schema/objects/datatypes.yaml
85+
---
86+
# This file defines valid BIDS datatypes.
87+
anat:
88+
value: anat
89+
display_name: Anatomical Magnetic Resonance Imaging
90+
description: |
91+
Magnetic resonance imaging sequences designed to characterize static, anatomical features.
92+
[...]
93+
+pet:
94+
+ value: pet
95+
+ display_name: Positron Emission Tomography
96+
+ description: |
97+
+ Positron emission tomography data
98+
[...]
99+
nirs:
100+
value: nirs
101+
display_name: Near-Infrared Spectroscopy
102+
description: Near-Infrared Spectroscopy data organized around the SNIRF format
103+
```
104+
105+
Here, the `value` field indicates the string that will appear in the file name.
106+
107+
Finally, `rules/modalities.yaml` must be updated to associate the new
108+
datatype to the new modality:
109+
110+
```diff
111+
# src/schema/rules/modalities.yaml
112+
---
113+
mri:
114+
datatypes:
115+
- anat
116+
- dwi
117+
- fmap
118+
- func
119+
[...]
120+
+pet:
121+
+ datatypes:
122+
+ - pet
123+
[...]
124+
mrs:
125+
datatypes:
126+
- mrs
127+
```
128+
129+
#### Suffixes
130+
131+
To add a suffix, update [suffixes.yaml][]:
132+
133+
```diff
134+
---
135+
# This file defines valid BIDS file suffixes.
136+
# For rules regarding how suffixes relate to datatypes, see files in `rules/files/raw/`.
137+
TwoPE:
138+
value: 2PE
139+
display_name: 2-photon excitation microscopy
140+
description: |
141+
2-photon excitation microscopy imaging data
142+
[...]
143+
+pet:
144+
+ value: pet
145+
+ display_name: Positron Emission Tomography
146+
+ description: |
147+
+ PET imaging data SHOULD be stored in 4D
148+
+ (or 3D, if only one volume was acquired) NIfTI files with the `_pet` suffix.
149+
+ Volumes MUST be stored in chronological order
150+
+ (the order they were acquired in).
151+
[...]
152+
unloc:
153+
value: unloc
154+
display_name: Unlocalized spectroscopy
155+
description: |
156+
MRS acquisitions run without localization.
157+
This includes signals detected using coil sensitivity only.
158+
```
159+
160+
#### Entities
161+
162+
To add an entity, [entities.yaml] must be updated.
163+
In this case, PET introduced a `trc-<label>` entity, short for "tracer":
164+
165+
```diff
166+
---
167+
# This file describes entities present in BIDS filenames.
168+
acquisition:
169+
name: acq
170+
display_name: Acquisition
171+
description: ...
172+
type: string
173+
format: label
174+
[...]
175+
+tracer:
176+
+ name: trc
177+
+ display_name: Tracer
178+
+ description: |
179+
+ The `trc-<label>` entity can be used to distinguish sequences using different tracers.
180+
+
181+
+ This entity represents the `"TracerName"` metadata field.
182+
+ Therefore, if the `trc-<label>` entity is present in a filename,
183+
+ `"TracerName"` MUST be defined in the associated metadata.
184+
+ Please note that the `<label>` does not need to match the actual value of the field.
185+
+ type: string
186+
+ format: label
187+
[...]
188+
tracksys:
189+
name: tracksys
190+
display_name: Tracking System
191+
description: ...
192+
type: string
193+
format: label
194+
```
195+
196+
Note that the `name` is what shows up in filenames before a dash (`-`).
197+
This is often a shortened version of the reference name, in this case, `tracer`.
198+
The `display_name` is a name that might be used when creating a document
199+
or entry form for presenting or requesting the entity value.
200+
`type` and `format` indicate the range of acceptable values.
201+
For entities, `type` is always `string`,
202+
and `format` may be `label` (alphanumeric) or `index` (numeric).
203+
204+
#### Extensions
205+
206+
It is rare, but adding a new extension entails updating [extensions.yaml][].
207+
208+
```yaml
209+
nii:
210+
value: .nii
211+
display_name: NIfTI
212+
description: |
213+
A Neuroimaging Informatics Technology Initiative (NIfTI) data file.
214+
nii_gz:
215+
value: .nii.gz
216+
display_name: Compressed NIfTI
217+
description: |
218+
A compressed Neuroimaging Informatics Technology Initiative (NIfTI) data file.
219+
```
220+
221+
As with datatype and suffix, the string that appears in the file path is in the `value` field.
222+
223+
#### File naming rules
224+
225+
Your next steps are to move from this template file name as described in your document to formally defining those entities within the schema.
226+
The definitions for these file entities are located within `src/schema/rules/files`.
227+
Since this BEP is referring to raw data we go one level further to `src/schema/rules/files/raw` and create a new `pet.yaml` file to record each entity/rule.
228+
There are additional directories for common elements (`common`) and derivative files (`derivatives/`), but we're only focusing on raw PET data for this example.
229+
230+
```console
231+
$ tree src/schema/rules/files -L 1
232+
src/schema/rules/files
233+
├── common
234+
├── deriv
235+
└── raw
236+
```
237+
238+
![PET file entity schematic](_static/pet_file_entity_schematic.png)
239+
240+
Given the template above we want to mark all entities surrounded by `[]` as optional while everything else not contained in `[]` will be marked as required.
241+
(The `acquisition` entity was dropped in favor of `tracer` after the Google Doc was closed to edits, before the final inclusion of BEP 009.)
242+
243+
So, we create that `pet.yaml` file and begin to populate it.
244+
245+
```yaml
246+
# src/schema/rules/files/pet.yaml
247+
---
248+
pet:
249+
suffixes:
250+
- pet
251+
extensions:
252+
- .nii.gz
253+
- .nii
254+
- .json
255+
datatypes:
256+
- pet
257+
entities:
258+
subject: required
259+
session: optional
260+
task: optional
261+
tracer: optional
262+
reconstruction: optional
263+
run: optional
264+
```
265+
266+
In rare cases, new files may not have any entities.
267+
See [Filename construction rules](#filename-construction-rules) for more details.
268+
269+
### Sidecars and Metadata Rules
270+
271+
![PET side car table](_static/pet_side_car_table.png)
272+
273+
![PET file Enum object](_static/pet_enum_table_entry.png)
274+
275+
[modalities.yaml]: https://github.com/bids-standard/bids-specification/blob/master/src/schema/objects/modalities.yaml
276+
[entities.yaml]: https://github.com/bids-standard/bids-specification/blob/master/src/schema/objects/entities.yaml
277+
[datatypes.yaml]: https://github.com/bids-standard/bids-specification/blob/master/src/schema/objects/datatypes.yaml
278+
[suffixes.yaml]: https://github.com/bids-standard/bids-specification/blob/master/src/schema/objects/suffixes.yaml
279+
[extensions.yaml]: https://github.com/bids-standard/bids-specification/blob/master/src/schema/objects/extensions.yaml

tools/schemacode/docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Contents
88
:caption: Tutorial
99

1010
quickstart
11+
doc_to_schema
12+
1113

1214
.. toctree::
1315
:caption: Reference

0 commit comments

Comments
 (0)