"""Score forecasts (human CSV sheet or a Harbor job directory) against FrontierCS-2 ground truth. python -I scripts/score_forecasts.py --gt tasks/fcs2-forecast-mlsys-sif.ground_truth.json --csv human_baseline/answer_sheet.csv python -I scripts/score_forecasts.py --gt ... --job [--tasks-dir tasks/fcs2-forecast-mlsys-sif] Metrics (per target): |error| on the 0-100 scale; reward = max(0, 1 - |error|/10); direction = whether the predicted change vs the last score the agent had seen has the right sign (|true change| < 0.5 counts as "flat" and is right when |predicted change| < 0.5 too). """ import argparse, csv, glob, json, math, os, statistics as st from collections import defaultdict def load_preds_csv(path): P = defaultdict(dict) for r in csv.DictReader(open(path)): if r.get('your_score', '').strip(): P[r['item_id']][int(r['target_index'])] = float(r['your_score']) return P def load_preds_job(job): P = defaultdict(dict) meta = {} for d in glob.glob(os.path.join(job, '*/')): cfg = os.path.join(d, 'config.json') if not os.path.exists(cfg): continue c = json.load(open(cfg)) tid = os.path.basename(str((c.get('task') or {}).get('path', '')).rstrip('/')) pred = None for cand in glob.glob(os.path.join(d, 'verifier', 'prediction.json')): pred = cand r = json.load(open(os.path.join(d, 'result.json'))) if os.path.exists(os.path.join(d, 'result.json')) else {} meta[tid] = {'trial': os.path.basename(d.rstrip('/')), 'exception': (r.get('exception_info') or {}).get('exception_type'), 'rewards': (r.get('verifier_result') or {}).get('rewards')} if pred: try: for x in json.load(open(pred))['predictions']: P[tid][int(x['index'])] = float(x['score']) except Exception: pass return P, meta def last_seen(tasks_dir, tid): p = os.path.join(tasks_dir, tid, 'tests', 'ground_truth.json') if os.path.exists(p): return json.load(open(p))['baselines'].get('last_seen') return None def summarize(rows): if not rows: return {} e = [r['err'] for r in rows] d = [r['dir_ok'] for r in rows if r['dir_ok'] is not None] out = {'n': len(rows), 'mae': st.mean(e), 'median_ae': st.median(e), 'reward': st.mean(max(0, 1 - x / 10) for x in e), 'within_5': st.mean(x <= 5 for x in e), 'within_10': st.mean(x <= 10 for x in e), 'direction_acc': st.mean(d) if d else None, 'n_direction': len(d)} t = [r['true'] for r in rows]; p = [r['pred'] for r in rows] if len(rows) > 2 and st.pstdev(t) > 0 and st.pstdev(p) > 0: out['pearson'] = st.correlation(t, p) return out def main(): ap = argparse.ArgumentParser() ap.add_argument('--gt', required=True) ap.add_argument('--csv'); ap.add_argument('--job') ap.add_argument('--tasks-dir', default='tasks/fcs2-forecast-mlsys-sif') ap.add_argument('--out') a = ap.parse_args() GT = json.load(open(a.gt)) P, meta = (load_preds_csv(a.csv), {}) if a.csv else load_preds_job(a.job) rows = [] for tid, preds in P.items(): if tid not in GT: continue ls = last_seen(a.tasks_dir, tid) for h, g in enumerate(GT[tid]): if g['index'] not in preds: continue pv = max(0.0, min(100.0, preds[g['index']])) dir_ok = None if ls is not None: dt, dp = g['score'] - ls, pv - ls dir_ok = (abs(dp) < 0.5) if abs(dt) < 0.5 else (dt * dp > 0) rows.append({'task_id': tid, 'fcs_task': tid.split('--')[0], 'horizon': h, 'kind': g['kind'], 'true': g['score'], 'pred': pv, 'err': abs(pv - g['score']), 'dir_ok': dir_ok}) n_items = len({r['task_id'] for r in rows}) res = {'items_scored': n_items, 'items_in_gt': len(GT), 'overall': summarize(rows), 'by_horizon': {h: summarize([r for r in rows if r['horizon'] == h]) for h in range(3)}, 'by_kind': {k: summarize([r for r in rows if r['kind'] == k]) for k in ('train', 'final')}, 'by_task': {t: summarize([r for r in rows if r['fcs_task'] == t]) for t in sorted({r['fcs_task'] for r in rows})}} if meta: res['trials'] = len(meta); res['trial_exceptions'] = sum(1 for m in meta.values() if m['exception']) print(json.dumps(res, indent=1)) if a.out: json.dump({'summary': res, 'rows': rows}, open(a.out, 'w'), indent=1) if __name__ == '__main__': main()