"""黄金语料 JSONL 的现场回放小结(给评审看的那几行数字;**不是**运行时组件)。 为什么单独一个脚本:``--dump-frames`` 的 JSONL 是逐帧明细(几十~几千行), 现场评审要的是"这批帧上证据链说了什么": * 状态分布(0/1/2/255)、弃权与运动门控各多少帧; * 证据命中次数(哪些证据在这段画面里稳定出现、哪些从没出现); * 自标尺与几何量(``px_per_m`` / ``box_h_px`` / ``outline_quality``)的区间 —— 这些正是 ``docs/edge_evidence.md`` §7 阈值表的整定依据; * 每帧一行明细(帧号 / 状态 / 置信度 / diag / 证据),便于直接跳到可疑帧。 用法:: python scripts/analyze_dump.py runs/field_B/dump.jsonl [--json runs/field_B/analysis.json] 只依赖标准库。 """ from __future__ import annotations import argparse import collections import json import statistics import sys #: 状态码(与 schema.hpp / docs/plc_interface.md 同一张表) STATE_NAME = {0: "0=不在车上", 1: "1=车斗(敞车)", 2: "2=平板车", 255: "255=不可信"} def load_rows(path: str) -> list[dict]: rows = [] with open(path, encoding="utf-8-sig") as handle: for lineno, line in enumerate(handle, 1): line = line.strip() if not line: continue try: rows.append(json.loads(line)) except json.JSONDecodeError as exc: raise ValueError("%s 第 %d 行不是合法 JSON: %s" % (path, lineno, exc)) from exc return rows def stats(values: list[float]) -> str: if not values: return "n/a" return "min=%.3f p50=%.3f max=%.3f" % (min(values), statistics.median(values), max(values)) def main(argv=None) -> int: parser = argparse.ArgumentParser(description="黄金语料 JSONL 现场回放小结(只读)") parser.add_argument("jsonl", help="portcv_run --dump-frames 写出的 JSONL") parser.add_argument("--json", default=None, help="把小结同时写成 JSON(可选)") args = parser.parse_args(argv) try: rows = load_rows(args.jsonl) except (OSError, ValueError) as exc: print("读不了导出文件: %s" % exc, file=sys.stderr) return 2 if not rows: print("导出文件里没有任何帧", file=sys.stderr) return 2 states = collections.Counter(int(r.get("state", -1)) for r in rows) edge_states = collections.Counter(int(r["edge_state"]) for r in rows if isinstance(r.get("edge_state"), int)) vote_states = collections.Counter(int(r["vote_state"]) for r in rows if isinstance(r.get("vote_state"), int)) diags = collections.Counter(str(r.get("diag_name", "?")) for r in rows) flags = collections.Counter(int(r.get("flags", 0)) for r in rows) hits: collections.Counter = collections.Counter() for r in rows: for key, value in (r.get("ev") or {}).items(): if value: hits[key] += 1 abstain = sum(1 for r in rows if r.get("abstain")) motion = sum(1 for r in rows if r.get("motion")) hold = sum(1 for r in rows if r.get("hold")) conflict = sum(1 for r in rows if r.get("conflict")) def column(name: str) -> list[float]: return [float(r[name]) for r in rows if isinstance(r.get(name), (int, float))] summary = { "frames": len(rows), "states": {str(k): v for k, v in sorted(states.items())}, "vote_states": {str(k): v for k, v in sorted(vote_states.items())}, "edge_states": {str(k): v for k, v in sorted(edge_states.items())}, "diag": dict(diags.most_common()), "flags": {("0x%04X" % k): v for k, v in sorted(flags.items())}, "abstain": abstain, "motion": motion, "hold": hold, "conflict": conflict, "evidence_hits": dict(hits.most_common()), "px_per_m": stats(column("px_per_m")), "box_h_px": stats(column("box_h_px")), "outline_quality": stats(column("outline_quality")), "ground_ratio_full": stats(column("ground_ratio_full")), "score_a": stats(column("score_a")), "score_b": stats(column("score_b")), "score_c": stats(column("score_c")), } print("帧数 : %d" % summary["frames"]) print("状态分布 : " + " ".join("%s × %d" % (STATE_NAME.get(int(k), k), v) for k, v in sorted(states.items(), key=lambda kv: int(kv[0])))) if vote_states: print("投票结论 : " + " ".join("%s × %d" % (STATE_NAME.get(int(k), k), v) for k, v in sorted(vote_states.items(), key=lambda kv: int(kv[0])))) if edge_states: print("本帧结论 : " + " ".join("%s × %d" % (STATE_NAME.get(int(k), k), v) for k, v in sorted(edge_states.items(), key=lambda kv: int(kv[0])))) print("证据后端 : abstain=%d conflict=%d motion=%d hold=%d" % (abstain, conflict, motion, hold)) print("diag 分布 : " + " ".join("%s×%d" % (k, v) for k, v in diags.most_common())) print("flags : " + " ".join("0x%04X×%d" % (k, v) for k, v in sorted(flags.items()))) print("证据命中 : " + (" ".join("%s×%d" % (k, v) for k, v in hits.most_common()) or "(一项都没命中)")) for key in ("px_per_m", "box_h_px", "outline_quality", "ground_ratio_full", "score_a", "score_b", "score_c"): print("%-18s: %s" % (key, summary[key])) print("逐帧明细 : 帧号 / 发布状态(投票,本帧结论) / 置信度 / diag / 证据") for r in rows: ev = ",".join(sorted(k for k, v in (r.get("ev") or {}).items() if v)) print(" f%-5d state=%-3d(vote=%-3s,edge=%-3s) conf=%-4s diag=%-12s %s" % (r.get("frame", -1), r.get("state", -1), "-" if r.get("vote_state") is None else r.get("vote_state"), "-" if r.get("edge_state") is None else r.get("edge_state"), r.get("conf_x1000", -1), r.get("diag_name", "?"), ev or "-")) if args.json: with open(args.json, "w", encoding="utf-8") as handle: json.dump(summary, handle, ensure_ascii=False, indent=2) handle.write("\n") print("小结 JSON -> %s" % args.json) return 0 if __name__ == "__main__": raise SystemExit(main())