Spaces:
Sleeping
Sleeping
Download tests/test_reference_data.py from charlesyapai/hcc-meat-coder: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/charlesyapai/hcc-meat-coder/resolve/main/tests/test_reference_data.py
- Command line
-
hf download hf://spaces/charlesyapai/hcc-meat-coder/tests/test_reference_data.py
-
curl -L -o test_reference_data.py https://huggingface.co/spaces/charlesyapai/hcc-meat-coder/resolve/main/tests/test_reference_data.py
2.83 kB
| """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}" | |
| 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 | |