Problems running mne.viz.plot_alignment() from within Spyder

After reading this thread I’m checking whether there’s been any developments on this topic, specifically for linux platform? I’m trying to use mne.viz.plot_alignment (which doesn’t have “block=” argument). The only way I’ve found to keep the plot window open is to execute the statement in a VSCode Jupyter notebook. Does anyone know whether other IDEs have yet recovered the previous Spyder capabilities? Or whether Spyder will ever work again with MNE?

@pettetmw I am able to run this in Spyder’s interactive console on Linux, and then interact with the figure (which stays popped open):

import os.path as op
import mne
data_path = mne.datasets.sample.data_path()
subjects_dir = op.join(data_path, 'subjects')
raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
trans_fname = op.join(data_path, 'MEG', 'sample',
                      'sample_audvis_raw-trans.fif')
raw = mne.io.read_raw_fif(raw_fname)
trans = mne.read_trans(trans_fname)
src = mne.read_source_spaces(op.join(subjects_dir, 'sample', 'bem',
                                     'sample-oct-6-src.fif'))

fig = mne.viz.plot_alignment(raw.info, trans=trans, subject='sample',
                             subjects_dir=subjects_dir, surfaces='head-dense',
                             show_axes=True, dig=True, eeg=[], meg='sensors',
                             coord_frame='meg', mri_fiducials='estimated')

It also works if I click “run file” in spyder. Does that not work for you?

Works until call to plot_alignment(), which throws the following:
RuntimeError: Could not load any valid 3D backend: pyvista, mayavi, notebook

This is what I saw before. It seems that spyder, when invoked from conda “base”, isn’t fully utilizing my conda mne environment, even though I’ve set spyder preferences to use the mne python interpreter (as per mne installation instructions). I know that my mne environment definitely has all three backends installed, which is why I can get the figure to work correctly in VSCode Jupyter notebook. But, if I slip a mne.sys_info() call into the script when running in spyder, it reports my python executable is still set to base environment, not mne, and none of the backends are installed.

I’d love to get spyder working again. VSCode isn’t doing it for me.

@pettetmw first thing I would do is remove MNE-Python from your base environment, if that is feasible given your workflows. Having MNE-Python installed in both base and mne envs is a recipe for confusion in my opinion.

Next thing I would do is conda update -n base spyder, and conda update -n mne spyder-kernels. Then I’d open spyder and re-do the step where you set the executable. Hopefully that sorts it.

OK, I did all that, but still stuck, here’s the relevant info (fyi, my mne environment is named “mne23”):

(base) mpettet@aloha:/mnt/ilabs/GITHUB$ pip show spyder-kernels
Name: spyder-kernels Version: 1.9.4
(mne23) mpettet@aloha:/mnt/ilabs/GITHUB$ pip show spyder-kernels
Name: spyder-kernels Version: 2.0.3
(mne23) mpettet@aloha:/mnt/ilabs/GITHUB$ python -c “import sys; print(sys.executable)”
/home/mpettet/anaconda3/envs/mne23/bin/python
(base) mpettet@aloha:/mnt/ilabs/GITHUB$ spyder &

It looks like things should work as you describe, but now I can’t even get a console to launch. Perhaps “base” environment is messed up. How would I recreate that? Would I need to uninstall then reinstall conda itself?

The error message says that you don’t have the right version of spyder-kernels. It wants between 1.9.4 and 1.10, and (here is the crucial part that they don’t clearly express) that requirement is for the mne23 environment, not base. As your console output shows, in the mne23 environment you have spyder-kernels version 2.0.3

To reiterate, the key version compatibility is between spyder in base and spyder-kernels in mne23.

I agree about incompatibility. The problem (I think) is that the conda update -n mne23 spyder-kernels command doesn’t seem to have the desired effect:

(base)  cd /mnt/ilabs/GITHUB/
(base)  conda remove --name mne23 --all
(base)  conda env update --file environment.yml --name mne23
Downloading and Extracting Packages
...
spyder-kernels-2.0.5 | 99 KB     | #######...##### | 100% 
...
(base)  conda activate mne23
(mne23)  pip show spyder-kernels
Name: spyder-kernels
Version: 2.0.5
...
(base)  conda deactivate
(base)  conda update -n mne23 spyder-kernels
The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
...
    spyder-kernels-2.0.5       |   py39h06a4308_0          99 KB
