You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
5.5 KiB
Python
118 lines
5.5 KiB
Python
"""Verify PTZ17 capture on the control path and through the actual JSONL queue."""
|
|
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
import importlib.util
|
|
import os
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from summarize_capture import read_jsonl, summarize
|
|
|
|
spec = importlib.util.spec_from_file_location("ptz17_existing_test", Path(os.environ["PTZ17_PARITY_TEST"]))
|
|
assert spec and spec.loader
|
|
existing = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(existing)
|
|
versions = existing.versions
|
|
|
|
|
|
@pytest.mark.parametrize("scenario", [
|
|
"hold_then_strict", "visual_recovers", "paired_hold", "geometry_two_frames",
|
|
"wide_two_frames", "ordinary_track", "seed_failed",
|
|
])
|
|
def test_control_parity_and_capture_paths(versions, scenario):
|
|
baseline, candidate = versions
|
|
previous = existing.exercise(baseline, scenario)[:2]
|
|
original_capture = candidate._capture_ptz17_hold_input
|
|
stop_at_first_hold = []
|
|
|
|
def capture_after_stop(*args, **kwargs):
|
|
if kwargs["geometry_hold"] and not stop_at_first_hold:
|
|
stop_at_first_hold.append(candidate.stop_ptz.called)
|
|
return original_capture(*args, **kwargs)
|
|
|
|
with patch.object(candidate, "_capture_ptz17_hold_input",
|
|
side_effect=capture_after_stop) as spy:
|
|
assert existing.exercise(candidate, scenario)[:2] == previous
|
|
expected = {
|
|
"hold_then_strict": [(41, "detector_control", True), (42, "detector_control", False)],
|
|
"visual_recovers": [(41, "visual_control", True), (42, "visual_control", False)],
|
|
"paired_hold": [(41, "visual_control", True), (42, "paired_detector", False)],
|
|
"geometry_two_frames": [(41, "detector_control", True), (42, "detector_control", True)],
|
|
}
|
|
assert [(call.args[3], call.kwargs["input_kind"], call.kwargs["geometry_hold"]) for call in spy.call_args_list
|
|
] == expected.get(scenario, [])
|
|
if scenario in expected:
|
|
assert stop_at_first_hold == [True]
|
|
|
|
|
|
@pytest.fixture()
|
|
def episode(versions, tmp_path):
|
|
_baseline, module = versions
|
|
directory = str(tmp_path)
|
|
state = dict(mode="TRACK", tracking_active=True, ptz_generation=7, track_started_at=90.0,
|
|
last_bbox=existing.TRUSTED, last_seen=99.8)
|
|
result_cls = module.visual_tracker_manager.update.__globals__["VisualTrackResult"]
|
|
candidates = [(existing.GOOD, .51, 0), (existing.WRONG, .95, 0)]
|
|
original = result_cls(False, existing.REJECTED, .29, 0, .80, 40, "area ratio 5.45", 41)
|
|
next_result = result_cls(True, existing.GOOD, .85, 0, .80, 41, "recovered", 42)
|
|
capture = module._capture_ptz17_hold_input
|
|
|
|
with patch.object(module.config, "PREDICTIVE_ZOOM_TELEMETRY_DIR", directory, create=True):
|
|
capture(state, 100.05, 100.0, 41, 1280, 720, original,
|
|
input_kind="detector_control", geometry_hold=True, captured_utc=None,
|
|
detector_result_present=True, raw_box_count=2, expected_track_class=0,
|
|
validation_targets=candidates, reacquire_targets=candidates)
|
|
capture(state, 100.25, 100.2, 42, 1280, 720, next_result,
|
|
input_kind="paired_detector", geometry_hold=False, captured_utc=100.2,
|
|
detector_result_present=True, raw_box_count=2, expected_track_class=0,
|
|
validation_targets=candidates, reacquire_targets=candidates)
|
|
module._episode_telemetry(module.config, 6, (7, 90.), "recovery_frame", 100.25,
|
|
frame_seq=42, outcome="detector_reacquire", observation_utc=100.2)
|
|
module._episode_telemetry(module.config, 6, (7, 90.), "decision_frame", 100.80,
|
|
frame_seq=48, zoom_command=.1)
|
|
module._finish_ptz17_hold_capture(state, 101.0, "track_end")
|
|
logger = module._episode_telemetry.__globals__["_telemetry_loggers"][directory]
|
|
logger.queue.join()
|
|
path, = tmp_path.glob("*.jsonl")
|
|
return list(read_jsonl(path))
|
|
|
|
|
|
def test_real_writer_preserves_frames_and_order(episode):
|
|
hold, paired, _, _, end = episode
|
|
assert (hold["utc"], hold["observation_utc"], hold["captured_utc"]) == (100.05, 100.0, None)
|
|
assert hold["frame_seq"] == hold["detector_frame_seq"] == hold["ostrack"]["frame_seq"] == 41
|
|
assert hold["trusted_bbox_px"] == list(existing.TRUSTED)
|
|
assert hold["ostrack"]["bbox_px"] == list(existing.REJECTED)
|
|
assert [b["bbox_px"] for b in hold["reacquire"]] == [existing.GOOD, existing.WRONG]
|
|
assert (paired["input_idx"], paired["captured_utc"], paired["detector_frame_seq"]) == (2, 100.2, 42)
|
|
assert end["input_count"] == 2 and end["close_reason"] == "track_end"
|
|
report, = summarize(episode)
|
|
assert report["complete"] and report["direct_same_frame_hold_inputs"] == 1
|
|
assert report["hold_to_recovery_ms"] == 200 and report["hold_to_zoom_decision_ms"] == 750
|
|
|
|
|
|
@pytest.mark.parametrize("case,issue", [
|
|
("missing", "missing_or_out_of_order_input"),
|
|
("open", "missing_or_misordered_close"),
|
|
("drop", "telemetry_drop_detected_or_unknown"),
|
|
("truncated", "candidate_list_truncated"),
|
|
("no_seq", "missing_frame_number"),
|
|
])
|
|
def test_refuse_incomplete_capture(episode, case, issue):
|
|
rows = deepcopy(episode)
|
|
if case == "missing":
|
|
rows.pop(0)
|
|
elif case == "open":
|
|
rows.pop()
|
|
elif case == "drop":
|
|
rows[1]["dropped_before"] += 1
|
|
elif case == "truncated":
|
|
rows[0]["reacquire"].pop()
|
|
elif case == "no_seq":
|
|
rows[0]["frame_seq"] = None
|
|
result, = summarize(rows)
|
|
assert not result["complete"] and issue in result["issues"]
|