script3_spectral_analysis.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #%% initial loading of tools
  2. import os
  3. os.chdir('C:\EAfiles\EAdetection')
  4. #import core.ea_management as eam
  5. import core.helpers as hf
  6. import os
  7. os.chdir('C:\EAfiles\MyCode')
  8. import pickle
  9. import matplotlib.cm as cmx # colormap module
  10. import numpy as np # module for low-level scientific computing
  11. import scipy as sp # library for working with NumPy arrays
  12. from scipy import stats
  13. import scipy.signal # signal processing module
  14. import pandas as pd # data manipulation tools. built on NumPy library
  15. import matplotlib.pyplot as plt # makes matplotlib work like MATLAB. ’pyplot’ functions.
  16. import seaborn as sns # based on matplotlib. high-level interface for visualization.
  17. '''
  18. abbreviations:
  19. recID-recording ID: 'animal_session_electrode', e.g. 'PK22_pre1-3_HCc'
  20. sr-sampling rate
  21. auc-area under the curve
  22. psd-power spectral density
  23. spec-spectrogram
  24. dict-dictionary
  25. ddict- data dictionary
  26. pre- pre recording (the hour before stimulation)
  27. stim- recording with 0.1 Hz or 0.05 Hz blue light stimulation
  28. post- post recording (the hour after stimulation)
  29. rec-recording
  30. chan- channel of the recording electrode (HCi1- ipsilateral to saline/kainate injection, HCc-contralateral to saline/kainate injection)
  31. '''
  32. #%% define functions
  33. # prepare variables:
  34. sr = 10000.
  35. #prepare functions:
  36. # Plot the power spectrum:
  37. def plot_psd(f,psd,chan,chan_x, session):
  38. plt.subplot(2,1,chan_x+1)
  39. plt.subplots_adjust(hspace=0.4)
  40. if session == 'pre':
  41. plt.semilogy(f,psd,'darkgrey')
  42. elif session == 'stim':
  43. plt.semilogy(f,psd,'dodgerblue')
  44. #plt.semilogy(f,psd,'dimgrey') #for reference
  45. elif session == 'post':
  46. plt.semilogy(f,psd,'k')
  47. sns.despine()
  48. plt.xlim(0,100)
  49. plt.ylim(10**-6,10**-1)
  50. plt.yticks(size=15)
  51. plt.xticks(size=15)
  52. plt.ylabel('power [$mV^{2}/Hz$]',size=15)
  53. plt.xlabel('f [Hz]',size=15)
  54. plt.title('PSD of '+chan, size=15)
  55. def plot_psd_average(f,psd, sem, session):
  56. if session == 'pre':
  57. plt.semilogy(f,psd,'darkgrey', linewidth=2)
  58. elif session == 'post':
  59. plt.semilogy(f,psd,'k', linewidth=2)
  60. elif session == 'stim':
  61. plt.semilogy(f,psd,'dodgerblue', linewidth=2)
  62. #plt.semilogy(f,psd,'dimgrey', linewidth=2) #for reference
  63. sns.despine()
  64. plt.xlim(0,120)
  65. plt.ylim(10**-6,10**-1)
  66. plt.yticks(size=20)
  67. plt.xticks(size=20)
  68. plt.ylabel('power [$mV^{2}/Hz$]',size=15)
  69. plt.xlabel('f [Hz]',size=15)
  70. #extract area under the curve (auc) of the psd plot
  71. def psd_auc(psd, freq_start, freq_end):
  72. start=freq_start*10 #the f-array is with 0.1 steps so that is why *10 is needed
  73. stop=freq_end*10+1
  74. freq_psd=psd[start:stop]
  75. freq_auc=np.sum(np.abs(freq_psd))
  76. return freq_auc
  77. def psd_auc_dict(stim_ID, dictionary, psd):
  78. all_freq_start=0
  79. all_freq_end=120
  80. gamma_start=30
  81. gamma_end=120
  82. beta_start=12
  83. beta_end=30
  84. theta_start=4
  85. theta_end=12
  86. delta_start=1
  87. delta_end=4
  88. dictionary[stim_ID]['psd_auc']=psd_auc(psd, all_freq_start, all_freq_end)
  89. dictionary[stim_ID]['gamma_auc']=psd_auc(psd, gamma_start, gamma_end)
  90. dictionary[stim_ID]['beta_auc']=psd_auc(psd, beta_start, beta_end)
  91. dictionary[stim_ID]['theta_auc']=psd_auc(psd, theta_start, theta_end)
  92. dictionary[stim_ID]['delta_auc']=psd_auc(psd, delta_start, delta_end)
  93. #plotting spectrograms:
  94. def plot_spec_80szoom(datatrace,recstart_s,recstop_s,vmin=-100,vmax=-20):
  95. #samp_spec = range(0,60*60*int(sr))
  96. samp_spec = range(0,80*int(sr))
  97. f, t_spec, x_spec = sp.signal.spectrogram(datatrace[samp_spec], fs=sr, \
  98. window='hanning', nperseg=1*int(sr/2), nfft=10*int(sr), noverlap=int(0.25*sr), mode='psd')
  99. #for a better quality zoom (shorter time periods), reduce the nperseg and noverlap, eg. noverlap=int(sr*0.05), nperseg=1*int(sr)
  100. '''
  101. f- Array of sample frequencies.
  102. t_spec-Array of segment times.
  103. x_spec- Spectrogram of x. By default, the last axis of x_spec corresponds to the segment times.
  104. '''
  105. fmax = 120
  106. x_mesh, y_mesh = np.meshgrid(t_spec, f[f<fmax])
  107. t_index = np.arange(0,len(datatrace)/sr,0.5/sr) # time array
  108. plt.pcolormesh(x_mesh+t_index[samp_spec[0]], y_mesh, 20*np.log10(x_spec[f<fmax]), cmap=cmx.jet, vmin=vmin, vmax=vmax)
  109. plt.ylabel('f [Hz]', size=12)
  110. plt.yticks(size=12)
  111. plt.xticks(size=12)
  112. #plt.colorbar()
  113. #plt.colorbar(label='dB')
  114. '''now follow exacmples of these functions were used to analyze/present data in Kleis et al., 2021'''
  115. #%% load dictionaries, where recording IDs are matched with corresponding smr file names (with .smr in the end)
  116. filename = 'C:\EAfiles\MyDicts\IDmatch_dict'
  117. infile = open(filename, 'rb')
  118. IDmatch_dict = pickle.load(infile)
  119. infile.close()
  120. filename = 'C:\EAfiles\MyDicts\preIDs_smr'
  121. infile = open(filename, 'rb')
  122. preIDs_smr = pickle.load(infile)
  123. infile.close()
  124. filename = 'C:\EAfiles\MyDicts\stimIDs_smr'
  125. infile = open(filename, 'rb')
  126. stimIDs_smr = pickle.load(infile)
  127. infile.close()
  128. filename = 'C:\EAfiles\MyDicts\postIDs_smr'
  129. infile = open(filename, 'rb')
  130. postIDs_smr= pickle.load(infile)
  131. infile.close()
  132. filename = 'C:\EAfiles\MyDicts\preIDs_smr_contra'
  133. infile = open(filename, 'rb')
  134. preIDs_smr_contra = pickle.load(infile)
  135. infile.close()
  136. filename = 'C:\EAfiles\MyDicts\stimIDs_smr_contra'
  137. infile = open(filename, 'rb')
  138. stimIDs_smr_contra = pickle.load(infile)
  139. infile.close()
  140. filename = 'C:\EAfiles\MyDicts\postIDs_smr_contra'
  141. infile = open(filename, 'rb')
  142. postIDs_smr_contra= pickle.load(infile)
  143. infile.close()
  144. filename = 'C:\EAfiles\MyDicts\preIDs_smr_ipsi'
  145. infile = open(filename, 'rb')
  146. preIDs_smr_ipsi = pickle.load(infile)
  147. infile.close()
  148. filename = 'C:\EAfiles\MyDicts\stimIDs_smr_ipsi'
  149. infile = open(filename, 'rb')
  150. stimIDs_smr_ipsi = pickle.load(infile)
  151. infile.close()
  152. filename = 'C:\EAfiles\MyDicts\postIDs_smr_ipsi'
  153. infile = open(filename, 'rb')
  154. postIDs_smr_ipsi= pickle.load(infile)
  155. infile.close()
  156. #%% creating lists from textfiles, which contain recIDs that will be used for each type of recording
  157. pathtobadrecIDs = 'C:\EAfiles\ID_Dateien\\badrecIDs.txt'
  158. badrecIDs = []
  159. with open (pathtobadrecIDs,'r') as f:
  160. badIDs = f.read().splitlines()
  161. badrecIDs = badrecIDs + badIDs
  162. pathto_saline_ref = 'C:\EAfiles\ID_Dateien\\saline_PACK-CA1_ref.txt' #used in example 1
  163. saline_PACK_CA1_ref = []
  164. with open (pathto_saline_ref,'r') as f:
  165. IDs = f.read().splitlines()
  166. saline_PACK_CA1_ref = saline_PACK_CA1_ref + IDs
  167. pathto_saline_01Hz = 'C:\EAfiles\ID_Dateien\\saline_PACK-CA1_01Hz.txt' #used in example 2
  168. saline_PACK_CA1_01Hz = []
  169. with open (pathto_saline_01Hz,'r') as f:
  170. IDs = f.read().splitlines()
  171. saline_PACK_CA1_01Hz = saline_PACK_CA1_01Hz+ IDs
  172. pathto_saline_bPAC_01Hz = 'C:\EAfiles\ID_Dateien\\saline_bPAC-CA1_01Hz.txt' #used in example 3
  173. saline_bPAC_CA1_01Hz = []
  174. with open (pathto_saline_bPAC_01Hz,'r') as f:
  175. IDs = f.read().splitlines()
  176. saline_bPAC_CA1_01Hz = saline_bPAC_CA1_01Hz+ IDs
  177. #%%(Example 1) plot individual PSDs and create a matrix with all PSDs (for saline PACK reference recordings)
  178. sr=10000
  179. count=0
  180. os.chdir('C:\EAfiles\ID_Dateien')
  181. file = open('saline_PACK-CA1.txt', 'r')
  182. rows = 0
  183. for line in file:
  184. line = line.strip('\n')
  185. rows += 1
  186. psd_saline_ref1_matrix=np.zeros((rows,50001), dtype=float)
  187. psd_saline_ref2_matrix=np.zeros((rows,50001), dtype=float)
  188. psd_saline_ref3_matrix=np.zeros((rows,50001), dtype=float)
  189. psd_saline_ref1_fmatrix=np.zeros((rows,50001), dtype=float)
  190. psd_saline_ref2_fmatrix=np.zeros((rows,50001), dtype=float)
  191. psd_saline_ref3_fmatrix=np.zeros((rows,50001), dtype=float)
  192. # Load data
  193. folder = r'I:\Piret\EEG' #add directory, where your raw data is (.smr files)
  194. for stim_ID in saline_PACK_CA1_ref:
  195. print 'working on '+stim_ID
  196. plt.figure(figsize=(5,8))
  197. pre_ID = IDmatch_dict[stim_ID]['pre']
  198. post_ID = IDmatch_dict[stim_ID]['post']
  199. if stim_ID [-1:] == '2':
  200. chanlist = ['HCi1_2', 'HCc_2']
  201. elif stim_ID [-1:] == '3':
  202. chanlist = ['HCi1_3', 'HCc_3']
  203. elif stim_ID [-1:] == '4':
  204. chanlist = ['HCi1_4', 'HCc_4']
  205. else:
  206. chanlist = ['HCi1', 'HCc' ]
  207. filename_pre = preIDs_smr[pre_ID]
  208. filename_stim = stimIDs_smr[stim_ID]
  209. filename_post = postIDs_smr[post_ID]
  210. ddict_pre = hf.extract_smrViaNeo(os.path.join(folder, filename_pre), chanlist=chanlist)
  211. ddict_stim = hf.extract_smrViaNeo(os.path.join(folder, filename_stim), chanlist=chanlist)
  212. ddict_post = hf.extract_smrViaNeo(os.path.join(folder, filename_post), chanlist=chanlist)
  213. excelpath = r'C:\EAfiles\Excel\startstop_PK_HCc.xlsx'
  214. df = pd.read_excel(excelpath) #reading an excel file with start and stop times (s) of a recording
  215. df.set_index('recID', inplace=True) #recID
  216. pre_start = df.start[pre_ID] #start time
  217. pre_stop = df.stop[pre_ID] #stop time
  218. stim_start = df.start[stim_ID] #start time
  219. stim_stop = df.stop[stim_ID] #stop time
  220. post_start = df.start[post_ID] #start time
  221. post_stop = df.stop[post_ID] #stop time
  222. for chan_x, chan in enumerate(chanlist):
  223. print chan
  224. #get datatrace
  225. datatrace_pre = ddict_pre[chan]['trace']
  226. datatrace_stim = ddict_stim[chan]['trace']
  227. datatrace_post = ddict_post[chan]['trace']
  228. #cut data trace
  229. reccut_pre=datatrace_pre[int(pre_start*sr):int(pre_stop*sr)]
  230. reccut_stim=datatrace_stim[int(stim_start*sr):int(stim_stop*sr)]
  231. reccut_post=datatrace_post[int(post_start*sr):int(post_stop*sr)]
  232. # Compute the power spectrum
  233. f_pre, psd_pre = sp.signal.welch(reccut_pre, sr, nperseg=10*sr)
  234. f_stim, psd_stim = sp.signal.welch(reccut_stim, sr, nperseg=10*sr)
  235. f_post, psd_post = sp.signal.welch(reccut_post, sr, nperseg=10*sr)
  236. #plot the power spectrum
  237. plot_psd(f_pre,psd_pre,chan,chan_x, 'pre')
  238. plot_psd(f_stim,psd_stim,chan,chan_x, 'stim')
  239. plot_psd(f_post,psd_post,chan,chan_x, 'post')
  240. #add the power spectrum into psd_matrix:
  241. if chan=='HCi1_2' or chan=='HCi1':
  242. psd_saline_ref1_matrix[count]=psd_pre
  243. psd_saline_ref2_matrix[count]=psd_stim
  244. psd_saline_ref3_matrix[count]=psd_post
  245. psd_saline_ref1_fmatrix[count]=f_pre
  246. psd_saline_ref2_fmatrix[count]=f_stim
  247. psd_saline_ref3_fmatrix[count]=f_post
  248. elif chan=='HCi1_3' or chan=='HCi1_4':
  249. psd_saline_ref1_matrix[count]=psd_pre
  250. psd_saline_ref2_matrix[count]=psd_stim
  251. psd_saline_ref3_matrix[count]=psd_post
  252. psd_saline_ref1_fmatrix[count]=f_pre
  253. psd_saline_ref2_fmatrix[count]=f_stim
  254. psd_saline_ref3_fmatrix[count]=f_post
  255. count += 1
  256. plt.savefig('C:\EAfiles\SPECTROGRAM\PSD_PACK_reference/'+stim_ID+'_psd.png')
  257. #plt.close('all')
  258. #%% save the matrices containing PSDs:
  259. np.save('psd_ref1_matrix', psd_saline_ref1_matrix)
  260. np.save('psd_ref2_matrix', psd_saline_ref2_matrix)
  261. np.save('psd_ref3_matrix', psd_saline_ref3_matrix)
  262. np.save('psd_ref1_fmatrix', psd_saline_ref1_fmatrix)
  263. np.save('psd_ref2_fmatrix', psd_saline_ref2_fmatrix)
  264. np.save('psd_ref3_fmatrix', psd_saline_ref3_fmatrix)
  265. #%% plot the average PSDs:
  266. psd_saline_ref1_average=np.mean(psd_saline_ref1_matrix, axis=0)
  267. psd_saline_ref2_average=np.mean(psd_saline_ref2_matrix, axis=0)
  268. psd_saline_ref3_average=np.mean(psd_saline_ref3_matrix, axis=0)
  269. psd_saline_ref1_std=np.std(psd_saline_ref1_matrix, axis=0)
  270. psd_saline_ref2_std=np.std(psd_saline_ref2_matrix, axis=0)
  271. psd_saline_ref3_std=np.std(psd_saline_ref3_matrix, axis=0)
  272. psd_saline_ref1_sem=stats.sem(psd_saline_ref1_matrix, axis=0)
  273. psd_saline_ref2_sem=stats.sem(psd_saline_ref2_matrix, axis=0)
  274. psd_saline_ref3_sem=stats.sem(psd_saline_ref3_matrix, axis=0)
  275. f_saline_ref1_average=np.mean(psd_saline_ref1_fmatrix, axis=0)
  276. f_saline_ref2_average=np.mean(psd_saline_ref2_fmatrix, axis=0)
  277. f_saline_ref3_average=np.mean(psd_saline_ref3_fmatrix, axis=0)
  278. np.save('psd_saline-PACK_ref1_average', psd_saline_ref1_average)
  279. np.save('psd_saline-PACK_ref2_average', psd_saline_ref2_average)
  280. np.save('psd_saline-PACL_ref3_average', psd_saline_ref3_average)
  281. plt.figure(figsize=(6,5))
  282. plot_psd_average(f_saline_ref1_average,psd_saline_ref1_average, psd_saline_ref1_sem, 'pre')
  283. plot_psd_average(f_saline_ref2_average,psd_saline_ref2_average, psd_saline_ref2_sem, 'stim')
  284. plot_psd_average(f_saline_ref3_average,psd_saline_ref3_average, psd_saline_ref3_sem, 'post')
  285. #%% (Example 2) getting the power areas of delta, theta, beta, and gamma oscillations
  286. #by taking the AUC of PSD plots in respective ranges
  287. sr=10000
  288. count=0
  289. os.chdir('C:\EAfiles\ID_Dateien')
  290. file = open('saline_bPAC-CA1_01Hz.txt', 'r')
  291. rows = 0
  292. psd_bPAC_01Hz_pre={}
  293. psd_bPAC_01Hz_stim={}
  294. psd_bPAC_01Hz_post={}
  295. # Load data
  296. folder = r'I:\Piret\EEG'
  297. for stim_ID in stimIDs_smr_contra.keys():
  298. if stim_ID in saline_bPAC_CA1_01Hz:
  299. #else:
  300. print 'working on '+stim_ID
  301. psd_bPAC_01Hz_pre[stim_ID]={}
  302. psd_bPAC_01Hz_stim[stim_ID]={}
  303. psd_bPAC_01Hz_post[stim_ID]={}
  304. plt.figure(figsize=(5,8))
  305. pre_ID = IDmatch_dict[stim_ID]['pre']
  306. post_ID = IDmatch_dict[stim_ID]['post']
  307. if stim_ID [-1:] == '2':
  308. chanlist = ['HCc_2']
  309. elif stim_ID [-1:] == '3':
  310. chanlist = ['HCc_3']
  311. elif stim_ID [-1:] == '4':
  312. chanlist = ['HCc_4']
  313. else:
  314. chanlist = ['HCc' ]
  315. filename_pre = preIDs_smr[pre_ID]
  316. filename_stim = stimIDs_smr[stim_ID]
  317. filename_post = postIDs_smr[post_ID]
  318. ddict_pre = hf.extract_smrViaNeo(os.path.join(folder, filename_pre), chanlist=chanlist)
  319. ddict_stim = hf.extract_smrViaNeo(os.path.join(folder, filename_stim), chanlist=chanlist)
  320. ddict_post = hf.extract_smrViaNeo(os.path.join(folder, filename_post), chanlist=chanlist)
  321. excelpath = r'C:\EAfiles\Excel\startstop_controls_HCc.xlsx'
  322. df = pd.read_excel(excelpath) #reading an excel file with start and stop times (s) of a recording
  323. df.set_index('recID', inplace=True) #recID
  324. pre_start = df.start[pre_ID] #start time
  325. pre_stop = df.stop[pre_ID] #stop time
  326. stim_start = df.start[stim_ID] #start time
  327. stim_stop = df.stop[stim_ID] #stop time
  328. post_start = df.start[post_ID] #start time
  329. post_stop = df.stop[post_ID] #stop time
  330. for chan_x, chan in enumerate(chanlist):
  331. print chan
  332. #get datatrace
  333. datatrace_pre = ddict_pre[chan]['trace']
  334. datatrace_stim = ddict_stim[chan]['trace']
  335. datatrace_post = ddict_post[chan]['trace']
  336. #cut data trace
  337. reccut_pre=datatrace_pre[int(pre_start*sr):int(pre_stop*sr)]
  338. reccut_stim=datatrace_stim[int(stim_start*sr):int(stim_stop*sr)]
  339. reccut_post=datatrace_post[int(post_start*sr):int(post_stop*sr)]
  340. # Compute the power spectrum
  341. f_pre, psd_pre = sp.signal.welch(reccut_pre, sr, nperseg=10*sr)
  342. f_stim, psd_stim = sp.signal.welch(reccut_stim, sr, nperseg=10*sr)
  343. f_post, psd_post = sp.signal.welch(reccut_post, sr, nperseg=10*sr)
  344. #add the psds into dictionaries:
  345. psd_bPAC_01Hz_pre[stim_ID]['psd']=psd_pre
  346. psd_bPAC_01Hz_stim[stim_ID]['psd']=psd_stim
  347. psd_bPAC_01Hz_post[stim_ID]['psd']=psd_post
  348. #calculate and save power areas for beta, low gamma, high gamma, all frequencies:
  349. psd_auc_dict(stim_ID, psd_bPAC_01Hz_pre, psd_pre)
  350. psd_auc_dict(stim_ID, psd_bPAC_01Hz_stim, psd_stim)
  351. psd_auc_dict(stim_ID, psd_bPAC_01Hz_post, psd_post)
  352. count += 1
  353. #save the 0.1 Hz PSD dictionaries and later convert them to excel files to extract data:
  354. filename = 'C:\EAfiles\MyDicts\psd_bPAC_01Hz_pre'
  355. outfile = open(filename, 'wb') #write binary: mit Erlaubnis zu (über)schreiben
  356. pickle.dump(psd_bPAC_01Hz_pre, outfile)
  357. outfile.close()
  358. filename = 'C:\EAfiles\MyDicts\psd_bPAC_01Hz_stim'
  359. outfile = open(filename, 'wb') #write binary: mit Erlaubnis zu (über)schreiben
  360. pickle.dump(psd_bPAC_01Hz_stim, outfile)
  361. outfile.close()
  362. filename = 'C:\EAfiles\MyDicts\psd_bPAC_01Hz_post'
  363. outfile = open(filename, 'wb') #write binary: mit Erlaubnis zu (über)schreiben
  364. pickle.dump(psd_bPAC_01Hz_post, outfile)
  365. outfile.close()
  366. #%% (Example 3) plot spectrograms for saline PACK mice in sessions with 0.1 Hz stimulation (used for the figures in the paper, aligned with snippets from Spike2 LFPs)
  367. folder = r'I:\Piret\EEG'
  368. for stim_ID in IDmatch_dict.keys():
  369. if stim_ID in saline_PACK_CA1_01Hz:
  370. plt.figure(figsize=(5,8))
  371. pre_ID = IDmatch_dict[stim_ID]['pre']
  372. post_ID = IDmatch_dict[stim_ID]['post']
  373. if stim_ID [-1:] == '2':
  374. chanlist = ['HCc_2']
  375. elif stim_ID [-1:] == '3':
  376. chanlist = ['HCc_3']
  377. elif stim_ID [-1:] == '4':
  378. chanlist = ['HCc_4']
  379. else:
  380. chanlist = ['HCc']
  381. filename_pre = preIDs_smr[pre_ID]
  382. filename_stim = stimIDs_smr[stim_ID]
  383. filename_post = postIDs_smr[post_ID]
  384. ddict_pre = hf.extract_smrViaNeo(os.path.join(folder, filename_pre), chanlist=chanlist)
  385. ddict_stim = hf.extract_smrViaNeo(os.path.join(folder, filename_stim), chanlist=chanlist)
  386. ddict_post = hf.extract_smrViaNeo(os.path.join(folder, filename_post), chanlist=chanlist)
  387. pre_start=1100
  388. pre_stop=1180
  389. stim_start=1100
  390. stim_stop=1180
  391. post_start=1100
  392. post_stop=1180
  393. for chan_x, chan in enumerate(chanlist):
  394. print chan
  395. sr=ddict_pre[chan]['sr']
  396. #get datatrace:
  397. datatrace_pre = ddict_pre[chan]['trace']
  398. datatrace_stim = ddict_stim[chan]['trace']
  399. datatrace_post = ddict_post[chan]['trace']
  400. #cut data trace:
  401. reccut_pre=datatrace_pre[int(pre_start*sr):int(pre_stop*sr)]
  402. reccut_stim=datatrace_stim[int(stim_start*sr):int(stim_stop*sr)]
  403. reccut_post=datatrace_post[int(post_start*sr):int(post_stop*sr)]
  404. #plot spectrograms for 80seconf LFP snippets:
  405. plt.subplot(4,1,1)
  406. plot_spec_80szoom(reccut_pre,pre_start,pre_stop,vmin=-100,vmax=-20)
  407. plt.title(stim_ID)
  408. plt.subplot(4,1,2)
  409. plot_spec_80szoom(reccut_stim,stim_start,stim_stop,vmin=-100,vmax=-20)
  410. plt.subplot(4,1,3)
  411. plot_spec_80szoom(reccut_post,post_start,post_stop,vmin=-100,vmax=-20)
  412. #showing 0.1 Hz pulses:
  413. plt.subplot(4,1,4)
  414. rectime_y=np.zeros(3600)
  415. rectime_x=np.arange(0,3600)
  416. start=10
  417. stop=3600
  418. period=10
  419. stimtimes=np.arange(start, stop, period)
  420. for i in rectime_x:
  421. if i in stimtimes:
  422. rectime_y[i]=1
  423. plt.scatter(rectime_x[stim_start:stim_stop], rectime_y[stim_start:stim_stop], marker='|', color='dodgerblue')
  424. plt.xlim(stim_start,stim_stop)
  425. plt.show()
  426. plt.savefig('C:\EAfiles\SPECTROGRAM\saline_bPAC_01Hz/'+stim_ID+'_fft.png')
  427. plt.close('all')