Skip to content

Commit

Permalink
feat: implement Num.from_dict()
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 11, 2024
1 parent 4e7b08a commit 06c5b77
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
39 changes: 18 additions & 21 deletions raillabel/format/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@

from dataclasses import dataclass

from .sensor import Sensor
from raillabel.json_format import JSONNum


@dataclass
class Num:
"""A number.
"""A number."""

Parameters
----------
uid: str
This a string representing the unique universal identifier of the annotation.
name: str
Human readable name describing the annotation.
val: int or float
This is the value of the number object.
attributes: dict, optional
Attributes of the annotation. Dict keys are the uid str of the attribute, values are the
attribute values. Default is {}.
sensor: raillabel.format.Sensor
A reference to the sensor, this value is represented in. Default is None.
"""

uid: str
name: str
val: int | float
sensor: Sensor
"Human readable name describing the annotation."

val: float
"This is the value of the number object."

sensor: str | None
"A reference to the sensor, this value is represented in."

@classmethod
def from_json(cls, json: JSONNum) -> Num:
"""Construct an instant of this class from RailLabel JSON data."""
return Num(
name=json.name,
val=json.val,
sensor=json.coordinate_system,
)
4 changes: 0 additions & 4 deletions raillabel/json_format/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from pydantic import BaseModel

from .attributes import JSONAttributes


class JSONNum(BaseModel):
"""A number."""
Expand All @@ -25,5 +23,3 @@ class JSONNum(BaseModel):

uid: UUID | None = None
"This is a string encoding the Universal Unique identifyer of the annotation."

attributes: JSONAttributes | None = None
44 changes: 44 additions & 0 deletions tests/test_raillabel/format/test_num.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from uuid import UUID

import pytest

from raillabel.format import Num
from raillabel.json_format import JSONNum

# == Fixtures =========================


@pytest.fixture
def num_json() -> JSONNum:
return JSONNum(
uid="78f0ad89-2750-4a30-9d66-44c9da73a714",
name="velocity",
val=49.21321,
coordinate_system="gps_imu",
)


@pytest.fixture
def num() -> Num:
return Num(
sensor="gps_imu",
name="velocity",
val=49.21321,
)


# == Tests ============================


def test_from_json(num, num_json):
actual = Num.from_json(num_json)
assert actual == num


if __name__ == "__main__":
pytest.main([__file__, "-v"])

0 comments on commit 06c5b77

Please sign in to comment.