"""Write results/v3_tables.md from v3_candidate_summary.csv and v3_skills_by_origin.csv.""" import json, sys import numpy as np, pandas as pd sys.path.insert(0, str(__import__('pathlib').Path(__file__).resolve().parent)) from v2lib import ROOT, METRICS from report_v2 import md RES = ROOT / 'results' def main(): sel = json.loads((RES / 'v3_selection.json').read_text()); ref = sel['reference'] t = pd.read_csv(RES / 'v3_candidate_summary.csv'); sk = pd.read_csv(RES / 'v3_skills_by_origin.csv'); sk = sk[sk.split != 'all'] rows = [] for _, r in t.iterrows(): R = t[(t.metric == r.metric) & (t.method == ref[r.metric])].iloc[0] rows.append(dict(metric=r.metric, method=r.method, dev_h47=round(r.dev_final_like, 4), late_h47=round(r.late_final_like, 4), dev_h13=round(r.dev_live_like, 4), late_h13=round(r.late_live_like, 4), d_dev_vs_ref=round(r.dev_final_like - R.dev_final_like, 4), d_late_vs_ref=round(r.late_final_like - R.late_final_like, 4), passes=('reference' if r.method == ref[r.metric] else ('yes' if r.dev_final_like > R.dev_final_like and r.late_final_like >= R.late_final_like else 'no')))) cand = pd.DataFrame(rows).sort_values(['metric', 'dev_h47'], ascending=[True, False]) best = {m: cand[(cand.metric == m) & ~cand.method.isin(['clim30', 'exp10'])].iloc[0].method for m in METRICS} models = {'v2 strict': {'annual_avg_temp': 'v2', 'cdd': 'exp10', 'total_precip_in': 'clim30', 'hottest_day_temp': 'clim30'}, 'v2b': {'annual_avg_temp': 'v2', 'cdd': 'v2b', 'total_precip_in': 'clim30', 'hottest_day_temp': 'clim30'}, 'v3 selected (= v2b)': sel['selection'], 'best-dev per metric (NOT adopted; fails late check)': best} per, summ = [], [] for name, ch in models.items(): g = pd.concat([sk[(sk.metric == m) & (sk.method == ch[m])] for m in METRICS]) p = g.pivot_table(index=['origin', 'split'], columns='metric', values='skill'); assert len(p) == 54 p['mean'] = p[METRICS].mean(1); p['model'] = name; per.append(p.reset_index()) per = pd.concat(per); per['set'] = np.where(per.origin <= 2004, 'dev', 'late') s = per.groupby(['model', 'set', 'split'])[METRICS + ['mean']].mean().reset_index() s['score_x100'] = (100 * s['mean']).round(2); s[METRICS] = s[METRICS].round(4) po = per[per.split == 'final_like'].pivot_table(index='origin', columns='model', values='mean').mul(100).round(2) pm = per[per.split == 'final_like'].pivot_table(index='origin', columns='model', values=METRICS).round(3) pm.columns = [f'{b} | {a}' for a, b in pm.columns] txt = ('# v3 ablations (pre-registered, research/V3_PROTOCOL.md)\n\nSkills are unclipped mean skill vs clim30 at the same origin; "score_x100" = 100 × mean skill (the official score clips at 0). ' 'dev = origins 1985–2004, late = 2005–2011 (locked). Selection statistic: h4–7.\n\n' f'Selection: `{json.dumps(sel["selection"])}` — no candidate passed the rule, so v3 = v2b.\n\n' '## Every candidate vs its reference (temp v2, CDD v2b, precip/hottest clim30)\n\n' + md(cand, False) + '\n\n## Assembled models: per-metric skill and overall score\n\n' + md(s[['model', 'set', 'split'] + METRICS + ['score_x100']], False) + '\n\n## Overall score ×100 by origin (h4–7)\n\n' + md(po) + '\n\n## Per-metric skill by origin (h4–7)\n\n' + md(pm) + '\n') (RES / 'v3_tables.md').write_text(txt); print(md(s[['model', 'set', 'split'] + METRICS + ['score_x100']], False)); print(best) if __name__ == '__main__': main()