Skip to content

Commit

Permalink
[ENH] use macros for filename templates (#536)
Browse files Browse the repository at this point in the history
* use files for templates

* fix
  • Loading branch information
Remi-Gau authored Oct 18, 2024
1 parent 2cf8af9 commit 3c4f714
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 767 deletions.
1 change: 0 additions & 1 deletion .remarkignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
docs/collaboration/bids_github/
docs/datasets/index.md
docs/getting_started/folders_and_files/files.md
docs/specification/macros_doc.md
2 changes: 2 additions & 0 deletions docs/faq/bids-extensions.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# BIDS extensions

## Can we introduce new entities / metadata in a BEP?

> We need to introduce an entity to distinguish on-device preprocessing methods
Expand Down
2 changes: 2 additions & 0 deletions docs/faq/eeg.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# EEG

## How to format Hardware and Software filter fields in a .json?

In the modality specific sidecar file `_eeg.json`, we can specify the software
Expand Down
2 changes: 2 additions & 0 deletions docs/faq/mri.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# MRI

## I only have nifti files and no dicom. Can I still create a BIDS dataset?

In theory yes, but it is possible that you will be missing some metadata that is required by the BIDS specification.
Expand Down
681 changes: 1 addition & 680 deletions docs/getting_started/folders_and_files/files.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions macros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
generate_apps_table,
generate_beps_table,
generate_converter_table,
generate_filename_templates,
generate_grants_table,
generate_members_table,
generate_tools_table,
Expand All @@ -18,4 +19,5 @@
"generate_grants_table",
"generate_apps_table",
"generate_working_groups_table",
"generate_filename_templates",
]
47 changes: 46 additions & 1 deletion macros/macros.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path

import ruamel.yaml
from bidsschematools import render, schema
from jinja2 import Environment, FileSystemLoader, select_autoescape
from rich import print

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
Expand Down Expand Up @@ -78,8 +80,51 @@ def generate_apps_table():
return template.render(include=content, type=type)


def generate_filename_templates():
"""Create filename templates for all datatypes of all modalities."""

schema_obj = schema.load_schema()

modalities = schema_obj.rules.modalities

to_render = [
{
"name": x,
"description": schema_obj.objects.modalities[x]["description"],
"datatypes": [
{
"name": dt,
"filenames": filename_template_for(schema_obj, dt),
}
for dt in modalities[x]["datatypes"]
],
}
for x in modalities
]

env = return_jinja_env()
template = env.get_template("filename_templates_md.jinja")
return template.render(include=to_render)


def filename_template_for(schema_obj, datatype):
"""Create filename templates for a single datatype."""
filenames = render.make_filename_template(
dstype="raw",
schema=schema_obj,
src_path=Path("https://bids-specification.readthedocs.io/en/latest/"),
pdf_format=False,
datatypes=[datatype],
)
filenames = filenames.replace(
"../../..",
"https://bids-specification.readthedocs.io/en/latest",
)
return filenames


def main():
print(generate_converter_table(file="converters.yml", data_type="EEG"))
print(generate_filename_templates())


if __name__ == "__main__":
Expand Down
4 changes: 4 additions & 0 deletions macros/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ def define_env(env):
macros.generate_working_groups_table,
"MACROS___generate_working_groups_table",
)
env.macro(
macros.generate_filename_templates,
"MACROS___generate_filename_templates",
)
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ rich
ruamel.yaml
tabulate
pyzotero
file:specification/tools/schemacode
bidsschematools[render]
-e file:tools
11 changes: 8 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bibtexparser==1.4.1
# via pyzotero
bids-validator==1.14.6
# via pybids
file:specification/tools/schemacode
bidsschematools==0.11.3
# via -r requirements.in
cairocffi==1.7.0
# via cairosvg
Expand Down Expand Up @@ -90,7 +90,9 @@ markdown==3.6
# mkdocs-material
# pymdown-extensions
markdown-it-py==3.0.0
# via -r requirements.in
# via
# -r requirements.in
# bidsschematools
markupsafe==2.1.5
# via
# jinja2
Expand Down Expand Up @@ -148,6 +150,7 @@ paginate==0.5.6
# via mkdocs-material
pandas==2.2.2
# via
# bidsschematools
# formulaic
# pybids
# seaborn
Expand Down Expand Up @@ -233,7 +236,9 @@ smmap==5.0.1
sqlalchemy==2.0.30
# via pybids
tabulate==0.9.0
# via -r requirements.in
# via
# -r requirements.in
# bidsschematools
tenacity==8.4.1
# via plotly
termcolor==2.4.0
Expand Down
15 changes: 15 additions & 0 deletions templates/filename_templates_md.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% for modality in include %}

### {{ modality["name"]|upper }}

{{ modality["description"] }}

{% for dt in modality["datatypes"] %}

#### {{ dt["name"] }}

{{ dt["filenames"] }}

{% endfor %}

{% endfor %}
81 changes: 0 additions & 81 deletions tools/build/print_filename_templates.py

This file was deleted.

0 comments on commit 3c4f714

Please sign in to comment.