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