Files
stack/tests/dev/test_ingest_ipps_tables.py
kert 1aa288c344
Some checks failed
CI / lint (push) Failing after 48s
CI / notebooks-smoke (push) Successful in 2m0s
Deploy / changes (push) Successful in 18s
CI / test (push) Successful in 4m23s
Infra CI / notebooks (push) Successful in 1m5s
Infra CI / zotero (push) Successful in 16s
Infra CI / docs (push) Successful in 19s
Infra CI / api (push) Successful in 1m19s
Infra CI / bcda (push) Successful in 1m48s
Infra CI / mc (push) Successful in 18s
Infra CI / llm (push) Successful in 56s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Successful in 2m34s
Lean Pay / pay (push) Successful in 3m22s
Deploy / llm (push) Successful in 1m43s
Deploy / mc (push) Has been skipped
Deploy / bcda (push) Successful in 2m10s
Deploy / report (push) Successful in 16s
feat(pay): load the published numbers the fence was missing
The wage index, pass-through rates, the missing APC years, the CY2027
work GPCI, equipment usage, IPPS standardized amounts, MS-DRG weights,
and the MSSP and REACH financial results now come from the CMS files.
The snapshot records those observed ranges. Maintenance stays the
published 5/100. There is still no IPPS or LEAD pricer.
2026-11-17 17:04:51 -05:00

162 lines
5.4 KiB
Python

