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

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

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

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`).
Copy link
Member

Choose a reason for hiding this comment

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

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")
Copy link
Member

Choose a reason for hiding this comment

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

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"
)
Copy link
Member

Choose a reason for hiding this comment

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

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")

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

2 participants