Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions t4_devkit/common/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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].

Expand Down
22 changes: 12 additions & 10 deletions t4_devkit/helper/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand All @@ -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),
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions t4_devkit/helper/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions t4_devkit/viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions tests/common/test_timestamp.py
Original file line number Diff line number Diff line change
@@ -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
Loading