from __future__ import annotations from datetime import UTC, date, datetime import numpy as np import polars as pl import pytest from countryatlas.pipeline.changes import Series, compute_changes_and_events, detect_changes, detect_events from countryatlas.registry import indicators_by_id def _series(ind: str, values: list[float], start: int = 2000, country: str = "CAN") -> Series: years = np.arange(start, start + len(values), dtype=np.int32) return Series(country, ind, [date(int(y), 1, 1) for y in years], years, np.array(values, dtype=float)) def test_inflation_drop_headline_and_since() -> None: ind = indicators_by_id()["inflation"] vals = [2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 8.1, 5.9, 5.5, 3.4] # 2015: 5.5 → 2016: 3.4 (−2.1 pts) hmm: last drop 2.1 vals = [2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 2.1, 2.0, 6.8, 3.4] out = detect_changes(_series("inflation", vals, 2016), ind) kinds = {r["kind"]: r for r in out} assert "yoy_drop" in kinds h = kinds["yoy_drop"]["headline"] assert h.startswith("Inflation fell 3.4 points to 3.4 % in 2025") assert "largest drop" in h assert 0 < kinds["yoy_drop"]["severity"] <= 1 def test_floor_blocks_small_moves() -> None: ind = indicators_by_id()["inflation"] # change_floor 2 points vals = [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0] # +1 pt: huge z but below the floor out = detect_changes(_series("inflation", vals), ind) assert not any(r["kind"] in ("yoy_jump", "yoy_drop") for r in out) def test_record_high_and_relative_headline() -> None: ind = indicators_by_id()["gdp-per-capita"] vals = [1000 * 1.03**k for k in range(15)] vals[5] *= 0.9 # a dip: the series must not be monotone, otherwise records are (rightly) not newsworthy vals[-1] = vals[-2] * 1.25 out = detect_changes(_series("gdp-per-capita", vals), ind) kinds = {r["kind"] for r in out} assert "record_high" in kinds and "yoy_jump" in kinds jump = next(r for r in out if r["kind"] == "yoy_jump") assert "25.0 %" in jump["headline"] and "US$" in jump["headline"] def test_sign_flip_and_events_history() -> None: ind = indicators_by_id()["gdp-growth"] vals = [3.0, 2.5, 3.1, 2.8, 3.0, 2.9, -4.5, 5.0, 2.0, -1.0] ch = detect_changes(_series("gdp-growth", vals), ind) assert any(r["kind"] == "sign_flip" for r in ch) ev = detect_events(_series("gdp-growth", vals), ind) flips = [r for r in ev if r["kind"] == "sign_flip"] assert len(flips) == 3 # 2006, 2007, 2009 assert any(r["kind"] == "yoy_drop" and r["year"] == 2006 for r in ev) def test_monotone_and_cumulative_series_are_silent() -> None: inds = indicators_by_id() vals = [100.0 * 1.02**k for k in range(30)] # strictly increasing: every point is a "record" ch = detect_changes(_series("population", vals), inds["population"]) assert not any(r["kind"] in ("record_high", "n_year_high") for r in ch) rows = [{"country_id": "CAN", "indicator_id": "cumulative-co2", "period": date(1990 + k, 1, 1), "year": 1990 + k, "value": v} for k, v in enumerate(vals)] ch2, ev2 = compute_changes_and_events(pl.DataFrame(rows), inds) assert ch2.height == 0 and ev2.height == 0 def test_changes_are_recent_and_weighted() -> None: inds = indicators_by_id() old = [2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 2.1, 2.0, 6.8, 3.4] # series ends in 2008 rows = [{"country_id": "CAN", "indicator_id": "inflation", "period": date(1999 + k, 1, 1), "year": 1999 + k, "value": v} for k, v in enumerate(old)] rows += [{"country_id": "FRA", "indicator_id": "inflation", "period": date(2016 + k, 1, 1), "year": 2016 + k, "value": v} for k, v in enumerate(old)] from datetime import UTC, datetime ch, ev = compute_changes_and_events(pl.DataFrame(rows), inds, headline_ids={"inflation"}, now=datetime(2026, 9, 1, tzinfo=UTC)) assert set(ch["country_id"].to_list()) == {"FRA"} # Canada's 2008 drop is an event, not a change assert "CAN" in set(ev["country_id"].to_list()) import json d = json.loads(ch["detail"][0]) assert d["weight"] == 1.0 and "raw_severity" in d ch_w, _ = compute_changes_and_events(pl.DataFrame(rows), inds, headline_ids=set(), now=datetime(2026, 9, 1, tzinfo=UTC)) assert json.loads(ch_w["detail"][0])["weight"] == 0.9 # inflation is featured but not headline here assert ch_w["severity"][0] == pytest.approx(min(1.0, d["raw_severity"] * 0.9), rel=1e-3) def test_index_series_need_10pct_and_quarterly_is_annualised() -> None: inds = indicators_by_id() ind = inds["real-house-price-index"] # format index, ranking_eligible false assert ind.format == "index" and not ind.ranking_eligible smooth = [100 + k for k in range(12)] smooth[-1] = smooth[-2] * 1.05 # +5 %: big z, but below the 10 % rule assert not any(r["kind"] in ("yoy_jump", "yoy_drop") for r in detect_changes(_series("real-house-price-index", smooth), ind)) big = list(smooth) big[-1] = big[-2] * 1.2 # +20 % assert any(r["kind"] == "yoy_jump" for r in detect_changes(_series("real-house-price-index", big), ind)) # quarterly rows collapse to one value per year (last value for an index) → at most one detection per year rows = [] for y in range(2015, 2026): for q, month in enumerate((1, 4, 7, 10)): v = 100 + (y - 2015) * 2 + q * 0.3 if y == 2025: v = 150.0 + q # +~25 % jump in 2025 rows.append({"country_id": "CAN", "indicator_id": "real-house-price-index", "period": date(y, month, 1), "year": y, "frequency": "Q", "value": v}) ch, ev = compute_changes_and_events(pl.DataFrame(rows), inds, now=datetime(2026, 1, 1, tzinfo=UTC)) assert ch.filter(pl.col("kind") == "yoy_jump").height == 1 assert ev.group_by(["indicator_id", "year"]).len()["len"].max() <= 3 assert ev["severity"].min() >= 0.35 assert ch.filter(pl.col("kind") == "yoy_jump")["value"][0] == 153.0 # last quarter of 2025 def test_driver_over_frame() -> None: inds = indicators_by_id() rows = [] for c in ("CAN", "FRA"): for k, v in enumerate([2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 2.1, 2.0, 6.8, 3.4]): rows.append({"country_id": c, "indicator_id": "inflation", "period": date(2016 + k, 1, 1), "year": 2016 + k, "value": v}) df = pl.DataFrame(rows) ch, ev = compute_changes_and_events(df, inds) assert ch.filter(pl.col("kind") == "yoy_drop").height == 2 assert set(ch.columns) >= {"id", "country_id", "indicator_id", "kind", "headline", "severity", "detected_at"} assert ev.height >= 2 # ------------------------------------------------------------------------------------------------ change detection 2.0 def test_structural_break_in_changes_and_events() -> None: ind = indicators_by_id()["unemployment-rate"] # 12 years around 5 % (with small noise so the series is not monotone), then 8 years around 12 % → break in 2012 vals = [5.0, 5.2, 4.9, 5.1, 5.0, 5.3, 4.8, 5.1, 5.0, 5.2, 4.9, 5.1, 12.0, 12.2, 11.9, 12.1, 12.0, 12.3, 11.8, 12.1] ch = detect_changes(_series("unemployment-rate", vals, 2000), ind) # last year 2019, break 2012 → within 10 years brk = [r for r in ch if r["kind"] == "structural_break"] assert len(brk) == 1 b = brk[0] assert b["year"] == 2012 and "shifted to a higher level around 2012" in b["headline"] assert 0.45 <= b["severity"] <= 0.7 import json as _json d = b["detail"] if isinstance(b["detail"], dict) else _json.loads(b["detail"]) assert d["break_year"] == 2012 and d["gain"] >= 0.5 and d["shift_ratio"] >= 1.5 and d["min_segment"] == 5 ev = detect_events(_series("unemployment-rate", vals, 2000), ind) assert len([r for r in ev if r["kind"] == "structural_break"]) == 1 # an old break (25 years ago) stays an event but is not a recent change old = vals + [12.0, 12.2, 11.9, 12.1, 12.0, 12.3, 11.8, 12.1, 12.0, 12.2, 11.9, 12.1, 12.0, 12.3, 11.8] ch_old = detect_changes(_series("unemployment-rate", old, 1988), ind) # last year 2022, break 2000 → 22 years ago assert not any(r["kind"] == "structural_break" for r in ch_old) assert any(r["kind"] == "structural_break" for r in detect_events(_series("unemployment-rate", old, 1988), ind)) # a smooth trend is not a level shift trend = [5.0 + 0.3 * k for k in range(20)] assert not any(r["kind"] == "structural_break" for r in detect_changes(_series("unemployment-rate", trend, 2000), ind)) def test_trend_reversal() -> None: ind = indicators_by_id()["unemployment-rate"] # floor 1 point vals = [8.0, 8.0, 8.0, 9.0, 8.0, 7.0, 6.0, 7.0, 8.0, 9.0] # 3 falls (9→6) then 3 rises (6→9): +3 points ≥ floor ch = detect_changes(_series("unemployment-rate", vals, 2010), ind) rev = [r for r in ch if r["kind"] == "trend_reversal"] assert len(rev) == 1 assert rev[0]["headline"].startswith("Unemployment has risen for three consecutive years after falling for three") assert rev[0]["severity"] == pytest.approx(0.55) and rev[0]["ref_value"] == 6.0 down = [3.0, 3.0, 3.0, 2.0, 3.0, 4.0, 5.0, 4.0, 3.0, 2.0] r2 = [r for r in detect_changes(_series("unemployment-rate", down, 2010), ind) if r["kind"] == "trend_reversal"] assert len(r2) == 1 and "has fallen for three consecutive years after rising for three" in r2[0]["headline"] # too small a swing (0.3 points) is silent tiny = [8.0, 8.0, 8.0, 8.3, 8.2, 8.1, 8.0, 8.1, 8.2, 8.3] assert not any(r["kind"] == "trend_reversal" for r in detect_changes(_series("unemployment-rate", tiny, 2010), ind)) def test_volatility_spike() -> None: ind = indicators_by_id()["inflation"] # floor 2 points calm = [2.0 + 0.1 * ((-1) ** k) for k in range(16)] # 15 tiny diffs (±0.2) wild = [2.0, 9.0, 1.0, 8.0, 0.0] # 5 large swings ch = detect_changes(_series("inflation", calm + wild, 2005), ind) vol = [r for r in ch if r["kind"] == "volatility_spike"] assert len(vol) == 1 assert "unusually volatile" in vol[0]["headline"] and 0.45 <= vol[0]["severity"] <= 0.7 import json as _json d = vol[0]["detail"] if isinstance(vol[0]["detail"], dict) else _json.loads(vol[0]["detail"]) assert d["ratio"] >= 3.0 and d["recent_years"] == 5 and d["baseline_years"] == 15 # same pattern but the recent swings are below the 2-point floor → silent small = calm + [2.0, 2.5, 2.0, 2.6, 2.1] assert not any(r["kind"] == "volatility_spike" for r in detect_changes(_series("inflation", small, 2005), ind)) # too short a history → silent assert not any(r["kind"] == "volatility_spike" for r in detect_changes(_series("inflation", wild * 2, 2015), ind)) def test_new_kinds_flow_through_driver_with_weights() -> None: inds = indicators_by_id() vals = [5.0, 5.2, 4.9, 5.1, 5.0, 5.3, 4.8, 5.1, 5.0, 5.2, 4.9, 5.1, 12.0, 12.2, 11.9, 12.1, 12.0, 12.3, 11.8, 12.1] rows = [{"country_id": "CAN", "indicator_id": "unemployment-rate", "period": date(2006 + k, 1, 1), "year": 2006 + k, "value": v} for k, v in enumerate(vals)] ch, ev = compute_changes_and_events(pl.DataFrame(rows), inds, headline_ids={"unemployment-rate"}, now=datetime(2026, 9, 1, tzinfo=UTC)) kinds = set(ch["kind"].to_list()) assert "structural_break" in kinds assert ev.filter(pl.col("kind") == "structural_break").height == 1