From 9048ae5b2966e538cf9458b9f90d3a478a1b4aef Mon Sep 17 00:00:00 2001 From: IanCa Date: Thu, 7 Mar 2024 18:01:12 -0600 Subject: [PATCH] Lazy load definition entries when creating tags(tests run ~25 faster locally) --- hed/models/definition_dict.py | 32 ++++++++++++-------------------- hed/models/hed_tag.py | 16 +++++++++++++++- hed/validator/hed_validator.py | 3 --- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/hed/models/definition_dict.py b/hed/models/definition_dict.py index 2c7f105ed..234cd5b92 100644 --- a/hed/models/definition_dict.py +++ b/hed/models/definition_dict.py @@ -251,30 +251,22 @@ def _validate_contents(self, definition_tag, group, error_handler): return issues - def construct_def_tags(self, hed_string_obj): - """ Identify def/def-expand tag contents in the given string. + def get_definition_entry(self, def_tag): + """ Get the entry for a given def tag. - Parameters: - hed_string_obj(HedString): The hed string to identify definition contents in - """ - for tag in hed_string_obj.get_all_tags(): - self.construct_def_tag(tag) - - def construct_def_tag(self, hed_tag): - """ Identify def/def-expand tag contents in the given HedTag. + Does not validate at all. Parameters: - hed_tag(HedTag): The hed tag to identify definition contents in + def_tag (HedTag): Source hed tag that may be a Def or Def-expand tag. + + Returns: + def_entry(DefinitionEntry or None): The definition entry if it exists """ - # Finish tracking down why parent is set incorrectly on def tags sometimes - # It should be ALWAYS set - if hed_tag.short_base_tag in {DefTagNames.DEF_ORG_KEY, DefTagNames.DEF_EXPAND_ORG_KEY}: - save_parent = hed_tag._parent - def_contents = self._get_definition_contents(hed_tag) - hed_tag._parent = save_parent - if def_contents is not None: - hed_tag._expandable = def_contents - hed_tag._expanded = hed_tag.short_base_tag == DefTagNames.DEF_EXPAND_ORG_KEY + tag_label, _, placeholder = def_tag.extension.partition('/') + + label_tag_lower = tag_label.lower() + def_entry = self.defs.get(label_tag_lower) + return def_entry def _get_definition_contents(self, def_tag): """ Get the contents for a given def tag. diff --git a/hed/models/hed_tag.py b/hed/models/hed_tag.py index 4dc99361f..d5afb132d 100644 --- a/hed/models/hed_tag.py +++ b/hed/models/hed_tag.py @@ -1,6 +1,7 @@ """ A single HED tag. """ from hed.schema.hed_schema_constants import HedKey import copy +from hed.models.model_constants import DefTagNames class HedTag: @@ -46,8 +47,10 @@ def __init__(self, hed_string, hed_schema, span=None, def_dict=None): self.tag_terms = None # tuple of all the terms in this tag Lowercase. self._calculate_to_canonical_forms(hed_schema) + self._def_entry = None if def_dict: - def_dict.construct_def_tag(self) + if self.short_base_tag in {DefTagNames.DEF_ORG_KEY, DefTagNames.DEF_EXPAND_ORG_KEY}: + self._def_entry = def_dict.get_definition_entry(self) def copy(self): """ Return a deep copy of this tag. @@ -261,9 +264,20 @@ def expandable(self): This is primarily used for Def/Def-expand tags at present. + Lazily set the first time it's called. + Returns: HedGroup or HedTag or None: Returns the expanded form of this tag. """ + if self._expandable is None and self._def_entry: + save_parent = self._parent + tag_label, _, placeholder = self.extension.partition('/') + + def_contents = self._def_entry.get_definition(self, placeholder_value=placeholder) + self._parent = save_parent + if def_contents is not None: + self._expandable = def_contents + self._expanded = self.short_base_tag == DefTagNames.DEF_EXPAND_ORG_KEY return self._expandable def is_column_ref(self): diff --git a/hed/validator/hed_validator.py b/hed/validator/hed_validator.py index e0a28c899..fe4ddc113 100644 --- a/hed/validator/hed_validator.py +++ b/hed/validator/hed_validator.py @@ -75,9 +75,6 @@ def run_basic_checks(self, hed_string, allow_placeholders): issues += hed_string._calculate_to_canonical_forms(self._hed_schema) if check_for_any_errors(issues): return issues - # This is required so it can validate the tag a tag expands into - # e.g. checking units when a definition placeholder has units - self._def_validator.construct_def_tags(hed_string) issues += self._validate_individual_tags_in_hed_string(hed_string, allow_placeholders=allow_placeholders) issues += self._def_validator.validate_def_tags(hed_string, self) return issues