76 lines
3.7 KiB
Python
76 lines
3.7 KiB
Python
"""What this module covers: the value checks of the cohort and estimate files (`COHORT_FRAME`, `ESTIMATE_FRAME`,
|
|
`check_frame` in `models.io.contracts`), and that reading a cohort file and building one apply them.
|
|
|
|
Fixtures: none; `_events` makes a valid two-member frame and each test breaks one rule.
|
|
"""
|
|
|
|
from datetime import date, datetime, timezone
|
|
|
|
import pandas as pd
|
|
import pyarrow as pa
|
|
import pyarrow.parquet as pq
|
|
import pytest
|
|
|
|
from models.io.contracts import (COHORT_FRAME, COHORT_SCHEMA, ESTIMATE_FRAME, CohortFileError, EstimateError,
|
|
check_frame)
|
|
from models.io.storage import read_cohort
|
|
from models.steps.cohort_file import cohort_table
|
|
|
|
|
|
def _events(**changes):
|
|
frame = pd.DataFrame({"pac_id": [1, 1, 2], "fecha_indice": [date(2025, 1, 1)] * 2 + [date(2025, 2, 1)],
|
|
"fecha": [date(2025, 1, 1), date(2025, 3, 1), date(2025, 2, 1)],
|
|
"conocida_en": [datetime(2025, 4, 1, tzinfo=timezone.utc)] * 3,
|
|
"clase": ["procedimiento", "medicion", "procedimiento"],
|
|
"tipo": ["citologia", "bethesda", "citologia"], "codigo": ["c", "b", "c"],
|
|
"valor_codigo": [None, "NORMAL", None], "valor_numerico": [None, None, None],
|
|
"fuente_tabla": ["citologiacervical"] * 3, "fuente_id": ["a", "a", "b"]})
|
|
for column, (row, value) in changes.items():
|
|
frame.loc[row, column] = value
|
|
return frame
|
|
|
|
|
|
def test_a_valid_build_passes():
|
|
"""Pins down: a well-formed frame comes back unchanged. Fails if a check rejects valid data, which would stop every
|
|
export and training."""
|
|
assert check_frame(COHORT_FRAME, _events(), CohortFileError, "test").equals(_events())
|
|
|
|
|
|
@pytest.mark.parametrize("changes, check", [
|
|
({"fecha_indice": (1, date(2025, 1, 5))}, "more than one index date"),
|
|
({"pac_id": (2, 0)}, "greater_than"),
|
|
({"fecha": (0, None)}, "not_nullable"),
|
|
({"tipo": (1, "")}, "str_length"),
|
|
])
|
|
def test_a_broken_build_names_what_failed(changes, check):
|
|
"""Pins down: two index dates for a member, a non-positive id, a missing date or an empty type are refused, and
|
|
the error names the check. Fails if a broken file reaches a model, where it would train on wrong rows."""
|
|
with pytest.raises(CohortFileError, match=check):
|
|
check_frame(COHORT_FRAME, _events(**changes), CohortFileError, "test")
|
|
|
|
|
|
def test_reading_and_building_a_file_apply_the_checks(tmp_path):
|
|
"""Pins down: `read_cohort` refuses a file whose rows break `COHORT_FRAME`, and `cohort_table` refuses to build
|
|
one. Fails if either path skips the check."""
|
|
broken = _events(fecha_indice=(1, date(2025, 1, 5)))
|
|
path = tmp_path / "1.parquet"
|
|
pq.write_table(pa.Table.from_pandas(broken, schema=COHORT_SCHEMA, preserve_index=False), path)
|
|
|
|
with pytest.raises(CohortFileError):
|
|
read_cohort(path)
|
|
with pytest.raises(CohortFileError):
|
|
cohort_table(broken, {"build": 1})
|
|
|
|
|
|
@pytest.mark.parametrize("estimate", [
|
|
pd.DataFrame({"pac_id": [1, 1], "fecha_indice": [date(2025, 1, 1)] * 2, "probabilidad": [0.1, 0.2]}),
|
|
pd.DataFrame({"pac_id": [1], "fecha_indice": [date(2025, 1, 1)], "probabilidad": [1.2]}),
|
|
pd.DataFrame({"pac_id": [1], "fecha_indice": [date(2025, 1, 1)], "probabilidad": [0.1], "extra": [1]}),
|
|
pd.DataFrame({"fecha_indice": [date(2025, 1, 1)], "pac_id": [1], "probabilidad": [0.1]}),
|
|
])
|
|
def test_an_estimate_is_one_probability_per_member_in_its_columns(estimate):
|
|
"""Pins down: a member twice, a probability outside 0..1, an extra column or another column order are refused.
|
|
Fails if a malformed output can be written or stored as risks."""
|
|
with pytest.raises(EstimateError):
|
|
check_frame(ESTIMATE_FRAME, estimate, EstimateError, "test")
|