Skip to content

Commit 0d359bf

Browse files
committed
feat: fix timestamp index
1 parent 6933b5d commit 0d359bf

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/opentau/scripts/segment_lerobot_dataset.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ def segment_dataset(
335335
if col_idx >= 0:
336336
seg_table = seg_table.set_column(col_idx, key, arr)
337337

338+
# Recompute timestamps from local frame_index to avoid subtraction drift.
339+
if "timestamp" in seg_table.column_names:
340+
ts_idx = seg_table.schema.get_field_index("timestamp")
341+
recomputed_ts = np.arange(seg_len, dtype=np.float64) / float(source_meta.fps)
342+
seg_table = seg_table.set_column(
343+
ts_idx,
344+
"timestamp",
345+
pa.array(recomputed_ts, type=seg_table.schema.field("timestamp").type),
346+
)
347+
338348
# For image-based datasets, copy only the segment frames and rewrite image references.
339349
image_keys = [k for k, ft in source_meta.features.items() if ft["dtype"] == "image"]
340350
for image_key in image_keys:

tests/datasets/test_segment_lerobot_dataset.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ def test_segment_lerobot_v21_dataset(tmp_path: Path, empty_lerobot_dataset_facto
102102
assert ep0["frame_index"] == [0, 1, 2]
103103
assert ep0["episode_index"] == [0, 0, 0]
104104
assert ep0["index"] == [0, 1, 2]
105+
assert np.isclose(float(ep0["timestamp"][0]), 0.0)
106+
assert np.allclose(np.diff(np.asarray(ep0["timestamp"], dtype=np.float64)), 1.0 / dataset.fps)
105107
assert [float(x) for x in ep0["state"]] == [2.0, 3.0, 4.0]
106108

107109
assert ep1["frame_index"] == [0, 1, 2, 3, 4]
108110
assert ep1["episode_index"] == [1, 1, 1, 1, 1]
109111
assert ep1["index"] == [3, 4, 5, 6, 7]
112+
assert np.isclose(float(ep1["timestamp"][0]), 0.0)
113+
assert np.allclose(np.diff(np.asarray(ep1["timestamp"], dtype=np.float64)), 1.0 / dataset.fps)
110114
assert [float(x) for x in ep1["state"]] == [5.0, 6.0, 7.0, 8.0, 9.0]
111115

112116

@@ -222,6 +226,11 @@ def test_segment_lerobot_non_consecutive_and_overlapping_ranges(
222226
assert ep1["index"] == list(range(10, 15))
223227
assert ep2["index"] == list(range(15, 25))
224228

229+
# Timestamps are rebased per output episode.
230+
assert np.isclose(float(ep0["timestamp"][0]), 0.0)
231+
assert np.isclose(float(ep1["timestamp"][0]), 0.0)
232+
assert np.isclose(float(ep2["timestamp"][0]), 0.0)
233+
225234
# Data slices match requested source windows.
226235
assert [float(x) for x in ep0["state"]] == [float(i) for i in range(0, 10)]
227236
assert [float(x) for x in ep1["state"]] == [float(i) for i in range(18, 23)]

0 commit comments

Comments
 (0)