#%% initial loading of tools import os os.chdir('C:\EAfiles\EAdetection') #import core.ea_management as eam import core.helpers as hf import os os.chdir('C:\EAfiles\MyCode') import pickle import matplotlib.cm as cmx # colormap module import numpy as np # module for low-level scientific computing import scipy as sp # library for working with NumPy arrays from scipy import stats import scipy.signal # signal processing module import pandas as pd # data manipulation tools. built on NumPy library import matplotlib.pyplot as plt # makes matplotlib work like MATLAB. ’pyplot’ functions. import seaborn as sns # based on matplotlib. high-level interface for visualization. ''' abbreviations: recID-recording ID: 'animal_session_electrode', e.g. 'PK22_pre1-3_HCc' sr-sampling rate auc-area under the curve psd-power spectral density spec-spectrogram dict-dictionary ddict- data dictionary pre- pre recording (the hour before stimulation) stim- recording with 0.1 Hz or 0.05 Hz blue light stimulation post- post recording (the hour after stimulation) rec-recording chan- channel of the recording electrode (HCi1- ipsilateral to saline/kainate injection, HCc-contralateral to saline/kainate injection) ''' #%% define functions # prepare variables: sr = 10000. #prepare functions: # Plot the power spectrum: def plot_psd(f,psd,chan,chan_x, session): plt.subplot(2,1,chan_x+1) plt.subplots_adjust(hspace=0.4) if session == 'pre': plt.semilogy(f,psd,'darkgrey') elif session == 'stim': plt.semilogy(f,psd,'dodgerblue') #plt.semilogy(f,psd,'dimgrey') #for reference elif session == 'post': plt.semilogy(f,psd,'k') sns.despine() plt.xlim(0,100) plt.ylim(10**-6,10**-1) plt.yticks(size=15) plt.xticks(size=15) plt.ylabel('power [$mV^{2}/Hz$]',size=15) plt.xlabel('f [Hz]',size=15) plt.title('PSD of '+chan, size=15) def plot_psd_average(f,psd, sem, session): if session == 'pre': plt.semilogy(f,psd,'darkgrey', linewidth=2) elif session == 'post': plt.semilogy(f,psd,'k', linewidth=2) elif session == 'stim': plt.semilogy(f,psd,'dodgerblue', linewidth=2) #plt.semilogy(f,psd,'dimgrey', linewidth=2) #for reference sns.despine() plt.xlim(0,120) plt.ylim(10**-6,10**-1) plt.yticks(size=20) plt.xticks(size=20) plt.ylabel('power [$mV^{2}/Hz$]',size=15) plt.xlabel('f [Hz]',size=15) #extract area under the curve (auc) of the psd plot def psd_auc(psd, freq_start, freq_end): start=freq_start*10 #the f-array is with 0.1 steps so that is why *10 is needed stop=freq_end*10+1 freq_psd=psd[start:stop] freq_auc=np.sum(np.abs(freq_psd)) return freq_auc def psd_auc_dict(stim_ID, dictionary, psd): all_freq_start=0 all_freq_end=120 gamma_start=30 gamma_end=120 beta_start=12 beta_end=30 theta_start=4 theta_end=12 delta_start=1 delta_end=4 dictionary[stim_ID]['psd_auc']=psd_auc(psd, all_freq_start, all_freq_end) dictionary[stim_ID]['gamma_auc']=psd_auc(psd, gamma_start, gamma_end) dictionary[stim_ID]['beta_auc']=psd_auc(psd, beta_start, beta_end) dictionary[stim_ID]['theta_auc']=psd_auc(psd, theta_start, theta_end) dictionary[stim_ID]['delta_auc']=psd_auc(psd, delta_start, delta_end) #plotting spectrograms: def plot_spec_80szoom(datatrace,recstart_s,recstop_s,vmin=-100,vmax=-20): #samp_spec = range(0,60*60*int(sr)) samp_spec = range(0,80*int(sr)) f, t_spec, x_spec = sp.signal.spectrogram(datatrace[samp_spec], fs=sr, \ window='hanning', nperseg=1*int(sr/2), nfft=10*int(sr), noverlap=int(0.25*sr), mode='psd') #for a better quality zoom (shorter time periods), reduce the nperseg and noverlap, eg. noverlap=int(sr*0.05), nperseg=1*int(sr) ''' f- Array of sample frequencies. t_spec-Array of segment times. x_spec- Spectrogram of x. By default, the last axis of x_spec corresponds to the segment times. ''' fmax = 120 x_mesh, y_mesh = np.meshgrid(t_spec, f[f