From 1b4f6eae826b834d6aeedb144e00fbc9e9200cfa Mon Sep 17 00:00:00 2001 From: IanCa Date: Tue, 29 Aug 2023 19:13:59 -0500 Subject: [PATCH] Make HedGroup.replace work on non direct children --- hed/models/hed_group.py | 36 ++++++++++++++----- .../operations/test_summarize_hed_tags_op.py | 4 +-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/hed/models/hed_group.py b/hed/models/hed_group.py index f0739e92c..b152ca2f9 100644 --- a/hed/models/hed_group.py +++ b/hed/models/hed_group.py @@ -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. @@ -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] @@ -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 \ No newline at end of file + return found_tags diff --git a/tests/tools/remodeling/operations/test_summarize_hed_tags_op.py b/tests/tools/remodeling/operations/test_summarize_hed_tags_op.py index 3e2a2d508..3e1c1d128 100644 --- a/tests/tools/remodeling/operations/test_summarize_hed_tags_op.py +++ b/tests/tools/remodeling/operations/test_summarize_hed_tags_op.py @@ -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)