Skip to content

Commit

Permalink
feat: implement Radar.to_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 14, 2024
1 parent 7d8c225 commit 3807d93
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
8 changes: 8 additions & 0 deletions raillabel/format/intrinsics_radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ def from_json(cls, json: JSONIntrinsicsRadar) -> IntrinsicsRadar:
width_px=json.width_px,
height_px=json.height_px,
)

def to_json(self) -> JSONIntrinsicsRadar:
"""Export this object into the RailLabel JSON format."""
return JSONIntrinsicsRadar(
resolution_px_per_m=self.resolution_px_per_m,
width_px=self.width_px,
height_px=self.height_px,
)
25 changes: 24 additions & 1 deletion raillabel/format/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

from dataclasses import dataclass

from raillabel.json_format import JSONCoordinateSystem, JSONStreamRadar, JSONTransformData
from raillabel.json_format import (
JSONCoordinateSystem,
JSONStreamRadar,
JSONStreamRadarProperties,
JSONTransformData,
)

from .intrinsics_radar import IntrinsicsRadar
from .transform import Transform
Expand Down Expand Up @@ -39,6 +44,24 @@ def from_json(
description=json_stream.description,
)

def to_json(self) -> tuple[JSONStreamRadar, JSONCoordinateSystem]:
"""Export this object into the RailLabel JSON format."""
return (
JSONStreamRadar(
type="radar",
stream_properties=JSONStreamRadarProperties(
intrinsics_radar=self.intrinsics.to_json()
),
uri=self.uri,
description=self.description,
),
JSONCoordinateSystem(
parent="base",
type="sensor",
pose_wrt_parent=self.extrinsics.to_json() if self.extrinsics is not None else None,
),
)


def _extrinsics_from_json(json_transform: JSONTransformData | None) -> Transform | None:
if json_transform is None:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_raillabel/format/test_intrinsics_radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ def test_from_json(intrinsics_radar, intrinsics_radar_json):
assert actual == intrinsics_radar


def test_to_json(intrinsics_radar, intrinsics_radar_json):
actual = intrinsics_radar.to_json()
assert actual == intrinsics_radar_json


if __name__ == "__main__":
pytest.main([__file__, "--disable-pytest-warnings", "--cache-clear", "-v"])
9 changes: 7 additions & 2 deletions tests/test_raillabel/format/test_radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def radar_json(
description="A very nice radar",
),
JSONCoordinateSystem(
parent="base", type="sensor", pose_wrt_parent=transform_json, children=[]
parent="base", type="sensor", pose_wrt_parent=transform_json, children=None
),
)

Expand All @@ -51,5 +51,10 @@ def test_from_json(radar, radar_json):
assert actual == radar


def test_to_json(radar, radar_json):
actual = radar.to_json()
assert actual == radar_json


if __name__ == "__main__":
pytest.main([__file__, "--disable-pytest-warnings", "--cache-clear", "-v"])
pytest.main([__file__, "-vv"])

0 comments on commit 3807d93

Please sign in to comment.