Contar etapas de descarte en cohortes CaCu

This commit is contained in:
2026-12-02 20:30:25 -06:00
parent d27319ad1f
commit 6e6c5bb81b
2 changed files with 21 additions and 0 deletions
+18
View File
@@ -34,6 +34,16 @@ COHORT_SCENARIOS = {
}
# Reasons that a result still to arrive could lift.
AWAITING = {"unreported_biopsy", "no_index_result"}
# Clinical categories, not execution order: a candidate may belong to several stages.
REASON_STAGE = {
"hysterectomy": "eligibility", "under_18": "eligibility",
"no_birth_date": "eligibility", "died_by_index": "eligibility",
"prior_outcome": "history", "outcome_by_index": "history",
"cancer_history": "history", "prior_treatment": "history",
"outcome_before_index": "history", "no_pretreatment_biopsy": "history",
"no_referral": "index", "no_cotest": "index",
"no_index_result": "index", "unreported_biopsy": "index",
}
def attrition(cohort: str, rows: pd.DataFrame, people: int, as_of: date) -> dict:
@@ -46,6 +56,7 @@ def attrition(cohort: str, rows: pd.DataFrame, people: int, as_of: date) -> dict
excluded = rows[rows["criterion"].notna()]
reasons = excluded.groupby(key, dropna=False)["criterion"].agg(set)
pending = int(sum(found <= AWAITING for found in reasons))
stages = excluded.assign(stage=excluded["criterion"].map(REASON_STAGE).fillna("unknown"))
return {
"cohort": cohort,
"scenario": COHORT_SCENARIOS[cohort],
@@ -60,6 +71,13 @@ def attrition(cohort: str, rows: pd.DataFrame, people: int, as_of: date) -> dict
str(reason): int(count)
for reason, count in excluded["criterion"].value_counts().sort_index().items()
},
"by_stage": {
str(stage): len(group[key].drop_duplicates())
for stage, group in stages.groupby("stage", sort=True)
},
"stage_by_reason": {str(reason): REASON_STAGE.get(str(reason), "unknown")
for reason in sorted(excluded["criterion"].unique())},
"stage_counts_overlap": True,
}
+3
View File
@@ -29,3 +29,6 @@ def test_a_candidate_with_two_reasons_is_excluded_once():
assert found["by_reason"] == {
"hysterectomy": 1, "no_index_result": 2, "prior_outcome": 1, "under_18": 1,
} # fmt: skip
assert found["by_stage"] == {"eligibility": 1, "history": 1, "index": 2}
assert found["stage_by_reason"]["no_index_result"] == "index"
assert found["stage_counts_overlap"] is True