Repository navigation
feat: TemplateFlow 2.0 rule set and tfmgr validate
#13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| """Files generated from a template's metadata. | ||
|
|
||
| The same functions write the files (``tfmgr generate``) and check that the committed copies | ||
| are up to date (rules TF410, TF607 and TF801). | ||
|
|
||
| - ``tpl-<label>_atlases.tsv`` is written by people; each of its rows generates an | ||
| ``atlas-<label>_description.json`` (BIDS 1.11). | ||
| - ``tpl-<label>_cohorts.tsv`` is generated from the ``cohort`` block of | ||
| template_description.json. | ||
| - ``templates.tsv`` (root of the Archive) is generated from every template_description.json. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from .tables import NA, Table, dump_json, json_cell, write_tsv | ||
|
|
||
| #: atlases.tsv column -> atlas-<label>_description.json field (BIDS 1.11) | ||
| ATLAS_FIELDS = { | ||
| 'name': 'Name', | ||
| 'license': 'License', | ||
| 'description': 'Description', | ||
| 'authors': 'Authors', | ||
| 'curators': 'Curators', | ||
| 'funding': 'Funding', | ||
| 'references_and_links': 'ReferencesAndLinks', | ||
| 'species': 'Species', | ||
| 'sample_size': 'SampleSize', | ||
| 'derived_from': 'DerivedFrom', | ||
| 'level_type': 'LevelType', | ||
| 'rrid': 'RRID', | ||
| } | ||
| #: Columns of atlases.tsv: BIDS (bids-specification#2286), then TemplateFlow's | ||
| ATLAS_COLUMNS = { | ||
| 'atlas_id': 'required', | ||
| 'name': 'required', | ||
| 'license': 'required', | ||
| 'description': 'recommended', | ||
| 'authors': 'recommended', | ||
| 'curators': 'optional', | ||
| 'funding': 'optional', | ||
| 'references_and_links': 'recommended', | ||
| 'species': 'optional', | ||
| 'sample_size': 'optional', | ||
| 'derived_from': 'optional', | ||
| 'level_type': 'optional', | ||
| 'rrid': 'optional', | ||
| 'readme': 'recommended', | ||
| 'derived_from_atlas': 'optional', | ||
| } | ||
| #: Columns whose cells hold JSON arrays | ||
| ATLAS_ARRAYS = {'authors', 'curators', 'funding', 'references_and_links'} | ||
|
|
||
| COHORT_COLUMNS = [ | ||
| 'cohort_id', | ||
| 'description', | ||
| 'age_min', | ||
| 'age_max', | ||
| 'age_units', | ||
| 'sample_size', | ||
| 'sex', | ||
| ] | ||
| COHORT_SIDECAR = { | ||
| 'cohort_id': {'Description': 'Cohort identifier (cohort-<index> in file names).'}, | ||
| 'description': {'Description': 'Readable name of the cohort.'}, | ||
| 'age_min': {'Description': 'Lowest age of the cohort.'}, | ||
| 'age_max': {'Description': 'Highest age of the cohort.'}, | ||
| 'age_units': { | ||
| 'Description': 'Units of age_min and age_max.', | ||
| 'Levels': { | ||
| 'weeks': 'Weeks (gestational, for neonates)', | ||
| 'months': 'Months', | ||
| 'years': 'Years', | ||
| }, | ||
| }, | ||
| 'sample_size': {'Description': 'Number of participants averaged into the cohort.'}, | ||
| 'sex': {'Description': 'Sex of the participants.'}, | ||
| } | ||
|
|
||
| TEMPLATE_COLUMNS = ['template_id', 'description', 'species', 'license', 'cohorts', 'atlases'] | ||
|
|
||
|
|
||
| def atlas_label(atlas_id: str) -> str: | ||
| return atlas_id.removeprefix('atlas-') | ||
|
|
||
|
|
||
| def atlas_description(row: dict[str, str]) -> dict: | ||
| """The atlas description (BIDS 1.11) generated from a row of atlases.tsv.""" | ||
| out = {} | ||
| for column, key in ATLAS_FIELDS.items(): | ||
| value = row.get(column, NA) | ||
| if value in ('', NA, None): | ||
| continue | ||
| if column in ATLAS_ARRAYS or column == 'species': | ||
| value = json_cell(value) | ||
| elif column == 'sample_size': | ||
| try: | ||
| value = int(value) if value.isdigit() else float(value) | ||
| except ValueError: | ||
| pass # reported by TF604 | ||
| out[key] = value | ||
| return out | ||
|
|
||
|
|
||
| def atlas_descriptions(table: Table) -> dict[str, str]: | ||
| """``{path: content}`` of the atlas descriptions generated from atlases.tsv.""" | ||
| return { | ||
| f'atlas-{atlas_label(row["atlas_id"])}_description.json': dump_json(atlas_description(row)) | ||
| for row in table.rows | ||
| if row.get('atlas_id', NA) not in ('', NA) | ||
| } | ||
|
|
||
|
|
||
| def _cohort_description(key: str, cohort: dict) -> str: | ||
| if cohort.get('name'): | ||
| return cohort['name'] | ||
| age = cohort.get('age') or [] | ||
| units = cohort.get('units', '') | ||
| if len(age) == 2 and age[0] != age[1]: | ||
| return f'Ages {_num(age[0])}-{_num(age[1])} {units}'.strip() | ||
| if age: | ||
| return f'Age {_num(age[0])} {units}'.strip() | ||
| return f'Cohort {key}' | ||
|
|
||
|
|
||
| def _num(value): | ||
| return int(value) if isinstance(value, float) and value.is_integer() else value | ||
|
|
||
|
|
||
| def cohorts_table(description: dict) -> tuple[str, str] | None: | ||
| """``(tsv, json)`` of the cohorts table, or ``None`` when the template has no cohorts.""" | ||
| cohorts = description.get('cohort') or {} | ||
| if not cohorts: | ||
| return None | ||
| rows = [] | ||
| for key in sorted(cohorts, key=lambda k: (int(k) if k.isdigit() else 1e9, k)): | ||
| cohort = cohorts[key] or {} | ||
| age = cohort.get('age') or [None, None] | ||
| if len(age) == 1: | ||
| age = [age[0], age[0]] | ||
| rows.append( | ||
| { | ||
| 'cohort_id': f'cohort-{key}', | ||
| 'description': _cohort_description(key, cohort), | ||
| 'age_min': _num(age[0]) if age[0] is not None else None, | ||
| 'age_max': _num(age[1]) if age[1] is not None else None, | ||
| 'age_units': cohort.get('units'), | ||
| 'sample_size': cohort.get('samplesize'), | ||
| 'sex': cohort.get('sex'), | ||
| } | ||
| ) | ||
| return write_tsv(COHORT_COLUMNS, rows), dump_json(COHORT_SIDECAR) | ||
|
|
||
|
|
||
| def templates_table( | ||
| descriptions: dict[str, dict], atlases: dict[str, list[str]] | None = None | ||
| ) -> str: | ||
| """templates.tsv from ``{template label: template_description}``.""" | ||
| atlases = atlases or {} | ||
| rows = [] | ||
| for label in sorted(descriptions, key=str.lower): | ||
| desc = descriptions[label] | ||
| species = desc.get('Species') | ||
| rows.append( | ||
| { | ||
| 'template_id': f'tpl-{label}', | ||
| 'description': desc.get('Name'), | ||
| 'species': species, | ||
| 'license': desc.get('License'), | ||
| 'cohorts': len(desc.get('cohort') or {}) or None, | ||
| 'atlases': sorted(atlases.get(label, [])) or None, | ||
| } | ||
| ) | ||
| return write_tsv(TEMPLATE_COLUMNS, rows) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an atlas TSV array cell starts with
[but is not valid JSON, the earlier check reports TF604, butjson_cellraisesValueErrorwhile generating the expected description. The CLI then exits with an internal error rather than returning the validation findings; avoid generating from invalid cells or handle the parse failure here.Useful? React with 👍 / 👎.