From 06c5b77e8188d0ba721214a605c4e47334d62804 Mon Sep 17 00:00:00 2001 From: Tobias Klockau Date: Mon, 11 Nov 2024 16:00:36 +0100 Subject: [PATCH] feat: implement Num.from_dict() --- raillabel/format/num.py | 39 ++++++++++------------ raillabel/json_format/num.py | 4 --- tests/test_raillabel/format/test_num.py | 44 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 tests/test_raillabel/format/test_num.py diff --git a/raillabel/format/num.py b/raillabel/format/num.py index 243c65b..d92aff6 100644 --- a/raillabel/format/num.py +++ b/raillabel/format/num.py @@ -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, + ) diff --git a/raillabel/json_format/num.py b/raillabel/json_format/num.py index 75d08d4..b51ac76 100644 --- a/raillabel/json_format/num.py +++ b/raillabel/json_format/num.py @@ -7,8 +7,6 @@ from pydantic import BaseModel -from .attributes import JSONAttributes - class JSONNum(BaseModel): """A number.""" @@ -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 diff --git a/tests/test_raillabel/format/test_num.py b/tests/test_raillabel/format/test_num.py new file mode 100644 index 0000000..dcde12d --- /dev/null +++ b/tests/test_raillabel/format/test_num.py @@ -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"])