"""Generate the v2 submission.csv (adopted after Phase-5 confirmation). Package (selected on tune-era origins 1990-2000, confirmed on 2001-2011): - annual_avg_temp: median of {L15+national-40yr-trend, L15+regional-40yr-trend, L15+national-40yr-trend on step-adjusted series} - cdd: 50/50 blend of CDD delta method (30 historical years, median) and the linear L15+national-40yr-trend CDD model - total_precip_in, hottest_day_temp: 30-year mean (all contested candidates were rejected in Phase 4) Fitted on data <= 2018 only; predicts 2019-2025. """ from __future__ import annotations import sys from pathlib import Path import numpy as np import pandas as pd sys.path.insert(0, str(Path(__file__).parent)) from dataset import METRICS from leakage import assert_no_forbidden_artifacts from run_v2_confirm import build_packages ORIGIN = 2018 TARGET_YEARS = list(range(2019, 2026)) def main() -> None: root = Path(__file__).resolve().parent.parent assert_no_forbidden_artifacts(Path(__file__).parent) panel, packages = build_packages() assert int(panel.years.max()) <= ORIGIN frames = [] for metric, cands in packages.items(): fc = cands["v2"](panel, ORIGIN) # (210, 7) if not np.isfinite(fc).all(): raise RuntimeError(f"{metric}: non-finite forecasts") frames.append(pd.DataFrame({ "city_id": np.repeat(panel.cities, 7), "year": np.tile(TARGET_YEARS, len(panel.cities)), "metric": metric, "predicted_value": fc.ravel(), })) out = pd.concat(frames, ignore_index=True) expected = {(c, y, m) for c in panel.cities for y in TARGET_YEARS for m in METRICS} got = set(zip(out["city_id"], out["year"], out["metric"])) if got != expected: raise RuntimeError(f"grid mismatch: missing {len(expected - got)}, extra {len(got - expected)}") out = out.sort_values(["city_id", "year", "metric"]).reset_index(drop=True) v1_path = root / "outputs" / "submission_v1.csv" sub_path = root / "submission.csv" if sub_path.exists() and not v1_path.exists(): v1_path.write_bytes(sub_path.read_bytes()) print(f"backed up v1 to {v1_path}") out.to_csv(sub_path, index=False) out.to_csv(root / "outputs" / "submission_v2.csv", index=False) print(f"wrote {sub_path}: {len(out)} rows") print(out.groupby("metric")["predicted_value"].describe().round(2).to_string()) if __name__ == "__main__": main()