Estimates: each risk's interval when the model gives one; a member it can't place has no rows
A run with `risk_interval_level` has a model that gives, after the risk at each horizon, its interval's lower and upper ends; the estimate keeps them as `ic_bajo` and `ic_alto`, the contract checks that they hold the risk, and the batch stores them. `scored_frame` is the one place that reads the run and calls the model, for the command and the WebApi's cycle. A member whose risk is NaN (a combination the model never saw) is left out instead of stopping the whole batch.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
"""What this module covers: `models.dags.estimation.estimate_frame`, from a model's output to the estimate's rows.
|
||||
|
||||
Fixtures: none; a two-member cohort frame built in each test.
|
||||
"""
|
||||
|
||||
from datetime import date
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from models.dags.estimation import estimate_frame
|
||||
|
||||
COHORT = pd.DataFrame({"pac_id": [1, 2], "fecha_indice": [date(2025, 1, 1), date(2025, 2, 1)]})
|
||||
|
||||
|
||||
def test_a_member_the_model_cannot_place_has_no_rows():
|
||||
"""Pins down: a member whose risk is NaN (a combination unseen in training) is left out and the rest keep their
|
||||
rows, so one such member doesn't stop the whole batch. Fails if the NaN reaches the estimate's contract."""
|
||||
found = estimate_frame(COHORT, np.array([[0.1, 0.2], [np.nan, np.nan]]), [0.0, 1.0])
|
||||
|
||||
assert found["pac_id"].tolist() == [1, 1] and found["probabilidad"].tolist() == [0.1, 0.2]
|
||||
|
||||
|
||||
def test_with_intervals_the_unplaced_member_is_left_out_too():
|
||||
"""Pins down: with intervals, the risk block decides who is scored and each kept row has its two ends. Fails if
|
||||
the blocks are read in another order or a NaN end drops a member that has a risk."""
|
||||
output = np.array([[0.1, 0.05, 0.2], [np.nan, np.nan, np.nan]])
|
||||
found = estimate_frame(COHORT, output, [1.0], intervals=True)
|
||||
|
||||
assert found[["pac_id", "probabilidad", "ic_bajo", "ic_alto"]].values.tolist() == [[1, 0.1, 0.05, 0.2]]
|
||||
@@ -75,11 +75,19 @@ def test_each_row_becomes_one_risk_at_its_horizon():
|
||||
(2, d2, 3.0, 0.23, None, None)]
|
||||
|
||||
|
||||
def test_a_risk_keeps_the_interval_the_model_gave():
|
||||
"""Pins down: an estimate with `ic_bajo` and `ic_alto` stores them with each risk, and NaN (a member the model
|
||||
couldn't bound) is stored as no interval. Fails if `risk_rows` drops the interval or stores NaN."""
|
||||
frame = _estimate((1, 2)).assign(ic_bajo=[0.05, float("nan")], ic_alto=[0.2, float("nan")])
|
||||
assert [row[4:] for row in risk_rows(frame)] == [(0.05, 0.2), (None, None)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("frame", [_estimate((1, 1)), _estimate().assign(probabilidad=[0.1, 1.5, 0.2]),
|
||||
_estimate().rename(columns={"probabilidad": "p"})])
|
||||
_estimate().rename(columns={"probabilidad": "p"}),
|
||||
_estimate().assign(ic_bajo=[0.2, 0.1, 0.3], ic_alto=[0.3, 0.3, 0.4])])
|
||||
def test_a_malformed_estimate_stops_the_batch(frame):
|
||||
"""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."""
|
||||
"""Pins down: a member scored twice at one horizon, a probability outside 0..1, other columns, or an interval that
|
||||
doesn't hold its risk stop the batch before anything is written. Fails if any of them reaches the database."""
|
||||
with pytest.raises(ValueError):
|
||||
risk_rows(frame)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user