models train: train a model from the command line
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
"""What this module covers: `models.cacu.cin2_logistic`: how the dummy model computes its outcome and input, its temporal
|
||||
split, and that training on a build's file registers a version with its lineage.
|
||||
split, that training on a build's file registers a version with its lineage, and the `models train` command over it.
|
||||
|
||||
Fixtures: `cohort_frame`, a long frame of 20 members whose index dates rise with `pac_id`; `cohort_file`, that frame as a
|
||||
build's Parquet file with its lineage; `mlflow_tracking` (conftest), a SQLite-backed MLflow.
|
||||
build's Parquet file with its lineage; `exported`, builds 7 and 12 under a `COHORTS_DIR`; `mlflow_tracking` (conftest), a
|
||||
SQLite-backed MLflow.
|
||||
"""
|
||||
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
@@ -13,6 +14,7 @@ import pyarrow as pa
|
||||
import pyarrow.parquet as pq
|
||||
import pytest
|
||||
|
||||
from models.__main__ import main
|
||||
from models.cacu.cin2_logistic import model
|
||||
from models.cacu.cin2_logistic.train import train
|
||||
from models.io.cohorts import SCHEMA
|
||||
@@ -38,13 +40,27 @@ def cohort_frame():
|
||||
return pd.DataFrame(rows, columns=SCHEMA.names)
|
||||
|
||||
|
||||
def _write(frame, path, armado_id):
|
||||
table = pa.Table.from_pandas(frame, schema=SCHEMA, preserve_index=False)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
pq.write_table(table.replace_schema_metadata({"armado_id": str(armado_id), "url": "cacu/cohorte_vigilancia"}), path)
|
||||
return path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cohort_file(cohort_frame, tmp_path):
|
||||
"""The frame as build 7's file."""
|
||||
table = pa.Table.from_pandas(cohort_frame, schema=SCHEMA, preserve_index=False)
|
||||
path = tmp_path / "7.parquet"
|
||||
pq.write_table(table.replace_schema_metadata({"armado_id": "7", "url": "cacu/cohorte_vigilancia"}), path)
|
||||
return path
|
||||
return _write(cohort_frame, tmp_path / "7.parquet", 7)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def exported(cohort_frame, tmp_path, monkeypatch):
|
||||
"""Builds 7 and 12 under `COHORTS_DIR`, in the folder the export writes to."""
|
||||
folder = tmp_path / "cohorts" / "cacu" / "cohorte_vigilancia"
|
||||
_write(cohort_frame, folder / "7.parquet", 7)
|
||||
_write(cohort_frame, folder / "12.parquet", 12)
|
||||
monkeypatch.setenv("COHORTS_DIR", str(tmp_path / "cohorts"))
|
||||
return folder
|
||||
|
||||
|
||||
def test_prepare_computes_y_and_days_from_bethesda_only(cohort_frame):
|
||||
@@ -91,3 +107,42 @@ def test_training_registers_a_version_with_its_lineage(cohort_file, cohort_frame
|
||||
|
||||
probabilities = mlflow.pyfunc.load_model(resolved.model_uri).predict(cohort_frame)
|
||||
assert len(probabilities) == 20 and probabilities.between(0, 1).all()
|
||||
|
||||
|
||||
def test_the_command_trains_on_the_latest_exported_build(exported, mlflow_tracking, capsys):
|
||||
"""Pins down: with no `--armado`, the command uses the highest build number, not the last by name, and registers a
|
||||
DEV version. Fails if builds sort as text (7 after 12), or the default environment changes."""
|
||||
assert main(["train", "cacu", "cin2_logistic"]) == 0
|
||||
|
||||
assert "version 1" in capsys.readouterr().out
|
||||
stored = registry.get("cacu", "cin2_logistic", "1")
|
||||
assert stored.tags[ARMADO_TAG] == "12" and stored.tags["environment"] == "DEV"
|
||||
|
||||
|
||||
def test_the_command_trains_on_the_build_it_is_given_as_prod(exported, mlflow_tracking):
|
||||
"""Pins down: `--armado` chooses the file and `--environment prod` tags the version PROD. Fails if either option
|
||||
is ignored."""
|
||||
assert main(["train", "cacu", "cin2_logistic", "--armado", "7", "--environment", "prod"]) == 0
|
||||
|
||||
stored = registry.get("cacu", "cin2_logistic", "1")
|
||||
assert stored.tags[ARMADO_TAG] == "7" and stored.tags["environment"] == "PROD"
|
||||
|
||||
|
||||
def test_prod_without_a_build_is_refused(capsys):
|
||||
"""Pins down: a prod run has to name its build (ML-03), and it's refused before anything loads. Fails if prod falls
|
||||
back to the latest file."""
|
||||
with pytest.raises(SystemExit) as stopped:
|
||||
main(["train", "cacu", "cin2_logistic", "--environment", "prod"])
|
||||
|
||||
assert stopped.value.code == 2 and "--armado" in capsys.readouterr().err
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arguments, message", [
|
||||
(["cacu", "ninguno"], "no such model: cacu/ninguno"),
|
||||
(["cacu", "cin2_logistic", "--armado", "99"], "export it"),
|
||||
])
|
||||
def test_what_is_missing_is_named(exported, arguments, message, capsys):
|
||||
"""Pins down: an unknown model or a build with no file ends with exit 1 and says which, with no traceback and no
|
||||
version. Fails if the command lets the import error through, or trains on another file."""
|
||||
assert main(["train", *arguments]) == 1
|
||||
assert message in capsys.readouterr().err
|
||||
|
||||
Reference in New Issue
Block a user