From 514888f958ef97d3ca0b311cf8378dda9c027306 Mon Sep 17 00:00:00 2001 From: kert Date: Thu, 12 Nov 2026 23:14:41 -0500 Subject: [PATCH] fix(pfs): dossier rounds, payment and utilization as typed rows Task 16's golden reads the dossier's valuation rounds and payment rows by attribute. Rounds are RoundView (the stored RoundRow plus each cited field's resolved Cite and the rationale paragraph's, R10), payment rows PaymentRow (a valuation row without the RVU file's short descriptor), utilization SeriesRow; all round-trip through the stored JSON. --- src/pfs/dossier.py | 106 ++++++++++++++++++++++++++++---------- tests/pfs/test_dossier.py | 42 ++++++++++----- 2 files changed, 108 insertions(+), 40 deletions(-) diff --git a/src/pfs/dossier.py b/src/pfs/dossier.py index 438c12b..0f6eff7 100644 --- a/src/pfs/dossier.py +++ b/src/pfs/dossier.py @@ -151,6 +151,42 @@ class Excerpt: cite: Cite +@dataclass(frozen=True) +class RoundView(RoundRow): + """A valuation round as the dossier carries it: the stored row, each value + field's resolved cite and the rationale paragraph's cite (R10: the page + never builds a URL). An empty field stays empty; the page omits it.""" + + cites: Mapping[str, Cite] = field(default_factory=dict) + rationale: Cite | None = None + + +@dataclass(frozen=True) +class PaymentRow: + """One ``pfs.valuation.valuation`` row without the RVU file's short + descriptor (AMA text): proposed and final rows stay separate rows.""" + + code: str + vintage: str + year: int + proposed: bool + status: str + work: float | None + pe_nf: float | None + pe_f: float | None + mp: float | None + total_nf: float | None + total_f: float | None + cf: float + pay_nf: float | None + pay_f: float | None + label: str + citation: str + url: str + status_note: str = "" + cf_note: str = "" + + @dataclass class Dossier: family: str @@ -160,9 +196,9 @@ class Dossier: years_active: str summary: str timeline: list[TimelineEvent] - rounds: list[dict] - payment: list[dict] - utilization: list[dict] + rounds: list[RoundView] + payment: list[PaymentRow] + utilization: list[Any] stakeholders: list[dict] governance: list[Excerpt] sources: dict[str, list[dict]] @@ -208,10 +244,28 @@ class Dossier: d["governance"] = [ Excerpt(e["text"], Cite(**e["cite"])) for e in d["governance"] ] + d["rounds"] = [ + RoundView( + **{ + **r, + "cites": {k: Cite(**c) for k, c in r["cites"].items()}, + "rationale": Cite(**r["rationale"]) if r["rationale"] else None, + } + ) + for r in d["rounds"] + ] + d["payment"] = [PaymentRow(**r) for r in d["payment"]] + d["utilization"] = [_series_row(**r) for r in d["utilization"]] d["facts"] = {k: _fact_from(v) for k, v in d["facts"].items()} return cls(**d) +def _series_row(**kw: Any) -> Any: + from pfs.utilization import SeriesRow + + return SeriesRow(**kw) + + def _fact_dict(f: Fact) -> dict: d = asdict(f) d["values"] = dict(f.values) @@ -1115,27 +1169,23 @@ def _pe_facts(bib: _Bib, rounds: Sequence[RoundRow]) -> list[Fact]: return out -def _round_dicts(bib: _Bib, rounds: Sequence[RoundRow]) -> list[dict]: - """The valuation panel's rows: every non-empty field (an empty column is - never shown as a value), each value field's resolved cite and the - rationale paragraph's link (R10).""" - out = [] - for r in rounds: - d = { - k: v - for k, v in asdict(r).items() - if v not in (None, "") and k not in ("field_cites", "extractor", "family") - } - d["cites"] = { - f: asdict(_round_cite(bib, r, f)[0]) - for f in json.loads(r.field_cites or "{}") - } - if r.rationale_p_id: - d["rationale"] = asdict( +def _round_views(bib: _Bib, rounds: Sequence[RoundRow]) -> list[RoundView]: + """The valuation panel's rows: each round with every cited field's + resolved cite and the rationale paragraph's (R10).""" + return [ + RoundView( + **asdict(r), + cites={ + f: _round_cite(bib, r, f)[0] for f in json.loads(r.field_cites or "{}") + }, + rationale=( fr_para_cite(bib.store, r.item_key, r.rationale_p_id) - ) - out.append(d) - return out + if r.rationale_p_id + else None + ), + ) + for r in rounds + ] # ── facts: CMS's summaries and our stance counts (kept apart) ─────── @@ -1280,10 +1330,10 @@ def _payment_rows(con: Any, family: Any) -> list[Any]: return [r for r in rows if r.code not in since or r.year >= since[r.code]] -def _payment_dict(r: Any) -> dict: +def _payment_row(r: Any) -> PaymentRow: d = asdict(r) d.pop("description", None) # the RVU file's short descriptor is AMA text - return d + return PaymentRow(**d) def _payment_facts(rows: Sequence[Any]) -> list[Fact]: @@ -2372,7 +2422,7 @@ def build_dossier( ) final_events = [replace(e, what_changed=lines[i]) for i, e in enumerate(events)] codes = [*family.codes, *(ln.target for ln in family.links)] - util = [asdict(r) for r in _rows(read_series, con, codes)] + util = _rows(read_series, con, codes) classified = _classified(con, family) acc = "" if classified: @@ -2386,8 +2436,8 @@ def build_dossier( years_active=_years_active(facts, a.payment), summary=summary, timeline=final_events, - rounds=_round_dicts(_Bib(store), a.rounds), - payment=[_payment_dict(r) for r in a.payment], + rounds=_round_views(_Bib(store), a.rounds), + payment=[_payment_row(r) for r in a.payment], utilization=util, stakeholders=_stakeholders(con, family, a), governance=list(excerpts), diff --git a/tests/pfs/test_dossier.py b/tests/pfs/test_dossier.py index 3b6109b..a71b675 100644 --- a/tests/pfs/test_dossier.py +++ b/tests/pfs/test_dossier.py @@ -749,9 +749,10 @@ def test_payment_facts_and_rows(home_fixture): f.text.startswith("For CY2027, CMS proposed a national non-facility payment") for f in pay ) - assert all("description" not in row for row in d.payment) - years_99343 = sorted({r["year"] for r in d.payment if r["code"] == "99343"}) - assert years_99343[-1] == 2022 + assert not hasattr(d.payment[0], "description") # AMA text never kept + assert {(p.code, p.year) for p in d.payment if p.code == "99343"} == { + ("99343", y) for y in range(2015, 2023) + } def test_coverage_notes_from_data(home_fixture): @@ -1025,17 +1026,28 @@ def test_build_dossier(home_fixture): ) assert d.utilization == [] assert d.rounds and all( - "url" in r["cites"]["final_work_rvu"] for r in d.rounds if "final_work_rvu" in r - ) - r = next( - r - for r in d.rounds - if r["code"] == "99341" and r["rule_year"] == 2023 and r["rule_kind"] == "final" + r.cites["final_work_rvu"].url for r in d.rounds if r.final_work_rvu is not None ) + r = {(x.code, x.item_key): x for x in d.rounds} + hv = r["99341", "PETM225D"] assert ( - "comparators" not in r and "survey_intra_min" not in r - ) # never an empty field - assert r["rationale"]["url"].endswith("#p-1685") + hv.rule_year, + hv.proposed_work_rvu, + hv.final_work_rvu, + hv.ruc_percentile, + hv.decision, + ) == (2023, 1.0, 1.0, "25th", "accept") + assert hv.rationale.url.endswith("#p-1685") + old = r["99343", "54GTKR5G"] + assert ( + old.rule_year, + old.source, + old.ruc_work_rvu, + old.final_work_rvu, + old.decision, + old.page, + ) == (2008, "fr-table", 2.27, 2.53, "refine", 66361) + assert r["99341", "MZ24MX5S"].rationale is None assert d.timeline and d.summary assert d.stance_accuracy.startswith("Preliminary model classification") @@ -1047,6 +1059,12 @@ def test_json_roundtrip(home_fixture): assert back == d assert isinstance(back.facts["F1"], Fact) assert isinstance(back.timeline[0], TimelineEvent) + from pfs.utilization import SeriesRow + + d2 = dataclasses.replace( + d, utilization=[SeriesRow("99341", 2023, 1, 2, 3.0, 4.0, 5.0)] + ) + assert Dossier.from_json(d2.to_json()) == d2 def test_write_read_dossier(home_fixture):