Files
tea/tests/test_modern_slice.py
T
AlbertoPMDM 1f8793aacb modern annotation_mask, sleep_state and posterior_rhythm
- processes.annotation_mask: the director's state words and interval rules
  on the EDF+ annotations (MNE), window dominant state and 'mixta'.
- processes.sleep_state: vigilancia's features from batched Hann
  periodograms (mne psd_array_welch), depth index, HK1K cut-offs or cohort
  reference + KMeans (scikit-learn) after train().
- biomarkers.posterior_rhythm: Welch of the wake windows (MNE), 1/f-fitted
  dominant peak, median of O1 O2 P3 P4; private copy of the baselines.
- Registered as the modern modules; Pipeline.default("modern") now runs
  the full slice.

On a synthetic study: annotation states and sleep-state labels identical to
legacy, rhythm equal (6.5 Hz); contract suites and cohort cut-offs tested.
2026-11-18 20:53:52 -06:00

66 lines
2.8 KiB
Python

"""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)