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