"""Spindle topography: modern agrees with legacy; spindle channels stand out.""" from __future__ import annotations import numpy as np import pytest from synth_edf import SPINDLE, make_edf from tea_modulo.contracts.testing import check_biomarker from tea_modulo.pipeline import Pipeline from tea_modulo.preprocessing import DIRECTOR_CLUSTER @pytest.fixture(scope="module") def studies(tmp_path_factory): p = make_edf(tmp_path_factory.mktemp("topo") / "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)) @pytest.mark.parametrize("laplacian", [False, True]) def test_modern_agrees_with_legacy(studies, laplacian): from tea_modulo.biomarkers.spindle_topography import SpindleTopography from tea_modulo.legacy.biomarkers.spindle_topography import LegacySpindleTopography m = SpindleTopography(laplacian=laplacian).measure(studies[0]) l = LegacySpindleTopography(laplacian=laplacian).measure(studies[1]) for k in m: assert (np.isnan(m[k]) and np.isnan(l[k])) or m[k] == pytest.approx(l[k], rel=0.02), k def test_spindle_channels_stand_out(studies): m = Pipeline.default("modern").module("spindle_topography").measure(studies[0]) inside = np.mean([m[f"topo_enh_{c}"] for c in SPINDLE]) outside = np.mean([m[f"topo_enh_{c}"] for c in ("Fp1", "Fp2", "F7", "F8", "P3", "P4", "Pz")]) assert inside > 1.5 * outside 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.spindle_topography import SpindleTopography check_biomarker(SpindleTopography(baseline="age_blocks"), sts, [20, 24, 28])