Skip to content
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

[SCHEMATOOLS] Implement inheritance for all TSV/JSON files #1671

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions tools/schemacode/bidsschematools/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _entity_rule(rule: Mapping, schema: bst.types.Namespace):
}


def _split_inheritance_rules(rule: Mapping) -> ty.List[Mapping]:
def _split_inheritance_rules(rule: dict) -> ty.List[dict]:
"""Break composite rules into main and sidecar rules

Implements the inheritance principle for file naming.
Expand All @@ -139,32 +139,28 @@ def _split_inheritance_rules(rule: Mapping) -> ty.List[Mapping]:
rule_exts = set(rule["extensions"])

main_exts = rule_exts - heritable_exts
# If a rule only has TSV or JSON files, entities can be
# made required
if not main_exts:
if ".tsv" in rule_exts:
main_exts = {".tsv"}
elif ".json" in rule_exts:
main_exts = {".json"}

sidecar_exts = rule_exts - main_exts
if not sidecar_exts:
return [rule]

sidecar_dtypes = [""] + rule.get("datatypes", [])
sidecar_entities = {ent: "optional" for ent in rule["entities"]}

main_rule = {**rule, **{"extensions": list(main_exts)}}
sidecar_rule = {
**rule,
**{
"extensions": list(sidecar_exts),
"datatypes": sidecar_dtypes,
"entities": sidecar_entities,
},
}
rules = []

# Some rules only address metadata, such as events.tsv or coordsystem.json
if main_exts:
rules.append({**rule, **{"extensions": list(main_exts)}})

rules.append(
{
**rule,
**{
"extensions": list(sidecar_exts),
"datatypes": [""] + rule.get("datatypes", []),
"entities": {ent: "optional" for ent in rule["entities"]},
},
}
)

return [main_rule, sidecar_rule]
return rules


def _sanitize_extension(ext: str) -> str:
Expand Down