Skip to content

Commit 772970f

Browse files
authored
ENH: Add block argument to stc.plot() (gh-14105) (#14185)
1 parent 71850f6 commit 772970f

5 files changed

Lines changed: 21 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a ``block`` parameter to :func:`mne.viz.plot_source_estimates`, :meth:`mne.SourceEstimate.plot` and :meth:`mne.VolSourceEstimate.plot_3d` to halt execution until the figure is closed, by `Cedric Conday`_.

mne/gui/_coreg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
_plot_mri_fiducials,
6666
_plot_sensors_3d,
6767
)
68-
from ..viz.backends._utils import _qt_app_exec, _qt_safe_window
68+
from ..viz.backends._utils import _qt_block, _qt_safe_window
6969
from ..viz.utils import safe_event
7070

7171

@@ -380,8 +380,8 @@ def _get_default(var, val):
380380
self._trans_modified = False
381381
self._mri_fids_modified = False
382382
self._mri_scale_modified = False
383-
if block and self._renderer._kind != "notebook":
384-
_qt_app_exec(self._renderer.figure.store["app"])
383+
if block and self._renderer._kind == "qt":
384+
_qt_block(self._renderer.plotter.app_window)
385385

386386
def _set_subjects_dir(self, subjects_dir):
387387
if subjects_dir is None or not subjects_dir:

mne/source_estimate.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ def plot(
778778
view_layout="vertical",
779779
add_data_kwargs=None,
780780
brain_kwargs=None,
781+
block=False,
781782
verbose=None,
782783
):
783784
from .viz import plot_source_estimates
@@ -813,6 +814,7 @@ def plot(
813814
view_layout=view_layout,
814815
add_data_kwargs=add_data_kwargs,
815816
brain_kwargs=brain_kwargs,
817+
block=block,
816818
verbose=verbose,
817819
)
818820
return brain
@@ -2399,6 +2401,7 @@ def plot_3d(
23992401
view_layout="vertical",
24002402
add_data_kwargs=None,
24012403
brain_kwargs=None,
2404+
block=False,
24022405
verbose=None,
24032406
):
24042407
return super().plot(
@@ -2431,6 +2434,7 @@ def plot_3d(
24312434
view_layout=view_layout,
24322435
add_data_kwargs=add_data_kwargs,
24332436
brain_kwargs=brain_kwargs,
2437+
block=block,
24342438
verbose=verbose,
24352439
)
24362440

mne/viz/_3d.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ def _plot_mpl_stc(
21892189
time_viewer=False,
21902190
colorbar=True,
21912191
transparent=True,
2192+
block=False,
21922193
):
21932194
"""Plot source estimate using mpl."""
21942195
import matplotlib.pyplot as plt
@@ -2328,7 +2329,7 @@ def _plot_mpl_stc(
23282329
cax.tick_params(labelsize=16)
23292330
cb.ax.set_facecolor("0.5")
23302331
cax.set(xlim=(scale_pts[0], scale_pts[2]))
2331-
plt_show(True)
2332+
plt_show(True, block=block)
23322333
return fig
23332334

23342335

@@ -2437,6 +2438,7 @@ def plot_source_estimates(
24372438
view_layout="vertical",
24382439
add_data_kwargs=None,
24392440
brain_kwargs=None,
2441+
block=False,
24402442
verbose=None,
24412443
):
24422444
"""Plot SourceEstimate.
@@ -2538,6 +2540,7 @@ def plot_source_estimates(
25382540
%(view_layout)s
25392541
%(add_data_kwargs)s
25402542
%(brain_kwargs)s
2543+
%(block)s
25412544
%(verbose)s
25422545
25432546
Returns
@@ -2557,12 +2560,14 @@ def plot_source_estimates(
25572560
- https://openwetware.org/wiki/Beauchamp:FreeSurfer
25582561
""" # noqa: E501
25592562
from ..source_estimate import _BaseSourceEstimate, _check_stc_src
2563+
from .backends._utils import _qt_block
25602564
from .backends.renderer import _get_3d_backend, use_3d_backend
25612565

25622566
_check_stc_src(stc, src)
25632567
_validate_type(stc, _BaseSourceEstimate, "stc", "source estimate")
25642568
subjects_dir = get_subjects_dir(subjects_dir=subjects_dir, raise_error=True)
25652569
subject = _check_subject(stc.subject, subject)
2570+
_validate_type(block, bool, "block")
25662571
_check_option("backend", backend, ["auto", "matplotlib", "pyvistaqt", "notebook"])
25672572
plot_mpl = backend == "matplotlib"
25682573
if not plot_mpl:
@@ -2593,10 +2598,10 @@ def plot_source_estimates(
25932598
transparent=transparent,
25942599
)
25952600
if plot_mpl:
2596-
return _plot_mpl_stc(stc, spacing=spacing, **kwargs)
2601+
return _plot_mpl_stc(stc, spacing=spacing, block=block, **kwargs)
25972602
else:
25982603
with use_3d_backend(backend):
2599-
return _plot_stc(
2604+
brain = _plot_stc(
26002605
stc,
26012606
overlay_alpha=alpha,
26022607
brain_alpha=alpha,
@@ -2614,6 +2619,9 @@ def plot_source_estimates(
26142619
title=title,
26152620
**kwargs,
26162621
)
2622+
if block and brain._renderer._kind == "qt":
2623+
_qt_block(brain.plotter.app_window)
2624+
return brain
26172625

26182626

26192627
def _plot_stc(

mne/viz/tests/test_3d.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,8 @@ def test_process_clim_plot(renderer_interactive, brain_gc):
11491149
brain = stc.plot(**kwargs)
11501150
assert brain.data["center"] is None
11511151
brain.close()
1152+
with pytest.raises(TypeError, match="block must be an instance of bool"):
1153+
stc.plot(block="yes", **kwargs)
11521154
brain = stc.plot(clim=dict(pos_lims=(10, 50, 90)), **kwargs)
11531155
assert brain.data["center"] == 0.0
11541156
brain.close()

0 commit comments

Comments
 (0)