81 lines
3.7 KiB
Python
81 lines
3.7 KiB
Python
"""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
|
|
set, so an estimator here ignores the wide frame and rebuilds the model's own input (`models.io.storage.KitBuild`):
|
|
the cycle's latest build for membership, the domain's timeline for the events, then the production version in
|
|
MLflow to score it at the horizons its run recorded (`horizons_years`). `estimar`'s arguments go
|
|
unused by design.
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
from importlib import import_module
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Provenance:
|
|
modelo_nombre: str
|
|
modelo_version: str
|
|
mlflow_run_id: str
|
|
modelo_sha: str | None
|
|
|
|
def como_dict(self) -> dict[str, str]:
|
|
return {key: value for key, value in self.__dict__.items() if value}
|
|
|
|
|
|
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, scenario: str, version: str | None = None):
|
|
self.domain = domain
|
|
self.scenario = scenario
|
|
self.version = version
|
|
self.desenlace = ""
|
|
self._provenance: Provenance | None = None
|
|
|
|
def estimar(self, columnas: list[str], filas: list[tuple]) -> list[tuple]:
|
|
from models.dags.estimation import scored_frame
|
|
from models.dags.training import Scenario
|
|
from models.io.registry import registered_name, registry
|
|
from models.io.storage import KitBuild
|
|
from models.steps.risks import risk_rows
|
|
|
|
repos = import_module(f"persistance.{self.domain}")
|
|
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()
|
|
self.desenlace = scenario.outcome
|
|
self._provenance = Provenance(
|
|
modelo_nombre=registered_name(self.domain, scenario.scenario), modelo_version=resolved.version,
|
|
mlflow_run_id=resolved.run_id, modelo_sha=resolved.modelo_sha)
|
|
return [tuple(row) for row in risk_rows(scored_frame(resolved, frame))]
|
|
|
|
def procedencia(self) -> Provenance:
|
|
if self._provenance is None:
|
|
raise RuntimeError("estimar() runs before procedencia()")
|
|
return self._provenance
|
|
|
|
|
|
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`, or one whose definition turns off `batch_estimates`, 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: dict[str, list[ScenarioEstimator]] = {}
|
|
for name in registry.in_production(domain):
|
|
try:
|
|
scenario = Scenario.load(domain, name)
|
|
except LookupError:
|
|
continue
|
|
if not scenario.batch_estimates:
|
|
continue
|
|
found.setdefault(scenario.cohort, []).append(ScenarioEstimator(domain, name))
|
|
return found
|