How to block visualization?

Hi everyone,

I have a problem keeping my visualization window open. I’m trying to run this example. Below is a snippet of the code.

brain_kwargs = dict(alpha=0.1, background="white", cortex="low_contrast")
brain = mne.viz.Brain("sample", subjects_dir=subjects_dir, **brain_kwargs)

stc = mne.read_source_estimate(sample_dir / "sample_audvis-meg")
stc.crop(0.09, 0.1)

kwargs = dict(
    fmin=stc.data.min(),
    fmax=stc.data.max(),
    alpha=0.25,
    smoothing_steps="nearest",
    time=stc.times,
)
brain.add_data(stc.lh_data, hemi="lh", vertices=stc.lh_vertno, **kwargs)
brain.add_data(stc.rh_data, hemi="rh", vertices=stc.rh_vertno, **kwargs)

When I run this cell on Jupyter notebook, my visualization window remains. When I run this as a script, it doesn’t. I’ve tried toggling the block argument in the mne.viz.Brain, but doing so only blocks the program from executing all of the lines below. It won’t include data. Any advice? I appreciate it.

Working environment:

  • mne: 1.5.0
  • Python version: 3.9.6
  • OS: Darwin
  • OS version: 22.6.0

Hello @heybriannguyen and welcome to the forum!

The reason for the windows closing is that Python simply exits once the end of the script is reached. What you can do to prevent this behavior is run python -i your_script.py (interactive mode).

Best wishes,
Richard

4 Likes

Hi Richard,

I appreciate the warm welcome. This community is great. Following your advice, I ran python3 -i your_script.py several times. Even though the window remains open, I still can’t interact with the visualization.

My current strategy is to step through each cleanup and destroy step until the window is open and interactive. I’ve tried running python3 -v your_script.py to inspect all of the steps from the end of the visualization to its close. Is this strategy effective? If not, what are some other things I can try?

Thanks,
Brian

If using IPython is an option, you could try:

  1. Open ipython
  2. %matplotlib
  3. %run your_script.py

Hi Clemens,

While that is possible, unfortunately we need to have this visualization open and interactive.

Best,

Brian

I think we need more info about your setup and how exactly the problem manifests. If I download the example you linked to as a .py file, open a terminal in that folder, and type python -i brain.py I get 6 open windows, all of which I can click-drag in to rotate the brains around.

Can you say more about what happens when you do that? Please also share the output of python -c "import mne; print(mne.sys_info())"

1 Like

This should be the case when using %matplotlib - all figures should open in interactive mode. I guess, as @drammock mentioned, we need more info.

1 Like

@cbrnr the script in question only generates pyvista figures, not matplotlib ones. so I don’t think %matplotlib has any relevant effect here.

Hi Dan,

I appreciate your advice.

When I enter my directory’s pipenv and run python3 -i brain.py (note the lowercase i flag), I see six windows open but remain non-interactive; on my Mac, the color wheel spins indefinitely. No errors are printed out in the terminal. Here is a 45-second video demo of the issue: https://youtu.be/Lp15KkYfWqM

When I run python3 -c "import mne; print(mne.sys_info())", I get

Platform macOS-13.5.2-arm64-arm-64bit
Python 3.9.6 (default, Aug 11 2023, 19:44:49) [Clang 15.0.0 (clang-1500.0.40.1)]
Executable /Users/machine/.local/share/virtualenvs/double_scoop-f6JcJJRI/bin/python3
CPU arm (8 cores)
Memory 16.0 GB

Core
β”œβ˜‘ mne 1.5.1
β”œβ˜‘ numpy 1.23.1 (OpenBLAS 0.3.20 with 8 threads)
β”œβ˜‘ scipy 1.11.2
β”œβ˜‘ matplotlib 3.7.2 (backend=MacOSX)
/Users/machine/.local/share/virtualenvs/double_scoop-f6JcJJRI/lib/python3.9/site-packages/urllib3/init.py:34: NotOpenSSLWarning: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the β€˜ssl’ module is compiled with β€˜LibreSSL 2.8.3’. See: urllib3>=2.0 does not work with system Python on macOS Β· Issue #3020 Β· urllib3/urllib3 Β· GitHub
warnings.warn(
β”œβ˜‘ pooch 1.7.0
β””β˜‘ jinja2 3.1.2

Numerical (optional)
β”œβ˜‘ sklearn 1.3.0
β”œβ˜‘ nibabel 5.1.0
β”œβ˜‘ nilearn 0.10.1
β”œβ˜‘ dipy 1.7.0
β”œβ˜‘ pandas 2.0.3
β””β˜ unavailable numba, openmeeg, cupy

Visualization (optional)
β”œβ˜‘ pyvista 0.41.1 (OpenGL 4.1 Metal - 83.1 via Apple M1)
β”œβ˜‘ pyvistaqt 0.11.0
β”œβ˜‘ vtk 9.2.6
β”œβ˜‘ qtpy 2.4.0 (PySide6=6.3.1)
β”œβ˜‘ pyqtgraph 0.12.4
β”œβ˜‘ ipywidgets 8.1.0
β””β˜ unavailable ipympl, mne-qt-browser, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)
β””β˜ unavailable mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline
None

Thanks,

Brian

@heybriannguyen I don’t know for sure what’s going on but the first thing I would try is upgrading to pyvista 0.42.2 (and adding the trame_* packages listed as unavailable).

@larsoner any idea why pyvista figures would be unresponsive on macOS?

1 Like

Sounds like Loading... . I do not use PySide6 for this reason. Can you live with using PyQt6 instead? If so then I’d uninstall PySide6 and install PyQt6.

4 Likes

Thank you so much @drammock @larsoner. Using PyQt6 instead of PySide, I got it to work! I didn’t need to download the trame_* packages. For those who are running into a similar issue, here is what happens when I run python3 -c "import mne; print(mne.sys_info())".

Platform             macOS-13.5.2-arm64-arm-64bit
Python               3.11.4 (v3.11.4:d2340ef257, Jun  6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)]
Executable           /Users/machine/Library/Caches/pypoetry/virtualenvs/yorick-K8TL7szY-py3.11/bin/python3
CPU                  arm (8 cores)
Memory               16.0 GB

Core
β”œβ˜‘ mne               1.5.1
β”œβ˜‘ numpy             1.26.0 (unknown linalg bindings (threadpoolctl module not found: No module named 'threadpoolctl'))
β”œβ˜‘ scipy             1.11.2
β”œβ˜‘ matplotlib        3.8.0 (backend=MacOSX)
β”œβ˜‘ pooch             1.7.0
β””β˜‘ jinja2            3.1.2

Numerical (optional)
β”œβ˜‘ nibabel           5.1.0
β””β˜ unavailable       sklearn, numba, nilearn, dipy, openmeeg, cupy, pandas

Visualization (optional)
β”œβ˜‘ pyvista           0.42.2 (OpenGL 4.1 Metal - 83.1 via Apple M1)
β”œβ˜‘ pyvistaqt         0.11.0
β”œβ˜‘ vtk               9.2.6
β”œβ˜‘ qtpy              2.4.0 (PyQt6=6.5.2)
β”œβ˜‘ ipywidgets        8.1.1
β””β˜ unavailable       ipympl, pyqtgraph, mne-qt-browser, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)
β””β˜ unavailable       mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline
None

I can’t thank everyone enough.

2 Likes