-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotFig2_v2.py
More file actions
300 lines (263 loc) · 11.1 KB
/
plotFig2_v2.py
File metadata and controls
300 lines (263 loc) · 11.1 KB
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
290
291
292
293
294
295
296
297
298
299
300
# Original Author: U. S. Bhalla, NCBS, Bangalore
# https://github.com/BhallaLab/STP_EI_paper_figs
# Modified by: Aditya Asopa, NCBS, Bangalore
# Date: 26 Jan 2024
import pandas
import matplotlib.pyplot as plt
import numpy as np
import argparse
import math
import scipy.optimize as sci
# datadir = "../../../2022/VC_DATA"
#datadir = "/home1/bhalla/adityaa/Lab/Projects/EI_Dynamics/Analysis/parsed_data"
sampleRate = 20000.0
OnsetDelay = 0.009 # Delay from the light stimulus to onset of syn current
startT = 0.2 + OnsetDelay
endT = 0.5 + OnsetDelay
runtime = 1.0
def tauFit(kernel, baseline):
"""
Fit an exponential curve to the kernel and return the fit parameters.
Args:
kernel (numpy.ndarray): The kernel data.
baseline (float): The baseline value of the kernel.
Returns:
tuple: A tuple containing the fit parameters (a, tau).
"""
y = kernel[int(round(0.05 * sampleRate)):]
pk = y[0]
x = np.linspace(0, len(y) / sampleRate, len(y), endpoint=False)
ret, cov = sci.curve_fit(lambda t, a, tau: a * np.exp(-t / tau), x, y, p0=(pk - baseline, 0.02))
plt.plot(x + startT + 0.05, ret[0] * np.exp(-x / ret[1]), ":")
return ret
def calcKernel(dat):
"""
Calculate the kernel from the input data.
Args:
dat (pandas.Series): The input data series.
Returns:
tuple: A tuple containing the peak value, kernel, baseline, and fit parameters (a, tau).
"""
startIdx = int(round(startT * sampleRate))
endIdx = int(round(endT * sampleRate))
baseline = np.mean(dat.iloc[startIdx - int(0.005 * sampleRate):startIdx])
rawKernel = np.array(dat.iloc[startIdx:endIdx])
kmax = max(rawKernel)
kmin = min(rawKernel)
if math.isnan(kmax) or math.isnan(kmin):
raise FloatingPointError("calcKernel: kmax or kmin is a nan")
if abs(kmax) > abs(kmin): # Inhib trace has a positive peak.
return kmax, rawKernel, baseline, tauFit(rawKernel, baseline)
else:
return kmin, rawKernel, baseline, tauFit(rawKernel, baseline)
def findStpScale(kernel, kpk, ret, si, stimWidth, tau, ax):
"""
Find the scale factor for short-term plasticity (STP) from the kernel and fit parameters.
Args:
kernel (numpy.ndarray): The kernel data.
kpk (float): The peak value of the kernel.
ret (tuple): The fit parameters (a, tau) from tauFit.
si (int): The start index of the stimulus.
stimWidth (int): The width of the stimulus in samples.
tau (tuple): The fit parameters (a, tau) from tauFit.
ax (matplotlib.axes.Axes): The axes object for plotting (optional).
Returns:
float: The scale factor for STP.
"""
# ret[0,1] = valley_t, y; ret[1,2] = pk_t, y
if ret[0] < endT and si < (endT * sampleRate):
return 1.0
if kpk < 0: # Exc
kpkIdx = np.argmin(kernel[:-stimWidth])
else:
kpkIdx = np.argmax(kernel[:-stimWidth])
pkIdx = int(round((ret[2]) * sampleRate)) - si
riseIdx = int(round((ret[2] - ret[0]) * sampleRate))
riseDelta1 = kernel[kpkIdx + stimWidth - riseIdx] - kernel[kpkIdx + stimWidth]
riseDelta = ret[1] - ret[1] * np.exp(-(ret[2] - ret[0]) / tau[1])
if ax:
label = "Min to Max" if (si < 11000 and kpk > 0) else None
ax.plot([ret[2], ret[2]], [-riseDelta + ret[1], ret[3]], "ro-", label=label)
if ret[0] < endT + 0.01: # First pulse after ref.
riseTotal = ret[3] - ret[1]
else:
riseTotal = riseDelta + ret[3] - ret[1]
return riseTotal / kpk
def findPkVal(dat, freq, startIdx, isExc):
"""
Find the peak and trough values for a given stimulus in the input data.
Args:
dat (pandas.Series): The input data series.
freq (float): The stimulus frequency.
startIdx (int): The start index of the stimulus.
isExc (bool): True if the input is excitatory, False if inhibitory.
Returns:
tuple: A tuple containing the time, value of the preceding trough, time, and value of the peak.
"""
stimWidth = int(round(0.7 * sampleRate / freq))
d2 = np.array(dat.iloc[startIdx:startIdx + stimWidth])
if isExc: # Sign flipped in current. imin is peak.
imin = np.argmin(d2)
# Look for valley preceding this.
d3 = np.array(dat.iloc[startIdx + imin - stimWidth:imin + startIdx])
imax = np.argmax(d3)
if imax + imin - stimWidth > imin:
print("WARNING: reversal")
return [(imax + startIdx + imin - stimWidth) / sampleRate, d3[imax],
(startIdx + imin) / sampleRate, d2[imin]]
else: # This is the inhibitory input, which has positive currents.
imax = np.argmax(d2)
d3 = np.array(dat.iloc[startIdx + imax - stimWidth:imax + startIdx])
imin = np.argmin(d3)
if imax < imin + imax - stimWidth:
print("WARNING: reversal")
return [(imin + startIdx + imax - stimWidth) / sampleRate, d3[imin],
(startIdx + imax) / sampleRate, d2[imax]]
def deconv(dat, freq, ax):
"""
Perform deconvolution on the input data and plot the results.
Args:
dat (pandas.Series): The input data series.
freq (float): The stimulus frequency.
ax (matplotlib.axes.Axes): The axes object for plotting.
Returns:
tuple: A tuple containing the deconvolved data, synthetic plot, and peak/trough values.
"""
startIdx = int(round(endT * sampleRate))
stimWidth = int(round(sampleRate / freq))
stimIdx = [int(startT * sampleRate)] + [int(round(sampleRate * (endT + i / freq))) for i in range(8)]
kpk, kernel, baseline, tau = calcKernel(dat)
kpkidx = np.argmax(kernel) if kpk > 0 else np.argmin(kernel)
scaleList = []
absPk = [kpk]
absVal = [baseline]
pv = []
correctedStimIdx = []
for si in stimIdx:
ret = findPkVal(dat, freq, si + kpkidx // 2, (kpk < 0))
pv.append(ret)
scale = findStpScale(kernel, kpk, ret, si, stimWidth, tau, ax)
scaleList.append(scale)
if kpk > 0:
label = "Inh"
else:
label = "Exc"
npv = np.array(pv).transpose()
synthPlot = plotFromKernel(scaleList, stimIdx, kernel, freq, npv, label, ax)
return np.array(scaleList), synthPlot, npv
def plotFromKernel(scaleList, stimIdx, kernel, freq, npv, label, ax):
"""
Plot the synthetic trace from the kernel and scale factors.
Args:
scaleList (list): The list of scale factors.
stimIdx (list): The list of stimulus indices.
kernel (numpy.ndarray): The kernel data.
freq (float): The stimulus frequency.
npv (numpy.ndarray): The array of peak/trough values.
label (str): The label for the plot ("Inh" or "Exc").
ax (matplotlib.axes.Axes): The axes object for plotting.
Returns:
numpy.ndarray: The synthetic trace data.
"""
ret = np.zeros(int(round(sampleRate * 1.5)))
ret[int(round(sampleRate * endT)):] += npv[1][1]
for ii in range(len(scaleList)):
ss = scaleList[ii]
idx = stimIdx[ii]
if idx > 0:
ks = kernel * ss
if label == "Inh":
offset = npv[3, ii] - max(ks + ret[idx:len(kernel) + idx])
else:
offset = npv[3, ii] - min(ks + ret[idx:len(kernel) + idx])
ret[idx:len(kernel) + idx] += ks + offset
t = np.arange(0.0, 1.0 - 1e-6, 1.0 / sampleRate)
if ax:
el1 = None if label == "Inh" else "Troughs"
el2 = None if label == "Inh" else "Peaks"
ax.plot(npv[0], npv[1], "c*-", label=el1)
ax.plot(npv[2], npv[3], "y.-", label=el2)
return ret[:len(t)]
def plotA(ax, imean, emean):
"""
Plot the mean inhibitory and excitatory synaptic currents in panel A.
Args:
ax (matplotlib.axes.Axes): The axes object for plotting.
imean (numpy.ndarray): The mean inhibitory synaptic current data.
emean (numpy.ndarray): The mean excitatory synaptic current data.
"""
t = np.arange(0.0, runtime - 1e-6, 1.0 / sampleRate)
ax.plot(t, imean, "g-", label="IPSC")
ax.plot(t, emean, "b-", label="EPSC")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Synaptic current (pA)")
ax.legend(loc="upper left", frameon=False)
ax.text(-0.12, 1.05, "A", fontsize=22, weight="bold", transform=ax.transAxes)
def plotB(ax, ideconv, edeconv):
"""
Plot the min-to-max ratio for inhibitory and excitatory deconvolved data in panel B.
Args:
ax (matplotlib.axes.Axes): The axes object for plotting.
ideconv (numpy.ndarray): The inhibitory deconvolved data.
edeconv (numpy.ndarray): The excitatory deconvolved data.
"""
ax.plot(range(len(ideconv)), ideconv / ideconv[0], label="Inh")
ax.plot(range(len(edeconv)), edeconv / edeconv[0], label="Exc")
ax.set_xlabel("Pulse # in burst")
ax.set_ylabel("Min-to-Max ratio")
ax.legend(loc="upper right", frameon=False)
ax.text(-0.24, 1.05, "B", fontsize=22, weight="bold", transform=ax.transAxes)
def plotC(ax, ipv, epv):
"""
Plot the peak-to-trough ratio for inhibitory and excitatory deconvolved data in panel C.
Args:
ax (matplotlib.axes.Axes): The axes object for plotting.
ipv (numpy.ndarray): The inhibitory peak/trough values.
epv (numpy.ndarray): The excitatory peak/trough values.
"""
y = ipv[1] - ipv[3]
ax.plot(range(len(y)), y / y[0], label="Inh")
y = epv[1] - epv[3]
ax.plot(range(len(y)), y / y[0], label="Exc")
ax.set_xlabel("Pulse # in burst")
ax.set_ylabel("Peak-to-Trough ratio")
ax.legend(loc="upper right", frameon=False)
ax.text(-0.24, 1.05, "C", fontsize=22, weight="bold", transform=ax.transAxes)
def plotDE(ax, sqDat, freq, patternList, panelName):
"""
Plot the min-to-max ratio for inhibitory and excitatory deconvolved data in panels D and E.
Args:
ax (matplotlib.axes.Axes): The axes object for plotting.
sqDat (pandas.DataFrame): The input data frame.
freq (float): The stimulus frequency.
patternList (list): The list of pattern IDs.
panelName (str): The panel name ("D" or "E").
"""
numSamples = int(round(sampleRate * runtime))
elabel = "Exc"
ilabel = "Inh"
for pp in patternList:
dataStartColumn = len(sqDat.columns) - 80000 # Was 29, new is 39.
inh = sqDat.loc[
(sqDat['stimFreq'] == freq) & (sqDat['clampPotential'] > -0.05) & (sqDat['patternList'] == pp)]
exc = sqDat.loc[
(sqDat['stimFreq'] == freq) & (sqDat['clampPotential'] < -0.05) & (sqDat['patternList'] == pp)]
yexc = exc.iloc[:, dataStartColumn: numSamples + dataStartColumn]
yinh = inh.iloc[:, dataStartColumn: numSamples + dataStartColumn]
emean = yexc.mean()
imean = yinh.mean()
try:
ii, iSynthPlot, ipv = deconv(imean, freq, None)
ee, eSynthPlot, epv = deconv(emean, freq, None)
except FloatingPointError as error:
print("innerAnalysis: freq = {}, pattern = {}, cellNum = {}".format(freq, pattern, cellNum))
raise
ax.plot(range(len(ii)), ii / ii[0], "g*-", label=ilabel)
ax.plot(range(len(ee)), ee / ee[0], "b*-", label=elabel)
ilabel = None
elabel = None
ax.set_xlabel("Pulse # in burst")
ax.set_ylabel("Min-to-Max ratio")
ax.legend(loc="upper right", frameon=False)
ax.text(-0.24, 1.05, panelName, fontsize=22, weight="bold", transform=ax.transAxes)
def