From 4f0ef6d849e5b714325fd07b6f32c51b08dd4b4f Mon Sep 17 00:00:00 2001 From: Tobias Klockau Date: Wed, 6 Sep 2023 09:43:55 +0200 Subject: [PATCH] refactor: replace all raillabel format UUIDs with str --- raillabel/format/raillabel/frame.py | 10 +++++----- raillabel/format/raillabel/object.py | 3 +-- raillabel/format/raillabel/scene.py | 5 ++--- tests/test_raillabel/format/raillabel/test_object.py | 6 +++--- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/raillabel/format/raillabel/frame.py b/raillabel/format/raillabel/frame.py index 39281af..1abd63d 100644 --- a/raillabel/format/raillabel/frame.py +++ b/raillabel/format/raillabel/frame.py @@ -36,19 +36,19 @@ class Frame: Read-Only Attributes -------------------- - object_data: dict[str, dict[UUID, _ObjectAnnotation subclass]] + object_data: dict[str, dict[str, _ObjectAnnotation subclass]] Annotations categorized by object. Keys are object uids and values are the annotations - as a dict, that have the object. + as a dict, that are part of the object. """ uid: int timestamp: t.Optional[decimal.Decimal] = None sensors: t.Dict[str, SensorReference] = field(default_factory=dict) frame_data: t.Dict[str, Num] = field(default_factory=dict) - annotations: t.Dict[uuid.UUID, t.Type[_ObjectAnnotation]] = field(default_factory=dict) + annotations: t.Dict[str, t.Type[_ObjectAnnotation]] = field(default_factory=dict) @property - def object_data(self) -> t.Dict[str, t.Dict[uuid.UUID, t.Type[_ObjectAnnotation]]]: + def object_data(self) -> t.Dict[str, t.Dict[str, t.Type[_ObjectAnnotation]]]: """Return annotations categorized by Object-Id. Returns @@ -221,7 +221,7 @@ def _objects_fromdict( for annotation in object_annotations: if annotation.uid in annotations: cls._issue_duplicate_annotation_uid_warning(annotation.uid, frame_id) - annotation.uid = uuid.uuid4() + annotation.uid = str(uuid.uuid4()) annotations[annotation.uid] = annotation diff --git a/raillabel/format/raillabel/object.py b/raillabel/format/raillabel/object.py index c06beb5..7e3ce24 100644 --- a/raillabel/format/raillabel/object.py +++ b/raillabel/format/raillabel/object.py @@ -3,7 +3,6 @@ import typing as t from dataclasses import dataclass -from uuid import UUID from _collections_abc import dict_values @@ -143,7 +142,7 @@ def _object_data_pointers_asdict( } def _is_object_in_frame(self, frame: "Frame") -> bool: - return UUID(self.uid) in frame.object_data or str(self.uid) in frame.object_data + return self.uid in frame.object_data def _filtered_annotations(self, frame: "Frame") -> dict_values: return [ann for ann in frame.annotations.values() if ann.object.uid == self.uid] diff --git a/raillabel/format/raillabel/scene.py b/raillabel/format/raillabel/scene.py index d45d7c8..763dd77 100644 --- a/raillabel/format/raillabel/scene.py +++ b/raillabel/format/raillabel/scene.py @@ -2,7 +2,6 @@ # SPDX-License-Identifier: Apache-2.0 import typing as t -import uuid from dataclasses import dataclass, field from ... import exceptions @@ -37,7 +36,7 @@ class Scene: metadata: Metadata sensors: t.Dict[str, Sensor] = field(default_factory=dict) - objects: t.Dict[uuid.UUID, Object] = field(default_factory=dict) + objects: t.Dict[str, Object] = field(default_factory=dict) frames: t.Dict[int, Frame] = field(default_factory=dict) @property @@ -223,7 +222,7 @@ def _coordinate_systems_asdict(self, sensors: t.Dict[str, Sensor]) -> dict: return coordinate_systems - def _objects_asdict(self, objects: t.Dict[uuid.UUID, Object], calculate_pointers: bool) -> dict: + def _objects_asdict(self, objects: t.Dict[str, Object], calculate_pointers: bool) -> dict: if calculate_pointers: return {str(uid): object.asdict(self.frames) for uid, object in objects.items()} diff --git a/tests/test_raillabel/format/raillabel/test_object.py b/tests/test_raillabel/format/raillabel/test_object.py index e3a489a..08a094c 100644 --- a/tests/test_raillabel/format/raillabel/test_object.py +++ b/tests/test_raillabel/format/raillabel/test_object.py @@ -385,7 +385,7 @@ def build_annotation(name: str, object: Object, attributes: dict={}) -> t.Union[ if ann_type == "bbox": return Bbox( - uid=uuid4(), + uid=str(uuid4()), object=object, attributes=attributes, sensor=sensor, @@ -395,7 +395,7 @@ def build_annotation(name: str, object: Object, attributes: dict={}) -> t.Union[ elif ann_type == "cuboid": return Cuboid( - uid=uuid4(), + uid=str(uuid4()), object=object, attributes=attributes, sensor=sensor, @@ -417,7 +417,7 @@ def build_frame(uid: int, raw_object_data: t.Dict[Object, t.List[t.Union[Bbox, C def build_object(type: str) -> Object: return Object( - uid=uuid4(), + uid=str(uuid4()), name=f"{type}_{str(random.randint(0, 9999)).zfill(4)}", type=type )