Merge dev into the Hamilton dataflow: the governance routines and the notebook runner on scenarios declared in model.toml

dev's 1b45bc6 (governance requirements, sandboxed notebooks) was written on the per-scenario driver that this branch
replaced with models.dags; it is carried over in that shape:

- cin2_logistic: the five ML-01a routines (sampling, missing_data, analytic_method, class_balance, output_confidence)
  and `routines` are nodes of the scenario's dataflow, on its node names (windowed_outcomes, train_set, test_set,
  fitted_pipeline, test_probabilities); `calibration_bins` is a config input with a default.
- io/adapters, dags/training: the saver writes each routine with TrainingRun.log_routine before logging the model;
  the driver hands it `routines` when the dataflow has that node. Without it a scenario still trains, but the registry
  won't promote it (ROUTINE_KINDS).
- io/tracking: dev's log_routine, run_files and run_file next to this branch's log_model (scikit-learn or pyfunc,
  pinned requirements).
- reports/rendering: dev's Quarto and Jupyter renderers (io/rendering.py, followed as a rename) with this branch's
  notebook hash; notebook_jobs and experiment import it from there, and cohort_reference and file_sha256 from
  io/storage.
- cin2_logistic/train.py: a delegate to models.dags.training for callers of the old driver.
- Persistance's English names from dev: persistance.edges.gold, cohort_repository.

Tests: 219 passed against the monorepo's dev Persistance, Extraction and Core (OCR with tesseract deselected).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
This commit is contained in:
2026-11-19 20:44:34 -06:00
43 changed files with 2304 additions and 229 deletions
+29 -19
View File
@@ -1,3 +1,4 @@
import mlflow.pyfunc
import pytest
@@ -44,35 +45,44 @@ def mlflow_tracking(tmp_path, monkeypatch):
yield
class Echo(mlflow.pyfunc.PythonModel):
"""A model whose class lives in a module, as a PROD run requires: it returns its input."""
def predict(self, context, model_input, params=None):
return model_input
def trained_run(domain: str, scenario: str, armado_id: int | None, metrics: dict, *, routines=None, model: bool = True):
"""A finished PROD run as training leaves it: each routine's record (all of them unless `routines` names some)
and, with `model`, a saved model."""
from core.governance.enums import Environment
from models.io.tracking import ROUTINE_KINDS, TrainingRun
run = TrainingRun(domain=domain, scenario=scenario, environment=Environment.PROD, cohorte_armado_id=armado_id)
for kind in ROUTINE_KINDS if routines is None else routines:
run.log_routine(kind, {"method": "prueba", "n": 8})
if model:
run.log_model(Echo())
return run.finish(metrics=metrics)
@pytest.fixture
def production_ready(governance_db, cacu_db, mlflow_tracking):
"""Factory: registers a PROD version that passes every promotion check,
returns (domain, scenario, version)."""
from core.governance.enums import ArtifactKind, Environment
"""Factory: registers a PROD version whose run recorded every routine and its model, with the three reports,
so it passes every promotion check; returns (domain, scenario, version)."""
from models.io.registry import registry
def make(tag: str, *, domain: str = "cacu", scenario: str = "tamizaje", metrics: dict | None = None):
version = registry.register(
domain=domain, scenario=scenario, environment=Environment.PROD,
cohorte_armado_id=armado(f"huella-{tag}"), metrics=metrics or {"auc": 0.8},
technical_validation_report_uri="docs/tecnica.pdf", clinical_validation_report_uri="docs/clinica.pdf",
run = trained_run(domain, scenario, armado(f"huella-{tag}"), metrics or {"auc": 0.8})
version = registry.register_run(
run, technical_validation_report_uri="docs/tecnica.pdf", clinical_validation_report_uri="docs/clinica.pdf",
interpretability_report_uri="docs/interpretabilidad.html",
)
registry.add_artifact(domain, scenario, version, kind=ArtifactKind.ONNX_CHECKPOINT, filename="model.onnx", data=tiny_onnx())
return domain, scenario, version
return make
def tiny_onnx() -> bytes:
import onnx.helper
node = onnx.helper.make_node("Identity", ["x"], ["y"])
x = onnx.helper.make_tensor_value_info("x", onnx.TensorProto.FLOAT, [1])
y = onnx.helper.make_tensor_value_info("y", onnx.TensorProto.FLOAT, [1])
return onnx.helper.make_model(onnx.helper.make_graph([node], "tiny", [x], [y]), producer_name="pytest").SerializeToString()
def armado(huella: str) -> int:
"""A CaCu cohort build with two attrition steps; returns its `armado_id`."""
from datetime import date, datetime, timezone
@@ -130,13 +140,13 @@ def predict_without_repository(tmp_path):
@pytest.fixture
def kit_stand_ins():
"""Factory: stand-ins of a domain's `cohorte_repository` and `timeline_repository` holding one kit build made of a
"""Factory: stand-ins of a domain's `cohort_repository` and `timeline_repository` holding one kit build made of a
long frame's members and events, with dates as text as the database returns them. The timeline records each read's
patients and cut-offs."""
from datetime import datetime, timezone
from types import SimpleNamespace
from persistance.aristas.oro import CohortBuild, CohortMember
from persistance.edges.gold import CohortBuild, CohortMember
def make(frame, build_id, cutoff, url="cacu/cohorte_vigilancia"):
index_dates = frame.groupby("pac_id")["fecha_indice"].first()