941ae15a3e
Risk and risk_rows give (pac_id, fecha_indice, horizonte_anios, probabilidad, ic_bajo, ic_alto), the shape a batch now stores: the horizons count from the model's index date, taken from the estimate's rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
65 lines
2.5 KiB
Python
65 lines
2.5 KiB
Python
"""What this module covers: the `RiskEstimator` contract in `models.io.contracts` and the two records it
|
|
returns, `Risk` and `Provenance`.
|
|
|
|
Fixtures: no pytest fixtures. `ConstantEstimator` is the smallest class that meets the contract: one
|
|
fixed probability per member and horizon. `Incomplete` lacks `provenance`.
|
|
"""
|
|
|
|
from datetime import date
|
|
from models.io.contracts import Provenance, Risk, RiskEstimator
|
|
|
|
|
|
class ConstantEstimator:
|
|
"""Meets the contract with a fixed probability for every member and horizon."""
|
|
|
|
cohort_url = "cacu/cohorte_vigilancia"
|
|
outcome = "cin2_mas"
|
|
|
|
def provenance(self) -> Provenance:
|
|
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, 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:
|
|
"""Has everything but `provenance`."""
|
|
|
|
cohort_url = "cacu/cohorte_vigilancia"
|
|
outcome = "cin2_mas"
|
|
|
|
def estimate(self, columns, rows):
|
|
return []
|
|
|
|
|
|
def test_a_class_with_the_four_members_meets_the_contract():
|
|
"""Pins down: `RiskEstimator` is checked at run time by its members (`cohort_url`, `outcome`,
|
|
`provenance`, `estimate`), not by inheritance; missing one fails the check.
|
|
|
|
Fails if the protocol stops being `runtime_checkable` or a member is renamed.
|
|
"""
|
|
assert isinstance(ConstantEstimator(), RiskEstimator)
|
|
assert not isinstance(Incomplete(), RiskEstimator)
|
|
|
|
|
|
def test_provenance_has_the_four_keys_a_batch_stores():
|
|
"""Pins down: `Provenance.as_dict()` has exactly the four keys of the risk batch's provenance columns.
|
|
|
|
Fails if a field is renamed or added: the batch columns wouldn't match.
|
|
"""
|
|
assert set(ConstantEstimator().provenance().as_dict()) == {"modelo_nombre", "modelo_version", "mlflow_run_id",
|
|
"modelo_sha"}
|
|
|
|
|
|
def test_a_risk_keeps_the_column_names_and_its_interval_is_optional():
|
|
"""Pins down: `Risk` fields are named after the columns they're saved to, in that order, and the
|
|
interval defaults to `None`.
|
|
|
|
Fails if a field is renamed or reordered, or the interval becomes required.
|
|
"""
|
|
risk = Risk(1, date(2025, 1, 1), 0.0, 0.25)
|
|
|
|
assert Risk._fields == ("pac_id", "fecha_indice", "horizonte_anios", "probabilidad", "ic_bajo", "ic_alto")
|
|
assert (risk.ic_bajo, risk.ic_alto) == (None, None)
|