HedString

class HedString(hed_string, hed_schema, def_dict=None, _contents=None)[source]

A HED string with its schema and definitions.

Methods

HedString.__init__(hed_string, hed_schema[, ...])

Constructor for the HedString class.

HedString.append(tag_or_group)

Add a tag or group to this group.

HedString.casefold()

Convenience function, equivalent to str(self).casefold().

HedString.check_if_in_original(tag_or_group)

Check if the tag or group in original string.

HedString.copy()

Return a deep copy of this string.

HedString.expand_defs()

Replace def tags with def-expand tags.

HedString.find_def_tags([recursive, ...])

Find def and def-expand tags.

HedString.find_exact_tags(exact_tags[, ...])

Find the given tags.

HedString.find_placeholder_tag()

Return a placeholder tag, if present in this group.

HedString.find_tags(search_tags[, ...])

Find the base tags and their containing groups.

HedString.find_tags_with_term(term[, ...])

Find any tags that contain the given term.

HedString.find_top_level_tags(anchor_tags[, ...])

Find top level groups with an anchor tag.

HedString.find_wildcard_tags(search_tags[, ...])

Find the tags and their containing groups.

HedString.from_hed_strings(hed_strings)

Factory for creating HedStrings via combination.

HedString.get_all_groups([also_return_depth])

Return HedGroups, including descendants and self.

HedString.get_all_tags()

Return HedTags, including descendants.

HedString.get_as_form(tag_attribute)

Get the string corresponding to the specified form.

HedString.get_as_indented([tag_attribute])

Return the string as a multiline indented format.

HedString.get_as_long()

Return this HedGroup as a long tag string.

HedString.get_as_original()

Return the original form of this string.

HedString.get_as_short()

Return this HedGroup as a short tag string.

HedString.get_first_group()

Return the first group in this HED string or group.

HedString.get_original_hed_string()

Get the original HED string.

HedString.groups()

Return the direct child groups of this group.

HedString.lower()

Convenience function, equivalent to str(self).lower().

HedString.remove(items_to_remove)

Remove any tags/groups in items_to_remove.

HedString.remove_definitions()

Remove definition tags and groups from this string.

HedString.remove_refs()

Remove any refs(tags contained entirely inside curly braces) from the string.

HedString.replace(item_to_replace, new_contents)

Replace an existing tag or group.

HedString.shrink_defs()

Replace def-expand tags with def tags.

HedString.sort()

Sort the tags and groups in this HedString in a consistent order.

HedString.sorted()

Return a sorted copy of this HED group

HedString.split_hed_string(hed_string)

Split a HED string into delimiters and tags.

HedString.split_into_groups(hed_string, ...)

Split the HED string into a parse tree.

HedString.tags()

Return the direct child tags of this group.

HedString.validate([allow_placeholders, ...])

Validate the string using the schema.

Attributes

HedString.CLOSING_GROUP_CHARACTER

HedString.OPENING_GROUP_CHARACTER

HedString.is_group

Always False since the underlying string is not a group with parentheses.

HedString.span

Return the source span.

HedString.__init__(hed_string, hed_schema, def_dict=None, _contents=None)[source]

Constructor for the HedString class.

Parameters:
  • hed_string (str) – A HED string consisting of tags and tag groups.

  • hed_schema (HedSchema) – The schema to use to identify tags.

  • def_dict (DefinitionDict or None) – The def dict to use to identify def/def expand tags.

  • _contents ([HedGroup and/or HedTag] or None) – Create a HedString from this exact list of children. Does not make a copy.

Notes

  • The HedString object parses its component tags and groups into a tree-like structure.

HedString.append(tag_or_group)

Add a tag or group to this group.

Parameters:

tag_or_group (HedTag or HedGroup) – The new object to add to this group.

HedString.casefold()

Convenience function, equivalent to str(self).casefold().

HedString.check_if_in_original(tag_or_group)

Check if the tag or group in original string.

Parameters:

tag_or_group (HedTag or HedGroup) – The HedTag or HedGroup to be looked for in this group.

Returns:

True if in this group.

Return type:

bool

HedString.copy()[source]

Return a deep copy of this string.

Returns:

The copied group.

Return type:

HedString

HedString.expand_defs()[source]

Replace def tags with def-expand tags.

This does very minimal validation.

Returns:

self

HedString.find_def_tags(recursive=False, include_groups=3)

Find def and def-expand tags.

