CaMa survival: the figure for a selection of members, scored with the production model over the live cohort; the pipeline's chart wording

This commit is contained in:
rockdrigo
2026-11-21 12:07:21 -06:00
parent ec430c3ee3
commit b8936522be
4 changed files with 192 additions and 10 deletions
+65
View File
@@ -413,3 +413,68 @@ def test_the_production_model_predicts_what_the_pipelines_points_say(members_fra
theirs = _stage_five_table(tmp_path / "depurado.csv", luis)
assert mean_combined == pytest.approx(theirs[theirs["modelo"] == "COMB"]["S"].tolist(), abs=1e-6)
@pytest.fixture(scope="module")
def production_model(members_frame):
"""A model fitted on the 240 members: what a version in production is when the dashboard scores a selection."""
return IntervalSurvival(CONFIG["survival"], tuple(h / 12 for h in CONFIG["survival"]["horizons"])).fit(
members_frame[FEATURES], members_frame[["months", "event"]])
def test_the_live_figure_scores_the_selected_members_with_the_production_model(members_frame, production_model):
"""Pins down: the record is for the selected members, its points are the model's mean survival for them at the model's
horizons with an interval around it, and nothing is refitted (the same selection twice gives the same record). Fails if
a filter is ignored, or the selection changes the model's horizons."""
from models.cama.survival import live
everyone = live.curves(SCENARIO, production_model, members_frame, {})
grade3 = live.curves(SCENARIO, production_model, members_frame, {"filtros": {"grade": ["G3"]}})
again = live.curves(SCENARIO, production_model, members_frame, {"filtros": {"grade": ["G3"]}})
assert everyone["n"] == len(members_frame) and everyone["horizons_months"] == [6, 12, 18]
assert 0 < grade3["n"] < everyone["n"] and grade3["points"] == again["points"]
comb = grade3["points"]["COMB"]
assert all(lo <= s <= hi for lo, s, hi in zip(comb["low"], comb["S"], comb["high"]))
assert comb["S"][0] < everyone["points"]["COMB"]["S"][0] # grade 3 does worse than everyone
assert len(grade3["km_adjusted"]["S"]) == grade3["months_max"] + 1 and len(grade3["weibull_fractional"]["S"]) == len(grade3["weibull"]["S"])
def test_the_live_options_list_the_values_the_limits_and_the_patients_own(members_frame):
"""Pins down: the options carry every covariate's values with their member counts, the period of the index dates, the
smallest selection that can be answered and, for a patient, her value of each covariate. Fails if a covariate is missing
or her values are not hers."""
from models.cama.survival import live
first = int(members_frame["pac_id"].iloc[0])
found = live.options(members_frame, first)
assert set(found["covariables"]) == set(FEATURES)
assert sum(v["n"] for v in found["covariables"]["grade"]) == len(members_frame)
assert found["paciente"]["grade"] == str(members_frame["grade"].iloc[0])
assert live.options(members_frame, 10**9)["paciente"] is None
@pytest.mark.parametrize("query", [
{"filtros": {"nope": ["x"]}},
{"filtros": {"grade": ["G1"], "er": ["NEGATIVO"], "her2": ["H3"]}},
{"periodo": {"desde": "no es fecha"}},
])
def test_a_selection_that_cannot_be_answered_says_why(members_frame, production_model, query):
"""Pins down: an unknown filter, a selection too small to say anything and an invalid date are each a `QueryError` with
a message. Fails if any of them runs and draws a meaningless figure or fails with an unreadable error."""
from models.cama.survival import live
with pytest.raises(live.QueryError):
live.curves(SCENARIO, production_model, members_frame, query)
def test_the_monthly_curve_passes_through_the_horizons_and_never_rises():
"""Pins down: the month-by-month survival equals the model's at each horizon, starts at 1, never rises, and goes on past
the last horizon at the last interval's rate. Fails if the interpolation misses a horizon or the curve can go up."""
from models.cama.survival import live
curve = live.monthly(np.array([[0.9, 0.8, 0.6]]), [6, 12, 18], 24)[0]
assert curve[0] == 1.0 and curve[6] == pytest.approx(0.9) and curve[12] == pytest.approx(0.8) and curve[18] == pytest.approx(0.6)
assert (np.diff(curve) <= 1e-12).all() and curve[24] < curve[18]