Records list CaMa treatments and recurrences and draw their clinical scenarios
ci / python (push) Successful in 43m53s
ci / web (push) Failing after 2m47s

This commit is contained in:
Roberto Treviño
2026-11-29 06:43:17 -06:00
parent 944f92e5d3
commit a176b5a19a
8 changed files with 75 additions and 11 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ WINDOWS = cama.FOLLOW_UP_WINDOWS
SORTS = ("-recent",)
DEFAULT_HORIZON = None
STATUS_SYSTEM = "cama-follow-up-status"
PROCEDURES: tuple[str, ...] = ()
PROCEDURES = ("treatment",) # by kind (persistance.fhir: CaMa's procedure read)
REPORT_FACTS: dict[str, str] = {}
URGENT = ["5", "6"] # BI-RADS whose pending biopsy is high priority
+17 -5
View File
@@ -84,6 +84,7 @@ def _catalog_systems() -> dict[str, tuple[str, dict[str, str]]]:
CODE_SYSTEMS |= _catalog_systems()
CODE_SYSTEMS["cama-procedure"] = ("Procedimientos de CaMa", CODE_SYSTEMS["breast-treatment"][1])
# What the catalog names: its studies, and every field as `<study>.<field>`.
CODE_SYSTEMS["study"] = ("Estudios", {n: s.title for n, s in catalog.studies().items()})
CODE_SYSTEMS["catalog-field"] = ("Campos de los estudios", {
@@ -302,14 +303,24 @@ def instance(study: str, row: dict, report_extensions: Iterable[dict] = ()) -> l
def condition(row: dict) -> Condition:
"""What a clinical note diagnosed (`silver.clinical_note`), by ICD-10 or as written."""
"""What a clinical note diagnosed (`silver.clinical_note`), by ICD-10 or as written; or
a study of the catalog that is a diagnosis (`study`: a breast recurrence), by its name
and its coded field (`code`: where it recurred)."""
person, day = row["person_id"], _day(row["note_date"])
code: dict = {"text": row.get("diagnosis_text") or row.get("icd10")}
if row.get("icd10"):
code["coding"] = [{"system": ICD10, "code": row["icd10"]}]
if study := row.get("study"):
code = concept("study", study)
system = next((f.system for f in catalog.studies()[study].fields.values()
if f.type == "code"), None) # fmt: skip
if system and row.get("code"):
where = coding(system, row["code"])
code["coding"].append(where)
code["text"] += f" ({where.get('display', row['code']).lower()})"
found: dict = {
"resourceType": "Condition",
"id": _id("condition", person, day, row.get("specialty")),
"id": _id("condition", person, day, row.get("specialty") or study),
"category": [{"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/condition-category",
"code": "encounter-diagnosis"}]}],
@@ -322,14 +333,15 @@ def condition(row: dict) -> Condition:
return Condition.model_validate(found)
def procedure(row: dict) -> Procedure:
"""A procedure the timeline inferred (`gold.cacu_events`: colposcopy, treatment)."""
def procedure(row: dict, domain: str = "cacu") -> Procedure:
"""A procedure the timeline inferred (`gold.<domain>_events`; CaCu: colposcopy,
treatment; CaMa: each treatment by its kind), coded in `<domain>-procedure`."""
person, day = row["person_id"], _day(row["date"])
return Procedure.model_validate({
"resourceType": "Procedure",
"id": _id("procedure", row["type"], person, day),
"status": "completed",
"code": concept("cacu-procedure", row["type"]),
"code": concept(f"{domain}-procedure", row["type"]),
"subject": subject(person),
"performedDateTime": day,
}) # fmt: skip
@@ -1,6 +1,9 @@
{# The latest risk batch of each CaCu model: from its champion version, one row per
patient and horizon (years from her index date; 0 is the index visit itself). #}
patient and horizon (years from her index date; 0 is the index visit itself), with the
report's clinical scenarios she is in (survival's). #}
select model, version, person_id, index_date, horizon_years, probability, ci_low, ci_high,
{% if staged_has_column('model_scores', 'scenarios') -%} scenarios
{%- else -%} cast(null as {{ dbt.type_string() }}) as scenarios {%- endif %},
scored_at
from {{ source('bronze', 'model_scores') }}
where model like 'cacu.%'
+21 -1
View File
@@ -45,7 +45,7 @@ COMMON = {
""",
"risk": """
select model, version, person_id, index_date, horizon_years, probability, ci_low,
ci_high, scored_at
ci_high, scenarios, scored_at
from gold.{domain}_risk
where person_id in :persons
order by person_id, model, index_date, horizon_years
@@ -102,6 +102,26 @@ READS = {
"cama": {
"person": CAMA_PERSONS + " where p.person_id = :id",
"persons": CAMA_PERSONS + " order by p.person_id",
# Her treatments, by kind (the timeline's `treatment` events carry it as their code).
"procedure": """
select distinct person_id, date, value_code as type from gold.cama_events
where person_id in :persons and date <= :cutoff and type in :types
and value_code is not null
order by person_id, date, type
""",
# What clinical notes diagnosed, and each recurrence (a study of the catalog).
"condition": """
select person_id, note_date, specialty, icd10, reason, diagnosis_text,
null as study, null as code
from silver.clinical_note
where person_id in :persons and note_date <= :cutoff
and (icd10 is not null or diagnosis_text is not null)
union all
select person_id, diagnosis_date, null, null, null, null, 'breast_recurrence', site
from silver.breast_recurrence
where person_id in :persons and diagnosis_date <= :cutoff
order by person_id, note_date
""",
# The list's candidates: each reported mammogram by the cut-off and its biopsies
# (core.cama.follow_up picks the latest per patient and places her in the window).
"follow_up": """
+1 -1
Submodule WebApi updated: 29186909cc...3a18af9516
+1 -1
Submodule WebApp updated: d09e8c59ed...ac1d8fc4fa
+1 -1
Submodule models updated: 7e8e8b44d8...43207fa3a5
+29
View File
@@ -0,0 +1,29 @@
"""Her record's history: CaMa's treatments as Procedures by their kind, and a recurrence as a
Condition named by the catalog, beside what clinical notes diagnosed."""
from core import fhir as f
from core.cama import follow_up
def test_a_cama_treatment_is_a_procedure_of_its_kind():
found = f.procedure({"person_id": "p1", "date": "2025-04-07", "type": "mastectomy"}, "cama")
assert found.code.text == "Mastectomía"
assert found.code.coding[0].system.endswith("/cama-procedure")
assert follow_up.PROCEDURES == ("treatment",)
def test_cacu_procedures_keep_their_codes_and_ids():
row = {"person_id": "p1", "date": "2024-02-01", "type": "colposcopy"}
assert f.procedure(row).code.text == f.procedure(row, "cacu").code.text == "Colposcopia"
assert f.procedure(row).id == f.procedure(row, "cacu").id
def test_a_recurrence_is_a_condition_named_by_the_catalog():
row = {"person_id": "p1", "note_date": "2026-10-30", "study": "breast_recurrence",
"code": "distant"} # fmt: skip
found = f.condition(row)
assert found.code.text == "Recurrencia de cáncer de mama (a distancia)"
assert [c.code for c in found.code.coding] == ["breast_recurrence", "distant"]
note = {"person_id": "p1", "note_date": "2026-10-30", "specialty": "medical_oncology",
"icd10": "C50.9", "diagnosis_text": "RECURRENCIA A DISTANCIA"} # fmt: skip
assert f.condition(note).id != found.id # the same day's note is another Condition