ConsDrive / scripts /analysis /cf_reactive.py
ZenoNing's picture
Upload ConsDrive v2 source (part 2)
314ecfb verified
Raw History Blame Contribute Delete
2.21 kB
"""Multi-actor reactive creep counterfactual for every collision in a failure report.
S1 (memoryless creep after a junction standstill, not the route-start standstill): start at rest at the
departure point. S2 (memory-gated creep): start 20 m before impact at the recorded speed, applicable when
the partner had been seen during the last 15 s but was out of view 1.5 s before impact.
Usage: python cf_reactive.py report.json NPZ_DIR extents.json out.json
"""
import json
import sys
from pathlib import Path
import numpy as np
from cf_batch import start_20m_before
from cf_creep import simulate_reactive
GRID = [(3.0, 0.5), (3.0, 1.0), (2.0, 0.5), (2.0, 1.0)]
def main():
report, npz_dir, extents, out = sys.argv[1:5]
report = json.load(open(report))
extents = json.load(open(extents))
results = []
for e in report:
if e["success"] or not isinstance(e.get("collisions"), list):
continue
d = np.load(Path(npz_dir) / f"RouteScenario_{e['route']}_rep0.npz")
for c in e["collisions"]:
oid = c.get("other_id")
if oid is None or c["class"] == "static object / layout" or not np.any(d["others"][:, 1] == oid):
continue
ext = extents.get(c["other_type"], (2.4, 1.0))
row = {"route": e["route"], "t": c["t"], "other_id": oid}
dep = c.get("departed_from_stop_s_before")
if dep is not None and c.get("dist_since_departure", 99) <= 30 and not c.get("stop_is_route_start", False):
row["S1"] = {f"v{v}_tau{tau}": simulate_reactive(d, c["t"], oid, ext, c["t"] - dep, 0.0, extents, v_cap=v, tau=tau)
for v, tau in GRID}
if c.get("fov_time_last15s", 0) > 0 and not c.get("in_fov_at", {}).get("-1.5s", True):
t0, v0 = start_20m_before(d["hero"], c["t"])
row["S2"] = {f"v{v}_tau{tau}": simulate_reactive(d, c["t"], oid, ext, t0, v0, extents, v_cap=v, tau=tau)
for v, tau in GRID}
results.append(row)
json.dump(results, open(out, "w"), indent=1, default=float)
print(f"{len(results)} collisions -> {out}")
if __name__ == "__main__":
main()