Skip to content

Commit

Permalink
explicitly disallow multitaper in presence of bad annotations (mne-to…
Browse files Browse the repository at this point in the history
  • Loading branch information
drammock authored Apr 9, 2024
1 parent 940ac95 commit 105c8b8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changes/devel/12535.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Calling :meth:`~mne.io.Raw.compute_psd` with ``method="multitaper"`` is now expressly disallowed when ``reject_by_annotation=True`` and ``bad_*`` annotations are present (previously this was nominally allowed but resulted in ``nan`` values in the PSD). By `Daniel McCloy`_.
4 changes: 3 additions & 1 deletion mne/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions mne/time_frequency/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions mne/time_frequency/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 105c8b8

Please sign in to comment.