Error while trying to call plot_sensors_connectivity

Platform Windows-11-10.0.22631-SP0
Python 3.12.3
β”œβ˜‘ mne 1.7.1 (latest release)
β”œβ˜‘ mne-connectivity 0.7.0

Hello, i have trouble with plotting connectivity with plot_sensors_connectivity. Error is the same regardless of change od the data and parameters. After calling plot_sensors_connectivity dark window pops up (where the plot should be), but its unresponsive, sometimes even lags my laptop, it is hard to close it (i have to use task manager to do so). I have read posts in community about similar error, and:

  1. my data i pass to plot function is not empty
  2. after calling get_montage() i have info that i have 19 electrodes
  3. i tried reinstalling mne

I also found that there is open bug similar to my error plot_sensors_connectivity fails when strongest conns are spatially close Β· Issue #148 Β· mne-tools/mne-connectivity Β· GitHub, but its not resolved, so that is why im asking here.

My code snippet:

sfreq = 500
tminanal = 0
tmaxanal = 4
baseline = (0, -0.1)
bl1epo = []


for fn2 in [bl1]:
    os.chdir(fn2)
    files2 = [fl for fl in os.listdir() if fl.endswith('.set')]
    for m in tqdm(files2):
        epochs_wid = mne.read_epochs_eeglab(m, verbose=False)
        epochs_wid = epochs_wid.pick_channels(epochs_wid.ch_names)
        epochs_wid.crop(tminanal, tmaxanal)
        bl1epo.append(epochs_wid)

bl1epoall = mne.concatenate_epochs(bl1epo)

event_counts = bl1epoall.event_id #to group all events together because they have different names
all_events_id = {key: 1 for key in event_counts.keys()}
bl1epoall.event_id = all_events_id


cona = spectral_connectivity_epochs(
    bl1epoall, method='pli', mode='multitaper', fmin=8, fmax=12, tmin=0, tmax=1,
    faverage=True, sfreq=sfreq, mt_adaptive=False, n_jobs=1
)

bl1c = cona.get_data('dense')[:, :, 0]
plot_sensors_connectivity(bl1epoall.info, bl1c, cbar_label='Alpha frequency band, time range 0-1s')

My error:

 computing cross-spectral density for epoch 1050
    assembling connectivity matrix
[Connectivity computation done]
Backend tkagg is interactive backend. Turning interactive mode on.
Using notebook 3d backend.
Traceback (most recent call last):
  File "C:\Users\Monika\PycharmProjects\EEG_analysis\.venv\Lib\site-packages\IPython\core\interactiveshell.py", line 3577, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-38f9b76ec237>", line 54, in <module>
    plot_sensors_connectivity(bl1epoall.info, bl1c, cbar_label='Alpha frequency band, time range 0-1s')
  File "C:\Users\Monika\PycharmProjects\EEG_analysis\.venv\Lib\site-packages\mne_connectivity\viz\_3d.py", line 95, in plot_sensors_connectivity
    vmax = np.max(con_val)
           ^^^^^^^^^^^^^^^
  File "C:\Users\Monika\PycharmProjects\EEG_analysis\.venv\Lib\site-packages\numpy\_core\fromnumeric.py", line 2899, in max
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Monika\PycharmProjects\EEG_analysis\.venv\Lib\site-packages\numpy\_core\fromnumeric.py", line 86, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zero-size array to reduction operation maximum which has no identity

I will appreciate any help.
Best regards,
Monika

Hi Monika,

Is there some example data you could send, e.g. via a Dropbox link? That would help to try and figure out what’s happening.

Cheers,
Thomas

Hi Thomas,

Yes, can it be via WeTransfer? I do not have dropbox on my laptop.

There is a link, i uploaded set and fdt data.

Thanks for your reply,
Monika

1 Like

So in the data you sent, there are no coordinates associated with the channels in the montage (all NaNs), which is causing the error (so indeed related to what you linked above, but for a slightly different reason).
bl1epoall.get_montage().dig:
image

I’ve commented and linked to your post there, so hopefully we can add a fix to catch these cases/give a clearer error in the future.

For solving your problem though, you’ll need to use a montage that has the coordinates. I’d have a look at the standard montages in case one matches your system (mne.channels.get_builtin_montages()).

Otherwise you’ll need to make a custom montage, either from arrays with mne.channels.make_dig_montage() or reading from a file (lots of different file types supported in the mne.channels module).

Hope that helps!

1 Like

Hi Thomas!

Thank you for your reply, it worked fine, the plot appears without errors and it looks good!
I’ve used it like this (maybe this will help someone looking for an answer):

for fn2 in [bl1]:
    os.chdir(fn2)
    files2 = [fl for fl in os.listdir() if fl.endswith('.set')]
    for m in tqdm(files2):
        epochs_wid = mne.read_epochs_eeglab(m, verbose=False)
        epochs_wid = epochs_wid.pick_channels(epochs_wid.ch_names)
        epochs_wid.crop(tminanal, tmaxanal)
        mont =mne.channels.make_standard_montage(kind='standard_1020', head_size='auto')
        epochs_wid.set_montage(mont)
        bl1epo.append(epochs_wid)

Once again i’m really thankful, I wish you all the best!
Monika

1 Like

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