File size: 2,914 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
66
67
68
69
70
71
72
73
74
75
76
77
78
"""V3 tests (run: .venv/Scripts/python.exe code/test_v3.py)."""
from __future__ import annotations

import sys
from pathlib import Path

import numpy as np

sys.path.insert(0, str(Path(__file__).parent))
from candidates_v3 import (DailyCandidates, _pooled_slope_masked, damped_temp,
                           hot_delta, quantile_temp, volcano_clean)
from harness_v3 import (ALL_ORIGINS, CONFIRMATION_ORIGINS, SELECTION_ORIGINS,
                        era_of)
from panel import Panel

ROOT = Path(__file__).resolve().parent.parent


def test_era_partition():
    assert sorted(SELECTION_ORIGINS + CONFIRMATION_ORIGINS) == ALL_ORIGINS
    assert not set(SELECTION_ORIGINS) & set(CONFIRMATION_ORIGINS)
    assert era_of(2004) == "selection" and era_of(2005) == "confirmation"
    assert max(SELECTION_ORIGINS) < min(CONFIRMATION_ORIGINS)


def test_masked_slope_matches_unmasked_when_no_exclusions(panel):
    got = _pooled_slope_masked(panel, "annual_avg_temp", 2004, 40, set())
    want = panel.stats("annual_avg_temp", 2004, years_back=40)["pooled_slope"]
    assert abs(got - want) < 1e-9, (got, want)


def test_candidates_finite_at_selection_origins(panel):
    daily = DailyCandidates(ROOT, panel)
    cands = {
        "damped": lambda p, o: damped_temp(p, o, 0.85),
        "quantile": quantile_temp,
        "hot_delta": lambda p, o: hot_delta(p, o, 30, "nat"),
        "evanguo": lambda p, o: daily.evanguo_cdd(o),
        "volcano_temp": lambda p, o: volcano_clean(p, o, "annual_avg_temp"),
        "volcano_cdd": lambda p, o: volcano_clean(p, o, "cdd"),
    }
    for origin in (1990, 2004):
        for name, fn in cands.items():
            fc = fn(panel, origin)
            assert fc.shape == (210, 7), (name, fc.shape)
            assert np.isfinite(fc).all(), f"{name} non-finite at {origin}"


def test_hot_delta_uses_only_past(panel):
    """Truncating the panel after the origin must not change the forecast."""
    fc = hot_delta(panel, 2004, 30, "nat")
    import copy
    p2 = copy.copy(panel)
    values = dict(panel.values)
    hot = panel.values["hottest_day_temp"].copy()
    hot[:, panel.year_idx[2005]:] = np.nan
    values["hottest_day_temp"] = hot
    temp = panel.values["annual_avg_temp"].copy()
    temp[:, panel.year_idx[2005]:] = np.nan
    values["annual_avg_temp"] = temp
    p2.values = values
    p2._cache = {}
    fc2 = hot_delta(p2, 2004, 30, "nat")
    assert np.allclose(fc, fc2, equal_nan=True)


if __name__ == "__main__":
    test_era_partition()
    print("era partition ok")
    panel = Panel(ROOT)
    test_masked_slope_matches_unmasked_when_no_exclusions(panel)
    print("masked slope ok")
    test_candidates_finite_at_selection_origins(panel)
    print("finite candidates ok")
    test_hot_delta_uses_only_past(panel)
    print("no-future-use ok")
    print("all v3 tests passed")