163 lines
7.2 KiB
Python
163 lines
7.2 KiB
Python
"""Every exclusion reason of a candidate, and the clinical and source cut-offs behind them."""
|
|
|
|
from datetime import date
|
|
|
|
from persistance import destination, gold, silver
|
|
|
|
COHORTS = [
|
|
"cacu_second_visit_features",
|
|
"cacu_cohort_surveillance",
|
|
"cacu_cohort_post_colposcopy",
|
|
"cacu_cohort_post_treatment",
|
|
"cacu_cohort_survival",
|
|
]
|
|
|
|
|
|
def _event(person, day, kind, value=None, table="cervical_cytology", source=None):
|
|
return (person, day, kind, value, table, source or day, None, None)
|
|
|
|
|
|
def _sample(person, day):
|
|
return _event(person, day, "cytology", "satisfactory")
|
|
|
|
|
|
def _result(person, day):
|
|
return _event(person, day, "bethesda", "NILM")
|
|
|
|
|
|
def _birth(person, day):
|
|
return _event(person, day, "birth", table="person", source=person)
|
|
|
|
|
|
def _note(person, day, kind):
|
|
return _event(person, day, kind, table="clinical_note", source=f"{kind}-{day}")
|
|
|
|
|
|
def _biopsy(person, day, value, order):
|
|
return _event(person, day, "biopsy", value, "cervical_biopsy", order)
|
|
|
|
|
|
def _hysterectomy(person, day, available):
|
|
return (person, day, "gynecologic_condition", "hysterectomy", "cervical_cytology", day,
|
|
day, available) # fmt: skip
|
|
|
|
|
|
def _events():
|
|
visit = "2025-02-10"
|
|
|
|
def indexed(person):
|
|
return [_sample(person, visit), _result(person, visit)]
|
|
|
|
return [
|
|
# Surveillance: four reasons on one candidate.
|
|
_birth("many", "2010-01-01"),
|
|
_hysterectomy("many", "2025-01-01", "2025-01-02"),
|
|
_biopsy("many", "2025-01-05", "HSIL", "o-many"),
|
|
_sample("many", visit),
|
|
# Age on the index date.
|
|
*indexed("age17"), _birth("age17", "2007-02-11"),
|
|
*indexed("age18"), _birth("age18", "2007-02-10"),
|
|
*indexed("noage"),
|
|
# Clinical date and source availability against the index.
|
|
*indexed("same_day"), _hysterectomy("same_day", visit, visit),
|
|
*indexed("later"), _hysterectomy("later", "2025-02-11", "2025-02-11"),
|
|
*indexed("late_source"), _hysterectomy("late_source", "2025-02-01", "2025-02-11"),
|
|
*indexed("no_source"), _hysterectomy("no_source", "2025-02-01", None),
|
|
*indexed("on_time"), _hysterectomy("on_time", "2025-02-01", "2025-02-10T23:00:00+00:00"),
|
|
# Post-colposcopy: every scenario reason at once, for the first and second visit.
|
|
_note("colpo", "2025-03-10", "colposcopy"),
|
|
_event("colpo", "2025-03-10", "biopsy_order", table="cervical_biopsy", source="o-open"),
|
|
_event("colpo", "2025-03-10", "biopsy_order", table="cervical_biopsy", source="o-done"),
|
|
_biopsy("colpo", "2025-03-15", "HSIL", "o-done"),
|
|
_note("colpo", "2025-03-20", "history"),
|
|
_note("colpo", "2025-03-22", "treatment"),
|
|
_sample("colpo", "2025-04-10"),
|
|
_sample("colpo", "2025-05-10"),
|
|
# Post-treatment: no biopsy before it, an outcome after it, no result at the index.
|
|
_note("treated", "2025-03-01", "treatment"),
|
|
_biopsy("treated", "2025-05-01", "HSIL", "o-treated"),
|
|
_sample("treated", "2025-10-05"),
|
|
# Survival: died by the index and no birth date.
|
|
_biopsy("case", "2025-02-10", "carcinoma", "o-case"),
|
|
_event("case", "2025-02-01", "death", table="person", source="case"),
|
|
# Two index dates of one patient, with the evidence dated between them.
|
|
_sample("twice", "2025-01-10"), _result("twice", "2025-01-10"),
|
|
_hysterectomy("twice", "2025-02-01", "2025-02-01"),
|
|
_sample("twice", "2025-03-10"), _result("twice", "2025-03-10"),
|
|
] # fmt: skip
|
|
|
|
|
|
def test_each_candidate_reports_all_its_reasons_once(tmp_path, monkeypatch):
|
|
"""Pins multiple reasons per scenario, the index cut-offs, age 17/18 and cohort priority."""
|
|
monkeypatch.setenv("ISSSTELEON_DATA", str(tmp_path))
|
|
monkeypatch.setenv("PLATFORM_DATABASE_URL", f"sqlite:///{tmp_path / 'platform.db'}")
|
|
monkeypatch.setenv("DBT_TARGET", "sqlite")
|
|
monkeypatch.setattr(destination, "DATA_DIR", tmp_path)
|
|
destination.engine.cache_clear()
|
|
engine = destination.engine()
|
|
|
|
try:
|
|
with engine.begin() as con:
|
|
con.exec_driver_sql("create table silver.person (person_id text)")
|
|
con.exec_driver_sql(
|
|
"create table gold.cacu_events (person_id text, date date, type text, "
|
|
"value_code text, source_table text, source_id text, evidence_date date, "
|
|
"source_available_at text, source_ref text)"
|
|
)
|
|
con.exec_driver_sql(
|
|
"insert into gold.cacu_events (person_id, date, type, value_code, source_table, "
|
|
"source_id, evidence_date, source_available_at) values (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
_events(),
|
|
)
|
|
|
|
silver.build([*COHORTS, "cacu_eligibility"], as_of=date(2025, 11, 1))
|
|
found = gold.cacu_eligibility_report()
|
|
|
|
key = ["scenario", "person_id", "index_date", "index_sample_id", "criterion"]
|
|
assert not found.duplicated(key).any()
|
|
# A candidate is either eligible, with one empty row, or excluded by its reasons.
|
|
for _, rows in found.groupby(key[:4], dropna=False):
|
|
if rows["eligible"].astype(bool).any():
|
|
assert len(rows) == 1 and rows["criterion"].isna().all()
|
|
else:
|
|
assert rows["criterion"].notna().all()
|
|
|
|
def reasons(scenario, person):
|
|
rows = found[(found["scenario"] == scenario) & (found["person_id"] == person)]
|
|
assert len(rows), f"no {scenario} candidate for {person}"
|
|
return set(rows["criterion"].dropna())
|
|
|
|
assert reasons("surveillance", "many") == {
|
|
"hysterectomy", "under_18", "prior_outcome", "no_index_result"} # fmt: skip
|
|
assert reasons("surveillance", "age17") == {"under_18"}
|
|
for person in ("age18", "noage", "same_day", "later", "late_source", "no_source"):
|
|
assert reasons("surveillance", person) == set(), person
|
|
assert reasons("surveillance", "on_time") == {"hysterectomy"}
|
|
|
|
colposcopy = {"outcome_by_index", "cancer_history", "prior_treatment", "no_referral",
|
|
"no_cotest"} # fmt: skip
|
|
assert reasons("post_colposcopy", "colpo") == colposcopy | {"unreported_biopsy"}
|
|
assert reasons("post_colposcopy_second_visit", "colpo") == colposcopy
|
|
assert reasons("post_treatment", "treated") == {
|
|
"no_pretreatment_biopsy", "outcome_before_index", "no_index_result"} # fmt: skip
|
|
assert reasons("survival", "case") == {"died_by_index", "no_birth_date"}
|
|
|
|
assert reasons("surveillance", "twice") == set()
|
|
assert reasons("pre_colposcopy", "twice") == {"hysterectomy"}
|
|
|
|
# The cohorts keep one reason each, in the order they had before.
|
|
with engine.connect() as con:
|
|
for cohort, person, first in (
|
|
("surveillance", "many", "hysterectomy"),
|
|
("post_colposcopy", "colpo", "unreported_biopsy"),
|
|
("post_treatment", "treated", "no_pretreatment_biopsy"),
|
|
("survival", "case", "no_birth_date"),
|
|
):
|
|
assert con.exec_driver_sql(
|
|
f"select excluded_by from gold.cacu_cohort_{cohort} where person_id = ?",
|
|
(person,),
|
|
).scalar_one() == first, cohort
|
|
finally:
|
|
engine.dispose()
|
|
destination.engine.cache_clear()
|