File size: 2,825 Bytes
c6cfe5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Sanity checks for the staged reference data.

Guards against:
  - Typo in an HCC id that appears only in one table.
  - ICD-10 codes in the crosswalk that have no description.
  - Hierarchy groups with no labeled members.
"""

from __future__ import annotations

import csv
from pathlib import Path

import pytest

REF = Path("data/reference")


def _rows(path: Path) -> list[dict[str, str]]:
    with path.open() as f:
        return list(csv.DictReader(f))


def test_all_crosswalk_hccs_are_labeled():
    labels = {r["hcc_id"] for r in _rows(REF / "hcc_v28_labels.csv")}
    crosswalk_hccs = {r["hcc_id"] for r in _rows(REF / "hcc_v28_crosswalk.csv")}
    missing = crosswalk_hccs - labels
    assert not missing, f"HCCs in crosswalk but not in labels: {sorted(missing)}"


def test_all_crosswalk_icd10s_have_descriptions():
    desc = {r["icd10_code"] for r in _rows(REF / "icd10_cm_descriptions.csv")}
    codes = {r["icd10_code"] for r in _rows(REF / "hcc_v28_crosswalk.csv")}
    missing = codes - desc
    assert not missing, f"ICD-10 codes in crosswalk but not in descriptions: {sorted(missing)}"


def test_hierarchy_references_known_hccs():
    labels = {r["hcc_id"] for r in _rows(REF / "hcc_v28_labels.csv")}
    for r in _rows(REF / "hcc_v28_hierarchy.csv"):
        assert r["higher_hcc"] in labels, f"Unknown higher_hcc: {r}"
        assert r["trumps_lower_hcc"] in labels, f"Unknown trumps_lower_hcc: {r}"


def test_hierarchy_groups_are_consistent():
    groups_from_labels: dict[str, set[str]] = {}
    for r in _rows(REF / "hcc_v28_labels.csv"):
        groups_from_labels.setdefault(r["hierarchy_group"], set()).add(r["hcc_id"])

    for r in _rows(REF / "hcc_v28_hierarchy.csv"):
        higher = r["higher_hcc"]
        lower = r["trumps_lower_hcc"]
        # Both members of an explicit trump edge must be in the same hierarchy group.
        higher_group = next(
            (g for g, m in groups_from_labels.items() if higher in m), None
        )
        lower_group = next(
            (g for g, m in groups_from_labels.items() if lower in m), None
        )
        assert higher_group == lower_group, (
            f"Trump edge crosses groups: {higher} ({higher_group}) → {lower} ({lower_group})"
        )


def test_raf_weights_are_non_negative():
    for r in _rows(REF / "hcc_v28_labels.csv"):
        for col in (
            "raf_community_non_dual_aged",
            "raf_community_dual_aged",
            "raf_institutional",
        ):
            v = float(r[col])
            assert v >= 0.0, f"Negative RAF for {r['hcc_id']}.{col}: {v}"


@pytest.mark.parametrize(
    "hcc_id", ["HCC-17", "HCC-18", "HCC-19", "HCC-85", "HCC-134", "HCC-111"]
)
def test_core_hccs_present(hcc_id):
    labels = {r["hcc_id"] for r in _rows(REF / "hcc_v28_labels.csv")}
    assert hcc_id in labels