Skip to content

Commit 0b56ea2

Browse files
authored
Make blocking consistent (#14098)
1 parent 102939d commit 0b56ea2

9 files changed

Lines changed: 55 additions & 19 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ``block`` parameter of :meth:`mne.time_frequency.Spectrum.plot_topo`, the legacy ``plot_psd_topo`` methods, and ``mne.viz.plot_raw_psd_topo`` is deprecated and will be removed in MNE 1.15; these plots now follow Matplotlib's blocking behavior, by `Clemens Brunner`_.

doc/changes/dev/14098.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed :func:`~mne.viz.plot_evoked_topomap` and :func:`~mne.viz.plot_sensors` (and the ``plot_sensors`` methods) to follow Matplotlib's blocking behavior when ``show=True`` instead of returning immediately; ``plot_sensors`` keeps its ``block`` argument (default now ``None``), by `Clemens Brunner`_.

mne/_fiff/meas_info.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def plot_sensors(
690690
ch_groups=None,
691691
to_sphere=True,
692692
axes=None,
693-
block=False,
693+
block=None,
694694
show=True,
695695
sphere=None,
696696
*,
@@ -736,11 +736,18 @@ def plot_sensors(
736736
instance of Axes3D. If None (default), a new axes will be created.
737737
738738
.. versionadded:: 0.13.0
739-
block : bool
740-
Whether to halt program execution until the figure is closed.
741-
Defaults to False.
739+
block : bool | None
740+
Whether to halt program execution until the figure is closed. By default
741+
(``None``) this follows :func:`matplotlib.pyplot.show`: it blocks unless
742+
Matplotlib's interactive mode is on (see :func:`matplotlib.pyplot.ion`), in
743+
which case it returns immediately. Set to ``True`` to force blocking, which
744+
is useful with ``kind="select"`` to collect the interactive selection
745+
synchronously when interactive mode is on.
742746
743747
.. versionadded:: 0.13.0
748+
.. versionchanged:: 1.13
749+
The default changed from ``False`` to ``None`` (follow
750+
Matplotlib).
744751
show : bool
745752
Show figure if True. Defaults to True.
746753
%(sphere_topomap_auto)s

mne/time_frequency/spectrum.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def plot_psd_topo(
161161
fig_facecolor="k",
162162
axis_facecolor="k",
163163
axes=None,
164-
block=False,
164+
block=None,
165165
show=True,
166166
n_jobs=None,
167167
verbose=None,
@@ -181,7 +181,9 @@ def plot_psd_topo(
181181
%(fig_facecolor)s
182182
%(axis_facecolor)s
183183
%(axes_spectrum_plot_topo)s
184-
%(block)s
184+
block : bool
185+
This parameter is deprecated and will be removed in MNE 1.15; blocking now
186+
follows Matplotlib's behavior (see ``show``).
185187
%(show)s
186188
%(n_jobs)s
187189
%(verbose)s
@@ -726,7 +728,7 @@ def plot_topo(
726728
fig_facecolor="k",
727729
axis_facecolor="k",
728730
axes=None,
729-
block=False,
731+
block=None,
730732
show=True,
731733
):
732734
"""Plot power spectral density, separately for each channel.
@@ -739,7 +741,9 @@ def plot_topo(
739741
%(fig_facecolor)s
740742
%(axis_facecolor)s
741743
%(axes_spectrum_plot_topo)s
742-
%(block)s
744+
block : bool | None
745+
This parameter is deprecated and will be removed in MNE 1.15; blocking now
746+
follows Matplotlib's behavior (see ``show``).
743747
%(show)s
744748
745749
Returns
@@ -777,7 +781,16 @@ def plot_topo(
777781
y_label=y_label,
778782
axes=axes,
779783
)
780-
plt_show(show, block=block)
784+
if block is None:
785+
plt_show(show)
786+
else:
787+
warn(
788+
"The 'block' parameter is deprecated and will be removed in MNE 1.15; "
789+
"blocking now follows Matplotlib's behavior. Pass show=False and call "
790+
"matplotlib.pyplot.show() to control it.",
791+
FutureWarning,
792+
)
793+
plt_show(show, block=block)
781794
return fig
782795

783796
@fill_doc

mne/time_frequency/tests/test_spectrum.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ def test_plot_spectrum(method, output, average, request):
718718
n_bad = sum(same_color(line.get_color(), bad_color) for line in lines)
719719
assert n_bad == 1
720720
spectrum.plot_topo()
721+
with pytest.warns(FutureWarning, match="'block' parameter is deprecated"):
722+
spectrum.plot_topo(block=True)
721723
spectrum.plot_topomap()
722724

723725

mne/utils/docs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4265,7 +4265,13 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
42654265

42664266
docdict["show"] = """\
42674267
show : bool
4268-
Show the figure if ``True``.
4268+
Show the figure if ``True``. When shown, blocking follows
4269+
:func:`matplotlib.pyplot.show`: the call blocks until the window is closed unless
4270+
Matplotlib's interactive mode is on (enabled with :func:`matplotlib.pyplot.ion` or
4271+
IPython's ``%%matplotlib`` magic command), in which case it returns immediately.
4272+
Interactive mode is off by default, so a plain script or REPL blocks. Pass
4273+
``show=False`` to build several figures and display them together with a single
4274+
:func:`matplotlib.pyplot.show` call.
42694275
"""
42704276

42714277
docdict["show_names_topomap"] = """

mne/viz/raw.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def plot_raw_psd_topo(
554554
fig_facecolor="k",
555555
axis_facecolor="k",
556556
axes=None,
557-
block=False,
557+
block=None,
558558
show=True,
559559
n_jobs=None,
560560
verbose=None,
@@ -587,9 +587,9 @@ def plot_raw_psd_topo(
587587
A matplotlib-compatible color to use for the axis background.
588588
Defaults to black.
589589
%(axes_spectrum_plot_topo)s
590-
block : bool
591-
Whether to halt program execution until the figure is closed.
592-
May not work on all systems / platforms. Defaults to False.
590+
block : bool | None
591+
This parameter is deprecated and will be removed in MNE 1.15; blocking now
592+
follows Matplotlib's behavior (see ``show``).
593593
%(show)s
594594
%(n_jobs)s
595595
%(verbose)s

mne/viz/topomap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,7 @@ def plot_evoked_topomap(
24202420
interactive_colorbar=True,
24212421
single_time_point=False,
24222422
)
2423-
plt_show(show, block=False)
2423+
plt_show(show)
24242424
if axes is not None:
24252425
fig.canvas.draw()
24262426
return fig

mne/viz/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ def plot_sensors(
941941
ch_groups=None,
942942
to_sphere=True,
943943
axes=None,
944-
block=False,
944+
block=None,
945945
show=True,
946946
sphere=None,
947947
pointsize=None,
@@ -992,11 +992,17 @@ def plot_sensors(
992992
%(axes_montage)s
993993
994994
.. versionadded:: 0.13.0
995-
block : bool
996-
Whether to halt program execution until the figure is closed. Defaults
997-
to False.
995+
block : bool | None
996+
Whether to halt program execution until the figure is closed. By default
997+
(``None``) this follows :func:`matplotlib.pyplot.show`: it blocks unless
998+
Matplotlib's interactive mode is on (see :func:`matplotlib.pyplot.ion`), in
999+
which case it returns immediately. Set to ``True`` to force blocking, which is
1000+
useful with ``kind="select"`` to collect the interactive selection synchronously
1001+
when interactive mode is on.
9981002
9991003
.. versionadded:: 0.13.0
1004+
.. versionchanged:: 1.13
1005+
The default changed from ``False`` to ``None`` (follow Matplotlib).
10001006
show : bool
10011007
Show figure if True. Defaults to True.
10021008
%(sphere_topomap_auto)s

0 commit comments

Comments
 (0)