Inhomogeneous shape error when using mne_connectivity.spectral_connectivity_time

Hello!

I want to compute spectral connectivity over the resting state data from the dataset EEG Motor Movement/Imagery Dataset. However, whise using the mne_connectivity.spectral_connectivity_time() function I get the error:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (64, 2) + inhomogeneous part.

I tried downgrading the numpy as recommended here. Unfortunately, it didn’t help, the same error still occurs.

Here is my minimal code:

!pip install numpy==1.21.1
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import mne
import mne_connectivity

from mne.datasets import eegbci
EO = eegbci.load_data(3, 1, "C:/path");
raw = mne.io.read_raw_edf(EO, preload = True)

mne.datasets.eegbci.standardize(raw)
raw.set_montage('standard_1020');
raw_avg_ref = raw.copy().set_eeg_reference(ref_channels='average')

epochs = mne.make_fixed_length_epochs(raw_avg_ref, duration = 60.0, preload = True)

con_epochs = mne_connectivity.spectral_connectivity_time(
data = raw_fb,
freqs = alphafb,
method = 'pli')

MNE version: 1.6.0
operating system: Windows 10

Hi,

The error comes from the fact that you are passing what I assume is raw data (raw_fb) to spectral_connectivity_time(), which expects data to be epoched.

If you instead pass epochs, this runs successfully for me with MNE 1.6 and MNE-Connectivity 0.6.

@drammock Do you think the error message could be more descriptive? Right now it happens because if the data is not a Epochs object, it is assumed to be an array-like, so we try to convert the Raw object to a NumPy array, which fails and gives this obscure message.

definitely. I can take a look.

1 Like

better error message added in

1 Like

Thank you!
I fixed that and the spectral_connectivity_time() function worked.

Unfortunately, I got problems with another function - mne_connectivity.viz.plot_sensors_connectivity().

While using it, I get an error:

ValueError: The number of channels picked (64) does not correspond to the size of the connectivity data (1)

Here is my code:

con_epochs = mne_connectivity.spectral_connectivity_time(
data = epochs,
freqs = freqs,
method = 'pli',
fmin = 8.0,
fmax = 13.0
)
plot_sensors_connectivity(epochs.info, con_epochs.get_data(output="dense")[:, :, 0])

I suppose, the problem might be with the wrong indexing of con_epochs.get_data, but I’m unaware how to fix that. I took the indexing from this tutorial.

I would be grateful if you could help with that.

So in the tutorial you link to, the connectivity results come from the spectral_connectivity_epochs() function.
If you call get_data(output="dense") on the returned connectivity object, the results will have shape (channels x channels x frequencies).
Indexing the results with [:, :, 0] will therefore give you results of shape (channels x channels), which is what plot_sensors_connectivity() expects.

However, here you are computing connectivity with the spectral_connectivity_time() function.
If you call get_data(output="dense") on this returned connectivity object, the results will have shape (epochs x channels x channels x frequencies).
Indexing the results with [:, :, 0] will therefore give you results of shape (epochs x channels x frequencies), which plot_sensors_connectivity() cannot properly interpret.

You would need to adapt the indexing to account for the additional epochs dimension. E.g. [0, :, :, 0] would allow you to plot the connections between all channels for the first epoch, and the first frequency (8 Hz in your case):

2 Likes