From 0e7cf0acbecb994e8fbbc3e0f6c8d534e7ca913a Mon Sep 17 00:00:00 2001 From: angel Date: Sat, 21 Nov 2026 15:42:15 -0600 Subject: [PATCH] CaMa survival menu: a patient in no drawable scenario gets no default curve --- tests/test_model_analysis.py | 9 +++++++++ webapi/model_analysis.py | 5 +++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_model_analysis.py b/tests/test_model_analysis.py index 6267750..8eb42d2 100644 --- a/tests/test_model_analysis.py +++ b/tests/test_model_analysis.py @@ -125,6 +125,15 @@ def test_the_menu_offers_the_scenarios_with_a_summary_row_and_enough_patients(li assert live_client.get("/cama/modelo/escenarios?paciente=500").status_code == 422 +def test_a_patient_in_no_drawable_scenario_gets_no_default(live_client): + """Pins down: a patient who belongs to none of the scenarios on the menu (patient 150 is outside `< 45 años`) gets + `predeterminado: null` while the menu is still offered. Fails if the default falls back to the first scenario and the dashboard + draws another subgroup's curve on her record.""" + menu = live_client.get("/cama/modelo/escenarios?paciente=150").get_json() + + assert menu["predeterminado"] is None and menu["escenarios"] == [{"nombre": "< 45 años", "n": 100}] + + def test_a_scenarios_figure_comes_with_its_risk_factors_and_the_answer_is_kept(live_client): """Pins down: `POST /cama/modelo/curvas/consulta` returns the figure of the named scenario with its summary row (the risk-factor label) and the version that scored it, and the same scenario a second time is answered without computing again; a scenario with diff --git a/webapi/model_analysis.py b/webapi/model_analysis.py index 814717d..c85ff4d 100644 --- a/webapi/model_analysis.py +++ b/webapi/model_analysis.py @@ -100,7 +100,7 @@ def model_analysis_blueprint(domain: str, scenario: str) -> Blueprint: def scenarios_menu(): """The clinical scenarios the menu offers: those with a row in the version's summary and enough patients over the data ingested so far to draw, in the pipeline's order, with their patients; `?paciente=` also names the one she - belongs to first as the default. 404 while there is no version in production.""" + belongs to first as the default (`null` when she is in none). 404 while there is no version in production.""" from models.cama.survival import live try: @@ -117,7 +117,8 @@ def model_analysis_blueprint(domain: str, scenario: str) -> Blueprint: mine = live.patient_scenarios(labelled, int(paciente)) except live.QueryError as error: return jsonify({"error": str(error)}), 422 - default = next((m["nombre"] for m in menu if m["nombre"] in mine), default) + # Belonging to no drawable scenario leaves no default: showing another subgroup's curve would misdescribe her. + default = next((m["nombre"] for m in menu if m["nombre"] in mine), None) return jsonify({"modelo": {"nombre": f"{domain}-{scenario}", "version": str(version)}, "escenarios": menu, "predeterminado": default}) @blueprint.post("/modelo/curvas/consulta")