Repository: mne-tools/mne-python

Issue:
raw.compute_psd("multitaper", ...) on a raw with "bad_xxx" annotations yields all np.nan
### 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

```python
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()))
```



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

Resolving patch:
diff --git a/mne/io/base.py b/mne/io/base.py
--- a/mne/io/base.py
+++ b/mne/io/base.py
@@ -2197,7 +2197,9 @@ def compute_psd(
         Parameters
         ----------
         %(method_psd)s
-            Default is ``'welch'``.
+            Note that ``"multitaper"`` cannot be used if ``reject_by_annotation=True``
+            and there are ``"bad_*"`` annotations in the :class:`~mne.io.Raw` data;
+            in such cases use ``"welch"``. Default is ``'welch'``.
         %(fmin_fmax_psd)s
         %(tmin_tmax_psd)s
         %(picks_good_data_noref)s
diff --git a/mne/time_frequency/spectrum.py b/mne/time_frequency/spectrum.py
--- a/mne/time_frequency/spectrum.py
+++ b/mne/time_frequency/spectrum.py
@@ -1132,6 +1132,12 @@ def __init__(
             data = self.inst.get_data(
                 self._picks, start, stop + 1, reject_by_annotation=rba
             )
+            if np.any(np.isnan(data)) and method == "multitaper":
+                raise NotImplementedError(
+                    'Cannot use method="multitaper" when reject_by_annotation=True. '
+                    'Please use method="welch" instead.'
+                )
+
         else:  # Evoked
             data = self.inst.data[self._picks][:, self._time_mask]
         # set nave


Test patch:
diff --git a/mne/time_frequency/tests/test_spectrum.py b/mne/time_frequency/tests/test_spectrum.py
--- a/mne/time_frequency/tests/test_spectrum.py
+++ b/mne/time_frequency/tests/test_spectrum.py
@@ -24,6 +24,9 @@ def test_compute_psd_errors(raw):
         raw.compute_psd(foo=None, bar=None)
     with pytest.raises(ValueError, match="Complex output is not supported in "):
         raw.compute_psd(output="complex")
+    raw.set_annotations(Annotations(onset=0.01, duration=0.01, description="bad_foo"))
+    with pytest.raises(NotImplementedError, match='Cannot use method="multitaper"'):
+        raw.compute_psd(method="multitaper", reject_by_annotation=True)
 
 
 @pytest.mark.parametrize("method", ("welch", "multitaper"))