diff --git a/src/tea_modulo/biomarkers/coupling/api.py b/src/tea_modulo/biomarkers/coupling/api.py index 5fd419c..e1d8816 100644 --- a/src/tea_modulo/biomarkers/coupling/api.py +++ b/src/tea_modulo/biomarkers/coupling/api.py @@ -38,7 +38,7 @@ def phases(raw, sp, slow=None): from scipy.signal import hilbert fs = float(raw.info["sfreq"]) names = [c for c, t in zip(raw.ch_names, raw.get_channel_types()) if t == "eeg" and c in CHANNELS] - x = raw.get_data(picks=names).T * 1e6 + x = raw.get_data(picks=names).T * 1e6 if names else np.empty((raw.n_times, 0)) lo = _signal.bandpass(x, fs, *SO) ph = _signal.by_runs(lo, lambda s: np.angle(hilbert(s, axis=0)), minimum=int(fs)) env = _signal.by_runs(_signal.bandpass(x, fs, *SIGMA), lambda s: np.abs(hilbert(s, axis=0)), minimum=int(fs)) diff --git a/src/tea_modulo/biomarkers/gradient/api.py b/src/tea_modulo/biomarkers/gradient/api.py index a2468f3..9804fa9 100644 --- a/src/tea_modulo/biomarkers/gradient/api.py +++ b/src/tea_modulo/biomarkers/gradient/api.py @@ -41,6 +41,8 @@ def slopes(raw, starts, window_s=4.0) -> dict: fs = float(raw.info["sfreq"]) chans = [c for c, t in zip(raw.ch_names, raw.get_channel_types()) if t == "eeg" and c in POS_Y] n = int(window_s * fs) + if not chans: + return out x = raw.get_data(picks=chans).T * 1e6 segs = [x[a:a + n].T for a in (int(round(t * fs)) for t in starts) if a + n <= len(x) and np.isfinite(x[a:a + n]).all()] diff --git a/src/tea_modulo/biomarkers/spindle_topography/api.py b/src/tea_modulo/biomarkers/spindle_topography/api.py index 0c23e06..2e0d593 100644 --- a/src/tea_modulo/biomarkers/spindle_topography/api.py +++ b/src/tea_modulo/biomarkers/spindle_topography/api.py @@ -85,7 +85,7 @@ class SpindleTopography: raw = study.prep.variant(self.variant) fs = float(raw.info["sfreq"]) names = [c for c, t in zip(raw.ch_names, raw.get_channel_types()) if t == "eeg"] - x = raw.get_data(picks=names).T * 1e6 + x = raw.get_data(picks=names).T * 1e6 if names else np.empty((raw.n_times, 0)) if self.laplacian: idx = {c: i for i, c in enumerate(names)} y = x.copy() diff --git a/src/tea_modulo/processes/spindle_detection/api.py b/src/tea_modulo/processes/spindle_detection/api.py index dcff182..c0e2953 100644 --- a/src/tea_modulo/processes/spindle_detection/api.py +++ b/src/tea_modulo/processes/spindle_detection/api.py @@ -108,10 +108,10 @@ def detect(x, fs, names, asleep, pct=95.0, min_narrow=1.5, min_enh=3.0): def wake_rhythm(raw, starts) -> float: """Median 3-14 Hz peak of O1 O2 P3 P4 over the wake windows (>= 15, >= 2 channels).""" from mne.time_frequency import psd_array_welch - if len(starts) < 15: + chans = [c for c in ("O1", "O2", "P3", "P4") if c in raw.ch_names] + if len(starts) < 15 or len(chans) < 2: # median of >= 2 channels with a peak return np.nan fs = float(raw.info["sfreq"]) - chans = [c for c in ("O1", "O2", "P3", "P4") if c in raw.ch_names] n = int(4 * fs) x = raw.get_data(picks=chans).T * 1e6 segs = [x[int(round(t * fs)):int(round(t * fs)) + n].T for t in starts @@ -163,7 +163,7 @@ class SpindleDetection: fs = float(raw.info["sfreq"]) lab, w = study.label("sleep_state").per_window, study.prep.windows names = [c for c, t in zip(raw.ch_names, raw.get_channel_types()) if t == "eeg" and c in SPINDLE_CHANNELS] - x = raw.get_data(picks=names).T * 1e6 + x = raw.get_data(picks=names).T * 1e6 if names else np.empty((raw.n_times, 0)) asleep = np.zeros(len(x), bool) n = int(4 * fs) for s, ok in zip(w.start_s, self._sleep(study, lab)): diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index db31b6f..cceff5e 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -5,6 +5,7 @@ from __future__ import annotations import os from pathlib import Path +import numpy as np import pandas as pd import pytest @@ -78,6 +79,20 @@ def test_dask_sync_and_local_equal_legacy_loop(edfs): cluster="local", n_workers=2, memory_per_worker="2GiB"), ref) +def test_no_posterior_channels_is_not_an_error(tmp_path): + """HK1K K7535V: no O1 O2 P3 P4 left. Every module must give NaN / no events, not raise.""" + from synth_edf import AUX, EARS + keep = [c for c in SCALP if c not in ("O1", "O2", "P3", "P4")] + list(EARS) + list(AUX) + p = make_edf(tmp_path / "noposterior.edf", minutes=6, channels=keep) + bios = tuple(Pipeline.default(impl="legacy").biomarker_names()) + for impl in ("legacy", "modern"): + t = Pipeline.default(impl=impl).map([p], plan=DIRECTOR_CLUSTER, ages_months=[24], biomarkers=bios, + **({"cluster": "sync"} if impl == "modern" else {})) + assert t.error.iloc[0] == "", (impl, t.error.iloc[0]) + assert t.valid.iloc[0] == 1, (impl, t.reasons.iloc[0]) + assert not np.isfinite(t.posterior_rhythm_hz.iloc[0]), impl + + def test_positive_priority_refused(): with pytest.raises(ValueError, match="positive priorities"): dask_runner.run(lambda: 1, [()], cluster="sync", priority=1)