Skip to content

Commit

Permalink
Merge pull request #882 from IanCa/develop
Browse files Browse the repository at this point in the history
Lazy load definition entries when creating tags
  • Loading branch information
VisLab authored Mar 8, 2024
2 parents b982688 + 9048ae5 commit fd20109
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
32 changes: 12 additions & 20 deletions hed/models/definition_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion hed/models/hed_tag.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 0 additions & 3 deletions hed/validator/hed_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fd20109

Please sign in to comment.