heat-seekers-protostar / code /report_v2.py
tchaudha's picture
Heat Seekers v4: detrended-trimmed temp level, v2b CDD, hottest-day blend
89379eb verified
Raw History Blame Contribute Delete
3.81 kB
"""Assemble per-origin official-style scores for named composite models from results/v2_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
RES = ROOT / 'results'
sel = json.loads((RES / 'v2_selection.json').read_text())['selection']
MODELS = {
'clim30 (official 30-yr average)': {m: 'clim30' for m in METRICS},
'linear trend (official lin30)': {m: 'lin30' for m in METRICS},
'v1 submitted (exp10 heat + clim30)': {'annual_avg_temp': 'exp10', 'cdd': 'exp10', 'hottest_day_temp': 'clim30', 'total_precip_in': 'clim30'},
'teammate-like (L15 a0.25 knn10, SHmedian CDD)': {'annual_avg_temp': 'LT_L15_a0.25_knn10', 'cdd': 'SHmedian_L15_a0.25_knn10', 'hottest_day_temp': 'median30', 'total_precip_in': 'median30'},
'v2 protocol-strict': sel,
'v2b (deviation: CDD daily shift, same slope)': {**sel, 'cdd': 'SHmean_' + sel['annual_avg_temp'][3:]},
}
def main():
sk = pd.read_csv(RES / 'v2_skills_by_origin.csv')
sk = sk[sk.split != 'all']
out = []
for name, ch in MODELS.items():
for m in METRICS:
g = sk[(sk.metric == m) & (sk.method == ch[m])].copy()
assert g.origin.nunique() == 27, (name, m, ch[m])
g['model'] = name; out.append(g)
t = pd.concat(out)
per = t.pivot_table(index=['model', 'origin', 'split'], columns='metric', values='skill').reset_index()
per['mean_skill'] = per[METRICS].mean(1); per['score'] = 100 * per.mean_skill.clip(lower=0)
maes = t.pivot_table(index=['model', 'origin', 'split'], columns='metric', values='mae').add_prefix('mae_').reset_index()
per = per.merge(maes, on=['model', 'origin', 'split'])
per.to_csv(RES / 'v2_model_scores_by_origin.csv', index=False)
per['set'] = np.where(per.origin <= 2004, 'dev_1985_2004', 'late_2005_2011')
agg = per.groupby(['model', 'set', 'split'])[METRICS + ['mean_skill']].mean().reset_index()
agg['score_of_mean_skill'] = 100 * agg.mean_skill.clip(lower=0)
agg.to_csv(RES / 'v2_model_summary.csv', index=False)
pd.set_option('display.width', 250)
print(agg.round(4).to_string())
c = per[per.origin == 2011]
print(c[['model', 'split', 'score'] + METRICS].round(4).to_string())
if __name__ == '__main__': main()
def md(df, index=True):
d = df.reset_index() if index else df
cols = [str(c) for c in d.columns]
lines = ['| ' + ' | '.join(cols) + ' |', '|' + '---|' * len(cols)]
for r in d.itertuples(index=False): lines.append('| ' + ' | '.join(str(v) for v in r) + ' |')
return '\n'.join(lines)
def markdown_tables():
short = {'clim30 (official 30-yr average)': 'clim30', 'linear trend (official lin30)': 'lin30', 'v1 submitted (exp10 heat + clim30)': 'v1',
'teammate-like (L15 a0.25 knn10, SHmedian CDD)': 'teammate-like', 'v2 protocol-strict': 'v2', 'v2b (deviation: CDD daily shift, same slope)': 'v2b'}
p = pd.read_csv(RES / 'v2_model_scores_by_origin.csv'); p['m'] = p.model.map(short)
order = ['lin30', 'v1', 'teammate-like', 'v2', 'v2b']
parts = []
for split, lab in (('final_like', 'h4-7'), ('live_like', 'h1-3')):
t = (100 * p[p.split == split].pivot_table(index='origin', columns='m', values='mean_skill'))[order].round(2)
parts.append(t.add_suffix(' ' + lab))
per = parts[0].join(parts[1])
s = pd.read_csv(RES / 'v2_model_summary.csv'); s['model'] = s.model.map(short)
s = s[['model', 'set', 'split'] + METRICS + ['mean_skill']].round(4)
txt = '## Unclipped mean skill x100 by origin (score = max(0, value))\n\n' + md(per) + '\n\n## Per-metric mean skill by set\n\n' + md(s, False) + '\n'
(RES / 'v2_tables.md').write_text(txt)
if __name__ == '__main__': markdown_tables()