01e4406838
They run a notebook on a build's file and keep it as an MLflow run, as eda does; io/ keeps the storage and MLflow boundary and pipelines/ the cohort pipelines. Moved whole with git mv, imports repointed; models.notebook, the API notebooks import, stays where it is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
85 lines
3.5 KiB
Python
85 lines
3.5 KiB
Python
"""What a notebook run from the platform works with (`models.reports.experiment`): the cohort build's file it was
|
|
run on and, if one was chosen, a registered model version.
|
|
|
|
from models.notebook import experiment
|
|
|
|
exp = experiment()
|
|
cohort = exp.cohort() # the build's rows, checked against the cohort contract
|
|
model = exp.model() # the chosen version, loaded; None if none was chosen
|
|
run = exp.training_run() # a DEV training run linked to this build's file (ML-02, ML-03)
|
|
run.log_routine("MUESTREO", {...})
|
|
run.log_model(MyModel(...))
|
|
version = exp.register(run.finish(metrics={...}))
|
|
|
|
The notebook gets no database connection and no access to the platform's MLflow (ML-04, ML-05): the build's file
|
|
is its data, and what it trains goes to a store of its own that the platform imports afterwards as DEV. Outside the
|
|
platform, set `COHORT_FILE`, `MODELS_DOMAIN` and `MODELS_SCENARIO` (and `MODEL_VERSION`) to work the same way.
|
|
"""
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
|
|
COHORT_FILE_ENV = "COHORT_FILE"
|
|
DOMAIN_ENV = "MODELS_DOMAIN"
|
|
SCENARIO_ENV = "MODELS_SCENARIO"
|
|
VERSION_ENV = "MODEL_VERSION"
|
|
MODEL_PATH_ENV = "MODEL_PATH"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Experiment:
|
|
domain: str
|
|
scenario: str
|
|
cohort_path: Path
|
|
version: str | None = None
|
|
|
|
def cohort(self) -> pd.DataFrame:
|
|
from models.io.storage import read_cohort
|
|
|
|
return read_cohort(self.cohort_path)
|
|
|
|
def lineage(self) -> dict[str, str]:
|
|
"""The build's own metadata: its cut-off, parameters, rules and attrition."""
|
|
from models.io.storage import read_metadata
|
|
|
|
return read_metadata(self.cohort_path)
|
|
|
|
def model(self):
|
|
"""The chosen version as an MLflow pyfunc (`.predict(cohort)`), or `None`. On the platform it's a copy of the
|
|
version's saved model (`MODEL_PATH`); elsewhere it's loaded from the registry."""
|
|
if self.version is None:
|
|
return None
|
|
import mlflow.pyfunc
|
|
|
|
if path := os.environ.get(MODEL_PATH_ENV):
|
|
return mlflow.pyfunc.load_model(path)
|
|
from models.io.registry import registry
|
|
|
|
return mlflow.pyfunc.load_model(registry.resolve(self.domain, self.scenario, self.version).model_uri)
|
|
|
|
def training_run(self, scenario: str | None = None):
|
|
"""A DEV run of this domain, linked to the build's file. On the platform it goes to the job's own MLflow and
|
|
is copied into the real one, as DEV, when the notebook ends (`models.reports.notebook_import`)."""
|
|
from models.io.storage import cohort_reference, file_sha256
|
|
from models.io.tracking import TrainingRun
|
|
|
|
cohort = cohort_reference(self.lineage(), file_sha256(self.cohort_path))
|
|
return TrainingRun(domain=self.domain, scenario=scenario or self.scenario, environment="DEV", cohort=cohort)
|
|
|
|
def register(self, run) -> str:
|
|
"""Registers a finished run as a new DEV version of its scenario and returns the version."""
|
|
from models.io.registry import registry
|
|
|
|
return registry.register_run(run)
|
|
|
|
|
|
def experiment() -> Experiment:
|
|
missing = [name for name in (COHORT_FILE_ENV, DOMAIN_ENV, SCENARIO_ENV) if not os.environ.get(name)]
|
|
if missing:
|
|
raise RuntimeError(f"this notebook wasn't run from the platform: {', '.join(missing)} not set")
|
|
return Experiment(os.environ[DOMAIN_ENV], os.environ[SCENARIO_ENV], Path(os.environ[COHORT_FILE_ENV]),
|
|
os.environ.get(VERSION_ENV) or None)
|