FHIR: CaCu's risk names the results of its index cytology

The cytology sampled on the risk's index date gives its squamous, glandular and HPV results in the extension
`resultados-indice`, which the record words its sentence with. CaCu's outcome codes `cin2` and `cancer` get labels.
This commit is contained in:
2026-11-22 09:34:49 -06:00
parent b63a253b19
commit b9795c02ca
2 changed files with 61 additions and 4 deletions
+29
View File
@@ -33,3 +33,32 @@ def test_a_model_without_a_scenario_has_no_estimand():
a missing scenario breaks the patient's risk."""
found = risk.risk_assessment(7, _risk("cacu-no_existe"), True, {}, "cin2_mas")
assert "text" not in found["method"] and not any(e.get("url") == risk.ESTIMAND for e in found.get("extension", []))
def test_a_cacu_risk_names_the_results_of_its_index_cytology(monkeypatch):
"""Pins down: CaCu's RiskAssessment carries the squamous, glandular and HPV results of the cytology sampled on the
risk's index date (the most complete if there are several), which the record words its sentence with; a risk
with none that day has no such extension. Fails if the extension's url or keys change, or another day's sample
is read."""
from persistance import cacu as repos
from webapi.fhir import cacu
asked = []
def rows(**filters):
asked.append(filters)
return [SimpleNamespace(cit_celulas_escamosas="LIEBG", cit_celulas_glandulares=None, cit_vph_resultado=None),
SimpleNamespace(cit_celulas_escamosas="LIEBG", cit_celulas_glandulares=None,
cit_vph_resultado="POSITIVO")]
monkeypatch.setattr(repos.citologia_cervical_repository, "list", rows)
found = cacu.risk_assessment(7, _risk("cacu-cin2_pim"), True)
extension = next(e for e in found["extension"] if e["url"] == cacu.INDEX_RESULTS)
assert {e["url"]: e["valueString"] for e in extension["extension"]} == {"escamosas": "LIEBG", "vph": "POSITIVO"}
assert asked[0]["pac_id"] == 7 and asked[0]["cit_fecha_toma"] == date(2025, 1, 1)
monkeypatch.setattr(repos.citologia_cervical_repository, "list", lambda **filters: [])
without = cacu.risk_assessment(7, _risk("cacu-cin2_pim"), True)["extension"]
assert not any(e["url"] == cacu.INDEX_RESULTS for e in without)
+32 -4
View File
@@ -1,12 +1,40 @@
"""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."""
`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"}
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`."""
return risk.risk_assessment(pac_id, patient_risk, simulated, OUTCOMES, "cin2_mas")
"""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