diff --git a/models/io/contracts.py b/models/io/contracts.py index d9b4df1..c376f23 100644 --- a/models/io/contracts.py +++ b/models/io/contracts.py @@ -110,7 +110,8 @@ class Risk(NamedTuple): """A member's risk at one horizon (FHIR `RiskAssessment.prediction`).""" pac_id: int - horizonte_anios: float # years from the index date; 0 = prevalent + fecha_indice: date # the model's index date for this member: the horizons count from it + horizonte_anios: float # years from `fecha_indice`; 0 = prevalent probabilidad: float # 0 to 1 ic_bajo: float | None = None ic_alto: float | None = None diff --git a/models/steps/risks.py b/models/steps/risks.py index 91cf537..a0ef30c 100644 --- a/models/steps/risks.py +++ b/models/steps/risks.py @@ -1,4 +1,5 @@ -"""From an estimate's rows to the risks a batch stores: `(pac_id, horizonte_anios, probabilidad, ic_bajo, ic_alto)`.""" +"""From an estimate's rows to the risks a batch stores: `(pac_id, fecha_indice, horizonte_anios, probabilidad, ic_bajo, +ic_alto)`; `fecha_indice` is the model's index date for the member, which its horizons count from.""" import pandas as pd @@ -9,6 +10,6 @@ def risk_rows(estimate: pd.DataFrame) -> list[tuple]: """One risk per member and horizon, without an interval, once the rows satisfy `ESTIMATE_FRAME`: a member scored twice at one horizon or a probability outside 0..1 stops the batch.""" check_frame(ESTIMATE_FRAME, estimate, EstimateError, "the estimate") - return [(int(pac_id), float(horizon), float(probability), None, None) - for pac_id, horizon, probability in zip(estimate["pac_id"], estimate["horizonte_anios"], - estimate["probabilidad"])] + return [(int(pac_id), pd.Timestamp(fecha).date(), float(horizon), float(probability), None, None) + for pac_id, fecha, horizon, probability in zip(estimate["pac_id"], estimate["fecha_indice"], + estimate["horizonte_anios"], estimate["probabilidad"])] diff --git a/models/testing.py b/models/testing.py index c6b2846..2615925 100644 --- a/models/testing.py +++ b/models/testing.py @@ -39,7 +39,7 @@ class CacuStandIn: missing = [c for c in ("pac_id", "pap_0", "hpv_0") if c not in columns] if missing: raise ValueError(f"The frame lacks the columns {missing}: the stand-in needs them.") - i_pac, i_pap, i_hpv = (columns.index(c) for c in ("pac_id", "pap_0", "hpv_0")) + i_pac, i_fecha, i_pap, i_hpv = (columns.index(c) for c in ("pac_id", "fecha_indice", "pap_0", "hpv_0")) risks = [] for row in rows: p = CACU_RULE["base"] @@ -48,5 +48,5 @@ class CacuStandIn: if row[i_hpv] == "POSITIVO": p += CACU_RULE["vph_positivo"] p = min(p, CACU_RULE["tope"]) - risks += [Risk(int(row[i_pac]), float(h), round(p, 6)) for h in self.horizons] + risks += [Risk(int(row[i_pac]), row[i_fecha], float(h), round(p, 6)) for h in self.horizons] return risks diff --git a/tests/io/test_contracts.py b/tests/io/test_contracts.py index b7b27dd..1c70d2f 100644 --- a/tests/io/test_contracts.py +++ b/tests/io/test_contracts.py @@ -5,6 +5,7 @@ Fixtures: no pytest fixtures. `ConstantEstimator` is the smallest class that mee fixed probability per member and horizon. `Incomplete` lacks `provenance`. """ +from datetime import date from models.io.contracts import Provenance, Risk, RiskEstimator @@ -18,8 +19,8 @@ class ConstantEstimator: return Provenance(modelo_nombre="constant", modelo_version="1", mlflow_run_id="none", modelo_sha="sha") def estimate(self, columns: list[str], rows: list[tuple]) -> list[Risk]: - i_pac = columns.index("pac_id") - return [Risk(row[i_pac], h, 0.1) for row in rows for h in (0.0, 1.0)] + i_pac, i_fecha = columns.index("pac_id"), columns.index("fecha_indice") + return [Risk(row[i_pac], row[i_fecha], h, 0.1) for row in rows for h in (0.0, 1.0)] class Incomplete: @@ -57,7 +58,7 @@ def test_a_risk_keeps_the_column_names_and_its_interval_is_optional(): Fails if a field is renamed or reordered, or the interval becomes required. """ - risk = Risk(1, 0.0, 0.25) + risk = Risk(1, date(2025, 1, 1), 0.0, 0.25) - assert Risk._fields == ("pac_id", "horizonte_anios", "probabilidad", "ic_bajo", "ic_alto") + assert Risk._fields == ("pac_id", "fecha_indice", "horizonte_anios", "probabilidad", "ic_bajo", "ic_alto") assert (risk.ic_bajo, risk.ic_alto) == (None, None) diff --git a/tests/test_store_risks.py b/tests/test_store_risks.py index 5c7e52b..46b72af 100644 --- a/tests/test_store_risks.py +++ b/tests/test_store_risks.py @@ -66,11 +66,13 @@ def _stored(): 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 + """Pins down: a risk is `(pac_id, fecha_indice, horizonte_anios, probabilidad, None, None)`, one per member and + horizon, the index date and 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.""" + d1, d2 = date(2025, 1, 1), date(2025, 1, 2) 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)] + (1, d1, 0.0, 0.1, None, None), (1, d1, 3.0, 0.13, None, None), (2, d2, 0.0, 0.2, None, None), + (2, d2, 3.0, 0.23, None, None)] @pytest.mark.parametrize("frame", [_estimate((1, 1)), _estimate().assign(probabilidad=[0.1, 1.5, 0.2]),