From 01e4406838f385dd74fc5c2057385d0c971860d9 Mon Sep 17 00:00:00 2001 From: edgar Date: Thu, 19 Nov 2026 20:50:46 -0600 Subject: [PATCH] The notebook runner moves to reports/: experiment, notebook_jobs and notebook_import next to rendering and eda 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 Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7 --- README.md | 6 +++--- models/__main__.py | 4 ++-- models/notebook.py | 4 ++-- models/reports/__init__.py | 11 +++++++---- models/{pipelines => reports}/experiment.py | 8 ++++---- models/{io => reports}/notebook_import.py | 2 +- models/{io => reports}/notebook_jobs.py | 2 +- pyproject.toml | 2 +- tests/{pipelines => reports}/test_experiment.py | 10 +++++----- 9 files changed, 26 insertions(+), 23 deletions(-) rename models/{pipelines => reports}/experiment.py (96%) rename models/{io => reports}/notebook_import.py (98%) rename models/{io => reports}/notebook_jobs.py (99%) rename tests/{pipelines => reports}/test_experiment.py (97%) diff --git a/README.md b/README.md index e60acb8..ec84c56 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ WebApi. | `cohorts list`, `cohorts show` | `python -m models cohorts list cacu cohorte_vigilancia [--source kit\|dbt]` lists the builds of both sources, newest first, and whether each has its file (dbt's from their files alone); `cohorts show … --source kit\|dbt --build N` prints one build's file, hash, parameters, rules and attrition as JSON (`io/storage.py`: `list_builds`, `describe_build`). | | `steps/risks.py`, `pipelines/store_risks.py` | Stores an estimate file as a batch of risks in the domain's database (`evaluacion_riesgo_lote`, `evaluacion_riesgo`), through `cohorte_repository`: one preliminary risk per member and horizon, with the version's MLflow keys. Storing the same estimate again adds nothing. A batch points to a kit build (`armado_id`), so estimates of dbt builds are refused. `python -m models store cacu cin2_logistic --input file.parquet`, or `run … --estimate --store --output file.parquet`. | | `reports/` (`rendering.py`, `eda.py`) | A model's exploratory notebook as an MLflow report: `python -m models report eda cacu cin2_logistic [--source kit\|dbt] [--build N] [--notebook path] [--kernel name]` runs `/notebooks/01_eda.ipynb` on the build's file (`COHORT_FILE`), renders it with Quarto to one HTML page, and logs the page and the notebook without outputs as a run of the experiment `-/`, tagged with the notebook's content hash and the build's file. The same notebook and file are reported once. Needs Quarto (`QUARTO_BIN`) and a Python with Jupyter (`QUARTO_PYTHON`). Rendering: Runs a notebook and renders its page. Renderers are classes (`RENDERERS`, `register_renderer`): `quarto` and `jupyter` (`nbclient` + `nbconvert`, the `notebooks` extra). `NOTEBOOK_RENDERER` picks one; otherwise Quarto if installed, else Jupyter. `NOTEBOOK_CELL_TIMEOUT` limits each cell (1800 s). | -| `pipelines/experiment.py` | A notebook uploaded to the platform, run on one exported cohort build as a run of `-/experimentos`: the notebook, its page, what was imported from it (`importado.json`), `error.txt` if it failed, and tags for the notebook's hash, the build's file, the version, the code and where it ran (`sandbox`). `start` and `execute` are separate so the WebApi runs it in the background. | -| `io/notebook_jobs.py` | Where the notebook runs: a job folder with the notebook, a copy of the build's file and of the chosen model, and an MLflow store of its own; every database URL is an empty in-memory one. Executors: `contenedor` leaves the job in `NOTEBOOK_JOBS_DIR` for the runner (`python -m models notebooks serve`, the `notebooks` container: no network, read-only, unprivileged, only that folder); `local` runs it in this process, unisolated, for development. | -| `io/notebook_import.py` | Copies what the notebook trained in its own store into the platform's MLflow, as DEV runs and versions with the build's real cohort tags and a recomputed `modelo_sha`, never with an alias; refuses symbolic links and anything outside the job folder. | +| `reports/experiment.py` | A notebook uploaded to the platform, run on one exported cohort build as a run of `-/experimentos`: the notebook, its page, what was imported from it (`importado.json`), `error.txt` if it failed, and tags for the notebook's hash, the build's file, the version, the code and where it ran (`sandbox`). `start` and `execute` are separate so the WebApi runs it in the background. | +| `reports/notebook_jobs.py` | Where the notebook runs: a job folder with the notebook, a copy of the build's file and of the chosen model, and an MLflow store of its own; every database URL is an empty in-memory one. Executors: `contenedor` leaves the job in `NOTEBOOK_JOBS_DIR` for the runner (`python -m models notebooks serve`, the `notebooks` container: no network, read-only, unprivileged, only that folder); `local` runs it in this process, unisolated, for development. | +| `reports/notebook_import.py` | Copies what the notebook trained in its own store into the platform's MLflow, as DEV runs and versions with the build's real cohort tags and a recomputed `modelo_sha`, never with an alias; refuses symbolic links and anything outside the job folder. | | `io/extraction_ranker.py` | Each domain's document extraction ranker (`extraction.nlp.ranking.LearnedRanker`) as the model `-extraccion`: every batch it learns from (an answer key, or the corrections made while reviewing an import) is a new DEV version continuing the latest, with its five routine records, its weights (`ranker/ranker.json`) and its saved model. `current` gives the version in production, otherwise the latest; `EXTRACTION_RANKER=produccion` only takes production, `ninguno` turns it off. Weights over how candidates were found and counts only, never document text. | | `notebook.py` | What a notebook uses inside: `experiment()` gives the build's rows (`cohort()`), its metadata, the chosen version loaded (`model()`), a DEV `TrainingRun` linked to the build (`training_run()`) and `register(run)`. | | `io/adapters.py` | Hamilton's boundary with storage and MLflow, attached by the driver with `with_materializers`: the loaders `cohort_source` and `cohort_source_lineage` (any source) and the saver `registered_model`, which logs a scenario's `fitted_model` in the open `TrainingRun`, records its `metrics` (numbers only) and config, registers and describes the version. The packages of the model's own classes are pickled by value, so a version loads without this repository. | diff --git a/models/__main__.py b/models/__main__.py index 66d18f4..135795a 100644 --- a/models/__main__.py +++ b/models/__main__.py @@ -137,11 +137,11 @@ def _report_eda(args: argparse.Namespace) -> int: def _serve_notebooks(args: argparse.Namespace) -> int: - """The runner container's loop (`models.io.notebook_jobs.serve`).""" + """The runner container's loop (`models.reports.notebook_jobs.serve`).""" import os from pathlib import Path - from models.io.notebook_jobs import JOBS_DIR_ENV, serve + from models.reports.notebook_jobs import JOBS_DIR_ENV, serve folder = args.jobs or os.environ.get(JOBS_DIR_ENV) if not folder: diff --git a/models/notebook.py b/models/notebook.py index 1687ede..82c3ce5 100644 --- a/models/notebook.py +++ b/models/notebook.py @@ -1,4 +1,4 @@ -"""What a notebook run from the platform works with (`models.pipelines.experiment`): the cohort build's file it was +"""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 @@ -62,7 +62,7 @@ class Experiment: 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.io.notebook_import`).""" + 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 diff --git a/models/reports/__init__.py b/models/reports/__init__.py index 64e0733..6a0aab5 100644 --- a/models/reports/__init__.py +++ b/models/reports/__init__.py @@ -1,6 +1,9 @@ -"""Reports: notebooks run on a build's file, rendered with Quarto and kept in MLflow, using `models.io` to read the -build and record the run. +"""Reports: notebooks run on a build's file, rendered and kept in MLflow, using `models.io` to read the build and record +the run. - rendering.py a notebook to a self-contained HTML page with Quarto, and what identifies its content - eda.py the exploratory notebook of a scenario, as a report run + rendering.py a notebook to a self-contained HTML page (Quarto or Jupyter), and what identifies its content + eda.py the exploratory notebook of a scenario, as a report run + experiment.py a notebook uploaded to the platform, run on a build's file as a run of `-/experimentos` + notebook_jobs.py where that notebook runs: its job folder and the executors (local, or the runner container) + notebook_import.py what the notebook trained in its own store, copied into the platform's MLflow as DEV """ diff --git a/models/pipelines/experiment.py b/models/reports/experiment.py similarity index 96% rename from models/pipelines/experiment.py rename to models/reports/experiment.py index 3463ce9..84b47bf 100644 --- a/models/pipelines/experiment.py +++ b/models/reports/experiment.py @@ -7,9 +7,9 @@ the page with its outputs (`report/`), what was imported from it (`importado.jso `error.txt`. `start` creates the run; `execute` runs the notebook through an executor and closes the run, so a caller can run it in the background and hand out the run's id at once. -The notebook runs in a job folder of its own (`models.io.notebook_jobs`) with the build's file as its only data and +The notebook runs in a job folder of its own (`models.reports.notebook_jobs`) with the build's file as its only data and an MLflow store of its own; what it trained is copied into the platform's MLflow as DEV afterwards -(`models.io.notebook_import`). Where it runs is the executor's: the runner container, with no network and no view +(`models.reports.notebook_import`). Where it runs is the executor's: the runner container, with no network and no view of the server's files, or this process for development. """ @@ -27,8 +27,8 @@ from mlflow.entities import RunStatus from models.io.lineage import COHORT_BUILD_TAG, COHORT_SOURCE_TAG, COHORT_URL_TAG, DOMAINS, code_version, \ registered_name -from models.io.notebook_import import import_job -from models.io.notebook_jobs import JOB_TIMEOUT, JOB_TIMEOUT_ENV, PAGE_FILE, executor, prepare_job, read_job_file, \ +from models.reports.notebook_import import import_job +from models.reports.notebook_jobs import JOB_TIMEOUT, JOB_TIMEOUT_ENV, PAGE_FILE, executor, prepare_job, read_job_file, \ result_of from models.io.storage import cohort_reference, file_sha256, read_metadata from models.io.tracking import MODEL_PATH, experiment_id, run_file, run_files diff --git a/models/io/notebook_import.py b/models/reports/notebook_import.py similarity index 98% rename from models/io/notebook_import.py rename to models/reports/notebook_import.py index de8e57a..d2a0c93 100644 --- a/models/io/notebook_import.py +++ b/models/reports/notebook_import.py @@ -20,7 +20,7 @@ from mlflow import MlflowClient from mlflow.entities import Metric, Param from models.io.lineage import MODEL_SHA_TAG, code_version, model_sha -from models.io.notebook_jobs import STORE_FILE, JobError, inside, read_job_file +from models.reports.notebook_jobs import STORE_FILE, JobError, inside, read_job_file from models.io.tracking import MODEL_PATH, experiment_id NOTEBOOK_RUN_TAG = "notebook.run_id" diff --git a/models/io/notebook_jobs.py b/models/reports/notebook_jobs.py similarity index 99% rename from models/io/notebook_jobs.py rename to models/reports/notebook_jobs.py index 6d3cb8a..6f6413d 100644 --- a/models/io/notebook_jobs.py +++ b/models/reports/notebook_jobs.py @@ -5,7 +5,7 @@ A job is one folder named after the notebook's run, holding everything the noteb (`model/`) and `job.json`. The notebook runs with that folder as its world: its MLflow is a throwaway store inside it (`mlflow.db`, `artefactos/`), and every database URL is an empty in-memory database. The runner writes the page (`page.html`) and `result.json`; what the notebook trained is imported afterwards by trusted code -(`models.io.notebook_import`). +(`models.reports.notebook_import`). Executors are classes in `EXECUTORS`: `local` runs the job in this process, unsandboxed, for development; `contenedor` leaves it in `NOTEBOOK_JOBS_DIR` for the runner container (`python -m models notebooks serve`), which diff --git a/pyproject.toml b/pyproject.toml index 1a39a44..ae4e725 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ dependencies = [ ] [project.optional-dependencies] -# Running uploaded notebooks without Quarto (models.reports.rendering.JupyterRenderer, models.pipelines.experiment). +# Running uploaded notebooks without Quarto (models.reports.rendering.JupyterRenderer, models.reports.experiment). notebooks = [ "nbclient>=0.10", "nbconvert>=7.16", diff --git a/tests/pipelines/test_experiment.py b/tests/reports/test_experiment.py similarity index 97% rename from tests/pipelines/test_experiment.py rename to tests/reports/test_experiment.py index b5227c5..17d28d3 100644 --- a/tests/pipelines/test_experiment.py +++ b/tests/reports/test_experiment.py @@ -1,6 +1,6 @@ -"""What this module covers: notebooks run on a cohort build's file as MLflow runs (`models.pipelines.experiment`), the -job folder and its executors (`models.io.notebook_jobs`), the import of what a notebook trained -(`models.io.notebook_import`), the API notebooks use inside (`models.notebook`) and the Jupyter renderer: what the +"""What this module covers: notebooks run on a cohort build's file as MLflow runs (`models.reports.experiment`), the +job folder and its executors (`models.reports.notebook_jobs`), the import of what a notebook trained +(`models.reports.notebook_import`), the API notebooks use inside (`models.notebook`) and the Jupyter renderer: what the notebook can reach, what the run keeps, a notebook that fails, a notebook that tries to reach past its job, the runner as the container runs it, and listing and reading runs back. @@ -21,7 +21,7 @@ import pytest from models.io.contracts import COHORT_SCHEMA from models.io.registry import registry -from models.pipelines import experiment +from models.reports import experiment def _notebook(*sources: str) -> dict: @@ -164,7 +164,7 @@ def test_files_a_notebook_links_outside_its_job_are_never_read(jupyter, cohort_f def test_read_job_file_refuses_links_and_paths_out_of_the_job(tmp_path): """Fails if `read_job_file` follows a symbolic link or a `..` path out of the job folder.""" - from models.io.notebook_jobs import JobError, read_job_file + from models.reports.notebook_jobs import JobError, read_job_file job = tmp_path / "job" job.mkdir()