AsianPlayer/TriDrive / code /eval /kin_clip_fraction.py
AsianPlayer's picture
download
raw
3.74 kB
"""cwm3/eval/kin_clip_fraction.py — how much of the standardised kinematics history Kh the ±K_CLIP (= 5) clamp of train/batch.std_kh truncates,
per channel, under the v1 (pre-fix float32) and v2 (float64) statistics, on the TRAIN split (both regimes) of the causal packs. Also verifies
that compute_stats_v1 reproduces the stored v1 statistics of a v1 run (results/runs/U0_kin_can_noK_s0_kcv1/stats.npz) on the v1 packs.
python eval/kin_clip_fraction.py [--out results/tables/k4_v2_clip_fraction.json] (coordinator request 2026-09-11; CPU, no DMS field)
"""
from __future__ import annotations
import argparse, json, sys, time
from pathlib import Path
import numpy as np
CWM3 = Path(__file__).resolve().parents[1]
for p in (CWM3, CWM3 / "data", CWM3 / "common", CWM3 / "train"):
sys.path.insert(0, str(p))
import cwm3_dataset as ds # noqa: E402
import batch as bt # noqa: E402
from runmeta import write_run_json # noqa: E402
def clip_frac(Kh, st, clip=bt.K_CLIP):
"""per-channel fraction of FINITE Kh values with |(Kh - kmu) / ksd| > clip (what torch.clamp truncates), chunked over rows."""
K = Kh.reshape(-1, bt.DK)
over = np.zeros(bt.DK, np.int64); fin = np.zeros(bt.DK, np.int64)
for i in range(0, len(K), 2_000_000):
z = (K[i:i + 2_000_000].astype(np.float32) - st["kmu"]) / st["ksd"]
ok = np.isfinite(z)
over += (ok & (np.abs(z) > clip)).sum(0); fin += ok.sum(0)
return over / np.maximum(fin, 1), fin
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--out", default=str(CWM3 / "results" / "tables" / "k4_v2_clip_fraction.json"))
a = ap.parse_args()
t0 = time.time()
S12R = CWM3.parent / "s12_wm" / "results"
z = np.load(S12R / "target_stats.npz"); tgt = {"mu": z["mu"], "sd": z["sd"]}
out = {"utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "clip": bt.K_CLIP, "channels": ds.KIN_CHANNELS, "packs": {}}
for ver in ("v1", "v2"):
ds.set_kin_source("causal_routes", ver)
raw = ds.load_regimes(["manual", "engaged"], "train", {"X", "Kh", "Y"}, verbose=lambda m: None)
rec = {"n_rows": int(len(raw["t"]))}
for sv in (1, 2):
st = bt.compute_stats(raw, tgt, version=sv)
fr, fin = clip_frac(raw["Kh"], st)
rec[f"stats{sv}"] = {"kmu": st["kmu"].tolist(), "ksd": st["ksd"].tolist(), "xmu": st["xmu"].tolist(), "xsd": st["xsd"].tolist(),
"clip_frac": fr.tolist(), "clip_frac_all": float((fr * fin).sum() / fin.sum()), "n_finite": fin.tolist()}
print(f"{ver} packs, stats v{sv}: clip>|5| overall {rec[f'stats{sv}']['clip_frac_all']:.5f}; per channel " + " ".join(f"{c}={f:.4f}" for c, f in zip(ds.KIN_CHANNELS, fr)), flush=True)
if ver == "v1":
ref = np.load(CWM3 / "results" / "runs" / "U0_kin_can_noK_s0_kcv1" / "stats.npz")
st1 = bt.compute_stats(raw, tgt, version=1)
rec["v1_reproduction"] = {k: float(np.max(np.abs(st1[k] - ref[k]))) for k in ("kmu", "ksd", "xmu", "xsd")}
print("v1 reproduction max|diff| vs stored kcv1 stats:", rec["v1_reproduction"], flush=True)
out["packs"][ver] = rec
del raw
ds.set_kin_source("cwm2", "v1")
Path(a.out).write_text(json.dumps(out, indent=1))
write_run_json(CWM3 / "results" / "tables", "kin_clip_fraction", [Path(__file__), CWM3 / "train" / "batch.py", CWM3 / "data" / "cwm3_dataset.py"], [],
seed=0, t_start=t0, input_listing=False, extra={"out": a.out, "dms_fields_used": []})
print(f"written {a.out} ({time.time() - t0:.0f}s)")
if __name__ == "__main__":
main()

Xet Storage Details

Size:
3.74 kB
·
Xet hash:
6f52a20e194d22f68196d50511e206b6801b78d7669874dccaa34a49563f7360

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.