Parameters:
  • recursive (bool) – If true, also check subgroups.

  • include_groups (int, 0, 1, 2, 3) – Options for return values. If 0: Return only def and def expand tags/. If 1: Return only def tags and def-expand groups. If 2: Return only groups containing defs, or def-expand groups. If 3 or any other value: Return all 3 as a tuple.

Returns:

A list of tuples. The contents depend on the values of the include_group.

Return type:

list

HedString.find_exact_tags(exact_tags, recursive=False, include_groups=1)

Find the given tags. This will only find complete matches, any extension or value must also match.

Parameters:
  • exact_tags (list of HedTag) – A container of tags to locate.

  • recursive (bool) – If true, also check subgroups.

  • include_groups (bool) – 0, 1 or 2. If 0: Return only tags If 1: Return only groups If 2 or any other value: Return both

Returns:

A list of tuples. The contents depend on the values of the include_group.

Return type:

list

HedString.find_placeholder_tag()

Return a placeholder tag, if present in this group.

Returns:

The placeholder tag if found.

Return type:

HedTag or None

Notes

  • Assumes a valid HedString with no erroneous “#” characters.

HedString.find_tags(search_tags, recursive=False, include_groups=2)

Find the base tags and their containing groups. This searches by short_base_tag, ignoring any ancestors or extensions/values.

Parameters:
  • search_tags (container) – A container of short_base_tags to locate.

  • recursive (bool) – If true, also check subgroups.

  • include_groups (0, 1 or 2) – Specify return values. If 0: return a list of the HedTags. If 1: return a list of the HedGroups containing the HedTags. If 2: return a list of tuples (HedTag, HedGroup) for the found tags.

Returns:

The contents of the list depends on the value of include_groups.

Return type:

list

HedString.find_tags_with_term(term, recursive=False, include_groups=2)

Find any tags that contain the given term.

Note: This can only find identified tags.

Parameters:
  • term (str) – A single term to search for.

  • recursive (bool) – If true, recursively check subgroups.

  • include_groups (0, 1 or 2) – Controls return values If 0: Return only tags. If 1: Return only groups. If 2 or any other value: Return both.

Return type:

list

HedString.find_top_level_tags(anchor_tags, include_groups=2)[source]

Find top level groups with an anchor tag.

A max of 1 tag located per top level group.

Parameters:
  • anchor_tags (container) – A list/set/etc. of short_base_tags to find groups by.

  • include_groups (0, 1 or 2) – Parameter indicating what return values to include. If 0: return only tags. If 1: return only groups. If 2 or any other value: return both.

Returns:

The returned result depends on include_groups.

Return type:

list

HedString.find_wildcard_tags(search_tags, recursive=False, include_groups=2)

Find the tags and their containing groups.

This searches tag.short_tag.casefold(), with an implicit wildcard on the end.

e.g. “Eve” will find Event, but not Sensory-event.

Parameters:
  • search_tags (container) – A container of the starts of short tags to search.

  • recursive (bool) – If True, also check subgroups.

  • include_groups (0, 1 or 2) – Specify return values. If 0: return a list of the HedTags. If 1: return a list of the HedGroups containing the HedTags. If 2: return a list of tuples (HedTag, HedGroup) for the found tags.

Returns:

The contents of the list depends on the value of include_groups.

Return type:

list

classmethod HedString.from_hed_strings(hed_strings)[source]

Factory for creating HedStrings via combination.

Parameters:

hed_strings (list or None) – A list of HedString objects to combine. This takes ownership of their children.

Returns:

The newly combined HedString.

Return type:

new_string(HedString)

HedString.get_all_groups(also_return_depth=False)

Return HedGroups, including descendants and self.

Parameters:

also_return_depth (bool) – If True, yield tuples (group, depth) rather than just groups.

Returns:

The list of all HedGroups in this group, including descendants and self.

Return type:

list

HedString.get_all_tags()

Return HedTags, including descendants.

Returns:

A list of all the tags in this group including descendants.

Return type:

list

HedString.get_as_form(tag_attribute)

Get the string corresponding to the specified form.

Parameters:

tag_attribute (str) – The hed_tag property to use to construct the string (usually short_tag or long_tag).

Returns:

The constructed string after transformation.

Return type:

str

HedString.get_as_indented(tag_attribute='short_tag')

Return the string as a multiline indented format.

Parameters:

tag_attribute (str) – The hed_tag property to use to construct the string (usually short_tag or long_tag).

Returns:

The indented string.

Return type:

formatted_hed (str)

