diff --git a/doc/changes/dev/13582.bugfix.rst b/doc/changes/dev/13582.bugfix.rst new file mode 100644 index 00000000000..3484e817660 --- /dev/null +++ b/doc/changes/dev/13582.bugfix.rst @@ -0,0 +1 @@ +Fix bug where plotting covariance matrices created by :func:`mne.cov.make_ad_hoc_cov` would raise an IndexError, by :newcontrib:`Arnav Kumar` (:gh:`13582`). diff --git a/doc/changes/names.inc b/doc/changes/names.inc index 5a5a1e98f89..52490fa9ffc 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -29,6 +29,7 @@ .. _Antti Rantala: https://github.com/Odingod .. _Apoorva Karekal: https://github.com/apoorva6262 .. _Archit Singhal: https://github.com/architsinghal-mriirs +.. _Arnav Kumar: https://github.com/Arnav1709 .. _Arne Pelzer: https://github.com/aplzr .. _Ashley Drew: https://github.com/ashdrew .. _Asish Panda: https://github.com/kaichogami diff --git a/mne/viz/misc.py b/mne/viz/misc.py index a62833d7f81..0b71673f3ad 100644 --- a/mne/viz/misc.py +++ b/mne/viz/misc.py @@ -83,7 +83,7 @@ def _index_info_cov(info, cov, exclude): for key in _DATA_CH_TYPES_SPLIT if len(idx_by_type[key]) > 0 ] - C = cov.data[ch_idx][:, ch_idx] + C = cov._get_square()[ch_idx][:, ch_idx] return info, C, ch_names, idx_names diff --git a/mne/viz/tests/test_misc.py b/mne/viz/tests/test_misc.py index 7e4a3b2d806..e727b16b495 100644 --- a/mne/viz/tests/test_misc.py +++ b/mne/viz/tests/test_misc.py @@ -11,6 +11,7 @@ from mne import ( SourceEstimate, + create_info, pick_events, read_cov, read_dipole, @@ -19,6 +20,7 @@ read_source_spaces, ) from mne.chpi import compute_chpi_snr +from mne.cov import make_ad_hoc_cov from mne.datasets import testing from mne.filter import create_filter from mne.io import read_raw_fif @@ -136,6 +138,19 @@ def test_plot_cov(): fig1, fig2 = cov.plot(raw.info) +def test_plot_cov_diagonal(): + """Test plotting of diagonal covariances (e.g., from make_ad_hoc_cov).""" + n_channels = 10 + sfreq = 100 + info = create_info( + [f"EEG{i:03d}" for i in range(n_channels)], sfreq, ch_types="eeg" + ) + cov = make_ad_hoc_cov(info, std={"eeg": 1}) + # This should not raise an IndexError + fig1, fig2 = cov.plot(info) + plt.close("all") + + @testing.requires_testing_data def test_plot_bem(): """Test plotting of BEM contours."""