Fit the season rule against the sourced seasons, record the figure, and add the estimate review list, proposals and check
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a2423e0ba5
commit
b2cdfc37cf
4 files changed
+1299
-29
No files matched your search
@@ -4,14 +4,19 @@ normals, so they run with no network.
|
||||
Run from the repository root:
|
||||
python3 -m unittest discover tools/tests
|
||||
"""
|
||||
import csv
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.dont_write_bytecode = True
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import estimate_season as E # noqa: E402
|
||||
import test_refresh as TR # noqa: E402
|
||||
import season_climate as C # noqa: E402
|
||||
|
||||
PARAMS = E.Params(snow_cm_max=10.0, cold_c_min=5.0, heat_c_max=32.0)
|
||||
@@ -200,5 +205,311 @@ class SamplePointsTest(unittest.TestCase):
|
||||
self.assertAlmostEqual(points[0].lat, 43.85)
|
||||
|
||||
|
||||
KNOWN = E.Params(snow_cm_max=10.0, cold_c_min=5.0, heat_c_max=30.0)
|
||||
GRID = {"snow_cm_max": [float(v) for v in range(0, 65, 5)],
|
||||
"cold_c_min": [float(v) for v in range(0, 13)],
|
||||
"heat_c_max": [float(v) for v in range(24, 41)]}
|
||||
|
||||
|
||||
def synthetic_sourced():
|
||||
"""Trails whose sourced months come from KNOWN, designed so each
|
||||
threshold is pinned by months just either side of it."""
|
||||
cases = [
|
||||
("cold-pin", normals(3000, [4, 4, 6, 6, 10, 15, 20, 20, 15, 10, 6, 4],
|
||||
snow=[0] * 12)),
|
||||
("snow-pin", normals(3000, [15] * 12,
|
||||
snow=[20, 20, 12, 8, 0, 0, 0, 0, 0, 0, 8, 12])),
|
||||
("heat-pin", normals(400, [20, 22, 26, 29, 31, 36, 38, 38, 36, 31, 28, 22],
|
||||
snow=[0] * 12)),
|
||||
("mild-all-year", normals(100, [15] * 12, snow=[0] * 12)),
|
||||
]
|
||||
out = []
|
||||
for tid, point in cases:
|
||||
months = E.months_in_season([point], KNOWN)
|
||||
assert months, tid
|
||||
out.append((tid, [point], months))
|
||||
return out
|
||||
|
||||
|
||||
class FitTest(unittest.TestCase):
|
||||
def test_held_out_is_stable_and_splits_roughly_in_half(self):
|
||||
ids = [f"trail-{i:04d}" for i in range(1000)]
|
||||
again = [t for t in ids if E.held_out(t)]
|
||||
self.assertEqual(again, [t for t in ids if E.held_out(t)])
|
||||
share = len(again) / len(ids)
|
||||
self.assertTrue(0.40 <= share <= 0.60, share)
|
||||
|
||||
def test_fit_recovers_known_params_from_synthetic_trails(self):
|
||||
sourced = synthetic_sourced()
|
||||
fitted = E.fit_params(sourced, GRID)
|
||||
self.assertLessEqual(abs(fitted.snow_cm_max - KNOWN.snow_cm_max), 5)
|
||||
self.assertLessEqual(abs(fitted.cold_c_min - KNOWN.cold_c_min), 1)
|
||||
self.assertLessEqual(abs(fitted.heat_c_max - KNOWN.heat_c_max), 1)
|
||||
self.assertEqual(E.report(sourced, fitted)["exact"], len(sourced))
|
||||
|
||||
def test_report_counts_grades_and_no_data(self):
|
||||
params = E.Params(snow_cm_max=10.0, cold_c_min=5.0, heat_c_max=32.0)
|
||||
mild = normals(100, [15] * 12, snow=[0] * 12)
|
||||
alpine = normals(3000, [0, 1, 3, 6, 10, 15, 20, 20, 15, 10, 4, 1],
|
||||
snow=[0] * 12)
|
||||
sourced = [
|
||||
("exact-1", [mild], list(range(1, 13))),
|
||||
("within-1", [alpine], [5, 6, 7, 8, 9]), # rule gives 4 to 10
|
||||
("worse-1", [alpine], [1, 2]),
|
||||
("nodata-1", [None], [6, 7, 8]),
|
||||
]
|
||||
rep = E.report(sourced, params)
|
||||
self.assertEqual(rep["n"], 4)
|
||||
self.assertEqual(rep["exact"], 1)
|
||||
self.assertEqual(rep["within1"], 2) # within1 counts exact too
|
||||
self.assertEqual(rep["worse"], 1)
|
||||
self.assertEqual(rep["no_data"], 1)
|
||||
self.assertEqual(rep["within1"] + rep["worse"] + rep["no_data"], rep["n"])
|
||||
self.assertIn("by_group", rep)
|
||||
|
||||
def test_passes_is_false_below_the_bar_and_true_at_it(self):
|
||||
def rep(within1, no_data=0, n=100):
|
||||
return {"n": n, "exact": 0, "within1": within1,
|
||||
"worse": n - within1 - no_data, "no_data": no_data}
|
||||
self.assertFalse(E.passes_bar(rep(74)))
|
||||
self.assertTrue(E.passes_bar(rep(75)))
|
||||
self.assertFalse(E.passes_bar(rep(90, no_data=11))) # coverage 89%
|
||||
|
||||
def test_the_same_input_gives_the_same_report_twice(self):
|
||||
sourced = synthetic_sourced()
|
||||
self.assertEqual(E.report(sourced, KNOWN), E.report(sourced, KNOWN))
|
||||
self.assertEqual(E.fit_params(sourced, GRID), E.fit_params(sourced, GRID))
|
||||
|
||||
|
||||
def mild_fetch(lat, lon, elev):
|
||||
return C.PointNormals(lat=float(lat), lon=float(lon),
|
||||
elev_m=float(elev if elev is not None else 100.0),
|
||||
tmax_c=tuple([15.0] * 12), snow_cm=tuple([0.0] * 12))
|
||||
|
||||
|
||||
def none_fetch(lat, lon, elev):
|
||||
return None
|
||||
|
||||
|
||||
def bounds_root(td):
|
||||
os.makedirs(os.path.join(td, "web"))
|
||||
with open(os.path.join(td, "web", "place-bounds.toml"), "w") as f:
|
||||
f.write('[box]\n"US-VT" = [-73.5, 42.7, -71.5, 45.0]\n')
|
||||
return td
|
||||
|
||||
|
||||
def ends_rec(tid="trail-x", lat=43.0, extra=None):
|
||||
hike = {"summary": "x", "checked": "2026-01-01", "source": [],
|
||||
"start_lonlat": [-110.0, lat], "end_lonlat": [-109.0, lat + 1.0]}
|
||||
hike.update(extra or {})
|
||||
return {"id": tid, "country": "US", "regions": ["VT"],
|
||||
"source": {"line_file": "line.geojson"}, "hike": hike}
|
||||
|
||||
|
||||
class EstimateTest(unittest.TestCase):
|
||||
def test_a_no_track_record_is_flagged(self):
|
||||
import tempfile
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
est = E.estimate(bounds_root(td), ends_rec(), PARAMS, fetch=mild_fetch)
|
||||
self.assertIn("no-track", est["flags"])
|
||||
self.assertEqual(est["months"], list(range(1, 13)))
|
||||
self.assertEqual(est["phrase"], "All year")
|
||||
|
||||
def test_a_trail_with_no_data_is_listed_not_estimated(self):
|
||||
import tempfile
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
est = E.estimate(bounds_root(td), ends_rec(), PARAMS, fetch=none_fetch)
|
||||
self.assertIsNone(est["months"])
|
||||
self.assertIn("no-data", est["flags"])
|
||||
self.assertIn("no data", est["reason"])
|
||||
|
||||
def test_flags_include_far_north_above_sixty_degrees(self):
|
||||
import tempfile
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
est = E.estimate(bounds_root(td), ends_rec(lat=61.0), PARAMS,
|
||||
fetch=mild_fetch)
|
||||
self.assertIn("far-north", est["flags"])
|
||||
|
||||
def test_nothing_in_estimate_touches_the_network_without_a_fetch(self):
|
||||
import tempfile
|
||||
|
||||
def raising(lat, lon, elev):
|
||||
raise AssertionError("network touched")
|
||||
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
with self.assertRaises(AssertionError):
|
||||
E.estimate(bounds_root(td), ends_rec(), PARAMS, fetch=raising)
|
||||
|
||||
def test_comparable_is_the_nearest_sourced_trail(self):
|
||||
import tempfile, shutil
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
root = TR.make_repo(tmp, trails=[
|
||||
("near-one", "Near", {"extra_hike":
|
||||
'season = "June to September"\nmonths = [6, 7, 8, 9]\n'
|
||||
'season_basis = "source"\n'
|
||||
'start_lonlat = [-110.0, 43.1]\nend_lonlat = [-110.0, 43.3]\n'}),
|
||||
("far-one", "Far", {"extra_hike":
|
||||
'season = "July to August"\nmonths = [7, 8]\n'
|
||||
'season_basis = "source"\n'
|
||||
'start_lonlat = [-100.0, 35.0]\nend_lonlat = [-100.0, 35.2]\n'}),
|
||||
("target-one", "Target", {"extra_hike":
|
||||
'start_lonlat = [-110.0, 43.0]\nend_lonlat = [-110.0, 43.2]\n'}),
|
||||
])
|
||||
os.makedirs(os.path.join(root, "web"))
|
||||
with open(os.path.join(root, "web", "place-bounds.toml"), "w") as f:
|
||||
f.write('[box]\n"US-VT" = [-73.5, 42.7, -71.5, 45.0]\n')
|
||||
import refresh as R
|
||||
rec = R.load_toml(os.path.join(root, "target-one", "trail.toml"))
|
||||
# fixture records claim line_file "" so sampling uses the ends
|
||||
rec["source"]["line_file"] = "line.geojson"
|
||||
est = E.estimate(root, rec, PARAMS, fetch=mild_fetch)
|
||||
self.assertEqual(est["comparable"], "near-one")
|
||||
self.assertEqual(est["comparable_phrase"], "June to September")
|
||||
|
||||
|
||||
class ProposeCheckTest(unittest.TestCase):
|
||||
def fit_doc(self, root, passes=True, held_within1=3, held_n=4):
|
||||
os.makedirs(os.path.join(root, "tools"), exist_ok=True)
|
||||
doc = {"computed": "2026-11-20", "rule_version": E.RULE_VERSION,
|
||||
"dataset": "test", "period": "2011-2020",
|
||||
"params": {"snow_cm_max": 10.0, "cold_c_min": 5.0,
|
||||
"heat_c_max": 32.0},
|
||||
"held_out": {"n": held_n, "exact": 0, "within1": held_within1,
|
||||
"worse": held_n - held_within1, "no_data": 0,
|
||||
"by_group": {}},
|
||||
"all": {"n": held_n, "exact": 0, "within1": held_within1,
|
||||
"worse": held_n - held_within1, "no_data": 0,
|
||||
"by_group": {}},
|
||||
"bar": E.BAR, "passes": passes}
|
||||
with open(os.path.join(root, "tools", "season_fit.json"), "w") as f:
|
||||
json.dump(doc, f)
|
||||
|
||||
def repo_with(self, tmp, trails):
|
||||
root = TR.make_repo(tmp, trails=trails)
|
||||
os.makedirs(os.path.join(root, "web"))
|
||||
with open(os.path.join(root, "web", "place-bounds.toml"), "w") as f:
|
||||
f.write('[box]\n"US-VT" = [-73.5, 42.7, -71.5, 45.0]\n')
|
||||
# fixture records name no track file; point them at the default
|
||||
for tid, _name, _kw in trails:
|
||||
path = os.path.join(root, tid, "trail.toml")
|
||||
text = TR.read(root, f"{tid}/trail.toml").replace(
|
||||
'line_file = ""', 'line_file = "line.geojson"')
|
||||
TR.write(path, text)
|
||||
return root
|
||||
|
||||
ENDS = 'start_lonlat = [-110.0, 43.0]\nend_lonlat = [-110.0, 43.2]\n'
|
||||
|
||||
def test_propose_writes_one_row_per_unsourced_trail(self):
|
||||
import tempfile, shutil, csv
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
root = self.repo_with(tmp, [
|
||||
("aaa", "Aaa", {"extra_hike": self.ENDS}),
|
||||
("bbb", "Bbb", {"extra_hike":
|
||||
'season = "June to September"\nmonths = [6, 7, 8, 9]\n'
|
||||
'season_basis = "source"\n' + self.ENDS}),
|
||||
("ccc", "Ccc", {"hike": False}),
|
||||
])
|
||||
stage = os.path.join(tmp, "stage")
|
||||
n = E.propose(root, PARAMS, mild_fetch, stage)
|
||||
self.assertEqual(n, 2) # aaa and ccc have no season
|
||||
with open(os.path.join(stage, "season", "estimates.csv"),
|
||||
newline="") as f:
|
||||
rows = list(csv.DictReader(f))
|
||||
self.assertEqual([r["id"] for r in rows], ["aaa", "ccc"])
|
||||
self.assertEqual(rows[0]["phrase"], "All year")
|
||||
|
||||
def est_repo(self, tmp, months_extra):
|
||||
return self.repo_with(tmp, [
|
||||
("aaa", "Aaa", {"extra_hike": months_extra + self.ENDS}),
|
||||
])
|
||||
|
||||
def test_check_passes_when_nothing_changed(self):
|
||||
import tempfile, shutil
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
root = self.est_repo(
|
||||
tmp,
|
||||
'season = "All year"\n'
|
||||
'months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n'
|
||||
'season_basis = "estimate"\nseason_method = "Estimated."\n')
|
||||
self.fit_doc(root)
|
||||
ok, lines = E.check(root, mild_fetch)
|
||||
self.assertTrue(ok, lines)
|
||||
|
||||
def test_check_names_the_trail_whose_estimate_changed(self):
|
||||
import tempfile, shutil
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
root = self.est_repo(
|
||||
tmp,
|
||||
'season = "June to July"\nmonths = [6, 7]\n'
|
||||
'season_basis = "estimate"\nseason_method = "Estimated."\n')
|
||||
self.fit_doc(root)
|
||||
ok, lines = E.check(root, mild_fetch)
|
||||
self.assertFalse(ok)
|
||||
self.assertTrue(any("aaa" in line and "[6, 7]" in line
|
||||
for line in lines), lines)
|
||||
|
||||
def test_check_fails_when_the_fit_got_worse(self):
|
||||
import tempfile, shutil
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
# one train and one held-out sourced trail, both graded worse
|
||||
# by mild all-year normals against a June-only season
|
||||
ids = [f"trail-{i:03d}" for i in range(40)]
|
||||
train_id = next(t for t in ids if not E.held_out(t))
|
||||
test_id = next(t for t in ids if E.held_out(t))
|
||||
extra = ('season = "June"\nmonths = [6]\n'
|
||||
'season_basis = "source"\n' + self.ENDS)
|
||||
root = self.repo_with(tmp, [(train_id, "Train", {"extra_hike": extra}),
|
||||
(test_id, "Test", {"extra_hike": extra})])
|
||||
self.fit_doc(root, held_within1=4, held_n=4)
|
||||
ok, lines = E.check(root, mild_fetch)
|
||||
self.assertFalse(ok)
|
||||
self.assertTrue(any("fit got worse" in line for line in lines), lines)
|
||||
|
||||
def test_proposals_collect_cleanly(self):
|
||||
import tempfile, shutil, csv
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
root = self.repo_with(tmp, [("aaa", "Aaa", {"extra_hike": self.ENDS})])
|
||||
stage = os.path.join(tmp, "stage")
|
||||
os.makedirs(os.path.join(stage, "season"))
|
||||
reviewed = os.path.join(stage, "season", "estimates.reviewed.csv")
|
||||
with open(reviewed, "w", newline="") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow(["id", "proposed_months", "phrase", "kind", "flags",
|
||||
"comparable", "comparable_phrase", "note"])
|
||||
w.writerow(["aaa", "6 7 8 9", "June to September", "ends",
|
||||
"no-track", "", "", ""])
|
||||
self.assertEqual(E.proposals(root, reviewed, stage), 1)
|
||||
code, out = TR.main(root, "--dir", stage, "collect")
|
||||
self.assertEqual(code, 0, out)
|
||||
import refresh as R
|
||||
coll = R.load_toml(os.path.join(stage, "collected.toml"))
|
||||
props = coll["proposal"]
|
||||
self.assertEqual(len(props), 1)
|
||||
self.assertEqual(props[0]["status"], "proposed", out)
|
||||
|
||||
|
||||
class SeasonKindTest(unittest.TestCase):
|
||||
def test_kinds_of_sourced_seasons(self):
|
||||
self.assertEqual(E.season_kind(list(range(1, 13))), "year-round")
|
||||
self.assertEqual(E.season_kind([7, 8, 9]), "alpine")
|
||||
self.assertEqual(E.season_kind([10, 11, 12, 1, 2, 3, 4]), "cool-season")
|
||||
self.assertEqual(E.season_kind([4, 5, 6, 7, 8, 9, 10]), "other")
|
||||
|
||||
def test_the_report_splits_by_kind(self):
|
||||
def pn(v):
|
||||
return C.PointNormals(40.0, -100.0, 1000.0, tuple([v] * 12))
|
||||
sourced = [("a", [pn(20.0)], list(range(1, 13)), "x")]
|
||||
rep = E.report(sourced, E.Params(10.0, 5.0, 40.0))
|
||||
self.assertEqual(rep["by_kind"]["year-round"]["n"], 1)
|
||||
self.assertEqual(rep["by_kind"]["year-round"]["exact"], 1)
|
||||
self.assertEqual(rep["by_kind"]["alpine"]["n"], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Tests for the committed fit figure in tools/season_fit.json.
|
||||
|
||||
Run from the repository root:
|
||||
python3 -m unittest discover tools/tests
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.dont_write_bytecode = True
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import estimate_season as E # noqa: E402
|
||||
import refresh # noqa: E402
|
||||
import validate as V # noqa: E402
|
||||
|
||||
FIT_PATH = os.path.join(V.ROOT, "tools", "season_fit.json")
|
||||
|
||||
|
||||
def load_fit():
|
||||
with open(FIT_PATH, encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
class SeasonFitFileTest(unittest.TestCase):
|
||||
def test_the_committed_fit_has_the_fields_the_site_reads(self):
|
||||
fit = load_fit()
|
||||
for key in ("computed", "rule_version", "dataset", "period",
|
||||
"params", "all", "held_out", "bar", "passes"):
|
||||
self.assertIn(key, fit)
|
||||
for key in ("snow_cm_max", "cold_c_min", "heat_c_max"):
|
||||
self.assertIn(key, fit["params"])
|
||||
for part in ("all", "held_out"):
|
||||
for key in ("n", "exact", "within1", "worse", "no_data", "by_group"):
|
||||
self.assertIn(key, fit[part], part)
|
||||
rep = fit[part]
|
||||
self.assertEqual(rep["within1"] + rep["worse"] + rep["no_data"],
|
||||
rep["n"], part)
|
||||
self.assertEqual(fit["bar"], E.BAR)
|
||||
self.assertEqual(fit["rule_version"], E.RULE_VERSION)
|
||||
self.assertEqual(fit["passes"], E.passes_bar(fit["held_out"], fit["bar"]))
|
||||
|
||||
def test_estimates_only_with_a_passing_fit(self):
|
||||
estimated = []
|
||||
for tid in refresh.trail_ids(V.ROOT):
|
||||
rec = refresh.load_toml(os.path.join(V.ROOT, tid, "trail.toml"))
|
||||
if (rec.get("hike") or {}).get("season_basis") == "estimate":
|
||||
estimated.append(tid)
|
||||
if not estimated:
|
||||
self.skipTest("no estimated seasons on file yet")
|
||||
fit = load_fit()
|
||||
self.assertTrue(fit["passes"], fit)
|
||||
held = fit["held_out"]
|
||||
self.assertGreaterEqual(held["within1"] / held["n"],
|
||||
E.BAR["within1_min"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user