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
This commit is contained in:
AlbertoPMDM
2026-11-19 02:10:15 -06:00
parent 0ee5263419
commit 6d0a99e6e5
13 changed files with 304 additions and 108 deletions
+19 -6
View File
@@ -2,8 +2,11 @@
Modern aperiodic (the cluster route's definitions):
windows clean 2 s windows of each state (at least 20)
spectrum multitaper per window (mne psd_array_multitaper, half-bandwidth 1 Hz on
2 s = 3 DPSS tapers), mean over windows, mean over the region's electrodes
spectrum multitaper per demeaned window: 3 DPSS tapers, NW = 2 (scipy.signal.windows.dpss),
equal weights, one-sided µV²/Hz (as espectro.multitaper); mean over windows,
mean over the region's electrodes. (MNE's psd_array_multitaper weights the
tapers by eigenvalue and does not remove the mean: on 2 s windows fitted from
2.5 Hz the DC leaks into the fit range.)
fit FOOOF 1.1 (fooof) over 2.5-50 Hz, fixed aperiodic mode, with Wilkinson
2024's two modifications (shifted robust aperiodic fit at the 10th
percentile; residual below the aperiodic floor set to zero before the
@@ -71,6 +74,19 @@ def wilkinson_fooof(**kw):
return FOOOFWilkinson(**kw)
def multitaper(segs, fs, k=3):
"""Mean multitaper spectrum over windows. segs: (windows, channels, samples) in µV.
k DPSS tapers with NW = (k + 1) / 2, equal weights, one-sided, µV²/Hz."""
from scipy.fft import rfft, rfftfreq
from scipy.signal.windows import dpss
n = segs.shape[-1]
tapers = dpss(n, NW=(k + 1) / 2, Kmax=k) # k x n
x = segs - segs.mean(axis=-1, keepdims=True)
S = (np.abs(rfft(tapers[None, None] * x[:, :, None], axis=-1)) ** 2).mean(axis=2) / fs
S[..., 1:-1] *= 2
return rfftfreq(n, 1 / fs), S.mean(axis=0)
def exponent(f, spectrum) -> float:
if spectrum is None or not np.all(np.isfinite(spectrum)) or np.any(spectrum <= 0):
return float("nan")
@@ -108,7 +124,6 @@ class Aperiodic:
return r
def measure(self, study) -> dict:
from mne.time_frequency import psd_array_multitaper
reg = self._reg(study)
tabla = _windows2s.table(study, self.variant, P["ventanas.duracion_s"], P["ventanas.borde_s"])
n = int(round(P["ventanas.duracion_s"] * reg.fs))
@@ -118,9 +133,7 @@ class Aperiodic:
segs = [reg.datos[i:i + n].T for i in sel.i0 if np.isfinite(reg.datos[i:i + n]).all()]
if len(sel) < P["ventanas.min_ventanas"] or not segs:
continue
Pw, f = psd_array_multitaper(np.stack(segs), reg.fs, bandwidth=2.0, adaptive=False, low_bias=True,
normalization="full", verbose="error")
Pm = Pw.mean(axis=0) # channels x freqs
f, Pm = multitaper(np.stack(segs), reg.fs, int(P["espectro.tapers"])) # channels x freqs
regions = {**{r: [reg.canales.index(c) for c in cs if c in reg.canales]
for r, cs in P["regiones"].items()}, "todo": list(range(len(reg.canales)))}
for r, idx in regions.items():