Loading and Visualizing the EEGEyeNet Dataset

Hello,
I’m new to MNE and EEG-analysis in general, and would appreciate your help :slight_smile:
I’m working with the EEGEyeNet dataset, a publicly available dataset with co-registered EEG and Eye Tracking data. The dataset was preprocessed using matlab’s EEG-Eye toolbox, and I’m trying to convert its data structure to MNE objects.

The conversion itself works well, but when I’m attempting to visualize the data, for example by generating a PSD and calling spectrum.plot(), the visualization hangs indefinitely. I tried using different matplotlib backends (TkAgg and QT5Agg), but the issue persists. I also applied the same steps to MNE’s sample dataset and here the visualization didn’t hang.

I’m wondering if there’s something inherently wrong with the EEGEyeNet dataset that prevents it from working with MNE (doubtful) or if I’m doing something wrong in my loading/preprocessing (probably).

The following script should run after you download a file subset of the EEGEyeNet dataset, which is available here:

import os

import numpy as np
import pandas as pd
import mne
from pymatreader import read_mat


import matplotlib
matplotlib.use('QT5Agg')     # or 'TkAgg'


_BASE_PATH = r'path-to-data\dots_data\sunchronised_min'
_FILE_PATH = r'EP10\EP10_DOTS3_EEG.mat'
FULL_PATH = os.path.join(_BASE_PATH, _FILE_PATH)


def channel_type(chan: str):
    if chan.startswith('E') and chan[1:].isdigit():
        return 'eeg'
    if chan == 'Cz':
        return 'eeg'
    if 'GAZE' in chan:
        return 'eyegaze'
    if 'AREA' in chan:
        return 'pupil'
    return 'misc'


mat = read_mat(FULL_PATH)['sEEG']
sfreq = mat['srate']
data = mat['data']
labels = mat['chanlocs']['labels']
types = list(map(lambda chan: channel_type(chan), labels))
data[np.array(types) == 'eeg'] *= 1e-6      # convert uV to V on EEG channels

info = mne.create_info(ch_names=labels, ch_types=types, sfreq=sfreq, verbose=False)
raw = mne.io.RawArray(data, info, verbose=False).crop(tmax=60)
raw.filter(1, 40.0, verbose=False)
raw.set_eeg_reference("average", verbose=False)

spectrum = raw.copy().compute_psd(picks='eeg', n_fft=512, verbose=False)
print((spectrum.data.min(), spectrum.data.max()))

spectrum.plot(average=False, picks=['eeg'])

Here, the last line opens a new window, but immediately it is classified as “not responding” and the code just hangs. Conversely, the following script runs smoothly:

from mne.datasets import sample
from mne.io import read_raw_fif

import matplotlib
matplotlib.use('TkAgg')     # or 'Qt5Agg'


EEG_REF = "EEG 010"

fname = sample.data_path() / "MEG" / "sample" / "sample_audvis_raw.fif"
raw = read_raw_fif(fname, preload=False)
raw.crop(0, 60).pick("eeg")
raw.load_data()
raw.filter(1., 40.)

raw.set_eeg_reference(ref_channels=[EEG_REF])
spectrum = raw.copy().compute_psd(n_fft=512, verbose=False, exclude=[EEG_REF])
print((spectrum.data.min(), spectrum.data.max()))

spectrum.plot(average=False, picks=['eeg', 'eog'])

any thoughts on the difference between the two datasets?

1 Like

I tried your code and it works for me (i.e., it shows the PSD). How do you run it? What’s your Python environment (e.g., show the output of mne.sys_info())? How did you install Python?

I’m using PyCharm with disabled scientific view, running a virtual environment with a Python 3.12 interpreter.
Here’s the mne.sys_info() output:

>>> mne.sys_info()

Platform             Windows-10-10.0.19045-SP0
Python               3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
Executable           # path-to-venv-executable
CPU                  Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz (8 cores)
Memory               15.8 GiB

Core
 + mne               1.9.0 (latest release)
 + numpy             2.2.3 (OpenBLAS 0.3.28 with 8 threads)
 + scipy             1.15.2
 + matplotlib        3.10.1Backend qtagg is interactive backend. Turning interactive mode on.
 (backend=qtagg)

Numerical (optional)
 + sklearn           1.6.1
 + pandas            2.2.3
 + h5py              3.13.0
 - unavailable       numba, nibabel, nilearn, dipy, openmeeg, cupy, h5io

Visualization (optional)
 + qtpy              2.4.3 (PySide6=6.8.2)
 + pyqtgraph         0.13.7
 + mne-qt-browser    0.6.3
 - unavailable       pyvista, pyvistaqt, vtk, ipympl, ipywidgets, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)
 - unavailable       mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo, eeglabio, edfio, mffpy, pybv

Just to clarify, @cbrnr - which of the code snippets runs for you, the 1st or 2nd? The 2nd one runs for me too, the problem is with the EEGEyeNet data which is the 1st script (and you need to download example data from the link I shared). Did you manage to produce a PSD there?

Yeah, the first one runs fine for me.

1 Like

Maybe this is related to how PyCharm runs your code? Have you tried running in script vs. interactive mode? When running in script mode, you either need to insert a plt.show() command or set block=True in the plot method call. But it’s weird that the second example does work for you…

I tried adding the block=True keyword but I get a TypeError: BaseSpectrum.plot() got an unexpected keyword argument 'block'.
I also tried replacing the last row with the following:

fig = spectrum.plot(average=False, picks=['eeg'])
fig.show()

still hangs indefinitely.

Maybe it’s the specific file I downloaded (though I tried several different ones). Can you share which file you used to test my code?

I think it should be plt.show() and not fig.show(), but this won’t probably work anyway.

I used the file you mentioned in your example (dots_data/synchronised_min/EP10/EP10_DOTS3_EEG.mat).

But again, how do you run your code? Interactively or as a script?

I’m using PyCharm’s built-in Python console to run the code. Is this considered “script” or “interactive”? I can try running it through a Jupyter notebook, but why would that make a difference?

This should be interactive, and yes, this can make a difference. Can you try running the entire script via the “Play” button (so not in the Python console)? And no, please do not use Jupyter notebooks.

So I had already tried it in Jupyter and it did work there :slight_smile:
Running my code* as a script also works, so the issue is with PyCharm’s interactive console… I’ll contact their support, but do you have any idea why there would be a difference?

In any case, thanks @cbrnr!


*for future reference, I changed the last two lines of my script to the following:

fig = spectrum.plot(average=False, picks=['eeg'])
plt.show()      # need this to prevent figure from closing immediately

PyCharm (but also VS Code) does all kinds of extra things with their interactive REPL, so I always run my code with plain Python as a script, which is the only way to make it reproducible. But yes, this sounds like a bug on their end, as normally the code should still work in interactive mode. I wonder if you can condense the issue into a minimal reproducible example though…

With your help in narrowing down the issue I was able to find ample examples for other cases where this issue rises in PyCharm. It is indeed a known bug :\

Interesting, that’s good to know! If it is a known bug can you maybe link to the bug report so that others can find that right away?

1 Like

Summarizing Findings for Future Readers
This issue is caused by a faulty integration between PyCharm and the recent versions of matplotlib. The spectrum.plot() line will hang indefinitely if called from the interactive console. Possible mediations:

  • run the code as a script instead of the interactive console
  • use Jupyter notebook
  • downgrade matplotlib to version 3.8.4 or earlier

As the matplotlib people said, this issue seems to be caused by faulty integration between PyCharm’s interactive console and newer matplotlib version.

The issue seems to be on PyCharm’s side, and can be tracked through the matplotlib bug tracker (here) and through PyCharm’s issue tracker (PY-52430 and PY-73139).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.