window_selection and spikes, legacy and modern
- legacy: seleccion.seleccionar and candidatos_punta.de_estudio + indicadores vendored unchanged, run on the contract adapter with the sleep_state labels as the vigilancia table. - modern: the director's detector functions copied by name into _detector.py with three substitutions (MNE band-pass, channel lists from the study, a private port of the seleccion gate); a modern window_selection on the same port. - synthetic builder: optional focal spike-and-wave (the director's own _punta shape) with a field on the neighbours. Modern equals legacy exactly on window verdicts and spike indicators; the C3 focus is found; contract suites pass.
This commit is contained in:
@@ -154,6 +154,18 @@ def make_signals(*, fs=250.0, minutes=6.0, states=None, rhythm_hz=7.0, line_hz=6
|
||||
|
||||
def _apply_faults(data, bad, rng, fs):
|
||||
n = len(next(iter(data.values())))
|
||||
for ch in bad.get("spikes", []): # focal spike-and-wave every ~3 s with a field
|
||||
# the director's own synthetic spike (candidatos_punta._punta): 50 ms, then a slow wave
|
||||
m = int(0.05 * fs)
|
||||
t = np.linspace(-1, 1, max(m, 3))
|
||||
shape = np.concatenate([250 * (1 - t ** 2) ** 2,
|
||||
-100 * np.sin(np.pi * np.linspace(0, 1, int(0.3 * fs)))])
|
||||
nb = {"C3": ("F3", "T3", "Cz", "P3"), "C4": ("F4", "T4", "Cz", "P4")}.get(ch, ())
|
||||
for c in np.arange(3.0, n / fs - 2, 3.1):
|
||||
i0 = int(c * fs)
|
||||
data[ch][i0:i0 + len(shape)] += shape
|
||||
for v in nb:
|
||||
data[v][i0:i0 + len(shape)] += 0.6 * shape
|
||||
for ch in bad.get("flat", []):
|
||||
data[ch] = np.zeros(n) + rng.normal(0, 0.05, n)
|
||||
for ch in bad.get("frozen", []):
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Window selection and spikes: modern equals legacy; the focus is found."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from synth_edf import make_edf
|
||||
from tea_modulo.contracts import Study
|
||||
from tea_modulo.contracts.testing import check_biomarker, check_process
|
||||
from tea_modulo.pipeline import Pipeline
|
||||
from tea_modulo.preprocessing import DIRECTOR_CLUSTER
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def studies(tmp_path_factory):
|
||||
p = make_edf(tmp_path_factory.mktemp("spk") / "s.edf", minutes=8, bad={"spikes": ["C3"]})
|
||||
return (Pipeline.default("modern").study(p, plan=DIRECTOR_CLUSTER, age_months=28),
|
||||
Pipeline.default("legacy").study(p, plan=DIRECTOR_CLUSTER, age_months=28))
|
||||
|
||||
|
||||
def test_window_selection_equal(studies):
|
||||
a = studies[0].label("window_selection").events
|
||||
b = studies[1].label("window_selection").events
|
||||
assert len(a) == len(b) and (a.apto.values == b.apto.values).all()
|
||||
|
||||
|
||||
def test_spike_indicators_equal(studies):
|
||||
im = Pipeline.default("modern").module("spikes").indicators(studies[0])
|
||||
il = Pipeline.default("legacy").module("spikes").indicators(studies[1])
|
||||
for k in ("n_total", "n_util", "canal_foco", "foco_frac", "lateralidad", "tasa_NREM", "tasa_despierto"):
|
||||
a, b = im[k], il[k]
|
||||
assert (isinstance(a, float) and np.isnan(a) and np.isnan(b)) or a == b, k
|
||||
assert im["n_total"] > 100 and im["canal_foco"] == "C3"
|
||||
|
||||
|
||||
def test_contracts(studies):
|
||||
for impl, s in zip(("modern", "legacy"), studies):
|
||||
base = Study(s.prep, {k: v for k, v in s.labels.items() if k == "annotation_mask"})
|
||||
check_process(Pipeline.default(impl).module("window_selection"), base)
|
||||
|
||||
|
||||
def test_biomarker_contract(tmp_path):
|
||||
pipe = Pipeline.default("modern")
|
||||
sts = [pipe.study(make_edf(tmp_path / f"s{i}.edf", minutes=5, seed=i, bad={"spikes": ["C3"]}),
|
||||
plan=DIRECTOR_CLUSTER, age_months=20 + 4 * i) for i in range(3)]
|
||||
from tea_modulo.biomarkers.spikes import Spikes
|
||||
check_biomarker(Spikes(baseline="age_blocks"), sts, [20, 24, 28])
|
||||
Reference in New Issue
Block a user