Skip to content

Commit

Permalink
refactor: remove Frame.uid field
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 4, 2024
1 parent 1778aea commit 11bf030
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ Other breaking changes:
- the `fromdict()` and `asdict()` methods in `raillabel.format` classes have been replaced with `from_json()` and `to_json` respectively
- `raillabel.format.Transform` fields have been changed by `pos -> position` and `quad -> quaternion` to make it more explicit
- `raillabel.format.FrameInterval` fields have been changed by `frame_start -> start` and `frame_end -> end` to make it more concise
- `raillabel.format.Frame.uid` field has been removed to avoid redundant information
5 changes: 0 additions & 5 deletions raillabel/format/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Frame:
Parameters
----------
uid: int
Number of the frame within the annotation file. Must be unique.
timestamp: decimal.Decimal, optional
Timestamp containing the Unix epoch time of the frame with up to nanosecond precision.
sensors: dict of raillabel.format.SensorReference, optional
Expand All @@ -42,7 +40,6 @@ class Frame:
"""

uid: int
timestamp: decimal.Decimal | None = None
sensors: dict[str, SensorReference] = field(default_factory=dict)
frame_data: dict[str, Num] = field(default_factory=dict)
Expand Down Expand Up @@ -71,7 +68,6 @@ def object_data(self) -> dict[str, dict[str, type[_ObjectAnnotation]]]:
@classmethod
def fromdict(
cls,
uid: str,
data_dict: dict,
objects: dict[str, Object],
sensors: dict[str, Sensor],
Expand All @@ -96,7 +92,6 @@ def fromdict(
"""
return Frame(
uid=int(uid),
timestamp=cls._timestamp_fromdict(data_dict),
sensors=cls._sensors_fromdict(data_dict, sensors),
frame_data=cls._frame_data_fromdict(data_dict, sensors),
Expand Down
8 changes: 4 additions & 4 deletions raillabel/format/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def frame_intervals(self, frames: dict[int, Frame]) -> list[FrameInterval]:
"""
frame_uids_containing_object = [
frame.uid for frame in frames.values() if self._is_object_in_frame(frame)
frame_uid for frame_uid, frame in frames.items() if self._is_object_in_frame(frame)
]

return FrameInterval.from_frame_uids(frame_uids_containing_object)
Expand Down Expand Up @@ -150,11 +150,11 @@ def _filtered_annotations(self, frame: Frame) -> list[t.Any]:

def _collect_pointer_ids_per_frame(self, frames: dict[int, Frame]) -> dict[int, set[str]]:
pointer_ids_per_frame: dict[int, set[str]] = {}
for frame in frames.values():
pointer_ids_per_frame[frame.uid] = set()
for frame_uid, frame in frames.items():
pointer_ids_per_frame[frame_uid] = set()

for annotation in self._filtered_annotations(frame):
pointer_ids_per_frame[frame.uid].add(annotation.name) # type: ignore
pointer_ids_per_frame[frame_uid].add(annotation.name) # type: ignore

return pointer_ids_per_frame

Expand Down
2 changes: 1 addition & 1 deletion raillabel/format/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _frames_fromdict(
) -> dict[int, Frame]:
frames = {}
for frame_uid, frame_dict in frames_dict.items():
frames[int(frame_uid)] = Frame.fromdict(frame_uid, frame_dict, objects, sensors)
frames[int(frame_uid)] = Frame.fromdict(frame_dict, objects, sensors)

return frames

Expand Down
11 changes: 2 additions & 9 deletions tests/test_raillabel/format/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def frame_dict(
@pytest.fixture
def frame(sensor_reference_camera, num, all_annotations) -> dict:
return Frame(
uid=0,
timestamp=Decimal("1632321743.100000072"),
sensors={sensor_reference_camera.sensor.uid: sensor_reference_camera},
frame_data={num.name: num},
Expand All @@ -55,7 +54,6 @@ def frame(sensor_reference_camera, num, all_annotations) -> dict:

def test_fromdict_sensors(sensor_reference_camera_dict, sensor_reference_camera, sensor_camera):
frame = Frame.fromdict(
uid=0,
data_dict={
"frame_properties": {
"timestamp": "1632321743.100000072",
Expand All @@ -66,14 +64,12 @@ def test_fromdict_sensors(sensor_reference_camera_dict, sensor_reference_camera,
objects={},
)

assert frame.uid == 0
assert frame.timestamp == Decimal("1632321743.100000072")
assert frame.sensors == {sensor_reference_camera.sensor.uid: sensor_reference_camera}


def test_fromdict_frame_data(num, num_dict, sensor_camera):
frame = Frame.fromdict(
uid=1,
data_dict={"frame_properties": {"frame_data": {"num": [num_dict]}}},
sensors={sensor_camera.uid: sensor_camera},
objects={},
Expand All @@ -91,7 +87,6 @@ def test_fromdict_annotations(
all_annotations,
):
frame = Frame.fromdict(
uid=2,
data_dict={
"objects": {
object_person.uid: object_data_person_dict,
Expand All @@ -113,7 +108,6 @@ def test_asdict_sensors(
sensor_reference_camera,
):
frame = Frame(
uid=0,
timestamp=Decimal("1632321743.100000072"),
sensors={sensor_reference_camera.sensor.uid: sensor_reference_camera},
)
Expand All @@ -127,15 +121,15 @@ def test_asdict_sensors(


def test_asdict_frame_data(num, num_dict):
frame = Frame(uid=0, frame_data={num.name: num})
frame = Frame(frame_data={num.name: num})

assert frame.asdict() == {"frame_properties": {"frame_data": {"num": [num_dict]}}}


def test_asdict_object_data(
object_data_person_dict, object_person, object_data_train_dict, object_train, all_annotations
):
frame = Frame(uid=0, annotations=all_annotations)
frame = Frame(annotations=all_annotations)

assert frame.asdict() == {
"objects": {
Expand All @@ -147,7 +141,6 @@ def test_asdict_object_data(

def test_object_data(object_person, object_train, bbox, cuboid, poly2d, poly3d, seg3d, bbox_train):
frame = Frame(
uid=2,
annotations={
bbox.uid: bbox,
poly2d.uid: poly2d,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_raillabel/format/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
import typing as t
from pathlib import Path
from uuid import UUID, uuid4
from uuid import uuid4

import pytest

Expand Down Expand Up @@ -405,11 +405,11 @@ def build_annotation(name: str, object: Object, attributes: dict = {}) -> t.Unio

def build_frame(uid: int, raw_object_data: dict[Object, list[t.Union[Bbox, Cuboid]]]) -> Frame:
annotations = {}
for object, object_data in raw_object_data.items():
for object_data in raw_object_data.values():
for annotation in object_data:
annotations[annotation.uid] = annotation

return Frame(uid=uid, annotations=annotations)
return Frame(annotations=annotations)


def build_object(type: str) -> Object:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_raillabel/format/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_fromdict_frames(
"coordinate_systems": coordinate_systems_dict,
"objects": objects_dict,
"frames": {
str(frame.uid): frame_dict,
"0": frame_dict,
},
"frame_intervals": [
{
Expand All @@ -208,7 +208,7 @@ def test_fromdict_frames(
)

assert scene.frames == {
frame.uid: frame,
0: frame,
}


Expand Down Expand Up @@ -300,7 +300,7 @@ def test_asdict_frames(
sensors=sensors,
objects=objects,
frames={
frame.uid: frame,
"0": frame,
},
)

Expand All @@ -311,7 +311,7 @@ def test_asdict_frames(
"coordinate_systems": coordinate_systems_dict,
"objects": objects_dict,
"frames": {
str(frame.uid): frame_dict,
"0": frame_dict,
},
"frame_intervals": [
{
Expand Down

0 comments on commit 11bf030

Please sign in to comment.