e8c264e571
Every CaCu PIM scenario (cin2_pim, post-colposcopy, post-treatment) and the survival scenario now keep, per
combination of their covariates, the model against its empirical estimate, with the methods each used:
- PIM (models/cacu/cin2_pim/report.py): Turnbull's interval-censored estimate (prevalent = mass at 0, free in (l, r],
unknown baseline in [0, r]; ported from bayes-pim chapter 24) with a bootstrap band, against the mixture refitted on
every coded member with a delta-method band over the likelihood (inverse numerical Hessian, numerical gradient of
CR(t), log scale); E/O at each horizon with log(E/O) taken as normal.
- Survival (models/cacu/survival/report.py): Kaplan-Meier with Greenwood's band against the SEER pipeline's own
subgroup points and bootstrap interval (survival_curves.sv_sub_weights and sv_sub_points, called as they are).
A version keeps the record as analisis/subgrupos.json and a self-contained Plotly page as reportes/subgrupos.html
(TrainingRun.log_report; the saver publishes a dataflow's `reports`). The same drawing is a notebook in the code,
models/cacu/{cin2_pim,survival}/notebooks/reporte_subgrupos.ipynb: chart names and computed results only, a
`parameters` cell, `#| echo: false` cells. models.reports.templates lists those notebooks with the scenarios each serves
and reads one by id, for the WebApi's /governance/notebook-templates. The Jupyter renderer now drops the code of
`#| echo: false` cells, as Quarto does.
plotly>=5.18 joins the `survival-curves` extra (installed by the WebApi) and the dev group.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
88 lines
4.5 KiB
Python
88 lines
4.5 KiB
Python
"""The population results of a training run, what the dashboard draws: the survival of the whole cohort and the model's
|
|
survival at each horizon, as CaMa's survival scenario publishes them (`models.cama.survival.population`).
|
|
|
|
Over every member of the indexed cohort: the Kaplan-Meier curve with its Greenwood interval, the covariate-adjusted
|
|
curve, the classic and the fractional Weibull fitted to it, and the mean survival at each horizon by the logistic
|
|
regression, the Bayesian network and their stacked combination, each with a 95 % bootstrap interval. The record is the
|
|
version's `analisis/curvas.json`; its labels come from `[config.etiquetas]`.
|
|
|
|
Every node but `survival_frame` is CaMa's, called from here (Hamilton takes only the functions defined in a listed
|
|
module). `survival_frame` is a copy over this scenario's `FEATURES` (DUPLICATED, issue #9).
|
|
"""
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
|
|
from models.cacu.survival.estimator import FEATURES
|
|
from models.cama.seer_survival import survival_curves as luis
|
|
from models.cama.seer_survival import survival_models as sm
|
|
from models.cama.seer_survival.settings import SurvivalSettings
|
|
from models.cama.survival import population as cama
|
|
from models.cama.survival.estimator import UNKNOWN
|
|
|
|
|
|
def survival_frame(labelled_members: pd.DataFrame, survival_settings: SurvivalSettings) -> luis.SurvivalFrame:
|
|
"""The members as the pipeline's stage 5 takes its cohort: follow-up truncated at `cap_months` (a later death counts
|
|
as alive) and the covariates as categories (DUPLICATED, issue #9)."""
|
|
s, lm = survival_settings, labelled_members
|
|
df = pd.DataFrame({"meses": lm["months"].astype(float).to_numpy(), "evento": lm["event"].to_numpy(bool)})
|
|
df["meses_int"] = df["meses"].astype(int)
|
|
if s.cap_months:
|
|
df["evento"] = df["evento"] & (df["meses_int"] < s.cap_months)
|
|
df["meses_int"] = np.minimum(df["meses_int"], s.cap_months)
|
|
for name in FEATURES:
|
|
df[name] = lm[name].fillna(UNKNOWN).astype(str).str.strip().to_numpy()
|
|
return luis.SurvivalFrame(df, list(FEATURES), None, [])
|
|
|
|
|
|
def curve_cohort(survival_frame: luis.SurvivalFrame, survival_settings: SurvivalSettings) -> sm.Cohort:
|
|
return cama.curve_cohort(survival_frame, survival_settings)
|
|
|
|
|
|
def km_raw(survival_frame: luis.SurvivalFrame, survival_settings: SurvivalSettings) -> luis.KaplanMeier:
|
|
return cama.km_raw(survival_frame, survival_settings)
|
|
|
|
|
|
def km_curve(km_raw: luis.KaplanMeier) -> dict:
|
|
return cama.km_curve(km_raw)
|
|
|
|
|
|
def adjusted_km(curve_cohort: sm.Cohort, km_raw: luis.KaplanMeier, survival_settings: SurvivalSettings) -> np.ndarray:
|
|
return cama.adjusted_km(curve_cohort, km_raw, survival_settings)
|
|
|
|
|
|
def population_weights(curve_cohort: sm.Cohort, survival_settings: SurvivalSettings) -> pd.DataFrame:
|
|
return cama.population_weights(curve_cohort, survival_settings)
|
|
|
|
|
|
def luis_points(curve_cohort: sm.Cohort, population_weights: pd.DataFrame, survival_settings: SurvivalSettings) -> luis.Points:
|
|
return cama.luis_points(curve_cohort, population_weights, survival_settings)
|
|
|
|
|
|
def point_estimates(luis_points: luis.Points) -> dict:
|
|
return cama.point_estimates(luis_points)
|
|
|
|
|
|
def bootstrap_points(curve_cohort: sm.Cohort, population_weights: pd.DataFrame, luis_points: luis.Points,
|
|
survival_settings: SurvivalSettings) -> np.ndarray:
|
|
return cama.bootstrap_points(curve_cohort, population_weights, luis_points, survival_settings)
|
|
|
|
|
|
def weibull_fits(adjusted_km: np.ndarray, survival_frame: luis.SurvivalFrame, survival_settings: SurvivalSettings) -> dict:
|
|
return cama.weibull_fits(adjusted_km, survival_frame, survival_settings)
|
|
|
|
|
|
def population_curves(labelled_members: pd.DataFrame, km_curve: dict, adjusted_km: np.ndarray, weibull_fits: dict,
|
|
point_estimates: dict, bootstrap_points: np.ndarray, population_weights: pd.DataFrame,
|
|
survival_settings: SurvivalSettings, etiquetas: dict) -> dict:
|
|
"""CaMa's: the `curvas` record the dashboard draws."""
|
|
return cama.population_curves(labelled_members, km_curve, adjusted_km, weibull_fits, point_estimates,
|
|
bootstrap_points, population_weights, survival_settings, etiquetas)
|
|
|
|
|
|
def population(population_curves: dict, subgroups: dict) -> dict:
|
|
"""What the saver publishes under `analisis/`, by file name: the curves, and each combination of the covariates'
|
|
Kaplan-Meier and model survival with the methods used (`report.subgroups`). CaMa adds its SEER development stages
|
|
instead, which need covariates CaCu doesn't have."""
|
|
return {**population_curves, "subgrupos": subgroups}
|