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

Conversation

mscheltienne
Copy link
Member

@mscheltienne mscheltienne commented Sep 30, 2024

Failure reported today:

import numpy as np
from mne.datasets import sample
from mne.io import read_raw
from mne.time_frequency import SpectrumArray
from numpy.testing import assert_allclose

fname = sample.data_path() / "MEG" / "sample" / "sample_audvis_raw.fif"
raw = read_raw(fname, preload=False).pick("eeg").load_data()

spectrum1 = raw.copy().crop(0, 10).compute_psd()
spectrum2 = raw.copy().crop(10, 20).compute_psd()

# make sure you have the same freqs!
assert_allclose(spectrum1.freqs, spectrum2.freqs)

# array of shape (n_channels, n_freqs)
data1 = spectrum1.get_data()
data2 = spectrum2.get_data()
data = np.array([data1, data2])  # array of shape (n_spectrum, n_channels, n_freqs)
data = np.average(data, axis=0)

# recompute the Spectrum object
spectrum = SpectrumArray(data, spectrum1.info, spectrum1.freqs)
spectrum.plot()

Raises IndexError: index 59 is out of bounds for axis 0 with size 59 in spectrum.plot, i.e. the plotting functions expects the good and bad channels. And indeed, spectrum1 or spectrum2 include the bads, i.e. the underlying array as shape (60, ...).

Now, if instead you include the bad channels:

data1 = spectrum1.get_data(exclude=())
data2 = spectrum2.get_data(exclude=())

It will also raise but this time in the creation of the SpectrumArray: ValueError: The number of channels in data(60) must match the number of good data channels ininfo (59).

There is a clear inconsistency in what SpectrumArray expects and BaseSpectrum.plot expects. I think the cleaner fix is to have SpectrumArray require the good and bad channels; matching the behavior obtained by Spectrum when running inst.compute_psd(...).

@larsoner
Copy link
Member

This one looks real:

FAILED mne/time_frequency/tests/test_spectrum.py::test_spectrum_array_errors - AssertionError: Regex pattern did not match.
 Regex: 'number of channels.*good data channels'
 Input: 'The number of channels in `data` (4) must match the number of good + bads data channels in `info` (5).'

@mscheltienne mscheltienne changed the title Require good and bad channels when creating a SpectrumArray object [MRG] Require good and bad channels when creating a SpectrumArray object Oct 1, 2024
Copy link
Member

@drammock drammock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a few typos. LGTM!

doc/changes/devel/12877.bugfix.rst Outdated Show resolved Hide resolved
mne/time_frequency/spectrum.py Outdated Show resolved Hide resolved
mne/time_frequency/tests/test_spectrum.py Outdated Show resolved Hide resolved
mscheltienne and others added 2 commits October 1, 2024 17:22
@mscheltienne mscheltienne enabled auto-merge (squash) October 1, 2024 16:37
@mscheltienne mscheltienne merged commit 7af60e6 into mne-tools:main Oct 1, 2024
28 checks passed
@mscheltienne mscheltienne deleted the fix-spectrum branch October 2, 2024 07:40
larsoner added a commit to larsoner/mne-python that referenced this pull request Oct 28, 2024
* upstream/main: (824 commits)
  Add `psd_args` to `plot_ica_sources` and `ICA.plot_sources` (mne-tools#12912)
  Fix GDF NumPy >= 2 (mne-tools#12909)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#12908)
  ENH: Improve report usability (mne-tools#12901)
  MAINT: Avoid problematic PySide6 (mne-tools#12902)
  Sync README dependencies with pyproject.toml (mne-tools#12890)
  remove trailing slash from pybv base URL [ci skip] (mne-tools#12892)
  Cast tuple of filenames to list to improve error handling (mne-tools#12891)
  Website (mne-tools#12885)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#12888)
  BUG: Fix bugs with coreg (mne-tools#12884)
  Bump mamba-org/setup-micromamba from 1 to 2 in the actions group (mne-tools#12887)
  Update spacing for comments in pyproject.toml (mne-tools#12886)
  make HTML repr for Forward match others (mne-tools#12883)
  MAINT: Linkchecks [circle deploy] (mne-tools#12882)
  Update roadmap (mne-tools#12872)
  [MRG] Require good and bad channels when creating a SpectrumArray object (mne-tools#12877)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#12879)
  MAINT: Update code credit (mne-tools#12880)
  BUG: Fix bug with Path casting (mne-tools#12878)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants