"""Render an episode JSONL as a static SVG without plotting dependencies.""" import argparse import json from pathlib import Path from xml.sax.saxutils import escape def render(rows): samples = sorted((r for r in rows if r["event"] == "tick"), key=lambda r: r["elapsed_ms"]) if not samples: raise ValueError("The episode has no TRACK ticks.") last = max(1.0, samples[-1]["elapsed_ms"]) groups = ( ("HFOV (deg)", 60.0, ( ("ISAPI actual", "#2563eb", lambda r: r["FOV_actual"]), ("Estimated", "#c2410c", lambda r: r["FOV_estimated"]), ("Narrow bound", "#64748b", lambda r: r["FOV_min_estimated"]), )), ("BBox (px)", max(48.0, max(max(r["bbox_px"]) for r in samples)), ( ("Width", "#2563eb", lambda r: r["bbox_px"][0]), ("Height", "#c2410c", lambda r: r["bbox_px"][1]), )), ("Confidence", 1.0, ( ("YOLO last match", "#2563eb", lambda r: r["detector_conf"]), ("OSTrack score", "#c2410c", lambda r: r["tracker_score"]), )), ) content = [ '', '', '', f'PTZ {escape(str(samples[0]["cam"]))} ยท TRACK telemetry', ] first = next((r.get("t_to_first_zoom_ms") for r in rows if r["event"] == "first_zoom"), None) for panel, (title, ymax, series) in enumerate(groups): y0 = 76 + 190 * panel plot_h = 133 content.append(f'{title}') content.append(f'') for frac in (0, .5, 1): content.append(f'{ymax*frac:.1f}') for number, (label, color, extract) in enumerate(series): content.append(f'{label}') path = [] for row in samples: value = extract(row) if value is None: if path and path[-1]: path.append("") continue x = 68 + 850 * row["elapsed_ms"] / last y = y0 + plot_h * (1 - value / ymax) command = "L" if path and path[-1] else "M" path.append(f"{command}{x:.1f} {y:.1f}") content.append(f'') if first is not None and 0 <= first <= last: x = 68 + 850 * first / last content.append(f'') for k in range(6): content.append(f'{last*k/5000:.2f}') content.append('Seconds since TRACK_START. Dashed marker = first acknowledged zoom.') content.append('') return "\n".join(content) + "\n" if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("episode", type=Path) parser.add_argument("output", type=Path) args = parser.parse_args() rows = [json.loads(line) for line in args.episode.read_text(encoding="utf-8").splitlines() if line] args.output.write_text(render(rows), encoding="utf-8")