File size: 4,191 Bytes
e302b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Offline alignment tests; never starts cameras or opens CAN."""
import csv
from collections import defaultdict
from pathlib import Path
import tempfile
import unittest
import cv2
import numpy as np

from record_top_piper_sync import export_aligned, select_sample
from timestamp_sync_video import remux


class AlignmentTests(unittest.TestCase):
    def test_nearest_may_use_future_but_causal_cannot(self):
        rows = [{"id": 1}, {"id": 2}]
        self.assertEqual(select_sample(rows, [100, 110], 108, 20), rows[1])
        self.assertEqual(select_sample(rows, [100, 110], 108, 20, causal=True), rows[0])

    def test_empty_and_missing_stay_missing(self):
        self.assertIsNone(select_sample([], [], 100, 10))
        self.assertIsNone(select_sample([{}], [0], 100, 10))
        self.assertIsNone(select_sample([{}], [110], 100, 20, causal=True))

    def test_exact_and_tolerance_boundaries(self):
        rows = [{"id": 1}]
        self.assertEqual(select_sample(rows, [100], 100, 0), rows[0])
        self.assertEqual(select_sample(rows, [100], 110, 10), rows[0])
        self.assertIsNone(select_sample(rows, [100], 111, 10))

    def test_export_one_row_per_frame_and_no_gap_filling(self):
        frames = [{"video_frame_index": i, "camera_frame_index": i + 50,
                   "time_sec": i / 60, "host_monotonic_ns": t,
                   "device_timestamp_us": t // 1000, "device_system_timestamp_us": t // 1000}
                  for i, t in enumerate([1_000_000_000, 1_016_666_667, 1_033_333_333])]
        sample = {"host_monotonic_ns": 1_001_000_000, "kernel_timestamp_sec": 100,
                  "joint_deg": [1, 2, 3, 4, 5, 6], "pair_skew_sec": 0.001}
        streams = defaultdict(list, feedback=[sample], target=[sample])
        with tempfile.TemporaryDirectory(prefix="top-piper-sync-test-") as folder:
            metrics = export_aligned(Path(folder), frames, streams, 60)
            with (Path(folder) / "aligned_60hz.csv").open() as file:
                rows = list(csv.DictReader(file))
        self.assertEqual(len(rows), 3)
        self.assertEqual([r["feedback_valid"] for r in rows], ["1", "0", "0"])
        self.assertEqual([r["target_valid"] for r in rows], ["0", "1", "0"])
        self.assertEqual(rows[1]["feedback_j1_deg"], "")
        self.assertEqual(metrics["feedback"]["matched_frames"], 1)

    def test_joint_pair_skew_rejected(self):
        frame = {"video_frame_index": 0, "camera_frame_index": 1, "time_sec": 0,
                 "host_monotonic_ns": 1, "device_timestamp_us": 1, "device_system_timestamp_us": 1}
        sample = {"host_monotonic_ns": 1, "pair_skew_sec": 0.006}
        with tempfile.TemporaryDirectory(prefix="top-piper-sync-test-") as folder:
            result = export_aligned(Path(folder), [frame], defaultdict(list, feedback=[sample]), 60)
        self.assertEqual(result["feedback"]["missing_frames"], 1)

    def test_timestamped_video_and_no_overwrite(self):
        with tempfile.TemporaryDirectory(prefix="top-piper-sync-test-") as folder:
            root = Path(folder)
            writer = cv2.VideoWriter(str(root / "input.mp4"), cv2.VideoWriter_fourcc(*"mp4v"), 60, (64, 64))
            self.assertTrue(writer.isOpened())
            for index in range(5):
                writer.write(np.full((64, 64, 3), index * 40, dtype=np.uint8))
            writer.release()
            stamps = [1_000_000_000, 1_016_000_000, 1_035_000_000, 1_080_000_000, 1_091_000_000]
            with (root / "times.csv").open("w", newline="") as file:
                out = csv.writer(file)
                out.writerow(["video_frame_index", "host_monotonic_ns", "time_sec"])
                out.writerows((i, t, (t-stamps[0]) / 1e9) for i, t in enumerate(stamps))
            result = remux(root / "input.mp4", root / "times.csv", root / "timed.mp4")
            self.assertEqual(result["frames"], 5)
            self.assertLess(result["max_pts_error_us"], 2)
            self.assertAlmostEqual(result["last_frame_pts_sec"], 0.091)
            with self.assertRaises(FileExistsError):
                remux(root / "input.mp4", root / "times.csv", root / "timed.mp4")


if __name__ == "__main__":
    unittest.main()