"""Modern annotation_mask + sleep_state + posterior_rhythm agree with the legacy (director) ones.""" from __future__ import annotations import pytest from synth_edf import make_edf from tea_modulo.contracts import Study from tea_modulo.contracts.testing import check_biomarker, check_process from tea_modulo.pipeline import Pipeline from tea_modulo.preprocessing import DIRECTOR_CLI, DIRECTOR_CLUSTER from tea_modulo.processes.sleep_state import SleepState ANN = [(5, None, "Paciente despierto"), (240.5, None, "Dormido"), (100, 20, "Fotica 5 Hz"), (150, None, "llanto")] @pytest.fixture(scope="module") def studies(tmp_path_factory): p = make_edf(tmp_path_factory.mktemp("slice") / "s.edf", minutes=8, rhythm_hz=6.5, annotations=ANN) return (Pipeline.default("modern").study(p, plan=DIRECTOR_CLUSTER, age_months=28), Pipeline.default("legacy").study(p, plan=DIRECTOR_CLUSTER, age_months=28)) def test_annotation_states_equal(studies): m, l = studies assert (m.label("annotation_mask").per_window.values == l.label("annotation_mask").per_window.values).all() def test_sleep_state_agrees(studies): m, l = studies a, b = m.label("sleep_state").per_window, l.label("sleep_state").per_window assert (a.values == b.values).mean() >= 0.95 def test_rhythm_agrees(studies): m, l = studies rm = Pipeline.default("modern").module("posterior_rhythm").measure(m)["posterior_rhythm_hz"] rl = Pipeline.default("legacy").module("posterior_rhythm").measure(l)["posterior_rhythm_hz"] assert rm == pytest.approx(rl, abs=0.25) and rm == pytest.approx(6.5, abs=0.3) def test_contracts(studies): m, _ = studies pm = Pipeline.default("modern") check_process(pm.module("annotation_mask"), Study(m.prep)) check_process(pm.module("sleep_state"), Study(m.prep).with_labels(pm.module("annotation_mask").predict(Study(m.prep)))) def test_biomarker_contract(tmp_path): pipe = Pipeline.default("modern") sts = [pipe.study(make_edf(tmp_path / f"s{i}.edf", minutes=4, seed=i, rhythm_hz=6 + 0.4 * i), plan=DIRECTOR_CLUSTER, age_months=20 + 4 * i) for i in range(3)] from tea_modulo.biomarkers.posterior_rhythm import PosteriorRhythm check_biomarker(PosteriorRhythm(baseline="age_blocks"), sts, [20, 24, 28]) def test_cohort_cutoffs_train(tmp_path): """DIRECTOR_CLI route: KMeans cut-offs on the cohort (cutoffs='cohort').""" pipe = Pipeline.default("modern") sts = [pipe.study(make_edf(tmp_path / f"c{i}.edf", minutes=6, seed=10 + i), plan=DIRECTOR_CLI, age_months=24, processes=["annotation_mask"]) for i in range(2)] ss = SleepState(cutoffs="cohort").train(sts) assert ss.cut["origin"] == "cohort" and len(ss.cut["cuts"]) == 2 lab = ss.predict(sts[0]).per_window assert {"vigilia", "sueno"} <= set(lab)