File size: 2,443 Bytes
49f3de8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | """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")
|