Epochs and bands

from mne.io import read_raw_fif
from mne import Epochs
from mne import make_fixed_length_epochs
#load average delta activation for every person and define minimum/ maximum power frame
for file in files:
    data = read_raw_fif(file, preload=True)
    epochs = make_fixed_length_epochs(data, duration=2.0, preload=True)
    epochs.plot_psd_topomap(bands=[(0,4, "Delta")], ch_type="eeg", normalize=True, dB=True, vlim=(0.1, 0.7));
#load average theta activation for every person and define minimum/ maximum power frame
for file in files:
    data = read_raw_fif(file, preload=True)
    epochs = make_fixed_length_epochs(data, duration=2.0, preload=True)
    epochs.plot_psd_topomap(bands=[(4,8, "Theta")], ch_type="eeg", normalize=True, dB=True, vlim=(0.1, 0.2));
#load average alpha activation for every person and define minimum/ maximum power frame
for file in files:
    data = read_raw_fif(file, preload=True)
    epochs = make_fixed_length_epochs(data, duration=2.0, preload=True)
    epochs.plot_psd_topomap(bands=[(8,12, "Alpha")], ch_type="eeg", normalize=True, dB=True, vlim=(0.1, 0.5));
#load average beta activation for every person and define minimum/ maximum power frame
for file in files:
    data = read_raw_fif(file, preload=True)
    epochs = make_fixed_length_epochs(data, duration=2.0, preload=True)
    epochs.plot_psd_topomap(bands=[(12,30, "Beta")], ch_type="eeg", normalize=True, dB=True, vlim=(0.1, 0.4));
#load average gamma activation for every person and define minimum/ maximum power frame
for file in files:
    data = read_raw_fif(file, preload=True)
    epochs = make_fixed_length_epochs(data, duration=2.0, preload=True)
    epochs.plot_psd_topomap(bands=[(30,45, "Gamma")], ch_type="eeg", normalize=True, dB=True, vlim=(0.0, 0.2));
#load all filtered and preprocessed eeg/ .fif files for every person
files = ["/mnt/c/Users/anna-/Desktop/reconst_raw/h01-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h02-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h03-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h04-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h05-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h06-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h07-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h08-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h09-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h10-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h11-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h12-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h13-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h14-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s01-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s02-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s03-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s04-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s05-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s06-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s07-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s08-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s09-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s10-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s11-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s12-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s13-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s14-raw.fif"]
#define all files which count to "healthy"
healthy = ["/mnt/c/Users/anna-/Desktop/reconst_raw/h01-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h02-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h03-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h04-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h05-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h06-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h07-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h08-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h09-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h10-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h11-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h12-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h13-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/h14-raw.fif"]
#define all files which count to "schizophrenic"
schizo = ["/mnt/c/Users/anna-/Desktop/reconst_raw/s01-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s02-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s03-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s04-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s05-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s06-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s07-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s08-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s09-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s10-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s11-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s12-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s13-raw.fif",
         "/mnt/c/Users/anna-/Desktop/reconst_raw/s14-raw.fif"]
import numpy as np
from mne.io import read_raw_fif
from mne import Epochs
from mne import make_fixed_length_epochs
from mne.time_frequency import psd_welch, psd_multitaper
from mne.viz import plot_topomap
sh_ave = np.zeros((14,19))
for n_file, file in enumerate(schizo):
    data = read_raw_fif(file, preload=True)
    epochs = make_fixed_length_epochs(data, duration=2.0, preload=True)
    #epochs.plot_psd_topomap(ch_type="eeg", normalize=True);
    info = epochs.info
    #psd, freqs = psd_welch(epochs, fmin=1.0, fmax=45.0)
    psd, freqs = psd_multitaper(epochs, fmin=1.0, fmax=45.0, n_jobs=1)
    psd /= psd.sum(axis=-1, keepdims=True)
    #for example, here gamma activation
    s_ave = psd[:,:,(freqs>30.0) & (freqs<45.0)].mean(axis=2).mean(axis=0)
    sh_ave[n_file,:] = s_ave
h_ave = np.zeros((14,19))
for n_file, file in enumerate(healthy):
    data = read_raw_fif(file, preload=True)
    epochs = make_fixed_length_epochs(data, duration=2.0, preload=True)
    #epochs.plot_psd_topomap(ch_type="eeg", normalize=True);
    info = epochs.info
    #psd, freqs = psd_welch(epochs, fmin=1.0, fmax=45.0)
    psd, freqs = psd_multitaper(epochs, fmin=1.0, fmax=45.0, n_jobs=1)
    psd /= psd.sum(axis=-1, keepdims=True)
    #for example, here gamma activation
    s_ave = psd[:,:,(freqs>30.0) & (freqs<45.0)].mean(axis=2).mean(axis=0)
    h_ave[n_file,:] = s_ave
