"""The development encoders (models.cacu.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" SAMPLE = "2025-01-10" def ev(date, kind, code=None, *, person="p1", table="cervical_cytology", source=None, clinical=None, available=None) -> dict: # fmt: skip return {"person_id": person, "index_date": INDEX, "date": date, "type": kind, "value_code": code, "value_num": np.nan, "source_table": table, "source_id": source or date, "evidence_date": clinical, "source_available_at": available} # fmt: skip def encoded(events, forms=None, person="p1") -> pd.Series: blank = dict.fromkeys(("screening_history", "adequacy", *FINDINGS)) forms = None if forms is None else pd.DataFrame([blank | f for f in forms]) found = encode(pd.DataFrame(events), forms).set_index("person_id") return found.loc[person].drop("index_date").astype(float) def test_an_unknown_value_is_missing_never_zero(): assert encoded([ev(INDEX, "cytology", "satisfactory")]).isna().all() found = encoded([ ev("1980-06-11", "birth", table="person"), *(ev("2025-01-15", k, c, source=SAMPLE) for k, c in ( ("bethesda", "LSIL"), ("hpv", "positive"), ("hpv_16", "negative"))), ev(INDEX, "cervix_appearance", "lesion"), ev(INDEX, "presenting_symptom", "other?"), ]) # fmt: skip assert (found["age__35-44"], found["age__45-54"]) == (1, 0) assert (found["bethesda__LSIL"], found["bethesda__HSIL"]) == (1, 0) assert (found["cytology_ascus_plus"], found["cytology_hsil_plus"]) == (1, 0) assert (found["hpv_positive"], found["hpv_16_positive"]) == (1, 0) assert np.isnan(found["hpv_18_positive"]) assert np.isnan(found["hpv_16_or_18_positive"]) # one negative, the other unreported assert (found["cervix_appearance__lesion"], found["cervix_appearance__healthy"]) == (1, 0) assert found.filter(like="presenting_symptom").isna().all() # not one of its codes assert found.filter(like="prior_").isna().all() @pytest.mark.parametrize("hpv_16, hpv_18, expected", [ ("positive", None, 1.0), ("negative", "negative", 0.0), ("negative", "positive", 1.0)]) # fmt: skip def test_genotypes_of_one_sample(hpv_16, hpv_18, expected): events = [ev("2025-01-15", "hpv_16", hpv_16)] events += [ev("2025-01-15", "hpv_18", hpv_18)] if hpv_18 else [] assert encoded(events)["hpv_16_or_18_positive"] == expected def test_what_was_not_available_at_the_index_is_not_read(): base = [ev(INDEX, "cytology", "satisfactory")] later = [ ev("2025-06-20", "bethesda", "HSIL", source=INDEX), # the index sample's report ev("2025-06-20", "hpv", "positive", source=INDEX), ev("2025-06-01", "bethesda", "HSIL", available="2025-06-11T08:00:00-06:00"), ev("2025-06-01", "hpv", "positive", clinical="2025-07-01"), # the index day's procedures may be the outcome ev(INDEX, "biopsy", "carcinoma", table="cervical_biopsy"), ev(INDEX, "colposcopy", table="clinical_note"), ev(INDEX, "treatment", table="clinical_note"), ev("2025-08-01", "history", table="clinical_note"), # standalone evidence with no availability: its onset says nothing of when it was known ev("2020-01-01", "gynecologic_condition", "hysterectomy", table="cacu_eligibility_evidence"), ] # fmt: skip forms = [{"person_id": "p1", "sample_date": INDEX, "report_date": "2025-06-20", "adequacy": "unsatisfactory", "finding_atrophy": True}] # fmt: skip pd.testing.assert_series_equal(encoded(base + later, forms), encoded(base)) found = encoded([ ev("2025-06-01", "bethesda", "HSIL", available="2025-06-10T23:00:00-06:00"), ev("2020-01-01", "gynecologic_condition", "hysterectomy", table="cacu_eligibility_evidence", available="2024-01-01"), ]) # fmt: skip assert (found["cytology_hsil_plus"], found["history_hysterectomy"]) == (1, 1) def test_prior_procedures_and_the_referral_grade(): found = encoded([ ev("2025-01-15", "bethesda", "HSIL", source=SAMPLE), ev("2025-03-01", "colposcopy", table="clinical_note"), ev("2025-03-01", "biopsy_order", table="cervical_biopsy"), ev("2025-03-10", "biopsy", "LSIL", table="cervical_biopsy"), ]) # fmt: skip assert (found["prior_colposcopy"], found["prior_biopsy_order"]) == (1, 1) assert (found["prior_biopsy_lsil_plus"], found["prior_biopsy_hsil_plus"]) == (1, 0) assert found["referral_high_grade"] == 1 and np.isnan(found["prior_treatment"]) def test_the_form_is_read_from_its_latest_report_whole(): forms = [ {"person_id": "p1", "sample_date": "2024-01-10", "report_date": "2024-01-20", "screening_history": "never", "finding_atrophy": True}, {"person_id": "p1", "sample_date": SAMPLE, "report_date": "2025-01-20", "adequacy": "satisfactory", "finding_candida": 1}, ] # fmt: skip found = encoded([ev(INDEX, "cytology")], forms) assert found["screening_history__never"] == 1 assert (found["sample_unsatisfactory"], found["finding_candida"]) == (0, 1) assert np.isnan(found["finding_atrophy"]) def test_members_apart_and_a_timeline_without_availability_refused(): events = [ev("2025-01-15", "hpv", "positive"), ev(INDEX, "cytology", person="p2")] assert np.isnan(encoded(events, person="p2")["hpv_positive"]) with pytest.raises(KeyError, match="source_available_at"): encode(pd.DataFrame([ev(INDEX, "cytology")]).drop(columns="source_available_at"))