Declarar las diferencias de CaCu frente a la plataforma original

This commit is contained in:
2026-12-03 22:22:19 -06:00
parent fb2f3c1753
commit 9d9d6879d2
+48 -5
View File
@@ -54,7 +54,14 @@ CODES = { # platform code -> the current platform's
} # fmt: skip } # fmt: skip
STRATA = {"age_group": "grupo_edad", "follow_up_status": "estatus_seguimiento", STRATA = {"age_group": "grupo_edad", "follow_up_status": "estatus_seguimiento",
"result": "citologia_alto_grado"} # fmt: skip "result": "citologia_alto_grado"} # fmt: skip
NEW_STRATA = {"round"} # CACU_EU7, Phase 6 NEW_STRATA = {"round", "subsequent_history"} # CACU_EU7: Phase 6 and the declared history
# Declared difference: the follow-up status is now what preceded each sample, not what preceded
# the period's start, so these two split the same patients differently.
RESTATED_STATUS = {"CACU_EU13", "CACU_EU7"}
# Declared difference: the mixture models no longer read a missing HPV as negative, and the
# post-colposcopy referral is stricter, so they learn from and score fewer patients. Their
# numbers are held to the current platform's in models/tests, which stands the old reading in.
HPV_GATED = {"cin2_pim", "cancer_pim", "cin2_pim_post_colposcopy", "cin2_pim_post_treatment"}
CATEGORY = {"CACU_EU7": "resultado", "CACU_CONFIRMACION_CITOLOGIA": "biopsia_grado"} CATEGORY = {"CACU_EU7": "resultado", "CACU_CONFIRMACION_CITOLOGIA": "biopsia_grado"}
@@ -104,7 +111,14 @@ def test_cacu_from_the_hospital_to_indicators_and_risks(api, platform, tmp_path)
current = json.loads((REFERENCE / "models.json").read_text()) current = json.loads((REFERENCE / "models.json").read_text())
post_colposcopy = current["cin2_pim_post_colposcopy"]["version"]["metrics"] post_colposcopy = current["cin2_pim_post_colposcopy"]["version"]["metrics"]
members = q("select count(*) from gold.cacu_cohort_post_colposcopy where excluded_by is null") members = q("select count(*) from gold.cacu_cohort_post_colposcopy where excluded_by is null")
assert members[0][0] == post_colposcopy["n_train"] + post_colposcopy["n_test"] # Declared difference: here the referral is the cytology linked to the colposcopy, and a
# low grade one counts only with a positive HPV; the current platform took any result of
# the year before. Whoever leaves for that reason is the whole difference.
ungraded = q("select count(*) from gold.cacu_cohort_post_colposcopy "
"where excluded_by = 'referral_not_graded'")[0][0] # fmt: skip
theirs = post_colposcopy["n_train"] + post_colposcopy["n_test"]
print("post-colposcopy members", members[0][0], "ungraded", ungraded, "current", theirs)
assert members[0][0] <= theirs <= members[0][0] + ungraded
# Indicators: every measure, every semester, as the current platform answers them # Indicators: every measure, every semester, as the current platform answers them
# (no risk batch yet, so the risk bands are empty as they were). # (no risk batch yet, so the risk bands are empty as they were).
@@ -122,7 +136,24 @@ def test_cacu_from_the_hospital_to_indicators_and_risks(api, platform, tmp_path)
a, b = _report(ours, name), _report(theirs, name) a, b = _report(ours, name), _report(theirs, name)
where = (name, ours["resource"]["period"]["start"]) where = (name, ours["resource"]["period"]["start"])
old = {k: v for k, v in a[3].items() if k not in NEW_STRATA} old = {k: v for k, v in a[3].items() if k not in NEW_STRATA}
assert a[0] == b[0] and old == b[3], where if name == "CACU_DIAS_DIAGNOSTICO_TRATAMIENTO":
# Declared difference: it starts at the diagnosis after the biopsy report, so
# it never counts more patients than the current platform's.
assert all(a[0][k] <= b[0][k] for k in b[0]), where
continue
if name == "CACU_DIAS_TOMA_DIAGNOSTICO":
# Declared difference: only a diagnosis before her first colposcopy counts, so
# there are never more observations than the current platform's.
assert all(a[0][k] <= b[0][k] for k in b[0]), where
continue
if name in RESTATED_STATUS:
mine, was = old.pop("estatus_seguimiento", {}), dict(b[3])
theirs_status = was.pop("estatus_seguimiento", {})
assert sum(v["initial-population"] for v in mine.values()) == sum(
v["initial-population"] for v in theirs_status.values()), where # fmt: skip
assert a[0] == b[0] and old == was, where
else:
assert a[0] == b[0] and old == b[3], where
if cohort: # scores its count; the current platform gave none if cohort: # scores its count; the current platform gave none
assert b[1] is None and a[1] == a[0]["initial-population"], where assert b[1] is None and a[1] == a[0]["initial-population"], where
continue continue
@@ -139,7 +170,11 @@ def test_cacu_from_the_hospital_to_indicators_and_risks(api, platform, tmp_path)
(version,) = ok(api.get(f"/models/cacu/{name}/versions")) (version,) = ok(api.get(f"/models/cacu/{name}/versions"))
assert version["reports"] == ["interpretability", "technical_validation"] assert version["reports"] == ["interpretability", "technical_validation"]
for metric in ("n_train", "n_test"): for metric in ("n_train", "n_test"):
assert version["metrics"][metric] == current[name]["version"]["metrics"][metric] was = current[name]["version"]["metrics"][metric]
if name in HPV_GATED:
assert version["metrics"][metric] <= was, (name, metric)
else:
assert version["metrics"][metric] == was, (name, metric)
assert api.post(f"/models/cacu/{name}/versions/1/promote").status_code == 409 assert api.post(f"/models/cacu/{name}/versions/1/promote").status_code == 409
files = {"file": ("validacion.pdf", b"%PDF-1.4 clinical validation", "application/pdf")} files = {"file": ("validacion.pdf", b"%PDF-1.4 clinical validation", "application/pdf")}
ok(api.post(f"/models/cacu/{name}/versions/1/reports/clinical_validation", files=files)) ok(api.post(f"/models/cacu/{name}/versions/1/reports/clinical_validation", files=files))
@@ -161,6 +196,10 @@ def test_cacu_from_the_hospital_to_indicators_and_risks(api, platform, tmp_path)
assert set(map(tuple, risks[["person_id", "index_date"]].astype(str).values)) == { assert set(map(tuple, risks[["person_id", "index_date"]].astype(str).values)) == {
(p, str(d)) for p, d in cohort} # fmt: skip (p, str(d)) for p, d in cohort} # fmt: skip
continue continue
if name in HPV_GATED: # nobody the current platform did not score
assert set(zip(risks["pac_id"], risks["horizon_years"])) <= set(
zip(expected["pac_id"], expected["horizonte_anios"])), name # fmt: skip
continue
both = risks.merge(expected, left_on=["pac_id", "horizon_years"], both = risks.merge(expected, left_on=["pac_id", "horizon_years"],
right_on=["pac_id", "horizonte_anios"], how="outer", right_on=["pac_id", "horizonte_anios"], how="outer",
indicator=True) # fmt: skip indicator=True) # fmt: skip
@@ -208,7 +247,11 @@ def test_cacu_from_the_hospital_to_indicators_and_risks(api, platform, tmp_path)
assert (shown["estatus"], item["estatus"]["codigo"]) == ( assert (shown["estatus"], item["estatus"]["codigo"]) == (
"PRE_COLPOSCOPIA", "POST_COLPOSCOPIA") # fmt: skip "PRE_COLPOSCOPIA", "POST_COLPOSCOPIA") # fmt: skip
shown["estatus"] = "POST_COLPOSCOPIA" shown["estatus"] = "POST_COLPOSCOPIA"
assert shown == {**{k: item[k] for k in shown}, "estatus": item["estatus"]["codigo"]} status = item["estatus"]["codigo"]
# Declared difference: after colposcopy only once she has a cytology taken after it.
if status == "POST_COLPOSCOPIA" and shown["estatus"] == "PRE_COLPOSCOPIA":
status = "PRE_COLPOSCOPIA"
assert shown == {**{k: item[k] for k in shown}, "estatus": status}
# Every resource the API serves, every search paged to the end, passes the HL7 # Every resource the API serves, every search paged to the end, passes the HL7
# validator (offline: structure, cardinality, profiles; no terminology server). # validator (offline: structure, cardinality, profiles; no terminology server).