Skip to content

Conversation

@Arnav1709
Copy link

Fixes #13142.

What does this implement/fix?

This PR fixes a bug where plotting covariance matrices created by make_ad_hoc_cov() would raise an IndexError: 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)
  • The plot_cov() function's internal _index_info_cov() helper was directly accessing cov.data[ch_idx][:, ch_idx], which fails when cov.data is 1D

Solution:

  • Changed _index_info_cov() to use cov._get_square() instead of directly accessing cov.data
  • The _get_square() method already exists in the Covariance class and properly handles both diagonal (1D) and full (2D) covariance matrices by converting diagonal arrays to full 2D matrices using np.diag() when needed

Changes:

  • Modified mne/viz/misc.py: Updated _index_info_cov() to use _get_square() method
  • Added test in mne/viz/tests/test_misc.py: test_plot_cov_diagonal() to verify diagonal covariance plotting works correctly

Additional information

  • The fix is minimal and uses existing infrastructure (_get_square() method)
  • No breaking changes - full covariance matrices continue to work as before
  • The test case uses make_ad_hoc_cov() to create a diagonal covariance and verifies plotting succeeds without errors

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.

plotting cov from mne.make_ad_hoc_cov leads to error

1 participant