-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example demonstrating difference between spectral connectivity over Epochs or over time #70
Comments
Hey @adam2392 , I could make an attempt at this. |
Sure go for it! I think ideally demonstrating this with a simulation + real dataset would be valuable. |
@adam2392 In #17 I also tested the difference between spectral connectivity over trials/Epochs vs time. It might be useful for inspiration.
|
Hi, it seems that the issues raised in the discussion above have been resolved now. However, just for transparency, we are still interested in accepting a contribution regarding the example docs. Thanks for all the discussion so far! |
If there are no one actively working on an example doc for demonstrating the difference of connectivity over time or over epochs, then I could try and make one. |
Perhaps any on openneuro, if we can easily clip the dataset, or perhaps one from the existing MNE-Python, or MNE-connectivity tutorials would be good candidates perhaps :) |
@adam2392 : Thank you so much for your great work MNE. import numpy as np
import mne
from mne_connectivity import spectral_connectivity_time
from mne_connectivity import spectral_connectivity_epochs
# Generate some data
n_epochs = 5
n_channels = 3
n_times = 1000
sfreq = 250
# CASE 1-----------------------------------------------------------------------
np.random.seed(42)
data = np.random.rand(n_epochs, n_channels, n_times)
for i in range(n_epochs):
data[i] = data[0]
ch_names = ["T1","T2","T3"] # random names
info = mne.create_info(ch_names, sfreq, ch_types="eeg")
data_epoch = mne.EpochsArray(data,info)
data_epoch.plot(scalings=1) # Visualize the data
# Using spectral_connectivity_epochs: phase difference is quantified over epochs at each specific time point of epoch
con_Epoch = spectral_connectivity_epochs(data_epoch, method="pli",
mode="cwt_morlet", sfreq=sfreq, cwt_freqs=np.array([10]))
con_Epoch = con_Epoch.get_data(output="dense")
con_Epoch = np.mean(con_Epoch[:,:,0,:],axis=-1) # avg over times
# Using spectral_connectivity_time: phase difference is quantified over all time points of each epoch
con_time = spectral_connectivity_time(data_epoch, method="pli",
mode="cwt_morlet", sfreq=sfreq, freqs=10)
con_time = con_time.get_data()
con_time = np.mean(con_time[:,:,0],axis=0) # avg over epochs
# CASE 2-----------------------------------------------------------------------
np.random.seed(42)
data = np.random.rand(n_epochs, n_channels, n_times)
for i in range(n_epochs):
for c in range(n_channels):
wave_freq = 10
epoch_len = n_times/sfreq
# Introduce random phase for each channel
phase = np.random.rand(1)*10
# Generate sinus wave
x = np.linspace(-wave_freq*epoch_len*np.pi+phase,
wave_freq*epoch_len*np.pi+phase,n_times)
data[i,c] = np.squeeze(np.sin(x))
ch_names = ["T1","T2","T3"] # random names
info = mne.create_info(ch_names, sfreq, ch_types="eeg")
data_epoch = mne.EpochsArray(data,info)
data_epoch.plot(scalings=2) # Visualize the data
# Using spectral_connectivity_epochs: phase difference is quantified over epochs at each specific time point of epoch
con_Epoch = spectral_connectivity_epochs(data_epoch, method="pli",
mode="cwt_morlet", sfreq=sfreq, cwt_freqs=np.array([10]))
con_Epoch = con_Epoch.get_data(output="dense")
con_Epoch = np.mean(con_Epoch[:,:,0,:],axis=-1) # avg over times
# Using spectral_connectivity_time: phase difference is quantified over all time points of each epoch
con_time = spectral_connectivity_time(data_epoch, method="pli",
mode="cwt_morlet", sfreq=sfreq, freqs=10)
con_time = con_time.get_data()
con_time = np.mean(con_time[:,:,0],axis=0) # avg over epochs |
What are the errors/issue? Do you mind helping to summarizing it here? And what version of mne are you using? E.g. the output of |
@TrinhThanhTung ahh yes I see what you mean with the comments that stated it was 1 when it shouldn't be. Thanks for the heads-up. I copied the code and just changed the "Data setting" variable, but forgot to change the comments in my previous post. The term "case" in the code you posted is probably more intuitive. I'll keep it in mind when writing the example doc. |
@Avoide A lot of thanks to your codes!!! |
Dear @Avoide, @adam2392 , I have 120 seconds of resting-state EEG (sampling rate=500 Hz). I segment it into 40 non-overlap 3 seconds, making the data of the shape (40,30,1500). Here 30 is the number of EEG electrodes. I think in this case "spectral_connectivity_time" is the right selection to calculate the connectivity of electrode pairs. If I want to calculate the alpha band (8~13 Hz) connectivity, how to set the parameters": freqs, fmin, fmax in your "spectral_connectivity_time" function? Thank you so much! |
@TrinhThanhTung Yes for resting-state EEG, I would choose spectral_connectivity_time. You could try:
This code allows you to change the freq band in the dictionary, but also add more frequency bands, e.g. theta or beta.
And the freqs, fmin and fmax will change accordingly. |
@Avoide I really appreciate the time you put into helping me with a very clear answer. It raises this error: *** ValueError: At least one value in n_cycles corresponds to a wavelet longer than the signal. Use less cycles, higher frequencies, or longer epochs. I used: method='pli', mode='cwt_morlet'. Actually, I wrote my own code (quite similar to your code) and met above error when I try with delta band first. |
This most likely means your data is not long enough. Btw I think Btw @TrinhThanhTung we have a great Discourse webpage for answering usage questions exactly like this: https://mne.discourse.group. Perhaps you'll find more fruitful discussion there since it's visible to everyone + searchable (so maybe someone previously had this issue). |
@TrinhThanhTung The error is due to the short duration of the epochs. If you have 3s epochs, but want to investigate 1 Hz freq, then you can only use 3 cycles. Otherwise the 1 Hz wavelet will be longer than the signal. It pertains to the trade-off between time resolution and frequency resolution. The better freq resolution you want, the worse time resolution. |
@TrinhThanhTung I recommend checking out the docs for |
@ruuskas Thank you so much for your recommendation. |
With the inclusion of #67
spectral_connectivity_epochs
andspectral_connectivity_time
are both available to estimate frequency-dependent connectivity.It would be helpful to augment the existing documentation with an example demonstrating the differences between the two.
Anyone in the community willing to help out with this? Would definitely be considered as a co-author in a JOSS/Methods publication for mne-connectivity.
The text was updated successfully, but these errors were encountered: