Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 28 additions & 8 deletions hed/models/hed_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,46 @@ def check_if_in_original(self, tag_or_group):

return self._check_in_group(tag_or_group, final_list)

def replace(self, item_to_replace, new_contents):
@staticmethod
def 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

:raises AttributeError:
- item_to_replace has no parent set
"""
parent = item_to_replace._parent
parent._replace(item_to_replace=item_to_replace, new_contents=new_contents)

def _replace(self, item_to_replace, new_contents):
""" Replace an existing tag or group.

Parameters:
item_to_replace (HedTag or HedGroup): The item to replace must exist and be a direct child,
or this will raise an error.
new_contents (HedTag or HedGroup): Replacement contents.

:raises KeyError:
- item_to_replace does not exist
"""
if self._original_children is self.children:
self._original_children = self.children.copy()

replace_index = -1
for i, child in enumerate(self.children):
if item_to_replace is child:
replace_index = i
break
self.children[replace_index] = new_contents
new_contents._parent = self
self.children[i] = new_contents
new_contents._parent = self
return

raise KeyError(f"The tag {item_to_replace} not found in the group.")

def remove(self, items_to_remove: Iterable[Union[HedTag, 'HedGroup']]):
""" Remove any tags/groups in items_to_remove.
Expand Down Expand Up @@ -476,7 +496,7 @@ def find_def_tags(self, recursive=False, include_groups=3):
for group in groups:
def_tags += self._get_def_tags_from_group(group)
else:
def_tags = self._get_def_tags_from_group(self)
def_tags = self._get_def_tags_from_group(self)

if include_groups == 0 or include_groups == 1 or include_groups == 2:
return [tag[include_groups] for tag in def_tags]
Expand Down Expand Up @@ -525,4 +545,4 @@ def find_tags_with_term(self, term, recursive=False, include_groups=2):

if include_groups == 0 or include_groups == 1:
return [tag[include_groups] for tag in found_tags]
return found_tags
return found_tags
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def test_do_op_options(self):
self.assertIsInstance(dispatch.summary_dicts[sum_op3.summary_name], HedTagSummary)
counts3 = dispatch.summary_dicts[sum_op3.summary_name].summary_dict['subj2_run1']
self.assertIsInstance(counts3, HedTagCounts)
self.assertEqual(32, len(counts3.tag_dict))
# self.assertIn('event-context', counts3.tag_dict) TODO: Fix this
self.assertEqual(33, len(counts3.tag_dict))
self.assertIn('event-context', counts3.tag_dict)
self.assertNotIn('def', counts3.tag_dict)
self.assertNotIn('task', counts3.tag_dict)
self.assertNotIn('condition-variable', counts3.tag_dict)
Expand Down