"""Route-level fixability of failed routes under S1 (creep after a junction standstill) / S2 (memory-gated creep). A collision counts as visibility-limited when the partner was continuously in the front-camera FOV for < 1.5 s before impact. It is fixed by a strategy when the multi-actor reactive replay (cf_reactive.py) avoids the partner without hitting any recorded actor for every (v_cap, tau) setting. A route is fixable when every infraction other than route deviation / min speed is a collision that is fixed, or that happens after a fixed collision inside its replay window. Route deviation is ignored by request. Usage: python fixability.py ANALYSIS_DIR """ import collections import json import sys import numpy as np IGNORED = {"route_dev", "min_speed_infractions"} COLLISIONS = {"collisions_vehicle", "collisions_pedestrian", "collisions_layout"} AVOIDED = {"passed", "passed_after_recording", "partner_avoided"} VIS_LIMIT_S = 1.5 REAR_THREAT_SCENARIOS = {"ParkingExit"} def verdict(sims): if not sims: return None good = [v["outcome"] in AVOIDED for v in sims.values()] return "fixed" if all(good) else ("fragile" if any(good) else "no") def classify_route(e, cf, npz_dir): infr = {k: len(v) for k, v in e["infractions"].items() if v and k not in IGNORED} if not infr: return "deviation only (ignored)", [] if set(infr) - COLLISIONS - {"collisions_vehicle"}: return "non-collision: " + "/".join(sorted(set(infr) - COLLISIONS)), [] if e["scenario"] in REAR_THREAT_SCENARIOS: return "not addressed: threat from behind", [] rec_end = float(np.load(f"{npz_dir}/RouteScenario_{e['route']}_rep0.npz")["hero"][-1, 0]) per, seen = [], set() for c in sorted(e["collisions"], key=lambda c: c["t"]): key = (c.get("other_id"), round(c["t"], 1)) if key in seen: continue seen.add(key) r = cf.get((e["route"], c.get("other_id"), round(c["t"], 2)), {}) per.append({"t": c["t"], "class": c["class"], "other": c["other_type"], "warning_s": c.get("warning_s"), "vis_limited": c["class"] != "static object / layout" and c.get("warning_s", 99) < VIS_LIMIT_S, "S1": verdict(r.get("S1")), "S2": verdict(r.get("S2"))}) first_fix = next((p for p in per if p["vis_limited"] and "fixed" in (p["S1"], p["S2"])), None) for p in per: if p["vis_limited"] and p["S1"] == "fixed": p["status"] = "S1" elif p["vis_limited"] and p["S2"] == "fixed": p["status"] = "S2" elif p["vis_limited"] and "fragile" in (p["S1"], p["S2"]): p["status"] = "fragile" elif first_fix and first_fix["t"] < p["t"] <= rec_end: p["status"] = "covered" else: p["status"] = "no" st = [p["status"] for p in per] if all(s in ("S1", "covered") for s in st): return "S1 fixes", per if all(s in ("S1", "S2", "covered") for s in st): return "S2 fixes", per if all(s in ("S1", "S2", "covered", "fragile") for s in st): return "S1/S2 fragile", per p = per[st.index("no")] if p["class"] == "static object / layout" or p["class"].startswith("ego hits"): return "not addressed: ego hits lead/stationary/static object", per if p["class"] == "pedestrian": return "not addressed: pedestrian", per if not p["vis_limited"]: return f"not addressed: partner visible >= {VIS_LIMIT_S} s but not yielded to", per return "not addressed: visibility-limited, creep does not help (lane change/merge/flow)", per def main(): adir = sys.argv[1] report = json.load(open(f"{adir}/report.json")) cf = {(r["route"], r["other_id"], round(r["t"], 2)): r for r in json.load(open(f"{adir}/cf_reactive.json"))} out = collections.defaultdict(list) for e in report: if not e["success"]: cat, per = classify_route(e, cf, f"{adir}/npz") out[cat].append({"route": e["route"], "scenario": e["scenario"], "collisions": per}) json.dump(out, open(f"{adir}/fixability.json", "w"), indent=1, default=float) for k in sorted(out): print(f"{len(out[k]):3d} {k}: {' '.join(r['route'] for r in out[k])}") print(f"{sum(map(len, out.values()))} failed routes -> {adir}/fixability.json") if __name__ == "__main__": main()