Files
tea/tests/test_aperiodic_mse.py
T
AlbertoPMDM 4deba99c92 aperiodic and mse, legacy and modern
- The cluster route's 2 s window table (ventanas.cortar + estado_director
  exclusions and labels) rebuilt from the contract, one private copy per
  module.
- legacy: biomarcadores espectro.py, aperiodico.py (FOOOF with Wilkinson's
  modifications) and complejidad.py copied unchanged; exponent per region
  and state (NaN under 20 windows or R2 < 0.9); MSE fine / coarse / total.
- modern: MNE multitaper + fooof with the same two Wilkinson modifications;
  antropy sample entropy with Costa's fixed tolerance.

Copies byte-identical to the submodule; modern within 2% (exponent) and 1%
(MSE) of legacy on the synthetic study; contract suites pass.
2026-11-18 21:52:22 -06:00

53 lines
2.0 KiB
Python

"""Aperiodic exponent and multiscale entropy: legacy copies unchanged, modern agrees with legacy."""
from __future__ import annotations
from pathlib import Path
import numpy as np
import pytest
from synth_edf import make_edf
from tea_modulo.contracts.testing import check_biomarker
from tea_modulo.pipeline import Pipeline
from tea_modulo.preprocessing import DIRECTOR_CLUSTER
ROOT = Path(__file__).resolve().parents[1]
BIO = ROOT / "biomarcadores-tea" / "biomarcadores"
LEG = ROOT / "src" / "tea_modulo" / "legacy" / "biomarkers"
@pytest.mark.skipif(not BIO.exists(), reason="submodule absent")
@pytest.mark.parametrize("copy", ["aperiodic/_bio/aperiodico.py", "aperiodic/_bio/espectro.py", "mse/_bio/complejidad.py"])
def test_copies_unchanged(copy):
body = (LEG / copy).read_text(encoding="utf-8").split("\n", 1)[1]
assert body == (BIO / Path(copy).name).read_text(encoding="utf-8")
@pytest.fixture(scope="module")
def studies(tmp_path_factory):
p = make_edf(tmp_path_factory.mktemp("apmse") / "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("name,rel", [("aperiodic", 0.02), ("mse", 0.01)])
def test_modern_agrees_with_legacy(studies, name, rel):
m = Pipeline.default("modern").module(name).measure(studies[0])
l = Pipeline.default("legacy").module(name).measure(studies[1])
assert any(np.isfinite(v) for v in l.values())
for k in m:
if np.isnan(l[k]):
assert np.isnan(m[k]), k
else:
assert m[k] == pytest.approx(l[k], rel=rel), k
@pytest.mark.parametrize("name", ["aperiodic", "mse"])
def test_biomarker_contract(tmp_path, name):
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)]
mod = type(pipe.module(name))(baseline="age_blocks")
check_biomarker(mod, sts, [20, 24, 28])