From 8498db08d3bd396760dab82776435fb6b8a0040e Mon Sep 17 00:00:00 2001 From: MatthisCl <76970409+MatthisCl@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:35:52 +0100 Subject: [PATCH] Fix add existing tree to annotation (#1201) * add test and change hash function * change hash function * fix typo. * Use always id for hash value of Group. * Adapt __eq__ method for group and skeleton. * Update changelog. * Adapt equality check. * Remove __eq__ and __hash__ method in Skeleton class. * Remove unused import. * Add eq=False to attr.define to avoid default equal method. * Run linter. --------- Co-authored-by: markbader --- webknossos/Changelog.md | 3 +-- webknossos/tests/test_skeleton.py | 12 ++++++++++++ webknossos/webknossos/skeleton/group.py | 9 ++++++--- webknossos/webknossos/skeleton/skeleton.py | 5 +---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/webknossos/Changelog.md b/webknossos/Changelog.md index 7e4d877bc..e71a64f31 100644 --- a/webknossos/Changelog.md +++ b/webknossos/Changelog.md @@ -34,6 +34,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section - Refactored the PimsTiffReader to read the data directly from the tiff file without creating a memmap-able copy first. This greatly reduces the time and storage requirements for converting large tiff files. [#1212](https://github.com/scalableminds/webknossos-libs/pull/1212) ### Fixed +- Fixed an issue where adding existing trees to an annotation fails. [#1201](https://github.com/scalableminds/webknossos-libs/pull/1201) - Fixed unpickling of the SSL_Context to allow for a second or third pickling. [#1223](https://github.com/scalableminds/webknossos-libs/pull/1223) - Fixed offset error in upsample_cube job [#1209](https://github.com/scalableminds/webknossos-libs/pull/1209) @@ -50,7 +51,6 @@ For upgrade instructions, please check the respective _Breaking Changes_ section - Fixed pickling issue that has been introduced in 0.15.9. [#1218](https://github.com/scalableminds/webknossos-libs/pull/1218) - ## [0.15.10](https://github.com/scalableminds/webknossos-libs/releases/tag/v0.15.10) - 2024-11-25 [Commits](https://github.com/scalableminds/webknossos-libs/compare/v0.15.9...v0.15.10) @@ -58,7 +58,6 @@ For upgrade instructions, please check the respective _Breaking Changes_ section - Fixed pickling issue that has been introduced in 0.15.9. [#1218](https://github.com/scalableminds/webknossos-libs/pull/1218) - ## [0.15.9](https://github.com/scalableminds/webknossos-libs/releases/tag/v0.15.9) - 2024-11-25 [Commits](https://github.com/scalableminds/webknossos-libs/compare/v0.15.8...v0.15.9) diff --git a/webknossos/tests/test_skeleton.py b/webknossos/tests/test_skeleton.py index 2e5d13d14..261bec90d 100644 --- a/webknossos/tests/test_skeleton.py +++ b/webknossos/tests/test_skeleton.py @@ -438,3 +438,15 @@ def test_add_tree_with_obj_and_properties(tmp_path: Path) -> None: assert new_tree.color == (1, 2, 3, 1) skeleton_b.save(output_path) + + +def test_add_tree_with_group() -> None: + annotation = wk.Annotation( + name="my_annotation", dataset_name="my_dataset", voxel_size=(11, 11, 24) + ) + group = annotation.skeleton.add_group("a group") + tree = group.add_tree("a tree") + + skeleton_a = create_dummy_skeleton() + + skeleton_a.add_tree(tree) diff --git a/webknossos/webknossos/skeleton/group.py b/webknossos/webknossos/skeleton/group.py index fdda6a834..f6d861d55 100644 --- a/webknossos/webknossos/skeleton/group.py +++ b/webknossos/webknossos/skeleton/group.py @@ -1,5 +1,5 @@ import copy -from typing import TYPE_CHECKING, Iterator, Optional, Set, Tuple, Union, cast +from typing import TYPE_CHECKING, Any, Iterator, Optional, Set, Tuple, Union, cast import attr from boltons.strutils import unit_len @@ -220,7 +220,7 @@ def children(self) -> Iterator[GroupOrTree]: @property def trees(self) -> Iterator[Tree]: """Returns all (immediate) tree children as an iterator. - Use flattened_trees if you need also need trees within subgroups.""" + Use flattened_trees if you also need trees within subgroups.""" return (child for child in self._child_trees) @property @@ -430,5 +430,8 @@ def as_nml_group(self) -> wknml.Group: children=[g.as_nml_group() for g in self._child_groups], ) + def __eq__(self, other: Any) -> bool: + return type(other) is type(self) and self._id == other._id + def __hash__(self) -> int: - return self._id + return id(self) diff --git a/webknossos/webknossos/skeleton/skeleton.py b/webknossos/webknossos/skeleton/skeleton.py index 93f7a7fd8..faf0c3cde 100644 --- a/webknossos/webknossos/skeleton/skeleton.py +++ b/webknossos/webknossos/skeleton/skeleton.py @@ -12,7 +12,7 @@ Vector3 = Tuple[float, float, float] -@attr.define +@attr.define(eq=False) class Skeleton(Group): """A hierarchical representation of skeleton annotations in WEBKNOSSOS. @@ -281,6 +281,3 @@ def write(self, out_path: PathLike) -> None: """Deprecated. Use Skeleton.save instead.""" warn_deprecated("Skeleton.write", "skeleton.save") self.save(out_path) - - def __hash__(self) -> int: - return id(self)