Failure with the mne_connectivity.SpectralConnectivity function

Hello, I’m trying to calculate the spectral connectivity between my electrodes from an EEG.fif file (already fully pre-processed and concatenated to contain only the participant’s eyes closed resting state data in a block sense without epochs) with the function mne_connectivity. SpectralConnectivity function, but I get the following error message: ‘ValueError: cannot reshape array of size 10682485 into shape (121,121,88285)’. I understand that there’s a shape compatibility problem between my arrays, but I don’t understand why.

  • MNE version: 1.6.1
  • operating system: Windows 10
import pandas as pd
import numpy as np
import seaborn as sns
import os.path as op
import mne
from mne_connectivity import SpectralConnectivity
from mne_connectivity.viz import plot_sensors_connectivity

eeg_file = 'REC-190517-B-RS_eeg.fif'
#ch_names = np.loadtxt('ch_names.txt', dtype=str)
beta_band = [14, 30]

raw = mne.io.read_raw_fif(eeg_file, preload=True)
raw = raw.drop_channels(['E14', 'E17', 'E21', 'E48', 'E119', 'E126', 'E127'])

start_time = raw.times[0] + 1.0
end_time = raw.times[-1] - 1.0
raw_band = raw.crop(tmin=start_time, tmax=end_time)

freqs = np.linspace(beta_band[0], beta_band[1], num=raw_band.n_times)

raw_band=raw_band.get_data()

connectivity = SpectralConnectivity(raw_band, n_nodes=121, freqs=freqs, spec_method='welch', method='coh')
Connectivity_band=connectivity.get_data()

Error message : ValueError: cannot reshape array of size 10682485 into shape (121,121,88285)

Hi,

The mne_connectivity.SpectralConnectivity class is a class for storing connectivity results, however here you are storing timeseries data. Calling the get_data() method raises this error because the size of the timeseries data does not match the size expected of connectivity results.

If you want to compute connectivity, you should use the mne_connectivity.spectral_connectivity_epochs() function.

Note that even for rest data, it is recommended you epoch the data to avoid noisy and unreliable connectivity estimates. This is also stated in the the documentation:

Spectral measures computed with only one Epoch will result in errorful values and spectral measures computed with few Epochs will be unreliable.

These epochs can simply be continuous (i.e. do not need to correspond to any event markers) with a duration of e.g. 2 seconds. You can easily create such epochs using the mne.make_fixed_length_epochs() function.

Hope this helps!

2 Likes

Okay, thank you very much for your help ! I’ll try to do this by making epochs :slight_smile:

1 Like

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