Skip to content

Commit

Permalink
fix UTs, move logic to SkeletonCoordinate (singular)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur-encord committed Jan 2, 2025
1 parent 1319ee3 commit 740b5f1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
37 changes: 17 additions & 20 deletions encord/objects/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ class SkeletonCoordinate(BaseDTO):

visibility: Optional[Visibility] = None

@staticmethod
def from_dict(value: dict) -> SkeletonCoordinate:
visibility: Optional[str] = value.get("visibility")

return SkeletonCoordinate(
name=value["name"],
x=value["x"],
y=value["y"],
color=value["color"],
feature_hash=value["featureHash"],
value=value["value"],
visibility=Visibility[visibility.upper()] if visibility else Visibility.VISIBLE,
)


@dataclass(frozen=True)
class SkeletonCoordinates:
Expand Down Expand Up @@ -338,8 +352,6 @@ def from_dict(d: dict) -> SkeletonCoordinates:
"""
skeleton_dict = d["skeleton"]
values: List[SkeletonCoordinate] = []
visible = None

if isinstance(skeleton_dict, dict):
sorted_dict_value_tuples = sorted((int(key), value) for key, value in skeleton_dict.items())
Expand All @@ -348,25 +360,10 @@ def from_dict(d: dict) -> SkeletonCoordinates:
sorted_dict_values = list(skeleton_dict)
else:
raise LabelRowError(f"Invalid format for skeleton coordinates: {skeleton_dict}")
for value in sorted_dict_values:
if value.get("invisible") and value["invisible"]:
visible = Visibility.INVISIBLE
elif value.get("occluded") and value["occluded"]:
visible = Visibility.OCCLUDED
else:
visible = Visibility.VISIBLE
skeleton_coordinates = SkeletonCoordinate(
name=value["name"],
x=value["x"],
y=value["y"],
color=value["color"],
feature_hash=value["featureHash"],
value=value["value"],
visibility=visible,
)
values.append(skeleton_coordinates)

return SkeletonCoordinates(name=d["name"], values=values)
return SkeletonCoordinates(
name=d["name"], values=[SkeletonCoordinate.from_dict(value) for value in sorted_dict_values]
)

def to_dict(self, by_alias=True, exclude_none=True) -> Dict[str, Any]:
"""
Expand Down
12 changes: 8 additions & 4 deletions tests/objects/data/skeleton_coordinates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from encord.objects.coordinates import SkeletonCoordinate, SkeletonCoordinates
from encord.objects.coordinates import SkeletonCoordinate, SkeletonCoordinates, Visibility

ontology = {
"objects": [
Expand Down Expand Up @@ -106,6 +106,7 @@
"color": "#000000",
"value": "point_0",
"featureHash": "1wthOoHe",
"visibility": "visible",
},
"1": {
"x": 0.4649,
Expand All @@ -114,6 +115,7 @@
"color": "#000000",
"value": "point_1",
"featureHash": "KGp1oToz",
"visibility": "occluded",
},
"2": {
"x": 0.2356,
Expand All @@ -122,6 +124,7 @@
"color": "#000000",
"value": "point_2",
"featureHash": "OqR+F4dN",
"visibility": "invisible",
},
},
"manualAnnotation": True,
Expand All @@ -139,6 +142,7 @@
"object_actions": {},
"label_status": "LABELLED",
}

expected_coordinates = SkeletonCoordinates(
values=[
SkeletonCoordinate(
Expand All @@ -148,7 +152,7 @@
color="#000000",
feature_hash="1wthOoHe",
value="point_0",
visibility=None,
visibility=Visibility.VISIBLE,
),
SkeletonCoordinate(
x=0.4649,
Expand All @@ -157,7 +161,7 @@
color="#000000",
feature_hash="KGp1oToz",
value="point_1",
visibility=None,
visibility=Visibility.OCCLUDED,
),
SkeletonCoordinate(
x=0.2356,
Expand All @@ -166,7 +170,7 @@
color="#000000",
feature_hash="OqR+F4dN",
value="point_2",
visibility=None,
visibility=Visibility.INVISIBLE,
),
],
name="Triangle",
Expand Down
29 changes: 28 additions & 1 deletion tests/objects/test_coordinates.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from copy import copy

from encord.objects import Shape
from encord.objects.coordinates import ACCEPTABLE_COORDINATES_FOR_ONTOLOGY_ITEMS
from encord.objects.coordinates import ACCEPTABLE_COORDINATES_FOR_ONTOLOGY_ITEMS, SkeletonCoordinate, Visibility


def test_acceptable_coordinates_for_ontology_items() -> None:
Expand All @@ -10,3 +10,30 @@ def test_acceptable_coordinates_for_ontology_items() -> None:
assert shape in all_mappings
del all_mappings[shape]
assert not all_mappings


def test_skeleton_coordinates_without_visibility_works() -> None:
invisible_pnt = SkeletonCoordinate.from_dict(
{
"x": 0.2356,
"y": 0.9396,
"name": "point_2",
"color": "#000000",
"value": "point_2",
"featureHash": "OqR+F4dN",
"visibility": "invisible",
}
)
assert invisible_pnt.visibility is Visibility.INVISIBLE

visible_pnt = SkeletonCoordinate.from_dict(
{
"x": 0.2356,
"y": 0.9396,
"name": "point_2",
"color": "#000000",
"value": "point_2",
"featureHash": "OqR+F4dN",
}
)
assert visible_pnt.visibility is Visibility.VISIBLE
12 changes: 7 additions & 5 deletions tests/objects/test_label_structure_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,11 @@ def test_skeleton_template_coordinates():
assert len(obj_instances) == 1

obj_instance = obj_instances[0]
ann = obj_instance.get_annotations()[0]
assert ann.coordinates == skeleton_coordinates.expected_coordinates
annotation = obj_instance.get_annotations()[0]
assert annotation.coordinates == skeleton_coordinates.expected_coordinates

label_dict = label_row.to_encord_dict()
label_dict_obj = list(skeleton_coordinates.labels["data_units"].values())[0]["labels"]["objects"][0]
origin_obj = list(label_dict["data_units"].values())[0]["labels"]["objects"][0]
assert origin_obj["skeleton"] == label_dict_obj["skeleton"]
skeleton_dict = list(label_dict["data_units"].values())[0]["labels"]["objects"][0]
expected_skeleton_dict = list(skeleton_coordinates.labels["data_units"].values())[0]["labels"]["objects"][0]

assert skeleton_dict["skeleton"] == expected_skeleton_dict["skeleton"]

0 comments on commit 740b5f1

Please sign in to comment.