Cycle: estimate with every scenario in production, one batch per model
The scenarios come from the registry instead of a fixed list, and a cohort can carry several estimators.
This commit is contained in:
@@ -203,20 +203,20 @@ def cohorts(event_models: int, ctx: Context) -> dict[str, tuple[int, bool]]:
|
||||
|
||||
|
||||
@config.when(with_cohorts=True)
|
||||
def estimate(cohorts: dict[str, tuple[int, bool]], ctx: Context) -> dict[str, tuple[int, bool]]:
|
||||
def estimate(cohorts: dict[str, tuple[int, bool]], ctx: Context) -> dict[str, list[tuple[int, bool]]]:
|
||||
"""El riesgo de los miembros del armado más reciente de cada cohorte que tiene estimador (ADR-MD-008).
|
||||
|
||||
Lee el marco ancho del armado (`read_frame`) en una sesión de lectura, se lo da al estimador
|
||||
(`ctx.estimators[url]`: `estimate(columnas, filas)`, `procedencia()` y `desenlace`) y escribe sus riesgos como
|
||||
un lote `preliminary` (`CohortRepository.guardar_lote`). Estimar lo mismo otra vez no duplica.
|
||||
Devuelve, por cohorte, (`lote_id`, si es nuevo). Confirmar o anular un lote es la orden
|
||||
Lee el marco ancho del armado (`read_frame`) en una sesión de lectura, se lo da a cada estimador de la cohorte
|
||||
(`ctx.estimators[url]`, una lista: `estimar(columnas, filas)`, `procedencia()` y `desenlace`) y escribe los
|
||||
riesgos de cada uno como un lote `preliminary` (`CohortRepository.save_batch`). Estimar lo mismo otra vez no
|
||||
duplica. Devuelve, por cohorte, un (`lote_id`, si es nuevo) por estimador, en su orden. Confirmar o anular un lote es la orden
|
||||
`confirm-risks`, no este paso.
|
||||
"""
|
||||
tablas_cohorte = import_module(f"webapi.orchestration.tables.{ctx.project}").COHORTES
|
||||
lotes = {}
|
||||
for url in cohort_urls(ctx.project, _manifest(ctx)):
|
||||
estimador = ctx.estimators.get(url)
|
||||
if estimador is None:
|
||||
estimadores = ctx.estimators.get(url) or []
|
||||
if not estimadores:
|
||||
continue
|
||||
|
||||
def frame(sesion, url=url):
|
||||
@@ -226,8 +226,9 @@ def estimate(cohorts: dict[str, tuple[int, bool]], ctx: Context) -> dict[str, tu
|
||||
armado_id, ancho = ctx.repos.gold.cohorts.read(frame)
|
||||
if armado_id is None:
|
||||
continue
|
||||
riesgos = [tuple(r) for r in estimador.estimar(*ancho)]
|
||||
lotes[url] = ctx.repos.gold.cohorts.save_batch(
|
||||
armado_id, estimador.procedencia().como_dict(), estimador.desenlace, riesgos
|
||||
)
|
||||
lotes[url] = []
|
||||
for estimador in estimadores:
|
||||
riesgos = [tuple(r) for r in estimador.estimar(*ancho)]
|
||||
lotes[url].append(ctx.repos.gold.cohorts.save_batch(
|
||||
armado_id, estimador.procedencia().como_dict(), estimador.desenlace, riesgos))
|
||||
return lotes
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""Live estimators for `Context.estimators`: the cycle's `estimate` step scores each cohort with the model version
|
||||
its domain has in production.
|
||||
"""Live estimators for `Context.estimators`: the cycle's `estimate` step scores each cohort with every model version
|
||||
of its domain in production, one batch per model.
|
||||
|
||||
`estimate` hands an estimator the cohort build's wide frame (`estimar(columnas, filas)`, `procedencia()`,
|
||||
`desenlace`): one column per the cohort's own variables. A registered model's predictors are a different, larger
|
||||
@@ -11,7 +11,6 @@ unused by design.
|
||||
|
||||
from dataclasses import dataclass
|
||||
from importlib import import_module
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -61,45 +60,19 @@ class ScenarioEstimator:
|
||||
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]:
|
||||
def live_estimators(domain: str) -> dict[str, list[ScenarioEstimator]]:
|
||||
"""The estimators of a domain's cohorts, by cohort url: one per scenario with a version in production in the
|
||||
registry, so promoting or retiring a version is what adds or drops it. A registered name with no scenario
|
||||
definition in `models` is skipped. Empty where none is in production, the same as no estimator being
|
||||
configured: `estimate` then does nothing."""
|
||||
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)
|
||||
found: dict[str, list[ScenarioEstimator]] = {}
|
||||
for name in registry.in_production(domain):
|
||||
try:
|
||||
registry.resolve(domain, name)
|
||||
scenario = Scenario.load(domain, name)
|
||||
except LookupError:
|
||||
continue
|
||||
found[scenario.cohort] = ScenarioEstimator(domain, name)
|
||||
found.setdefault(scenario.cohort, []).append(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 _scenario_estimators(domain)
|
||||
from models.dags.training import Scenario
|
||||
from models.io.registry import registry
|
||||
|
||||
scenario = Scenario.load(domain, "cin2_logistic")
|
||||
try:
|
||||
registry.resolve(domain, scenario.scenario)
|
||||
except LookupError:
|
||||
return {}
|
||||
return {scenario.cohort: Cin2LogisticEstimator(domain)}
|
||||
|
||||
Reference in New Issue
Block a user