7d50786ac2
Two TOML-only scenarios on cin2_pim's dataflow (KPNC's scenarios 4 and 5): CIN2+, KPNC's three post cytology groups, the referral grade as history after colposcopy, and the strata that reproduce KPNC's tables 17 and 24 (one mixture per referral grade; one mixture). The kit definitions of both cohorts mirror the dbt rules, so the WebApi cycle can build them and KitBuild read them. A notebook describes the scenarios, their encoders and the shared DAG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
147 lines
7.5 KiB
Python
147 lines
7.5 KiB
Python
"""What this module covers: `models.cohorts.cacu.post_treatment.frame`: the index cytology, each exclusion in its
|
|
order, the attrition counts, and the definition's TOML.
|
|
|
|
Fixtures: none; each test builds its timeline rows with `_row`.
|
|
"""
|
|
|
|
from datetime import date, timedelta
|
|
|
|
from models.cohorts.cacu import post_treatment
|
|
from models.cohorts.definition import cohort_path, read_definition
|
|
|
|
RULES = {"event_grades": ["LIEAG", "CIS", "CARCINOMA"], "index_after_days": 90, "pretreatment_lookback_days": 365}
|
|
START, END, CUTOFF = date(2025, 1, 1), date(2026, 1, 1), date(2026, 12, 31)
|
|
TREATMENT = date(2025, 1, 1)
|
|
|
|
|
|
def _row(pac_id, tipo, fecha, valor=None, fuente_id="a", fuente_tabla="t"):
|
|
return {"pac_id": pac_id, "fecha": fecha, "tipo": tipo, "valor_codigo": valor, "fuente_tabla": fuente_tabla,
|
|
"fuente_id": f"{pac_id}{fuente_id}"}
|
|
|
|
|
|
def _after(days):
|
|
return TREATMENT + timedelta(days=days)
|
|
|
|
|
|
def _patient(pac_id, *extra, biopsy=True, index_result="LIEBG"):
|
|
"""A patient with a treatment, a biopsy 30 days before it, and an index cytology 180 days after."""
|
|
rows = [_row(pac_id, "tratamiento", TREATMENT, fuente_id="x"), _row(pac_id, "citologia", _after(180), fuente_id="i")]
|
|
if biopsy:
|
|
rows.append(_row(pac_id, "biopsia", _after(-30), "LIEAG", fuente_id="b"))
|
|
if index_result:
|
|
rows.append(_row(pac_id, "bethesda", _after(180), index_result, fuente_id="i"))
|
|
return rows + list(extra)
|
|
|
|
|
|
def _run(rows, rules=RULES):
|
|
return post_treatment.frame(rows, rules, START, END, CUTOFF)
|
|
|
|
|
|
def test_index_is_the_first_cytology_past_the_days_after_treatment():
|
|
"""Pins down, as dbt's index unit test does: a cytology 60 days after the first treatment is too early; the one
|
|
at 180 days is the index, even with a later treatment. A patient never treated has no index. Fails if the index
|
|
stops waiting `index_after_days` or takes a later treatment."""
|
|
rows = _patient(1, _row(1, "tratamiento", _after(31), fuente_id="x2"), _row(1, "citologia", _after(60), fuente_id="e"))
|
|
rows += [_row(2, "citologia", _after(180), fuente_id="c"), _row(2, "bethesda", _after(180), "LIEBG", fuente_id="c")]
|
|
|
|
members, attrition = _run(rows)
|
|
|
|
assert [(m.pac_id, m.fecha_indice, m.indice_fuente_id) for m in members] == [(1, _after(180), "1i")]
|
|
assert attrition[0] == ("candidatas", 1)
|
|
|
|
|
|
def test_index_needs_more_than_the_wait_days_and_falls_in_the_window():
|
|
"""Pins down: a cytology exactly `index_after_days` after the treatment is too early, one day more is the
|
|
index; an index outside the recruitment window or after the cut-off gives no member. Fails if the comparison
|
|
becomes `>=` or the window/cut-off stop applying."""
|
|
base = [_row(1, "tratamiento", TREATMENT, fuente_id="x"), _row(1, "biopsia", _after(-5), "LIEAG", fuente_id="b"),
|
|
_row(1, "citologia", _after(90), fuente_id="x"), _row(1, "citologia", _after(91), fuente_id="y"),
|
|
_row(1, "vph", _after(91), "POSITIVO", fuente_id="y")]
|
|
late = [_row(2, "tratamiento", date(2025, 11, 1), fuente_id="x"), _row(2, "biopsia", date(2025, 10, 1), "LIEAG", fuente_id="b"),
|
|
_row(2, "citologia", date(2026, 6, 1), fuente_id="e"), _row(2, "vph", date(2026, 6, 1), "POSITIVO", fuente_id="e")]
|
|
|
|
members, _ = _run(base + late)
|
|
cut, _ = post_treatment.frame(base, RULES, START, END, _after(91 - 1))
|
|
|
|
assert [m.indice_fuente_id for m in members] == ["1y"]
|
|
assert cut == []
|
|
|
|
|
|
def test_member_carries_the_index_results():
|
|
"""Pins down: the member's variables are the squamous, glandular and HPV results of the index cytology only,
|
|
and a missing one is None. Fails if results of other samples leak in or a variable is renamed."""
|
|
rows = _patient(1, _row(1, "glandular", _after(180), "AGC", fuente_id="i"),
|
|
_row(1, "vph", _after(250), "POSITIVO", fuente_id="z"))
|
|
|
|
members, _ = _run(rows)
|
|
|
|
assert members[0].variables == {"pap_0": "LIEBG", "glandular_0": "AGC", "hpv_0": None}
|
|
|
|
|
|
def test_no_pretreatment_biopsy_looks_back_from_the_treatment():
|
|
"""Pins down: a biopsy 365 days before the treatment still counts, 366 days or after the treatment doesn't, and
|
|
a biopsy without a code isn't a report. Fails if the lookback, its direction or the null check change."""
|
|
def case(pac_id, days, valor="NEGATIVO"):
|
|
return _patient(pac_id, _row(pac_id, "biopsia", _after(days), valor, fuente_id="q"), biopsy=False)
|
|
|
|
rows = case(1, -365) + case(2, -366) + case(3, 10) + case(4, -20, None) + case(5, 0)
|
|
|
|
members, attrition = _run(rows)
|
|
|
|
assert [m.pac_id for m in members] == [1, 5]
|
|
assert attrition[1] == ("sin no_pretreatment_biopsy", 2)
|
|
|
|
|
|
def test_event_before_index_is_between_treatment_and_index():
|
|
"""Pins down, as dbt's unit test does: a CIN2+ biopsy between the treatment and the index is flagged; one before
|
|
the treatment, on the treatment date or on the index date isn't; a negative one between them isn't. Fails if
|
|
the interval's ends or the grades change."""
|
|
rows = (_patient(1, _row(1, "biopsia", _after(60), "LIEAG")) + _patient(2, _row(2, "biopsia", _after(-60), "LIEAG"))
|
|
+ _patient(3, _row(3, "biopsia", _after(180), "LIEAG")) + _patient(4, _row(4, "biopsia", _after(60), "NEGATIVO"))
|
|
+ _patient(5, _row(5, "biopsia", _after(0), "CIS")))
|
|
|
|
members, attrition = _run(rows)
|
|
|
|
assert [m.pac_id for m in members] == [2, 3, 4, 5]
|
|
assert attrition[2] == ("sin event_before_index", 4)
|
|
|
|
|
|
def test_no_index_result_needs_a_result_of_the_index_cytology():
|
|
"""Pins down: an index cytology with only a glandular result (or only HPV) stays; one with none, or whose
|
|
results belong to another sample, is left out. Fails if the rule reads only the squamous result or ignores the
|
|
sample."""
|
|
rows = (_patient(1, _row(1, "glandular", _after(180), "AGC", fuente_id="i"), index_result=None)
|
|
+ _patient(2, _row(2, "vph", _after(180), "NEGATIVO", fuente_id="i"), index_result=None)
|
|
+ _patient(3, index_result=None)
|
|
+ _patient(4, _row(4, "bethesda", _after(180), None, fuente_id="i"), index_result=None)
|
|
+ _patient(5, _row(5, "vph", _after(250), "POSITIVO", fuente_id="z"), index_result=None))
|
|
|
|
members, attrition = _run(rows)
|
|
|
|
assert [m.pac_id for m in members] == [1, 2]
|
|
assert attrition[3] == ("sin no_index_result", 2)
|
|
|
|
|
|
def test_attrition_charges_each_rule_only_for_what_it_removes_from_the_rest():
|
|
"""Pins down: a patient flagged by several rules is charged to the first in order. Fails if each rule counted
|
|
against all candidates."""
|
|
rows = _patient(1, _row(1, "biopsia", _after(60), "CIS"), biopsy=False, index_result=None) + _patient(2)
|
|
|
|
members, attrition = _run(rows)
|
|
|
|
assert [m.pac_id for m in members] == [2]
|
|
assert attrition == [("candidatas", 2), ("sin no_pretreatment_biopsy", 1), ("sin event_before_index", 1),
|
|
("sin no_index_result", 1)]
|
|
|
|
|
|
def test_definition_loads_and_resolves_the_frame():
|
|
"""Pins down: the TOML loads through `read_definition`, with the dbt rule parameters, and its `expression`
|
|
resolves to `post_treatment.frame`. Fails if a required key is dropped or the expression is renamed."""
|
|
definition = read_definition(cohort_path("cacu", "post_treatment"))
|
|
|
|
assert definition.url == "cacu/cohorte_post_tratamiento"
|
|
assert definition.function() is post_treatment.frame
|
|
assert definition.reglas["event_grades"] == ["LIEAG", "CIS", "CARCINOMA"]
|
|
assert (definition.reglas["index_after_days"], definition.reglas["pretreatment_lookback_days"]) == (90, 365)
|
|
assert [v.nombre for v in definition.variables] == ["pap_0", "glandular_0", "hpv_0"]
|