9cba11c2ec
A new scenario, models/cacu/survival, from the total cohort. PROVISIONAL index: the first biopsy reported as invasive carcinoma (`primera_biopsia_invasora` with `CARCINOMA`), both the inclusion criterion and the index date; out without a birth date or with a death on or before it. The covariates are the most recent observations up to the diagnosis: the age group at it, the latest cytology sample in KPNC's categories and the latest HPV result. The outcome is the timeline's `defuncion` (emitted by the monorepo's CaCu timeline on its own branch). The fit is the SEER pipeline's interval survival as cama/survival runs it (LR and Bayesian network per interval, stacked), and a version publishes the same population record, `analisis/curvas.json` (KM with Greenwood, adjusted KM, Weibull, LR/HBN/combined points with bootstrap intervals): the survival panel a collaborator's proof of concept asked for, without its synthetic covariates or its fixed 50:50 mix. CaMa's code is not changed. The nodes that don't depend on the covariates call CaMa's; encode, interval_cohort, IntervalSurvival, labelled_members, missing_data, analytic_method and survival_frame are copies over CaCu's covariates, because CaMa's read its FEATURES as a module constant. Issue #9 records the duplication and how to remove it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
87 lines
4.4 KiB
Python
87 lines
4.4 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) -> dict:
|
|
"""What the saver publishes under `analisis/`: the curves only (CaMa adds its SEER development stages, which need
|
|
covariates CaCu doesn't have)."""
|
|
return population_curves
|