diff --git a/models/riesgo.py b/models/riesgo.py index f9dcc1a..b716758 100644 --- a/models/riesgo.py +++ b/models/riesgo.py @@ -89,3 +89,48 @@ class RiesgoSintetico: p = min(p, REGLA_SINTETICA["tope"]) riesgos += [Riesgo(int(fila[i_pac]), float(h), round(p, 6)) for h in self.horizontes] return riesgos + + +# La regla del sustituto de TEA: lo que suma la interpretación del M-CHAT índice y lo perinatal. +REGLA_SINTETICA_TEA = {"base": 0.02, "mchat_medio": 0.10, "mchat_alto": 0.30, "ucin": 0.05, "apgar_bajo": 0.05, + "apgar_corte": 7, "tope": 0.95} + + +class RiesgoSinteticoTea: + """El sustituto determinista de TEA, sobre el marco de `tea/cohorte_tamizaje` (T-21e). Sin MLflow. + + La probabilidad de un diagnóstico de TEA al horizonte (2 años, el de la cohorte) es + `REGLA_SINTETICA_TEA["base"]`, más `mchat_medio` o `mchat_alto` según `mchat_0`, más `ucin` si + `ingreso_ucin` es 1, más `apgar_bajo` si `apgar_5min` es menor que `apgar_corte`, con tope. Sin + intervalo. No es un modelo clínico. + """ + + def __init__(self, cohorte_url: str = "tea/cohorte_tamizaje", desenlace: str = "d", + horizontes: tuple[float, ...] = (2.0,)): + self.cohorte_url = cohorte_url + self.desenlace = desenlace + self.horizontes = tuple(horizontes) + + def procedencia(self) -> Procedencia: + regla = {"regla": REGLA_SINTETICA_TEA, "horizontes": self.horizontes} + sha = hashlib.sha256(json.dumps(regla, sort_keys=True).encode("utf-8")).hexdigest() + return Procedencia(modelo_nombre="sustituto_tea", modelo_version="1", mlflow_run_id="sin-mlflow", + modelo_sha=sha) + + def estimar(self, columnas: list[str], filas: list[tuple]) -> list[Riesgo]: + necesarias = ("pac_id", "mchat_0", "ingreso_ucin", "apgar_5min") + faltan = [c for c in necesarias if c not in columnas] + if faltan: + raise ValueError(f"El marco no trae las columnas {faltan}: el sustituto las necesita.") + i_pac, i_mchat, i_ucin, i_apgar = (columnas.index(c) for c in necesarias) + r = REGLA_SINTETICA_TEA + riesgos = [] + for fila in filas: + p = r["base"] + {"RIESGO_MEDIO": r["mchat_medio"], "RIESGO_ALTO": r["mchat_alto"]}.get(fila[i_mchat], 0.0) + if fila[i_ucin] == 1: + p += r["ucin"] + if fila[i_apgar] is not None and fila[i_apgar] < r["apgar_corte"]: + p += r["apgar_bajo"] + p = min(p, 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 2a19da7..c45fa15 100644 --- a/tests/test_riesgo.py +++ b/tests/test_riesgo.py @@ -3,7 +3,7 @@ import pytest from models import riesgo -from models.riesgo import EstimadorDeRiesgo, Riesgo, RiesgoSintetico +from models.riesgo import REGLA_SINTETICA_TEA, EstimadorDeRiesgo, Riesgo, RiesgoSintetico, 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)] @@ -45,3 +45,29 @@ def test_la_procedencia_trae_las_cuatro_llaves_y_su_sha_cambia_con_la_regla(monk def test_sin_los_predictores_falla_y_los_nombra(): with pytest.raises(ValueError, match="pap_0"): RiesgoSintetico().estimar(["pac_id", "hpv_0"], [(1, None)]) + + +# --- El sustituto de TEA (T-21f) --------------------------------------------------------------- + +COLUMNAS_TEA = ["pac_id", "fecha_indice", "d", "t", "mchat_0", "apgar_5min", "ingreso_ucin", "n_antecedentes", + "hipoacusia_0"] + + +def test_el_sustituto_de_tea_cumple_el_contrato_y_lee_su_cohorte(): + estimador = RiesgoSinteticoTea() + assert isinstance(estimador, EstimadorDeRiesgo) + assert estimador.cohorte_url == "tea/cohorte_tamizaje" and estimador.horizontes == (2.0,) + + +def test_la_regla_de_tea_suma_mchat_ucin_y_apgar_bajo(): + filas = [(1, None, None, None, "RIESGO_BAJO", 9.0, 0.0, 0, None), + (2, None, None, None, "RIESGO_ALTO", 6.0, 1.0, 2, "LEVE"), + (3, None, None, None, None, None, None, 0, None)] + r = REGLA_SINTETICA_TEA + assert [x.probabilidad for x in RiesgoSinteticoTea().estimar(COLUMNAS_TEA, filas)] == pytest.approx( + [r["base"], r["base"] + r["mchat_alto"] + r["ucin"] + r["apgar_bajo"], r["base"]]) + + +def test_el_sustituto_de_tea_sin_sus_predictores_falla(): + with pytest.raises(ValueError, match="mchat_0"): + RiesgoSinteticoTea().estimar(["pac_id", "fecha_indice"], [])