forked from BackyardBrains/SpikeTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBYBSpikes.py
301 lines (256 loc) · 11.1 KB
/
BYBSpikes.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
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 31 13:50:41 2018
@author: benrobbins
This is a module contains functions to help facilitate the creation of graphs.
Particularly Rasters and PETHs for the Grasshopper data.
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
def plotPETH(session, events,numPointsToSmooth = 3,
binSize = .03, timerange = [-1, 1], makeplot = 1,
barColor = ['r', 'b', 'g', 'y'], linewidth = 1, ylim = None):
"""
Plots a histogram of a PETH
Parameters
----------
session: experiment session
events: A string or an array-like object of strings that represent the
attribute(s) in the session that should be used to
identify when the impacts occured.
numPointsToSmooth: The number of bins that each value should be smoothed
over. Defult is 3.
binSize: The size of each bar along the x-axis in secounds. Defult is
.03 secound.
timerange: An array-like object containing two integers, which represent
the range you want to search for spikes in each trial relative
to the each event. The event timestamp is 0. Defult is [-1,1].
Meassured is secounds.
makeplot: If 1 this will generate graph. If 0 it won't. 1 is defult.
barColor: An array-like object containing strings that represent colors
for the line graph. Defult is is ['r'(red),'b'(blue),
'g'(green),'y'(yellow)].
linewidth: Line width for the line graph. Defult is one.
ylim: If set, will be the top frequency of the graph. Else, it will go to
the defult limit for the data set by matplotlib.hist().
Returns
-------
out: A list of lists or a 3d list, each innermost list representing a trial
and full of all the spikes that were within the range of impact with
the next layer of list(s) representing one element in events.
"""
# """
# This is a calculation for the 95% confidence interval of a spike. It
# assumes that the spike train is a Possison train. It was taken from
# Abeles M. Quantification, smoothing, and confidence limits for single-units
# histograms. Journal of Nueroscience Methodes.
# """
# """
# frequency = 1/np.mean(np.diff(spiketimes))
# c = frequency * binSizenumEvents
#
# if (c >= 30):
# lowerConfidanceLimit = c - 2.58 * (c ** .5)
# higherConfidenceLimit = c + 2.58 * (c ** .5)
#
# else:
# lowerConfidanceLimit = -1
# higherConfidenceLimit = 1
#
# s = 0
#
# for aa in range(51):
# pp = math.exp(-c) * power(c, aa)
#
# fact = math.factorial(aa)
# pp /= fact
# s += pp
#
# if lowerConfidanceLimit == -1 and s >= 0.05:
# lowerConfidanceLimit = aa - 1
#
# if higherConfidenceLimit == -1 and s >= .95:
# higherConfidenceLimit = aa
#
# higherConfidenceLimit = higherConfidenceLimit / binSize / numEvents
# lowerConfidanceLimit = lowerConfidanceLimit / binSize / numEvents
#
# out.ci.high = higherConfidenceLimit
# out.ci.low = lowerConfidanceLimit
# out.m = frequency
# """
try:
allS = []
out = []
for i in range(len(events)):
singleListOfEvents = eval("session.timestamps." + events[i])
trials = session.trials
sS = []
o =[]
for time in singleListOfEvents:
for index in range(len(trials)):
s = []
if abs(trials[index].timeOfImpact - (time - (index * 45000))) < .00001:
for spike in trials[index].spikeTimestamps:
if (spike - (time - (index * 45000))) >= (timerange[0]- (numPointsToSmooth - 1)//2 * binSize) and ((spike - (time - (index * 45000))) <= (timerange[1]+ (numPointsToSmooth - 1)//2 * binSize) ):
s.append(spike - (time - (index * 45000)))
o.append(s)
sS += s
out.append(o)
allS.append(sS)
bins = abs(np.diff(timerange)[0])/ binSize
if (bins % 1) != 0:
bins = int(bins) + 1
else:
bins = int(bins)
if makeplot:
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
values = []
times = []
for i in range(len(events)):
value, time, throughAway = ax1.hist(allS[i], bins = bins,
range=timerange)
value = list(value)
value.append(value[-1])
values.append(value)
times.append(time)
ax1.clear()
for i in range(len(events)):
times[i] = times[i][(numPointsToSmooth - (numPointsToSmooth - 1)//2 - 1):(len(times[i]) - (numPointsToSmooth - 1)//2)]
ax1.plot(times[i], smooth(values[i], numPointsToSmooth), drawstyle = 'steps-post',
color = barColor[i], linewidth = linewidth)
ax1.set_ylim(bottom = 0)
if (ylim != None):
ax1.set_ylim(top = ylim * (binSize * len(eval('session.timestamps.' + events[0]))))
bottomLim, topLim = ax1.get_ylim()
top = topLim
topLim = topLim/(binSize * len(eval('session.timestamps.' + events[0])))
labels = []
locs = []
for i in range(5):
labels.append('%d' % (bottomLim + (((topLim - bottomLim)/4) * i)))
locs.append(bottomLim + (((top - bottomLim)/4) * i))
plt.yticks(locs, labels)
plt.ylabel('Firing Rate (Hz)')
plt.title(session.session)
plt.xlabel('Time (Secounds with impact at 0)')
except:
singleListOfEvents = eval("session.timestamps." + events)
trials = session.trials
for time in singleListOfEvents:
for index in range(len(trials)):
s = []
if abs(trials[index].timeOfImpact - (time - (index * 45000))) < .00001:
for spike in trials[index].spikeTimestamps:
if (spike - (time - (index * 45000))) >= (timerange[0]- (numPointsToSmooth - 1)//2 * binSize) and ((spike - (time - (index * 45000))) <= (timerange[1]+ (numPointsToSmooth - 1)//2 * binSize) ):
s.append(spike - (time - (index * 45000)))
out.append(s)
allS += s
bins = abs(np.diff(timerange)[0])/ binSize
if (bins % 1) != 0:
bins = int(bins) + 1
else:
bins = int(bins)
if makeplot:
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 3])
ax1 = plt.subplot(gs[0])
value, time, throughAway = ax1.hist(smooth(allS, numPointsToSmooth), bins = bins, color = 'k', range=timerange)
value = list(value)
value.append(value[-1])
ax1.clear()
ax1.bar(range(len(value)), value, color = 'k', width = 1)
if (ylim != None):
ax1.set_ylim(top = ylim * (binSize * len(eval('session.timestamps.' + events))))
bottomLim, topLim = ax1.get_ylim()
top = topLim
bottomLim = bottomLim/(binSize * len(eval('session.timestamps.' + events)))
topLim = topLim/(binSize * len(eval('session.timestamps.' + events)))
labels = []
locs = []
for i in range(5):
labels.append('%d' % (bottomLim + (((topLim - bottomLim)/4) * i)))
locs.append(bottomLim + (((top - bottomLim)/4) * i))
plt.yticks(locs, labels)
plt.ylabel('Firing Rate (Hz)')
plt.title(session.session)
return out
def plotRaster(session, event, makeplot = 1, timerange = [-1,1]):
"""
Creates a raster plot.
Parameters
----------
session : A session object
event: A string that represents the attribute in the session that the
function should use to identify when the impacts occured.
makeplot: If 1 then a plot will be generated, if 0 then no plot will be
generated. Defult is 1.
timerange: An array-like object containing two integers, which represent
the range you want to search for spikes in each trial relative
to the each event. The event timestamp is 0. Defult is [-1,1].
Returns
-------
ax : an axis containing the raster plot
"""
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
allS = []
out = []
singleListOfEvents = eval("session.timestamps." + event)
trials = session.trials
for time in singleListOfEvents:
for index in range(len(trials)):
s = []
if abs(trials[index].timeOfImpact - (time - (index * 45000))) < .00001:
for spike in trials[index].spikeTimestamps:
if (spike - (time - (index * 45000))) >= timerange[0] and (spike - (time - (index * 45000))) <= timerange[1]:
s.append(spike - (time - (index * 45000)))
out.append(s)
allS += s
if makeplot:
raster(out)
plt.xlabel('Time (Secounds with impact at 0)')
plt.ylabel('Trial')
plt.subplots_adjust(hspace = 0.05)
fig.show()
return ax
def raster(event_times_list, **kwargs): #pulled from the internet
"""
Creates a raster plot
Parameters
----------
event_times_list : iterable
a list of event time iterables
color : string
color of vlines
Returns
-------
ax : an axis containing the raster plot
"""
ax = plt.gca()
for ith, trial in enumerate(event_times_list):
plt.vlines(trial, ith + .5, ith + 1.3)
plt.ylim(.5, len(event_times_list) + .5)
ax.invert_yaxis()
return ax
def smooth(mylist, smoothInt):
"""
Mimics the smooth method in matlab
Parameters
----------
mylist: A list of numbers to be smoothed
smoothInt: The span of the moving average to span. It must be odd.
Returns
-------
newlist: A list of the smoothed out floats.
"""
newlist = []
for i in range((smoothInt - (smoothInt - 1)//2 - 1),(len(mylist) - (smoothInt - 1)//2)):
runningSum = 0
for index in range(smoothInt):
runningSum += mylist[index + (i - (((smoothInt - 1)//2)))]
k = runningSum / smoothInt
newlist.append(k)
return newlist