-
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
[DOC] Example for spectral connectivity methods comparing epochs and time #73
Draft
Div12345
wants to merge
3
commits into
mne-tools:main
Choose a base branch
from
Div12345:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
examples/spectral_connectivity_epoch_vs_time_simulated_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,112 @@ | ||||||
""" | ||||||
====================================================================== | ||||||
Contrasting the methods to calculate connectivity over epochs and time using simulated data | ||||||
====================================================================== | ||||||
|
||||||
This example shows how to use the spectral connectivity measures using simulated data. | ||||||
|
||||||
Spectral connectivity is generally used to caculate connectivity between electrodes or regions in | ||||||
different frequency bands | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
When there are multiple epochs for a session, like ERP data, the spectral_connectivity_epochs | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
method can be used to infer the connectivity structure between channels across the epochs. It | ||||||
will return the connectivity over time estimated from all the epochs. | ||||||
|
||||||
When the connectivity is to be calculated on a single trial basis across the channels, | ||||||
the spectral_connectivity_time can be used. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
# Author: Divyesh Narayanan <[email protected]> | ||||||
# | ||||||
# License: BSD (3-clause) | ||||||
|
||||||
import numpy as np | ||||||
import mne | ||||||
from mne_connectivity import spectral_connectivity_epochs, spectral_connectivity_time | ||||||
from matplotlib import pyplot as plt | ||||||
print(__doc__) | ||||||
|
||||||
############################################################################### | ||||||
|
||||||
# Generate some data | ||||||
n_epochs = 2 | ||||||
n_channels = 3 | ||||||
n_samples = 1000 | ||||||
sfreq = 250 | ||||||
time = np.arange(n_samples)/sfreq | ||||||
# 4 secs | ||||||
|
||||||
# Things I tried | ||||||
# 1 epoch - all connectivity values give 1 | ||||||
# Should serve as useful warning. All give 1 when the channels values are different but there is | ||||||
# only one epoch | ||||||
|
||||||
# multiple epochs - over epochs returns 1 when the connectivity between 2 channels are 1 in all | ||||||
# the epochs. Otherwise it is some form of estimate across the epochs. Not exactly the average of | ||||||
# the single epoch connectivity values. | ||||||
|
||||||
# Simulating data | ||||||
rng = np.random.RandomState(0) | ||||||
x1 = rng.rand(1, 1, n_samples) | ||||||
x2 = np.sin(2*np.pi*10*time) | ||||||
x3 = np.sin(2*np.pi*15*time) | ||||||
|
||||||
data = np.zeros((n_epochs,n_channels,n_samples)) | ||||||
data[0,0,:] = x1 | ||||||
data[0,1,:] = x2 | ||||||
data[0,2,:] = x2 # x3 | ||||||
|
||||||
# Same value for each channel in all the epochs | ||||||
# for i in range(n_epochs): | ||||||
# data[i] = data[0] | ||||||
|
||||||
# Different values in different epochs | ||||||
data[1,0,:] = x1 | ||||||
data[1,1,:] = x3 | ||||||
data[1,2,:] = x3 | ||||||
|
||||||
# Create epochs object for mne compatibility | ||||||
ch_names = ["T1","T2","T3"] # random names | ||||||
info = mne.create_info(ch_names, sfreq, ch_types="eeg") | ||||||
data_epoch = mne.EpochsArray(data,info) | ||||||
|
||||||
# Visualize the data | ||||||
data_epoch.plot(scalings=2) | ||||||
|
||||||
# Calculate coh over epochs/trials | ||||||
con_Epoch = spectral_connectivity_epochs(data_epoch, method="coh", | ||||||
mode="cwt_morlet", sfreq=sfreq, cwt_freqs=np.array([10])) | ||||||
|
||||||
c_ep = con_Epoch.get_data(output='dense').squeeze(2) # squeezing freq ind since only one freq | ||||||
# average over time | ||||||
print(c_ep.mean(2)) | ||||||
|
||||||
con_epoch = con_Epoch.get_data(output="raveled") | ||||||
plt.plot(con_epoch.squeeze(1).T) | ||||||
plt.show() | ||||||
|
||||||
# Calculating time-resolved spectral connectivity for each epoch | ||||||
con_Time = spectral_connectivity_time(data_epoch, method="coh", | ||||||
mode="cwt_morlet", sfreq=sfreq, freqs=10) | ||||||
|
||||||
con_time = con_Time.get_data("raveled") | ||||||
con_time = con_time.squeeze(2) # removing the freq axis | ||||||
c_t1 = con_time[0] | ||||||
plt.plot(c_t1.T) | ||||||
plt.show() | ||||||
|
||||||
c_t2 = con_time[1] | ||||||
plt.plot(c_t2.T) | ||||||
plt.show() | ||||||
|
||||||
c_ti = con_Time.get_data('dense') | ||||||
c_ti = c_ti.squeeze(3) | ||||||
a = c_ti[0] | ||||||
b = c_ti[1] | ||||||
print(a.mean(2)) | ||||||
print(b.mean(2)) | ||||||
|
||||||
# other testing to compare with epochs method | ||||||
print((a+b).mean(2)/2) | ||||||
print((a.mean(2)+b.mean(2))/2) | ||||||
|
||||||
print() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.