"""The EEG–ADOS-2 association (models.tea.association) computes what the old platform's `ados2_association` 0.1.0 computes, on the cohorts tools/capture_association.py ran it on (tests/fixtures/reference/tea/association.json): the old reference tables, where it estimates nothing, and a seeded synthetic cohort, where it finds the effects planted in it.""" import json from pathlib import Path import pandas as pd import pytest from core.tea import association as spec from models.tea import association CAPTURED = json.loads( (Path(__file__).parent / "fixtures" / "reference" / "tea" / "association.json").read_text() ) # The old platform's names: its modules, zones and item columns (by module). MODULES = {"TODDLER": "toddler", "MODULO_1": "module_1", "MODULO_2": "module_2"} ZONES = { "frontal_izq": "frontal_left", "frontal_der": "frontal_right", "frontal_media": "frontal_midline", "temporal_izq": "temporal_left", "temporal_der": "temporal_right", } OLD_ITEMS = { 1: {"toddler": "sa_a2", "module_1": "sa_a2"}, 2: {"toddler": "sa_a7", "module_1": "sa_a7", "module_2": "sa_a6"}, 3: {"toddler": "sa_a8", "module_1": "sa_a8", "module_2": "sa_a7"}, 4: {"toddler": "sa_b1", "module_1": "sa_b1", "module_2": "sa_b1"}, 5: {"toddler": "sa_b4", "module_1": "sa_b3", "module_2": "sa_b2"}, 6: {"toddler": "sa_b5", "module_1": "sa_b4"}, 7: {"toddler": "sa_b6", "module_1": "sa_b5", "module_2": "sa_b3"}, 8: {"toddler": "sa_b7"}, 9: {"toddler": "sa_b8"}, 10: {"toddler": "sa_b9"}, 11: {"toddler": "sa_b12", "module_1": "sa_b9", "module_2": "sa_b5"}, 12: {"toddler": "sa_b13", "module_1": "sa_b10", "module_2": "sa_b6"}, 13: {"toddler": "sa_b14", "module_1": "sa_b11"}, 14: {"toddler": "sa_b15", "module_1": "sa_b12", "module_2": "sa_b8"}, 15: {"toddler": "sa_b16b"}, 16: {"module_2": "sa_b11"}, 17: {"toddler": "sa_b18", "module_2": "sa_b12"}, 18: {"toddler": "rrb_a3", "module_1": "rrb_a3"}, 19: {"module_1": "rrb_a5", "module_2": "rrb_a4"}, 20: {"toddler": "rrb_d1", "module_1": "rrb_d1", "module_2": "rrb_d1"}, 21: {"toddler": "rrb_d2", "module_1": "rrb_d2", "module_2": "rrb_d2"}, 22: {"toddler": "rrb_d5", "module_1": "rrb_d4", "module_2": "rrb_d4"}, } def _zone(name: str) -> str: for old, new in ZONES.items(): name = name.replace(old, new) return name def ours(records: list[dict]) -> pd.DataFrame: """A captured cohort in the platform's names: each item in its own column.""" rows = [] for r in records: module = MODULES.get(r["ados_module"]) if r["ados_module"] else None row = {"key": r["key"], "age_months": r["age_months"], "module": module, "css": r["css"]} for n, (name, _) in spec.ITEMS.items(): code = OLD_ITEMS[n].get(module or "") row[name] = r.get(code) if code else None row |= {_zone(k): v for k, v in r.items() if k.startswith("z_")} rows.append(row) return pd.DataFrame(rows) KEPT = ( "biomarker", "kind", "outcome_code", "event", "model", "n", "odds_ratio", "ci_low", "ci_high", "p", "q", "sens_model", "sens_odds_ratio", "same_direction", "reason", ) def _close(a, b): if a is None or b is None or isinstance(a, (str, bool)): return a == b return a == pytest.approx(b, abs=2e-4) # their values are rounded to 4 decimals @pytest.mark.parametrize("cohort", ["reference_tables", "synthetic"]) def test_the_association_is_the_old_platforms(cohort): captured = CAPTURED[cohort] fitted = association.fit(ours(captured["fit"])) assert (fitted["level"], fitted["n"]) == (captured["level"], captured["n"]) found = { (a["biomarker"], a["zone"], a["outcome_code"]): a for a in association.associations(fitted) } theirs = { (a["biomarker"], _zone(a["zone"]), a["outcome_code"]): a for a in captured["associations"] } assert set(found) == set(theirs) for key, old in theirs.items(): for column in KEPT: assert _close(found[key][column], old[column]), (key, column) scores = { (s["key"], s["biomarker"], s["zone"], s["outcome_code"]): s for s in association.score(fitted, ours(captured["scored"])) } expected = { (s["key"], s["biomarker"], _zone(s["zone"]), s["outcome_code"]): s for s in captured["scores"] } assert set(scores) == set(expected) for key, old in expected.items(): assert scores[key]["probability"] == pytest.approx(old["probability"], abs=2e-4), key assert scores[key]["reference_probability"] == pytest.approx( old["reference_probability"], abs=2e-4 ), key def test_the_planted_effects_are_found_and_nothing_else_stands_out(): captured = CAPTURED["synthetic"] fitted = association.fit(ours(captured["fit"])) found = { ( f"z_{a['biomarker']}_{a['zone']}" if a["biomarker"] in spec.ZONED else f"z_{a['biomarker']}", a["outcome_code"], ): a for a in association.associations(fitted) } planted = { ( _zone(p["predictor"]), "CSS" if p["outcome"] == "css" else "ados2_item_" + p["outcome"][-2:], ): p["log_odds"] for p in CAPTURED["planted"] } for key, log_odds in planted.items(): a = found[key] assert a["q"] < 0.01 and (a["odds_ratio"] > 1) == (log_odds > 0), key assert all(a["q"] is None or a["q"] > 0.01 for k, a in found.items() if k not in planted)