From da14e4ea8e9fb49534eb6c6eb64306d69bc02dcb Mon Sep 17 00:00:00 2001 From: edgar Date: Wed, 2 Dec 2026 23:20:50 -0600 Subject: [PATCH] =?UTF-8?q?Publicar=20Kaplan-Meier=20por=20combinaci=C3=B3?= =?UTF-8?q?n=20de=20valores=20en=20supervivencia=20CaCu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/survival.py | 27 +++++++++++++++++++++++++++ tests/test_cacu_models.py | 5 +++++ 2 files changed, 32 insertions(+) diff --git a/models/survival.py b/models/survival.py index 560a23c..e21478a 100644 --- a/models/survival.py +++ b/models/survival.py @@ -435,6 +435,32 @@ def _scenario_curves(months: np.ndarray, event: np.ndarray, cap_months: int) -> } # fmt: skip +def _combination_curves(spec: Spec, members: pd.DataFrame) -> list[dict]: + """Kaplan-Meier of the members who share every covariate value: the one curve a patient with + those values is shown. Fewer than MIN_SCENARIO_EVENTS deaths: counts and why, no curve.""" + from models import curves + + months, event = members["months"].to_numpy(), members["event"].to_numpy(bool) + keys = spec._combinations(members) + found = [] + for values, rows in keys.groupby(list(spec.features), sort=True).indices.items(): + values = values if isinstance(values, tuple) else (values,) + entry = {"valores": dict(zip(spec.features, values, strict=True)), "n": len(rows), + "fallecidos": int(event[rows].sum())} # fmt: skip + if entry["fallecidos"] < MIN_SCENARIO_EVENTS: + entry["reason"] = "insufficient_deaths" + else: + until = int(months[rows].max()) + km = curves.kaplan_meier(months[rows], event[rows], until) + entry["km"] = { + "months": list(range(until + 1)), + **{n: v.tolist() for n, v in zip(("S", "low", "high"), km, strict=True)}, + "interval": {"available": True, "level": 0.95, "method": "greenwood_log_log"}, + } + found.append(entry) + return found + + def interpretability(spec: Spec, model: IntervalSurvival, data: dict) -> dict: """The training members' observed survival (Kaplan-Meier with its Greenwood band, and a Weibull fit when anyone died), the model's mean survival at each horizon with a bootstrap @@ -532,6 +558,7 @@ def interpretability(spec: Spec, model: IntervalSurvival, data: dict) -> dict: "available": True, "level": 0.95, "method": "bootstrap_percentile"} for scenario in report["escenarios_sin_curvas"]: scenario["reason"] = "insufficient_deaths" + report["km_combinaciones"] = _combination_curves(spec, members) return report diff --git a/tests/test_cacu_models.py b/tests/test_cacu_models.py index efadd3c..af73ed4 100644 --- a/tests/test_cacu_models.py +++ b/tests/test_cacu_models.py @@ -125,6 +125,11 @@ def test_survival_matches_the_current_model_and_publishes_patient_scenarios(monk monkeypatch.setattr(curves, "fit_fractional", lambda d, c, k, lam: (1.0, 1.0, lam)) with parallel_config(backend="sequential"): report = reports.interpretability(spec, model, data) + combined = report["km_combinaciones"] + assert sum(c["n"] for c in combined) == len(pd.concat([data["train"], data["test"]])) + for c in combined: + assert set(c["valores"]) == set(spec.features) + assert ("km" in c) == (c["fallecidos"] >= survival.MIN_SCENARIO_EVENTS) (scenario,) = report["escenarios"] name = scenario["escenario"] assert name == "Citología · HSIL+"