mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.71k stars 1.32k forks source link

raw.compute_psd("multitaper", ...) on a raw with "bad_xxx" annotations yields all np.nan #12519

Closed mscheltienne closed 6 months ago

mscheltienne commented 7 months ago

Description of the problem

Reported on our site today, it looks like raw.compute_psd() does not handle correctly annotated bad segments. Both on 1.6 and on main. @drammock Maybe you have an idea from the top of your head?

Steps to reproduce

import numpy as np
from mne import Annotations, create_info
from mne.io import RawArray

data = np.random.randn(3, 10000)
info = create_info(ch_names=["A", "B", "C"], sfreq=1000, ch_types="eeg")
raw = RawArray(data, info)
spectrum = raw.compute_psd(
    method="multitaper", fmin=0, fmax=40, picks="eeg", exclude="bads"
)
assert spectrum.get_data().shape[0] == len(raw.ch_names)
assert not np.all(np.isnan(spectrum.get_data()))

# with a bad segment
annotation = Annotations(onset=1, duration=1, description="bad_segment")
raw.set_annotations(annotation)
spectrum = raw.compute_psd(
    method="multitaper", fmin=0, fmax=40, picks="eeg", exclude="bads"
)
assert spectrum.get_data().shape[0] == len(raw.ch_names)
assert np.all(np.isnan(spectrum.get_data()))
larsoner commented 7 months ago

Tangentially related https://github.com/mne-tools/mne-python/issues/11413 could maybe be tackled at the same time