Datasets:
Download code/experiments.py from sanvicpatel/protostar-climate-ensemble: direct link, hf CLI and curl.
- Browser
- Download file 5.28 kB
-
https://huggingface.co/datasets/sanvicpatel/protostar-climate-ensemble/resolve/main/code/experiments.py
- Command line
-
hf download hf://datasets/sanvicpatel/protostar-climate-ensemble/code/experiments.py
-
curl -L -o experiments.py https://huggingface.co/datasets/sanvicpatel/protostar-climate-ensemble/resolve/main/code/experiments.py
5.28 kB
| """Rolling-origin experiments with the challenge's score and immutable 2018 cutoff.""" | |
| from pathlib import Path | |
| import argparse | |
| import json | |
| import os | |
| import time | |
| import warnings | |
| import numpy as np | |
| from inputs import Panel, ROOT, METRICS, MONTH_LENGTH | |
| from models import Origin | |
| from ocean import Ocean | |
| REPORT=ROOT/'reports' | |
| def generate(o, include_ocean=True): | |
| def yield1(name,p): return name,o.final_values(p) | |
| for win in [6,9,12,16,20,25,30,40,50,65]: | |
| for adj in [0,.85]: | |
| for med in [0,.5]: | |
| yield yield1(f'normal:{win}:{adj}:{med}',o.line('annual',window=0,anchor=win,adjust=adj,median=med)) | |
| for win in [20,30,45,60,80]: | |
| for band,pool in [(500,0),(400,.55),(750,.85),(1250,.95)]: | |
| for anc in [12,20,30]: | |
| for med in [0,.5]: | |
| yield yield1(f'line:{win}:{band}:{pool}:{anc}:{med}',o.line('annual',window=win,anchor=anc,band=band,pool=pool,median=med)) | |
| for win in [25,40,60]: | |
| for pool in [.3,.8]: | |
| for anc in [12,25]: | |
| for gain in [.65,1.35,1.7]: | |
| yield yield1(f'fast:{win}:{pool}:{anc}:{gain}',o.line('annual',window=win,decay=win/2.4,anchor=anc,anchor_decay=anc/2.4,band=750,pool=pool,gain=gain,median=.5)) | |
| for win in [25,40,60]: | |
| for anc in [15,30]: | |
| for pool in [.25,.8]: | |
| q=o.line('log',window=win,anchor=anc,band=650,pool=pool,median=.5) | |
| q[:,:,1]=np.expm1(q[:,:,1]); q[:,:,3]=np.maximum(0,q[:,:,3])**2 | |
| yield yield1(f'transform:{win}:{anc}:{pool}',q) | |
| for win in [20,30,45,65]: | |
| for anc in [12,20,30]: | |
| for band,pool in [(450,.35),(750,.8),(1300,.95)]: | |
| for gain in [.8,1.25]: | |
| b=o.line('annual',window=win,anchor=anc,band=band,pool=pool,median=.5,gain=gain) | |
| b[:,:,0]=np.sum(o.line('season',window=win,anchor=anc,band=band,pool=pool,median=0,gain=gain)*MONTH_LENGTH,axis=-1)/MONTH_LENGTH.sum() | |
| b[:,:,1]=np.sum(o.line('monthly_p',window=win,anchor=anc,band=band,pool=pool,median=0,gain=gain)*MONTH_LENGTH,axis=-1) | |
| b[:,:,2]=o.season_max(win,anc,pool,band,gain,use_day=True) | |
| b[:,:,3]=o.season_cdd(win,anc,pool,band,gain=gain) | |
| yield yield1(f'season:{win}:{anc}:{band}:{pool}:{gain}',b) | |
| for recent in [1,3,5,8]: | |
| for damp in [.55,.85,.97]: | |
| for gain in [.4,.8]: | |
| b=o.line('annual',window=45,anchor=22,band=750,pool=.8,median=.5) | |
| b+=gain*o.recent_persistence(years=recent,band=850,damp=damp) | |
| yield yield1(f'persist:{recent}:{damp}:{gain}',b) | |
| if include_ocean and o.p.sst is not None: | |
| sea=Ocean(o) | |
| for win in [35,55,80]: | |
| for anc in [12,25]: | |
| for gwin in [15,26,42]: | |
| for persist in [0,.55]: | |
| b=sea.forced(win,anc,gwin,.8,750,persist=persist) | |
| yield yield1(f'forced:{win}:{anc}:{gwin}:{persist}',b) | |
| b=o.line('annual',window=45,anchor=22,band=750,pool=.8,median=.5) | |
| for mode in ['enso','pacific','all']: | |
| for win in [45,75,100]: | |
| for band in [500,1100]: | |
| for shape in ['direct','detrend']: | |
| adj=sea.correction(mode,win,band,20,shape) | |
| for gain in [.45,.9]: | |
| yield yield1(f'ocean:{mode}:{win}:{band}:{shape}:{gain}',b+gain*adj) | |
| def metric_mae(actual,pred,base): | |
| mask=np.isfinite(actual)&np.isfinite(base) | |
| return np.sum(np.where(mask,np.abs(actual-pred),0.),axis=(0,1))/mask.sum(axis=(0,1)) | |
| def info(actual,base,pred): | |
| b=metric_mae(actual,base,base); e=metric_mae(actual,pred,base) | |
| imp=(1-e/b)*100 | |
| raw=float(imp.mean()) | |
| return dict(score=max(0.,raw),raw_skill=raw,mae=e.tolist(),skill=imp.tolist(),baseline=b.tolist()) | |
| def reset(): | |
| for name in ['weight','scale','raw_slope','spatial','slope']:getattr(Origin,name).cache_clear() | |
| def run(p,origins,out,include_ocean): | |
| for year in origins: | |
| h=7 if year!=2010 else 8 | |
| path=REPORT/f'{out}_{year}.npz' | |
| if path.exists(): continue | |
| t=time.monotonic(); o=Origin(p,year,h) | |
| models=list(generate(o,include_ocean)) | |
| names,pr=zip(*models); pr=np.stack(pr).astype('float32') | |
| if year+h<=2018: | |
| a=p.actual(year,h); b=p.baseline(year,h) | |
| sc=np.array([info(a,b,z)['score'] for z in pr]); ids=sc.argsort()[-3:][::-1] | |
| print(year,round(time.monotonic()-t,1),'s',[(names[i],round(sc[i],2)) for i in ids],flush=True) | |
| else: | |
| a=np.full(pr.shape[1:],np.nan); b=p.baseline(year,h) | |
| np.savez_compressed(path,models=names,pred=pr,actual=a,base=b) | |
| reset() | |
| if __name__=='__main__': | |
| ap=argparse.ArgumentParser();ap.add_argument('--origins',default='1962:2012:2,2011,2018');ap.add_argument('--out',default='core');ap.add_argument('--no-ocean',action='store_true');args=ap.parse_args() | |
| origins=[] | |
| for a in args.origins.split(','): | |
| origins+=list(range(*map(int,a.split(':')))) if ':' in a else [int(a)] | |
| p=Panel(sst=not args.no_ocean) | |
| run(p,sorted(set(origins)),args.out,not args.no_ocean) | |