This notebook is slightly different than the PC notebooks due to differences in the Mac operating system.
First setup the computer by following the instructions in the mac_instructions_cueing.docx file, then return to this file.
The cueing task can ellicit a number of reliable changes. A central cue indicates the location of an upcoming target onset. Here the task can be changed to be perfectly predictive, or have some level of cue validity. Task is to indicate the orientation of a spatial grating on the target, up for vertical, right for horizontal.
ERP - Validly cued targets ellict larger ERP's than invalidly cued targets
Response ERPs - Validly cued targets are more quickly identified and better identified
Oscillations - Alpha power lateralizes after a spatial cue onset preceeding the upcoming onset of a target. Alpha power becomes smaller contraleral to the target side, and larger ipsilateral with the target.
!git clone https://github.com/kylemath/eeg-notebooks --recurse-submodules
%cd eeg-notebooks/notebooks
fatal: destination path 'eeg-notebooks' already exists and is not an empty directory. /content/eeg-notebooks/notebooks
#from muselsl import stream, list_muses, view, record
!pip install mne
from multiprocessing import Process
from mne import Epochs, find_events, concatenate_raws
from mne.time_frequency import tfr_morlet
import numpy as np
from time import time, strftime, gmtime
import os
#from stimulus_presentation import cueing
from utils import utils
from collections import OrderedDict
import warnings
warnings.filterwarnings('ignore')
from matplotlib import pyplot as plt
import matplotlib.patches as patches
Requirement already satisfied: mne in /usr/local/lib/python3.6/dist-packages (0.17.0)
Once a suitable data set has been collected, it is now time to analyze the data and see if we can identify the cueing effects
MNE is a very powerful Python library for analyzing EEG data. It provides helpful functions for performing key tasks such as filtering EEG data, rejecting artifacts, and grouping EEG data into chunks (epochs).
The first step after loading dependencies is use MNE to read the data we've collected into an MNE Raw object
subs = [101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112,
202, 203, 204, 205, 207, 208, 209, 210, 211,
301, 302, 303, 304, 305, 306, 307, 308, 309]
diff_out = []
Ipsi_out = []
Contra_out = []
Ipsi_spectra_out = []
Contra_spectra_out = []
diff_spectra_out = []
ERSP_diff_out = []
ERSP_Ipsi_out = []
ERSP_Contra_out = []
frequencies = np.linspace(6, 30, 100, endpoint=True)
wave_cycles = 6
# time frequency window for analysis
f_low = 7 # Hz
f_high = 10
f_diff = f_high-f_low
t_low = 0 # s
t_high = 1
t_diff = t_high-t_low
bad_subs= [6, 7, 13, 26]
really_bad_subs = [11, 12, 19]
sub_count = 0
for sub in subs:
print(sub)
sub_count += 1
if (sub_count in really_bad_subs):
rej_thresh_uV = 90
elif (sub_count in bad_subs):
rej_thresh_uV = 90
else:
rej_thresh_uV = 90
rej_thresh = rej_thresh_uV*1e-6
# Load both sessions
raw = utils.load_data('visual/cueing', sfreq=256.,
subject_nb=sub, session_nb=1)
raw.append( utils.load_data('visual/cueing', sfreq=256.,
subject_nb=sub, session_nb=2) )
# Filter Raw Data
raw.filter(1,30, method='iir')
#Select Events
events = find_events(raw)
event_id = {'LeftCue': 1, 'RightCue': 2}
epochs = Epochs(raw, events=events, event_id=event_id,
tmin=-1, tmax=2, baseline=(-1, 0),
reject={'eeg':rej_thresh}, preload=True,
verbose=False, picks=[0, 3])
print('Trials Remaining: ' + str(len(epochs.events)) + '.')
# Compute morlet wavelet
# Left Cue
tfr, itc = tfr_morlet(epochs['LeftCue'], freqs=frequencies,
n_cycles=wave_cycles, return_itc=True)
tfr = tfr.apply_baseline((-1,-.5),mode='mean')
#tfr.plot(picks=[0], mode='logratio',
# title='TP9 - Ipsi');
#tfr.plot(picks=[3], mode='logratio',
# title='TP10 - Contra');
power_Ipsi_TP9 = tfr.data[0,:,:]
power_Contra_TP10 = tfr.data[1,:,:]
# Right Cue
tfr, itc = tfr_morlet(epochs['RightCue'], freqs=frequencies,
n_cycles=wave_cycles, return_itc=True)
tfr = tfr.apply_baseline((-1,-.5),mode='mean')
#tfr.plot(picks=[0], mode='logratio',
# title='TP9 - Contra');
#tfr.plot(picks=[3], mode='logratio',
# title='TP10 - Ipsi');
power_Contra_TP9 = tfr.data[0,:,:]
power_Ipsi_TP10 = tfr.data[1,:,:]
# Plot Differences
%matplotlib inline
times = epochs.times
power_Avg_Ipsi = (power_Ipsi_TP9+power_Ipsi_TP10)/2;
power_Avg_Contra = (power_Contra_TP9+power_Contra_TP10)/2;
power_Avg_Diff = power_Avg_Ipsi-power_Avg_Contra;
#find max to make color range
plot_max = np.max([np.max(np.abs(power_Avg_Ipsi)), np.max(np.abs(power_Avg_Contra))])
plot_diff_max = np.max(np.abs(power_Avg_Diff))
#Ipsi
fig, ax = plt.subplots(1)
im = plt.imshow(power_Avg_Ipsi,
extent=[times[0], times[-1], frequencies[0], frequencies[-1]],
aspect='auto', origin='lower', cmap='coolwarm', vmin=-plot_max, vmax=plot_max)
plt.xlabel('Time (sec)')
plt.ylabel('Frequency (Hz)')
plt.title('Power Average Ipsilateral to Cue')
cb = fig.colorbar(im)
cb.set_label('Power')
# Create a Rectangle patch
rect = patches.Rectangle((t_low,f_low),t_diff,f_diff,linewidth=1,edgecolor='k',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
#TP10
fig, ax = plt.subplots(1)
im = plt.imshow(power_Avg_Contra,
extent=[times[0], times[-1], frequencies[0], frequencies[-1]],
aspect='auto', origin='lower', cmap='coolwarm', vmin=-plot_max, vmax=plot_max)
plt.xlabel('Time (sec)')
plt.ylabel('Frequency (Hz)')
plt.title(str(sub) + ' - Power Average Contra to Cue')
cb = fig.colorbar(im)
cb.set_label('Power')
# Create a Rectangle patch
rect = patches.Rectangle((t_low,f_low),t_diff,f_diff,linewidth=1,edgecolor='k',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
#difference between conditions
fig, ax = plt.subplots(1)
im = plt.imshow(power_Avg_Diff,
extent=[times[0], times[-1], frequencies[0], frequencies[-1]],
aspect='auto', origin='lower', cmap='coolwarm', vmin=-plot_diff_max, vmax=plot_diff_max)
plt.xlabel('Time (sec)')
plt.ylabel('Frequency (Hz)')
plt.title('Power Difference Ipsi-Contra')
cb = fig.colorbar(im)
cb.set_label('Ipsi-Contra Power')
# Create a Rectangle patch
rect = patches.Rectangle((t_low,f_low),t_diff,f_diff,linewidth=1,edgecolor='k',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
#output data into array
Ipsi_out.append(np.mean(power_Avg_Ipsi[np.argmax(frequencies>f_low):
np.argmax(frequencies>f_high)-1,
np.argmax(times>t_low):np.argmax(times>t_high)-1 ]
)
)
Ipsi_spectra_out.append(np.mean(power_Avg_Ipsi[:,np.argmax(times>t_low):
np.argmax(times>t_high)-1 ],1
)
)
Contra_out.append(np.mean(power_Avg_Contra[np.argmax(frequencies>f_low):
np.argmax(frequencies>f_high)-1,
np.argmax(times>t_low):np.argmax(times>t_high)-1 ]
)
)
Contra_spectra_out.append(np.mean(power_Avg_Contra[:,np.argmax(times>t_low):
np.argmax(times>t_high)-1 ],1))
diff_out.append(np.mean(power_Avg_Diff[np.argmax(frequencies>f_low):
np.argmax(frequencies>f_high)-1,
np.argmax(times>t_low):np.argmax(times>t_high)-1 ]
)
)
diff_spectra_out.append(np.mean(power_Avg_Diff[:,np.argmax(times>t_low):
np.argmax(times>t_high)-1 ],1
)
)
ERSP_diff_out.append(power_Avg_Diff)
ERSP_Ipsi_out.append(power_Avg_Ipsi)
ERSP_Contra_out.append(power_Avg_Contra)
101 Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 122 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 10. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 102 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 92 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 40. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 103 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 160 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 58. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 104 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 272 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 39. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 105 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 111 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 21. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 106 Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 201 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 2. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 108 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 130 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 0. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 109 Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 160 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 5. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 110 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 107 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 28. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 111 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 223 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 32. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 112 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 86 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 0. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 202 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 156 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 61. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 203 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 120 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 4. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 204 Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 183 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 49. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 205 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 194 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 87. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 207 Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 181 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 70. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 208 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 181 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 70. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 209 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 118 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 5. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 210 Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 170 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 67. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 211 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 96 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 9. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 301 Creating RawArray with float64 data, n_channels=5, n_times=30564 Range : 0 ... 30563 = 0.000 ... 119.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=30564 Range : 0 ... 30563 = 0.000 ... 119.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 30564 Setting up band-pass filter from 1 - 30 Hz Using filter length: 30564 54 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 26. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 302 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 213 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 11. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 303 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 198 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 8. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 304 Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61284 Range : 0 ... 61283 = 0.000 ... 239.387 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61284 195 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 24. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 305 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 166 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 9. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 306 Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 147 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 1. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 307 Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 171 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 49. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 308 Creating RawArray with float64 data, n_channels=5, n_times=61296 Range : 0 ... 61295 = 0.000 ... 239.434 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61296 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 180 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 71. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean) 309 Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Creating RawArray with float64 data, n_channels=5, n_times=61308 Range : 0 ... 61307 = 0.000 ... 239.480 secs Ready. Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 Setting up band-pass filter from 1 - 30 Hz Using filter length: 61308 196 events found Event IDs: [ 1 2 11 12 21 22] Trials Remaining: 74. Applying baseline correction (mode: mean) Applying baseline correction (mode: mean)
print(np.shape(ERSP_diff_out))
print(np.shape(Contra_spectra_out))
print(diff_out)
GrandAvg_diff = np.nanmean(ERSP_diff_out,0)
GrandAvg_Ipsi = np.nanmean(ERSP_Ipsi_out,0)
GrandAvg_Contra = np.nanmean(ERSP_Contra_out,0)
GrandAvg_spec_Ipsi = np.nanmean(Ipsi_spectra_out,0)
GrandAvg_spec_Contra = np.nanmean(Contra_spectra_out,0)
GrandAvg_spec_diff = np.nanmean(diff_spectra_out,0)
num_good = len(diff_out) - sum(np.isnan(diff_out))
GrandAvg_spec_Ipsi_ste = np.nanstd(Ipsi_spectra_out,0)/np.sqrt(num_good)
GrandAvg_spec_Contra_ste = np.nanstd(Contra_spectra_out,0)/np.sqrt(num_good)
GrandAvg_spec_diff_ste = np.nanstd(diff_spectra_out,0)/np.sqrt(num_good)
#Spectra error bars
fig, ax = plt.subplots(1)
plt.errorbar(frequencies,GrandAvg_spec_Ipsi,yerr=GrandAvg_spec_Ipsi_ste)
plt.errorbar(frequencies,GrandAvg_spec_Contra,yerr=GrandAvg_spec_Contra_ste)
plt.legend(('Ipsi','Contra'))
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power (uV^2)')
plt.hlines(0,3,33)
#Spectra Diff error bars
fig, ax = plt.subplots(1)
plt.errorbar(frequencies,GrandAvg_spec_diff,yerr=GrandAvg_spec_diff_ste)
plt.legend('Ipsi-Contra')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power (uV^2)')
plt.hlines(0,3,33)
#Grand Average Ipsi
plot_max = np.max([np.max(np.abs(GrandAvg_Ipsi)), np.max(np.abs(GrandAvg_Contra))])
fig, ax = plt.subplots(1)
im = plt.imshow(GrandAvg_Ipsi,
extent=[times[0], times[-1], frequencies[0], frequencies[-1]],
aspect='auto', origin='lower', cmap='coolwarm', vmin=-plot_max, vmax=plot_max)
plt.xlabel('Time (sec)')
plt.ylabel('Frequency (Hz)')
plt.title('Power Ipsi')
cb = fig.colorbar(im)
cb.set_label('Power')
# Create a Rectangle patch
rect = patches.Rectangle((t_low,f_low),t_diff,f_diff,linewidth=1,edgecolor='k',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
#Grand Average Contra
fig, ax = plt.subplots(1)
im = plt.imshow(GrandAvg_Contra,
extent=[times[0], times[-1], frequencies[0], frequencies[-1]],
aspect='auto', origin='lower', cmap='coolwarm', vmin=-plot_max, vmax=plot_max)
plt.xlabel('Time (sec)')
plt.ylabel('Frequency (Hz)')
plt.title('Power Contra')
cb = fig.colorbar(im)
cb.set_label('Power')
# Create a Rectangle patch
rect = patches.Rectangle((t_low,f_low),t_diff,f_diff,linewidth=1,edgecolor='k',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
#Grand Average Ipsi-Contra Difference
plot_max_diff = np.max(np.abs(GrandAvg_diff))
fig, ax = plt.subplots(1)
im = plt.imshow(GrandAvg_diff,
extent=[times[0], times[-1], frequencies[0], frequencies[-1]],
aspect='auto', origin='lower', cmap='coolwarm', vmin=-plot_max_diff, vmax=plot_max_diff)
plt.xlabel('Time (sec)')
plt.ylabel('Frequency (Hz)')
plt.title('Power Difference Ipsi-Contra')
cb = fig.colorbar(im)
cb.set_label('Ipsi-Contra Power')
# Create a Rectangle patch
rect = patches.Rectangle((t_low,f_low),t_diff,f_diff,linewidth=1,edgecolor='k',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
(29, 100, 769) (29, 100) [-1.8633016529355503e-10, -5.19449269804164e-11, 3.965717366845645e-11, -9.439937924298804e-11, 1.5395426956926097e-10, nan, nan, 1.437520530470469e-10, 6.156413801292706e-12, -3.454228962737695e-11, nan, 1.4040022690241467e-10, 4.5250575579733986e-11, 8.133414421717266e-11, 1.7562617842729205e-12, 3.4989854712143306e-11, -7.35844888841272e-11, -2.0667398674718962e-11, 5.83383223147634e-11, -1.1353507280272532e-10, 2.057854566935435e-10, 8.136129078256774e-11, 2.2311245651295555e-09, 4.417204016506356e-13, 1.5824220701839011e-10, nan, 2.972694444208326e-11, -2.705345319003894e-11, 1.0032262566341921e-10]
<matplotlib.patches.Rectangle at 0x7fd9b86f23c8>
import scipy
num_good = len(diff_out) - sum(np.isnan(diff_out))
[tstat, pval] = scipy.stats.ttest_ind(diff_out,np.zeros(len(diff_out)),nan_policy='omit')
print('Ipsi Mean: '+ str(np.nanmean(Ipsi_out)))
print('Contra Mean: '+ str(np.nanmean(Contra_out)))
print('Mean Diff: '+ str(np.nanmean(diff_out)))
print('t(' + str(num_good-1) + ') = ' + str(round(tstat,3)))
print('p = ' + str(round(pval,3)))
Ipsi Mean: 9.024561362956465e-11 Contra Mean: -2.6175863611708235e-11 Mean Diff: 1.164214772412728e-10 t(24) = 1.394 p = 0.169
import pandas as pd
print(diff_out)
raw_data = {'Ipsi Power': Ipsi_out,
'Contra Power': Contra_out}
df = pd.DataFrame(raw_data, columns = ['Ipsi Power', 'Contra Power'])
df.to_csv('375CueingEEG.csv')
print('Saved subject averages for each condition to 375CueingEEG.csv file in present directory')
[-1.8633016529355503e-10, -5.19449269804164e-11, 3.965717366845645e-11, -9.439937924298804e-11, 1.5395426956926097e-10, nan, nan, 1.437520530470469e-10, 6.156413801292706e-12, -3.454228962737695e-11, nan, 1.4040022690241467e-10, 4.5250575579733986e-11, 8.133414421717266e-11, 1.7562617842729205e-12, 3.4989854712143306e-11, -7.35844888841272e-11, -2.0667398674718962e-11, 5.83383223147634e-11, -1.1353507280272532e-10, 2.057854566935435e-10, 8.136129078256774e-11, 2.2311245651295555e-09, 4.417204016506356e-13, 1.5824220701839011e-10, nan, 2.972694444208326e-11, -2.705345319003894e-11, 1.0032262566341921e-10] Saved subject averages for each condition to 375CueingEEG.csv file in present directory
df = pd.DataFrame(Ipsi_spectra_out,columns=frequencies)
df.to_csv('375CueingIpsiSpec.csv')
df = pd.DataFrame(Contra_spectra_out,columns=frequencies)
df.to_csv('375CueingContraSpec.csv')
print('Saved Spectra to 375Cueing*Spec.csv file in present directory')
Saved Spectra to 375Cueing*Spec.csv file in present directory