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.
102 lines
5.5 KiB
Python
102 lines
5.5 KiB
Python
"""Check a PTZ17 episode JSONL before replaying its accepted HOLD inputs."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
import json
|
|
from typing import Any, Iterable
|
|
|
|
Row = dict[str, Any]
|
|
|
|
|
|
def read_jsonl(path: Path) -> Iterable[Row]:
|
|
with path.open(encoding="utf-8") as stream:
|
|
for number, line in enumerate(stream, 1):
|
|
row = json.loads(line)
|
|
if not isinstance(row, dict) or not line.endswith("\n"):
|
|
raise ValueError(f"Incomplete JSONL row {number}")
|
|
yield row
|
|
|
|
|
|
def summarize(rows: Iterable[Row]) -> list[Row]:
|
|
episodes = defaultdict(list)
|
|
for row in rows:
|
|
if row.get("cam") == 6:
|
|
episodes[(row["generation"], row["track_start_utc"])].append(row)
|
|
|
|
reports = []
|
|
for (generation, started_utc), events in episodes.items():
|
|
windows = defaultdict(list)
|
|
for row in events:
|
|
if row["event"] in ("hold_capture_input", "hold_capture_end"):
|
|
windows[row["window_utc"]].append(row)
|
|
for window_utc, captured in sorted(windows.items()):
|
|
inputs = [r for r in captured if r["event"] == "hold_capture_input"]
|
|
ends = [r for r in captured if r["event"] == "hold_capture_end"]
|
|
problems = []
|
|
if any(r.get("schema") != 1 for r in captured):
|
|
problems.append("unknown_schema")
|
|
if len(ends) != 1 or captured[-1] is not ends[0]:
|
|
problems.append("missing_or_misordered_close")
|
|
elif ends[0]["input_count"] != len(inputs):
|
|
problems.append("input_count_mismatch")
|
|
if [r["input_idx"] for r in inputs] != list(range(1, len(inputs) + 1)):
|
|
problems.append("missing_or_out_of_order_input")
|
|
if not inputs or not inputs[0].get("geometry_hold") or inputs[0]["utc"] != window_utc:
|
|
problems.append("missing_first_hold")
|
|
if any(r["validation_count"] != len(r["validation"])
|
|
or r["reacquire_count"] != len(r["reacquire"]) for r in inputs):
|
|
problems.append("candidate_list_truncated")
|
|
if any(not isinstance(r.get("frame_seq"), int) or r["frame_seq"] <= 0 for r in inputs):
|
|
problems.append("missing_frame_number")
|
|
dropped = {r.get("dropped_before") for r in captured}
|
|
if None in dropped or len(dropped) > 1:
|
|
problems.append("telemetry_drop_detected_or_unknown")
|
|
if any(a["utc"] > b["utc"] for a, b in zip(captured, captured[1:])):
|
|
problems.append("clock_went_back")
|
|
|
|
first_hold = next((r for r in inputs if r["geometry_hold"]), None)
|
|
last_hold = next((r for r in reversed(inputs) if r["geometry_hold"]), None)
|
|
closed_utc = ends[0]["utc"] if len(ends) == 1 else captured[-1]["utc"]
|
|
outcomes = [r for r in events if r["event"] == "recovery_frame"
|
|
and window_utc <= r["utc"] <= closed_utc]
|
|
recovered = next((r for r in outcomes if last_hold and r["utc"] >= last_hold["utc"]
|
|
and r["observation_utc"] >= last_hold["observation_utc"]
|
|
and r.get("outcome") in ("detector_reacquire", "visual_recovered",
|
|
"geometry_reinitialized")), None)
|
|
# Measure a request before another HOLD window could own the result.
|
|
next_window = next((stamp for stamp in sorted(windows) if stamp > window_utc), float("inf"))
|
|
zoom = next((r for r in events if r["event"] == "decision_frame" and last_hold
|
|
and last_hold["utc"] <= r["utc"] < next_window and r.get("zoom_command", 0) > 0), None)
|
|
candidates = [r for r in inputs if r["geometry_hold"]
|
|
and r["input_kind"] == "detector_control"
|
|
and r["detector_result_present"]
|
|
and r["detector_frame_seq"] == r["frame_seq"] == r["ostrack"]["frame_seq"]]
|
|
reports.append(dict(generation=generation, track_start_utc=started_utc, window_utc=window_utc,
|
|
complete=not problems, issues=problems, inputs=len(inputs),
|
|
first_hold_frame=first_hold["frame_seq"] if first_hold else None,
|
|
last_hold_frame=last_hold["frame_seq"] if last_hold else None,
|
|
direct_same_frame_hold_inputs=len(candidates),
|
|
same_frame_hold_with_reacquire_boxes=sum(bool(r["reacquire"]) for r in candidates),
|
|
recovery_frame=recovered["frame_seq"] if recovered else None,
|
|
zoom_frame=zoom["frame_seq"] if zoom else None,
|
|
hold_to_recovery_ms=round((recovered["observation_utc"]
|
|
- last_hold["observation_utc"]) * 1000, 1)
|
|
if recovered else None,
|
|
hold_to_zoom_decision_ms=round((zoom["utc"] - last_hold["utc"]) * 1000, 1)
|
|
if zoom else None))
|
|
return reports
|
|
|
|
|
|
def main() -> int:
|
|
cli = argparse.ArgumentParser(description=__doc__)
|
|
cli.add_argument("episode", type=Path)
|
|
reports = summarize(read_jsonl(cli.parse_args().episode))
|
|
print(json.dumps({"windows": reports}, indent=2, allow_nan=False))
|
|
return 0 if reports and all(r["complete"] for r in reports) else 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|