Training, estimating and running on dbt cohort builds: runs and versions record the cohort file they used

This commit is contained in:
2026-11-05 18:21:06 -06:00
parent 9808701c46
commit 3853cc5522
7 changed files with 122 additions and 29 deletions
+50 -2
View File
@@ -136,7 +136,7 @@ def test_prod_without_a_build_is_refused(capsys):
with pytest.raises(SystemExit) as stopped:
main(["train", "cacu", "cin2_logistic", "--environment", "prod"])
assert stopped.value.code == 2 and "--armado" in capsys.readouterr().err
assert stopped.value.code == 2 and "--build" in capsys.readouterr().err
@pytest.mark.parametrize("arguments, message", [
@@ -166,7 +166,7 @@ def test_estimate_scores_each_member_with_the_production_version(exported, mlflo
assert list(risks.columns) == ["pac_id", "fecha_indice", "probabilidad"]
assert sorted(risks["pac_id"]) == list(range(1, 21)) and risks["probabilidad"].between(0, 1).all()
metadata = pq.read_metadata(output).metadata
assert metadata[b"modelo_version"] == b"1" and metadata[b"armado_id"] == b"12"
assert metadata[b"modelo_version"] == b"1" and metadata[b"cohort.build"] == b"12" and metadata[b"cohort.source"] == b"kit"
def test_estimate_uses_the_version_it_is_given(exported, mlflow_tracking, tmp_path):
@@ -291,3 +291,51 @@ def test_export_runs_first_and_stops_the_rest_if_it_fails(exported, mlflow_track
assert calls == [("cacu", "cacu/cohorte_vigilancia", 7)]
assert not _versions()
@pytest.fixture
def dbt_exported(cohort_frame, tmp_path, monkeypatch):
"""Builds 1 and 2 of dbt's rules under `COHORTS_DIR`, in the `dbt/` folder the export writes to; build 2 lacks member 1."""
folder = tmp_path / "cohorts" / "cacu" / "cohorte_vigilancia" / "dbt"
for number, frame in ((1, cohort_frame), (2, cohort_frame[cohort_frame["pac_id"] != 1])):
path = folder / f"{number}.parquet"
path.parent.mkdir(parents=True, exist_ok=True)
table = pa.Table.from_pandas(frame, schema=SCHEMA, preserve_index=False)
pq.write_table(table.replace_schema_metadata({"build": str(number), "producer": "dbt", "url": "cacu/cohorte_vigilancia"}), path)
monkeypatch.setenv("COHORTS_DIR", str(tmp_path / "cohorts"))
return folder
def test_training_on_a_dbt_build_records_its_file_not_a_kit_build(dbt_exported, mlflow_tracking, capsys):
"""Pins down: `--source dbt` trains on the latest dbt build, and the version points to that file (cohort, source,
number, hash) with no kit `armado_id`, which would name a kit build that doesn't exist. Fails if the dbt folder is
ignored, or the version loses its cohort file."""
assert main(["train", "cacu", "cin2_logistic", "--source", "dbt"]) == 0
tags = registry.get("cacu", "cin2_logistic", "1").tags
assert ARMADO_TAG not in tags
assert (tags["cohort.source"], tags["cohort.build"], tags["cohort.url"]) == ("dbt", "2", "cacu/cohorte_vigilancia")
assert len(tags["cohort.file_sha256"]) == 64 and "dbt/2.parquet" in capsys.readouterr().out
def test_a_prod_run_on_dbt_names_its_build_and_estimates_from_it(dbt_exported, mlflow_tracking, tmp_path):
"""Pins down: a PROD version from dbt build 1 is linked to that build (ML-03) without a kit build, and estimating
it writes the same file reference. Fails if PROD still demands a kit `armado_id`, or `--build` stops choosing the
dbt file."""
output = tmp_path / "output.parquet"
assert main(["run", "cacu", "cin2_logistic", "--source", "dbt", "--build", "1", "--environment", "prod",
"--train", "--promote", "--estimate", "--output", str(output)]) == 0
tags = registry.get("cacu", "cin2_logistic", "1").tags
assert tags["environment"] == "PROD" and tags["cohort.build"] == "1"
metadata = pq.read_metadata(output).metadata
assert metadata[b"cohort.source"] == b"dbt" and metadata[b"cohort.build"] == b"1"
assert metadata[b"cohort.file_sha256"].decode() == tags["cohort.file_sha256"]
def test_a_missing_dbt_build_says_how_to_export_it(dbt_exported, capsys):
"""Pins down: a dbt build that isn't exported ends with exit 1 and the dbt export command. Fails if the kit's
command is suggested or another build is used."""
assert main(["train", "cacu", "cin2_logistic", "--source", "dbt", "--build", "9"]) == 1
assert "--source dbt" in capsys.readouterr().err