ENH: Add block argument to stc.plot() (gh-14105) - #14185
Open
CedricConday wants to merge 2 commits into
Open
Conversation
`stc.plot()` returns immediately, so running it in a script leaves no opportunity to interact with the figure before the interpreter exits. Unlike `raw.plot()` there was no way to ask for the call to halt until the window is closed. Add `block=False` to `plot_source_estimates`, and thread it through `SourceEstimate.plot` and `VolSourceEstimate.plot_3d`, which share its docstring. The default preserves the current non-blocking behaviour, which is what makes the function usable for generating figures in a script or report. Blocking reuses the mechanism the coregistration GUI already used; that code is now shared as `_qt_block` rather than duplicated. The matplotlib backend blocks via `plt_show`. Closes mne-toolsgh-14105
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #14105.
What
Adds
block=Falsetomne.viz.plot_source_estimates, threaded throughSourceEstimate.plotandVolSourceEstimate.plot_3d.stc.plot()returns immediately, so in a script there is no opportunity tointeract with the figure before the interpreter exits.
raw.plot()hasblock;this gives
stc.plot()the same option.The default is
False, preserving current behaviour — generating figures in ascript or a report is the common case and must not start blocking.
Scope
Deliberately not added to
VectorSourceEstimate.plot/.plot_3d, whichdocument from
plot_vector_source_estimates. Only the two methods that sharethe
plot_source_estimatesdocstring accept it, per @cbrnr's "YAGNI and let'sjust fix this one" and @larsoner's preference not to add
blockeverywhere.VolSourceEstimate.plot_3dhad to be included because it shares that docstringvia
copy_function_doc_to_method_doc— documenting the parameter withoutaccepting it fails
test_docstring_parameters(PR02: Unknown parameters).Implementation
The coregistration GUI already had this logic inline:
Rather than copy it, it is now
_qt_blockinmne/viz/backends/_utils.py, and_coreg.pycalls that. So this removes a duplicate rather than adding one. Thematplotlib backend blocks through the existing
plt_show(True, block=block).Tests
test_qt_block_runs_event_loop— arms aQTimerthat records and quits, thenasserts the recorder ran, so an early return is caught. State-based rather
than a wall-clock threshold, to avoid CI flakiness.
test_qt_block_without_qt_app— notebook backend, and a renderer whoseplotter came from the caller, both return without attempting to block.
test_process_clim_plotgains ablock="yes"→TypeErrorcheck.Verified locally, including end-to-end under Xvfb on the testing dataset:
stc.plot(block=True)returned after 2.55 s against a 2.5 s quit timer, with thetimer confirmed fired, and still returned a
Brain. Removing theboolvalidation makes
stc.plot(block="yes")block forever, which is what that checkprevents.
mne/viz/backends/tests/andmne/tests/test_docstring_parameters.pypass (29 passed, 6 skipped, 1 xfailed);
ruff formatandruff checkare clean.Blocking on the notebook backend is not tested — no notebook backend available
in the environment I verified in.
AI assistance disclosure
Per CONTRIBUTING's policy on AI assistance: this contribution is AI-assisted. The
code, tests and this description were written by Claude (Anthropic), directed by
me. I set the scope, made the design calls above, reviewed the diff, and ran the
verification described. It is not the output of a fully automated tool — every
step was directed and reviewed by me before submission.