Spectral connectivity on single epoch

Hello everyone,

I’m currently using MNE connectivity to perform a functional connectivity analysis in a single epoch. With this said, I’ve read some threads where concerns were raised about using spectral_connectivity/spectral_connectivity_epochs on a single epoch.In such case we are advised to perform a time spectral connectivity analysis on the epoch. As my goal is to obtain connectivity in the following structure-(n_nodes,n_nodes,n_freqs), I tried to use the follwing functions: spectral_connectivity_time with mode=“multitaper”, SpectralConnectivity and EpochSpectralConnectivity. The code snippets are provided below and contain also the errors. At the snippets we only try to analyze theta and alpha freqs and the df_V[“data_ref”][0] represent a session of neurofeedback training in visual modality. Notice that this df_V[“data_ref”][0] is composed by 30 epochs so we are using the index 0 to access to the first epoch.

Thank you all,
Best Regards

  • MNE version: 0.24.1
    -MNE connectivity version: 0.4.dev0
  • operating system: Windows 10

Option 1: spectral_connectivity_time

sfreq = df_V[“Data_REF”][0].info[‘sfreq’] # the sampling frequency
tmin = 0.0 # exclude the baseline period
con_trial = mne_connectivity.spectral_connectivity_time(df_V[“Data_REF”][0][0], method=‘plv’, mode=‘multitaper’,
sfreq=sfreq, foi=((4,8),(8,12)))
conmat_trial = con_trial.get_data(output=‘dense’)[:, :, 0]
#fig=plot_connectivity_circle(conmat_trial, df_V[“Data_REF”][0][0].info[‘ch_names’])
#fig[0].savefig(“fig.png”, facecolor=fig[0].get_facecolor())

This raises an error on spectral connectivity time- UnboundLocalError: local variable ‘f_vec’ referenced before assignment. Notice that i tried using freqs instead of foi but i gaves me also errors.

Option 2: SpectralConnectivity function. In this case the error seems to be the parameters of the input array as it asks for a list/ [ np.ndarray ] with the following structure [epochs], n_estimated_nodes, [freqs], [times]). As my array has only 3 dimension (with the third being times), it gives me an error like the one below. It seems that he is considering the data as beeing on the frequency domain.

sfreq = df_V[“Data_REF”][0].info[‘sfreq’] # the sampling frequency
tmin = 0.0 # exclude the baseline period
con_trial = mne_connectivity.SpectralConnectivity(x[0], method=‘imcoh’,n_nodes=32, names=df_V[“Data_REF”][0][0].info[‘ch_names’],
n_epochs_used=1, freqs=[2,[[4,8],[8,12]]])
conmat_trial = con_trial.get_data(output=‘dense’)[:, :, 0]
#fig=plot_connectivity_circle(conmat_trial, df_V[“Data_REF”][0][0].info[‘ch_names’])
#fig[0].savefig(“fig.png”, facecolor=fig[0].get_facecolor())
ValueError: conflicting sizes for dimension ‘freqs’: length 30001 on the data but length 2 on coordinate ‘freqs’


[[ 4.49762816e-06 9.10974306e-06 1.40401711e-05 … -2.35420393e-06
2.98088536e-06 4.50267834e-06]
[ 2.14575434e-05 2.40399849e-05 2.74856399e-05 … 1.00799157e-04
9.36656125e-05 8.75125799e-05]
[-1.22865745e-05 -1.71034458e-05 -2.26082490e-05 … -3.30322897e-05
-4.07222123e-05 -4.48906183e-05]

[-1.47824244e-05 -1.98617468e-05 -2.26040034e-05 … -9.73291995e-05
-1.05251876e-04 -1.06747114e-04]
[ 3.01504084e-06 3.02932533e-06 1.05186535e-08 … -1.11299831e-04
-1.15024991e-04 -1.15647967e-04]
[-5.38182499e-05 -5.08062563e-05 -4.83661139e-05 … -3.87917777e-05
-3.52674487e-05 -3.18219798e-05]]

This last error seems to apply also to the function- EpochSpectralConnectivity

Hi @pedro3714 apologies for the delay.

Is it possible to post this in the GH issues?

For single-epoch time-resolved spectral connectivity, mne-connectivity does not guarantee correctness currently. We ported the functionality over from Frites, which seems to have a few issues to sort out.

See: [DOC] Example for spectral connectivity methods by Div12345 · Pull Request #73 · mne-tools/mne-connectivity · GitHub

In summary, I hope that we can resolve this soon, but for now single-epoch time-resolved connectivity is not fully supported. If you have a work-around, happy to chat on a GH issue.

Hello @adam2392,

Thanks for the reply, yes i can post it!
For now the way that I found to avoid/solve this, was by partitioning our epochs in sub-epochs, making it possible to apply the spectral_connectivity_epochs in the sub-epochs, what by consequence will return us the mean epoch spectral connectivity.
Anyway if these could be done directly, it would be better. So later today i will post it. Do I copy this thread?

Best regards


Hi @pedro3714 could you show me how you performed the sub-epoch connectivity? I am having a situation where I need to quantify the connectivity changes across each epoch in EEG.

Hello Healthpy, so I ended up doing a three trial analysis with each trial splitted in windows of four seconds. In my case as im using ImCoh this is enough to stabilitize the problem of epoch lenght vs number of epochs. So before doing a per epoch analysis remember that your results wont be as stable.
I recommend you to read this article before you plan to do a per epoch analysis. [2108.13611] EEG-connectivity: A fundamental guide and checklist for optimal study design and evaluation
Anyway if you still want to proceed with this analysis, what u can do is to split the epochs in several windoes and then perform a spectral_connectivity_epochs function.

1 Like

Thank you @pedro3714 for the recommended article, it was informative.