# How to block visualization?

**URL:** <https://mne.discourse.group/t/how-to-block-visualization/7514>\
**Category:** Support & Discussions\
**Tags:** visualization\
**Created:** [September 11, 2023, 12:55am UTC](https://mne.discourse.group/t/how-to-block-visualization/7514 "2023-09-11T00:55:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![heybriannguyen](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/heybriannguyen/32/3027_2.png) [@heybriannguyen](https://mne.discourse.group/u/heybriannguyen)\
**Post date:** [September 11, 2023, 12:55am UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/1 "2023-09-11T00:55:11Z")

</div>

Hi everyone,

I have a problem keeping my visualization window open. I’m trying to run [this example](https://mne.tools/stable/auto_examples/visualization/brain.html#sphx-glr-auto-examples-visualization-brain-py). Below is a snippet of the code.

```python
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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [September 11, 2023, 11:58am UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/2 "2023-09-11T11:58:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![heybriannguyen](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/heybriannguyen/32/3027_2.png) [@heybriannguyen](https://mne.discourse.group/u/heybriannguyen)\
**Post date:** [September 18, 2023, 7:01pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/3 "2023-09-18T19:01:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [September 19, 2023, 5:05am UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/4 "2023-09-19T05:05:11Z")

</div>

If using IPython is an option, you could try:

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

---

<div class="post-metadata">

**Author:** ![heybriannguyen](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/heybriannguyen/32/3027_2.png) [@heybriannguyen](https://mne.discourse.group/u/heybriannguyen)\
**Post date:** [September 19, 2023, 10:19pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/5 "2023-09-19T22:19:42Z")

</div>

Hi Clemens,

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

Best,

Brian

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [September 19, 2023, 10:35pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/6 "2023-09-19T22:35:07Z")

</div>

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())"`

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [September 20, 2023, 5:13am UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/7 "2023-09-20T05:13:43Z")

</div>

> [@heybriannguyen](#):
>
> While that is possible, unfortunately we need to have this visualization open and interactive.

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

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [September 20, 2023, 2:23pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/8 "2023-09-20T14:23:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![heybriannguyen](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/heybriannguyen/32/3027_2.png) [@heybriannguyen](https://mne.discourse.group/u/heybriannguyen)\
**Post date:** [September 20, 2023, 5:50pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/9 "2023-09-20T17:50:03Z")

</div>

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](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](https://github.com/urllib3/urllib3/issues/3020)  
> 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

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [September 20, 2023, 6:52pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/10 "2023-09-20T18:52:48Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [September 21, 2023, 6:06pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/11 "2023-09-21T18:06:55Z")

</div>

Sounds like [Loading...](https://bugreports.qt.io/browse/PYSIDE-2192) . 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.

---

<div class="post-metadata">

**Author:** ![heybriannguyen](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/heybriannguyen/32/3027_2.png) [@heybriannguyen](https://mne.discourse.group/u/heybriannguyen)\
**Post date:** [September 21, 2023, 7:16pm UTC](https://mne.discourse.group/t/how-to-block-visualization/7514/12 "2023-09-21T19:16:56Z")

</div>

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())"`.

```python
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.
