tchaudha's picture
Heat Seekers v4: detrended-trimmed temp level, v2b CDD, hottest-day blend
89379eb verified
Raw History Blame Contribute Delete
2.37 kB
"""Offline tests for v4: leakage, protocol freeze, reference parity with v2/v2b, submission validity."""
import ast, io, hashlib, json, sys, unittest
import numpy as np, pandas as pd
sys.path.insert(0, str(__import__('pathlib').Path(__file__).resolve().parent))
from v4lib import *
import make_submission_v4 as ms4
P = attach(Panel()); OFF = ROOT / 'research/official/protostar-challenge-climate-risk-intelligence'
class V4(unittest.TestCase):
def test_poisoned_future(self):
for o in (1990, 2009):
a, ia = v4_block(P, o); b, ib = v4_block(cut(P, o), o)
pd.testing.assert_frame_equal(a, b); self.assertEqual(ia, ib)
def test_protocol_frozen(self):
self.assertEqual(hashlib.sha256((ROOT / 'research/V4_PROTOCOL.md').read_bytes()).hexdigest(),
(ROOT / 'results/v4_protocol_hash.txt').read_text().split()[0])
def test_references_match_v2(self):
o = 2004; f4, _ = v4_block(P, o); f2 = forecast_block(P, o, configs=[(15, 0.0, 'national')])
for m4, m2, met in (('v2', 'LT_L15_a0_national', 'annual_avg_temp'), ('v2b', 'SHmean_L15_a0_national', 'cdd')):
np.testing.assert_allclose(f4[(f4.method == m4) & (f4.metric == met)].predicted_value.to_numpy(),
f2[(f2.method == m2) & (f2.metric == met)].predicted_value.to_numpy(), atol=1e-9)
def test_trim(self):
self.assertAlmostEqual(trim1(np.arange(10.0)), np.arange(1, 9).mean()); self.assertAlmostEqual(trim1(np.arange(9.0)), 4.0)
def test_submission(self):
tree = ast.parse((OFF / 'utils.py').read_text(encoding='utf-8'))
pure = [n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'check_forecasts']
ns = dict(pd=pd, io=io, CITY_IDS=set((OFF / 'data/city_ids.txt').read_text().split()), YEARS=set(range(2019, 2026)), METRICS=set(METRICS), COLUMNS=KEY + ['predicted_value'])
exec(compile(ast.Module(body=pure, type_ignores=[]), 'utils.py', 'exec'), ns)
out, _, _ = ms4.build(P); buf = out.to_csv(index=False, float_format='%.6f').encode()
self.assertIsNone(ns['check_forecasts'](buf)); self.assertEqual(len(out), 5880)
self.assertEqual(hashlib.sha256(buf).hexdigest(), json.loads((ROOT / 'results/submission_v4.json').read_text())['sha256'])
if __name__ == '__main__': unittest.main(verbosity=2)