449c67b19d
Drops the helpers only the stand-ins used, tests the contract with an estimator defined in the test, and updates the README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
"""What this module covers: the `RiskEstimator` contract in `models.contract` 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 models.contract 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 = columns.index("pac_id")
|
|
return [Risk(row[i_pac], 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, 0.0, 0.25)
|
|
|
|
assert Risk._fields == ("pac_id", "horizonte_anios", "probabilidad", "ic_bajo", "ic_alto")
|
|
assert (risk.ic_bajo, risk.ic_alto) == (None, None)
|