Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Require good and bad channels when creating a SpectrumArray object #12877

Merged
merged 7 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/changes/devel/12877.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug when creating a :class:`~mne.time_frequency.SpectrumArray`. It nows requires
both 'good' and 'bad' data channels to be present in the :class:`~mne.Info` and in the
:class:`numpy.ndarray`, by `Mathieu Scheltienne`_.
mscheltienne marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions mne/time_frequency/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,12 +1238,12 @@ def _check_data_shape(data, info, freqs, dim_names, weights, is_epoched):
f"'channel' must be the {'second' if is_epoched else 'first'} dimension of "
"the data."
)
want_n_chan = _pick_data_channels(info).size
want_n_chan = _pick_data_channels(info, exclude=()).size
got_n_chan = data.shape[list(dim_names).index("channel")]
if got_n_chan != want_n_chan:
raise ValueError(
f"The number of channels in `data` ({got_n_chan}) must match the number of "
f"good data channels in `info` ({want_n_chan})."
f"good + bad data channels in `info` ({want_n_chan})."
)

# given we limit max array size and ensure channel & freq dims present, only one of
Expand Down
20 changes: 19 additions & 1 deletion mne/time_frequency/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

import re
from functools import partial

import numpy as np
Expand Down Expand Up @@ -503,7 +504,9 @@ def test_spectrum_array_errors():
with pytest.raises(ValueError, match="'channel' must be the second dimension"):
EpochsSpectrumArray(data, info, freqs, dim_names=("epoch", "freq", "channel"))
# test mismatching number of channels
with pytest.raises(ValueError, match=r"number of channels.*good data channels"):
with pytest.raises(
ValueError, match=re.escape("number of good + bad data channels")
):
EpochsSpectrumArray(data[:, :-1, :], info, freqs, dim_names=dim_names)
# test incorrect taper position
with pytest.raises(ValueError, match="'taper' must be the second to last dim"):
Expand Down Expand Up @@ -609,3 +612,18 @@ def test_plot_spectrum(method, output, average, request):
assert n_bad == 1
spectrum.plot_topo()
spectrum.plot_topomap()


def test_plot_spectrum_array_with_bads():
"""Test plotting a spectrum array with bads."""
raw = RawArray(np.random.randn(3, 1000), create_info(3, 1000, "eeg"))
raw.info["bads"] = [raw.ch_names[1]]
spectrum = raw.compute_psd()
with pytest.raises(
ValueError, match=re.escape("match the number of good + bads data channels")
):
SpectrumArray(spectrum.get_data(), spectrum.info, spectrum.freqs)
spectrum2 = SpectrumArray(
spectrum.get_data(exclude=()), spectrum.info, spectrum.freqs
)
spectrum2.plot(spatial_colors=False)
Loading