"""Published IPPS table readers. Synthetic files, so CI has no CMS dump."""
from __future__ import annotations
import importlib.util
import io
import sys
import zipfile
from pathlib import Path
import openpyxl
_SCRIPT = (
Path(__file__).resolve().parents[2] / "dev" / "scripts" / "ingest_ipps_tables.py"
)
_spec = importlib.util.spec_from_file_location("ingest_ipps_tables", _SCRIPT)
assert _spec is not None and _spec.loader is not None
ipps = importlib.util.module_from_spec(_spec)
sys.modules["ingest_ipps_tables"] = ipps
_spec.loader.exec_module(ipps)
def test_member_kind_is_the_table_id_not_a_digit_in_the_rule_number() -> None:
assert ipps._member_kind("CMS-1833-F Tables 3.txt") == "table3"
assert ipps._member_kind("CMS-1632-F Table 3.txt") == "table3"
assert ipps._member_kind("FY 2014 FR Table 3A.txt") is None
assert ipps._member_kind("CMS-1735-CN Table 2.txt") is None
assert ipps._member_kind("CMS_1632_CN_Table_5.txt") == "table5"
assert ipps._member_kind("CMS-1833-F Tables 4a.txt") == "table4a"
def test_the_reclassified_index_is_not_the_puerto_rico_column() -> None:
text = (
"CBSA\tArea Name\tState\tWage Index\tReclassified Wage Index\t"
"Reclassified Puerto Rico Specific Wage Index\n"
"01\tAlabama\tAL\t0.7393\t0.8000\t1.1111\n"
)
frame = ipps.parse_wage_text(text, 2026)
row = frame.iloc[0]
assert row["wage_index"] == 0.7393
assert row["reclassified_wage_index"] == 0.8
def test_a_short_correction_does_not_replace_the_full_table(tmp_path: Path) -> None:
full = _wage_text([("10180", "TX", "0.8464")] * 10)
short = _wage_text([("99999", "MA", "0.5000")])
path = tmp_path / "wage.zip"
with zipfile.ZipFile(path, "w") as zf:
zf.writestr("FR Table 3.txt", full)
zf.writestr("CN Table 3.txt", short)
frame = ipps.read_wage_year(path, 2014)
assert len(frame) == 10
assert set(frame["wage_index"]) == {0.8464}
def test_a_full_correction_reprint_replaces_the_final_rule(tmp_path: Path) -> None:
final = _wage_text([("10180", "TX", "0.8464")] * 10)
corrected = _wage_text([("10180", "TX", "0.8500")] * 10)
path = tmp_path / "wage.zip"
with zipfile.ZipFile(path, "w") as zf:
zf.writestr("FR Table 3.txt", final)
zf.writestr("CN Table 3.txt", corrected)
frame = ipps.read_wage_year(path, 2014)
assert set(frame["wage_index"]) == {0.85}
def test_the_cap_applied_weight_is_the_paid_weight() -> None:
capped = (
"MS-DRG\tWeights\tWeights - 10% Cap Applied\t"
"Geometric Mean LOS\tArithmetic Mean LOS\n"
"001\t1.5\t1.2\t2.0\t3.0\n"
)
frame = ipps.parse_drg_text(capped, 2026)
assert frame.iloc[0]["relative_weight"] == 1.2
plain = "MS-DRG\tWeights\tGeometric Mean LOS\tArithmetic Mean LOS\n001\t1.5\t2\t3\n"
assert ipps.parse_drg_text(plain, 2026).iloc[0]["relative_weight"] == 1.5
def test_standardized_amounts_are_the_published_points() -> None:
grid = [
["TABLE 1A"],
[None, "Submitted quality data and is a meaningful EHR user", None],
[None, "Labor", "Nonlabor"],
["National", 4456.72, "Not Applicable"],
["TABLE 1D"],
[None, "Rate"],
["National", 524.15],
]
frame = ipps.parse_standardized_xlsx(_workbook({"FY 2026 Table 1A-1E": grid}), 2026)
got = {(row.table_code, row.component): row.amount for row in frame.itertuples()}
assert got["1A", "national_quality_ehr_labor"] == 4456.72
assert got["1D", "national_rate"] == 524.15
assert all(amount != 0 for amount in got.values())
assert len(got) == 2
def test_a_full_later_reprint_wins_and_a_short_one_does_not() -> None:
full = [
["TABLE 1A"],
[None, "Labor", "Nonlabor"],
["National", 10, 11],
]
short = [
["TABLE 1A"],
[None, "Labor"],
["National", 99],
]
kept = ipps.parse_standardized_xlsx(
_workbook({"FR Table 1A-1E": full, "CN Table 1A-1E": short}),
2016,
)
assert set(kept["amount"]) == {10, 11}
corrected = [
["TABLE 1A"],
[None, "Labor", "Nonlabor"],
["National", 20, 21],
]
kept = ipps.parse_standardized_xlsx(
_workbook({"FR Table 1A-1E": full, "CN Table 1A-1E": corrected}),
2016,
)
assert set(kept["amount"]) == {20, 21}
caa = [
["TABLE 1A"],
[None, "Labor", "Nonlabor"],
["National", 30, 31],
]
kept = ipps.parse_standardized_xlsx(
_workbook({"CN Table 1A-1E": corrected, "CAA Table 1A-1E": caa}),
2016,
)
assert set(kept["amount"]) == {30, 31}
def test_a_percent_encoded_space_is_not_the_performance_year() -> None:
assert ipps._perf_year_from_filename("PY%202023%20ACO%20Results%20PUF.csv") == 2023
name = "PY_Financial_and_Quality_Results_2024_revised%202026_07_17.csv"
assert ipps._perf_year_from_filename(name) == 2024
def _wage_text(rows: list[tuple[str, str, str]]) -> str:
lines = ["CBSA\tArea Name\tState\tWage Index"]
lines.extend(f"{cbsa}\tArea\t{state}\t{index}" for cbsa, state, index in rows)
return "\n".join(lines) + "\n"
def _workbook(sheets: dict[str, list[list[object]]]) -> bytes:
book = openpyxl.Workbook()
first = True
for title, rows in sheets.items():
sheet = book.active if first else book.create_sheet()
first = False
sheet.title = title
for row in rows:
sheet.append(row)
buf = io.BytesIO()
book.save(buf)
book.close()
return buf.getvalue()