Buckets:
| """cwm3/eval/write_pred_layout_joint.py — the joint model's prediction file (train/train_cwm3_joint.py) -> the cwm2 pred_layout.py contract. | |
| Input <run_dir>/pred_{regime}_{split}.npz: hz_on / hz_off f16[N,25,12] head hazards of the 12 chains | |
| (gas, brake, steer_phase, resp | hands_on, head_turned | steer_rate, decel, ttc, ttc_drop, lane, badas), z0 / z0valid [N,12], | |
| valid_V (x8_ok: the V branch needs the 8 s CAN history) / valid_U / valid_E [N], pi_resp [N,25], y_mae / y_crps [N,3], f_cos [N,5], | |
| k_mae [N], K_hat [N,3,11] (standardised K̂ at +1 / +3 / +5 s; consumed by the warning head, not by the evaluators). | |
| Everything below is CLOSED FORM under the model's chain structure (chains conditionally independent given the context; the interaction I | |
| changes the hazards, not the composition) — no sampled paths, no Monte-Carlo noise; score_source = independent_marginals: | |
| onset_p[N,4,E] 1 - prod_{k<tau}(1 - h_on,k) for the 14 EVENT_NAMES; veh_any_on = 1 - prod_c P(no onset of chain c within tau | z0_c) | |
| with the 3-state (0 / 1 / absorbed) recursion of `no_event_prob`. | |
| h_on[N,25,E] per-step hazards (hazard-NLL cells). | |
| ep_p[N,P] cross-leg endpoints (populations.py definitions, WIN = 10 steps, k* <= 14): | |
| C1 sum_k q^hands_k * P(any vehicle onset in (k, k+10]) / sum_k q^hands_k q = first-passage law of hand_return from z0 = 0 | |
| C1_given_k P(any vehicle onset in (k*, k*+10]) at the OBSERVED first hand_return k* (kin S / S0 / EM), NaN when none in [0,14] | |
| C2_<f> sum_k q^{D_f}_k * P(away_k AND driver return in (k, k+10]) / sum_k q^{D_f}_k * P(away_k) | |
| away = hands_on = 0 OR head_turned = 1; return = hand_return (hands_on 0->1) OR head_return (head_turned 1->0); | |
| hands and head chains independent -> P(away AND return) summed over the (A_h, A_d) state combinations | |
| C2_hand_<f> / C2_head_<f> the single-chain versions; C2_given_k_<f> P(return | away) at the OBSERVED demand onset k* | |
| w[N,Wn] W_direct_t{15,10}_w{10,15}_RV_{6fam,warn4} (first_passage.w_direct on the hazard marginals; V rows only), | |
| driver_only_t{15,10} (pi_away), demand_only_t{15,10}_{6fam,warn4} (independent-family any-demand marginal). | |
| w_mask = True on every row; W_direct is NaN where valid_V is False (the evaluator drops those rows from the common set). | |
| p_onset_p / p_pi / p_z0 / p_names, r_onset_p / r_pi / r_z0 / r_names detached road-pressure (6) and hard-response (1) chains — extra keys | |
| outside the cwm2 contract (ignored by eval_grid / warning_gold), read by the warning head only. | |
| gaze_off_onset_p / gaze_off_pi / gaze_off_h_on / gaze_off_h_off / gaze_off_z0 / gaze_off_z0valid / gaze_off_taus (gaze U bases, 2026-09-12 appendix variant; | |
| the base writer's block verbatim; extra keys, contract columns unchanged) | |
| nr_p[N,25] (1 - pi_k) prod_{j=k+1..k+15}(1 - h_on,j) from the resp chain (as write_pred_layout.py); cooccur_p[N] = P(any driver | |
| onset within 3 s) * P(any vehicle onset within 3 s) (independence); y_mae / y_crps / f_cos copied. | |
| meta: arm cwm3_I (stage I1) / cwm3_F (F1), regime, seed, split, score_source independent_marginals, roi route-median (non-causal) (the | |
| driver inputs), model_id with the base checkpoints, code_sha256, inputs_digest, dms_fields_used []. | |
| No openpilot DMS field is read (targets / kin / demand files for the uid-t witness and the observed k* only). | |
| python write_pred_layout_joint.py --run-dir results/runs/I1_all_s0 [--regimes manual,engaged] [--splits test,val] | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| CWM3 = Path(__file__).resolve().parents[1] | |
| EXPLORE = CWM3.parent | |
| CWM2 = EXPLORE / "cwm2" | |
| S12R = EXPLORE / "s12_wm" / "results" | |
| KIN = CWM2 / "results" / "targets_kin" | |
| DEM = CWM2 / "results" / "targets_demand" | |
| for p in (CWM2 / "eval", CWM3 / "common", CWM3 / "eval"): | |
| sys.path.insert(0, str(p)) | |
| import first_passage as fp # noqa: E402 | |
| from pred_layout import EVENT_NAMES, EP_NAMES, ONSET_TAUS, FAMILIES, WARNABLE, w_name # noqa: E402 | |
| from runmeta import code_hash, sha256_file # noqa: E402 | |
| T, WIN, KMAX = 25, 10, 14 | |
| CHAINS = ["gas", "brake", "steer_phase", "resp", "hands_on", "head_turned", "steer_rate", "decel", "ttc", "ttc_drop", "lane", "badas"] | |
| ARM_OF = {"I1": "cwm3_I", "F1": "cwm3_F"} | |
| EXTERNAL_PRIORS = ["YOLO11x-pose: driver kinematics Kh/K/S (route-median ROI, non-causal)", | |
| "V-JEPA2 ViT-g 384: front region tokens / pooled Vf (PCA) and F targets", "OpenBADAS: `badas` family of the D target"] | |
| KINC = {"v1": CWM3 / "results" / "targets_kin_causal_routes", "v2": CWM3 / "results" / "targets_kin_causal_routes_v2", | |
| "v3": CWM3 / "results" / "targets_kin_causal_routes_v3", "v2p": CWM3 / "results" / "targets_kin_causal_routes_v2p"} # v2p (2026-09-18): S_c / S0_c / EM_c bit-identical to v2 | |
| def roi_of(kin_source): | |
| """meta.roi of the driver inputs: the route-median crop cache (cwm2) or the causal per-frame box (kin_source causal_routes; 2026-09-11 final assembly).""" | |
| return "causal_clip_roi" if kin_source == "causal_routes" else "route-median (non-causal)" | |
| def external_priors(kin_source="cwm2", kin_version="v1"): | |
| if kin_source == "causal_routes": | |
| return [f"YOLO11x-pose causal driver box + RTMW-x-l HALPE-136 @10 Hz: causal route-level driver kinematics Kh/K/S ({KINC[kin_version].name}; " | |
| f"{'population driver-side prior on 74 co-occupant routes' if kin_version in ('v2', 'v3', 'v2p') else 'largest-area fallback'}" | |
| f"{'; v2p: + 3 keypoint input proxies head_pitch_kp / wrist_face_kp / hands_lap_kp' if kin_version == 'v2p' else ''})"] + EXTERNAL_PRIORS[1:] | |
| return list(EXTERNAL_PRIORS) | |
| def load_kin_states(regime, split, tg, kin_source="cwm2", kin_version="v1"): | |
| """S / S0 / EM of the driver targets used for the OBSERVED first hand_return k* (C1_given_k): the cwm2 route-median file (default) or the causal | |
| route-level kin pack of the run's kin_source (keys *_c; rows without a causal box censored) — the same definition the paired evaluator uses with | |
| --kin-targets causal_routes. -> (S [N,25,2] f32, S0 [N,2] f32, EM [N,25,3] bool, source path).""" | |
| if kin_source in ("causal_routes", "oldin_ctgt"): | |
| kp = KINC[kin_version] / f"kin_causal_{regime}_{split}.npz" | |
| kin = np.load(kp, allow_pickle=True) | |
| assert np.array_equal(kin["uid"], tg["uid"]) and np.allclose(kin["t"], tg["t"]), f"{kp}: uid/t witness" | |
| ok = kin["row_ok"].astype(bool) | |
| S = kin["S_c"].astype(np.float32); S[~ok] = np.nan | |
| S0 = kin["S0_c"].astype(np.float32); S0[~ok] = np.nan | |
| EM = kin["EM_c"].astype(bool) & tg["M"].astype(bool)[:, :, None] & ok[:, None, None] | |
| return S, S0, EM, kp | |
| kp = KIN / f"kin_{regime}_{split}.npz" | |
| kin = np.load(kp, allow_pickle=True) | |
| assert np.array_equal(kin["uid"], tg["uid"]) | |
| return kin["S"].astype(np.float32), kin["S0"].astype(np.float32), kin["EM"].astype(bool) & tg["M"].astype(bool)[:, :, None], kp | |
| EVENT_SRC = {"gas_on": ("gas", "on"), "brake_on": ("brake", "on"), "steer_phase_on": ("steer_phase", "on"), "resp_on": ("resp", "on"), | |
| "hand_return": ("hands_on", "on"), "hand_leave": ("hands_on", "off"), "head_turn_onset": ("head_turned", "on"), "head_return": ("head_turned", "off"), | |
| **{f"D_{f}": (f, "on") for f in FAMILIES}} | |
| BRANCH_OF = {c: ("V" if c in ("gas", "brake", "steer_phase", "resp") else "U" if c in ("hands_on", "head_turned") else "E") for c in CHAINS} | |
| # ----------------------------------------------------------------------------- chain algebra (numpy, [N,T]) | |
| def forward_marginal(h_on, h_off, z0): | |
| pi = np.empty_like(h_on) | |
| prev = z0.astype(np.float64) | |
| for k in range(h_on.shape[1]): | |
| prev = prev * (1 - h_off[:, k]) + (1 - prev) * h_on[:, k] | |
| pi[:, k] = prev | |
| return pi | |
| def first_onset_law(h_on): | |
| """q_k = P(first 0->1 at step k | z0 = 0) = prod_{j<k}(1 - h_on_j) h_on_k. [N,T]""" | |
| surv = np.cumprod(1 - h_on, 1) | |
| prev = np.concatenate([np.ones((h_on.shape[0], 1)), surv[:, :-1]], 1) | |
| return prev * h_on | |
| def no_event_prob(h_on, h_off, k, W, p0, p1, kind="on"): | |
| """P(no 0->1 (kind on) / no 1->0 (kind off) transition in steps k+1..k+W | state law (p0, p1) at step k). Steps beyond T-1 ignored. | |
| 3-state recursion: (0, not yet), (1, not yet), absorbed.""" | |
| p0, p1 = p0.astype(np.float64).copy(), p1.astype(np.float64).copy() | |
| for j in range(k + 1, min(k + W, T - 1) + 1): | |
| hon, hoff = h_on[:, j], h_off[:, j] | |
| if kind == "on": | |
| n0 = p0 * (1 - hon) + p1 * hoff | |
| n1 = p1 * (1 - hoff) | |
| else: | |
| n0 = p0 * (1 - hon) | |
| n1 = p1 * (1 - hoff) + p0 * hon | |
| p0, p1 = n0, n1 | |
| return p0 + p1 | |
| def onset_within(h_on, h_off, z0, tau): | |
| """P(at least one 0->1 transition within steps 0..tau-1 | z0) — general (z0 may be 1).""" | |
| z0 = z0.astype(np.float64) | |
| return 1 - no_event_prob(h_on, h_off, -1, tau, 1 - z0, z0, "on") | |
| # ----------------------------------------------------------------------------- observed k* (targets only; the population rule) | |
| def observed_first_onset(z0, z, em): | |
| zp = np.concatenate([z0[:, None], z[:, :-1]], 1) | |
| onset = (np.nan_to_num(z, nan=0.0) > 0.5) & (np.nan_to_num(zp, nan=1.0) < 0.5) & em | |
| return np.where(onset.any(1), onset.argmax(1), -1) | |
| def write_layout(run_dir, regime, split, run_cfg, code_files, log=print): | |
| t0 = time.time() | |
| rd = Path(run_dir) | |
| pf = rd / f"pred_{regime}_{split}.npz" | |
| z = np.load(pf, allow_pickle=True) | |
| seed, stage = int(z["seed"]), str(z["stage"]) | |
| tg = np.load(S12R / f"targets_{regime}_{split}.npz", allow_pickle=True) | |
| N = len(tg["t"]) | |
| chains = [str(c) for c in z["chain_names"]] | |
| assert chains == CHAINS and z["hz_on"].shape == (N, T, len(CHAINS)), f"{pf}: chain layout / rows" | |
| hz_on, hz_off = z["hz_on"].astype(np.float64), z["hz_off"].astype(np.float64) | |
| hz_on, hz_off = np.clip(hz_on, 1e-6, 1 - 1e-6), np.clip(hz_off, 1e-6, 1 - 1e-6) | |
| z0 = z["z0"].astype(np.float64) | |
| valid = {"V": z["valid_V"].astype(bool), "U": z["valid_U"].astype(bool), "E": z["valid_E"].astype(bool)} | |
| ci = {c: i for i, c in enumerate(CHAINS)} | |
| H = lambda c: (hz_on[:, :, ci[c]], hz_off[:, :, ci[c]], z0[:, ci[c]]) # noqa: E731 | |
| E_ = len(EVENT_NAMES) | |
| # ---- onset probabilities + hazards | |
| h = np.full((N, T, E_), np.nan, np.float32) | |
| onset_p = np.full((N, len(ONSET_TAUS), E_), np.nan, np.float32) | |
| emitted = [] | |
| for e, ev in enumerate(EVENT_NAMES): | |
| if ev == "veh_any_on": | |
| ok = valid["V"] | |
| for ti, tau in enumerate(ONSET_TAUS): | |
| none = np.ones(N) | |
| for c in ("gas", "brake", "steer_phase"): | |
| hon, hoff, zz = H(c) | |
| none *= 1 - onset_within(hon, hoff, zz, tau) | |
| onset_p[ok, ti, e] = (1 - none)[ok] | |
| emitted.append(ev) | |
| continue | |
| c, kind = EVENT_SRC[ev] | |
| ok = valid[BRANCH_OF[c]] | |
| hh = (hz_on if kind == "on" else hz_off)[:, :, ci[c]] | |
| h[ok, :, e] = hh[ok] | |
| for ti, tau in enumerate(ONSET_TAUS): | |
| onset_p[ok, ti, e] = (1 - np.prod(1 - hh[:, :tau], 1))[ok] | |
| emitted.append(ev) | |
| # ---- chain marginals / first-onset laws | |
| pi = {c: forward_marginal(*H(c)) for c in CHAINS} | |
| q = {c: first_onset_law(hz_on[:, :, ci[c]]) for c in CHAINS} | |
| # vehicle: P(any onset in (k, k+WIN]) for k = 0..KMAX from the marginal state law at k | |
| veh_win = np.ones((N, KMAX + 1)) | |
| for c in ("gas", "brake", "steer_phase"): | |
| hon, hoff, _ = H(c) | |
| for k in range(KMAX + 1): | |
| veh_win[:, k] *= no_event_prob(hon, hoff, k, WIN, 1 - pi[c][:, k], pi[c][:, k], "on") | |
| veh_win = 1 - veh_win | |
| # driver: per k, a_h = P(hands_on = 0 at k), r_h0 = P(hand_return in window | hands 0 at k), r_h1 (| hands 1); head analogues | |
| hon_h, hoff_h, _ = H("hands_on") | |
| hon_d, hoff_d, _ = H("head_turned") | |
| one, zero = np.ones(N), np.zeros(N) | |
| a_h = 1 - pi["hands_on"] | |
| a_d = pi["head_turned"] | |
| r_h0 = np.stack([1 - no_event_prob(hon_h, hoff_h, k, WIN, one, zero, "on") for k in range(KMAX + 1)], 1) | |
| r_h1 = np.stack([1 - no_event_prob(hon_h, hoff_h, k, WIN, zero, one, "on") for k in range(KMAX + 1)], 1) | |
| r_d0 = np.stack([1 - no_event_prob(hon_d, hoff_d, k, WIN, zero, one, "off") for k in range(KMAX + 1)], 1) # head turned at k | |
| r_d1 = np.stack([1 - no_event_prob(hon_d, hoff_d, k, WIN, one, zero, "off") for k in range(KMAX + 1)], 1) # head straight at k | |
| ah, ad = a_h[:, :KMAX + 1], a_d[:, :KMAX + 1] | |
| away_and_ret = (ah * ad * (1 - (1 - r_h0) * (1 - r_d0)) + ah * (1 - ad) * (1 - (1 - r_h0) * (1 - r_d1)) | |
| + (1 - ah) * ad * (1 - (1 - r_h1) * (1 - r_d0))) | |
| away = 1 - (1 - ah) * (1 - ad) | |
| # ---- cross-leg endpoints | |
| ep_p = np.full((N, len(EP_NAMES)), np.nan, np.float32) | |
| ep_mask = np.zeros((N, len(EP_NAMES)), bool) | |
| def put(name, val, ok): | |
| j = EP_NAMES.index(name) | |
| ep_p[ok, j] = val[ok] | |
| ep_mask[ok, j] = True | |
| okC1 = valid["V"] & valid["U"] | |
| qh = q["hands_on"][:, :KMAX + 1] | |
| put("C1", (qh * veh_win).sum(1) / np.maximum(qh.sum(1), 1e-12), okC1) | |
| kin_source = str(run_cfg.get("cfg", {}).get("kin_source", "cwm2") or "cwm2") | |
| kin_version = str(run_cfg.get("cfg", {}).get("kin_version", "v1") or "v1") | |
| S, S0, EM, kin_path = load_kin_states(regime, split, tg, kin_source, kin_version) | |
| dm = np.load(DEM / f"demand_{regime}_{split}.npz", allow_pickle=True) | |
| assert np.array_equal(dm["uid"], tg["uid"]) | |
| k_obs = observed_first_onset(S0[:, 0], S[:, :, 0], EM[:, :, 0]) | |
| ok_k = okC1 & (k_obs >= 0) & (k_obs <= KMAX) | |
| val = np.full(N, np.nan) | |
| val[ok_k] = veh_win[ok_k, k_obs[ok_k]] | |
| put("C1_given_k", val, ok_k) | |
| okC2 = valid["E"] & valid["U"] | |
| fam_names = [str(f) for f in dm["families"]] | |
| for f in FAMILIES: | |
| qd = q[f][:, :KMAX + 1] | |
| put(f"C2_{f}", (qd * away_and_ret).sum(1) / np.maximum((qd * away).sum(1), 1e-12), okC2) | |
| put(f"C2_hand_{f}", (qd * ah * r_h0).sum(1) / np.maximum((qd * ah).sum(1), 1e-12), okC2) | |
| put(f"C2_head_{f}", (qd * ad * r_d0).sum(1) / np.maximum((qd * ad).sum(1), 1e-12), okC2) | |
| fi = fam_names.index(f) | |
| Dz, D0z = dm["D"][:, :, fi].astype(np.float32), dm["D0"][:, fi].astype(np.float32) | |
| ko = observed_first_onset(D0z, Dz, np.ones_like(Dz, bool)) | |
| okk = okC2 & (ko >= 0) & (ko <= KMAX) | |
| val = np.full(N, np.nan) | |
| val[okk] = away_and_ret[okk, ko[okk]] / np.maximum(away[okk, ko[okk]], 1e-12) | |
| put(f"C2_given_k_{f}", val, okk) | |
| # ---- warnings | |
| pi_fam = np.stack([pi[f] for f in FAMILIES], -1) # [N,T,6] | |
| pi_resp = pi["resp"] | |
| pi_away = 1 - pi["hands_on"] * (1 - pi["head_turned"]) | |
| wcols = {} | |
| for tau, w in ((15, 10), (10, 15)): | |
| for dname, fams in (("6fam", list(FAMILIES)), ("warn4", list(WARNABLE))): | |
| pi_D = fp.any_demand_marginal_independent(pi_fam[:, :, [FAMILIES.index(f) for f in fams]]) | |
| wd = fp.w_direct(pi_D, pi_resp, tau, w) | |
| wd[~valid["V"]] = np.nan | |
| wcols[w_name("W_direct", tau, w, "RV", dname)] = wd | |
| wcols[w_name("demand_only", tau, 0, demand=dname)] = fp.demand_only(pi_D, tau) | |
| wcols[w_name("driver_only", tau, 0)] = fp.driver_only(pi_away, tau) | |
| w_names = list(wcols) | |
| W = np.stack([wcols[k] for k in w_names], 1).astype(np.float32) | |
| # ---- nr_p, cooccur, per-row means | |
| h_resp = hz_on[:, :, ci["resp"]] | |
| lc = np.concatenate([np.zeros((N, 1)), np.cumsum(np.log1p(-np.clip(h_resp, 0, 1 - 1e-12)), 1)], 1) | |
| nr = np.full((N, T), np.nan) | |
| for k in range(T): | |
| if k + 15 <= T - 1: | |
| nr[:, k] = (1 - pi_resp[:, k]) * np.exp(lc[:, k + 16] - lc[:, k + 1]) | |
| nr[~valid["V"]] = np.nan | |
| drv_none = np.ones(N) | |
| for c, kind in (("hands_on", "on"), ("hands_on", "off"), ("head_turned", "on")): | |
| hon, hoff, zz = H(c) | |
| drv_none *= no_event_prob(hon, hoff, -1, 15, 1 - zz, zz, kind) | |
| veh_none = np.ones(N) | |
| for c in ("gas", "brake", "steer_phase"): | |
| hon, hoff, zz = H(c) | |
| veh_none *= no_event_prob(hon, hoff, -1, 15, 1 - zz, zz, "on") | |
| cooccur = ((1 - drv_none) * (1 - veh_none)).astype(np.float32) | |
| cooccur[~(valid["V"] & valid["U"])] = np.nan | |
| # detached auxiliary chains (road pressure P, hard response R): extra keys outside the cwm2 contract, consumed by the warning head only | |
| extra = {} | |
| if "hz_on_P" in z.files: | |
| for key, names in (("P", [str(c) for c in z["p_chain_names"]]), ("R", [str(c) for c in z["r_chain_names"]])): | |
| hon = np.clip(z[f"hz_on_{key}"].astype(np.float64), 1e-6, 1 - 1e-6) | |
| hoff = np.clip(z[f"hz_off_{key}"].astype(np.float64), 1e-6, 1 - 1e-6) | |
| zz0 = z[f"z0_{key}"].astype(np.float64) | |
| op = np.stack([1 - np.prod(1 - hon[:, :tau], 1) for tau in ONSET_TAUS], 1) # [N,4,C] | |
| pim = np.stack([forward_marginal(hon[:, :, c], hoff[:, :, c], zz0[:, c]) for c in range(hon.shape[-1])], -1) | |
| if key == "R": | |
| op[~valid["V"]] = np.nan | |
| pim[~valid["V"]] = np.nan | |
| extra[f"{key.lower()}_names"] = np.array(names) | |
| extra[f"{key.lower()}_onset_p"] = op.astype(np.float16) | |
| extra[f"{key.lower()}_pi"] = pim.astype(np.float16) | |
| extra[f"{key.lower()}_z0"] = zz0.astype(np.float16) | |
| if "hz_on_G" in z.files: | |
| # gaze_off chain of the gaze U bases (2026-09-12 appendix variant): the SAME extra keys and arithmetic as the base layout writer's block | |
| # (eval/write_pred_layout.py, `hz_on_G`), so eval/verify_identity_final.py can require max |Δ| = 0 on gaze_off_h_on / h_off between the assembled | |
| # `indep` joint and its U base; rows = valid_U AND anchor gaze state defined; the 14 contract event columns are untouched (warning head input only). | |
| hon = np.clip(z["hz_on_G"].astype(np.float64)[..., 0], 1e-6, 1 - 1e-6) | |
| hoff = np.clip(z["hz_off_G"].astype(np.float64)[..., 0], 1e-6, 1 - 1e-6) | |
| zz0 = z["z0_G"].astype(np.float64)[:, 0] | |
| z0v = z["z0valid_G"].astype(bool)[:, 0] | |
| op = np.stack([1 - np.prod(1 - hon[:, :tau], 1) for tau in ONSET_TAUS], 1) # [N,4] | |
| pim = np.empty_like(hon) | |
| prev = zz0.copy() | |
| for k in range(hon.shape[1]): | |
| prev = prev * (1 - hoff[:, k]) + (1 - prev) * hon[:, k] | |
| pim[:, k] = prev | |
| bad = ~(valid["U"] & z0v) | |
| for arr in (op, pim, hon, hoff): | |
| arr[bad] = np.nan | |
| zz0[bad] = np.nan | |
| extra.update(gaze_off_onset_p=op.astype(np.float16), gaze_off_pi=pim.astype(np.float16), gaze_off_h_on=hon.astype(np.float16), gaze_off_h_off=hoff.astype(np.float16), | |
| gaze_off_z0=zz0.astype(np.float16), gaze_off_z0valid=(valid["U"] & z0v), gaze_off_taus=np.array(ONSET_TAUS)) | |
| y_mae, y_crps, f_cos = z["y_mae"].astype(np.float32), z["y_crps"].astype(np.float32), z["f_cos"].astype(np.float32) | |
| y_mae[~valid["V"]] = np.nan | |
| y_crps[~valid["V"]] = np.nan | |
| f_cos[~valid["E"]] = np.nan | |
| arm = ARM_OF.get(stage, "cwm3_I") | |
| meta = {"arm": arm, "regime": regime, "seed": seed, "split": split, "score_source": "independent_marginals", "n_samples": 0, "sample_seed": "", | |
| "roi": roi_of(kin_source), "roi_streams": ["Kh", "K", "S"], "driver_input_shift_s": 0, "kin_source": kin_source, "kin_version": kin_version, | |
| "kin_states_file": str(kin_path), | |
| "model_id": f"{stage}_{str(z['variant'])}_s{seed}:{str(z['best_ckpt'])}", "base_ckpts": json.loads(str(z["base_ckpts"])) if "base_ckpts" in z.files else {}, | |
| "code_sha256": code_hash(code_files), "inputs_digest": sha256_file(pf), "external_priors": external_priors(kin_source, kin_version), "dms_fields_used": [], | |
| "hazard_kind": "head", "n_rows_predicted": int(valid["U"].sum()), "n_rows_uniform": int(valid["U"].sum()), "n_rows_V": int(valid["V"].sum()), | |
| "events_emitted": emitted, "absent_blocks": [], "stage": stage, "variant": str(z["variant"]), "cfg": run_cfg.get("cfg", {}), | |
| "composition": "closed-form independence composition of the chain hazards (no sampled paths); C1/C2 per the populations.py definitions " | |
| "(WIN 10 steps, k* <= 14); *_given_k at the observed k* from the kin / demand targets", | |
| "nr_p_form": "chain_form: (1-pi_k) * prod_{j=k+1..k+15}(1-h_on_j) from the resp chain", | |
| "notes": "cwm3 joint model (V can8s: vehicle-side blocks NaN on rows without the 8 s CAN history; U on rows with a driver box (valid_U; every row for the " | |
| "route-median cache, kin_row_ok for the causal packs); E on every row)"} | |
| out = {"event_names": np.array(EVENT_NAMES), "onset_taus": np.array(ONSET_TAUS), "onset_p": onset_p.astype(np.float16), "h_on": h.astype(np.float16), | |
| "uniform_mask": valid["U"].copy(), "uid": tg["uid"], "t": tg["t"], "ep_names": np.array(EP_NAMES), "ep_p": ep_p.astype(np.float16), "ep_mask": ep_mask, | |
| "w_names": np.array(w_names), "w": W.astype(np.float16), "w_mask": np.ones(N, bool), "nr_p": nr.astype(np.float16), "cooccur_p": cooccur.astype(np.float16), | |
| "y_mae": y_mae, "y_crps": y_crps, "f_cos": f_cos, "pi_D": fp.any_demand_marginal_independent(pi_fam).astype(np.float16), | |
| "pi_NR": fp.nr_marginal_independent(pi_resp, 10).astype(np.float16)} | |
| out.update(extra) | |
| meta["aux_blocks"] = sorted(extra) | |
| outp = rd / f"pred_layout_{regime}_{split}.npz" | |
| np.savez_compressed(outp, meta=np.array(json.dumps(meta)), **out) | |
| log(f"[layout-joint] {outp.name}: N={N} rows_V={int(valid['V'].sum())} events={len(emitted)} ep={int(ep_mask.any(1).sum())} w={len(w_names)} ({time.time() - t0:.0f}s)") | |
| return outp | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--run-dir", required=True) | |
| ap.add_argument("--regimes", default="manual,engaged") | |
| ap.add_argument("--splits", default="test,val") | |
| a = ap.parse_args() | |
| rd = Path(a.run_dir) | |
| runs = sorted(rd.glob("run_train_*_latest.json")) | |
| run = json.loads(runs[-1].read_text()) if runs else {} | |
| code_files = [Path(__file__), CWM2 / "eval" / "first_passage.py", CWM2 / "eval" / "pred_layout.py"] + sorted((CWM3 / "models").glob("*.py")) | |
| for r in a.regimes.split(","): | |
| for s in a.splits.split(","): | |
| if (rd / f"pred_{r}_{s}.npz").exists(): | |
| write_layout(rd, r, s, run, code_files) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 22.9 kB
- Xet hash:
- 505df283c12eeb6ad8b97a19d789e08488d0a150e07b599a02f54a7b7fb89600
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.