...
The following packages will be SUPERSEDED by a higher-priority channel:
...
  spyder-kernels     conda-forge::spyder-kernels-2.0.5-py3~ --> pkgs/main::spyder-kernels-2.0.5-py39h06a4308_0
...
(base)  conda activate mne23
(mne23)  pip show spyder-kernels
Name: spyder-kernels
Version: 2.0.5

How do I force conda update to downgrade to a specified version, and how do I specify that version? I tried tacking on the version number to the following command, which didn’t work:

(base) mpettet@aloha:/mnt/ilabs/GITHUB$ conda update -n mne23 spyder-kernels-1.9.4

PackageNotInstalledError: Package is not installed in prefix.
  prefix: /home/mpettet/anaconda3/envs/mne23
  package name: spyder-kernels-1.9.4

conda update --help wasn’t too helpful for me, but might be for someone more familiar with it.

You need spyder-kernels from conda-forge; could you try:

 conda update -c conda-forge -n mne23 spyder-kernels

No luck… same result as before (spyder throws error while starting mne23 kernel, which is still 2.0.5), except that conda update -c conda-forge no longer reports that it is downloading spyder-kernels. The message still appears about spyder-kernels version being SUPERSEDED by itself (which seems to be not what we want). Please also note that my previous error posting suggests that spyder kernels were already being obtained from conda-forge. Is there another channel, or a way to specify version?

I would assume that there is something inconsistent about your mne23 environment. Can you try to re-create it from scratch, following our installation instructions?

Do you think I should go so far as to name the environment “mne” instead of “mne23”? It seems like an unlikely explanation, but it is an inconsistency.

Hello, no, the name of the environment should not have any influence. I only meant to suggest that you should try to re-create your environment – I’ve got the feeling that something is “off” with your current environment.

I had trouble with removing and re-installing mne, so I removed and re-installed anaconda too. This resulted in a more normal looking mne install, but the end result is still the same. Here’s my bash history:

conda remove --name mne23 --all
conda deactivate
rm -rf ~/anaconda3/
bash ~/Downloads/Anaconda3-2021.05-Linux-x86_64.sh
# restart terminal
cd /mnt/ilabs/GITHUB/ # the location of my downloaded environment.yml file
(base) conda env update --file environment.yml --name mne23
(base) conda update -c conda-forge -n mne23 spyder-kernels

After this, I launched spyder from (base) and changed console settings to use /home/mpettet/anaconda3/envs/mne23/bin/python, as instructed, then quit and relaunched spyder. I still get the same spyder-kernels version error as my previous posting.

One other odd thing… possibly irrelevant: I noticed, when I went to configure the spyder console settings, that the path to mne23/bin/python was already filled into the edit box in the settings dialog. I’m going to try running anaconda-clean next. Stay tuned.

Thanks for your effort and always reporting back, @pettetmw!

Say, would you be willing to try out a (preliminary) MNE installer? I’ve been working on something there to help reduce to number of problems some users are running into trying to get MNE-Python to work. Let me know if you’re interested and I can share something with you.

No problem. I’ll need a bit of downtime to catch up on some other tasks, but I can try installer when I’m back on this project next week. -Mark

There’s now a pre-release available from Release v2021.1-pre.3 · hoechenberger/mne-installers · GitHub

The MNE-Python environment file does not (and cannot) know what version of spyder you have in your base environment, so conda will always install the highest version of spyder-kernels that is compatible with everything else it is installing at the time. As you’ve already figured out, this means you need to downgrade spyder-kernels in your mne23 environment (or alternatively, you could upgrade the version of spyder in base, but that can be trickier).

conda install spyder-kernels=1.9.4 (use equals sign, not dash)

The following command:
(mne23) mpettet@aloha:~$ conda install spyder-kernels=1.9.4
definitely seems to do what we want, but sadly, fails after about 15 minutes, disgorging an epic stream of output detailing all the unresolvable conflicts.

I’ll try Richard’s installer next.

Hi @richard, Readme is under construction… I assume I just run the shell script from my home dir? Does it assume anything about existing conda environments? Does it take any arguments, like, e.g., the name of the conda environment being created? FYI, I don’t have an environment called “mne”, so if your script defaults to that name, I guess I can just give it a try? I can wait for instructions to be added to Readme if that’s already in your queue. Thanks, -Mark

Hello, it’s an interactive installer that will prompt you for a destination :slight_smile: it will not interfere with existing Python or MNE installs

We’ll publish an updated readme very soon!