Add season arcs, grades, phrases and the method sentence

This commit is contained in:
swaits committed 2026-11-20 10:16:19 -07:00
1 parent 157f268974
commit 15dbf07603
2 files changed
+189

No files matched your search

+108
View File
@@ -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}.")