Files
tea/tests/test_coupling.py
T
AlbertoPMDM d21ace9aea coupling, legacy and modern
- legacy: acoplamiento.fases_de_estudio + resumen (vendored unchanged);
  spindle and slow-wave events read through the contract columns, so
  either detector can feed it; in_slow_wave = --solo-en-onda.
- modern: slow-oscillation phase (MNE 0.5-2 Hz, scipy Hilbert, per run) at
  the sigma-envelope peak of each spindle; R from 200 subsamples of 30,
  excess over the chance floor, preferred angle, raw R.

Modern within 0.03 of legacy; phase-locked synthetic spindles give R > 0.8
at the trough in both; contract suite passes.
2026-11-18 21:55:30 -06:00

65 lines
2.7 KiB
Python

"""Coupling: modern agrees with legacy; phase-locked spindles give R well above the chance floor."""
from __future__ import annotations
import numpy as np
import pandas as pd
import pytest
from synth_edf import make_edf
from tea_modulo.contracts import Labels, Study
from tea_modulo.contracts.testing import check_biomarker
from tea_modulo.pipeline import Pipeline
from tea_modulo.preprocessing import DIRECTOR_CLUSTER
from conftest import make_prep
@pytest.fixture(scope="module")
def studies(tmp_path_factory):
p = make_edf(tmp_path_factory.mktemp("cpl") / "s.edf", minutes=8)
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_modern_agrees_with_legacy(studies):
m = Pipeline.default("modern").module("coupling").measure(studies[0])
l = Pipeline.default("legacy").module("coupling").measure(studies[1])
assert m["coupling_r"] == pytest.approx(l["coupling_r"], abs=0.03)
assert m["coupling_r_raw"] == pytest.approx(l["coupling_r_raw"], abs=0.03)
def _locked_study():
"""A 1 Hz oscillation on Fz with 13 Hz bursts always at its trough, every 2.3 s."""
prep = make_prep(seconds=240, channels=("Fz", "Cz", "Pz", "C3", "C4"))
raw = prep.variant("hp05_prom")
fs = raw.info["sfreq"]
t = np.arange(raw.n_times) / fs
so = 60e-6 * np.cos(2 * np.pi * 1.0 * t)
rows = []
burst = np.zeros_like(t)
for c in np.arange(5.5, 235, 2.3):
trough = np.floor(c) + 0.5 # cos(2*pi*t) minimum at half seconds
m = np.abs(t - trough) < 0.4
burst[m] += 20e-6 * np.hanning(m.sum()) * np.sin(2 * np.pi * 13 * t[m])
rows.append({"onset_s": trough - 0.4, "duration_s": 0.8, "channel": "Fz", "frequency_hz": 13.0})
raw._data[0] += so + burst
ev = pd.DataFrame(rows)
ev.attrs.update({"minutes": 4.0, "channels": ["Fz"], "searched": True})
return Study(prep).with_labels(Labels("spindle_detection", events=ev))
@pytest.mark.parametrize("impl", ["modern", "legacy"])
def test_phase_locked_spindles_are_coupled(impl):
r = Pipeline.default(impl).module("coupling").measure(_locked_study())
assert r["coupling_r"] > 0.8 and r["coupling_r_over_floor"] > 0.7
assert abs(abs(r["coupling_angle_deg"]) - 180) < 30 # at the trough
def test_biomarker_contract(tmp_path):
pipe = Pipeline.default("modern")
sts = [pipe.study(make_edf(tmp_path / f"s{i}.edf", minutes=6, seed=i), plan=DIRECTOR_CLUSTER,
age_months=20 + 4 * i) for i in range(3)]
from tea_modulo.biomarkers.coupling import Coupling
check_biomarker(Coupling(baseline="age_blocks"), sts, [20, 24, 28])