from __future__ import annotations from pathlib import Path import pyarrow.parquet as pq from kitti_c2l.kitti_calib import load_rig_chain from kitti_c2l.schema import WINDOWS_COLUMN_NAMES, WINDOWS_SCHEMA from kitti_c2l.sequence_mapping import SequenceInfo from kitti_c2l.stages import STAGES from kitti_c2l.windows import ( derive_window_seed, generate_sequence_windows, generate_window_row, iter_windows, ) from kitti_c2l.writer import write_windows def _fake_seq_info(frame_count: int = 4) -> SequenceInfo: return SequenceInfo(sequence="04", date="2011_09_30", drive="0016", start=0, end=frame_count - 1, frame_count=frame_count, split="train") def test_iter_windows_tiles_non_overlapping(): windows = list(iter_windows(frame_count=10, window_length=3)) assert windows == [(0, 3), (3, 3), (6, 3), (9, 1)] # final tile shorter, no frames dropped assert sum(length for _, length in windows) == 10 def test_iter_windows_exact_multiple(): windows = list(iter_windows(frame_count=9, window_length=3)) assert windows == [(0, 3), (3, 3), (6, 3)] def test_iter_windows_single_window_larger_than_sequence(): windows = list(iter_windows(frame_count=4, window_length=30)) assert windows == [(0, 4)] def test_derive_window_seed_deterministic_and_distinct(): a = derive_window_seed("00", 0, 1) b = derive_window_seed("00", 0, 1) c = derive_window_seed("00", 30, 1) d = derive_window_seed("00", 0, 2) assert a == b assert a != c assert a != d def test_generate_sequence_windows_count(fake_kitti_root: Path): seq_info = _fake_seq_info(frame_count=4) rows = list(generate_sequence_windows(seq_info, fake_kitti_root, window_length=3, stages=STAGES)) # frame_count=4, window_length=3 -> windows (0,3) and (3,1) -> 2 windows x 5 stages = 10 rows assert len(rows) == 2 * len(STAGES) starts = sorted({row.window_start_frame for row in rows}) assert starts == [0, 3] def test_generate_sequence_windows_max_windows(fake_kitti_root: Path): seq_info = _fake_seq_info(frame_count=4) rows = list(generate_sequence_windows(seq_info, fake_kitti_root, window_length=3, stages=STAGES, max_windows=1)) assert len(rows) == 1 * len(STAGES) assert {row.window_start_frame for row in rows} == {0} def test_window_frames_share_the_same_decalibration(fake_kitti_root: Path): """The whole point: every frame in a window uses the SAME miscal extrinsic (one row covers the window).""" seq_info = _fake_seq_info(frame_count=4) rig = load_rig_chain(fake_kitti_root / "2011_09_30", camera_id=2) row = generate_window_row(seq_info, rig, window_start_frame=0, window_length=4, stage=STAGES[0]) # A single row's miscal extrinsic is what's applied to every frame_offset in [0, window_length) -- # verify raw_frame_start_index + window_length correctly spans the intended frame range. assert row.raw_frame_start_index == 0 assert row.window_length == 4 assert row.window_start_frame == 0 def test_raw_frame_start_index_offset(fake_kitti_root: Path): seq_info = SequenceInfo(sequence="08", date="2011_09_30", drive="0016", start=1100, end=1103, frame_count=4, split="train") rig = load_rig_chain(fake_kitti_root / "2011_09_30", camera_id=2) row = generate_window_row(seq_info, rig, window_start_frame=2, window_length=2, stage=STAGES[0]) assert row.raw_frame_start_index == 1100 + 2 # start offset carried through correctly def test_windows_parquet_round_trip(fake_kitti_root: Path, tmp_path: Path): seq_info = _fake_seq_info(frame_count=4) rows = list(generate_sequence_windows(seq_info, fake_kitti_root, window_length=3, stages=STAGES)) output_path = tmp_path / "windows_train.parquet" count = write_windows(rows, output_path) assert count == len(rows) table = pq.read_table(output_path) assert table.schema.equals(WINDOWS_SCHEMA) first = table.to_pylist()[0] assert set(first.keys()) == set(WINDOWS_COLUMN_NAMES) assert len(first["gt_R_cam_from_velo"]) == 9 def test_window_id_format(fake_kitti_root: Path): seq_info = _fake_seq_info(frame_count=4) rig = load_rig_chain(fake_kitti_root / "2011_09_30", camera_id=2) row = generate_window_row(seq_info, rig, window_start_frame=3, window_length=1, stage=STAGES[2]) assert row.window_id == "04_000003_stage3"