Download code/run_v2_phase4.py from Tonic/protostar-climate-2026: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/run_v2_phase4.py
- Command line
-
hf download hf://datasets/Tonic/protostar-climate-2026/code/run_v2_phase4.py
-
curl -L -o run_v2_phase4.py https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/run_v2_phase4.py
4.57 kB
| """Phase 4 contested candidates: precip wet-day decomposition, hottest EVT. | |
| Each gets evaluated on tune era; adoption additionally requires a positive | |
| check-era result in the Phase-5 confirmation (one shot, no iteration). | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from scipy import stats | |
| sys.path.insert(0, str(Path(__file__).parent)) | |
| from candidates import level_trend | |
| from dataset import download | |
| from harness import evaluate, skill_table | |
| from panel import Panel | |
| def show(metric: str, detail: pd.DataFrame) -> None: | |
| tab = skill_table(detail) | |
| piv = tab.pivot_table(index="candidate", columns="era", values="skill") | |
| print(f"\n== {metric} [phase4] ==") | |
| print(piv.sort_values("tune", ascending=False).round(4).to_string()) | |
| def precip_wetday_arrays(root: Path, panel: Panel): | |
| data_root = download(root / "data" / "_source") | |
| daily = pd.read_parquet(data_root / "daily" / "train-00000-of-00001.parquet", | |
| columns=["ID", "Date", "prcp"]) | |
| daily["Year"] = daily["Date"].astype(str).str.slice(0, 4).astype(int) | |
| daily = daily[(daily["Year"] >= panel.years[0]) & daily["prcp"].notna()] | |
| daily["wet"] = (daily["prcp"] > 0.01).astype(float) | |
| g = daily.groupby(["ID", "Year"]).agg(total=("prcp", "sum"), wet=("wet", "sum"), | |
| ndays=("prcp", "size")).reset_index() | |
| wet_arr = np.full((len(panel.cities), len(panel.years)), np.nan) | |
| int_arr = np.full_like(wet_arr, np.nan) | |
| ci = {c: i for i, c in enumerate(panel.cities)} | |
| for r in g.itertuples(): | |
| if r.ID in ci and int(r.Year) in panel.year_idx and r.ndays >= 355 and r.wet > 0: | |
| wet_arr[ci[r.ID], panel.year_idx[int(r.Year)]] = r.wet | |
| int_arr[ci[r.ID], panel.year_idx[int(r.Year)]] = r.total / r.wet | |
| return wet_arr, int_arr | |
| def main() -> None: | |
| root = Path(__file__).resolve().parent.parent | |
| panel = Panel(root) | |
| out = root / "outputs" / "v2" | |
| wet_arr, int_arr = precip_wetday_arrays(root, panel) | |
| def wetday_fc(p: Panel, origin: int) -> np.ndarray: | |
| i1 = p.year_idx[origin] | |
| w = wet_arr[:, i1 - 29:i1 + 1] | |
| inten = int_arr[:, i1 - 29:i1 + 1] | |
| w_fc = np.nanmean(w, axis=1) | |
| i_fc = np.nanmean(inten, axis=1) | |
| fc = w_fc * i_fc | |
| fb = np.nanmean(p.values["total_precip_in"][:, i1 - 29:i1 + 1], axis=1) | |
| fc = np.where(np.isfinite(fc), fc, fb) | |
| return np.repeat(np.maximum(fc, 0)[:, None], 7, axis=1) | |
| detail_p = evaluate(panel, "total_precip_in", { | |
| "wetday_decomp": wetday_fc, | |
| "L30mean": lambda p, o: level_trend(p, "total_precip_in", o, 30, "mean", "none"), | |
| }) | |
| detail_p.to_csv(out / "detail_precip_phase4.csv", index=False) | |
| show("total_precip_in", detail_p) | |
| def gev_fc(p: Panel, origin: int) -> np.ndarray: | |
| vals = p.values["hottest_day_temp"] | |
| i1 = p.year_idx[origin] | |
| out_fc = np.full((len(p.cities), 7), np.nan) | |
| base = np.nanmean(vals[:, i1 - 29:i1 + 1], axis=1) | |
| for ci in range(len(p.cities)): | |
| series = vals[ci, i1 - 29:i1 + 1] | |
| series = series[np.isfinite(series)] | |
| if len(series) >= 15: | |
| try: | |
| c, loc, scale = stats.genextreme.fit(series) | |
| out_fc[ci] = stats.genextreme.ppf(0.5, c, loc=loc, scale=scale) | |
| continue | |
| except Exception: | |
| pass | |
| out_fc[ci] = base[ci] | |
| return out_fc | |
| def top2_fc(p: Panel, origin: int) -> np.ndarray: | |
| vals = p.values["hottest_day_temp"] | |
| i1 = p.year_idx[origin] | |
| window = vals[:, i1 - 29:i1 + 1] | |
| second = np.partition(window, -2, axis=1)[:, -2] | |
| base = np.nanmean(window, axis=1) | |
| fc = np.where(np.isfinite(second), second, base) | |
| return np.repeat(fc[:, None], 7, axis=1) | |
| detail_h = evaluate(panel, "hottest_day_temp", { | |
| "gev_median": gev_fc, | |
| "top2_second": top2_fc, | |
| "L30mean": lambda p, o: level_trend(p, "hottest_day_temp", o, 30, "mean", "none"), | |
| "L30_reghalf": lambda p, o: level_trend(p, "hottest_day_temp", o, 30, "mean", | |
| "reg", 50, trend_mult=0.5), | |
| }) | |
| detail_h.to_csv(out / "detail_hottest_phase4.csv", index=False) | |
| show("hottest_day_temp", detail_h) | |
| if __name__ == "__main__": | |
| main() | |