Download code/test_amend.py from Tonic/protostar-climate-2026: direct link, hf CLI and curl.
- Browser
- Download file 2.44 kB
-
https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/test_amend.py
- Command line
-
hf download hf://datasets/Tonic/protostar-climate-2026/code/test_amend.py
-
curl -L -o test_amend.py https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/test_amend.py
2.44 kB
| """Amendment tests (run: .venv/Scripts/python.exe code/test_amend.py).""" | |
| 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 candidates_amend import (EoSimilarity, eosim_forecast, | |
| load_impervious_proxy, urban_temp) | |
| from dcpp import DcppCandidates | |
| from panel import Panel | |
| ROOT = Path(__file__).resolve().parent.parent | |
| def test_dcpp_no_future_starts(panel): | |
| dcpp = DcppCandidates(ROOT, panel) | |
| df = dcpp.df | |
| for origin in (1990, 2004): | |
| clim_starts = df[(df["variable"] == "tas") & (df["member"] == "r1i1p1f1") | |
| & (df["start"] <= origin)]["start"] | |
| assert clim_starts.max() <= origin | |
| clim = dcpp._climatology("tas", origin) | |
| manual = (df[(df["variable"] == "tas") & (df["member"] == "r1i1p1f1") | |
| & (df["start"] <= origin) & (df["start"] >= 1960)] | |
| .groupby("lead")[dcpp.city_cols].mean().sort_index() | |
| .to_numpy(float).T) | |
| assert np.allclose(clim, manual) | |
| ens = dcpp._ensemble("tas", origin, ("r1i1p1f1", "r2i1p1f1")) | |
| assert ens.shape == (210, 7) and np.isfinite(ens).all() | |
| def test_dcpp_units_pattern_scale(panel): | |
| """Pattern added to v2 must be O(1 degF), not O(100) (K/F unit guard).""" | |
| dcpp = DcppCandidates(ROOT, panel) | |
| base = lambda p, o: np.full((210, 7), 60.0) | |
| out = dcpp.temp_pattern(2004, base, 1.0) | |
| assert np.isfinite(out).all() | |
| assert np.abs(out - 60.0).max() < 10.0, np.abs(out - 60.0).max() | |
| def test_urban_and_eosim_finite(panel): | |
| proxy = load_impervious_proxy(ROOT, panel) | |
| assert proxy.shape == (210,) and np.isfinite(proxy).all() | |
| fc = urban_temp(panel, 2004, proxy) | |
| assert fc.shape == (210, 7) and np.isfinite(fc).all() | |
| eosim = EoSimilarity(ROOT, panel) | |
| for metric in ("annual_avg_temp", "cdd"): | |
| fc = eosim_forecast(panel, 2004, metric, eosim) | |
| assert fc.shape == (210, 7) and np.isfinite(fc).all(), metric | |
| if __name__ == "__main__": | |
| panel = Panel(ROOT) | |
| test_dcpp_no_future_starts(panel) | |
| print("dcpp no-future-starts ok") | |
| test_dcpp_units_pattern_scale(panel) | |
| print("dcpp units ok") | |
| test_urban_and_eosim_finite(panel) | |
| print("urban/eosim ok") | |
| print("all amendment tests passed") | |