HedString.get_as_long()

Return this HedGroup as a long tag string.

Returns:

The group as a string with all tags as long tags.

Return type:

str

HedString.get_as_original()[source]

Return the original form of this string.

Returns:

The string with all the tags in their original form.

Return type:

str

Notes

Potentially with some extraneous spaces removed on returned string.

HedString.get_as_short()

Return this HedGroup as a short tag string.

Returns:

The group as a string with all tags as short tags.

Return type:

str

HedString.get_first_group()

Return the first group in this HED string or group.

Useful for things like Def-expand where they only have a single group.

Raises a ValueError if there are no groups.

Returns:

The first group.

Return type:

HedGroup

HedString.get_original_hed_string()

Get the original HED string.

Returns:

The original string with no modification.

Return type:

str

HedString.groups()

Return the direct child groups of this group.

Returns:

All groups directly in this group, filtering out HedTag children.

Return type:

list

HedString.lower()

Convenience function, equivalent to str(self).lower().

HedString.remove(items_to_remove: Iterable[Union[HedTag, HedGroup]])

Remove any tags/groups in items_to_remove.

Parameters:

items_to_remove (list) – List of HedGroups and/or HedTags to remove by identity.

Notes

  • Any groups that become empty will also be pruned.

  • If you pass a child and parent group, the child will also be removed from the parent.

HedString.remove_definitions()[source]

Remove definition tags and groups from this string.

This does not validate definitions and will blindly removing invalid ones as well.

HedString.remove_refs()[source]

Remove any refs(tags contained entirely inside curly braces) from the string.

This does NOT validate the contents of the curly braces. This is only relevant when directly editing sidecar strings. Tools will naturally ignore these.

static HedString.replace(item_to_replace, new_contents)

Replace an existing tag or group.

Note: This is a static method that relies on the parent attribute of item_to_replace.

Parameters:
  • item_to_replace (HedTag or HedGroup) – The item to replace must exist or this will raise an error.

  • new_contents (HedTag or HedGroup) – Replacement contents.

Raises:
  • KeyError

    • item_to_replace does not exist.

  • AttributeError

    • item_to_replace has no parent set.

HedString.shrink_defs()[source]

Replace def-expand tags with def tags.

This does not validate them and will blindly shrink invalid ones as well.

Returns:

self

HedString.sort()

Sort the tags and groups in this HedString in a consistent order.

HedString.sorted()

Return a sorted copy of this HED group

Returns:

The sorted copy.

Return type:

sorted_copy (HedGroup)

static HedString.split_hed_string(hed_string)[source]

Split a HED string into delimiters and tags.

Parameters:

hed_string (str) – The HED string to split.

Returns:

A list of tuples where each tuple is (is_hed_tag, (start_pos, end_pos)).

Return type:

list

Notes

  • The tuple format is as follows
    • is_hed_tag (bool): A (possible) HED tag if True, delimiter if not.

    • start_pos (int): Index of start of string in hed_string.

    • end_pos (int): Index of end of string in hed_string.

  • This function does not validate tags or delimiters in any form.

static HedString.split_into_groups(hed_string, hed_schema, def_dict=None)[source]

Split the HED string into a parse tree.

Parameters:
  • hed_string (str) – A HED string consisting of tags and tag groups to be processed.

  • hed_schema (HedSchema) – HED schema to use to identify tags.

  • def_dict (DefinitionDict) – The definitions to identify.

Returns:

A list of HedTag and/or HedGroup.

Return type:

list

Raises:

ValueError

  • The string is significantly malformed, such as mismatched parentheses.

Notes

  • The parse tree consists of tag groups, tags, and delimiters.

HedString.tags()

Return the direct child tags of this group.

Returns:

All tags directly in this group, filtering out HedGroup children.

Return type:

list

HedString.validate(allow_placeholders=True, error_handler=None)[source]

Validate the string using the schema.

Parameters:
  • allow_placeholders (bool) – Allow placeholders in the string.

  • error_handler (ErrorHandler or None) – The error handler to use, creates a default one if none passed.

Returns:

A list of issues for HED string.

Return type:

issues (list of dict)

HedString.CLOSING_GROUP_CHARACTER = ')'
HedString.OPENING_GROUP_CHARACTER = '('
HedString.is_group

Always False since the underlying string is not a group with parentheses.

HedString.span

Return the source span.

Returns:

start index of the group (including parentheses) from the source string. int: end index of the group (including parentheses) from the source string.

Return type:

int