fix(pfs): disappeared only when the status is complete in the year; inclusive range cap
This commit is contained in:
@@ -60,8 +60,30 @@ def _ev(code: str, year: int, kind: str, note: str = "") -> EventRow:
|
||||
return EventRow(code.upper(), year, kind, "", "", "", 0, 0, "rvu", False, note)
|
||||
|
||||
|
||||
def status_counts(con: Any) -> dict[tuple[int, str], int]:
|
||||
"""Distinct codes per (year, status) in ``pfs.rvu`` — one GROUP BY."""
|
||||
return {
|
||||
(int(y), s or ""): int(n)
|
||||
for y, s, n in con.execute(
|
||||
"SELECT year, status_code, count(DISTINCT hcpcs) FROM pfs.rvu "
|
||||
"GROUP BY year, status_code"
|
||||
).fetchall()
|
||||
}
|
||||
|
||||
|
||||
#: 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.
|
||||
_COMPLETE_SHARE = 0.9
|
||||
|
||||
|
||||
def rvu_events(
|
||||
con: Any, code: str, *, revalue_threshold: float = 0.10
|
||||
con: Any,
|
||||
code: str,
|
||||
*,
|
||||
revalue_threshold: float = 0.10,
|
||||
counts: dict[tuple[int, str], int] | None = None,
|
||||
) -> list[EventRow]:
|
||||
rows = _base_rows(con, code)
|
||||
if not rows:
|
||||
@@ -72,7 +94,11 @@ def rvu_events(
|
||||
if first > lo:
|
||||
out.append(_ev(code, first, "appeared"))
|
||||
if last < hi:
|
||||
out.append(_ev(code, last + 1, "disappeared"))
|
||||
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):
|
||||
out.append(_ev(code, last + 1, "disappeared"))
|
||||
for (py, ps, pd, pt), (y, s, d, t) in zip(rows, rows[1:]):
|
||||
if s != ps:
|
||||
out.append(_ev(code, y, "status_change", f"{ps}→{s}"))
|
||||
@@ -184,8 +210,8 @@ def _patterns(code: str) -> list[tuple[str, re.Pattern[str], str]]:
|
||||
]
|
||||
|
||||
|
||||
#: A code range is expanded only when its numeric span is at most this
|
||||
#: many codes past the first (a sane block, never "00100-99999").
|
||||
#: A code range is expanded only when its end is at most this many past
|
||||
#: its first code (b - a <= 50, i.e. 51 codes; never "00100-99999").
|
||||
_RANGE_MAX = 50
|
||||
_RE_RANGE = re.compile(r"\b(\d{5})[-\u2013](\d{5})\b")
|
||||
_RE_LETTER_RANGE = re.compile(r"\b([A-Z])(\d{4})[-\u2013]\1(\d{4})\b")
|
||||
@@ -199,7 +225,7 @@ def _expand_ranges(text: str) -> str:
|
||||
a, b = int(m.group(1)), int(m.group(2))
|
||||
return (
|
||||
" ".join(str(n) for n in range(a, b + 1))
|
||||
if 0 < b - a < _RANGE_MAX
|
||||
if 0 < b - a <= _RANGE_MAX
|
||||
else m.group(0)
|
||||
)
|
||||
|
||||
@@ -208,7 +234,7 @@ def _expand_ranges(text: str) -> str:
|
||||
a, b = int(m.group(2)), int(m.group(3))
|
||||
return (
|
||||
" ".join(f"{letter}{n:04d}" for n in range(a, b + 1))
|
||||
if 0 < b - a < _RANGE_MAX
|
||||
if 0 < b - a <= _RANGE_MAX
|
||||
else m.group(0)
|
||||
)
|
||||
|
||||
@@ -467,10 +493,11 @@ def lineage_all(
|
||||
``_merge``."""
|
||||
codes_u = [c.upper() for c in codes]
|
||||
fr_by_code = fr_events_bucketed(store, codes_u)
|
||||
counts = status_counts(con)
|
||||
return {
|
||||
code: _merge(
|
||||
code,
|
||||
rvu_events(con, code),
|
||||
rvu_events(con, code, counts=counts),
|
||||
fr_by_code.get(code, []),
|
||||
cpt_events(con, code),
|
||||
)
|
||||
|
||||
@@ -469,6 +469,10 @@ class TestLineage:
|
||||
res = runner.invoke(app, ["pfs", "lineage", "--family", "NOPE-NOPE"])
|
||||
assert res.exit_code != 0
|
||||
|
||||
def test_codes_for_with_no_targets_names_every_flag(self, con):
|
||||
with pytest.raises(Exception, match="--all-seen"):
|
||||
pfs_cli._codes_for(con, [], [], False)
|
||||
|
||||
def test_codes_for_all_seen_warns_unless_quiet(self, con, caplog):
|
||||
import logging
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ from pfs.lineage import (
|
||||
rvu_events,
|
||||
rvu_year_span,
|
||||
seen_codes,
|
||||
status_counts,
|
||||
)
|
||||
|
||||
RVU_COLS = "hcpcs VARCHAR, mod VARCHAR, description VARCHAR, status_code VARCHAR, non_fac_total DOUBLE, year INTEGER"
|
||||
@@ -392,7 +393,11 @@ class TestDeletedCodeDisappears:
|
||||
c = duckdb.connect(":memory:")
|
||||
ensure_tables(c)
|
||||
c.execute(f"CREATE TABLE pfs.rvu ({RVU_COLS})")
|
||||
rows = [("00000", None, "f", "A", 1.0, y) for y in range(2015, 2028)]
|
||||
rows = [
|
||||
(f"0000{i}", None, "f", "A", 1.0, y)
|
||||
for y in range(2015, 2028)
|
||||
for i in range(9)
|
||||
]
|
||||
rows += [("99343", None, "d", "A", 1.0, y) for y in range(2015, 2023)]
|
||||
c.executemany("INSERT INTO pfs.rvu VALUES (?,?,?,?,?,?)", rows)
|
||||
assert [(e.year, e.kind) for e in rvu_events(c, "99343")] == [
|
||||
@@ -400,6 +405,53 @@ class TestDeletedCodeDisappears:
|
||||
]
|
||||
|
||||
|
||||
class TestPartialYearDisappearance:
|
||||
@staticmethod
|
||||
def _con(m_last_year_kept):
|
||||
c = duckdb.connect(":memory:")
|
||||
ensure_tables(c)
|
||||
c.execute(f"CREATE TABLE pfs.rvu ({RVU_COLS})")
|
||||
rows = []
|
||||
for y in (2025, 2026):
|
||||
rows += [(f"A{i:04d}", None, "d", "A", 1.0, y) for i in range(10)]
|
||||
rows += [(f"M{i:04d}", None, "d", "M", 1.0, y) for i in range(10)]
|
||||
# final, partial year: 9 of 10 A codes (exactly 90%), M kept per arg
|
||||
rows += [(f"A{i:04d}", None, "d", "A", 1.0, 2027) for i in range(9)]
|
||||
rows += [
|
||||
(f"M{i:04d}", None, "d", "M", 1.0, 2027) for i in range(m_last_year_kept)
|
||||
]
|
||||
c.executemany("INSERT INTO pfs.rvu VALUES (?,?,?,?,?,?)", rows)
|
||||
return c
|
||||
|
||||
def test_incomplete_status_gets_no_disappeared_and_complete_does(self):
|
||||
c = self._con(0)
|
||||
assert rvu_events(c, "M0001") == []
|
||||
# A is at exactly 90% -> complete -> the dropped A code disappears
|
||||
assert [(e.year, e.kind) for e in rvu_events(c, "A0009")] == [
|
||||
(2027, "disappeared")
|
||||
]
|
||||
|
||||
def test_just_below_ninety_percent_is_incomplete(self):
|
||||
c = self._con(0)
|
||||
c.execute("DELETE FROM pfs.rvu WHERE year = 2027 AND hcpcs = 'A0008'")
|
||||
assert status_counts(c)[(2027, "A")] == 8
|
||||
assert rvu_events(c, "A0009") == []
|
||||
|
||||
def test_status_at_ninety_percent_of_m_fires(self):
|
||||
c = self._con(9)
|
||||
assert [(e.year, e.kind) for e in rvu_events(c, "M0009")] == [
|
||||
(2027, "disappeared")
|
||||
]
|
||||
|
||||
|
||||
class TestExpandRangeBoundary:
|
||||
def test_span_fifty_expands_fifty_one_does_not(self):
|
||||
assert len(_expand_ranges("99100-99150").split()) == 51
|
||||
assert _expand_ranges("99100-99151") == "99100-99151"
|
||||
assert len(_expand_ranges("G0100-G0150").split()) == 51
|
||||
assert _expand_ranges("G0100-G0151") == "G0100-G0151"
|
||||
|
||||
|
||||
class TestExpandRanges:
|
||||
def test_letter_range(self):
|
||||
assert _expand_ranges("G0008-G0010") == "G0008 G0009 G0010"
|
||||
|
||||
Reference in New Issue
Block a user