Skip to content

Commit

Permalink
BUG: Fix bug with too many legend entries (#12498)
Browse files Browse the repository at this point in the history
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
larsoner and autofix-ci[bot] authored Mar 14, 2024
1 parent 2a97333 commit 60505e9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 2 additions & 0 deletions doc/changes/devel/12498.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug with :meth:`mne.preprocessing.ICA.plot_sources` for ``evoked`` data where the
legend contained too many entries, by `Eric Larson`_.
1 change: 0 additions & 1 deletion examples/preprocessing/muscle_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
artifact is produced during postural maintenance. This is more appropriately
removed by ICA otherwise there wouldn't be any epochs left! Note that muscle
artifacts of this kind are much more pronounced in EEG than they are in MEG.
"""
# Authors: Alex Rockhill <[email protected]>
#
Expand Down
4 changes: 2 additions & 2 deletions mne/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ def _fig_to_img(fig, *, image_format="png", own_figure=True):
if pil_kwargs:
# matplotlib modifies the passed dict, which is a bug
mpl_kwargs["pil_kwargs"] = pil_kwargs.copy()
with warnings.catch_warnings():
fig.savefig(output, format=image_format, dpi=dpi, **mpl_kwargs)

fig.savefig(output, format=image_format, dpi=dpi, **mpl_kwargs)

if own_figure:
plt.close(fig)
Expand Down
14 changes: 12 additions & 2 deletions mne/viz/ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,18 @@ def _plot_ica_sources_evoked(evoked, picks, exclude, title, show, ica, labels=No
lines[-1].set_pickradius(3.0)

ax.set(title=title, xlim=times[[0, -1]], xlabel="Time (ms)", ylabel="(NA)")
if len(lines):
ax.legend(lines, exclude_labels, loc="best")
leg_lines_labels = list(
zip(
*[
(line, label)
for line, label in zip(lines, exclude_labels)
if label is not None
]
)
)
if len(leg_lines_labels):
leg_lines, leg_labels = leg_lines_labels
ax.legend(leg_lines, leg_labels, loc="best")

texts.append(
ax.text(
Expand Down
3 changes: 3 additions & 0 deletions mne/viz/tests/test_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,15 @@ def test_plot_ica_sources(raw_orig, browser_backend, monkeypatch):
ica.plot_sources(epochs)
ica.plot_sources(epochs.average())
evoked = epochs.average()
ica.exclude = [0]
fig = ica.plot_sources(evoked)
# Test a click
ax = fig.get_axes()[0]
line = ax.lines[0]
_fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], "data")
_fake_click(fig, ax, [ax.get_xlim()[0], ax.get_ylim()[1]], "data")
leg = ax.get_legend()
assert len(leg.get_texts()) == len(ica.exclude) == 1

# plot with bad channels excluded
ica.exclude = [0]
Expand Down
9 changes: 4 additions & 5 deletions tutorials/preprocessing/40_artifact_correction_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,10 @@
ica.plot_sources(eog_evoked)

# %%
# Note that above we used `~mne.preprocessing.ICA.plot_sources` on both
# the original `~mne.io.Raw` instance and also on an
# `~mne.Evoked` instance of the extracted EOG artifacts. This can be
# another way to confirm that `~mne.preprocessing.ICA.find_bads_eog` has
# identified the correct components.
# Note that above we used :meth:`~mne.preprocessing.ICA.plot_sources` on both the
# original :class:`~mne.io.Raw` instance and also on an `~mne.Evoked` instance of the
# extracted EOG artifacts. This can be another way to confirm that
# :meth:`~mne.preprocessing.ICA.find_bads_eog` has identified the correct components.
#
#
# Using a simulated channel to select ICA components
Expand Down

0 comments on commit 60505e9

Please sign in to comment.