"""CaCu's patients in FHIR R4: a patient's individual risk as a `RiskAssessment` (DEV only, simulated data), built by `webapi.fhir.risk` with CaCu's outcomes. The results the risk was computed from, the cytology sampled on its index date, go in the extension `resultados-indice`: one `valueString` per result it has (`escamosas`, `glandulares`, `vph`), as `citologiacervical` holds them. A risk with no cytology sampled that day has no such extension. """ from webapi.fhir import risk from webapi.fhir.common import STRUCTURE from webapi.fhir.risk import DAYS_PER_YEAR, INTERVAL, MODEL, OUTCOME # noqa: F401 (kept for readers of this module) OUTCOMES = {"cin2_mas": "CIN2+ por biopsia", "cin2_12m": "CIN2+ por biopsia en 12 meses", "cin2": "CIN2+ por biopsia", "cancer": "Cáncer cervicouterino por biopsia"} INDEX_RESULTS = f"{STRUCTURE}/resultados-indice" RESULT_COLUMNS = {"escamosas": "cit_celulas_escamosas", "glandulares": "cit_celulas_glandulares", "vph": "cit_vph_resultado"} def index_results(pac_id: int, fecha_indice) -> dict[str, str]: """The results of the patient's cytology sampled on `fecha_indice` (the one with most of them), by name.""" if fecha_indice is None: return {} from persistance import cacu found = {} for row in cacu.citologia_cervical_repository.list(pac_id=pac_id, cit_fecha_toma=fecha_indice, limit=10): results = {name: str(v) for name, column in RESULT_COLUMNS.items() if (v := getattr(row, column, None))} if len(results) > len(found): found = results return found def risk_assessment(pac_id: int, patient_risk, simulated: bool) -> dict: """The patient's current risk (`PatientRisk`) as a `RiskAssessment`, with the index cytology's results.""" resource = risk.risk_assessment(pac_id, patient_risk, simulated, OUTCOMES, "cin2_mas") if results := index_results(pac_id, patient_risk.fecha_indice): resource.setdefault("extension", []).append( {"url": INDEX_RESULTS, "extension": [{"url": k, "valueString": v} for k, v in results.items()]}) return resource