From 60505e96a7d90a56b48a6e1ea1e9f9f16a3dd87a Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Thu, 14 Mar 2024 16:16:26 -0400 Subject: [PATCH] BUG: Fix bug with too many legend entries (#12498) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- doc/changes/devel/12498.bugfix.rst | 2 ++ examples/preprocessing/muscle_ica.py | 1 - mne/report/report.py | 4 ++-- mne/viz/ica.py | 14 ++++++++++++-- mne/viz/tests/test_ica.py | 3 +++ .../preprocessing/40_artifact_correction_ica.py | 9 ++++----- 6 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 doc/changes/devel/12498.bugfix.rst diff --git a/doc/changes/devel/12498.bugfix.rst b/doc/changes/devel/12498.bugfix.rst new file mode 100644 index 00000000000..2655cf692d1 --- /dev/null +++ b/doc/changes/devel/12498.bugfix.rst @@ -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`_. diff --git a/examples/preprocessing/muscle_ica.py b/examples/preprocessing/muscle_ica.py index f57e24a678b..64c14f5f5af 100644 --- a/examples/preprocessing/muscle_ica.py +++ b/examples/preprocessing/muscle_ica.py @@ -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 # diff --git a/mne/report/report.py b/mne/report/report.py index 43c3d7c7ac4..6519d5cbb06 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -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) diff --git a/mne/viz/ica.py b/mne/viz/ica.py index e2eb6273cb3..1ec18fde1da 100644 --- a/mne/viz/ica.py +++ b/mne/viz/ica.py @@ -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( diff --git a/mne/viz/tests/test_ica.py b/mne/viz/tests/test_ica.py index 7972e4d36b6..39d4b616431 100644 --- a/mne/viz/tests/test_ica.py +++ b/mne/viz/tests/test_ica.py @@ -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] diff --git a/tutorials/preprocessing/40_artifact_correction_ica.py b/tutorials/preprocessing/40_artifact_correction_ica.py index 3e0698a0efe..7c7c872ff70 100644 --- a/tutorials/preprocessing/40_artifact_correction_ica.py +++ b/tutorials/preprocessing/40_artifact_correction_ica.py @@ -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