diff --git a/src/pfs/lineage.py b/src/pfs/lineage.py index f66e795..3a88fb7 100644 --- a/src/pfs/lineage.py +++ b/src/pfs/lineage.py @@ -71,13 +71,20 @@ def status_counts(con: Any) -> dict[tuple[int, str], int]: } -#: A status counts as present in a year when that year holds at least this -#: share of the prior year's codes with it. A partial file (an Addendum B, -#: not the full RVU file) drops whole statuses; a code vanishing from it is -#: not evidence the code left the schedule. +#: A status is "complete" in year Z when Z holds at least this share of +#: year Z-1's codes with it. A partial file (an Addendum B, not the full +#: RVU file) drops whole statuses; a code vanishing from it alone is not +#: evidence the code left the schedule. _COMPLETE_SHARE = 0.9 +def _status_complete(counts: dict[tuple[int, str], int], year: int, st: str) -> bool: + """``count(year, st) >= 0.9 * count(year - 1, st)``. When the prior + year had no such codes (retired status, or one never used) the test is + ``0 >= 0``, so the status counts as complete.""" + return counts.get((year, st), 0) >= _COMPLETE_SHARE * counts.get((year - 1, st), 0) + + def rvu_events( con: Any, code: str, @@ -97,7 +104,11 @@ def rvu_events( if counts is None: counts = status_counts(con) st = rows[-1][1] - if counts.get((last + 1, st), 0) >= _COMPLETE_SHARE * counts.get((last, st), 0): + # The code is absent from every year after `last`, so ANY later + # complete file for its status (Z in last+1..hi) confirms the + # absence: a retired status or a one-year dip still fires, a partial + # newest year with no later file does not. + if any(_status_complete(counts, z, st) for z in range(last + 1, hi + 1)): out.append(_ev(code, last + 1, "disappeared")) for (py, ps, pd, pt), (y, s, d, t) in zip(rows, rows[1:]): if s != ps: diff --git a/tests/pfs/test_lineage.py b/tests/pfs/test_lineage.py index e3c297e..d4d0b1f 100644 --- a/tests/pfs/test_lineage.py +++ b/tests/pfs/test_lineage.py @@ -444,6 +444,53 @@ class TestPartialYearDisappearance: ] +class TestRetiredAndDipStatuses: + @staticmethod + def _con(rows): + c = duckdb.connect(":memory:") + ensure_tables(c) + c.execute(f"CREATE TABLE pfs.rvu ({RVU_COLS})") + c.executemany("INSERT INTO pfs.rvu VALUES (?,?,?,?,?,?)", rows) + return c + + def test_retired_status_fires_in_the_year_after_last_seen(self): + rows = [ + (f"A{i:04d}", None, "d", "A", 1.0, y) + for y in (1, 2, 3, 4) + for i in range(10) + ] + rows += [ + (f"Q{i:04d}", None, "d", "Q", 1.0, y) for y in (1, 2) for i in range(5) + ] + c = self._con(rows) + assert [(e.year, e.kind) for e in rvu_events(c, "Q0001")] == [ + (3, "disappeared") + ] + + def test_one_year_dip_that_recovers_still_fires_in_the_dip_year(self): + rows = [ + (f"A{i:04d}", None, "d", "A", 1.0, y) for y in (1, 2, 3) for i in range(10) + ] + rows += [(f"M{i:04d}", None, "d", "M", 1.0, 1) for i in range(10)] + rows += [("M0000", None, "d", "M", 1.0, 2)] + rows += [(f"N{i:04d}", None, "d", "M", 1.0, 3) for i in range(10)] + c = self._con(rows) + # M0001 last seen in year 1, never again; year 2 dipped, year 3 recovered + assert [(e.year, e.kind) for e in rvu_events(c, "M0001")] == [ + (2, "disappeared") + ] + + def test_partial_newest_year_with_no_later_file_is_suppressed(self): + rows = [ + (f"A{i:04d}", None, "d", "A", 1.0, y) for y in (1, 2, 3) for i in range(10) + ] + rows += [ + (f"M{i:04d}", None, "d", "M", 1.0, y) for y in (1, 2) for i in range(10) + ] + c = self._con(rows) + assert rvu_events(c, "M0001") == [] + + class TestExpandRangeBoundary: def test_span_fifty_expands_fifty_one_does_not(self): assert len(_expand_ranges("99100-99150").split()) == 51