Layout: per-domain folders, io, steps and pipelines, and one command line
This commit is contained in:
+1
-1
@@ -49,7 +49,7 @@ 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
|
||||
from models.registry import registry
|
||||
from models.io.registry import registry
|
||||
|
||||
def make(tag: str, *, domain: str = "cacu", scenario: str = "tamizaje", metrics: dict | None = None):
|
||||
version = registry.register(
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
"""What this module covers: the `RiskEstimator` contract in `models.contract` and the two records it
|
||||
"""What this module covers: the `RiskEstimator` contract in `models.io.contract` and the two records it
|
||||
returns, `Risk` and `Provenance`.
|
||||
|
||||
Fixtures: no pytest fixtures. `ConstantEstimator` is the smallest class that meets the contract: one
|
||||
fixed probability per member and horizon. `Incomplete` lacks `provenance`.
|
||||
"""
|
||||
|
||||
from models.contract import Provenance, Risk, RiskEstimator
|
||||
from models.io.contract import Provenance, Risk, RiskEstimator
|
||||
|
||||
|
||||
class ConstantEstimator:
|
||||
@@ -2,7 +2,7 @@ import onnx
|
||||
import onnx.helper
|
||||
import pytest
|
||||
|
||||
from models.export import inspect_onnx_metadata
|
||||
from models.io.export import inspect_onnx_metadata
|
||||
|
||||
|
||||
def _tiny_onnx_bytes() -> bytes:
|
||||
@@ -16,9 +16,9 @@ import pytest
|
||||
from core.governance.enums import Environment
|
||||
from mlflow import MlflowClient
|
||||
|
||||
from models import training
|
||||
from models.registry import MODEL_SHA_TAG, registry
|
||||
from models.training import GIT_COMMIT_ENV, GIT_COMMIT_TAG, GIT_DIRTY_TAG, TrainingRun
|
||||
from models.io import training
|
||||
from models.io.registry import MODEL_SHA_TAG, registry
|
||||
from models.io.training import GIT_COMMIT_ENV, GIT_COMMIT_TAG, GIT_DIRTY_TAG, TrainingRun
|
||||
|
||||
|
||||
class Constant(mlflow.pyfunc.PythonModel):
|
||||
@@ -2,17 +2,11 @@ import json
|
||||
|
||||
import pytest
|
||||
|
||||
from models.base import ScreeningModel
|
||||
from models.prediction import ApplicabilityRejected, Interpretability, PredictionService
|
||||
from models.registry import registry
|
||||
from models.io.prediction import ApplicabilityRejected, Interpretability, PredictionService
|
||||
from models.io.registry import registry
|
||||
|
||||
|
||||
class StubModel(ScreeningModel):
|
||||
domain = "cacu"
|
||||
|
||||
def train(self, data=None):
|
||||
raise NotImplementedError
|
||||
|
||||
class StubModel:
|
||||
def predict(self, data):
|
||||
return {"label": "positivo", "confianza": 0.73}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
from core.governance.enums import ArtifactKind, Environment
|
||||
from models.registry import PromotionBlocked, registry
|
||||
from models.io.registry import PromotionBlocked, registry
|
||||
|
||||
|
||||
def test_dev_version_cannot_be_promoted_and_says_why(governance_db, mlflow_tracking):
|
||||
@@ -56,7 +56,7 @@ def test_retire_and_lineage(production_ready):
|
||||
|
||||
|
||||
def test_invalid_onnx_checkpoint_is_rejected(production_ready):
|
||||
from models.export import inspect_onnx_metadata
|
||||
from models.io.export import inspect_onnx_metadata
|
||||
|
||||
with pytest.raises(ValueError, match="ONNX"):
|
||||
import tempfile
|
||||
@@ -0,0 +1,28 @@
|
||||
"""What this module covers: `resolve_repository`, how `models` finds a domain's Persistance repository by name.
|
||||
|
||||
Fixtures: `cacu_db` (from `conftest.py`), an empty CaCu SQLite database per test.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from models.io.repositories import resolve_repository
|
||||
|
||||
|
||||
def test_resolves_the_domain_repository_and_reads_its_rows(cacu_db):
|
||||
"""A repository found by name reads what was written through it, in batches and with filters. Fails if
|
||||
`resolve_repository` looks in another module or stops returning a working repository."""
|
||||
from datetime import date
|
||||
|
||||
from core.cacu.models import Paciente
|
||||
|
||||
repository = resolve_repository("cacu", "paciente")
|
||||
for i in range(7):
|
||||
repository.create(Paciente(pac_id_hospital=f"M-{i}", pac_sexo="Femenino", pac_fecha_nacimiento=date(1990, 1, 1)))
|
||||
|
||||
assert [r.pac_id_hospital for r in repository.iter_all(batch_size=3)] == [f"M-{i}" for i in range(7)]
|
||||
assert list(repository.iter_all(pac_id_hospital="M-3"))[0].pac_sexo == "Femenino"
|
||||
|
||||
|
||||
def test_unknown_entity_is_a_clear_error():
|
||||
"""An unknown name raises `ValueError` naming it. Fails if the `AttributeError` is no longer translated."""
|
||||
with pytest.raises(ValueError, match="no nope_repository"):
|
||||
resolve_repository("tea", "nope")
|
||||
@@ -1,10 +1,10 @@
|
||||
import pytest
|
||||
|
||||
from core.governance.enums import ArtifactKind, Environment
|
||||
from models.registry import registry
|
||||
from models.io.registry import registry
|
||||
from mlflow import MlflowException
|
||||
|
||||
from models.training import ReadOnlyRepository, TrainingRun
|
||||
from models.io.training import ReadOnlyRepository, TrainingRun
|
||||
|
||||
|
||||
def test_prod_run_requires_a_cohort_build():
|
||||
@@ -1,4 +1,4 @@
|
||||
"""What this module covers: the reading nodes of `models.dags.reading` (`cohort_build`, `members`, `known_until`, `timeline`)
|
||||
"""What this module covers: the reading nodes of `models.pipelines.reading` (`cohort_build`, `members`, `known_until`, `timeline`)
|
||||
run by a Hamilton driver built from that module alone, and their place in the asset graph.
|
||||
|
||||
Fixtures: `repositories`, an in-memory SQLite database with the CaCu tables, one cohort build and a plain
|
||||
@@ -29,7 +29,7 @@ from sqlalchemy import Column, Date, DateTime, Float, Integer, MetaData, String,
|
||||
from sqlalchemy.pool import StaticPool
|
||||
from sqlmodel import Session, create_engine
|
||||
|
||||
from models.dags import reading
|
||||
from models.pipelines import reading
|
||||
|
||||
TABLES = TablasCohorte(CohorteArmado, CohorteMiembro, CohorteExclusion, CohorteVariable, EvaluacionRiesgoLote,
|
||||
EvaluacionRiesgo)
|
||||
@@ -1,27 +0,0 @@
|
||||
"""`models` reads through Persistance's Repository, the same path WebApi
|
||||
uses, against a temporary SQLite database."""
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
def test_dataset_pages_through_every_row(cacu_db):
|
||||
from datetime import date
|
||||
|
||||
from core.cacu.models import Paciente
|
||||
from models.cacu import CaCuModel
|
||||
|
||||
model = CaCuModel()
|
||||
repository = model.repository("paciente")
|
||||
for i in range(7):
|
||||
repository.create(Paciente(pac_id_hospital=f"M-{i}", pac_sexo="Femenino", pac_fecha_nacimiento=date(1990, 1, 1)))
|
||||
|
||||
rows = list(model.dataset("paciente", batch_size=3))
|
||||
assert [r["pac_id_hospital"] for r in rows] == [f"M-{i}" for i in range(7)]
|
||||
assert list(model.dataset("paciente", pac_id_hospital="M-3"))[0]["pac_sexo"] == "Femenino"
|
||||
|
||||
|
||||
def test_unknown_entity_is_a_clear_error():
|
||||
from models.tea import TeaModel
|
||||
|
||||
with pytest.raises(ValueError, match="no nope_repository"):
|
||||
TeaModel().repository("nope")
|
||||
@@ -1,19 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from models.cacu import CaCuModel
|
||||
from models.cama import CaMaModel
|
||||
from models.tea import TeaModel
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cls", [CaCuModel, CaMaModel, TeaModel])
|
||||
def test_train_not_implemented(cls):
|
||||
model = cls()
|
||||
with pytest.raises(NotImplementedError):
|
||||
model.train()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cls", [CaCuModel, CaMaModel, TeaModel])
|
||||
def test_predict_not_implemented(cls):
|
||||
model = cls()
|
||||
with pytest.raises(NotImplementedError):
|
||||
model.predict(data=None)
|
||||
@@ -0,0 +1,33 @@
|
||||
"""What this module covers: the command line in `models/__main__.py`.
|
||||
|
||||
Fixtures: none; the commands declared so far touch no data.
|
||||
"""
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from models.__main__ import main
|
||||
|
||||
|
||||
def test_help_lists_the_subcommands():
|
||||
"""`python -m models --help` runs as a module and lists `cohorts`. Fails if the entry point breaks or the
|
||||
subcommand stops being registered."""
|
||||
result = subprocess.run([sys.executable, "-m", "models", "--help"], capture_output=True, text=True)
|
||||
assert result.returncode == 0
|
||||
assert "cohorts" in result.stdout
|
||||
|
||||
|
||||
def test_a_command_is_required():
|
||||
"""Without a command the parser exits with an error. Fails if `required=True` is dropped, which would make
|
||||
a bare call look successful."""
|
||||
with pytest.raises(SystemExit) as exit_info:
|
||||
main([])
|
||||
assert exit_info.value.code == 2
|
||||
|
||||
|
||||
def test_a_command_without_actions_does_not_report_success(capsys):
|
||||
"""`cohorts` has no actions yet, so it shows its help and returns an error code. Fails if it returns 0, which
|
||||
would let a script believe it had run."""
|
||||
assert main(["cohorts"]) == 2
|
||||
assert "cohorts" in capsys.readouterr().out
|
||||
Reference in New Issue
Block a user