Skip to content

Commit 19e6c4a

Browse files
authored
[ENH] Add macro to render descriptions (#1495)
1 parent e58df85 commit 19e6c4a

File tree

10 files changed

+112
-104
lines changed

10 files changed

+112
-104
lines changed

macros_doc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ All the macros we use are in listed in this
9292
| make_suffix_table | Generate a markdown table of suffix information. | Yes | [link](https://github.com/bids-standard/bids-specification/blob/9201b203ffaa72d83b2fa30d1c61f46f089f77de/src/04-modality-specific-files/01-magnetic-resonance-imaging-data.md?plain=1#L199) |
9393
| define_common_principles | List the common principles and definitions. | Yes | [link](https://github.com/bids-standard/bids-specification/blob/831ee55577b91aaa110153e9269e7829b095fb6f/src/02-common-principles.md?plain=1#L12) |
9494
| define_allowed_top_directories | Create a list of allowed top-level directories with their descriptions. | Yes | [link](https://github.com/bids-standard/bids-specification/blob/2a701fd034d51c25e8fe18ba67bb7b76621ba477/src/02-common-principles.md?plain=1#L124) |
95+
| render_description | Renders the description of an object in the schema. | Yes | [link] (???) |
9596

9697

9798
## When should I use a macro?

src/modality-agnostic-files.md

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ Templates:
1111

1212
### `dataset_description.json`
1313

14-
The file `dataset_description.json` is a JSON file describing the dataset.
14+
<!-- This block generates a description.
15+
A guide for using macros can be found at
16+
https://github.com/bids-standard/bids-specification/blob/master/macros_doc.md
17+
-->
18+
{{ MACROS___render_text("objects.files.dataset_description.description") }}
19+
1520
Every dataset MUST include this file with the following fields:
1621

1722
<!-- This block generates a metadata table.
@@ -156,27 +161,19 @@ Example:
156161

157162
### `README`
158163

159-
A REQUIRED text file, `README`, SHOULD describe the dataset in more detail.
160-
The `README` file MUST be either in ASCII or UTF-8 encoding and MAY have one of the extensions:
161-
`.md` ([Markdown](https://www.markdownguide.org/)),
162-
`.rst` ([reStructuredText](https://docutils.sourceforge.io/rst.html)),
163-
or `.txt`.
164-
A BIDS dataset MUST NOT contain more than one `README` file (with or without extension)
165-
at its root directory.
166-
BIDS does not make any recommendations with regards to the
167-
[Markdown flavor](https://www.markdownguide.org/extended-syntax/#lightweight-markup-languages)
168-
and does not validate the syntax of Markdown and reStructuredText.
169-
The `README` file SHOULD be structured such that its contents can be easily understood
170-
even if the used format is not rendered.
171-
A guideline for creating a good `README` file can be found in the
172-
[bids-starter-kit](https://github.com/bids-standard/bids-starter-kit/tree/main/templates/).
164+
<!-- This block generates a file tree.
165+
A guide for using macros can be found at
166+
https://github.com/bids-standard/bids-specification/blob/master/macros_doc.md
167+
-->
168+
{{ MACROS___render_text("objects.files.README.description") }}
173169

174170
### `CHANGES`
175171

176-
Version history of the dataset (describing changes, updates and corrections) MAY
177-
be provided in the form of a `CHANGES` text file. This file MUST follow the
178-
[CPAN Changelog convention](https://metacpan.org/pod/release/HAARG/CPAN-Changes-0.400002/lib/CPAN/Changes/Spec.pod).
179-
The `CHANGES` file MUST be either in ASCII or UTF-8 encoding.
172+
<!-- This block generates a description.
173+
A guide for using macros can be found at
174+
https://github.com/bids-standard/bids-specification/blob/master/macros_doc.md
175+
-->
176+
{{ MACROS___render_text("objects.files.CHANGES.description") }}
180177

181178
Example:
182179

@@ -190,10 +187,11 @@ Example:
190187

191188
### `LICENSE`
192189

193-
A `LICENSE` file MAY be provided in addition to the short specification of the
194-
used license in the `dataset_description.json` `"License"` field.
195-
The `"License"` field and `LICENSE` file MUST correspond.
196-
The `LICENSE` file MUST be either in ASCII or UTF-8 encoding.
190+
<!-- This block generates a description.
191+
A guide for using macros can be found at
192+
https://github.com/bids-standard/bids-specification/blob/master/macros_doc.md
193+
-->
194+
{{ MACROS___render_text("objects.files.LICENSE.description") }}
197195

198196
## Participants file
199197

@@ -204,23 +202,15 @@ participants.tsv
204202
participants.json
205203
```
206204

207-
The purpose of this RECOMMENDED file is to describe properties of participants
208-
such as age, sex, handedness, species and strain.
209-
If this file exists, it MUST contain the column `participant_id`,
210-
which MUST consist of `sub-<label>` values identifying one row for each participant,
211-
followed by a list of optional columns describing participants.
212-
Each participant MUST be described by one and only one row.
213-
214-
The RECOMMENDED `species` column SHOULD be a binomial species name from the
215-
[NCBI Taxonomy](https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi)
216-
(for examples `homo sapiens`, `mus musculus`, `rattus norvegicus`).
217-
For backwards compatibility, if `species` is absent, the participant is assumed to be
218-
`homo sapiens`.
205+
<!-- This block generates a description.
206+
A guide for using macros can be found at
207+
https://github.com/bids-standard/bids-specification/blob/master/macros_doc.md
208+
-->
209+
{{ MACROS___render_text("objects.files.participants.description") }}
219210

220-
Commonly used *optional* columns in `participants.tsv` files are `age`, `sex`,
221-
`handedness`, `strain`, and `strain_rrid`. We RECOMMEND to make use
222-
of these columns, and in case that you do use them, we RECOMMEND to use the
223-
following values for them:
211+
We RECOMMEND to make use of these columns, and
212+
in case that you do use them, we RECOMMEND to use the following values
213+
for them:
224214

225215
<!-- This block generates a columns table.
226216
The definitions of these fields can be found in
@@ -293,9 +283,11 @@ samples.tsv
293283
samples.json
294284
```
295285

296-
The purpose of this file is to describe properties of samples, indicated by the `sample` entity.
297-
This file is REQUIRED if `sample-<label>` is present in any filename within the dataset.
298-
Each sample MUST be described by one and only one row.
286+
<!-- This block generates a description.
287+
A guide for using macros can be found at
288+
https://github.com/bids-standard/bids-specification/blob/master/macros_doc.md
289+
-->
290+
{{ MACROS___render_text("objects.files.samples.description") }}
299291

300292
<!-- This block generates a columns table.
301293
The definitions of these fields can be found in

src/modality-specific-files/genetic-descriptor.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ Template:
8383
genetic_info.json
8484
```
8585

86-
The `genetic_info.json` file describes the genetic information available in the
87-
`participants.tsv` file and/or the genetic database described in
88-
`dataset_description.json`.
89-
Datasets containing the `Genetics` field in `dataset_description.json` or the
90-
`genetic_id` column in `participants.tsv` MUST include this file with the following
91-
fields:
86+
The following fields are defined for genetic_info.json:
87+
88+
<!-- This block generates a description.
89+
A guide for using macros can be found at
90+
https://github.com/bids-standard/bids-specification/blob/master/macros_doc.md
91+
-->
92+
{{ MACROS___render_text("objects.files.genetic_info.description") }}
9293

9394
<!-- This block generates a metadata table.
9495
The definitions of these fields can be found in

src/schema/objects/files.yaml

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,82 +50,35 @@ genetic_info:
5050
The `genetic_info.json` file describes the genetic information available in the
5151
`participants.tsv` file and/or the genetic database described in
5252
`dataset_description.json`.
53+
5354
Datasets containing the `Genetics` field in `dataset_description.json` or the
5455
`genetic_id` column in `participants.tsv` MUST include this file.
5556
participants:
5657
display_name: Participant Information
5758
file_type: regular
5859
description: |
5960
The purpose of this RECOMMENDED file is to describe properties of participants
60-
such as age, sex, handedness.
61+
such as age, sex, handedness, species and strain.
6162
If this file exists, it MUST contain the column `participant_id`,
6263
which MUST consist of `sub-<label>` values identifying one row for each participant,
6364
followed by a list of optional columns describing participants.
6465
Each participant MUST be described by one and only one row.
6566
66-
Commonly used *optional* columns in `participant.tsv` files are `age`, `sex`,
67-
and `handedness`. We RECOMMEND to make use of these columns, and
68-
in case that you do use them, we RECOMMEND to use the following values
69-
for them:
70-
71-
- `age`: numeric value in years (float or integer value)
72-
73-
- `sex`: string value indicating phenotypical sex, one of "male", "female",
74-
"other"
75-
76-
- for "male", use one of these values: `male`, `m`, `M`, `MALE`, `Male`
77-
78-
- for "female", use one of these values: `female`, `f`, `F`, `FEMALE`,
79-
`Female`
80-
81-
- for "other", use one of these values: `other`, `o`, `O`, `OTHER`,
82-
`Other`
83-
84-
- `handedness`: string value indicating one of "left", "right",
85-
"ambidextrous"
86-
87-
- for "left", use one of these values: `left`, `l`, `L`, `LEFT`, `Left`
88-
89-
- for "right", use one of these values: `right`, `r`, `R`, `RIGHT`,
90-
`Right`
91-
92-
- for "ambidextrous", use one of these values: `ambidextrous`, `a`, `A`,
93-
`AMBIDEXTROUS`, `Ambidextrous`
94-
95-
Throughout BIDS you can indicate missing values with `n/a` (for "not
96-
available").
67+
Commonly used *optional* columns in `participants.tsv` files are `age`, `sex`,
68+
`handedness`, `strain`, and `strain_rrid`.
9769
70+
The RECOMMENDED `species` column SHOULD be a binomial species name from the
71+
[NCBI Taxonomy](https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi)
72+
(for examples `homo sapiens`, `mus musculus`, `rattus norvegicus`).
73+
For backwards compatibility, if `species` is absent, the participant is assumed to be
74+
`homo sapiens`.
9875
samples:
9976
display_name: Sample Information
10077
file_type: regular
10178
description: |
10279
The purpose of this file is to describe properties of samples, indicated by the `sample` entity.
10380
This file is REQUIRED if `sample-<label>` is present in any filename within the dataset.
104-
If this file exists, it MUST contain the three following columns:
105-
106-
- `sample_id`: MUST consist of `sample-<label>` values identifying one row
107-
for each sample
108-
109-
- `participant_id`: MUST consist of `sub-<label>`
110-
111-
- `sample_type`: MUST consist of sample type values, either `cell line`, `in vitro differentiated cells`,
112-
`primary cell`, `cell-free sample`, `cloning host`, `tissue`, `whole organisms`, `organoid` or
113-
`technical sample` from [ENCODE Biosample Type](https://www.encodeproject.org/profiles/biosample_type)
114-
115-
Other optional columns MAY be used to describe the samples.
11681
Each sample MUST be described by one and only one row.
117-
118-
Commonly used *optional* columns in `samples.tsv` files are `pathology` and
119-
`derived_from`. We RECOMMEND to make use of these columns, and in case that
120-
you do use them, we RECOMMEND to use the following values for them:
121-
122-
- `pathology`: string value describing the pathology of the sample or type of control.
123-
When different from `healthy`, pathology SHOULD be specified in `samples.tsv`.
124-
The pathology MAY instead be specified in
125-
[Sessions files](SPEC_ROOT/modality-agnostic-files.md#sessions-file) in case it changes over time.
126-
127-
- `derived_from`: `sample-<label>` key/value pair from which a sample is derived from,
128-
for example a slice of tissue (`sample-02`) derived from a block of tissue (`sample-01`)
12982
code:
13083
display_name: Code
13184
file_type: directory

tools/mkdocs_macros_bids/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
make_filetree_example,
66
make_metadata_table,
77
make_suffix_table,
8+
render_text,
89
)
910
from .main import define_env
1011

@@ -16,4 +17,5 @@
1617
"make_suffix_table",
1718
"make_metadata_table",
1819
"make_filetree_example",
20+
"render_text",
1921
]

tools/mkdocs_macros_bids/macros.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,12 @@ def define_allowed_top_directories(src_path=None):
364364
schema_obj = schema.load_schema()
365365
string = render.define_allowed_top_directories(schema_obj, src_path=src_path)
366366
return string
367+
368+
369+
def render_text(key, src_path=None):
370+
if src_path is None:
371+
src_path = _get_source_path()
372+
373+
schema_obj = schema.load_schema()
374+
string = render.render_text(schema_obj, key, src_path=src_path)
375+
return string

tools/mkdocs_macros_bids/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ def define_env(env):
4646
env.macro(
4747
macros.define_allowed_top_directories, "MACROS___define_allowed_top_directories"
4848
)
49+
env.macro(macros.render_text, "MACROS___render_text")

tools/schemacode/bidsschematools/render/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
make_entity_definitions,
1414
make_filename_template,
1515
make_glossary,
16+
render_text,
1617
)
1718

1819
__all__ = [
@@ -27,4 +28,5 @@
2728
"make_filename_template",
2829
"define_common_principles",
2930
"define_allowed_top_directories",
31+
"render_text",
3032
]

tools/schemacode/bidsschematools/render/text.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,34 @@ def define_allowed_top_directories(schema, src_path=None) -> str:
528528
string += f"- `{dirname}`: {definition.description}"
529529

530530
return string.replace("SPEC_ROOT", utils.get_relpath(src_path))
531+
532+
533+
def render_text(schema, key: str, src_path=None):
534+
"""
535+
536+
Parameters
537+
----------
538+
schema : dict
539+
The BIDS schema.
540+
541+
object : str
542+
The object to render the description for:
543+
possible values correspond to the keys in schema["objects"].
544+
545+
key : str
546+
The key of the object to render the description for:
547+
possible values correspond to the keys in schema["objects"][object]
548+
549+
src_path : str or None
550+
The file where this macro is called, which may be explicitly provided
551+
by the "page.file.src_path" variable.
552+
553+
Returns
554+
-------
555+
desc : str
556+
Description of the object.
557+
"""
558+
text = schema.get(key)
559+
if not isinstance(text, str):
560+
raise ValueError(f"{key} does not refer to a text field")
561+
return text.replace("SPEC_ROOT", utils.get_relpath(src_path))

tools/schemacode/bidsschematools/tests/test_render_text.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Tests for the bidsschematools package."""
22
import os
33

4+
import pytest
5+
46
from bidsschematools.render import text
57

68

@@ -130,3 +132,17 @@ def test_define_allowed_top_directories(schema_obj):
130132
"""smoke test for allowed top directories."""
131133
test_str = text.define_allowed_top_directories(schema_obj)
132134
assert isinstance(test_str, str)
135+
136+
137+
def test_render_text(schema_obj):
138+
test_str = text.render_text(
139+
schema_obj, key="objects.files.dataset_description.description", src_path=None
140+
)
141+
assert (
142+
test_str == "The file `dataset_description.json` is a JSON file describing the dataset.\n"
143+
)
144+
145+
146+
def test_render_text_errors(schema_obj):
147+
with pytest.raises(ValueError, match="does not refer to a text field"):
148+
text.render_text(schema_obj, key="dataset_description", src_path=None)

0 commit comments

Comments
 (0)