diff --git a/t4_devkit/common/timestamp.py b/t4_devkit/common/timestamp.py index cb9beae..b42324b 100644 --- a/t4_devkit/common/timestamp.py +++ b/t4_devkit/common/timestamp.py @@ -2,27 +2,59 @@ from typing import TYPE_CHECKING +from typing_extensions import deprecated + if TYPE_CHECKING: from t4_devkit.typing import ScalarLike -__all__ = ("us2sec", "sec2us") - +@deprecated("Please use `microseconds2seconds(...)` instead.") def us2sec(timestamp: ScalarLike) -> float: """Convert timestamp from micro seconds [us] to seconds [s]. + Deprecated: + This function is deprecated. Please use `microseconds2seconds(...)` instead. + Args: timestamp (ScalarLike): Timestamp in [us]. Returns: Timestamp in [s]. """ - return 1e-6 * timestamp + return microseconds2seconds(timestamp) +@deprecated("Please use `seconds2microseconds(...)` instead.") def sec2us(timestamp: ScalarLike) -> float: """Convert timestamp from seconds [s] to micro seconds [us]. + Deprecated: + This function is deprecated. Please use `seconds2microseconds(...)` instead. + + Args: + timestamp (ScalarLike): Timestamp in [s]. + + Returns: + Timestamp in [us]. + """ + return seconds2microseconds(timestamp) + + +def microseconds2seconds(timestamp: ScalarLike) -> float: + """Convert timestamp from micro seconds [us] to seconds [s]. + + Args: + timestamp (ScalarLike): Timestamp in [us]. + + Returns: + Timestamp in [s]. + """ + return 1e-6 * timestamp + + +def seconds2microseconds(timestamp: ScalarLike) -> float: + """Convert timestamp from seconds [s] to micro seconds [us]. + Args: timestamp (ScalarLike): Timestamp in [s]. diff --git a/t4_devkit/helper/rendering.py b/t4_devkit/helper/rendering.py index 015b6a6..6088908 100644 --- a/t4_devkit/helper/rendering.py +++ b/t4_devkit/helper/rendering.py @@ -12,7 +12,7 @@ from PIL import Image from t4_devkit.common.geometry import view_points -from t4_devkit.common.timestamp import sec2us, us2sec +from t4_devkit.common.timestamp import microseconds2seconds, seconds2microseconds from t4_devkit.dataclass import LidarPointCloud, RadarPointCloud from t4_devkit.schema import SensorModality from t4_devkit.viewer import ( @@ -126,7 +126,7 @@ def render_scene( scene: Scene = self._t4.scene[0] first_sample: Sample = self._t4.get("sample", scene.first_sample_token) - max_timestamp_us = first_sample.timestamp + sec2us(max_time_seconds) + max_timestamp_us = first_sample.timestamp + seconds2microseconds(max_time_seconds) concurrent.futures.wait( self._render_lidar_and_ego( @@ -295,7 +295,9 @@ def render_pointcloud( raise ValueError("There is no 3D pointcloud data.") first_lidar_sample_data: Sample = self._t4.get("sample_data", first_lidar_token) - max_timestamp_us = first_lidar_sample_data.timestamp + sec2us(max_time_seconds) + max_timestamp_us = first_lidar_sample_data.timestamp + seconds2microseconds( + max_time_seconds + ) concurrent.futures.wait( self._render_lidar_and_ego( @@ -354,7 +356,7 @@ def _render_single_lidar(first_lidar_token: str) -> None: osp.join(self._t4.data_root, sample_data.filename) ) viewer.render_pointcloud( - seconds=us2sec(sample_data.timestamp), + seconds=microseconds2seconds(sample_data.timestamp), channel=sample_data.channel, pointcloud=pointcloud, color_mode=color_mode, @@ -384,7 +386,7 @@ def _render_single_radar(first_radar_token: str) -> None: osp.join(self._t4.data_root, sample_data.filename) ) viewer.render_pointcloud( - seconds=us2sec(sample_data.timestamp), + seconds=microseconds2seconds(sample_data.timestamp), channel=sample_data.channel, pointcloud=pointcloud, ) @@ -410,7 +412,7 @@ def _render_single_camera(first_camera_token: str) -> None: break viewer.render_image( - seconds=us2sec(sample_data.timestamp), + seconds=microseconds2seconds(sample_data.timestamp), camera=sample_data.channel, image=osp.join(self._t4.data_root, sample_data.filename), ) @@ -455,7 +457,7 @@ def _render_points_on_single_camera(camera: str) -> None: color_mode=color_mode, ) - rr.set_time_seconds(ViewerConfig.timeline, us2sec(sample.timestamp)) + rr.set_time_seconds(ViewerConfig.timeline, microseconds2seconds(sample.timestamp)) rr.log(format_entity(ViewerConfig.ego_entity, camera), rr.Image(image)) rr.log( @@ -582,7 +584,7 @@ def _render_annotation3ds( self._t4.get_box3d(token, future_seconds=future_seconds) for token in sample.ann_3ds ] - viewer.render_box3ds(us2sec(sample.timestamp), boxes) + viewer.render_box3ds(microseconds2seconds(sample.timestamp), boxes) current_sample_token = sample.next @@ -636,7 +638,7 @@ def _render_annotation2ds( ) # Render 2D box - viewer.render_box2ds(us2sec(sample.timestamp), boxes) + viewer.render_box2ds(microseconds2seconds(sample.timestamp), boxes) if instance_tokens is None: # Surface Annotation @@ -655,7 +657,7 @@ def _render_annotation2ds( # Render 2D segmentation image for camera, data in camera_masks.items(): viewer.render_segmentation2d( - seconds=us2sec(sample.timestamp), camera=camera, **data + seconds=microseconds2seconds(sample.timestamp), camera=camera, **data ) # TODO: add support of rendering keypoints diff --git a/t4_devkit/helper/timeseries.py b/t4_devkit/helper/timeseries.py index b9ed2dc..ed3db34 100644 --- a/t4_devkit/helper/timeseries.py +++ b/t4_devkit/helper/timeseries.py @@ -2,7 +2,7 @@ from typing import TYPE_CHECKING -from t4_devkit.common.timestamp import us2sec +from t4_devkit.common.timestamp import microseconds2seconds if TYPE_CHECKING: from t4_devkit import Tier4 @@ -57,7 +57,9 @@ def get_sample_annotations_until( while current_sample_token != "": current_sample: Sample = self._t4.get("sample", current_sample_token) - if abs(us2sec(current_sample.timestamp - start_sample.timestamp)) > abs(seconds): + if abs(microseconds2seconds(current_sample.timestamp - start_sample.timestamp)) > abs( + seconds + ): break ann_token = self._sample_and_instance_to_ann3d.get( @@ -100,9 +102,9 @@ def get_object_anns_until( while current_sample_data_token != "": current_sample_data: SampleData = self._t4.get("sample_data", current_sample_data_token) - if abs(us2sec(current_sample_data.timestamp - start_sample_data.timestamp)) > abs( - seconds - ): + if abs( + microseconds2seconds(current_sample_data.timestamp - start_sample_data.timestamp) + ) > abs(seconds): break ann_token = self._sample_data_and_instance_to_ann2d.get( diff --git a/t4_devkit/viewer/viewer.py b/t4_devkit/viewer/viewer.py index a1d9a94..036ee8d 100644 --- a/t4_devkit/viewer/viewer.py +++ b/t4_devkit/viewer/viewer.py @@ -8,7 +8,7 @@ import rerun as rr from t4_devkit.common.converter import to_quaternion -from t4_devkit.common.timestamp import us2sec +from t4_devkit.common.timestamp import microseconds2seconds from t4_devkit.lanelet import LaneletParser from t4_devkit.schema import SensorModality @@ -487,7 +487,7 @@ def render_ego(self, *args, **kwargs) -> None: def _render_ego_with_schema(self, ego_pose: EgoPose) -> None: self._render_ego_without_schema( - seconds=us2sec(ego_pose.timestamp), + seconds=microseconds2seconds(ego_pose.timestamp), translation=ego_pose.translation, rotation=ego_pose.rotation, geocoordinate=ego_pose.geocoordinate, diff --git a/tests/common/test_timestamp.py b/tests/common/test_timestamp.py index 7f78a6c..2de87d9 100644 --- a/tests/common/test_timestamp.py +++ b/tests/common/test_timestamp.py @@ -1,12 +1,14 @@ -from t4_devkit.common.timestamp import sec2us, us2sec +from __future__ import annotations + +from t4_devkit.common.timestamp import microseconds2seconds, seconds2microseconds def test_timestamp() -> None: """Test timestamp conversion.""" seconds = 1e-6 - us = sec2us(seconds) - sec = us2sec(us) + us = seconds2microseconds(seconds) + sec = microseconds2seconds(us) assert us == 1 assert sec == 1e-6