forked from jonescompneurolab/hnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vislfp.py
289 lines (250 loc) · 10.4 KB
/
vislfp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import sys, os
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QToolTip, QPushButton, QFormLayout
from PyQt5.QtWidgets import QMenu, QSizePolicy, QMessageBox, QWidget, QFileDialog, QComboBox, QTabWidget
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QGroupBox, QDialog, QGridLayout, QLineEdit, QLabel
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtGui import QIcon, QFont, QPixmap
from PyQt5.QtCore import QCoreApplication, QThread, pyqtSignal, QObject, pyqtSlot
from PyQt5 import QtCore
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import pylab as plt
import matplotlib.gridspec as gridspec
from DataViewGUI import DataViewGUI
from neuron import h
from run import net
import paramrw
from filt import boxfilt, hammfilt, lowpass
import spikefn
from math import ceil
from conf import dconf
from specfn import MorletSpec
if dconf['fontsize'] > 0: plt.rcParams['font.size'] = dconf['fontsize']
debug = True
tstop = -1; ntrial = 1; maxlfp = 0; scalefctr = 30e3; lfppath = ''; paramf = ''; laminar = False
for i in range(len(sys.argv)):
if sys.argv[i].endswith('.txt'):
lfppath = sys.argv[i]
elif sys.argv[i].endswith('.param'):
paramf = sys.argv[i]
scalefctr = paramrw.find_param(paramf,'dipole_scalefctr')
if type(scalefctr)!=float and type(scalefctr)!=int: scalefctr=30e3
tstop = paramrw.find_param(paramf,'tstop')
ntrial = paramrw.quickgetprm(paramf,'N_trials',int)
basedir = os.path.join(dconf['datdir'],paramf.split(os.path.sep)[-1].split('.param')[0])
ddat = {}; tvec = None; dspec = None
def readLFPs (basedir, ntrial):
if debug: print('readLFPs')
ddat = {'lfp':{}}
lfile = os.listdir(basedir)
maxlfp = 0; tvec = None
if debug: print('readLFPs:',lfile)
for f in lfile:
if f.count('lfp_') > 0 and f.endswith('.txt'):
lf = f.split('.txt')[0].split('_')
if debug: print('readLFPs: lf=',lf,'ntrial=',ntrial)
if ntrial > 1:
trial = int(lf[1])
nlfp = int(lf[2])
else:
trial = 0
nlfp = int(lf[1])
maxlfp = max(nlfp,maxlfp)
if debug: print('readLFPs:',trial,nlfp,maxlfp)
fullpath = os.path.join(basedir,f)
if debug: print('readLFPs: fullpath=',fullpath)
try:
k2 = (trial,nlfp)
#print('k2:',k2)
ddat['lfp'][k2] = np.loadtxt(fullpath)
if tvec is None: tvec = ddat['lfp'][k2][:,0]
except:
print('exception!')
print('readLFPs:',ddat['lfp'].keys())
#print('ddat:',ddat,maxlfp)
return ddat, maxlfp, tvec
# lowpass filter the items in lfps. lfps is a list or numpy array of LFPs arranged spatially by row
def getlowpass (lfps,sampr,maxf):
return np.array([lowpass(lfp,maxf,df=sampr,zerophase=True) for lfp in lfps])
# gets 2nd spatial derivative of voltage as approximation of CSD.
# performs lowpass filter on voltages before taking spatial derivative
# input dlfp is dictionary of LFP voltage time-series keyed by (trial, electrode)
# output dCSD is keyed by trial
def getCSD (dlfp,sampr,minf=0.1,maxf=300.0):
if debug: print('getCSD:','sampr=',sampr,'ntrial=',ntrial,'maxlfp=',maxlfp)
dCSD = {}
for trial in range(ntrial):
if debug: print('trial:',trial)
lfps = [dlfp[(trial,i)][:,1] for i in range(maxlfp+1)]
datband = getlowpass(lfps,sampr,maxf)
dCSD[trial] = -np.diff(datband,n=2,axis=0) # now each row is an electrode -- CSD along electrodes
return dCSD
try:
ddat, maxlfp, tvec = readLFPs(basedir,ntrial)
if maxlfp > 1: laminar = True
ddat['spec'] = {}
waveprm = {'f_max_spec':40.0,'dt':tvec[1]-tvec[0],'tstop':tvec[-1]}
minwavet = 50.0
sampr = 1e3 / (tvec[1]-tvec[0])
if laminar:
print('getting CSD')
ddat['CSD'] = getCSD(ddat['lfp'],sampr)
if ntrial > 1:
ddat['avgCSD'] = np.zeros(ddat['CSD'][1].shape)
for i in range(ntrial): ddat['avgCSD'] += ddat['CSD'][i]
ddat['avgCSD']/=float(ntrial)
print('Extracting Wavelet spectrogram(s).')
for i in range(maxlfp+1):
for trial in range(ntrial):
ddat['spec'][(trial,i)] = MorletSpec(tvec, ddat['lfp'][(trial,i)][:,1],None,None,waveprm,minwavet)
if ntrial > 1:
if debug: print('here')
davglfp = {}; davgspec = {}
for i in range(maxlfp+1):
if debug: print(i,maxlfp,list(ddat['lfp'].keys())[0])
davglfp[i] = np.zeros(len(ddat['lfp'][list(ddat['lfp'].keys())[0]]),)
try:
ms = ddat['spec'][(0,0)]
if debug: print('shape',ms.TFR.shape,ms.tmin,ms.f[0],ms.f[-1])
davgspec[i] = [np.zeros(ms.TFR.shape), ms.tmin, ms.f]
except:
print('err in davgspec[i]=')
for trial in range(ntrial):
davglfp[i] += ddat['lfp'][(trial,i)][:,1]
davgspec[i][0] += ddat['spec'][(trial,i)].TFR
davglfp[i] /= float(ntrial)
davgspec[i][0] /= float(ntrial)
ddat['avglfp'] = davglfp
ddat['avgspec'] = davgspec
except:
print('Could not load LFPs')
quit()
def getnorm (yin):
yout = yin - min(yin)
return yout / max(yout)
def getrngfctroff (dat):
yrng = [max(dat[i,:])-min(dat[i,:]) for i in range(dat.shape[0])]
mxrng = np.amax(yrng)
yfctr = [yrng[i]/mxrng for i in range(len(yrng))]
yoff = [maxlfp - 1 - (i + 1) for i in range(len(yrng))]
return yrng,yfctr,yoff
class LFPCanvas (FigureCanvas):
def __init__ (self, paramf, index, parent=None, width=12, height=10, dpi=120, title='LFP Viewer'):
FigureCanvas.__init__(self, Figure(figsize=(width, height), dpi=dpi))
self.title = title
self.setParent(parent)
self.index = index
FigureCanvas.setSizePolicy(self,QSizePolicy.Expanding,QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.paramf = paramf
self.drawwavelet = True
self.plot()
def clearaxes (self):
try:
for ax in self.lax:
ax.set_yticks([])
ax.cla()
except:
pass
def drawCSD (self, fig, G):
ax = fig.add_subplot(G[:,2])
ax.set_yticks([])
lw = 2; clr = 'k'
if ntrial > 1:
if self.index == 0:
cax = ax.imshow(ddat['avgCSD'],extent=[0, tstop, 0, maxlfp-1], aspect='auto', origin='upper',cmap=plt.get_cmap('jet'),interpolation='None')
# overlay the time-series
yrng,yfctr,yoff = getrngfctroff(ddat['avgCSD'])
for i in range(ddat['avgCSD'].shape[0]):
y = yfctr[i] * getnorm(ddat['avgCSD'][i,:]) + yoff[i]
ax.plot(tvec,y,clr,linewidth=lw)
else:
cax = ax.imshow(ddat['CSD'][self.index-1],extent=[0, tstop, 0, maxlfp-1], aspect='auto', origin='upper',cmap=plt.get_cmap('jet'),interpolation='None')
# overlay the time-series
yrng,yfctr,yoff = getrngfctroff(ddat['CSD'][self.index-1])
for i in range(ddat['CSD'][self.index-1].shape[0]):
y = yfctr[i] * getnorm(ddat['CSD'][self.index-1][i,:]) + yoff[i]
ax.plot(tvec,y,clr,linewidth=lw)
else:
# draw CSD as image; blue/red corresponds to excit/inhib
cax = ax.imshow(ddat['CSD'][0],extent=[0, tstop, 0, 15], aspect='auto', origin='upper',cmap=plt.get_cmap('jet'),interpolation='None')
# overlay the time-series
yrng,yfctr,yoff = getrngfctroff(ddat['CSD'][0])
for i in range(ddat['CSD'][0].shape[0]):
y = yfctr[i] * getnorm(ddat['CSD'][0][i,:]) + yoff[i]
ax.plot(tvec,y,clr,linewidth=lw)
cbaxes = fig.add_axes([0.69, 0.88, 0.005, 0.1])
fig.colorbar(cax, cax=cbaxes, orientation='vertical')
ax.set_xlim((minwavet,tstop)); ax.set_ylim((0,maxlfp-1))
def drawLFP (self, fig):
if laminar:
nrow = maxlfp+1
ncol = 3
ltitle = ['' for x in range(nrow*ncol)]
else:
nrow = (maxlfp+1) * 2
ncol = 1
ltitle = ['LFP'+str(x) for x in range(nrow)]
G = gridspec.GridSpec(nrow,ncol)
white_patch = mpatches.Patch(color='white', label='Average')
gray_patch = mpatches.Patch(color='gray', label='Individual')
lpatch = []
if debug: print('ntrial:',ntrial)
if ntrial > 1: lpatch = [white_patch,gray_patch]
yl = [1e9,-1e9]
minx = 100
for i in [1]: # this gets min,max LFP values
# print('ddat[lfp].keys():',ddat['lfp'].keys())
for k in ddat['lfp'].keys():
yl[0] = min(yl[0],ddat['lfp'][k][minx:-1,i].min())
yl[1] = max(yl[1],ddat['lfp'][k][minx:-1,i].max())
yl = tuple(yl) # y-axis range
self.lax = []
for nlfp in range(maxlfp+1):
title = ltitle[nlfp]
if laminar: ax = fig.add_subplot(G[nlfp, 0])
else: ax = fig.add_subplot(G[nlfp*2])
self.lax.append(ax)
if self.index == 0: # draw all along with average
if ntrial > 1: clr = 'gray'
else: clr = 'white'
for i in range(ntrial): ax.plot(tvec,ddat['lfp'][(i,nlfp)][:,1],color=clr,linewidth=2)
if ntrial > 1:
ax.plot(tvec,ddat['avglfp'][nlfp],'w',linewidth=3)
if nlfp == 0: ax.legend(handles=lpatch)
else: # draw individual trial
ax.plot(tvec,ddat['lfp'][(self.index-1,nlfp)][:,1],color='white',linewidth=2)
if not laminar: ax.set_ylabel(r'$\mu V$')
if tstop != -1: ax.set_xlim((minwavet,tstop))
ax.set_ylim(yl)
ax.set_facecolor('k'); ax.grid(True); ax.set_title(title)
# plot wavelet spectrogram
if laminar: ax = fig.add_subplot(G[nlfp, 1])
else: ax = fig.add_subplot(G[nlfp*2+1])
self.lax.append(ax)
if self.index == 0:
if ntrial > 1:
TFR,tmin,F = ddat['avgspec'][nlfp]
ax.imshow(TFR, extent=[tmin, tvec[-1], F[-1], F[0]], aspect='auto', origin='upper',cmap=plt.get_cmap('jet'))
else:
ms = ddat['spec'][(0,nlfp)]
ax.imshow(ms.TFR, extent=[ms.tmin, tvec[-1], ms.f[-1], ms.f[0]], aspect='auto', origin='upper',cmap=plt.get_cmap('jet'))
else:
ms = ddat['spec'][(self.index-1,nlfp)]
ax.imshow(ms.TFR, extent=[ms.tmin, tvec[-1], ms.f[-1], ms.f[0]], aspect='auto', origin='upper',cmap=plt.get_cmap('jet'))
ax.set_xlim(minwavet,tvec[-1])
if nlfp == maxlfp: ax.set_xlabel('Time (ms)')
if not laminar: ax.set_ylabel('Frequency (Hz)');
if laminar: self.drawCSD(fig, G)
self.figure.subplots_adjust(bottom=0.04, left=0.04, right=1.0, top=0.99, wspace=0.1, hspace=0.01)
def plot (self):
self.drawLFP(self.figure)
self.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = DataViewGUI(LFPCanvas,paramf,ntrial,'LFP Viewer')
sys.exit(app.exec_())