from __future__ import annotations from pathlib import Path import numpy as np from kitti_c2l.decalibration import sample_dual_decalibration from kitti_c2l.generate import ( compose_miscalibrated_extrinsic, derive_seed, generate_row, generate_sequence_rows, ) from kitti_c2l.kitti_calib import load_rig_chain from kitti_c2l.sequence_mapping import SequenceInfo from kitti_c2l.stages import STAGES 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_derive_seed_deterministic_and_distinct(): a = derive_seed("00", 5, 1) b = derive_seed("00", 5, 1) c = derive_seed("00", 5, 2) d = derive_seed("00", 6, 1) assert a == b assert a != c assert a != d def test_composed_miscalibration_matches_closed_form(fake_kitti_root: Path): """Cross-checks the 3-call se3_compose/se3_inverse pipeline against the closed-form formula: R_miscal = R_dc @ R_gt @ R_dl.T; t_miscal = (R_dc @ t_gt + t_dc) - R_miscal @ t_dl.""" rig = load_rig_chain(fake_kitti_root / "2011_09_30", camera_id=2) stage = STAGES[0] dual = sample_dual_decalibration(seed=777, stage=stage) r_miscal, t_miscal = compose_miscalibrated_extrinsic(rig, dual) r_gt, t_gt = rig.rotation_cam_from_lidar, rig.translation_cam_from_lidar_cm r_dl, t_dl = dual.lidar.rotation, dual.lidar.translation_cm r_dc, t_dc = dual.camera.rotation, dual.camera.translation_cm r_expected = r_dc @ r_gt @ r_dl.T t_expected = (r_dc @ t_gt + t_dc) - r_expected @ t_dl np.testing.assert_allclose(r_miscal, r_expected, atol=1e-8) np.testing.assert_allclose(t_miscal, t_expected, atol=1e-8) def test_zero_decalibration_matches_ground_truth(fake_kitti_root: Path, monkeypatch): """If both sampled deltas were identity, the composed miscalibration must equal ground truth.""" rig = load_rig_chain(fake_kitti_root / "2011_09_30", camera_id=2) stage = STAGES[0] dual = sample_dual_decalibration(seed=1, stage=stage) dual.lidar.rotation[:] = np.eye(3) dual.lidar.translation_cm[:] = 0.0 dual.camera.rotation[:] = np.eye(3) dual.camera.translation_cm[:] = 0.0 r_miscal, t_miscal = compose_miscalibrated_extrinsic(rig, dual) np.testing.assert_allclose(r_miscal, rig.rotation_cam_from_lidar, atol=1e-8) np.testing.assert_allclose(t_miscal, rig.translation_cam_from_lidar_cm, atol=1e-8) def test_generate_row_fields(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_row(seq_info, rig, frame_index=2, stage=STAGES[1]) assert row.sample_id == "04_000002_stage2" assert row.sequence == "04" assert row.split == "train" assert row.raw_frame_index == 2 assert row.raw_date == "2011_09_30" assert row.raw_drive == "0016" assert len(row.gt_R_cam_from_velo) == 9 assert len(row.miscal_t_cam_from_velo_cm) == 3 assert row.image_width == 32 assert row.image_height == 32 def test_generate_sequence_rows_count(fake_kitti_root: Path): seq_info = _fake_seq_info(frame_count=4) rows = list(generate_sequence_rows(seq_info, fake_kitti_root, stages=STAGES)) assert len(rows) == 4 * len(STAGES) seeds = {row.sample_seed for row in rows} assert len(seeds) == len(rows) # every (frame, stage) gets a distinct seed