#show all channel names
epochs.ch_names
['Fp2',
 'F8',
 'T4',
 'T6',
 'O2',
 'Fp1',
 'F7',
 'T3',
 'T5',
 'O1',
 'F4',
 'C4',
 'P4',
 'F3',
 'C3',
 'P3',
 'Fz',
 'Cz',
 'Pz']
#show delta/ theta/ alpha/ beta/ gamma activation head for healthies
plot_topomap(h_ave.mean(axis=0), info, vmin=0.001, vmax=0.005)
#show delta/ theta/ alpha/ beta/ gamma activation head for schizophrenics
plot_topomap(sh_ave.mean(axis=0), info, vmin=0.001, vmax=0.005)
import seaborn
import pandas as pd
import matplotlib.pyplot as plt
#detect which electrodes might be of critical interest in comparison between healthies and schizophrenics for delta activation
delta_healthy = pd.DataFrame({"F8":h_ave[:,1],"F7":h_ave[:,6],"Cz":h_ave[:,17]})
delta_healthy["group"] = np.repeat("healthy",14)
delta_schizo = pd.DataFrame({"F8":sh_ave[:,1],"F7":sh_ave[:,6],"Cz":sh_ave[:,17]})
delta_schizo["group"] = np.repeat("schizo",14)
#detect which electrodes might be of critical interest in comparison between healthies and schizophrenics for theta activation
theta_healthy = pd.DataFrame({"Fz":h_ave[:,16],"Cz":h_ave[:,17]})
theta_healthy["group"] = np.repeat("healthy",14)
theta_schizo = pd.DataFrame({"Fz":sh_ave[:,16],"Cz":sh_ave[:,17]})
theta_schizo["group"] = np.repeat("schizo",14)
#detect which electrodes might be of critical interest in comparison between healthies and schizophrenics for alpha activation, btw. for alpha almost all (except temporal) electrodes could be candidates
alpha_healthy = pd.DataFrame({"C4":h_ave[:,11],"P4":h_ave[:,12],"F3":h_ave[:,13],"C3":h_ave[:,14],"P3":h_ave[:,15],"Fz":h_ave[:,16],"Cz":h_ave[:,17],"Pz":h_ave[:,18]})
alpha_healthy["group"] = np.repeat("healthy",14)
alpha_schizo = pd.DataFrame({"C4":sh_ave[:,11],"P4":sh_ave[:,12],"F3":sh_ave[:,13],"C3":sh_ave[:,14],"P3":sh_ave[:,15],"Fz":sh_ave[:,16],"Cz":sh_ave[:,17],"Pz":sh_ave[:,18]})
alpha_schizo["group"] = np.repeat("schizo",14)
#detect which electrodes might be of critical interest in comparison between healthies and schizophrenics for beta activation
beta_healthy = pd.DataFrame({"T5":h_ave[:,8]})
beta_healthy["group"] = np.repeat("healthy",14)
beta_schizo = pd.DataFrame({"T5":sh_ave[:,8]})
beta_schizo["group"] = np.repeat("schizo",14)
#detect which electrodes might be of critical interest in comparison between healthies and schizophrenics for gamma activation
gamma_healthy = pd.DataFrame({"T4":h_ave[:,2],"T3":h_ave[:,7]})
gamma_healthy["group"] = np.repeat("healthy",14)
gamma_schizo = pd.DataFrame({"T4":sh_ave[:,2],"T3":sh_ave[:,7]})
gamma_schizo["group"] = np.repeat("schizo",14)
#define critical electrodes for delta acitvation
delta_schizo = delta_schizo.append(delta_healthy)
delta_schizo = delta_schizo.reset_index()
delta_schizo = pd.melt(delta_schizo,id_vars=["index","group"],value_vars=["F8","F7","Cz"])
#define critical electrodes for theta acitvation
theta_schizo = theta_schizo.append(theta_healthy)
theta_schizo = theta_schizo.reset_index()
theta_schizo = pd.melt(theta_schizo,id_vars=["index","group"],value_vars=["Fz","Cz"])
#define critical electrodes for alpha acitvation
alpha_schizo = alpha_schizo.append(alpha_healthy)
alpha_schizo = alpha_schizo.reset_index()
alpha_schizo = pd.melt(alpha_schizo,id_vars=["index","group"],value_vars=["C4","P4","F3","C3","P3","Fz","Cz","Pz"])
#define critical electrodes for beta acitvation
beta_schizo = beta_schizo.append(beta_healthy)
beta_schizo = beta_schizo.reset_index()
beta_schizo = pd.melt(beta_schizo,id_vars=["index","group"],value_vars=["T5"])
#define critical electrodes for gamma acitvation
gamma_schizo = gamma_schizo.append(gamma_healthy)
gamma_schizo = gamma_schizo.reset_index()
gamma_schizo = pd.melt(gamma_schizo,id_vars=["index","group"],value_vars=["T4","T3"])
#load and show swarm and box plots for average delta activation for healthies and schizophrenics for F8, F7 and Cz
ax = seaborn.boxplot(x="variable",y="value",data=delta_schizo,hue="group",dodge=True,width=0.25,showfliers=False)
for patch in ax.artists:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))
ax = seaborn.swarmplot(x="variable",y="value",data=delta_schizo,hue="group",dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
#load and show swarm and box plots for average theta activation for healthies and schizophrenics for Fz and Cz
ax = seaborn.boxplot(x="variable",y="value",data=theta_schizo,hue="group",dodge=True,width=0.25,showfliers=False)
for patch in ax.artists:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))
ax = seaborn.swarmplot(x="variable",y="value",data=theta_schizo,hue="group",dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
#load and show swarm and box plots for average alpha activation for healthies and schizophrenics for C4, P4, F3, C3, P3, Fz, Cz, Pz
ax = seaborn.boxplot(x="variable",y="value",data=alpha_schizo,hue="group",dodge=True,width=0.25,showfliers=False)
for patch in ax.artists:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))
ax = seaborn.swarmplot(x="variable",y="value",data=alpha_schizo,hue="group",dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
#load and show swarm and box plots for average beta activation for healthies and schizophrenics for T5
ax = seaborn.boxplot(x="variable",y="value",data=beta_schizo,hue="group",dodge=True,width=0.25,showfliers=False)
for patch in ax.artists:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))
ax = seaborn.swarmplot(x="variable",y="value",data=beta_schizo,hue="group",dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
#load and show swarm and box plots for average gamma activation for healthies and schizophrenics for T4 and T3
ax = seaborn.boxplot(x="variable",y="value",data=gamma_schizo,hue="group",dodge=True,width=0.25,showfliers=False)
for patch in ax.artists:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))
ax = seaborn.swarmplot(x="variable",y="value",data=gamma_schizo,hue="group",dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
from scipy import stats
#load average delta activation of F8 for healthies and schizophrenics to compare them statistically
h_F8 = delta_schizo.loc[(delta_schizo['variable'] == "F8") & (delta_schizo['group'] == "healthy")]
s_F8 = delta_schizo.loc[(delta_schizo['variable'] == "F8") & (delta_schizo['group'] == "schizo")]
#do independent t-test for the two variables to find out if there is a significant difference
stats.ttest_ind(h_F8["value"], s_F8["value"], equal_var=False)
Ttest_indResult(statistic=2.014769507827961, pvalue=0.05448583575057455)
#load average theta activation of Cz for healthies and schizophrenics to compare them statistically
h_Cz = theta_schizo.loc[(theta_schizo['variable'] == "Cz") & (theta_schizo['group'] == "healthy")]
s_Cz = theta_schizo.loc[(theta_schizo['variable'] == "Cz") & (theta_schizo['group'] == "schizo")]
#do independent t-test for the two variables to find out if there is a significant difference
stats.ttest_ind(h_Cz["value"], s_Cz["value"], equal_var=False)
Ttest_indResult(statistic=0.9678150389704233, pvalue=0.342062534997258)
#load average alpha activation of Pz for healthies and schizophrenics to compare them statistically
h_Pz = alpha_schizo.loc[(alpha_schizo['variable'] == "Pz") & (alpha_schizo['group'] == "healthy")]
s_Pz = alpha_schizo.loc[(alpha_schizo['variable'] == "Pz") & (alpha_schizo['group'] == "schizo")]
#do independent t-test for the two variables to find out if there is a significant difference
stats.ttest_ind(h_Pz["value"], s_Pz["value"], equal_var=False)
Ttest_indResult(statistic=-0.9903178215794465, pvalue=0.33114734174667837)
#load average beta activation of T5 for healthies and schizophrenics to compare them statistically
h_T5 = beta_schizo.loc[(beta_schizo['variable'] == "T5") & (beta_schizo['group'] == "healthy")]
s_T5 = beta_schizo.loc[(beta_schizo['variable'] == "T5") & (beta_schizo['group'] == "schizo")]
#do independent t-test for the two variables to find out if there is a significant difference
stats.ttest_ind(h_T5["value"], s_T5["value"], equal_var=False)
Ttest_indResult(statistic=0.6010039892023995, pvalue=0.5536453239922046)
#load average gamma activation of T3 for healthies and schizophrenics to compare them statistically
h_T3 = gamma_schizo.loc[(gamma_schizo['variable'] == "T3") & (gamma_schizo['group'] == "healthy")]
s_T3 = gamma_schizo.loc[(gamma_schizo['variable'] == "T3") & (gamma_schizo['group'] == "schizo")]
#do independent t-test for the two variables to find out if there is a significant difference
stats.ttest_ind(h_T3["value"], s_T3["value"], equal_var=False)
Ttest_indResult(statistic=-1.81202761730623, pvalue=0.08505950808332291)