Skip to content

Commit

Permalink
refactor: create parent class for all json format classes
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 14, 2024
1 parent e404739 commit 342bf96
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 68 deletions.
8 changes: 8 additions & 0 deletions raillabel/json_format/_json_format_base.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions raillabel/json_format/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions raillabel/json_format/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions raillabel/json_format/boolean_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions raillabel/json_format/coordinate_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""]
Expand Down
5 changes: 2 additions & 3 deletions raillabel/json_format/cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions raillabel/json_format/element_data_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions raillabel/json_format/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions raillabel/json_format/frame_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions raillabel/json_format/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions raillabel/json_format/num_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions raillabel/json_format/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions raillabel/json_format/object_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@

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
from .poly3d import JSONPoly3d
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
Expand Down
5 changes: 2 additions & 3 deletions raillabel/json_format/poly2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions raillabel/json_format/poly3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions raillabel/json_format/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions raillabel/json_format/stream_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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[
Expand Down
4 changes: 2 additions & 2 deletions raillabel/json_format/stream_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions raillabel/json_format/stream_radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions raillabel/json_format/stream_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions raillabel/json_format/text_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions raillabel/json_format/transform_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading

0 comments on commit 342bf96

Please sign in to comment.