Models as a dataflow with Hamilton adapters: scenarios declared in model.toml, cohort read from the database or a file, a scikit-learn model registered with its requirements, estimates per horizon, and the CIN2+ prevalence-incidence scenario
This commit is contained in:
+26
-12
@@ -1,6 +1,6 @@
|
||||
"""What this module covers: storing an estimate file as a batch of risks (`models.steps.risks`,
|
||||
`models.io.storage.store_risk_batch`, `models.pipelines.store_risks`) and the `models store` command: one risk per
|
||||
member at the model's horizon, the batch's MLflow keys, no duplicate batch, and builds without a `cohorte_armado` row
|
||||
member and horizon, the batch's MLflow keys, no duplicate batch, and builds without a `cohorte_armado` row
|
||||
refused.
|
||||
|
||||
Fixtures: `cacu_db` (conftest), an empty CaCu schema on SQLite; `kit_build`, kit build 1 with members 1 to 3;
|
||||
@@ -20,13 +20,13 @@ from models.pipelines.store_risks import store_estimate
|
||||
from models.steps.risks import risk_rows
|
||||
|
||||
METADATA = {"modelo_nombre": "cacu-cin2_logistic", "modelo_version": "3", "mlflow_run_id": "run-a", "modelo_sha": "sha-a",
|
||||
"desenlace": "cin2_12m", "horizonte_anios": "1.0", "cohort.url": "cacu/cohorte_vigilancia",
|
||||
"desenlace": "cin2_12m", "cohort.url": "cacu/cohorte_vigilancia",
|
||||
"cohort.source": "kit", "cohort.build": "1", "cohort.file_sha256": "f" * 64}
|
||||
|
||||
|
||||
def _estimate(pac_ids=(1, 2, 3)):
|
||||
return pd.DataFrame({"pac_id": list(pac_ids), "fecha_indice": [date(2025, 1, i) for i in pac_ids],
|
||||
"probabilidad": [0.1 * i for i in pac_ids]})
|
||||
def _estimate(pac_ids=(1, 2, 3), horizons=(1.0,)):
|
||||
rows = [(i, date(2025, 1, i), h, round(0.1 * i + 0.01 * h, 2)) for i in pac_ids for h in horizons]
|
||||
return pd.DataFrame(rows, columns=["pac_id", "fecha_indice", "horizonte_anios", "probabilidad"])
|
||||
|
||||
|
||||
def _write(frame, path, **metadata):
|
||||
@@ -65,19 +65,21 @@ def _stored():
|
||||
return session.exec(select(EvaluacionRiesgoLote)).all(), session.exec(select(EvaluacionRiesgo)).all()
|
||||
|
||||
|
||||
def test_each_member_becomes_one_risk_at_the_horizon():
|
||||
"""Pins down: a risk is `(pac_id, horizonte_anios, probabilidad, None, None)`, one per member. Fails if the horizon is
|
||||
dropped or the interval is filled with something the model didn't give."""
|
||||
assert risk_rows(_estimate((1, 2)), 1.0) == [(1, 1.0, 0.1, None, None), (2, 1.0, 0.2, None, None)]
|
||||
def test_each_row_becomes_one_risk_at_its_horizon():
|
||||
"""Pins down: a risk is `(pac_id, horizonte_anios, probabilidad, None, None)`, one per member and horizon, the
|
||||
horizon taken from the row. Fails if the horizon is dropped, taken from elsewhere, or the interval is filled with
|
||||
something the model didn't give."""
|
||||
assert risk_rows(_estimate((1, 2), horizons=(0.0, 3.0))) == [
|
||||
(1, 0.0, 0.1, None, None), (1, 3.0, 0.13, None, None), (2, 0.0, 0.2, None, None), (2, 3.0, 0.23, None, None)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("frame", [_estimate((1, 1)), _estimate().assign(probabilidad=[0.1, 1.5, 0.2]),
|
||||
_estimate().rename(columns={"probabilidad": "p"})])
|
||||
def test_a_malformed_estimate_stops_the_batch(frame):
|
||||
"""Pins down: a member scored twice, a probability outside 0..1, or other columns stop the batch before anything is
|
||||
written. Fails if any of them reaches the database."""
|
||||
"""Pins down: a member scored twice at one horizon, a probability outside 0..1, or other columns stop the batch
|
||||
before anything is written. Fails if any of them reaches the database."""
|
||||
with pytest.raises(ValueError):
|
||||
risk_rows(frame, 1.0)
|
||||
risk_rows(frame)
|
||||
|
||||
|
||||
def test_storing_writes_one_batch_with_the_versions_keys(kit_build, estimate_file):
|
||||
@@ -92,6 +94,18 @@ def test_storing_writes_one_batch_with_the_versions_keys(kit_build, estimate_fil
|
||||
(1, 1.0, "preliminary", 1), (2, 1.0, "preliminary", 1), (3, 1.0, "preliminary", 1)]
|
||||
|
||||
|
||||
def test_several_horizons_are_stored_in_one_batch(kit_build, tmp_path):
|
||||
"""Pins down: an estimate with the prevalence and later horizons is one batch with a risk per member and horizon.
|
||||
Fails if storing assumes one horizon per batch."""
|
||||
path = _write(_estimate(horizons=(0.0, 1.0, 5.0)), tmp_path / "horizons.parquet")
|
||||
|
||||
store_estimate(path, "cacu")
|
||||
|
||||
batches, risks = _stored()
|
||||
assert len(batches) == 1 and len(risks) == 9
|
||||
assert sorted({r.horizonte_anios for r in risks}) == [0.0, 1.0, 5.0]
|
||||
|
||||
|
||||
def test_storing_the_same_estimate_again_adds_nothing(kit_build, estimate_file, capsys):
|
||||
"""Pins down: the command is idempotent: the second time it reports the existing batch. Fails if Persistance's
|
||||
content check stops matching what this module sends."""
|
||||
|
||||
Reference in New Issue
Block a user