CaMa: individual risk and population curves served, and the cycle estimates with any scenario in production

webapi/fhir/risk.py builds a patient's RiskAssessment for CaCu and CaMa; GET /cama/modelo/curvas serves the production
version's survival curves; the cycle's estimator is generic by scenario; silver persona carries the date of death for CaMa;
the import schema accepts it; the CaMa risk batches can be confirmed and voided.
This commit is contained in:
rockdrigo
2026-11-21 02:07:34 -06:00
parent 26f2b35338
commit e2ecaa027c
17 changed files with 446 additions and 85 deletions
+37 -8
View File
@@ -25,15 +25,16 @@ class Provenance:
return {key: value for key, value in self.__dict__.items() if value}
class Cin2LogisticEstimator:
"""Scores CaCu's surveillance cohort with the `cacu-cin2_logistic` version in production (or `version`, to
test a candidate). Offered by `live_estimators` only once that version exists, so `procedencia` always has
something to report when `estimate` calls it."""
class ScenarioEstimator:
"""Scores a domain's cohort with the version of one scenario that is in production (or `version`, to test a
candidate). Offered by `live_estimators` only once that version exists, so `procedencia` always has something to
report when `estimate` calls it."""
def __init__(self, domain: str = "cacu", version: str | None = None):
def __init__(self, domain: str, scenario: str, version: str | None = None):
self.domain = domain
self.scenario = scenario
self.version = version
self.desenlace = "cin2_mas"
self.desenlace = ""
self._provenance: Provenance | None = None
def estimar(self, columnas: list[str], filas: list[tuple]) -> list[tuple]:
@@ -49,7 +50,7 @@ class Cin2LogisticEstimator:
from models.steps.risks import risk_rows
repos = import_module(f"persistance.{self.domain}")
scenario = Scenario.load(self.domain, "cin2_logistic")
scenario = Scenario.load(self.domain, self.scenario)
build_id = repos.cohort_repository.latest_build(scenario.cohort)
resolved = registry.resolve(self.domain, scenario.scenario, self.version)
frame = KitBuild(self.domain, build_id).frame()
@@ -67,11 +68,39 @@ class Cin2LogisticEstimator:
return self._provenance
class Cin2LogisticEstimator(ScenarioEstimator):
"""Scores CaCu's surveillance cohort with the `cacu-cin2_logistic` version in production (or `version`)."""
def __init__(self, domain: str = "cacu", version: str | None = None):
super().__init__(domain, "cin2_logistic", version)
self.desenlace = "cin2_mas"
# The scenarios the cycle estimates with, per domain other than CaCu: each one that has a version in production
# scores the cohort it names.
LIVE_SCENARIOS = {"cama": ("survival",)}
def _scenario_estimators(domain: str) -> dict[str, Any]:
from models.dags.training import Scenario
from models.io.registry import registry
found = {}
for name in LIVE_SCENARIOS.get(domain, ()):
scenario = Scenario.load(domain, name)
try:
registry.resolve(domain, name)
except LookupError:
continue
found[scenario.cohort] = ScenarioEstimator(domain, name)
return found
def live_estimators(domain: str) -> dict[str, Any]:
"""The estimators of a domain's cohorts whose model has a version in production, by cohort url. Empty where
none is in production yet, the same as no estimator being configured: `estimate` then does nothing."""
if domain != "cacu":
return {}
return _scenario_estimators(domain)
from models.dags.training import Scenario
from models.io.registry import registry
+6
View File
@@ -25,6 +25,12 @@ def de_derechohabiente(paciente) -> dict:
}
def con_defuncion(paciente) -> dict:
"""Lo de `de_derechohabiente` y la fecha de defunción, el desenlace de la supervivencia: nula mientras vive o si se
desconoce."""
return {**de_derechohabiente(paciente), "fecha_defuncion": paciente.pac_fecha_defuncion}
def con_sexo(paciente) -> dict:
"""Lo que la persona de TEA agrega: el sexo, como código."""
return {"sexo": codigo(paciente.pac_sexo)}
+2 -2
View File
@@ -34,7 +34,7 @@ from core.cama.indicadores.models import (
Persona,
)
from webapi.orchestration.silver.persons import de_derechohabiente
from webapi.orchestration.silver.persons import con_defuncion
from core.indicadores.cohortes import TablasCohorte
from webapi.orchestration.tables import TablasKit
@@ -63,7 +63,7 @@ TABLAS = TablasKit(
CatMeasureEstrato=CatMeasureEstrato,
MeasureReportUnidad=MeasureReportUnidad,
MeasureReportUnidadEstrato=MeasureReportUnidadEstrato,
persona_propias=de_derechohabiente,
persona_propias=con_defuncion,
)
# Las cohortes para modelos y sus estimaciones de riesgo (oro restringido, ADR-MD-003; MD-12).