Add season arcs, grades, phrases and the method sentence
This commit is contained in:
1 parent
157f268974
commit
15dbf07603
2 files changed
+189
No files matched your search
+108
@@ -68,3 +68,111 @@ def check_season(tid, hike, errors):
|
||||
errors.append(
|
||||
f"{tid}: hike.season_basis \"estimate\" needs exactly one source naming "
|
||||
"season or months")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Shared helpers for the checker and the estimator. Months are a set on a
|
||||
# circle: a season may wrap the year end (November to March), so spans,
|
||||
# grades and phrases are all circular.
|
||||
|
||||
MONTH_NAMES = ("January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December")
|
||||
|
||||
|
||||
def _months(months):
|
||||
got = sorted({int(m) for m in months})
|
||||
if not got or got[0] < 1 or got[-1] > 12:
|
||||
raise ValueError(f"not a set of months: {months!r}")
|
||||
return got
|
||||
|
||||
|
||||
def span(months):
|
||||
"""The smallest circular arc covering the months, as (first, last).
|
||||
|
||||
December to February is one arc, (12, 2). Two separate windows give the
|
||||
arc covering both. All twelve months give (1, 12).
|
||||
"""
|
||||
got = _months(months)
|
||||
if len(got) == 1:
|
||||
return (got[0], got[0])
|
||||
best_gap, first = -1, got[0]
|
||||
for i, month in enumerate(got):
|
||||
nxt = got[(i + 1) % len(got)]
|
||||
gap = (nxt - month) % 12
|
||||
if gap == 0:
|
||||
gap = 12
|
||||
# The arc starts after the largest gap. Ties go to the gap whose
|
||||
# following month is smallest, so all twelve months give (1, 12).
|
||||
if gap > best_gap or (gap == best_gap and nxt < first):
|
||||
best_gap, first = gap, nxt
|
||||
i = got.index(first)
|
||||
return (first, got[i - 1])
|
||||
|
||||
|
||||
def _circ_distance(a, b):
|
||||
d = abs(a - b)
|
||||
return min(d, 12 - d)
|
||||
|
||||
|
||||
def grade(estimated, sourced):
|
||||
"""How well an estimated season matches a sourced one.
|
||||
|
||||
"exact" when the arcs have the same first and last month, "within1"
|
||||
when each end is within one month (counted on the circle), else "worse".
|
||||
"""
|
||||
if not estimated or not sourced:
|
||||
return "worse"
|
||||
est_first, est_last = span(estimated)
|
||||
src_first, src_last = span(sourced)
|
||||
if (est_first, est_last) == (src_first, src_last):
|
||||
return "exact"
|
||||
if (_circ_distance(est_first, src_first) <= 1
|
||||
and _circ_distance(est_last, src_last) <= 1):
|
||||
return "within1"
|
||||
return "worse"
|
||||
|
||||
|
||||
def phrase_for(months):
|
||||
"""The plain season phrase built from months, for an estimate.
|
||||
|
||||
One run reads "June to September", a run over the year end reads
|
||||
"November to March", separate runs are joined with "and", one month
|
||||
reads "July" and twelve read "All year".
|
||||
"""
|
||||
got = _months(months)
|
||||
if len(got) == 12:
|
||||
return "All year"
|
||||
runs = []
|
||||
start = prev = got[0]
|
||||
for month in got[1:]:
|
||||
if month == prev + 1:
|
||||
prev = month
|
||||
else:
|
||||
runs.append((start, prev))
|
||||
start = prev = month
|
||||
runs.append((start, prev))
|
||||
if len(runs) > 1 and runs[0][0] == 1 and runs[-1][1] == 12:
|
||||
runs = [(runs[-1][0], runs[0][1])] + runs[1:-1]
|
||||
first = span(got)[0]
|
||||
while runs and runs[0][0] != first:
|
||||
runs.append(runs.pop(0))
|
||||
|
||||
def words(run):
|
||||
a, b = run
|
||||
if a == b:
|
||||
return MONTH_NAMES[a - 1]
|
||||
return f"{MONTH_NAMES[a - 1]} to {MONTH_NAMES[b - 1]}"
|
||||
|
||||
return " and ".join(words(run) for run in runs)
|
||||
|
||||
|
||||
def method_sentence(dataset, period, n_points, kind, rule_version):
|
||||
"""The one fixed sentence an estimated season carries as its method."""
|
||||
where = {
|
||||
"track": "along the track",
|
||||
"ends": "at the trail ends",
|
||||
"place": "at the place centre",
|
||||
}.get(kind, kind)
|
||||
noun = "point" if n_points == 1 else "points"
|
||||
return (f"Estimated from {dataset} monthly climate ({period}) at "
|
||||
f"{n_points} {noun} {where}; rule version {rule_version}.")
|
||||
@@ -0,0 +1,81 @@
|
||||
"""Tests for the shared season helpers in tools/season.py.
|
||||
|
||||
Run from the repository root:
|
||||
python3 -m unittest discover tools/tests
|
||||
"""
|
||||
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 season # noqa: E402
|
||||
|
||||
|
||||
class SpanTest(unittest.TestCase):
|
||||
def test_span_wraps_the_year(self):
|
||||
self.assertEqual(season.span([11, 12, 1, 2, 3]), (11, 3))
|
||||
|
||||
def test_span_of_two_windows_covers_both(self):
|
||||
self.assertEqual(season.span([4, 5, 9, 10]), (4, 10))
|
||||
|
||||
def test_span_of_one_month(self):
|
||||
self.assertEqual(season.span([7]), (7, 7))
|
||||
|
||||
def test_span_of_all_year(self):
|
||||
self.assertEqual(season.span(list(range(1, 13))), (1, 12))
|
||||
|
||||
def test_span_is_order_independent(self):
|
||||
self.assertEqual(season.span([3, 1, 12, 2, 11]), (11, 3))
|
||||
|
||||
|
||||
class GradeTest(unittest.TestCase):
|
||||
def test_grade_exact(self):
|
||||
self.assertEqual(season.grade([6, 7, 8, 9], [6, 7, 8, 9]), "exact")
|
||||
self.assertEqual(season.grade([11, 12, 1, 2], [11, 12, 1, 2]), "exact")
|
||||
|
||||
def test_grade_within_one_at_each_end(self):
|
||||
self.assertEqual(season.grade([5, 6, 7, 8, 9], [6, 7, 8]), "within1")
|
||||
|
||||
def test_grade_worse(self):
|
||||
self.assertEqual(season.grade([6, 7], [9, 10]), "worse")
|
||||
|
||||
def test_grade_across_the_year_end(self):
|
||||
self.assertEqual(season.grade([12, 1, 2], [1, 2, 3]), "within1")
|
||||
|
||||
def test_grade_two_months_off_at_one_end_is_worse(self):
|
||||
self.assertEqual(season.grade([4, 5, 6, 7, 8], [6, 7, 8]), "worse")
|
||||
|
||||
|
||||
class PhraseTest(unittest.TestCase):
|
||||
def test_phrase_runs(self):
|
||||
self.assertEqual(season.phrase_for([6, 7, 8, 9]), "June to September")
|
||||
self.assertEqual(season.phrase_for([11, 12, 1, 2, 3]), "November to March")
|
||||
self.assertEqual(season.phrase_for([4, 5, 9, 10]),
|
||||
"April to May and September to October")
|
||||
self.assertEqual(season.phrase_for([7]), "July")
|
||||
self.assertEqual(season.phrase_for(list(range(1, 13))), "All year")
|
||||
|
||||
def test_phrase_two_month_run(self):
|
||||
self.assertEqual(season.phrase_for([7, 8]), "July to August")
|
||||
self.assertEqual(season.phrase_for([12, 1]), "December to January")
|
||||
|
||||
|
||||
class MethodSentenceTest(unittest.TestCase):
|
||||
def test_method_sentence_names_dataset_period_and_points(self):
|
||||
text = season.method_sentence("ERA5-Land via Open-Meteo", "2011-2020", 5, "track", 1)
|
||||
self.assertIn("ERA5-Land via Open-Meteo", text)
|
||||
self.assertIn("2011-2020", text)
|
||||
self.assertIn("5", text)
|
||||
self.assertTrue(text.endswith("."))
|
||||
self.assertLess(len(text), 400)
|
||||
|
||||
def test_method_sentence_for_each_kind(self):
|
||||
for kind in ("track", "ends", "place"):
|
||||
text = season.method_sentence("Dataset", "2011-2020", 1, kind, 1)
|
||||
self.assertIn("Dataset", text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user