mne-tools / mne-connectivity

Connectivity algorithms that leverage the MNE-Python API.
https://mne.tools/mne-connectivity/dev/index.html
BSD 3-Clause "New" or "Revised" License
66 stars 34 forks source link

Issues with time-resolved spectral connectivity #90

Closed adam2392 closed 1 year ago

adam2392 commented 2 years ago

"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

#fmin=df_V[“IAF”][0]+2
#fmax=30
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]
#mne_connectivity.viz.plot_sensors_connectivity(df_V[“Data_REF”][0].info,conmat_trial)
#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())
#mean=Mean_Lower_Triangle(conmat_trial)
print(conmat_trial)

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.

#fmin=df_V[“IAF”][0]+2
#fmax=30
sfreq = df_V[“Data_REF”][0].info[‘sfreq’] # the sampling frequency
tmin = 0.0 # exclude the baseline period
x=df_V[“Data_REF”][0].get_data()
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]
#mne_connectivity.viz.plot_sensors_connectivity(df_V[“Data_REF”][0].info,conmat_trial)
#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())
#mean=Mean_Lower_Triangle(conmat_trial)
print(conmat_trial)
ValueError: conflicting sizes for dimension ‘freqs’: length 30001 on the data but length 2 on coordinate ‘freqs’

in:x=df_V[“Data_REF”][0].get_data()
print(x[0])
x.ndim

out:
[[ 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]]
3

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

Originally posted by @pedro3714 in https://github.com/mne-tools/mne-connectivity/issues/73#issuecomment-1075391829

adam2392 commented 2 years ago

@pedro3714 reposting here as a GH issue

Also, if you want to format the code to make it easier to read.

single enclosed brackets ` makes a code line triple enclosed brackets ``` makes a code section

E.g. code line

code section

...
pedro3714 commented 2 years ago

@adam2392 thank you! How can i edit it?

thomasfillon commented 2 years ago

Hi, The issue seems to be related to f_vec not being defined if the input parameters freqs is None in the function spectral_connectivity_time

f_vec is not defined outside the if condition if freqs is not None: at https://github.com/mne-tools/mne-connectivity/blob/58f33c0abc0441315b8bc6b394c432a62c1da692/mne_connectivity/spectral/time.py#L169

And this would raise an error at https://github.com/mne-tools/mne-connectivity/blob/58f33c0abc0441315b8bc6b394c432a62c1da692/mne_connectivity/spectral/time.py#L199

adam2392 commented 2 years ago

Ah I see, so yea the function will require some bit of refactoring and additional testing.

There is still an issue to be sorted out in #73 where there is a discussion going on as to how to best implement time-resolved spectral connectivity. Unfortunately, I am out of depth there, and cannot be of much help unless someone explains things to me :p.

szerfas1 commented 2 years ago

FYI I also encountered this bug, came here, and see #73 is blocking

adam2392 commented 2 years ago

Hi @szerfas1 yes, unfortunately I do not have enough time/expertise to handle this specific related line of work with time-resolved spectral connectivity.

Hoping a community member is interested in doing some OSS work and being a contributor to the MNE ecosystem :)

juancamiload commented 1 year ago

@adam2392 ...Perhaps the implementation in HyPyP may help? To compute connectivity, they obtain the complex signal from a time-resolved spectral computation (e.g., Time-freq with multi-tapers , or Hilbert), and use those to compute what is needed depending on the connectivity metric (e.g., real part, imaginary part, cross-spectral density -complex conjugate multiplication, etc). This is implemented in compute_sync