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:
@@ -4,10 +4,11 @@ Modern preprocessing: a PreprocessPlan executed with MNE.
|
||||
Order (as preproceso.procesar):
|
||||
read EDF (MNE) -> drop bad channels (plan.bads) -> line removal on the raw signal
|
||||
(interpolated notch if the line is >= min_db over its neighbourhood, measured on the
|
||||
kept scalp channels; or ZapLine, meegkit) -> per variant: high-pass + low-pass
|
||||
(Raw.filter, IIR Butterworth order 4, zero phase, run by run between BAD_ACQ_SKIP)
|
||||
-> reference (Raw.set_eeg_reference: average of the kept scalp channels, or the
|
||||
linked ears A1+A2). EOG / ECG are filtered and never re-referenced.
|
||||
kept scalp channels; or ZapLine, meegkit) -> per variant: one Butterworth band-pass
|
||||
(order 4, zero phase: scipy sosfiltfilt, as eeg_io.pasa_banda) on each gap-free run
|
||||
-> reference (nanmean of the kept scalp channels, or of the linked ears A1+A2).
|
||||
EOG / ECG are filtered and never re-referenced. Gaps (BAD_ACQ_SKIP) stay NaN in
|
||||
every variant, as in the director's npz: modules see a gap as NaN, never as 0 µV.
|
||||
|
||||
Windows: the plan's grid (window_s / step_s), flagged from a 4 s back-to-back
|
||||
window QC (private copy of the validation's rules); quality_spans are the QC
|
||||
@@ -17,6 +18,7 @@ windows that are not clean.
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import warnings
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
@@ -28,7 +30,6 @@ from tea_modulo.contracts import CONTRACT_VERSION, Preprocessed, ValidationSumma
|
||||
from . import _channels, _qc, _signal
|
||||
from .plan import PreprocessPlan
|
||||
|
||||
GAP = ("BAD_ACQ_SKIP", "edge")
|
||||
KEEP = ("eeg", "orejas", "eog", "cardiaco")
|
||||
_TYPE = {"eeg": "eeg", "orejas": "eeg", "eog": "eog", "cardiaco": "ecg"}
|
||||
|
||||
@@ -89,22 +90,34 @@ class Preprocessing:
|
||||
x = _zapline(x, fs, p.line_hz, int(zap.params.get("nremove", 1)))
|
||||
removal = "zapline"
|
||||
gaps = ~np.isfinite(x).all(axis=1)
|
||||
raw._data[:] = np.nan_to_num(x, nan=0.0).T * 1e-6
|
||||
|
||||
# variants as the director builds them: one Butterworth band-pass per gap-free run
|
||||
# (NaN stays NaN), then the reference with nanmean, so a gap is still a gap (NaN) in
|
||||
# every variant and no module filters across it
|
||||
lp = p.step("lowpass").params["hz"] if p.step("lowpass") else None
|
||||
iir = {"order": 4, "ftype": "butter"}
|
||||
idx_ears = [keep.index(c) for c in ears]
|
||||
others = [i for i, c in enumerate(keep) if c not in ears]
|
||||
aux_idx = [i for i in others if i not in idx_scalp]
|
||||
base_raw = raw.copy().drop_channels(ears)
|
||||
variants = {}
|
||||
for v in p.variants:
|
||||
r = raw.copy().filter(v.highpass_hz, lp, method="iir", iir_params=iir, phase="zero",
|
||||
skip_by_annotation=GAP, verbose="error")
|
||||
if v.reference == "linked_ears":
|
||||
if len(ears) != 2:
|
||||
raise ValueError(f"variant {v.name}: linked ears need both A1 and A2 usable")
|
||||
r.set_eeg_reference(ears, verbose="error")
|
||||
r.drop_channels(ears)
|
||||
else:
|
||||
r.drop_channels(ears)
|
||||
r.set_eeg_reference("average", verbose="error")
|
||||
y = _signal.bandpass(x, fs, v.highpass_hz, lp)
|
||||
cue = y[:, idx_scalp]
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", RuntimeWarning) # nanmean of a gap row
|
||||
if v.reference == "linked_ears":
|
||||
if len(ears) != 2:
|
||||
raise ValueError(f"variant {v.name}: linked ears need both A1 and A2 usable")
|
||||
ref = np.nanmean(y[:, idx_ears], axis=1, keepdims=True)
|
||||
else:
|
||||
ref = np.nanmean(cue, axis=1, keepdims=True)
|
||||
out = np.empty((len(y), len(others)))
|
||||
pos = {i: k for k, i in enumerate(others)}
|
||||
out[:, [pos[i] for i in idx_scalp]] = cue - ref
|
||||
if aux_idx:
|
||||
out[:, [pos[i] for i in aux_idx]] = y[:, aux_idx] # EOG/EKG: not re-referenced
|
||||
r = base_raw.copy()
|
||||
r._data = out.T * 1e-6
|
||||
variants[v.name] = r
|
||||
|
||||
# window QC (4 s back-to-back) on the kept scalp channels, notch + 0.5-45 Hz
|
||||
|
||||
Reference in New Issue
Block a user