Recurrence figures for a patient; governance trains scenarios with external data; lifelines in the lock

This commit is contained in:
rockdrigo
2026-11-21 13:34:56 -06:00
parent a53179aa2e
commit 2c4bfa5691
6 changed files with 153 additions and 6 deletions
+53
View File
@@ -139,3 +139,56 @@ def test_the_scenarios_of_a_patient_are_listed_and_a_stranger_is_refused(live_cl
assert live_client.get("/cama/modelo/escenarios?paciente=8").get_json()["escenarios"] == ["Toda la cohorte"]
assert live_client.get("/cama/modelo/escenarios?paciente=404").status_code == 422
assert live_client.get("/cama/modelo/escenarios").status_code == 422
@pytest.fixture
def recurrence_client(monkeypatch):
"""The recurrence route on a bare app, with the registry's production version, its recurrence record and the labelled members
replaced by stand-ins: one patient aged 40 with grade 3 and an estrogen-negative tumor."""
import pandas as pd
from flask import Flask
from models.io import registry as registry_module
from webapi import model_analysis
record = {"horizons_months": [12, 36, 60], "escenarios": [
{"cohorte": "metabric", "escenario": "< 45 años", "condiciones": {"Edad": ["<45"]}, "n": 100},
{"cohorte": "metabric", "escenario": "RE− · grado 3", "condiciones": {"Receptor_estrogeno": ["Negative"], "Grado_histologico": ["3"]}, "n": 300},
{"cohorte": "metabric", "escenario": "Ganglios 4+", "condiciones": {"Ganglios_positivos": ["4+"]}, "n": 80},
{"cohorte": "scanb", "escenario": "Grado 3", "condiciones": {"Grado_histologico": ["3"]}, "n": 900}]}
labelled = pd.DataFrame({"pac_id": [7], "age_years": [40.0], "size_mm": [None], "grade": ["G3"], "er": ["NEGATIVO"], "pr": ["NEGATIVO"], "her2": ["H2"]})
holder = SimpleNamespace(production="2")
def resolve(domain, scenario, version=None):
if holder.production is None:
raise LookupError("no hay una versión en producción")
return SimpleNamespace(version=holder.production, run_id="run-2")
monkeypatch.setattr(registry_module.registry, "resolve", resolve)
monkeypatch.setattr(registry_module.registry, "artifact", lambda d, s, v, p: ("recurrencia.json", json.dumps(record).encode()))
monkeypatch.setattr(model_analysis, "_live_context", lambda d, s: ("spec", "model", labelled, "3"))
app = Flask(__name__)
app.register_blueprint(model_analysis.recurrence_blueprint(), url_prefix="/cama")
with app.test_client() as client:
client.holder = holder
yield client
def test_a_patient_gets_the_recurrence_scenarios_her_covariates_meet(recurrence_client):
"""Pins down: `GET /cama/modelo/recurrencia?paciente=7` returns the scenarios whose every condition her age, grade and
receptors meet (one that needs the nodes, which the hospital does not report, is left out), the larger first within a
cohort, with the version that published them. Fails if a scenario she does not meet is returned, a missing covariate counts
as met, or the version is not named."""
body = recurrence_client.get("/cama/modelo/recurrencia?paciente=7").get_json()
assert [(s["cohorte"], s["escenario"]) for s in body["escenarios"]] == [("metabric", "RE− · grado 3"), ("metabric", "< 45 años"), ("scanb", "Grado 3")]
assert body["modelo"] == {"nombre": "cama-recurrence", "version": "2"} and body["horizontes"] == [12, 36, 60]
def test_without_a_recurrence_version_or_for_a_stranger_it_says_so(recurrence_client):
"""Pins down: no recurrence version in production is a 404, a patient outside the cohort or no patient is a 422 with a reason.
Fails if either becomes a 200 with nothing or a 500."""
assert recurrence_client.get("/cama/modelo/recurrencia?paciente=99").status_code == 422
assert recurrence_client.get("/cama/modelo/recurrencia").status_code == 422
recurrence_client.holder.production = None
assert recurrence_client.get("/cama/modelo/recurrencia?paciente=7").status_code == 404
+15
View File
@@ -50,6 +50,7 @@ def test_the_catalog_lists_the_trainable_scenarios_and_not_the_pipeline_settings
assert ("cama", "survival") in entries and ("cacu", "cin2_logistic") in entries
assert entries[("tea", "eeg")]["kind"] == "referencia" and entries[("cama", "survival")]["kind"] == "cohorte"
assert ("cama", "seer_survival") not in entries
assert entries[("cama", "recurrence")]["kind"] == "referencia" # trained on its external files, not on a dbt cohort
@pytest.mark.parametrize("code,estado", [
@@ -115,3 +116,17 @@ def test_tea_trains_on_a_local_reference_folder_when_one_is_configured(monkeypat
assert tea[tea.index("--reference-dir") + 1] == "/referencia" and tea[tea.index("--reference-ages") + 1] == "/referencia/edades.csv"
assert "--manifest" not in tea and "--reference-dir" not in cama
def test_a_scenario_with_external_data_trains_without_a_dbt_export_or_manifest(monkeypatch):
"""Pins down: CaMa's recurrence scenario, whose training data are files, is started with no `--export`, `--manifest` or dbt
manifest, while CaMa's survival scenario still is. Fails if an external scenario is refused for want of a manifest or run
through the cohort export, which would train it on the wrong data."""
monkeypatch.delenv("DBT_MANIFEST", raising=False)
recurrence = training_jobs._command("cama", "recurrence")
monkeypatch.setenv("DBT_MANIFEST", "m.json")
survival = training_jobs._command("cama", "survival")
assert "--export" not in recurrence and "--manifest" not in recurrence
assert "--export" in survival and survival[survival.index("--manifest") + 1] == "m.json"