FIX: Handle diagonal covariances in plot_cov#13582
Open
Arnav1709 wants to merge 5 commits intomne-tools:mainfrom
Open
FIX: Handle diagonal covariances in plot_cov#13582Arnav1709 wants to merge 5 commits intomne-tools:mainfrom
Arnav1709 wants to merge 5 commits intomne-tools:mainfrom
Conversation
0051bbf to
9d9f2d2
Compare
larsoner
reviewed
Jan 29, 2026
Member
larsoner
left a comment
There was a problem hiding this comment.
Sorry for the slow response, just a few tweaks!
Was this PR generated or assisted by an LLM?
| @@ -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`). | |||
Member
There was a problem hiding this comment.
Suggested change
| 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`). | |
| Fix bug where plotting covariance matrices created by :func:`mne.cov.make_ad_hoc_cov` would raise an IndexError, by :newcontrib:`Arnav Kumar`. |
| cov = make_ad_hoc_cov(info, std={"eeg": 1}) | ||
| # This should not raise an IndexError | ||
| fig1, fig2 = cov.plot(info) | ||
| plt.close("all") |
Member
There was a problem hiding this comment.
Not needed, we have a pytest fixture that does this part
| ) | ||
| cov = make_ad_hoc_cov(info, std={"eeg": 1}) | ||
| # This should not raise an IndexError | ||
| fig1, fig2 = cov.plot(info) |
Member
There was a problem hiding this comment.
Outputs are unused so no need to assign
Suggested change
| fig1, fig2 = cov.plot(info) | |
| cov.plot(info) |
Comment on lines
+145
to
+147
| info = create_info( | ||
| [f"EEG{i:03d}" for i in range(n_channels)], sfreq, ch_types="eeg" | ||
| ) |
Member
There was a problem hiding this comment.
This will do the same thing more compactly!
Suggested change
| info = create_info( | |
| [f"EEG{i:03d}" for i in range(n_channels)], sfreq, ch_types="eeg" | |
| ) | |
| info = create_info(n_channels, sfreq, ch_types="eeg") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13142.
What does this implement/fix?
This PR fixes a bug where plotting covariance matrices created by
make_ad_hoc_cov()would raise anIndexError: too many indices for array: array is 1-dimensional, but 2 were indexed.Root cause:
make_ad_hoc_cov()creates diagonal covariance matrices stored as 1D arrays (the diagonal values)plot_cov()function's internal_index_info_cov()helper was directly accessingcov.data[ch_idx][:, ch_idx], which fails whencov.datais 1DSolution:
_index_info_cov()to usecov._get_square()instead of directly accessingcov.data_get_square()method already exists in theCovarianceclass and properly handles both diagonal (1D) and full (2D) covariance matrices by converting diagonal arrays to full 2D matrices usingnp.diag()when neededChanges:
mne/viz/misc.py: Updated_index_info_cov()to use_get_square()methodmne/viz/tests/test_misc.py:test_plot_cov_diagonal()to verify diagonal covariance plotting works correctlyAdditional information
_get_square()method)make_ad_hoc_cov()to create a diagonal covariance and verifies plotting succeeds without errors