Skip to content

Commit 492dbbb

Browse files
authored
refactor: replace sec2us and us2sec to seconds2microseconds and microseconds2seconds (#230)
* refactor: replace sec2us and us2sec to seconds2microseconds and microseconds2seconds Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> * chore: fix typo Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> --------- Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent 5780512 commit 492dbbb

5 files changed

Lines changed: 61 additions & 23 deletions

File tree

t4_devkit/common/timestamp.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,59 @@
22

33
from typing import TYPE_CHECKING
44

5+
from typing_extensions import deprecated
6+
57
if TYPE_CHECKING:
68
from t4_devkit.typing import ScalarLike
79

8-
__all__ = ("us2sec", "sec2us")
9-
1010

11+
@deprecated("Please use `microseconds2seconds(...)` instead.")
1112
def us2sec(timestamp: ScalarLike) -> float:
1213
"""Convert timestamp from micro seconds [us] to seconds [s].
1314
15+
Deprecated:
16+
This function is deprecated. Please use `microseconds2seconds(...)` instead.
17+
1418
Args:
1519
timestamp (ScalarLike): Timestamp in [us].
1620
1721
Returns:
1822
Timestamp in [s].
1923
"""
20-
return 1e-6 * timestamp
24+
return microseconds2seconds(timestamp)
2125

2226

27+
@deprecated("Please use `seconds2microseconds(...)` instead.")
2328
def sec2us(timestamp: ScalarLike) -> float:
2429
"""Convert timestamp from seconds [s] to micro seconds [us].
2530
31+
Deprecated:
32+
This function is deprecated. Please use `seconds2microseconds(...)` instead.
33+
34+
Args:
35+
timestamp (ScalarLike): Timestamp in [s].
36+
37+
Returns:
38+
Timestamp in [us].
39+
"""
40+
return seconds2microseconds(timestamp)
41+
42+
43+
def microseconds2seconds(timestamp: ScalarLike) -> float:
44+
"""Convert timestamp from micro seconds [us] to seconds [s].
45+
46+
Args:
47+
timestamp (ScalarLike): Timestamp in [us].
48+
49+
Returns:
50+
Timestamp in [s].
51+
"""
52+
return 1e-6 * timestamp
53+
54+
55+
def seconds2microseconds(timestamp: ScalarLike) -> float:
56+
"""Convert timestamp from seconds [s] to micro seconds [us].
57+
2658
Args:
2759
timestamp (ScalarLike): Timestamp in [s].
2860

t4_devkit/helper/rendering.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from PIL import Image
1313

1414
from t4_devkit.common.geometry import view_points
15-
from t4_devkit.common.timestamp import sec2us, us2sec
15+
from t4_devkit.common.timestamp import microseconds2seconds, seconds2microseconds
1616
from t4_devkit.dataclass import LidarPointCloud, RadarPointCloud
1717
from t4_devkit.schema import SensorModality
1818
from t4_devkit.viewer import (
@@ -126,7 +126,7 @@ def render_scene(
126126

127127
scene: Scene = self._t4.scene[0]
128128
first_sample: Sample = self._t4.get("sample", scene.first_sample_token)
129-
max_timestamp_us = first_sample.timestamp + sec2us(max_time_seconds)
129+
max_timestamp_us = first_sample.timestamp + seconds2microseconds(max_time_seconds)
130130

131131
concurrent.futures.wait(
132132
self._render_lidar_and_ego(
@@ -295,7 +295,9 @@ def render_pointcloud(
295295
raise ValueError("There is no 3D pointcloud data.")
296296

297297
first_lidar_sample_data: Sample = self._t4.get("sample_data", first_lidar_token)
298-
max_timestamp_us = first_lidar_sample_data.timestamp + sec2us(max_time_seconds)
298+
max_timestamp_us = first_lidar_sample_data.timestamp + seconds2microseconds(
299+
max_time_seconds
300+
)
299301

300302
concurrent.futures.wait(
301303
self._render_lidar_and_ego(
@@ -354,7 +356,7 @@ def _render_single_lidar(first_lidar_token: str) -> None:
354356
osp.join(self._t4.data_root, sample_data.filename)
355357
)
356358
viewer.render_pointcloud(
357-
seconds=us2sec(sample_data.timestamp),
359+
seconds=microseconds2seconds(sample_data.timestamp),
358360
channel=sample_data.channel,
359361
pointcloud=pointcloud,
360362
color_mode=color_mode,
@@ -384,7 +386,7 @@ def _render_single_radar(first_radar_token: str) -> None:
384386
osp.join(self._t4.data_root, sample_data.filename)
385387
)
386388
viewer.render_pointcloud(
387-
seconds=us2sec(sample_data.timestamp),
389+
seconds=microseconds2seconds(sample_data.timestamp),
388390
channel=sample_data.channel,
389391
pointcloud=pointcloud,
390392
)
@@ -410,7 +412,7 @@ def _render_single_camera(first_camera_token: str) -> None:
410412
break
411413

412414
viewer.render_image(
413-
seconds=us2sec(sample_data.timestamp),
415+
seconds=microseconds2seconds(sample_data.timestamp),
414416
camera=sample_data.channel,
415417
image=osp.join(self._t4.data_root, sample_data.filename),
416418
)
@@ -455,7 +457,7 @@ def _render_points_on_single_camera(camera: str) -> None:
455457
color_mode=color_mode,
456458
)
457459

458-
rr.set_time_seconds(ViewerConfig.timeline, us2sec(sample.timestamp))
460+
rr.set_time_seconds(ViewerConfig.timeline, microseconds2seconds(sample.timestamp))
459461

460462
rr.log(format_entity(ViewerConfig.ego_entity, camera), rr.Image(image))
461463
rr.log(
@@ -582,7 +584,7 @@ def _render_annotation3ds(
582584
self._t4.get_box3d(token, future_seconds=future_seconds)
583585
for token in sample.ann_3ds
584586
]
585-
viewer.render_box3ds(us2sec(sample.timestamp), boxes)
587+
viewer.render_box3ds(microseconds2seconds(sample.timestamp), boxes)
586588

587589
current_sample_token = sample.next
588590

@@ -636,7 +638,7 @@ def _render_annotation2ds(
636638
)
637639

638640
# Render 2D box
639-
viewer.render_box2ds(us2sec(sample.timestamp), boxes)
641+
viewer.render_box2ds(microseconds2seconds(sample.timestamp), boxes)
640642

641643
if instance_tokens is None:
642644
# Surface Annotation
@@ -655,7 +657,7 @@ def _render_annotation2ds(
655657
# Render 2D segmentation image
656658
for camera, data in camera_masks.items():
657659
viewer.render_segmentation2d(
658-
seconds=us2sec(sample.timestamp), camera=camera, **data
660+
seconds=microseconds2seconds(sample.timestamp), camera=camera, **data
659661
)
660662

661663
# TODO: add support of rendering keypoints

t4_devkit/helper/timeseries.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import TYPE_CHECKING
44

5-
from t4_devkit.common.timestamp import us2sec
5+
from t4_devkit.common.timestamp import microseconds2seconds
66

77
if TYPE_CHECKING:
88
from t4_devkit import Tier4
@@ -57,7 +57,9 @@ def get_sample_annotations_until(
5757
while current_sample_token != "":
5858
current_sample: Sample = self._t4.get("sample", current_sample_token)
5959

60-
if abs(us2sec(current_sample.timestamp - start_sample.timestamp)) > abs(seconds):
60+
if abs(microseconds2seconds(current_sample.timestamp - start_sample.timestamp)) > abs(
61+
seconds
62+
):
6163
break
6264

6365
ann_token = self._sample_and_instance_to_ann3d.get(
@@ -100,9 +102,9 @@ def get_object_anns_until(
100102
while current_sample_data_token != "":
101103
current_sample_data: SampleData = self._t4.get("sample_data", current_sample_data_token)
102104

103-
if abs(us2sec(current_sample_data.timestamp - start_sample_data.timestamp)) > abs(
104-
seconds
105-
):
105+
if abs(
106+
microseconds2seconds(current_sample_data.timestamp - start_sample_data.timestamp)
107+
) > abs(seconds):
106108
break
107109

108110
ann_token = self._sample_data_and_instance_to_ann2d.get(

t4_devkit/viewer/viewer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import rerun as rr
99

1010
from t4_devkit.common.converter import to_quaternion
11-
from t4_devkit.common.timestamp import us2sec
11+
from t4_devkit.common.timestamp import microseconds2seconds
1212
from t4_devkit.lanelet import LaneletParser
1313
from t4_devkit.schema import SensorModality
1414

@@ -487,7 +487,7 @@ def render_ego(self, *args, **kwargs) -> None:
487487

488488
def _render_ego_with_schema(self, ego_pose: EgoPose) -> None:
489489
self._render_ego_without_schema(
490-
seconds=us2sec(ego_pose.timestamp),
490+
seconds=microseconds2seconds(ego_pose.timestamp),
491491
translation=ego_pose.translation,
492492
rotation=ego_pose.rotation,
493493
geocoordinate=ego_pose.geocoordinate,

tests/common/test_timestamp.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from t4_devkit.common.timestamp import sec2us, us2sec
1+
from __future__ import annotations
2+
3+
from t4_devkit.common.timestamp import microseconds2seconds, seconds2microseconds
24

35

46
def test_timestamp() -> None:
57
"""Test timestamp conversion."""
68
seconds = 1e-6
79

8-
us = sec2us(seconds)
9-
sec = us2sec(us)
10+
us = seconds2microseconds(seconds)
11+
sec = microseconds2seconds(us)
1012

1113
assert us == 1
1214
assert sec == 1e-6

0 commit comments

Comments
 (0)