"""Data for the v3 Opus-vs-truth visualization. Writes results/v3/viz.json. - runs: 10 runs that have clean predictions at all 3 cuts (6 where best-so-far rose after some cut, 4 flat; seed 0), each with its full ground-truth best-so-far step curve, cut times, Opus predicted curves and finals. - horizon: MAE / reward per target for opus, hold, extrap (all tasks, and only targets that rose). - gain: per task at best_deadline: predicted gain (opus - best at cut) vs actual gain (truth - best at cut). """ import json, random, statistics as st, sys, collections sys.path.insert(0, 'v3') import build_v3 as B R = B.OUT + 'full/metadata/' gt = json.load(open(R + 'ground_truth.json')); base = json.load(open(R + 'baselines.json')) idx = {json.loads(l)['task_id']: json.loads(l) for l in open(R + 'task_index.jsonl')} preds = {} for l in open('results/v3/opus55-high.preds.jsonl'): r = json.loads(l); preds[r['task_id']] = {p['target']: p['score'] for p in r['predictions'] if isinstance(p.get('score'), (int, float))} fr = {r['record_id']: r for r in json.load(open(B.FR))['runs']} # horizon table + gain scatter rows = collections.defaultdict(lambda: collections.defaultdict(list)); gain = [] for tid, labs in gt.items(): if tid not in preds: continue bc = idx[tid]['best_at_cut'] for l in labs: if l['status'] != 'ok': continue t, y = l['target'], l['truth'] for name, p in (('opus', preds[tid].get(t)), ('hold', base[tid]['hold'][t]), ('extrap', base[tid]['extrap'][t])): e = 100.0 if p is None else abs(max(0, min(100, p)) - y) rows[t][name].append(e) if l.get('rose'): rows[t + '|rose'][name].append(e) if t == 'best_deadline' and preds[tid].get(t) is not None: gain.append([round(preds[tid][t] - bc, 2), round(y - bc, 2), idx[tid]['fcs_task'], tid]) order = ['best_+15m', 'best_+30m', 'best_+60m', 'best_+120m', 'best_deadline', 'final'] horizon = [] for t in order: for sub in ('', '|rose'): k = t + sub if k not in rows: continue horizon.append({'target': t, 'subset': 'rose' if sub else 'all', 'n': len(rows[k]['opus']), **{f'{m}_mae': round(st.mean(v), 3) for m, v in rows[k].items()}, **{f'{m}_reward': round(st.mean(max(0, 1 - e / 10) for e in v), 4) for m, v in rows[k].items()}}) # example runs byrun = collections.defaultdict(list) for tid in preds: m = idx[tid]; byrun[f"{m['fcs_task']}/{m['model']}"].append(tid) full3 = [r for r, ts in byrun.items() if len(ts) == 3] rose = [r for r in full3 if any(l.get('rose') for t in byrun[r] for l in gt[t])] flat = [r for r in full3 if r not in rose] rng = random.Random(0) pick = sorted(rng.sample(sorted(rose), 6)) + sorted(rng.sample(sorted(flat), 4)) runs = [] for rid in pick: run = fr[rid] fam, U, N, P, VIS = B.analyze(run) t0 = U[0]['t']; mn = lambda t: round((t - t0) / 60000, 2) recv = B.receive_log(U, N, VIS) curve, best = [], None for t, s, i in recv: if best is None or s > best: best = s; curve.append([mn(t), round(s, 4)]) cuts = [] for tid in sorted(byrun[rid], key=lambda x: idx[x]['cut_fraction']): frac = idx[tid]['cut_fraction'] pf, _ = B.make_prefix(run, fam, U, N, P, VIS, frac) cuts.append({'task_id': tid, 'frac': frac, 't_cut': mn(pf['T']), 'best_at_cut': idx[tid]['best_at_cut'], 'pred': [[mn(g), round(preds[tid].get(n, float('nan')), 3)] for n, g in pf['grid']], 'pred_final': preds[tid].get('final'), 'reward': None}) runs.append({'run': rid, 'task': run['task'], 'agent': run.get('candidate_name'), 'category': idx[byrun[rid][0]]['category'], 'budget_min': round(idx[byrun[rid][0]]['budget_s'] / 60, 1), 'curve': curve, 'final': run.get('score'), 'log_end': mn(U[-1]['t']), 'cuts': cuts, 'rose': rid in rose}) json.dump({'runs': runs, 'horizon': horizon, 'gain': gain}, open('results/v3/viz.json', 'w')) print('runs', len(runs), 'gain points', len(gain), 'horizon rows', len(horizon))