80b3d23b75
Use the models router for curves and training inputs, the EEG router for summary, and a separate preview router. Keep the preview result typed with the existing persistence model and declare multipart binary schemas for generated Blob parameters.
110 lines
4.7 KiB
Python
110 lines
4.7 KiB
Python
import hashlib
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from test_bundles import NAME, _post, _register, _run, _trust
|
|
|
|
from models.tea.jobs import Baseline
|
|
from persistance import registry
|
|
|
|
VERSIONS = "/models/tea/curve_x/versions"
|
|
|
|
|
|
def test_version_lifecycle(api, platform, monkeypatch):
|
|
monkeypatch.setenv("MLFLOW_TRACKING_URI", platform.env["MLFLOW_TRACKING_URI"])
|
|
monkeypatch.chdir(platform.env["ISSSTELEON_DATA"])
|
|
for version in (1, 2):
|
|
assert registry.register(NAME, Baseline("x", {"value": {}}), {}, {}, {}) == version
|
|
path = f"{VERSIONS}/1"
|
|
response = api.get(f"{path}/checks")
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {
|
|
"version": 1,
|
|
"status": "candidate",
|
|
"checks": [{"report": report, "present": False} for report in registry.REPORTS],
|
|
"promotable": False,
|
|
}
|
|
assert api.get(f"{VERSIONS}/99/checks").status_code == 404
|
|
for version in (1, 2):
|
|
for report in registry.REPORTS:
|
|
registry.add_report(NAME, version, report, "report.json", b"{}")
|
|
assert api.get(f"{path}/checks").json()["promotable"]
|
|
assert api.post(f"{path}/promote").json() == {"champion": 1}
|
|
assert api.get(f"{path}/checks").json()["status"] == "champion"
|
|
refused = api.post(f"{path}/retire")
|
|
assert refused.status_code == 409
|
|
assert (
|
|
refused.json()["detail"] == "the champion cannot be retired; promote another version first"
|
|
)
|
|
assert api.post(f"{VERSIONS}/2/promote").json() == {"champion": 2}
|
|
assert api.get(f"{path}/checks").json()["status"] == "candidate"
|
|
assert api.post(f"{path}/retire").json() == {"status": "retired"}
|
|
assert api.post(f"{path}/promote").status_code == 409
|
|
checks = api.get(f"{path}/checks").json()
|
|
assert checks["status"] == "retired" and not checks["promotable"]
|
|
assert [v["status"] for v in api.get(VERSIONS).json()] == ["retired", "champion"]
|
|
|
|
|
|
def test_imported_lineage(api, platform, fresh_api, fresh_platform, monkeypatch):
|
|
_register(platform, monkeypatch)
|
|
bundle_exported = api.get("/models/tea/export")
|
|
assert bundle_exported.status_code == 200, bundle_exported.text
|
|
sha = hashlib.sha256(bundle_exported.content).hexdigest()
|
|
_trust(fresh_platform, sha, monkeypatch)
|
|
run = _run(fresh_api, _post(fresh_api, bundle_exported.content, sha))
|
|
assert run["state"] == "succeeded", run
|
|
response = fresh_api.get(f"{VERSIONS}/1/lineage")
|
|
assert response.status_code == 200, response.text
|
|
lineage = response.json()
|
|
assert lineage["version"] == 1 and lineage["created"] > 0 and lineage["run_id"]
|
|
assert lineage["params"] == {}
|
|
assert lineage["metrics"] == {"n_reference": 10}
|
|
assert all(not tag.startswith("mlflow.") for tag in lineage["tags"])
|
|
assert lineage["imported"] == {"sha256": sha, "source_name": NAME, "source_version": "1"}
|
|
|
|
|
|
@pytest.fixture
|
|
def platform(platform):
|
|
platform.env["TEA_REFERENCE_ROOT"] = str(Path(platform.env["ISSSTELEON_DATA"]) / "references")
|
|
return platform
|
|
|
|
|
|
def test_training_inputs(api, platform):
|
|
root = Path(platform.env["TEA_REFERENCE_ROOT"])
|
|
path = "/models/tea/curve_x/inputs"
|
|
assert api.get(path).json()["sets"] == []
|
|
assert api.get("/models/tea/ados2_association/inputs").status_code == 404
|
|
response = api.post(
|
|
f"{path}/cohort/files",
|
|
files=[("files", ("one.EDF", b"edf")), ("files", ("edades.csv", b"ages"))],
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
cohort = {"name": "cohort", "edf_files": 1, "has_ages": True, "bytes": 7}
|
|
assert response.json() == cohort
|
|
assert api.get(path).json()["sets"] == [cohort]
|
|
for filename in ("bad.txt", "../x"):
|
|
response = api.post(
|
|
f"{path}/cohort/files",
|
|
files=[("files", ("two.edf", b"edf")), ("files", (filename, b"bad"))],
|
|
)
|
|
assert response.status_code == 422, response.text
|
|
assert (
|
|
api.post(f"{path}/bad%20set/files", files={"files": ("one.edf", b"edf")}).status_code == 422
|
|
)
|
|
assert {p.name for p in (root / "cohort").iterdir()} == {"one.EDF", "edades.csv"}
|
|
|
|
|
|
def test_preview(api, tmp_path, monkeypatch):
|
|
monkeypatch.syspath_prepend(str(Path(__file__).resolve().parents[2] / "models" / "tests"))
|
|
from edf_files import small_edf
|
|
|
|
signal = small_edf(tmp_path / "small.edf")
|
|
run = _run(api, api.post("/tea/preview", files={"edf": ("small.edf", signal)}))
|
|
assert run["state"] == "succeeded", run
|
|
response = api.get(f"/tea/preview/{run['id']}")
|
|
assert response.status_code == 200, response.text
|
|
preview = response.json()
|
|
assert preview["state"] == "succeeded"
|
|
outputs = api.get(f"/runs/{run['id']}", params={"outputs": "true"}).json()["outputs"]
|
|
assert preview["result"] == outputs["preview_study.result"]
|