Download scripts/analysis/cf_batch.py from ZenoNing/ConsDrive: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/ZenoNing/ConsDrive/resolve/main/scripts/analysis/cf_batch.py
- Command line
-
hf download hf://ZenoNing/ConsDrive/scripts/analysis/cf_batch.py
-
curl -L -o cf_batch.py https://huggingface.co/ZenoNing/ConsDrive/resolve/main/scripts/analysis/cf_batch.py
2.93 kB
| """Run the creep counterfactual for every collision in a failure report. | |
| S1 (memoryless creep after a standstill): applies when the ego departed from a standstill <= 30 m before | |
| the impact; simulate from the departure point at rest. | |
| S2 (memory-gated creep): applies when the other actor had been inside the FOV during the last 15 s but was | |
| out of view 1.5 s before impact; simulate from 20 m (arc length) before impact at the recorded speed. | |
| Usage: python cf_batch.py report.json NPZ_DIR extents.json out.json | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| from cf_creep import bystander_index, simulate | |
| def start_20m_before(hero, t_c, dist=20.0): | |
| seg = hero[hero[:, 0] <= t_c] | |
| d = np.concatenate([[0], np.cumsum(np.hypot(np.diff(seg[::-1, 1]), np.diff(seg[::-1, 2])))]) | |
| k = np.searchsorted(d, dist) | |
| k = min(k, len(seg) - 1) | |
| row = seg[::-1][k] | |
| return float(row[0]), float(row[4]) | |
| 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") | |
| others = d["others"] | |
| for c in e["collisions"]: | |
| oid = c.get("other_id") | |
| if oid is None or c["class"] == "static object / layout" or not np.any(others[:, 1] == oid): | |
| continue | |
| ext = extents.get(c["other_type"], (2.4, 1.0)) | |
| bys = bystander_index(d, oid, extents) | |
| row = {"route": e["route"], "scenario": e["scenario"], "t": c["t"], "other": c["other_type"], | |
| "class": c["class"]} | |
| try: | |
| row["replay_actual"] = simulate(d, c["t"], oid, ext, c["t"] - 8.0, 0.0, replay_actual=True)["outcome"] | |
| except Exception as ex: | |
| row["replay_actual"] = f"error {ex!r}" | |
| 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): | |
| t0 = c["t"] - dep | |
| row["S1"] = {f"tau{tau}": simulate(d, c["t"], oid, ext, t0, 0.0, v_cap=3.0, tau=tau, bystanders=bys) | |
| for tau in (0.5, 1.0, None)} | |
| fov = c.get("in_fov_at", {}) | |
| if c.get("fov_time_last15s", 0) > 0 and not fov.get("-1.5s", True): | |
| t0, v0 = start_20m_before(d["hero"], c["t"]) | |
| row["S2"] = {f"tau{tau}": simulate(d, c["t"], oid, ext, t0, v0, v_cap=3.0, tau=tau, bystanders=bys) | |
| for tau in (0.5, 1.0, None)} | |
| results.append(row) | |
| json.dump(results, open(out, "w"), indent=1, default=float) | |
| print(f"{len(results)} collisions simulated -> {out}") | |
| if __name__ == "__main__": | |
| main() | |