0c1b383da2
GET /cacu/fhir/RiskAssessment?subject=Patient/<pac_id> (DEV only, tagged simulado, a searchset validated with R4B, as TEA's patient resources): the patient's current batch, one prediction per horizon with its outcome, probability and whenPeriod from the risk's index date, the confidence interval in an extension; declared in the CapabilityStatement. GET /cacu/pacientes/<id>/riesgo adds riesgo.fecha_indice. Tests and the risk-band fixture carry the index date. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
52 lines
2.8 KiB
Python
52 lines
2.8 KiB
Python
"""A CaCu patient's individual risk: `GET /cacu/pacientes/<pac_id>/riesgo`.
|
|
|
|
The contract the WebApp's patient record reads (`RiesgoPacienteCacu`): the patient's membership in the surveillance
|
|
cohort's latest build, with its variables, and its risks in the current batch, the newest one with a confirmed
|
|
evaluation, with the index date the model scored her from (`riesgo.fecha_indice`). Patient data, on simulated data: every response carries `simulado: true`. The route is always registered;
|
|
who may see it is an access rule still to be defined. `pac_id` is the internal id; whoever calls is already inside the secure environment. A field is never
|
|
removed or renamed, only added.
|
|
"""
|
|
|
|
from datetime import timezone
|
|
|
|
from flask import Blueprint, jsonify
|
|
|
|
COHORT_URL = "cacu/cohorte_vigilancia"
|
|
COHORT_VARIABLES = ("tipo_censura", "c", "l", "r", "pap_0", "hpv_0")
|
|
|
|
|
|
def patient_risk_blueprint(repositories) -> Blueprint:
|
|
"""The blueprint, to register with `url_prefix="/cacu"`. `repositories` is `persistance.cacu`."""
|
|
blueprint = Blueprint("cacu_patient_risk", __name__)
|
|
|
|
@blueprint.get("/pacientes/<int:pac_id>/riesgo")
|
|
def get_patient_risk(pac_id: int):
|
|
"""A patient's cohort membership and risks in the current batch (DEV only, simulated data)."""
|
|
if repositories.persona_repository.get(pac_id) is None:
|
|
return jsonify({"error": f"no existe la paciente {pac_id}"}), 404
|
|
membership = repositories.cohort_repository.membership(pac_id, COHORT_URL)
|
|
risk = repositories.cohort_repository.current_risk(pac_id)
|
|
return jsonify({"simulado": True, "cohorte": _cohort(membership), "riesgo": _risk(risk)})
|
|
|
|
return blueprint
|
|
|
|
|
|
def _cohort(membership) -> dict | None:
|
|
if membership is None:
|
|
return None
|
|
return {"url": membership.url, "version": membership.version, "fecha_indice": membership.fecha_indice.isoformat(),
|
|
**{name: membership.variables.get(name) for name in COHORT_VARIABLES}}
|
|
|
|
|
|
def _risk(risk) -> dict | None:
|
|
if risk is None:
|
|
return None
|
|
evaluated = risk.evaluado_en if risk.evaluado_en.tzinfo else risk.evaluado_en.replace(tzinfo=timezone.utc)
|
|
return {"lote_id": risk.lote_id, "modelo_nombre": risk.modelo_nombre, "modelo_version": risk.modelo_version,
|
|
# the model's index date for the patient: the horizons count from it (not the cohort's)
|
|
"fecha_indice": risk.fecha_indice.isoformat() if risk.fecha_indice else None,
|
|
"evaluado_en": evaluated.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
|
"estado_lote": risk.estado_lote,
|
|
"horizontes": [{"horizonte_anios": h.horizonte_anios, "probabilidad": h.probabilidad, "ic_bajo": h.ic_bajo,
|
|
"ic_alto": h.ic_alto, "estado": h.estado} for h in risk.horizontes]}
|