acc8c371bc
models/cama/seer_survival is the SEER survival pipeline as Hamilton dataflows, with the interval LR and network keeping their per-interval fits and surviving an interval without deaths. models/cama/survival is the scenario: it indexes the total cohort at the first invasive biopsy report (steps/indexing.py), derives the covariates and the outcome from the cohort's events, fits the interval survival, writes the five routines and publishes the population curves (analisis/curvas.json) with the version. An estimate may carry the model's own index date.
27 lines
1.2 KiB
Python
27 lines
1.2 KiB
Python
"""Stage 03, bootstrapping: the row indices of each resample of the balanced frame, fixed once so every model stage
|
|
resamples the same rows. Hamilton nodes; the inputs are `balanced_csv` and `bootstrap` (its settings)."""
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
|
|
from models.cama.seer_survival.settings import BootstrapSettings
|
|
|
|
|
|
def balanced_rows(balanced_csv: str) -> int:
|
|
"""The balanced frame's number of rows (only the first column is read)."""
|
|
return len(pd.read_csv(balanced_csv, dtype=str, usecols=[0]))
|
|
|
|
|
|
def bootstrap_indices(balanced_rows: int, bootstrap: BootstrapSettings) -> np.ndarray:
|
|
"""`(n_bootstrap, rows)` indices drawn with replacement, reproducible from `random_state`."""
|
|
rng = np.random.default_rng(bootstrap.random_state)
|
|
out = np.empty((bootstrap.n_bootstrap, balanced_rows), dtype=np.int64)
|
|
for b in range(bootstrap.n_bootstrap):
|
|
out[b] = rng.integers(0, balanced_rows, size=balanced_rows)
|
|
return out
|
|
|
|
|
|
def bootstrap_metadata(balanced_csv: str, balanced_rows: int, bootstrap: BootstrapSettings) -> dict:
|
|
return {"source_csv": balanced_csv, "n_filas": int(balanced_rows), "n_bootstrap": int(bootstrap.n_bootstrap),
|
|
"random_state": int(bootstrap.random_state)}
|