diff --git a/models/riesgo.py b/models/riesgo.py index b716758..9381958 100644 --- a/models/riesgo.py +++ b/models/riesgo.py @@ -134,3 +134,43 @@ class RiesgoSinteticoTea: p = min(p, r["tope"]) riesgos += [Riesgo(int(fila[i_pac]), float(h), round(p, 6)) for h in self.horizontes] return riesgos + + +# La regla del sustituto de CaMa: lo que suma el BI-RADS índice. +REGLA_SINTETICA_CAMA = {"base": 0.005, "birads_0": 0.03, "birads_3": 0.02, "birads_4": 0.30, "birads_5": 0.80, + "tope": 0.95} +BIRADS_SUSPECHA = {"B0": "birads_0", "B3": "birads_3", "B4": "birads_4", "B4A": "birads_4", "B4B": "birads_4", + "B4C": "birads_4", "B5": "birads_5"} + + +class RiesgoSinteticoCama: + """El sustituto determinista de CaMa, sobre el marco de `cama/cohorte_tamizaje` (T-21g). Sin MLflow. + + La probabilidad de cáncer de mama por biopsia al horizonte (1 año, el de la cohorte) es + `REGLA_SINTETICA_CAMA["base"]` más lo que suma la categoría del BI-RADS índice (`birads_0`), con + tope. Las subcategorías 4A, 4B y 4C suman como 4. Sin intervalo. No es un modelo clínico. + """ + + def __init__(self, cohorte_url: str = "cama/cohorte_tamizaje", desenlace: str = "d", + horizontes: tuple[float, ...] = (1.0,)): + self.cohorte_url = cohorte_url + self.desenlace = desenlace + self.horizontes = tuple(horizontes) + + def procedencia(self) -> Procedencia: + regla = {"regla": REGLA_SINTETICA_CAMA, "birads": BIRADS_SUSPECHA, "horizontes": self.horizontes} + sha = hashlib.sha256(json.dumps(regla, sort_keys=True).encode("utf-8")).hexdigest() + return Procedencia(modelo_nombre="sustituto_cama", modelo_version="1", mlflow_run_id="sin-mlflow", + modelo_sha=sha) + + def estimar(self, columnas: list[str], filas: list[tuple]) -> list[Riesgo]: + faltan = [c for c in ("pac_id", "birads_0") if c not in columnas] + if faltan: + raise ValueError(f"El marco no trae las columnas {faltan}: el sustituto las necesita.") + i_pac, i_birads = columnas.index("pac_id"), columnas.index("birads_0") + r = REGLA_SINTETICA_CAMA + riesgos = [] + for fila in filas: + p = min(r["base"] + r.get(BIRADS_SUSPECHA.get(fila[i_birads]), 0.0), r["tope"]) + riesgos += [Riesgo(int(fila[i_pac]), float(h), round(p, 6)) for h in self.horizontes] + return riesgos diff --git a/tests/test_riesgo.py b/tests/test_riesgo.py index c45fa15..030d93c 100644 --- a/tests/test_riesgo.py +++ b/tests/test_riesgo.py @@ -3,7 +3,15 @@ import pytest from models import riesgo -from models.riesgo import REGLA_SINTETICA_TEA, EstimadorDeRiesgo, Riesgo, RiesgoSintetico, RiesgoSinteticoTea +from models.riesgo import ( + REGLA_SINTETICA_CAMA, + REGLA_SINTETICA_TEA, + EstimadorDeRiesgo, + Riesgo, + RiesgoSintetico, + RiesgoSinteticoCama, + RiesgoSinteticoTea, +) COLUMNAS = ["pac_id", "fecha_indice", "c", "pap_0", "hpv_0"] FILAS = [(1, "2024-07-01", 0, "NORMAL", "NEGATIVO"), (2, "2024-08-01", 0, "LIEAG", "POSITIVO"), (3, "2024-09-01", 0, None, None)] @@ -71,3 +79,27 @@ def test_la_regla_de_tea_suma_mchat_ucin_y_apgar_bajo(): def test_el_sustituto_de_tea_sin_sus_predictores_falla(): with pytest.raises(ValueError, match="mchat_0"): RiesgoSinteticoTea().estimar(["pac_id", "fecha_indice"], []) + + +# --- El sustituto de CaMa (T-21g) -------------------------------------------------------------- + +COLUMNAS_CAMA = ["pac_id", "fecha_indice", "d", "t", "birads_0", "calidad_0", "n_ordenes_previas"] + + +def test_el_sustituto_de_cama_cumple_el_contrato_y_lee_su_cohorte(): + estimador = RiesgoSinteticoCama() + assert isinstance(estimador, EstimadorDeRiesgo) + assert estimador.cohorte_url == "cama/cohorte_tamizaje" and estimador.horizontes == (1.0,) + + +def test_la_regla_de_cama_suma_por_birads_indice_y_4a_cuenta_como_4(): + filas = [(1, None, None, None, "B2", None, 0), (2, None, None, None, "B4A", None, 0), + (3, None, None, None, "B5", None, 0), (4, None, None, None, None, None, 0)] + r = REGLA_SINTETICA_CAMA + assert [x.probabilidad for x in RiesgoSinteticoCama().estimar(COLUMNAS_CAMA, filas)] == pytest.approx( + [r["base"], r["base"] + r["birads_4"], r["base"] + r["birads_5"], r["base"]]) + + +def test_el_sustituto_de_cama_sin_su_predictor_falla(): + with pytest.raises(ValueError, match="birads_0"): + RiesgoSinteticoCama().estimar(["pac_id", "fecha_indice"], [])