Repository navigation
perf(python): decode one window of a stored Scene - #23
Conversation
from_proto(message, rows=(start, stop)) decodes the numeric columns, cuts the window with slice_time, and decodes camera, LiDAR and string rows only inside it. Decoding also shares the arrays of a camera calibration that repeats across rows. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
xtk8532704
left a comment
There was a problem hiding this comment.
Please check the packed offsets used by the requested window before slicing. This affects malformed stored input, and the check can stay local to the window.
| ) | ||
| if window: | ||
| start, stop = rows | ||
| scene = scene.slice_time(start, stop) |
There was a problem hiding this comment.
With five boxes (one per frame), changing the stored box_offsets to [0, 1, 2, -3, -2, 5] makes rows=(3, 4) return box 2 with validate=True. Python interprets [-3:-2] relative to the end of the column. Rebasing turns the boundaries into [0, 1], so the final validator accepts the wrong annotation. Whole decode rejects the same message. Could you check the selected boundaries for range and ordering before using them as indices, including the nested polygon and signal offsets, and add a regression test? Checking just the boundaries used to extract the window preserves window-local validation.
There was a problem hiding this comment.
Thanks, reproduced: with box_offsets = [0, 1, 3, -5, -1, 12] on the fixture, rows=(3, 4) returned other rows' boxes and passed validation. Fixed in 2ff34fc.
- The window decode now checks every boundary it indexes with before
slice_time: the per-row box, cylinder, polygon and signal group offsets, and the nested polygon vertex and signal element offsets of the selected objects and groups. - Each must be an integer array of the right length, with the window's boundaries non-negative, non-decreasing and inside their column.
- The check covers only the boundaries of the window, so validation stays local to it. It also runs with
validate=False, because these values are used as indices. - Regression tests in
test_scene_window_decode.py: eight malformed cases across the five offset columns (negative, decreasing, past the end), each withvalidateon and off. Each is rejected by the whole decode and by a window that uses the boundary, and a window that does not use it still decodes.
The 180 windows from 60 stored Scenes are still byte-identical to whole decode plus slice_time, and the 111-row window takes 17 ms (15 ms before, within run-to-run noise on a shared node).
A negative or decreasing stored offset inside the window indexed the column from its end, and rebasing hid it from the window's validation. The window decode now checks the boundaries it indexes with: per-row annotation and signal offsets, and the nested polygon vertex and signal element offsets. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
xtk8532704
left a comment
There was a problem hiding this comment.
The original negative-offset case now raises ValueError, and all 55 window-decode and time-slice tests passed locally. I found two remaining cases in the new boundary check. Both can be fixed while keeping validation local to the window.
| edges = np.asarray(offsets) | ||
| if edges.ndim != 1 or len(edges) != rows + 1 or edges.dtype.kind not in "iu": | ||
| raise ValueError(f"{name} must hold one integer boundary per row and a final one") | ||
| part = edges[start : stop + 1] |
There was a problem hiding this comment.
Please normalize both row bounds with operator.index() after rejecting booleans, before doing arithmetic, and use those Python integers throughout the window path. On NumPy 2, a valid 127-frame Scene with one box per frame and rows=(np.int8(125), np.int8(127)) overflows stop + 1 to -128. The empty boundary slice then raises IndexError at part[0]. Whole decode followed by slice_time(*rows) returns frames 125 and 126. This case would make a useful regression test for equivalence with slice_time().
There was a problem hiding this comment.
Fixed in 15c5264. The bounds are now converted with operator.index() after the boolean check and used as Python integers on the whole window path. test_window_rows_may_be_small_numpy_integers compares np.int8 bounds with whole decode followed by slice_time(*rows).
| raise ValueError(f"{name} must hold one integer boundary per row and a final one") | ||
| part = edges[start : stop + 1] | ||
| if np.ma.getmaskarray(offsets[start : stop + 1]).any() or ( | ||
| part[0] < 0 or part[-1] > size or np.any(part[1:] < part[:-1]) |
There was a problem hiding this comment.
Please enforce the endpoint invariant when the selected rows reach either end of the packed column. On the five-frame fixture with 12 boxes, changing only the first box offset from 0 to 1 or only the final offset from 12 to 11 makes whole decode reject the message. In both cases rows=(0, 5), validate=True returns 11 boxes and silently drops one. When start == 0 the first boundary must be zero, and when stop == rows the final boundary must equal the packed column size. These checks only read endpoints already in the window.
There was a problem hiding this comment.
Fixed in 15c5264. When start == 0 the first boundary must be zero, and when stop is the row count the last one must equal the column length. The same rule applies to the nested polygon vertex and traffic light element offsets. test_window_at_an_end_of_the_scene_checks_that_end_of_the_column covers both ends for boxes and polygons, with validation on and off; a window away from that end still decodes.
|
Could we add a Could the window decoder pass the model payload and the half-open row bounds in the stored range to the callback, and use its return value as the window's A regression test could request |
# Conflicts: # CHANGELOG.md
…l_data callback - Normalize the window bounds with operator.index so small NumPy integers cannot overflow. - A window that reaches either end of a packed column requires that end's boundary to be zero or the column's length. - from_proto(rows=...) takes the slice_model_data callback of slice_time for a nonempty model_data payload. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Added in 15c5264:
I also merged |
xtk8532704
left a comment
There was a problem hiding this comment.
Verified both fixes on 15c5264. The window ending at np.int8(127) matches whole decode followed by slicing, and malformed first or last offsets are rejected for boxes, polygon vertices, and traffic light elements. The local Python suite passed with 235 tests passed and 50 skipped.
First part of #22 (window decode). The zstd question stays in the issue.
Problem
A reader that wants a short window of a stored Scene has to call
from_protoon the whole message and thenslice_time.from_protodecodes and validates every row first, so the cost follows the file, not the window. On 60-second Scenes with 11 cameras per row this is about 290 ms per read.Change
T4Scene.from_proto(message, rows=(start, stop), current_index=k)decodes one window.from_proto(message), then settingcurrent_index, thenslice_time(start, stop).slice_timeitself, so the offset handling is not duplicated.track_ids) are read lazily in this mode; slicing gives the same lists as before.validate=Truevalidates the window only.current_indexis an index into the stored rows and defaults to the stored one. It needsrows.model_datais rejected in this mode, becauseslice_timeneeds the caller's callback for it.Independent of
rows, a camera calibration that repeats across rows is now decoded once and its read-only arrays are shared between the rows'Cameraobjects. Cameras that carry an image or annotations are decoded as before.Validation
pytest t4scene/python/tests: 296 passed, 4 skipped.ruff format --checkandruff checkare clean.tests/test_scene_window_decode.pychecks that a window serializes to the same bytes as whole decode plusslice_time, that no camera outside the window is decoded, the shared calibration, the default and invalid current rows, themodel_datarejection, and window-only validation.slice_time. One process, median:from_protovalidate=Truevalidate=Falsevalidate=Truevalidate=Falsevalidate=FalseThe "before" and "after" columns come from two runs on the same node and the same Scenes.
Not in this PR
contains_tensorwalking the record tree once pervalidate). That can be a separate change.🤖 Generated with Claude Code