"""The binary encoders read only what was available at the index, and never turn an absent value into 0.""" import numpy as np import pandas as pd import pytest from models.cacu.encoders import FINDINGS, encode INDEX = "2025-06-10" def event(day, kind, code=None, person="p1", table="cervical_cytology", sample=None, clinical=None, available=None): # fmt: skip return { "person_id": person, "index_date": INDEX, "date": day, "type": kind, "value_code": code, "value_num": np.nan, "source_table": table, "source_id": sample or day, "evidence_date": clinical, "source_available_at": available, } # fmt: skip def form(sample, report, person="p1", **fields): empty = dict.fromkeys(("screening_history", "adequacy", *FINDINGS)) return {"person_id": person, "sample_date": sample, "report_date": report} | empty | fields def row(events, forms=None, person="p1"): found = encode(pd.DataFrame(events), None if forms is None else pd.DataFrame(forms)) return found.set_index("person_id").loc[person].drop("index_date").astype(float) def test_nothing_known_is_all_missing(): found = row([event(INDEX, "cytology", "satisfactory")]) assert found.isna().all() def test_values_and_missing(): found = row([ event("1980-06-11", "birth", table="person"), event("2025-01-15", "bethesda", "LSIL", sample="2025-01-10"), event("2025-01-15", "hpv", "positive", sample="2025-01-10"), event("2025-01-15", "hpv_16", "negative", sample="2025-01-10"), event(INDEX, "cervix_appearance", "lesion"), event(INDEX, "presenting_symptom", "not-a-code"), ]) # fmt: skip assert found["age__35-44"] == 1 and found["age__45-54"] == 0 assert found["bethesda__LSIL"] == 1 and found["bethesda__HSIL"] == 0 assert found["cytology_ascus_plus"] == 1 and found["cytology_hsil_plus"] == 0 assert found["hpv_positive"] == 1 and found["hpv_16_positive"] == 0 assert np.isnan(found["hpv_18_positive"]) # one genotype negative and the other unreported is unknown, not negative assert np.isnan(found["hpv_16_or_18_positive"]) assert found["cervix_appearance__lesion"] == 1 and found["cervix_appearance__healthy"] == 0 assert found.filter(like="presenting_symptom").isna().all() # ambiguous value assert found.filter(like="prior_").isna().all() @pytest.mark.parametrize("positive, other, expected", [ ("positive", None, 1.0), ("negative", "negative", 0.0), ("negative", "positive", 1.0), ]) # fmt: skip def test_genotypes_combined(positive, other, expected): events = [event("2025-01-15", "hpv_16", positive)] if other: events.append(event("2025-01-15", "hpv_18", other)) assert row(events)["hpv_16_or_18_positive"] == expected def test_nothing_later_than_the_index_is_read(): base = [event(INDEX, "cytology", "satisfactory")] later = [ event("2025-06-20", "bethesda", "HSIL", sample=INDEX), # the index sample's report event("2025-06-20", "hpv", "positive", sample=INDEX), event("2025-06-01", "bethesda", "HSIL", available="2025-06-11T08:00:00-06:00"), event("2025-06-01", "hpv", "positive", clinical="2025-07-01"), # procedures of the index day may be the outcome event(INDEX, "biopsy", "carcinoma", table="cervical_biopsy"), event(INDEX, "colposcopy", table="clinical_note"), event(INDEX, "treatment", table="clinical_note"), event("2025-08-01", "history", table="clinical_note"), # standalone evidence without an availability date: its onset says nothing of it event("2020-01-01", "gynecologic_condition", "hysterectomy", table="cacu_eligibility_evidence"), ] # fmt: skip forms = [form(INDEX, "2025-06-20", adequacy="unsatisfactory", finding_atrophy=True)] pd.testing.assert_series_equal(row(base + later, forms), row(base)) def test_available_by_the_index_day_is_read(): found = row([ event("2025-06-01", "bethesda", "HSIL", available="2025-06-10T23:00:00-06:00"), event("2020-01-01", "gynecologic_condition", "hysterectomy", table="cacu_eligibility_evidence", available="2024-01-01"), ]) # fmt: skip assert found["cytology_hsil_plus"] == 1 and found["history_hysterectomy"] == 1 def test_prior_procedures_and_referral(): found = row([ event("2025-01-15", "bethesda", "HSIL", sample="2025-01-10"), event("2025-03-01", "colposcopy", table="clinical_note"), event("2025-03-01", "biopsy_order", table="cervical_biopsy"), event("2025-03-10", "biopsy", "LSIL", table="cervical_biopsy"), ]) # fmt: skip assert found["prior_colposcopy"] == 1 and found["prior_biopsy_order"] == 1 assert found["prior_biopsy_lsil_plus"] == 1 and found["prior_biopsy_hsil_plus"] == 0 assert found["referral_high_grade"] == 1 assert np.isnan(found["prior_treatment"]) def test_form_fields_come_from_the_latest_report(): forms = [ form("2024-01-10", "2024-01-20", screening_history="never", finding_atrophy=True), form("2025-01-10", "2025-01-20", adequacy="satisfactory", finding_candida=1), ] found = row([event(INDEX, "cytology")], forms) assert found["screening_history__never"] == 1 assert found["sample_unsatisfactory"] == 0 and found["finding_candida"] == 1 assert np.isnan(found["finding_atrophy"]) def test_members_are_kept_apart(): events = [event("2025-01-15", "hpv", "positive"), event(INDEX, "cytology", person="p2")] assert np.isnan(row(events, person="p2")["hpv_positive"]) def test_a_timeline_without_availability_is_refused(): events = pd.DataFrame([event(INDEX, "cytology")]).drop(columns="source_available_at") with pytest.raises(ValueError, match="source_available_at"): encode(events)