#!/usr/bin/env python3 """Phase A - E-pre: ECHO target signal analysis (feeds E00-E02). Zero-training analysis of the ECHO target R = Z(t+H) - Z(t) in the frozen pooled feature space (main camera), for horizons H in {1, 4, 8, 16} control steps, over all valid (episode, t) pairs of the feature cache with a stride of 4. Reports per horizon: - mean |R| (signal magnitude) and the Huber loss (delta = 1) of the zero-residual prediction, which is the baseline ECHO must beat; - spatial concentration: mean share of total |R| held by the top 8 of 64 patches; - Pearson r between per-sample |R| and the EE displacement |state[t+H] - state[t]| - does the residual track real motion? """ import json from pathlib import Path import torch EXP = Path.home() / "sera_v4_experiments" OUT = EXP / "trace_validation" / "results" / "epre_echo.json" HORIZONS = [1, 4, 8, 16] def main(): cache = torch.load(EXP / "trace_vla" / "feature_cache.pt", weights_only=False) feats, states, episodes = cache["features"], cache["states"], \ cache["episodes"] report = {} for h in HORIZONS: mags, hubers, concs, motions = [], [], [], [] for ep in episodes: s, L = ep["start"], ep["length"] z = feats[s:s + L, 0].float() # [L, 64, 768] st = states[s:s + L] for t in range(0, L - h, 4): r = (z[t + h] - z[t]) # [64, 768] a = r.abs() mags.append(float(a.mean())) hub = torch.where(a <= 1.0, 0.5 * r ** 2, a - 0.5).mean() hubers.append(float(hub)) per_patch = a.mean(-1) # [64] top8 = per_patch.topk(8).values.sum() concs.append(float(top8 / per_patch.sum().clamp(min=1e-9))) motions.append(float((st[t + h, :3] - st[t, :3]) .norm())) mags_t = torch.tensor(mags) mot_t = torch.tensor(motions) r_pearson = float(torch.corrcoef( torch.stack([mags_t, mot_t]))[0, 1]) report[f"H{h}"] = { "n_samples": len(mags), "mean_abs_residual": round(float(mags_t.mean()), 5), "zero_baseline_huber": round(float(torch.tensor(hubers).mean()), 5), "top8_patch_share": round(float(torch.tensor(concs).mean()), 3), "pearson_absres_vs_ee_motion": round(r_pearson, 3), } print(f"H{h}", report[f"H{h}"]) OUT.parent.mkdir(parents=True, exist_ok=True) OUT.write_text(json.dumps(report, indent=1)) print("saved", OUT) if __name__ == "__main__": main()