File size: 4,474 Bytes
db613b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from __future__ import annotations

from pathlib import Path

import numpy as np
import pyarrow.parquet as pq

from kitti_c2l.assets import generate_frame_asset, generate_sequence_assets
from kitti_c2l.kitti_calib import load_rig_chain
from kitti_c2l.schema import ASSETS_COLUMN_NAMES, ASSETS_SCHEMA, frame_id
from kitti_c2l.sequence_mapping import SequenceInfo
from kitti_c2l.writer import write_assets, write_assets_sharded


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_frame_id_format():
    assert frame_id("04", 2) == "04_000002"


def test_generate_frame_asset_matches_raw_files(fake_kitti_root: Path):
    seq_info = _fake_seq_info()
    rig = load_rig_chain(fake_kitti_root / "2011_09_30", camera_id=2)
    row = generate_frame_asset(seq_info, fake_kitti_root, frame_index=1, image_width=rig.image_width, image_height=rig.image_height)

    raw_image_path = fake_kitti_root / "2011_09_30" / "2011_09_30_drive_0016_sync" / "image_02" / "data" / "0000000001.png"
    raw_lidar_path = fake_kitti_root / "2011_09_30" / "2011_09_30_drive_0016_sync" / "velodyne_points" / "data" / "0000000001.bin"

    assert row.image_png == raw_image_path.read_bytes()
    assert row.points_bin == raw_lidar_path.read_bytes()
    assert row.frame_id == "04_000001"
    assert row.raw_frame_index == 1


def test_generate_sequence_assets_count(fake_kitti_root: Path):
    seq_info = _fake_seq_info()
    rows = list(generate_sequence_assets(seq_info, fake_kitti_root))
    assert len(rows) == 4
    frame_ids = {row.frame_id for row in rows}
    assert len(frame_ids) == 4  # one row per unique frame, no stage multiplication


def test_write_assets_round_trip(fake_kitti_root: Path, tmp_path: Path):
    seq_info = _fake_seq_info()
    rows = list(generate_sequence_assets(seq_info, fake_kitti_root))
    output_path = tmp_path / "frames_train.parquet"
    count = write_assets(rows, output_path)
    assert count == 4

    table = pq.read_table(output_path)
    assert table.schema.equals(ASSETS_SCHEMA)
    read_rows = table.to_pylist()
    assert set(read_rows[0].keys()) == set(ASSETS_COLUMN_NAMES)

    for original, read_back in zip(rows, read_rows, strict=True):
        assert read_back["frame_id"] == original.frame_id
        assert bytes(read_back["image_png"]) == original.image_png
        assert bytes(read_back["points_bin"]) == original.points_bin


def test_write_assets_sharded_splits_by_target_size(fake_kitti_root: Path, tmp_path: Path):
    seq_info = _fake_seq_info()
    rows = list(generate_sequence_assets(seq_info, fake_kitti_root))
    row_bytes = len(rows[0].image_png) + len(rows[0].points_bin)

    # Force multiple shards: cap each shard at roughly 1 row's worth of bytes.
    shard_paths = write_assets_sharded(rows, tmp_path, split="train", target_shard_bytes=row_bytes)

    assert len(shard_paths) == 4  # one row per shard
    for i, path in enumerate(shard_paths):
        assert path.name == f"frames_train-{i:05d}-of-00004.parquet"
        assert path.exists()

    all_rows = []
    for path in shard_paths:
        all_rows.extend(pq.read_table(path).to_pylist())
    assert len(all_rows) == 4
    assert {r["frame_id"] for r in all_rows} == {row.frame_id for row in rows}


def test_write_assets_sharded_single_shard_when_under_target(fake_kitti_root: Path, tmp_path: Path):
    seq_info = _fake_seq_info()
    rows = list(generate_sequence_assets(seq_info, fake_kitti_root))
    shard_paths = write_assets_sharded(rows, tmp_path, split="test", target_shard_bytes=10**9)
    assert len(shard_paths) == 1
    assert shard_paths[0].name == "frames_test-00000-of-00001.parquet"


def test_write_assets_sharded_empty_input(tmp_path: Path):
    shard_paths = write_assets_sharded([], tmp_path, split="train")
    assert shard_paths == []


def test_asset_points_bin_decodes_to_same_points_as_raw_file(fake_kitti_root: Path):
    seq_info = _fake_seq_info()
    rows = list(generate_sequence_assets(seq_info, fake_kitti_root))
    row = rows[2]

    raw_lidar_path = fake_kitti_root / "2011_09_30" / "2011_09_30_drive_0016_sync" / "velodyne_points" / "data" / "0000000002.bin"
    expected = np.fromfile(raw_lidar_path, dtype=np.float32).reshape(-1, 4)
    decoded = np.frombuffer(row.points_bin, dtype=np.float32).reshape(-1, 4)
    np.testing.assert_array_equal(decoded, expected)