FHIR: the patient's RiskAssessment says what the model's number is

The registered model's scenario (`[estimand]` of its `model.toml`) goes in the extension `estimando`, one
`valueString` per key, and its quantity as `method.text`. A model whose scenario can't be read has neither.
This commit is contained in:
2026-11-22 07:47:05 -06:00
parent a04ceb6301
commit f002e24811
2 changed files with 57 additions and 0 deletions
+22
View File
@@ -13,17 +13,23 @@
model gives one, goes in the extension `intervalo-confianza` (`probability[x]` holds a
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
`quantity` as `method.text`. A model whose scenario can't be read has neither.
Not yet: `basis` (the observations the model used) and `mitigation`. Each domain gives its outcomes' codes and labels
(`webapi.fhir.cacu`, `webapi.fhir.cama`).
"""
from datetime import date, datetime, timedelta, timezone
from functools import lru_cache
from webapi.fhir.common import CODE_SYSTEM, STRUCTURE, coding, concept, tagged
OUTCOME = f"{CODE_SYSTEM}/desenlace"
MODEL = f"{CODE_SYSTEM}/modelo"
INTERVAL = f"{STRUCTURE}/intervalo-confianza"
ESTIMAND = f"{STRUCTURE}/estimando"
DAYS_PER_YEAR = 365.25
@@ -46,6 +52,18 @@ def _prediction(horizon, outcome: dict, start: date | None) -> dict:
return item
@lru_cache(maxsize=32)
def estimand(modelo_nombre: str) -> dict[str, str]:
"""The `[estimand]` of the registered model's scenario (`<domain>-<scenario>`), empty if it can't be read."""
domain, _, scenario = modelo_nombre.partition("-")
try:
from models.dags.training import Scenario
return {str(k): str(v) for k, v in Scenario.load(domain, scenario).estimand.items()}
except (ImportError, LookupError, OSError, ValueError):
return {}
def risk_assessment(pac_id: int, risk, simulated: bool, outcomes: dict[str, str], default_outcome: str) -> dict:
"""The patient's current risk (`PatientRisk`) as a `RiskAssessment`; `outcomes` maps the domain's outcome codes to
their labels, and `default_outcome` stands for a batch that doesn't name one."""
@@ -57,4 +75,8 @@ 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 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)