FIX: Handle diagonal covariances in plot_cov #13582
Open
+18
−1
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