Files
models/tests/cacu/test_cancer_pim.py
edgar 28858c6966 CaCu: the surveillance PIM with cancer as the outcome (cancer_pim)
The same dataflow, cohort, index and prediction index as the CIN2+ surveillance model, with carcinoma as the only
event, like the cancer column of KPNC's tables: the patient's two risks are of the same visit. The subgroup report
template serves it too.
2026-11-22 09:34:35 -06:00

55 lines
3.0 KiB
Python

"""What this module covers: the `cancer_pim` scenario, the surveillance PIM with carcinoma as the outcome, through its
dataflow and its registered model.
Fixtures: `cohort_frame`, members of `test_cin2_pim`'s design whose CIN2+ biopsies are carcinomas for a part of them.
"""
import numpy as np
import pandas as pd
import pytest
from models.cacu.cin2_pim import model
from models.dags import training
from tests.cacu.test_cin2_pim import cohort_frame as cin2_cohort_frame # noqa: F401
SCENARIO = training.Scenario.load("cacu", "cancer_pim")
SURVEILLANCE = training.Scenario.load("cacu", "cin2_pim")
@pytest.fixture
def cohort_frame(cin2_cohort_frame): # noqa: F811
"""`test_cin2_pim`'s members with every third LIEAG biopsy reported as carcinoma."""
frame = cin2_cohort_frame.copy()
lieag = frame.index[(frame["tipo"] == "biopsia") & (frame["valor_codigo"] == "LIEAG")]
frame.loc[lieag[::3], "valor_codigo"] = "CARCINOMA"
return frame
def test_the_scenario_is_the_surveillance_model_with_carcinoma_as_the_outcome():
"""Pins down: same dataflow, cohort, index and prediction index as the CIN2+ surveillance model, with carcinoma as
the only event, so the patient's two risks speak of the same visit. Fails if the scenarios drift apart."""
assert SCENARIO.config["event_grades"] == ["CARCINOMA"] == SCENARIO.config["index"]["event_grades"]
assert SCENARIO.outcome == "cancer" and SCENARIO.cohort == SURVEILLANCE.cohort
assert [m.__name__ for m in SCENARIO.modules] == [m.__name__ for m in SURVEILLANCE.modules]
same = {k: v for k, v in SCENARIO.config.items() if k not in ("event_grades", "index")}
assert same == {k: v for k, v in SURVEILLANCE.config.items() if k not in ("event_grades", "index")}
assert SCENARIO.estimand["outcome"].startswith("Cáncer")
def test_the_dataflow_counts_only_carcinomas_as_events(cohort_frame):
"""Pins down: a LIEAG biopsy is not a case for this scenario, and the registered model gives a cancer risk no
higher than the CIN2+ model's at the same visit. Fails if `event_grades` stops reaching the coding."""
run = training.dataflow(*SCENARIO.modules, config=SCENARIO.config).execute(
["interval_coded_members", "fitted_model"], inputs={"source_cohort": cohort_frame, "cohort_lineage": {}})
coded = run["interval_coded_members"]
carcinomas = set(cohort_frame.loc[cohort_frame["valor_codigo"] == "CARCINOMA", "pac_id"])
cases = set(coded.loc[(coded["c"] == 1) | np.isfinite(coded["r"]), "pac_id"])
assert cases == carcinomas
cin2 = training.dataflow(*SURVEILLANCE.modules, config=SURVEILLANCE.config).execute(
["fitted_model"], inputs={"source_cohort": cohort_frame, "cohort_lineage": {}})["fitted_model"]
h = len(SCENARIO.config["horizons_years"])
cancer, cin2plus = (m.predict_proba(cohort_frame)[:, 1:1 + h] for m in (run["fitted_model"], cin2))
scored = ~np.isnan(cancer) & ~np.isnan(cin2plus)
assert scored.any() and (cancer[scored] <= cin2plus[scored] + 1e-6).all()