FHIR: one RiskAssessment per outcome, and one with no prediction for a model that didn't score her

The patient's Bundle holds the current batch of each outcome (`CohortRepository.current_risks`), so a CIN2+ and a
cancer model are both shown. For a patient with some risk, each current model of another outcome that didn't score
her (`current_batches`) gives a RiskAssessment with no prediction and the extension `sin-estimacion` (its outcome and
why it may be so), so the record can tell "no model" from "not estimated for her".
This commit is contained in:
2026-11-22 09:34:49 -06:00
parent f002e24811
commit b63a253b19
3 changed files with 80 additions and 3 deletions
+10 -2
View File
@@ -24,6 +24,7 @@ from werkzeug.exceptions import HTTPException
from webapi.fhir.common import FHIR_VERSION, canonical, entry, fhir, outcome, page, searchset, simulated_data
from webapi.fhir.measures import fhir_id, labels, measure_report, measure_resource, measure_url
from webapi.fhir.risk import not_scored
from webapi.fhir.terminology import code_systems, metric_value_set
from webapi.kpis import ANSWERS, _date, _datetime, _last_load, definitions_of, scoring_of
from webapi.metrics import Question, UndecidedRule
@@ -198,8 +199,15 @@ def _risk_routes(blueprint: Blueprint, repositories, project: str) -> None:
except ValueError as error:
return outcome(400, "invalid", str(error))
pac_id = int(reference)
risk = repositories.cohort_repository.current_risk(pac_id)
found = [] if risk is None else [domain.risk_assessment(pac_id, risk, simulated_data())]
# one RiskAssessment per outcome: the current batch of each (a CIN2+ and a cancer model, say), and, for a
# patient with some risk, one with no prediction for each current model of another outcome that didn't
# score her
risks = repositories.cohort_repository.current_risks(pac_id)
found = [domain.risk_assessment(pac_id, r, simulated_data()) for r in risks]
if risks:
scored = {r.desenlace for r in risks}
found += [not_scored(domain.risk_assessment(pac_id, b, simulated_data()), b.desenlace)
for b in repositories.cohort_repository.current_batches() if b.desenlace not in scored]
return fhir(searchset([entry(r) for r in found[offset:offset + count]], len(found), count, offset))
+18 -1
View File
@@ -14,9 +14,12 @@
decimal or a range, not both).
What the number is, from the model's scenario (`[estimand]` of its `model.toml`), goes in the extension `estimando`:
one `valueString` per key (`quantity`, `description`, `outcome`, `index_date`, `horizons_unit`, `interval`), and its
one `valueString` per key (`quantity`, `description`, `outcome`, `index_date`, `horizons_unit`), and its
`quantity` as `method.text`. A model whose scenario can't be read has neither.
A current model of an outcome that didn't score the patient gives a `RiskAssessment` with no `prediction` and the
extension `sin-estimacion` (`not_scored`), so her record can tell "no model" from "not estimated for her".
Not yet: `basis` (the observations the model used) and `mitigation`. Each domain gives its outcomes' codes and labels
(`webapi.fhir.cacu`, `webapi.fhir.cama`).
"""
@@ -30,6 +33,9 @@ OUTCOME = f"{CODE_SYSTEM}/desenlace"
MODEL = f"{CODE_SYSTEM}/modelo"
INTERVAL = f"{STRUCTURE}/intervalo-confianza"
ESTIMAND = f"{STRUCTURE}/estimando"
NOT_SCORED = f"{STRUCTURE}/sin-estimacion"
NOT_SCORED_TEXT = ("el modelo vigente de este desenlace no estima a la paciente: queda fuera de su población (su "
"índice o sus reglas) o su combinación de resultados no estuvo en su entrenamiento")
DAYS_PER_YEAR = 365.25
@@ -75,8 +81,19 @@ def risk_assessment(pac_id: int, risk, simulated: bool, outcomes: dict[str, str]
"method": {"coding": [{**coding(MODEL, risk.modelo_nombre), "version": str(risk.modelo_version)}]},
"prediction": [_prediction(h, outcome, risk.fecha_indice) for h in risk.horizontes],
}
if not risk.horizontes: # a current model of the outcome that didn't score her (`not_scored`)
del resource["prediction"]
if found := estimand(risk.modelo_nombre):
resource["method"]["text"] = found.get("quantity", "")
resource["extension"] = [{"url": ESTIMAND, "extension": [{"url": k, "valueString": v}
for k, v in found.items()]}]
return tagged(resource, simulated)
def not_scored(resource: dict, desenlace: str | None) -> dict:
"""A `RiskAssessment` of a model that didn't score the patient (a batch with no horizons of hers), marked with
the extension `sin-estimacion`: its outcome (`desenlace`, since there is no prediction to carry it) and why it
may be so (`motivo`)."""
resource.setdefault("extension", []).append({"url": NOT_SCORED, "extension": [
{"url": "desenlace", "valueCode": desenlace or ""}, {"url": "motivo", "valueString": NOT_SCORED_TEXT}]})
return resource