Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sort function. Might need tweaking later. Minor test fixes. #576

Merged
merged 3 commits into from
Jan 27, 2023
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
25 changes: 19 additions & 6 deletions hed/models/hed_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,35 @@ def copy(self):
return return_copy

def sort(self):
""" Sort the tags and groups in this HedString in a consistent order.

Returns:
self
"""
self.sorted(update_self=True)
return self

def sorted(self, update_self=False):
""" Returns a sorted copy of this hed group as a list of it's children

Parameters:
update_self (bool): If True, update the contents of this group to be sorted as well.

Returns:
list: The list of all tags in this group, with subgroups being returned as further nested lists
"""
output_list = []
queue_list = list(self.children)
for child in queue_list:
if isinstance(child, HedTag):
output_list.append(child)
output_list.append((child, child))
else:
output_list.append(child.sorted(update_self))
output_list.append((child, child.sorted(update_self)))

output_list.sort(key=lambda x: str(x))
output_list.sort(key=lambda x: str(x[0]))
if update_self:
self._contents = output_list
return output_list
self._children = [x[0] for x in output_list]
return [x[1] for x in output_list]

@property
def children(self):
Expand Down Expand Up @@ -534,4 +547,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
9 changes: 8 additions & 1 deletion hed/validator/hed_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ def _check_for_duplicate_groups_recursive(self, sorted_group, validation_issues)
validation_issues += ErrorHandler.format_error(error_code, child)
else:
error_code = ValidationErrors.HED_TAG_REPEATED_GROUP
validation_issues += ErrorHandler.format_error(error_code, child[0]._parent)
found_group = child
base_steps_up = 0
while isinstance(found_group, list):
found_group = found_group[0]
base_steps_up += 1
for _ in range(base_steps_up):
found_group = found_group._parent
validation_issues += ErrorHandler.format_error(error_code, found_group)
if not isinstance(child, HedTag):
self._check_for_duplicate_groups_recursive(child, validation_issues)
prev_child = child
Expand Down
35 changes: 0 additions & 35 deletions tests/data/tests_outputtest_sidecar_save.json

This file was deleted.

2 changes: 1 addition & 1 deletion tests/models/test_sidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_duplicate_def(self):

def test_save_load(self):
sidecar = Sidecar(self.json_def_filename)
save_filename = self.base_output_folder + "test_sidecar_save.json"
save_filename = os.path.join(self.base_output_folder, "test_sidecar_save.json")
sidecar.save_as_json(save_filename)

reloaded_sidecar = Sidecar(save_filename)
Expand Down
6 changes: 3 additions & 3 deletions tests/validator/test_tag_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ def test_no_duplicates(self):
'legalDuplicate': [],
'noDuplicate': [],
'duplicateGroup': self.format_error(ValidationErrors.HED_TAG_REPEATED_GROUP,
group=HedString("(Man-made-object/VehicleTrain, Sensory-event)")),
group=HedString("(Sensory-event, Man-made-object/VehicleTrain)")),
'duplicateSubGroup': self.format_error(ValidationErrors.HED_TAG_REPEATED_GROUP,
group=HedString("(Event, (Man-made-object/VehicleTrain, Sensory-event))")),
group=HedString("(Event, (Sensory-event, Man-made-object/VehicleTrain))")),
'duplicateSubGroupF': self.format_error(ValidationErrors.HED_TAG_REPEATED_GROUP,
group=HedString(
"((Man-made-object/VehicleTrain, Sensory-event), Event)")),
"((Sensory-event, Man-made-object/VehicleTrain), Event)")),
}
self.validator_syntactic(test_strings, expected_results, expected_issues, False)

Expand Down