Files
models/tests/test_estimate_frame.py
edgar 78a4748592 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.
2026-11-22 07:38:17 -06:00

31 lines
1.3 KiB
Python

"""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]]