forked from glarange/Theoretical-Neuroscience-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch1 Exercises.py
726 lines (565 loc) · 26.8 KB
/
Ch1 Exercises.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#Jordan Breffle
#https://github.com/jtbreffle
#Theoretical Neuroscience Exercises, Dayan and Abbott, 2001
#Chapter 1: Neural Encoding I: Firing Rates and Spike Statistics
import scipy.io #use scipy.io.loadmat() for MAT files
import numpy as np #if loadmat() fails, then use numpy.loadtxt(), becuase it is a plain text file
import random
import matplotlib.pyplot as plt
# ------------------Helper Functions--------------------------------------
def spikeCountWindow(input, window):
'''for an input array of spikes, this returns an array of the number of
spikes that occur within each window '''
op = []
spikeCounter = 0
windowCounter = 0
for i in range(len(input)):
if windowCounter == window:
op = op + [spikeCounter]
spikeCounter = 0
windowCounter = 0
else:
windowCounter = windowCounter + 1
if input[i] > 0:
spikeCounter = spikeCounter + 1
return op
def pairedSTA(SSI, data): #interval between two spikes in 2x ms\
'''
returns the spike triggered average of pairs of spikes seperated by SSI (second spike interval)
data contains rho (list of when spikes occured) and stim (intensity of stimulus)
'''
#list of preceding stims that led to spikes
allSTs = []
for i in range(len(data["rho"])):
#break when i wili exceed length to find second spike
if i > (len(data["rho"]) + SSI):
break
#for first 300ms, cannot compute 300ms STA of any spikes that occur
elif i < 150:
pass
#if a spike occured at time point i AND a spike occures at time point i+SSI, add the preceding 300ms of stim data as a new list to the list allSTs
elif data["rho"][i] == 1 and data["rho"][i+SSI] == 1:
allSTs = allSTs + [data["stim"][(i-150+SSI):(i+SSI)]]
#if no spike occured at time point i, do nothing
else:
pass
#break prematurely for quick debugging
#if i == 1000000:
#break
#allSTs is now a list of lists, where each list is the stim that lead to a single spike
STA = []
for i in range(len(allSTs[0])):
sum = 0
for j in range(len(allSTs)):
sum = sum + allSTs[j][i]
STA = STA + [float (sum / len(allSTs) )]
return STA
def extraSpike(i, SSI, data):
#increment through rho from i to i+SSI
j = i+1
while j < (i + SSI):
#if any spikes occur between i and i+SSI, return true
if data["rho"][j] == 1:
return True
j = j + 1
#if no spikes are found between i and i+SSI, return false
return False
def pairedSTAExcl(SSI, data): #interval between two spikes in 2x ms\
'''
returns the spike triggered average of pairs of spikes seperated by SSI (second spike interval)
data contains rho (list of when spikes occured) and stim (intensity of stimulus)
same as pairedSTA(), except no extra spikes can occur between the two spikes of interest
'''
#list of preceding stims that led to spikes
allSTs = []
for i in range(len(data["rho"])):
#break when i wili exceed length to find second spike
if i > (len(data["rho"]) + SSI):
break
#for first 300ms, cannot compute 300ms STA of any spikes that occur
elif i < 150:
pass
#if a spike occured at time point i AND a spike occures at time point i+SSI AND there are no spikes between i and i+SSI, add the preceding 300ms of stim data as a new list to the list allSTs
elif data["rho"][i] == 1 and\
data["rho"][i+SSI] == 1 and \
not extraSpike(i, SSI, data):
allSTs = allSTs + [data["stim"][(i-150+SSI):(i+SSI)]]
#if no spike occured at time point i, do nothing
else:
pass
#break prematurely for quick debugging
if i == 50000:
break
#allSTs is now a list of lists, where each list is the stim that lead to a single spike
STA = []
for i in range(len(allSTs[0])):
sum = 0
for j in range(len(allSTs)):
sum = sum + allSTs[j][i]
STA = STA + [float (sum / len(allSTs) )]
return STA
#----------------------------Exercises------------------------------------
def Ex1():
'''Generate spikes for 10 s (or longer if you want better statistics) using
a Poisson spike generator with a constant rate of 100 Hz, and record
their times of occurrence. Compute the coeÆcient of variation of the
interspike intervals, and the Fano factor for spike counts obtained
over counting intervals ranging from 1 to 100 ms. Plot the interspike
interval histogram. '''
#10s with points every 1ms
x = np.linspace(0, 10, 1001)
#samples drawn from a poisson distribution every 1ms for 10s, with probability corresponding to 100Hz
y = np.random.poisson(0.1, 1001)
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(y)):
if y[i] > 0:
y[i] = 1
plt.figure(num='Exercise 1')
plt.subplot(2, 1, 1)
plt.title("Poisson Generated Spikes, 100Hz")
plt.ylabel('Spikes')
plt.xlabel('Time (S)')
plt.plot(x, y)
#list of all interspike-intervals of y
ISI = []
#current ISI in ms
counter = 0
for i in range(len(y)):
#if a spike does not occure at y[i], increment the counter and look for the next spike
if y[i] == 0:
counter = counter + 1
#if y[i] is a spike, add the counter to the list of ISIs and start counting the next ISI
else:
ISI = ISI + [counter]
counter = 1 #set to 1 because if there are two concurent spikes then the ISI is 1ms
#coefficient of Variation of ISIs
coefVar = ( np.std(ISI) / np.mean(ISI)) * 100
spikes5ms = spikeCountWindow(y, window=5)
#Fano Factor for number of spikes in 20ms windows
fanoFactor5ms = (np.var(spikes5ms) / np.mean(spikes5ms) )
spikes40ms = spikeCountWindow(y, window=40)
#Fano Factor for number of spikes in 20ms windows
fanoFactor40ms = (np.var(spikes40ms) / np.mean(spikes40ms) )
spikes100ms = spikeCountWindow(y, window=100)
#Fano Factor for number of spikes in 20ms windows
fanoFactor100ms = (np.var(spikes100ms) / np.mean(spikes100ms) )
plt.subplot(2, 1, 2)
plt.hist(ISI)
plt.title("Interspike Intervals")
plt.xlabel("ISI (ms)\n \
Coefficient of Variation of ISIs:" + str(np.round(coefVar)) \
+ "\n Fano Factor for 5ms Windows: " + str(fanoFactor5ms) \
+ "\n Fano Factor for 40ms Windows: " + str(fanoFactor40ms)\
+ "\n Fano Factor for 100ms Windows: " + str(fanoFactor100ms))
plt.ylabel("Frequency")
plt.tight_layout()
plt.show()
def Ex2():
'''
Add a refractory period to the Poisson spike generator by allowing
the firing rate to depend on time. Initially, set the firing rate to a
constant value, r(t) = r0. After every spike, set r(t) to 0, and then
allow it to recover exponentially back to r0 with a time constant TauRef
that controls the refractory recovery rate. In other words, have r(t)
obey the equation
TauRef(dr/dt) =r0 - r
except immediately after a spike, when it is set to 0. Plot the coeÆ-
cient of variation as a function of TauRef over the range 1 ms <= TauRef <= 20 ms, and plot interspike interval histograms for a few diferent values of TauRef in this range. Compute the Fano factor for spike counts obtained over counting intervals ranging from 1 to 100 ms for the case TauRef =10 ms.
'''
#10s with points every 1ms
x = np.linspace(0, 10, 1001)
#samples drawn from a poisson distribution every 1ms for 10s, with probability reseting and then increasing exponentially after each spike
y = []
prob = 0.1
#probability of a spike is set to 100Hz initially, if a spike occurs it is reset to 0, each ms that passes without a spike increases the probability of a spike by 1Hz
for i in range(len(x)):
y = y + [np.random.poisson(prob, 1)]
if y[i] > 0:
prob = 0
else:
prob = prob + 0.01
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(y)):
if y[i] > 0:
y[i] = 1
plt.figure(num='Exercise 2')
plt.subplot(2, 1, 1)
plt.title("Poisson Generated Spikes with Refractory Period \n (Spike frequency increases by 1Hz per ms that passes without a spike)")
plt.ylabel('Spikes')
plt.xlabel('Time (S)')
plt.plot(x, y)
#list of all interspike-intervals of y
ISI = []
#current ISI in ms
counter = 0
for i in range(len(y)):
#if a spike does not occure at y[i], increment the counter and look for the next spike
if y[i] == 0:
counter = counter + 1
#if y[i] is a spike, add the counter to the list of ISIs and start counting the next ISI
else:
ISI = ISI + [counter]
counter = 1 #set to 1 because if there are two concurent spikes then the ISI is 1ms
#coefficient of Variation of ISIs
coefVar = ( np.std(ISI) / np.mean(ISI)) * 100
spikes5ms = spikeCountWindow(y, window=5)
#Fano Factor for number of spikes in 20ms windows
fanoFactor5ms = (np.var(spikes5ms) / np.mean(spikes5ms) )
spikes40ms = spikeCountWindow(y, window=40)
#Fano Factor for number of spikes in 20ms windows
fanoFactor40ms = (np.var(spikes40ms) / np.mean(spikes40ms) )
spikes100ms = spikeCountWindow(y, window=100)
#Fano Factor for number of spikes in 20ms windows
fanoFactor100ms = (np.var(spikes100ms) / np.mean(spikes100ms) )
plt.subplot(2, 1, 2)
plt.hist(ISI)
plt.title("Interspike Intervals")
plt.xlabel("ISI (ms)\n \
Coefficient of Variation of ISIs:" + str(np.round(coefVar)) \
+ "\n Fano Factor for 5ms Windows: " + str(fanoFactor5ms) \
+ "\n Fano Factor for 40ms Windows: " + str(fanoFactor40ms)\
+ "\n Fano Factor for 100ms Windows: " + str(fanoFactor100ms))
plt.ylabel("Frequency")
plt.tight_layout()
plt.show()
def Ex3():
'''
Compute autocorrelation histograms of spike trains generated by
1) a Poisson generator with a constant firing rate of 100 Hz
2) a constant firing rate of 100 Hz together with a refractory period modeled as in exercise 2 with Tauref = 10 ms
3) a variable firing rate r(t) = 100(1 + cos(2PIt/25 ms)) Hz.
Plot the histograms over a range from 0 to 100 ms.
'''
#10s with points every 1ms
x = np.linspace(0, 10, 1001)
#samples drawn from a poisson distribution every 1ms for 10s, with probability corresponding to 100Hz
yNoRefPer = np.random.poisson(0.1, 1001)
#samples drawn from a poisson distribution every 1ms for 10s, with probability reseting and then increasing exponentially after each spike
yRefPer = []
prob = 0.1
#probability of a spike is set to 100Hz initially, if a spike occurs it is reset to 0, each ms that passes without a spike increases the probability of a spike by 1Hz
for i in range(len(x)):
yRefPer = yRefPer + [np.random.poisson(prob, 1)]
if yRefPer[i] > 0:
prob = 0
else:
prob = prob + 0.01
#variable firing rate r(t) = 100(1 + cos(2PIt/25 ms))
yVarFir = []
for i in range(len(x)):
t = i / 2 #time t in ms
prob = 100 * (1 + np.cos(2 * np.pi * (t / 25) ) )
yVarFir = yVarFir + [np.random.poisson(prob, 1)]
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(yNoRefPer)):
if yNoRefPer[i] > 0:
yNoRefPer[i] = 1
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(yRefPer)):
if yRefPer[i] > 0:
yRefPer[i] = 1
else:
yRefPer[i] = 0
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(yVarFir)):
if yVarFir[i] > 0:
yVarFir[i] = 1
plt.figure(num='Exercise 3')
plt.subplot(3, 1, 1)
plt.title("Poisson Generated Spikes, 100Hz")
plt.ylabel('Spikes')
plt.xlabel('Time (S)')
plt.plot(x, yNoRefPer)
plt.subplot(3, 1, 2)
plt.title("Poisson Generated Spikes with Refractory Period \n (Spike frequency increases by 1Hz per ms that passes without a spike)")
plt.ylabel('Spikes')
plt.xlabel('Time (S)')
plt.plot(x, yRefPer)
plt.subplot(3, 1, 3)
#plt.hist(ISI)
plt.title("Variable fire rate")
plt.ylabel('Spikes')
plt.xlabel('Time (S)')
plt.plot(x, yVarFir)
plt.tight_layout()
plt.show()
def Ex4():
'''
Generate a Poisson spike train with a time-dependent firing rate
r(t) = 100(1+ cos(2PIt/300 ms)) Hz. Approximate the firing rate from
this spike train using a variable rapprox that satisfies
Tauapprox * (drapprox / dt) = -rapprox ,
except that rapprox -> rapprox + 1/Tauapprox every time a spike occurs.
*Make plots of the true rate, the spike sequence generated, and the
estimated rate.
*Experiment with a few different values of Tauapprox in the range of 1 to
100 ms.
*Determine the best value of Tauapprox by computing the average squared
error of the estimate, INTEGRAL: dt(r(t) - rapprox(t))2, for different
values of Tauapprox, and finding the value of Tauapprox that minimizes
this error.
'''
#10s with points every 1ms
x = np.linspace(0, 10, 1001)
#samples drawn from a poisson distribution every 1ms for 10s, with probability corresponding to 100Hz
yNoRefPer = np.random.poisson(0.1, 1001)
#samples drawn from a poisson distribution every 1ms for 10s, with probability reseting and then increasing exponentially after each spike
yRefPer = []
prob = 0.1
#probability of a spike is set to 100Hz initially, if a spike occurs it is reset to 0, each ms that passes without a spike increases the probability of a spike by 1Hz
for i in range(len(x)):
yRefPer = yRefPer + [np.random.poisson(prob, 1)]
if yRefPer[i] > 0:
prob = 0
else:
prob = prob + 0.01
#variable firing rate r(t) = 100(1+ cos(2PIt/300 ms))
yVarFir = []
for i in range(len(x)):
t = i / 2 #time t in ms
prob = 100 * (1 + np.cos(2 * np.pi * (t / 300) ) )
yVarFir = yVarFir + [np.random.poisson(prob, 1)]
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(yNoRefPer)):
if yNoRefPer[i] > 0:
yNoRefPer[i] = 1
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(yRefPer)):
if yRefPer[i] > 0:
yRefPer[i] = 1
else:
yRefPer[i] = 0
#a single ms timepoint can have >1 event so...
#this for-loop rounds all events >1 to a single spike
for i in range(len(yVarFir)):
if yVarFir[i] > 0:
yVarFir[i] = 1
plt.figure(num='Exercise 4')
plt.subplot(3, 1, 1)
#plt.hist(ISI)
plt.title("Variable fire rate")
plt.ylabel('Spikes')
plt.xlabel('Time (S)')
plt.plot(x, yVarFir)
#plt.subplot(3, 1, 2)
#plt.title("Poisson Generated Spikes, 100Hz")
#plt.ylabel('Spikes')
#plt.xlabel('Time (S)')
#plt.plot(x, yNoRefPer)
#plt.subplot(3, 1, 3)
#plt.title("Poisson Generated Spikes with Refractory Period \n (Spike frequency increases by 1Hz per ms that passes without a spike)")
#plt.ylabel('Spikes')
#plt.xlabel('Time (S)')
#plt.plot(x, yRefPer)
plt.tight_layout()
plt.show()
def Ex5():
'''
For a constant rate Poisson process, every specific (up to a finite
resolution) sequence of N spikes occurring over a given time interval
is equally likely. This seems paradoxical because we certainly do not
expect to see all N spikes appearing within the first 1% of the time
interval. Resolve this paradox.
'''
print ( "\
We do not expect to see all N spikes appearing within the first 1% of the\
\n time interval because the set of all possible sequences of N spikes \
\n occurring within the first 1% of the time interval is a very small \
\n subset of the total possible sequences of N spikes occuring withing the \
\n first 100% of the time interval. \
\n Although any give sequence from either set is equally likely, the \
\n first set has many fewer possible members and therefore the chance of \
\n some individual member of the set occuring is very small.\
")
#TODO: what is the exact probability?
def Ex6():
'''
Build an approximatewhite-noise stimulus by choosing randomvalues
at discrete times separated by a time-step interval deltat. Plot its
autocorrelation function and power spectrum(use theMATLAB® function
spectrum or psd). Discuss how well this stimulus matches an
ideal white-noise stimulus given the value of deltat you used.
'''
def Ex7():
'''
Consider a modelwith a firing rate determined in terms of a stimulus
s(t) by integrating the equation
Taur (drest(t) / dt) = [r0 + s]+ - rest(t),
where r0 is a constant that determines the background firing rate and
Taur = 20 ms. Drive the model with an approximate white-noise stimulus.
Adjust the amplitude of the white-noise and the parameter r0
so that rectification is not a big effect (i.e. r0 + s > 0 most of the time).
Fromthe responses of themodel, compute the stimulus-response correlation
function, Qrs. Next, generate spikes from this model using a
Poisson generatorwith a rate rest(t), and compute the spike-triggered
average stimulus from the spike trains produced by the white-noise
stimulus. By comparing the stimulus-response correlation function
with the spike-triggered average, verify that equation 1.22 is satisfied.
Examine what happens if you set r0 = 0, so that the white-noise
stimulus becomes half-wave rectified.
'''
def Ex8(data):
'''
MATLAB® file c1p8.mat contains data collected and provided by Rob
de Ruyter van Steveninck from a fly H1 neuron responding to an approximate
white-noise visual motion stimulus. Data were collected
for 20minutes at a sampling rate of 500Hz. In the file, rho is a vector
that gives the sequence of spiking events or nonevents at the sampled
times (every 2 ms). When an element of rho is one, this indicates the
presence of a spike at the corresponding time, whereas a zero value
indicates no spike. The variable stim gives the sequence of stimulus
values at the sampled times.
**Calculate and plot the spike-triggered average from these data over the range from 0 to 300 ms (150 time steps). (Based on a problem from Sebastian Seung.)**
'''
#linear space 300ms before a spike, point every 2 ms
x = np.linspace(-298, 0, 150)
#list of preceding stims that led to spikes
allSTs = []
for i in range(len(data["rho"])):
#for first 300ms, cannot compute 300ms STA of any spikes that occur
if i < 150:
pass
#if a spike occured at time point i, add the preceding 300ms of stim data as a new list to the list allSTs
elif data["rho"][i] == 1:
allSTs = allSTs + [data["stim"][(i-150):i]]
#if no spike occured at time point i, do nothing
else:
pass
#break prematurely for quick debugging
#if i == 5000:
#break
#allSTs is now a list of lists, where each list is the stim that lead to a single spike
STA = []
for i in range(len(allSTs[0])):
sum = 0
for j in range(len(allSTs)):
sum = sum + allSTs[j][i]
STA = STA + [float (sum / len(allSTs) )]
plt.figure(num='Exercise 8')
plt.plot(x, STA)
plt.title("Spike-Triggered Average of a Fly H1 Neuron")
plt.xlabel("Time (ms)")
plt.ylabel("Stimulus")
plt.tight_layout()
plt.show()
def Ex9(data):
'''
Using the data of problem 8, calculate and plot stimulus averages
triggered on events consisting of a pair of spikes (which need not necessarily
be adjacent) separated by a given interval (as in figure 1.10).
1) Plot these two-spike-triggered average stimuli for various separation
intervals ranging from 2 to 100 ms. (Hint: in MATLAB® , use convolution
for pattern matching: e.g. find(conv(rho,[1 0 1])==2) will
contain the indices of all the events with two spikes separated by 4
ms.)
2) Plot, as a function of the separation between the two spikes,
the magnitude of the difference between the two-spike-triggered average
and the sum of two single-spike-triggered averages (obtained
in exercise 8) separated by the same time interval.
3)At what temporal separation does this difference become negligibly small. (Based on a problem from Sebastian Seung.)
'''
#linear space 300ms before a spike, point every 2 ms
x = np.linspace(-298, 0, 150)
ssi2ms = pairedSTA(SSI=1, data=data) #STA for spikes sepearted by 2ms
ssi4ms = pairedSTA(SSI=2, data=data) #STA for spikes sepearted by 4ms
ssi6ms = pairedSTA(SSI=3, data=data) #STA for spikes sepearted by 6ms
ssi10ms = pairedSTA(SSI=5, data=data) #STA for spikes sepearted by 10ms
ssi50ms = pairedSTA(SSI=25, data=data) #STA for spikes sepearted by 50ms
ssi100ms = pairedSTA(SSI=50, data=data) #STA for spikes sepearted by 100ms
STAMax = 29.472907029165032 #maximum stim amplitude for single spike STA from exercise 8
plt.figure(num='Exercise 9')
plt.subplot(2, 1, 1)
plt.plot(x, ssi100ms, label="100ms")
plt.plot(x, ssi50ms, label="50ms")
plt.plot(x, ssi10ms, label="10ms")
plt.plot(x, ssi6ms, label="6ms")
plt.plot(x, ssi4ms, label="4ms")
plt.plot(x, ssi2ms, label="2ms")
plt.title("STA's for pairs of spikes")
plt.xlabel("Time (ms)")
plt.ylabel("Stimulus")
plt.legend(loc='best', ncol=2, fancybox=True, shadow=True)
plt.subplot(2, 1, 2)
plt.xlabel("Interval between spikes")
plt.ylabel("Peak stim - single spike peak")
plt.plot(2, (max(ssi2ms) - (STAMax)), 'bo')
plt.plot(50, (max(ssi50ms) - (STAMax)), 'bo')
plt.plot(10, (max(ssi10ms) - (STAMax)), 'bo')
plt.plot(100, (max(ssi100ms) - (STAMax)), 'bo')
plt.plot(4, (max(ssi4ms) - (STAMax)), 'bo')
plt.plot(6, (max(ssi6ms) - (STAMax)), 'bo')
plt.tight_layout()
plt.show()
def Ex10(data):
'''
Using the data of problem 8, find the spike-triggered average stimulus
for events that contain exactly two adjacent spikes separated by
various different intervals ranging from 2 to 100 ms (e.g. for 4 ms,
the event [1 0 1] but not the event [1 1 1]). This is distinct from
exercise 9 in which we only required two spikes separated by a given
interval, but did not restrict what happened between the two spikes.
Compare results of the exclusive case considered here with those of
the inclusive two-spike-triggered average computed in exercise 9. In
what ways and why are they different? (Based on a problem from
Sebastian Seung.)
'''
#linear space 300ms before a spike, point every 2 ms
x = np.linspace(-298, 0, 150)
ssi2ms = pairedSTAExcl(SSI=1, data=data) #STA for spikes sepearted by 2ms
ssi4ms = pairedSTAExcl(SSI=2, data=data) #STA for spikes sepearted by 4ms
ssi6ms = pairedSTAExcl(SSI=3, data=data) #STA for spikes sepearted by 6ms
ssi10ms = pairedSTAExcl(SSI=5, data=data) #STA for spikes sepearted by 10ms
ssi50ms = pairedSTAExcl(SSI=25, data=data) #STA for spikes sepearted by 50ms
ssi100ms = pairedSTAExcl(SSI=50, data=data) #STA for spikes sepearted by 100ms
STAMax = 29.472907029165032 #maximum stim amplitude for single spike STA from exercise 8
plt.figure(num='Exercise 10')
plt.subplot(2, 1, 1)
plt.plot(x, ssi100ms, label="100ms")
plt.plot(x, ssi50ms, label="50ms")
plt.plot(x, ssi10ms, label="10ms")
plt.plot(x, ssi6ms, label="6ms")
plt.plot(x, ssi4ms, label="4ms")
plt.plot(x, ssi2ms, label="2ms")
plt.title("STA's for pairs of spikes, no between-spike spikes")
plt.xlabel("Time (ms)")
plt.ylabel("Stimulus")
plt.legend(loc='best', ncol=3, fancybox=True, shadow=True, prop={'size': 11})
plt.subplot(2, 1, 2)
plt.xlabel("Interval between spikes")
plt.ylabel("Peak stim - single spike peak")
plt.plot(2, (max(ssi2ms) - (STAMax)), 'bo')
plt.plot(50, (max(ssi50ms) - (STAMax)), 'bo')
plt.plot(10, (max(ssi10ms) - (STAMax)), 'bo')
plt.plot(100, (max(ssi100ms) - (STAMax)), 'bo')
plt.plot(4, (max(ssi4ms) - (STAMax)), 'bo')
plt.plot(6, (max(ssi6ms) - (STAMax)), 'bo')
plt.tight_layout()
plt.show()
def main():
#loads data
c1p8 = scipy.io.loadmat(r'exercises\c1\data\c1p8.MAT')
c10p1 = np.loadtxt(r'exercises\c1\data\c10p1.MAT')
#Visualizes data parameters
#for key in matc1p8:
#print (key, ": ", len(matc1p8[key])
#print (len(matc10p1))
#execute exercises here
#Ex1()
#Ex2()
#Ex3() #not finished
#Ex4() #not finished
#Ex5() #not finished
#Ex6() #not finished
#Ex7() #not finished
#Ex8(data = c1p8)
#Ex9(data = c1p8)
#Ex10(data = c1p8)
if __name__ == "__main__":
main()