A training run takes every model; the public cohorts' code lives with the tests

This commit is contained in:
Roberto Treviño
2026-11-29 21:12:17 -06:00
parent 3a749ba8d1
commit a9945bc021
12 changed files with 225 additions and 203 deletions
+11 -11
View File
@@ -25,6 +25,7 @@ from pathlib import Path
import numpy as np
import pandas as pd
import pytest
import recurrence_reference as reference
from joblib.externals.loky import get_reusable_executor
from scipy.special import gammaln
@@ -71,14 +72,14 @@ def raws(tmp_path_factory) -> dict[str, pd.DataFrame]:
# Each cohort's rows as the pipeline loaded them (one source node per cohort).
out = folder / "datasets.pkl"
platform.run("python", "-c", "import pandas as pd; from persistance import nodes; "
"from models.cama import recurrence; pd.to_pickle({s: pd.DataFrame(nodes.rows("
f"'cama_reference', s)) for s in recurrence.SOURCES}}, {str(out)!r})") # fmt: skip
f"pd.to_pickle({{s: pd.DataFrame(nodes.rows('cama_reference', s)) "
f"for s in {reference.SOURCES!r}}}, {str(out)!r})") # fmt: skip
return pd.read_pickle(out)
@pytest.fixture(scope="module")
def prepared(raws):
return {s: cohorts.prepare(s, raw) for s, raw in raws.items()}
return {s: reference.prepare(s, raw) for s, raw in raws.items()}
@pytest.fixture(scope="module")
@@ -155,7 +156,7 @@ def _check_figures(records: list[dict], fractional_tolerance: float):
# ---------------------------------------------------------------- fast
def test_cohorts(prepared, raws):
def test_cohorts(prepared):
sampling = _json("rutinas/MUESTREO.json")["cohortes"]
missing = _json("rutinas/DATOS_FALTANTES.json")["desconocidos_por_covariable"]
for source, c in prepared.items():
@@ -164,18 +165,17 @@ def test_cohorts(prepared, raws):
assert c.eliminated == sampling[source]["eliminadas_por_colinealidad"]
assert c.covariates == list(missing[source])
assert cohorts.unknown_shares(c) == missing[source]
pd.testing.assert_frame_equal(recurrence.cohort(source, raws[source]), c.frame)
assert prepared["metabric"].covariates == VERSION["model"]["covariates"]
def test_routines(prepared):
found = reports.routines(prepared, modeling.N_BOOTSTRAP)
found = reports.routines(prepared, reference.N_BOOTSTRAP)
for name, record in found.items():
assert record == _json(f"rutinas/{name}.json"), name
def test_c_index_and_internal_auc(fits):
rows = [r for s in recurrence.SOURCES for r in reports.auc_rows(fits[s])]
rows = [r for s in reference.SOURCES for r in reports.auc_rows(fits[s])]
assert _auc_rows(rows, False) == _auc_rows(DEVELOPMENT["auc"]["filas"], False)
metrics = reports.metrics(rows)
assert metrics == {k: v for k, v in VERSION["metrics"].items() if "bootstrap" not in k}
@@ -206,7 +206,7 @@ def test_saved_model(fits):
def test_calculator(fits):
"""All 60 table rows, and the 20 figures but their fractional Weibull's tolerance."""
records, rows = [], []
for source in recurrence.SOURCES:
for source in reference.SOURCES:
found = calculator.calculator(fits[source])
records += found[0]
rows += found[1]
@@ -220,12 +220,12 @@ def test_calculator(fits):
def test_risk_factors(fits):
"""All 273 rows, with v1's bootstrap selection shares (the slow test computes them)."""
table = pd.concat(
[factors.risk_factors(fits[s], _reference_selection(s)) for s in recurrence.SOURCES],
[factors.risk_factors(fits[s], _reference_selection(s)) for s in reference.SOURCES],
ignore_index=True,
)
_same_rows(reports.table(table)["filas"], DEVELOPMENT["factores_riesgo"]["filas"],
FACTOR_TOLERANCE) # fmt: skip
summary = [r for s in recurrence.SOURCES for r in factors.scenario_summary(fits[s], table)]
summary = [r for s in reference.SOURCES for r in factors.scenario_summary(fits[s], table)]
assert reports.table(summary) == _stages(DEVELOPMENT["escenarios_resumen"])
@@ -262,7 +262,7 @@ def test_fractional_weibull_solver_matches_the_series():
@pytest.mark.slow
@slow
def test_train_reproduces_v1(raws):
model, metrics, found = recurrence.train(raws, bootstrap=True)
model, metrics, found = reference.train(raws, bootstrap=True)
assert metrics == VERSION["metrics"]
for h in recurrence.HORIZONS:
mine = model.tables[h]