From 342bf96f3e9c49268075fe5b1198734f09c8483d Mon Sep 17 00:00:00 2001 From: unexcellent <> Date: Thu, 14 Nov 2024 10:02:33 +0100 Subject: [PATCH] refactor: create parent class for all json format classes --- raillabel/json_format/_json_format_base.py | 8 ++++++++ raillabel/json_format/attributes.py | 5 ++--- raillabel/json_format/bbox.py | 5 ++--- raillabel/json_format/boolean_attribute.py | 4 ++-- raillabel/json_format/coordinate_system.py | 5 ++--- raillabel/json_format/cuboid.py | 5 ++--- raillabel/json_format/element_data_pointer.py | 5 ++--- raillabel/json_format/frame.py | 9 ++++----- raillabel/json_format/frame_interval.py | 4 ++-- raillabel/json_format/num.py | 4 ++-- raillabel/json_format/num_attribute.py | 4 ++-- raillabel/json_format/object.py | 5 ++--- raillabel/json_format/object_data.py | 7 +++---- raillabel/json_format/poly2d.py | 5 ++--- raillabel/json_format/poly3d.py | 5 ++--- raillabel/json_format/scene.py | 7 +++---- raillabel/json_format/stream_camera.py | 8 ++++---- raillabel/json_format/stream_other.py | 4 ++-- raillabel/json_format/stream_radar.py | 8 ++++---- raillabel/json_format/stream_sync.py | 8 ++++---- raillabel/json_format/text_attribute.py | 4 ++-- raillabel/json_format/transform_data.py | 4 ++-- raillabel/json_format/vec.py | 5 ++--- raillabel/json_format/vec_attribute.py | 4 ++-- 24 files changed, 64 insertions(+), 68 deletions(-) create mode 100644 raillabel/json_format/_json_format_base.py diff --git a/raillabel/json_format/_json_format_base.py b/raillabel/json_format/_json_format_base.py new file mode 100644 index 0000000..5095d77 --- /dev/null +++ b/raillabel/json_format/_json_format_base.py @@ -0,0 +1,8 @@ +# Copyright DB InfraGO AG and contributors +# SPDX-License-Identifier: Apache-2.0 + +from pydantic import BaseModel + + +class _JSONFormatBase(BaseModel, extra="forbid"): + pass diff --git a/raillabel/json_format/attributes.py b/raillabel/json_format/attributes.py index 68aaacb..30846e6 100644 --- a/raillabel/json_format/attributes.py +++ b/raillabel/json_format/attributes.py @@ -3,15 +3,14 @@ from __future__ import annotations -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .boolean_attribute import JSONBooleanAttribute from .num_attribute import JSONNumAttribute from .text_attribute import JSONTextAttribute from .vec_attribute import JSONVecAttribute -class JSONAttributes(BaseModel, extra="forbid"): +class JSONAttributes(_JSONFormatBase): """Attributes is the alias of element data that can be nested inside geometric object data. For example, a certain bounding box can have attributes related to its score, visibility, etc. diff --git a/raillabel/json_format/bbox.py b/raillabel/json_format/bbox.py index 944db2c..7fd9f6a 100644 --- a/raillabel/json_format/bbox.py +++ b/raillabel/json_format/bbox.py @@ -5,12 +5,11 @@ from uuid import UUID -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .attributes import JSONAttributes -class JSONBbox(BaseModel, extra="forbid"): +class JSONBbox(_JSONFormatBase): """A 2D bounding box is defined as a 4-dimensional vector [x, y, w, h]. [x, y] is the center of the bounding box and [w, h] represent the width (horizontal, diff --git a/raillabel/json_format/boolean_attribute.py b/raillabel/json_format/boolean_attribute.py index 91c86ea..a41ca11 100644 --- a/raillabel/json_format/boolean_attribute.py +++ b/raillabel/json_format/boolean_attribute.py @@ -3,10 +3,10 @@ from __future__ import annotations -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONBooleanAttribute(BaseModel, extra="forbid"): +class JSONBooleanAttribute(_JSONFormatBase): """A boolean attribute.""" name: str diff --git a/raillabel/json_format/coordinate_system.py b/raillabel/json_format/coordinate_system.py index fedc174..899581d 100644 --- a/raillabel/json_format/coordinate_system.py +++ b/raillabel/json_format/coordinate_system.py @@ -5,12 +5,11 @@ from typing import Literal -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .transform_data import JSONTransformData -class JSONCoordinateSystem(BaseModel, extra="forbid"): +class JSONCoordinateSystem(_JSONFormatBase): """A 3D reference frame.""" parent: Literal["base", ""] diff --git a/raillabel/json_format/cuboid.py b/raillabel/json_format/cuboid.py index a58431a..0831add 100644 --- a/raillabel/json_format/cuboid.py +++ b/raillabel/json_format/cuboid.py @@ -5,12 +5,11 @@ from uuid import UUID -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .attributes import JSONAttributes -class JSONCuboid(BaseModel, extra="forbid"): +class JSONCuboid(_JSONFormatBase): """A cuboid or 3D bounding box. It is defined by the position of its center, the rotation in 3D, and its dimensions. diff --git a/raillabel/json_format/element_data_pointer.py b/raillabel/json_format/element_data_pointer.py index 745c9c0..d0dea1d 100644 --- a/raillabel/json_format/element_data_pointer.py +++ b/raillabel/json_format/element_data_pointer.py @@ -5,12 +5,11 @@ from typing import Literal -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .frame_interval import JSONFrameInterval -class JSONElementDataPointer(BaseModel, extra="forbid"): +class JSONElementDataPointer(_JSONFormatBase): """A pointer to element data of elements. It is indexed by 'name', and containing information about the element data type, for example, diff --git a/raillabel/json_format/frame.py b/raillabel/json_format/frame.py index cfc1386..2ec6334 100644 --- a/raillabel/json_format/frame.py +++ b/raillabel/json_format/frame.py @@ -6,14 +6,13 @@ from decimal import Decimal from uuid import UUID -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .num import JSONNum from .object_data import JSONObjectData from .stream_sync import JSONStreamSync -class JSONFrame(BaseModel, extra="forbid"): +class JSONFrame(_JSONFormatBase): """A frame is a container of dynamic, timewise, information.""" frame_properties: JSONFrameProperties | None = None @@ -24,7 +23,7 @@ class JSONFrame(BaseModel, extra="forbid"): strings containing 32 bytes UUIDs. Object values contain an 'object_data' JSON object.""" -class JSONFrameProperties(BaseModel, extra="forbid"): +class JSONFrameProperties(_JSONFormatBase): """Container of frame information other than annotations.""" timestamp: Decimal | str | None = None @@ -38,7 +37,7 @@ class JSONFrameProperties(BaseModel, extra="forbid"): "Additional data to describe attributes of the frame (like GPS position)." -class JSONFrameData(BaseModel, extra="forbid"): +class JSONFrameData(_JSONFormatBase): """Additional data to describe attributes of the frame (like GPS position).""" num: list[JSONNum] | None = None diff --git a/raillabel/json_format/frame_interval.py b/raillabel/json_format/frame_interval.py index d7f9034..984065c 100644 --- a/raillabel/json_format/frame_interval.py +++ b/raillabel/json_format/frame_interval.py @@ -3,10 +3,10 @@ from __future__ import annotations -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONFrameInterval(BaseModel, extra="forbid"): +class JSONFrameInterval(_JSONFormatBase): """A frame interval defines a starting and ending frame number as a closed interval. That means the interval includes the limit frame numbers. diff --git a/raillabel/json_format/num.py b/raillabel/json_format/num.py index be9089b..ce67b9f 100644 --- a/raillabel/json_format/num.py +++ b/raillabel/json_format/num.py @@ -5,10 +5,10 @@ from uuid import UUID -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONNum(BaseModel, extra="forbid"): +class JSONNum(_JSONFormatBase): """A number.""" name: str diff --git a/raillabel/json_format/num_attribute.py b/raillabel/json_format/num_attribute.py index e829712..edfdfcd 100644 --- a/raillabel/json_format/num_attribute.py +++ b/raillabel/json_format/num_attribute.py @@ -3,10 +3,10 @@ from __future__ import annotations -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONNumAttribute(BaseModel, extra="forbid"): +class JSONNumAttribute(_JSONFormatBase): """A number attribute.""" name: str diff --git a/raillabel/json_format/object.py b/raillabel/json_format/object.py index 514f1e8..777214e 100644 --- a/raillabel/json_format/object.py +++ b/raillabel/json_format/object.py @@ -3,13 +3,12 @@ from __future__ import annotations -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .element_data_pointer import JSONElementDataPointer from .frame_interval import JSONFrameInterval -class JSONObject(BaseModel, extra="forbid"): +class JSONObject(_JSONFormatBase): """An object is the main type of annotation element. Object is designed to represent spatiotemporal entities, such as physical objects in the real diff --git a/raillabel/json_format/object_data.py b/raillabel/json_format/object_data.py index 04609e7..68d4ba9 100644 --- a/raillabel/json_format/object_data.py +++ b/raillabel/json_format/object_data.py @@ -3,8 +3,7 @@ from __future__ import annotations -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .bbox import JSONBbox from .cuboid import JSONCuboid from .poly2d import JSONPoly2d @@ -12,13 +11,13 @@ from .vec import JSONVec -class JSONObjectData(BaseModel, extra="forbid"): +class JSONObjectData(_JSONFormatBase): """Container of annotations of an object in a frame.""" object_data: JSONAnnotations -class JSONAnnotations(BaseModel, extra="forbid"): +class JSONAnnotations(_JSONFormatBase): """Container of the annotations by type.""" bbox: list[JSONBbox] | None = None diff --git a/raillabel/json_format/poly2d.py b/raillabel/json_format/poly2d.py index 5ccda8c..3a0ddcd 100644 --- a/raillabel/json_format/poly2d.py +++ b/raillabel/json_format/poly2d.py @@ -6,12 +6,11 @@ from typing import Literal from uuid import UUID -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .attributes import JSONAttributes -class JSONPoly2d(BaseModel, extra="forbid"): +class JSONPoly2d(_JSONFormatBase): """A 2D polyline defined as a sequence of 2D points.""" name: str diff --git a/raillabel/json_format/poly3d.py b/raillabel/json_format/poly3d.py index 714204a..b185c93 100644 --- a/raillabel/json_format/poly3d.py +++ b/raillabel/json_format/poly3d.py @@ -5,12 +5,11 @@ from uuid import UUID -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .attributes import JSONAttributes -class JSONPoly3d(BaseModel, extra="forbid"): +class JSONPoly3d(_JSONFormatBase): """A 3D polyline defined as a sequence of 3D points.""" name: str diff --git a/raillabel/json_format/scene.py b/raillabel/json_format/scene.py index 36135d6..1a618b0 100644 --- a/raillabel/json_format/scene.py +++ b/raillabel/json_format/scene.py @@ -5,8 +5,7 @@ from uuid import UUID -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .coordinate_system import JSONCoordinateSystem from .frame import JSONFrame from .frame_interval import JSONFrameInterval @@ -17,13 +16,13 @@ from .stream_radar import JSONStreamRadar -class JSONScene(BaseModel, extra="forbid"): +class JSONScene(_JSONFormatBase): """Root RailLabel object.""" openlabel: JSONSceneContent -class JSONSceneContent(BaseModel, extra="forbid"): +class JSONSceneContent(_JSONFormatBase): """Container of all scene content.""" metadata: JSONMetadata diff --git a/raillabel/json_format/stream_camera.py b/raillabel/json_format/stream_camera.py index 1541c64..fbcd980 100644 --- a/raillabel/json_format/stream_camera.py +++ b/raillabel/json_format/stream_camera.py @@ -5,10 +5,10 @@ from typing import Literal -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONStreamCamera(BaseModel, extra="forbid"): +class JSONStreamCamera(_JSONFormatBase): """A stream describes the source of a data sequence, usually a sensor. This specific object contains the intrinsics of a camera sensor. @@ -27,13 +27,13 @@ class JSONStreamCamera(BaseModel, extra="forbid"): "Description of the stream." -class JSONStreamCameraProperties(BaseModel, extra="forbid"): +class JSONStreamCameraProperties(_JSONFormatBase): """Intrinsic calibration of the stream.""" intrinsics_pinhole: JSONIntrinsicsPinhole -class JSONIntrinsicsPinhole(BaseModel, extra="forbid"): +class JSONIntrinsicsPinhole(_JSONFormatBase): """JSON object defining an instance of the intrinsic parameters of a pinhole camera.""" camera_matrix: tuple[ diff --git a/raillabel/json_format/stream_other.py b/raillabel/json_format/stream_other.py index 75fd241..27fee1b 100644 --- a/raillabel/json_format/stream_other.py +++ b/raillabel/json_format/stream_other.py @@ -5,10 +5,10 @@ from typing import Literal -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONStreamOther(BaseModel, extra="forbid"): +class JSONStreamOther(_JSONFormatBase): """A stream describes the source of a data sequence, usually a sensor. This specific object describes a sensor without intrinsic calibration. diff --git a/raillabel/json_format/stream_radar.py b/raillabel/json_format/stream_radar.py index 1ac48e3..67dfceb 100644 --- a/raillabel/json_format/stream_radar.py +++ b/raillabel/json_format/stream_radar.py @@ -5,10 +5,10 @@ from typing import Literal -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONStreamRadar(BaseModel, extra="forbid"): +class JSONStreamRadar(_JSONFormatBase): """A stream describes the source of a data sequence, usually a sensor. This specific object contains the intrinsics of a radar sensor. @@ -27,13 +27,13 @@ class JSONStreamRadar(BaseModel, extra="forbid"): "Description of the stream." -class JSONStreamRadarProperties(BaseModel, extra="forbid"): +class JSONStreamRadarProperties(_JSONFormatBase): """Intrinsic calibration of the stream.""" intrinsics_radar: JSONIntrinsicsRadar -class JSONIntrinsicsRadar(BaseModel, extra="forbid"): +class JSONIntrinsicsRadar(_JSONFormatBase): """JSON object defining an instance of the intrinsic parameters of a radar.""" resolution_px_per_m: float diff --git a/raillabel/json_format/stream_sync.py b/raillabel/json_format/stream_sync.py index 5e48974..0c7c95f 100644 --- a/raillabel/json_format/stream_sync.py +++ b/raillabel/json_format/stream_sync.py @@ -5,23 +5,23 @@ from decimal import Decimal -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONStreamSync(BaseModel, extra="forbid"): +class JSONStreamSync(_JSONFormatBase): """Syncronization information of a stream in a frame.""" stream_properties: JSONStreamSyncProperties uri: str | None = None -class JSONStreamSyncProperties(BaseModel, extra="forbid"): +class JSONStreamSyncProperties(_JSONFormatBase): """The sync information.""" sync: JSONStreamSyncTimestamp -class JSONStreamSyncTimestamp(BaseModel, extra="forbid"): +class JSONStreamSyncTimestamp(_JSONFormatBase): """The timestamp of a stream sync.""" timestamp: Decimal | str diff --git a/raillabel/json_format/text_attribute.py b/raillabel/json_format/text_attribute.py index 24cc389..ddccbbc 100644 --- a/raillabel/json_format/text_attribute.py +++ b/raillabel/json_format/text_attribute.py @@ -3,10 +3,10 @@ from __future__ import annotations -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONTextAttribute(BaseModel, extra="forbid"): +class JSONTextAttribute(_JSONFormatBase): """A text attribute.""" name: str diff --git a/raillabel/json_format/transform_data.py b/raillabel/json_format/transform_data.py index 4d5a235..c23addb 100644 --- a/raillabel/json_format/transform_data.py +++ b/raillabel/json_format/transform_data.py @@ -3,10 +3,10 @@ from __future__ import annotations -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONTransformData(BaseModel, extra="forbid"): +class JSONTransformData(_JSONFormatBase): """The translation and rotation of one coordinate system to another.""" translation: tuple[float, float, float] diff --git a/raillabel/json_format/vec.py b/raillabel/json_format/vec.py index d9dec89..1d81162 100644 --- a/raillabel/json_format/vec.py +++ b/raillabel/json_format/vec.py @@ -6,12 +6,11 @@ from typing import Literal from uuid import UUID -from pydantic import BaseModel - +from ._json_format_base import _JSONFormatBase from .attributes import JSONAttributes -class JSONVec(BaseModel, extra="forbid"): +class JSONVec(_JSONFormatBase): """A vector (list) of numbers.""" name: str diff --git a/raillabel/json_format/vec_attribute.py b/raillabel/json_format/vec_attribute.py index c6df6a2..9dc1104 100644 --- a/raillabel/json_format/vec_attribute.py +++ b/raillabel/json_format/vec_attribute.py @@ -3,10 +3,10 @@ from __future__ import annotations -from pydantic import BaseModel +from ._json_format_base import _JSONFormatBase -class JSONVecAttribute(BaseModel, extra="forbid"): +class JSONVecAttribute(_JSONFormatBase): """A vec attribute.""" name: str