Add governance and TEA preview REST endpoints
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.
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
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"]
|
||||
@@ -345,11 +345,19 @@ def test_tea_from_the_hospital_to_indicators(api, scored_hospital, monkeypatch):
|
||||
expected = summary["body"]
|
||||
assert (len(listed), sum(e["analysed"] for e in listed), sum(bool(e["valid"]) for e in listed)
|
||||
) == (expected["eegs"], expected["analysed"], expected["valid"]) # fmt: skip
|
||||
actual_summary = ok(api.get("/tea/eegs/summary"))
|
||||
assert actual_summary == {
|
||||
key: count for key, count in expected.items() if key not in ("fhir_eegs", "fhir_metrics")
|
||||
}
|
||||
one = next(e for e in listed if e["valid"])
|
||||
shown = ok(api.get(f"/tea/eegs/{one['id']}"))
|
||||
assert shown["valid"] and shown["metrics"] and all(m["value"] is not None
|
||||
for m in shown["metrics"]) # fmt: skip
|
||||
|
||||
assert shown["checks"] and shown["regions"]
|
||||
curve = ok(api.get("/models/tea/eeg-posterior-rhythm/versions/1/curve"))
|
||||
assert [row["age_months"] for row in curve] == list(range(16, 37))
|
||||
|
||||
_preview_matches_the_analysis_and_leaves_bronze_unchanged(scored_hospital, monkeypatch)
|
||||
_reanalysis_by_id_in_local_mode_matches_sync(api, scored_hospital)
|
||||
_cancel_during_analysis_leaves_bronze_and_gold_untouched(api, scored_hospital)
|
||||
|
||||
Reference in New Issue
Block a user