"""Compare two Bench2Drive full-220 runs through their failure_report.py outputs. Every failed route gets one primary cause: the earliest collision (by its class and front-camera warning time) or, without collisions, its non-collision infractions. Driving-score loss is (100 - route score) / 220. Usage: python compare_runs.py NAME_A REPORT_A NAME_B REPORT_B [--out OUT.json] """ import argparse import collections import json IGNORED = {"route_dev", "min_speed_infractions"} VIS_LIMIT_S = 1.5 LEAD = {"ego hits stationary vehicle", "ego hits vehicle ahead (same direction)"} def primary_cause(e): infr = sorted(k for k, v in e["infractions"].items() if v and k not in IGNORED) colls = e["collisions"] if isinstance(e.get("collisions"), list) else [] if colls: c = min(colls, key=lambda c: c["t"]) if c["class"] == "pedestrian": return "pedestrian", c if c["class"] == "static object / layout": return "static object / layout", c if c["class"] == "rear-ended by other": return "rear threat", c if c.get("warning_s", 99) < VIS_LIMIT_S: return "visibility-limited (<1.5 s in FOV)", c if c["class"] in LEAD: return "lead / stationary vehicle", c return "visible, not yielded (" + c["class"] + ")", c if any(k.startswith("collisions") for k in infr): return "collision without recorder match", None if infr: return "non-collision: " + "/".join(infr), None if e["status"] != "Completed": return "status: " + e["status"], None return "deviation / min speed only", None def summarize(report): out = {} for e in report: cause, c = (None, None) if e["success"] else primary_cause(e) out[e["route"]] = {"scenario": e["scenario"], "town": e["town"], "score": e["score"], "success": e["success"], "status": e["status"], "cause": cause, "first_collision": None if c is None else {k: c.get(k) for k in ("t", "class", "other_type", "warning_s", "ego_speed", "other_speed")}} return out def group(cause): if cause is None: return None return "visible, not yielded" if cause.startswith("visible, not yielded") else \ "non-collision" if cause.startswith("non-collision") else cause def main(): ap = argparse.ArgumentParser() ap.add_argument("name_a"); ap.add_argument("report_a"); ap.add_argument("name_b"); ap.add_argument("report_b") ap.add_argument("--out") args = ap.parse_args() A, B = (summarize(json.load(open(p))) for p in (args.report_a, args.report_b)) na, nb = args.name_a, args.name_b assert A.keys() == B.keys() n = len(A) print(f"{'':28s}{na:>14s}{nb:>14s}") for label, f in (("driving score", lambda S: sum(v["score"] for v in S.values()) / n), ("success rate %", lambda S: 100 * sum(v["success"] for v in S.values()) / n), ("DS loss on failed routes", lambda S: sum(100 - v["score"] for v in S.values() if not v["success"]) / n), ("DS loss on successes", lambda S: sum(100 - v["score"] for v in S.values() if v["success"]) / n)): print(f"{label:28s}{f(A):14.2f}{f(B):14.2f}") print("\nprimary cause of failure: count (DS loss)") ca, cb = collections.Counter(), collections.Counter() la, lb = collections.Counter(), collections.Counter() for S, c, l in ((A, ca, la), (B, cb, lb)): for v in S.values(): if not v["success"]: g = group(v["cause"]); c[g] += 1; l[g] += (100 - v["score"]) / n for g in sorted(set(ca) | set(cb), key=lambda g: -(la[g] + lb[g])): print(f" {g:38s}{ca[g]:4d} ({la[g]:5.2f}){cb[g]:6d} ({lb[g]:5.2f})") fa = {r for r, v in A.items() if not v["success"]} fb = {r for r, v in B.items() if not v["success"]} print(f"\nfailed in both: {len(fa & fb)} only {na}: {len(fa - fb)} only {nb}: {len(fb - fa)}") for title, rs in ((f"newly failing in {na} (succeeded in {nb})", fa - fb), (f"fixed in {na} (failed in {nb})", fb - fa)): print(f"\n{title}:") for r in sorted(rs, key=lambda r: A[r]["scenario"]): print(f" {r:>6s} {A[r]['scenario']:38s} {na}: {A[r]['score']:5.1f} {group(A[r]['cause']) or 'success'}" f" {nb}: {B[r]['score']:5.1f} {group(B[r]['cause']) or 'success'}") both_same = sum(group(A[r]["cause"]) == group(B[r]["cause"]) for r in fa & fb) print(f"\nfailed in both with the same primary cause: {both_same}/{len(fa & fb)}") print("\nper scenario success (only scenarios that differ):") sa, sb, tot = collections.Counter(), collections.Counter(), collections.Counter() for r in A: s = A[r]["scenario"]; tot[s] += 1; sa[s] += A[r]["success"]; sb[s] += B[r]["success"] for s in sorted(tot, key=lambda s: sa[s] - sb[s]): if sa[s] != sb[s]: print(f" {s:42s}{sa[s]:3d}/{tot[s]:<3d}{sb[s]:6d}/{tot[s]:<3d}{sa[s] - sb[s]:+4d}") if args.out: json.dump({na: A, nb: B}, open(args.out, "w"), indent=1) if __name__ == "__main__": main()