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

performance: do not duplicate schema loading #982

Merged
merged 2 commits into from
Apr 25, 2022
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
23 changes: 12 additions & 11 deletions dandi/bids_validator_xs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from copy import deepcopy
import datetime
from functools import lru_cache
import json
import os
import re
Expand Down Expand Up @@ -191,24 +192,24 @@ def _add_suffixes(regex_string, variant):


def load_top_level(
schema_dir,
my_schema,
):
"""
Create full path regexes for top level files, as documented by a target BIDS YAML schema
version.


Parameters
----------
schema_dir : str
A string pointing to a BIDS directory for which paths should be validated.
my_schema : dict
A nested dictionary, as returned by `dandi.support.bids.schema.load_schema()`.

Returns
-------
regex_schema : list of dict
A list of dictionaries, with keys including 'regex' and 'mandatory'.
"""

my_schema = schema.load_schema(schema_dir)
top_level_files = my_schema["rules"]["top_level_files"]

regex_schema = []
Expand All @@ -231,14 +232,14 @@ def load_top_level(


def load_entities(
schema_dir,
my_schema,
):
"""Create full path regexes for entities, as documented by a target BIDS YAML schema version.

Parameters
----------
schema_dir : str
A string pointing to a BIDS directory for which paths should be validated.
my_schema : dict
A nested dictionary, as returned by `dandi.support.bids.schema.load_schema()`.

Notes
-----
Expand All @@ -263,8 +264,6 @@ def load_entities(
A list of dictionaries, with keys including 'regex' and 'mandatory'.
"""

my_schema = schema.load_schema(schema_dir)

label = "([a-z,A-Z,0-9]*?)"

# Parsing tabular_metadata as a datatype, might be done automatically if the YAML is moved
Expand Down Expand Up @@ -332,6 +331,7 @@ def load_entities(
return regex_schema


@lru_cache()
def load_all(
schema_dir,
):
Expand All @@ -349,11 +349,12 @@ def load_all(
A list of dictionaries, with keys including 'regex' and 'mandatory'.
"""

my_schema = schema.load_schema(schema_dir)
all_regex = load_entities(
schema_dir=schema_dir,
my_schema=my_schema,
)
top_level_regex = load_top_level(
schema_dir=schema_dir,
my_schema=my_schema,
)
all_regex.extend(top_level_regex)

Expand Down