CaMa survival: the published figure runs the SEER pipeline's stage-5 functions, and the production model keeps its structure settings
This commit is contained in:
@@ -292,7 +292,7 @@ def test_a_version_publishes_its_population_record(cohort_file, mlflow_tracking)
|
||||
def development_record(cohort_frame):
|
||||
"""The `desarrollo` record the dataflow publishes for the 240 indexed members, with the smallest budget."""
|
||||
config = {**CONFIG, "desarrollo": {**CONFIG["desarrollo"], "null_perm": 5, "n_bootstrap": 4, "bootstrap_max_seconds": 30,
|
||||
"proposals_max_seconds": 20, "restarts": 1}}
|
||||
"proposals_max_seconds": 20, "restarts": 1, "n_splits_proposals": 2, "bootstrap_restarts": 2}}
|
||||
dr = dataflow(*SCENARIO.modules, config=config)
|
||||
return dr.execute(["development"], inputs={"source_cohort": cohort_frame, "cohort_lineage": LINEAGE})["development"]["desarrollo"]
|
||||
|
||||
@@ -326,7 +326,7 @@ def test_the_development_stages_publish_their_tables_and_figures_on_a_varied_coh
|
||||
"ki67_group": rng.choice(["<14", "14-19", "≥20"], n)})
|
||||
members["months"] = np.where(members["event"], np.minimum(members["months"], 23), 24)
|
||||
settings = {**CONFIG["desarrollo"], "null_perm": 5, "n_bootstrap": 4, "bootstrap_max_seconds": 30,
|
||||
"proposals_max_seconds": 20, "restarts": 1}
|
||||
"proposals_max_seconds": 20, "restarts": 1, "n_splits_proposals": 2, "bootstrap_restarts": 2}
|
||||
|
||||
r = dev.development(members, settings)["desarrollo"]
|
||||
|
||||
@@ -335,3 +335,81 @@ def test_the_development_stages_publish_their_tables_and_figures_on_a_varied_coh
|
||||
arcs = r["tablas"]["hbn_arcos"]
|
||||
assert arcs["columnas"] == list(arcs["filas"][0]) and len(arcs["columnas"]) > 1
|
||||
assert r["figuras"] and all(isinstance(v, str) for v in r["figuras"].values())
|
||||
|
||||
|
||||
def _stage_five_table(csv_path, survival):
|
||||
"""The table of the SEER pipeline's stage 5 for the main curve (`sv_table`): its own nodes over a CSV of the cohort."""
|
||||
from hamilton import driver
|
||||
|
||||
from models.cama.seer_survival import survival_curves
|
||||
|
||||
dr = driver.Builder().with_modules(survival_curves).build()
|
||||
return dr.execute(["sv_table"], inputs={"depurated_csv": str(csv_path), "survival": survival})["sv_table"]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def members_frame(cohort_frame):
|
||||
"""The 240 indexed members, labelled (covariates, follow-up, death), as the figure's nodes take them."""
|
||||
return dataflow(*SCENARIO.modules, config=SCENARIO.config).execute(
|
||||
["labelled_members"], inputs={"source_cohort": cohort_frame, "cohort_lineage": LINEAGE})["labelled_members"]
|
||||
|
||||
|
||||
def test_the_published_figure_is_the_seer_pipelines_stage_five_on_the_same_members(members_frame, tmp_path):
|
||||
"""Pins down: the Kaplan-Meier curve, the adjusted curve, the stacking weights, the points of LR, HBN and their
|
||||
combination and their bootstrap interval in the published record are those that `models.cama.seer_survival`'s own stage 5
|
||||
(`run_survival`, on a CSV of the same members and the same settings) gives. Fails if the record's code path stops being
|
||||
the pipeline's, which would make the dashboard's figure another model's."""
|
||||
import dataclasses
|
||||
|
||||
from models.cama.survival import estimator
|
||||
|
||||
survival = {**CONFIG["survival"], "n_boot": 6, "ks_max_seconds": 5}
|
||||
config = {**CONFIG, "survival": survival}
|
||||
ours = dataflow(*SCENARIO.modules, config=config).execute(
|
||||
["population_curves"], inputs={"source_cohort": pd.DataFrame(), "cohort_lineage": {}},
|
||||
overrides={"labelled_members": members_frame})["population_curves"]["curvas"]
|
||||
|
||||
csv = pd.DataFrame({"Survival months": members_frame["months"].astype(int).astype(str),
|
||||
"Vital status recode (study cutoff used)": members_frame["event"].map({True: "Dead", False: "Alive"})})
|
||||
for name in estimator.FEATURES:
|
||||
csv[f"{name}_cat"] = members_frame[name].fillna("NA").astype(str)
|
||||
csv.to_csv(tmp_path / "depurado.csv", index=False)
|
||||
fixed = tuple(f"{n}_cat" for n in survival["hbn_fixed_parents"])
|
||||
luis = dataclasses.replace(estimator.survival_settings(survival), hbn_fixed_parents=fixed)
|
||||
theirs = _stage_five_table(tmp_path / "depurado.csv", luis)
|
||||
|
||||
for model in ("LR", "HBN", "COMB"):
|
||||
rows = theirs[theirs["modelo"] == model]
|
||||
assert ours["points"][model]["S"] == pytest.approx(rows["S"].tolist(), abs=1e-9)
|
||||
assert ours["points"][model]["low"] == pytest.approx(rows["IC_inf"].tolist(), abs=1e-9)
|
||||
assert ours["points"][model]["high"] == pytest.approx(rows["IC_sup"].tolist(), abs=1e-9)
|
||||
km = theirs[theirs["modelo"] == "COMB"]
|
||||
assert [ours["km"]["S"][m] for m in (6, 12, 18)] == pytest.approx(km["KM_crudo"].tolist(), abs=1e-9)
|
||||
assert [ours["km_adjusted"]["S"][m] for m in (6, 12, 18)] == pytest.approx(km["KM_ajustada"].tolist(), abs=1e-9)
|
||||
|
||||
|
||||
def test_the_production_model_predicts_what_the_pipelines_points_say(members_frame, tmp_path):
|
||||
"""Pins down: the `IntervalSurvival` a version in production is (the model that scores each patient) gives, averaged over
|
||||
the members it was fitted on, the survival at each horizon that the pipeline's stage 5 gives for LR, HBN and their
|
||||
combination, exactly (the same data, the same settings). Fails if the model that scores patients drifts from the model whose
|
||||
figure is published."""
|
||||
import dataclasses
|
||||
|
||||
from models.cama.survival import estimator
|
||||
from models.cama.survival.estimator import IntervalSurvival
|
||||
|
||||
survival = {**CONFIG["survival"], "n_boot": 0}
|
||||
model = IntervalSurvival(survival, tuple(h / 12 for h in survival["horizons"])).fit(
|
||||
members_frame[estimator.FEATURES], members_frame[["months", "event"]])
|
||||
mean_combined = model.survival_proba(members_frame[estimator.FEATURES]).mean(axis=0)
|
||||
|
||||
csv = pd.DataFrame({"Survival months": members_frame["months"].astype(int).astype(str),
|
||||
"Vital status recode (study cutoff used)": members_frame["event"].map({True: "Dead", False: "Alive"})})
|
||||
for name in estimator.FEATURES:
|
||||
csv[f"{name}_cat"] = members_frame[name].fillna("NA").astype(str)
|
||||
csv.to_csv(tmp_path / "depurado.csv", index=False)
|
||||
fixed = tuple(f"{n}_cat" for n in survival["hbn_fixed_parents"])
|
||||
luis = dataclasses.replace(estimator.survival_settings(survival), hbn_fixed_parents=fixed)
|
||||
theirs = _stage_five_table(tmp_path / "depurado.csv", luis)
|
||||
|
||||
assert mean_combined == pytest.approx(theirs[theirs["modelo"] == "COMB"]["S"].tolist(), abs=1e-6)
|
||||
|
||||
Reference in New Issue
Block a user