Files
tea/tests/test_modern_precision.py
T
AlbertoPMDM 6d0a99e6e5 modern: the director's filter and spectra, and gaps stay NaN
- every private _signal.bandpass: scipy butter + sosfiltfilt per gap-free run, as
  eeg_io.pasa_banda (MNE's IIR padded the run edges differently: up to tens of µV)
- preprocessing: variants built as preproceso (band-pass per run, nanmean
  reference); BAD_ACQ_SKIP gaps stay NaN in every variant instead of 0 µV, so no
  module filters or detects across a gap (the director's EDF reader reads them as
  signal; his Natus route keeps NaN)
- aperiodic: DPSS multitaper on demeaned windows with equal taper weights
  (espectro.multitaper) instead of MNE's eigenvalue-weighted one without demeaning
- gradient: symmetric Hann periodogram of demeaned windows (gradiente.potencias)
- tests: preprocessing equal to the director sample by sample (edges included);
  every biomarker equal to legacy within 1e-4 on gap-free recordings
2026-11-19 02:10:15 -06:00

54 lines
2.1 KiB
Python

"""
Modern = legacy on a gap-free recording: since the modern modules use the director's own
filter (scipy sosfiltfilt) and spectra (DPSS multitaper with demeaned windows, symmetric
Hann periodogram), every biomarker agrees with legacy to float32 precision (legacy stores
its variants as float32). With gaps they differ by design: modern keeps gaps as NaN.
"""
from __future__ import annotations
import numpy as np
import pytest
from synth_edf import make_edf
from tea_modulo.pipeline import Pipeline
from tea_modulo.preprocessing import DIRECTOR_CLUSTER
TOL = 1e-4
@pytest.fixture(scope="module")
def tables(tmp_path_factory):
d = tmp_path_factory.mktemp("prec")
st = [("vigilia", 0, 150), ("sueno", 150, 600)]
paths = [make_edf(d / f"p{i}.edf", minutes=10, states=st, seed=i, rhythm_hz=6.5 + i) for i in range(2)]
bios = tuple(Pipeline.default(impl="legacy").biomarker_names())
kw = dict(plan=DIRECTOR_CLUSTER, ages_months=[24, 30], biomarkers=bios)
return (Pipeline.default(impl="legacy").map(paths, **kw),
Pipeline.default(impl="modern").map(paths, cluster="sync", **kw))
def test_every_feature_matches_legacy_without_gaps(tables):
legacy, modern = tables
assert (legacy.error == "").all() and (modern.error == "").all()
skip = {"key", "age_months", "valid", "level", "reasons", "plan_hash", "error", "versions", "seconds",
"clean_fraction", "n_bad_channels"}
feats = [c for c in legacy.columns if c in modern.columns and c not in skip]
assert len(feats) > 60
bad = {}
for f in feats:
a, b = legacy[f].astype(float).to_numpy(), modern[f].astype(float).to_numpy()
if (np.isfinite(a) != np.isfinite(b)).any():
bad[f] = "NaN in one only"
continue
ok = np.isfinite(a)
if ok.any():
tol = TOL * max(1.0, float(np.abs(a[ok]).max()))
if f == "coupling_angle_deg":
d = np.abs((a[ok] - b[ok] + 180) % 360 - 180)
else:
d = np.abs(a[ok] - b[ok])
if d.max() > tol:
bad[f] = float(d.max())
assert not bad, bad