-
Notifications
You must be signed in to change notification settings - Fork 4
/
PlotPrediction.py
executable file
·1082 lines (933 loc) · 54.4 KB
/
PlotPrediction.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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import matplotlib
matplotlib.use('Agg')
import pylab
import numpy as np
import simulation_parameters
import utils
from matplotlib import cm
class PlotPrediction(object):
def __init__(self, params=None, data_fn=None):
if params == None:
self.network_params = simulation_parameters.parameter_storage() # network_params class containing the simulation parameters
self.params = self.network_params.load_params() # params stores cell numbers, etc as a dictionary
else:
self.params = params
self.no_spikes = False
self.n_fig_x = 2
self.n_fig_y = 2
# self.fig_size = (11.69, 8.27) #A4
self.fig_size = (14, 10)
if self.params['t_blank'] == 0:
self.show_blank = False
else:
self.show_blank = True
self.spiketimes_loaded = False
self.data_to_store = {}
# define parameters
self.time_binsize = int(round(self.params['t_sim'] / 20))
self.time_binsize = 25# [ms]
self.trace_length = 4 * self.time_binsize # [ms] window length for moving average
self.n_bins = int((self.params['t_sim'] / self.time_binsize) )
self.time_bins = [self.time_binsize * i for i in xrange(self.n_bins)]
self.t_axis = np.arange(0, self.n_bins * self.time_binsize, self.time_binsize)
self.n_vx_bins, self.n_vy_bins = 30, 30 # colormap grid dimensions for predicted direction
self.n_x_bins, self.n_y_bins = 50, 50 # colormap grid dimensions for predicted position
self.t_ticks = np.linspace(0, self.params['t_sim'], 6)
self.tuning_prop = np.loadtxt(self.params['tuning_prop_means_fn'])
self.n_cells = self.tuning_prop[:, 0].size #self.params['n_exc']
# create data structures
self.nspikes = np.zeros(self.n_cells) # summed activity
self.nspikes_binned = np.zeros((self.n_cells, self.n_bins)) # binned activity over time
self.nspikes_binned_normalized = np.zeros((self.n_cells, self.n_bins)) # normalized so that for each bin, the sum of the population activity = 1
self.nspikes_normalized = np.zeros(self.n_cells) # activity normalized, so that sum = 1
self.spiketrains = [[] for i in xrange(self.n_cells)]
# sort the cells by their tuning vx, vy properties
# vx
self.vx_tuning = self.tuning_prop[:, 2].copy()
self.vx_tuning.sort()
self.sorted_indices_vx = self.tuning_prop[:, 2].argsort()
self.vx_min, self.vx_max = .7 * self.tuning_prop[:, 2].min(), .7 * self.tuning_prop[:, 2].max()
# maximal range of vx_speeds
# self.vx_min, self.vx_max = np.min(self.vx_tuning), np.max(self.vx_tuning)
self.vx_grid = np.linspace(self.vx_min, self.vx_max, self.n_vx_bins, endpoint=True)
#self.vx_grid = np.linspace(np.min(self.vx_tuning), np.max(self.vx_tuning), self.n_vx_bins, endpoint=True)
# vy
self.vy_tuning = self.tuning_prop[:, 3].copy()
self.vy_tuning.sort()
self.sorted_indices_vy = self.tuning_prop[:, 3].argsort()
# self.vy_min, self.vy_max = -0.5, 0.5
self.vy_min, self.vy_max = self.tuning_prop[:, 3].min(), self.tuning_prop[:, 3].max()
# self.vy_min, self.vy_max = np.min(self.vy_tuning), np.max(self.vy_tuning)
self.vy_grid = np.linspace(self.vy_min, self.vy_max, self.n_vy_bins, endpoint=True)
# x
self.sorted_indices_x = self.tuning_prop[:, 0].argsort()
self.x_tuning = self.tuning_prop[:, 0].copy()
self.x_tuning.sort()
self.x_min, self.x_max = .0, self.params['torus_width']
self.x_grid = np.linspace(self.x_min, self.x_max, self.n_x_bins, endpoint=True)
# y
self.y_tuning = self.tuning_prop[:, 1].copy()
self.y_tuning.sort()
self.sorted_indices_y = self.tuning_prop[:, 1].argsort()
self.y_min, self.y_max = .0, self.params['torus_height']
self.y_grid = np.linspace(self.y_min, self.y_max, self.n_y_bins, endpoint=True)
self.normalize_spiketimes(data_fn)
if self.no_spikes:
return
fig_width_pt = 800.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (np.sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
params = {#'backend': 'png',
'title.fontsize': 32,
'axes.labelsize': 32,
'legend.fontsize': 14,
# 'text.fontsize': 10,
# 'xtick.labelsize': 8,
# 'ytick.labelsize': 8,
# 'text.usetex': True,
'figure.figsize': fig_size}
pylab.rcParams.update(params)
def normalize_spiketimes(self, fn=None):
"""
Fills the following arrays with data:
self.nspikes = np.zeros(self.n_cells) # summed activity
self.nspikes_binned = np.zeros((self.n_cells, self.n_bins)) # binned activity over time
self.nspikes_binned_normalized = np.zeros((self.n_cells, self.n_bins)) # normalized so that for each bin, the sum of the population activity = 1
self.nspikes_normalized = np.zeros(self.n_cells) # activity normalized, so that sum = 1
self.nspikes_normalized_nonlinear
"""
print(' Loading data from %s ...' % fn)
try:
d = np.loadtxt(fn)
for i in xrange(d[:, 0].size):
self.spiketrains[int(d[i, 1])].append(d[i, 0])
except:
print 'WARNING: no spikes found in:', fn
self.no_spikes = True
return
for gid in xrange(self.n_cells):
# for gid in xrange(self.params['n_exc']):
# spiketimes = spiketrains[gid+1.].spike_times
# nspikes = spiketimes.size
nspikes = len(self.spiketrains[gid])
if (nspikes > 0):
count, bins = np.histogram(self.spiketrains[gid], bins=self.n_bins, range=(0, self.params['t_sim']))
self.nspikes_binned[gid, :] = count
self.nspikes[gid] = nspikes
# normalization
for i in xrange(int(self.n_bins)):
if (self.nspikes_binned[:, i].sum() > 0):
self.nspikes_binned_normalized[:, i] = self.nspikes_binned[:, i] / self.nspikes_binned[:,i].sum()
self.nspikes_normalized = self.nspikes / self.nspikes.sum()
# activity normalized, nonlinear
nspikes_shifted = self.nspikes - self.nspikes.max()
nspikes_exp = np.exp(nspikes_shifted)
self.nspikes_normalized_nonlinear = nspikes_exp / nspikes_exp.sum()
def bin_estimates(self, grid_edges, index=2):
"""
Bring the speed estimates from the neuronal level to broader representation in a grid:
index = index in tuning_parameters for the parameter (vx=2, vy=3)
^
vx_binned |
|
+------>
time_bins
"""
# torus dimensions
w, h = self.params['torus_width'], self.params['torus_height']
output_data = np.zeros((len(grid_edges), self.n_bins))
for gid in xrange(self.n_cells):
xyuv_predicted = self.tuning_prop[gid, index] # cell tuning properties
if (index == 0):
# xyuv_predicted += self.tuning_prop[gid, 2]
xyuv_predicted = xyuv_predicted % w
elif (index == 1):
# xyuv_predicted += self.tuning_prop[gid, 3]
xyuv_predicted = xyuv_predicted % h
y_pos_grid = utils.get_grid_pos_1d(xyuv_predicted, grid_edges)
output_data[y_pos_grid, :] += self.nspikes_binned_normalized[gid, :]
return output_data, grid_edges
def compute_position_estimates(self):
pass
def get_average_of_circular_quantity(self, confidence_vec, tuning_vec, xv='x'):
"""
Computes the population average of a circular quantity.
This is done by 1) mapping the quantity onto a circle
2) weighting the single quantities on the circle
3) getting the average by using arctan2 giving the 'directed' angle
"""
if xv == 'x':
range_0_1 = True
else:
range_0_1 = False
n = confidence_vec.size
sin = np.zeros(n)
cos = np.zeros(n)
if range_0_1:
sin = confidence_vec * np.sin(tuning_vec * 2 * np.pi - np.pi)
cos = confidence_vec * np.cos(tuning_vec * 2 * np.pi - np.pi)
avg = .5 * (np.arctan2(sin.sum(), cos.sum()) / np.pi + 1.)
else: # range_-1_1
sin = confidence_vec * np.sin(tuning_vec * np.pi)
cos = confidence_vec * np.cos(tuning_vec * np.pi)
avg = np.arctan2(sin.sum(), cos.sum()) / np.pi
return avg
def compute_v_estimates(self):
"""
This function combines activity on the population level to estimate vx, vy
On which time scale shall the prediction work?
There are (at least) 3 different ways to do it:
Very short time-scale:
1) Compute the prediction for each time bin - based on the activitiy in the respective time bin
Short time-scale:
2) Compute the prediction for each time bin based on all activity in the past
3) Non-linear 'voting' based on 1)
Long time-scale:
3) Compute the prediction based on the the activity of the whole run - not time dependent
4) Non-linear 'voting' based on 3)
"""
print 'Computing v estimates...'
mp = self.params['motion_params']
self.x_stim = np.zeros(self.n_bins) # stimulus positions binned
self.y_stim = np.zeros(self.n_bins)
self.x_stim_delayed = np.zeros(self.n_bins) # stimulus positions binned
self.y_stim_delayed = np.zeros(self.n_bins)
# momentary result, based on the activity in one time bin
self.x_avg = np.zeros(self.n_bins)
self.y_avg = np.zeros(self.n_bins)
self.xdiff_avg = np.zeros(self.n_bins) # stores |x_predicted(t) - x_stimulus(t)|
self.vx_avg = np.zeros(self.n_bins)
self.vy_avg = np.zeros(self.n_bins)
self.vdiff_avg = np.zeros(self.n_bins) # stores |v_predicted(t) - v_stimulus(t)|
# ---> gives theta_avg
# based on the activity in several time bins
self.x_moving_avg = np.zeros((self.n_bins, 2))
self.y_moving_avg = np.zeros((self.n_bins, 2))
self.xdiff_moving_avg = np.zeros((self.n_bins, 2))
self.vx_moving_avg = np.zeros((self.n_bins, 2))
self.vy_moving_avg = np.zeros((self.n_bins, 2))
self.vdiff_moving_avg = np.zeros((self.n_bins, 2))
# non linear transformation of vx_avg
self.x_non_linear = np.zeros(self.n_bins)
self.y_non_linear = np.zeros(self.n_bins)
self.xdiff_non_linear = np.zeros(self.n_bins)
self.vx_non_linear = np.zeros(self.n_bins)
self.vy_non_linear = np.zeros(self.n_bins)
self.vdiff_non_linear = np.zeros(self.n_bins)
trace_length_in_bins = int(round(self.trace_length / self.time_binsize))
# ---> gives theta_moving_avg
# # # # # # # # # # # # # # # # # # # # # #
# L O C A T I O N P R E D I C T I O N #
# # # # # # # # # # # # # # # # # # # # # #
self.x_confidence_binned = self.nspikes_binned_normalized[self.sorted_indices_x]
self.y_confidence_binned = self.nspikes_binned_normalized[self.sorted_indices_y]
x_prediction_trace = np.zeros((self.n_cells, self.n_bins, 2)) # _trace: prediction based on the momentary and past activity (moving average, and std) --> trace_length
y_prediction_trace = np.zeros((self.n_cells, self.n_bins, 2)) # _trace: prediction based on the momentary and past activity (moving average, and std) --> trace_length
# # # # # # # # # # # # # # # # # # #
# S P E E D P R E D I C T I O N #
# # # # # # # # # # # # # # # # # # #
self.vx_confidence_binned = self.nspikes_binned_normalized[self.sorted_indices_vx]
self.vy_confidence_binned = self.nspikes_binned_normalized[self.sorted_indices_vy]
vx_prediction_trace = np.zeros((self.n_cells, self.n_bins, 2)) # _trace: prediction based on the momentary and past activity (moving average, and std) --> trace_length
vy_prediction_trace = np.zeros((self.n_cells, self.n_bins, 2)) # _trace: prediction based on the momentary and past activity (moving average, and std) --> trace_length
# torus dimensions
w, h = self.params['torus_width'], self.params['torus_height']
# introduce neural delay
n_delay_bins = self.params['sensory_delay'] / self.time_binsize
for i in xrange(self.n_bins):
# 1) momentary vote
# take the weighted average for v_prediction (weight = normalized activity)
vx_pred = self.vx_confidence_binned[:, i] * self.vx_tuning
vy_pred = self.vy_confidence_binned[:, i] * self.vy_tuning
self.vx_avg[i] = self.get_average_of_circular_quantity(self.vx_confidence_binned[:, i], self.vx_tuning, xv='v')
self.vy_avg[i] = self.get_average_of_circular_quantity(self.vy_confidence_binned[:, i], self.vy_tuning, xv='v')
# self.vx_avg[i] = np.sum(vx_pred)
# self.vy_avg[i] = np.sum(vy_pred)
self.vdiff_avg[i] = np.sqrt((mp[2] - self.vx_avg[i])**2 + (mp[3] - self.vy_avg[i])**2)
# position
# t = i * self.time_binsize + .5 * self.time_binsize
t = (i + n_delay_bins) * self.time_binsize + .5 * self.time_binsize
# stim_pos_x = (mp[0] + mp[2] * t / self.params['t_stimulus']) % w# be sure that this works the same as utils.get_input is called!
# stim_pos_y = (mp[1] + mp[3] * t / self.params['t_stimulus']) % h # be sure that this works the same as utils.get_input is called!
stim_pos_x = (mp[0] + mp[2] * t / 1000.) % w# be sure that this works the same as utils.get_input is called!
stim_pos_y = (mp[1] + mp[3] * t / 1000.) % h # be sure that this works the same as utils.get_input is called!
self.x_stim_delayed[i] = (mp[0] + mp[2] * (t / 1000. - self.params['sensory_delay'])) % w# be sure that this works the same as utils.get_input is called!
self.y_stim_delayed[i] = (mp[1] + mp[3] * (t / 1000. - self.params['sensory_delay'])) % w# be sure that this works the same as utils.get_input is called!
self.x_stim[i] = stim_pos_x
self.y_stim[i] = stim_pos_y
x_pred = self.x_confidence_binned[:, i] * self.x_tuning
y_pred = self.y_confidence_binned[:, i] * self.y_tuning
self.x_avg[i] = self.get_average_of_circular_quantity(self.x_confidence_binned[:, i], self.x_tuning, xv='x')
self.y_avg[i] = self.get_average_of_circular_quantity(self.y_confidence_binned[:, i], self.y_tuning, xv='x')
# self.x_avg[i] = np.sum(x_pred)
# self.y_avg[i] = np.sum(y_pred)
if self.params['n_grid_dimensions'] == 2:
self.xdiff_avg[i] = np.sqrt((stim_pos_x - self.x_avg[i])**2 + (stim_pos_y - self.y_avg[i])**2)
else:
self.xdiff_avg[i] = np.abs(stim_pos_x - self.x_avg[i])
# 2) moving average
past_bin = max(0, i-trace_length_in_bins)
if i == past_bin:
self.x_moving_avg[i, 0] = self.x_avg[i]
self.x_moving_avg[i, 1] = 0.
self.y_moving_avg[i, 0] = self.y_avg[i]
self.y_moving_avg[i, 1] = 0.
self.vx_moving_avg[i, 0] = self.vx_avg[i]
self.vx_moving_avg[i, 1] = 0.
self.vy_moving_avg[i, 0] = self.vy_avg[i]
self.vy_moving_avg[i, 1] = 0.
else:
self.x_moving_avg[i, 0] = self.x_avg[past_bin:i].mean()
self.x_moving_avg[i, 1] = self.x_avg[past_bin:i].std()
self.y_moving_avg[i, 0] = self.y_avg[past_bin:i].mean()
self.y_moving_avg[i, 1] = self.y_avg[past_bin:i].std()
self.vx_moving_avg[i, 0] = self.vx_avg[past_bin:i].mean()
self.vx_moving_avg[i, 1] = self.vx_avg[past_bin:i].std()
self.vy_moving_avg[i, 0] = self.vy_avg[past_bin:i].mean()
self.vy_moving_avg[i, 1] = self.vy_avg[past_bin:i].std()
# x moving average
self.xdiff_moving_avg[i, 0] = np.sqrt((stim_pos_x - self.x_moving_avg[i, 0])**2 + (stim_pos_y - self.y_moving_avg[i, 0])**2)
x_diff = (self.x_avg[i] - stim_pos_x)
y_diff = (self.y_avg[i] - stim_pos_x)
self.xdiff_moving_avg[i, 1] = (2. / self.xdiff_moving_avg[i, 0]) * ( x_diff * self.x_moving_avg[i, 1] + y_diff * self.y_moving_avg[i, 1])
# v
self.vdiff_moving_avg[i, 0] = np.sqrt((mp[2] - self.vx_moving_avg[i, 0])**2 + (mp[3] - self.vy_moving_avg[i, 0])**2)
# propagation of uncertainty
vx_diff = self.vx_moving_avg[i ,0] - mp[2]
vy_diff = self.vy_moving_avg[i ,0] - mp[3]
self.vdiff_moving_avg[i, 1] = (2. / self.vdiff_moving_avg[i, 0]) * ( vx_diff * self.vx_moving_avg[i, 1] + vy_diff * self.vy_moving_avg[i, 1])
# 3) soft-max
# x
# rescale activity to negative values
x_shifted = self.nspikes_binned[self.sorted_indices_x, i] - self.nspikes_binned[self.sorted_indices_x, i].max()
y_shifted = self.nspikes_binned[self.sorted_indices_y, i] - self.nspikes_binned[self.sorted_indices_y, i].max()
# exp --> mapping to range(0, 1)
x_exp = np.exp(x_shifted)
y_exp = np.exp(y_shifted)
# normalize and vote
# x_votes = (x_exp / x_exp.sum()) * self.x_tuning
# y_votes = (y_exp / y_exp.sum()) * self.y_tuning
# self.x_non_linear[i] = x_votes.sum()
# self.y_non_linear[i] = y_votes.sum()
# self.xdiff_non_linear[i] = np.sqrt((stim_pos_x - self.x_non_linear[i])**2 + (stim_pos_y - self.y_non_linear[i])**2)
self.x_non_linear[i] = self.get_average_of_circular_quantity(x_exp, self.x_tuning, xv='x')
self.y_non_linear[i] = self.get_average_of_circular_quantity(y_exp, self.x_tuning, xv='x')
self.xdiff_non_linear[i] = np.sqrt((stim_pos_x - self.x_non_linear[i])**2 + (stim_pos_y - self.y_non_linear[i])**2)
# v
# rescale activity to negative values
vx_shifted = self.nspikes_binned[self.sorted_indices_vx, i] - self.nspikes_binned[self.sorted_indices_vx, i].max()
vy_shifted = self.nspikes_binned[self.sorted_indices_vy, i] - self.nspikes_binned[self.sorted_indices_vy, i].max()
# exp --> mapping to range(0, 1)
vx_exp = np.exp(vx_shifted)
vy_exp = np.exp(vy_shifted)
# normalize and vote
# vx_votes = (vx_exp / vx_exp.sum()) * self.vx_tuning
# vy_votes = (vy_exp / vy_exp.sum()) * self.vy_tuning
# self.vx_non_linear[i] = vx_votes.sum()
# self.vy_non_linear[i] = vy_votes.sum()
# self.vdiff_non_linear[i] = np.sqrt((mp[2] - self.vx_non_linear[i])**2 + (mp[3] - self.vy_non_linear[i])**2)
self.vx_non_linear[i] = self.get_average_of_circular_quantity(vx_exp, self.vx_tuning, xv='v')
self.vy_non_linear[i] = self.get_average_of_circular_quantity(vy_exp, self.vy_tuning, xv='v')
self.vdiff_non_linear[i] = np.sqrt((mp[2]- self.vx_non_linear[i])**2 + (mp[3]- self.vy_non_linear[i])**2)
# in the first step the trace can not have a standard deviation --> avoid NANs
self.x_moving_avg[0, 0] = np.sum(self.x_confidence_binned[self.sorted_indices_x, 0] * self.x_tuning)
self.y_moving_avg[0, 0] = np.sum(self.y_confidence_binned[self.sorted_indices_y, 0] * self.y_tuning)
self.x_moving_avg[0, 1] = 0
self.y_moving_avg[0, 1] = 0
self.xdiff_moving_avg[0, 1] = 0
self.xdiff_moving_avg[0, 1] = 0
self.vx_moving_avg[0, 0] = np.sum(self.vx_confidence_binned[self.sorted_indices_vx, 0] * self.vx_tuning)
self.vy_moving_avg[0, 0] = np.sum(self.vy_confidence_binned[self.sorted_indices_vy, 0] * self.vy_tuning)
self.vx_moving_avg[0, 1] = 0
self.vy_moving_avg[0, 1] = 0
self.vdiff_moving_avg[0, 1] = 0
self.vdiff_moving_avg[0, 1] = 0
# ---> time INdependent estimates: based on activity of the full run
# compute the marginalized (over all positions) vx, vy estimates and bin them in a grid
# is omitted for position because full run estimates for a moving stimulus do not make sense
self.vx_marginalized_binned = np.zeros(self.n_vx_bins)
self.vy_marginalized_binned = np.zeros(self.n_vy_bins)
self.vx_marginalized_binned_nonlinear = np.zeros(self.n_vx_bins)
self.vy_marginalized_binned_nonlinear = np.zeros(self.n_vy_bins)
for gid in xrange(self.n_cells):
vx_cell, vy_cell = self.tuning_prop[gid, 2], self.tuning_prop[gid, 3] # cell properties
vx_grid_pos, vy_grid_pos = utils.get_grid_pos(vx_cell, vy_cell, self.vx_grid, self.vy_grid)
self.vx_marginalized_binned[vx_grid_pos] += self.nspikes_normalized[gid]
self.vy_marginalized_binned[vy_grid_pos] += self.nspikes_normalized[gid]
self.vx_marginalized_binned_nonlinear[vx_grid_pos] += self.nspikes_normalized_nonlinear[gid]
self.vy_marginalized_binned_nonlinear[vy_grid_pos] += self.nspikes_normalized_nonlinear[gid]
# assert (np.sum(self.vx_marginalized_binned) == 1.), "Marginalization incorrect: %.10e" % (np.sum(self.vx_marginalized_binned))
# assert (np.sum(self.vx_marginalized_binned_nonlinear) == 1.), "Marginalization incorrect: %f" % (np.sum(self.vx_marginalized_binned_nonlinear))
# assert (np.sum(self.vy_marginalized_binned) == 1.), "Marginalization incorrect: %f" % (np.sum(self.vy_marginalized_binned))
# assert (np.sum(self.vy_marginalized_binned_nonlinear) == 1.), "Marginalization incorrect: %f" % (np.sum(self.vy_marginalized_binned))
def save_data(self):
output_folder = self.params['data_folder']
for key in self.data_to_store:
d = self.data_to_store[key]
data = d['data']
fn = self.params['data_folder'] + key
print 'Saving data to:', fn
np.savetxt(fn, data)
def compute_theta_estimates(self):
# time dependent averages
self.theta_avg = np.arctan2(self.vy_avg, self.vx_avg)
self.theta_moving_avg = np.zeros((self.n_bins, 2))
self.theta_moving_avg[:, 0] = np.arctan2(self.vy_moving_avg[:, 0], self.vx_moving_avg[:, 0])
self.theta_moving_avg[:, 1] = self.theta_uncertainty(self.vx_moving_avg[:, 0], self.vx_moving_avg[:, 1], self.vy_moving_avg[:, 0], self.vy_moving_avg[:, 1])
self.theta_non_linear = np.arctan2(self.vy_non_linear, self.vx_non_linear)
# full run estimates
all_thetas = np.arctan2(self.tuning_prop[:, 3], self.tuning_prop[:, 2])
self.theta_grid = np.linspace(np.min(all_thetas), np.max(all_thetas), self.n_vx_bins, endpoint=True)
self.theta_marginalized_binned = np.zeros(self.n_vx_bins)
self.theta_marginalized_binned_nonlinear = np.zeros(self.n_vx_bins)
for gid in xrange(self.n_cells):
theta = np.arctan2(self.tuning_prop[gid, 3], self.tuning_prop[gid, 2])
grid_pos = utils.get_grid_pos_1d(theta, self.theta_grid)
self.theta_marginalized_binned[grid_pos] += self.nspikes_normalized[gid]
self.theta_marginalized_binned_nonlinear[grid_pos] += self.nspikes_normalized_nonlinear[gid]
# assert (np.sum(self.theta_marginalized_binned) == 1), "Marginalization incorrect: %.1f" % (np.sum(self.theta_marginalized_binned))
# assert (np.sum(self.theta_marginalized_binned_nonlinear) == 1), "Marginalization incorrect: %.1f" % (np.sum(self.theta_marginalized_binned_nonlinear))
def create_fig(self):
print "plotting ...."
rcParams = { 'axes.labelsize' : 48,
'axes.titlesize' : 48,
'label.fontsize': 32,
'xtick.labelsize' : 32,
'ytick.labelsize' : 32,
'legend.fontsize': 16,
'lines.markeredgewidth' : 0}
pylab.rcParams.update(rcParams)
self.fig = pylab.figure(figsize=self.fig_size)
pylab.subplots_adjust(hspace=0.4)
pylab.subplots_adjust(wspace=0.35)
def load_spiketimes(self, cell_type):
if cell_type == 'inh':
fn = self.params['inh_spiketimes_fn_merged'] + '.ras'
n_cells = self.params['n_inh']
nspikes, self.inh_spiketimes = utils.get_nspikes(fn, n_cells, get_spiketrains=True)
spiketimes = self.inh_spiketimes
np.savetxt(self.params['inh_nspikes_fn_merged'] + '.dat', nspikes)
idx = np.nonzero(nspikes)[0]
np.savetxt(self.params['inh_nspikes_nonzero_fn'], np.array((idx, nspikes[idx])).transpose())
elif cell_type == 'exc':
fn = self.params['exc_spiketimes_fn_merged'] + '.ras'
n_cells = self.params['n_exc']
nspikes, self.exc_spiketimes = utils.get_nspikes(fn, n_cells, get_spiketrains=True)
spiketimes = self.exc_spiketimes
np.savetxt(self.params['exc_nspikes_fn_merged'] + '.dat', np.array((range(n_cells), nspikes)).transpose())
idx = np.nonzero(nspikes)[0]
np.savetxt(self.params['exc_nspikes_nonzero_fn'], np.array((idx, nspikes[idx])).transpose())
self.spiketimes_loaded = True
return spiketimes, nspikes
def plot_rasterplot(self, cell_type, fig_cnt=1, show_blank=None):
spiketimes, nspikes = self.load_spiketimes(cell_type)
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
for cell in xrange(int(len(spiketimes))):
ax.plot(spiketimes[cell], cell * np.ones(nspikes[cell]), 'o', color='k', markersize=1)
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
ylim = ax.get_ylim()
if cell_type == 'exc':
ax.set_ylim((0, self.params['n_exc']))
else:
ax.set_ylim((0, self.params['n_inh']))
# ax.set_ylim((ylim[0] - 1, ylim[1] + 1))
ax.set_xlim(0, self.params['t_sim'])
ax.set_title('Rasterplot of %s neurons' % cell_type)
ax.set_xlabel('Time [ms]')
ax.set_ylabel('Neuron GID')
def plot_network_activity(self, cell_type, fig_cnt=1):
if cell_type == 'exc':
n_cells = self.params['n_exc']
spiketimes = self.spiketrains
else:
n_cells = self.params['n_inh']
spiketimes = utils.get_spiketrains(self.params['inh_spiketimes_fn_merged'] + '.ras', n_cells)
n_bins = int(round(self.params['t_sim'] / self.time_binsize))
binned_spiketimes = [[] for i in xrange(n_cells)]
avg_activity = np.zeros(n_bins)
n_active = 0
for cell in xrange(n_cells):
if len(spiketimes[cell]) > 0:
binned_spiketimes[cell], bins = np.histogram(spiketimes[cell], n_bins, range=(0, self.params['t_sim']))
n_active += 1
for time_bin in xrange(n_bins):
for cell in xrange(n_cells):
if len(spiketimes[cell]) > 0:
avg_activity[time_bin] += binned_spiketimes[cell][time_bin]
avg_activity[time_bin] /= n_active
avg_activity /= (self.time_binsize / 1000.)
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_xlim((0, self.params['t_sim']))
bins = np.linspace(0, self.params['t_sim'], n_bins, endpoint=True)
ax.bar(bins, avg_activity, width=bins[1]-bins[0], )
ax.set_xlabel('Time [ms]')
ax.set_ylabel('Average firing rate [Hz]')
ax.set_title('Activity of %s cells' % cell_type)
def plot_vx_grid_vs_time(self, fig_cnt=1):
print 'plot_vx_grid_vs_time ... '
xlabel = 'Time [ms]'
ylabel = '$v_x$'
# title = '$v_x$ binned vs time'
title = ''
vx_grid, v_edges = self.bin_estimates(self.vx_grid, index=2)
self.plot_grid_vs_time(vx_grid, title, xlabel, ylabel, v_edges, fig_cnt)
self.data_to_store['vx_grid.dat'] = {'data' : vx_grid, 'edges': v_edges}
def plot_vy_grid_vs_time(self, fig_cnt=1):
print 'plot_vy_grid_vs_time ...'
xlabel = 'Time [ms]'
ylabel = '$v_y$'
title = ''#$v_y$ binned vs time'
vy_grid, v_edges = self.bin_estimates(self.vy_grid, index=3)
self.plot_grid_vs_time(vy_grid, title, xlabel, ylabel, v_edges, fig_cnt)
self.data_to_store['vy_grid.dat'] = {'data' : vy_grid, 'edges': v_edges}
def plot_x_grid_vs_time(self, fig_cnt=1, ylabel=None, title=''):
print 'plot_x_grid_vs_time ...'
xlabel = 'Time [ms]'
if ylabel == None:
ylabel = '$x_{predicted}$'
# title = ''#$x_{predicted}$ binned vs time'
x_grid, x_edges = self.bin_estimates(self.x_grid, index=0)
self.plot_grid_vs_time(x_grid, title, xlabel, ylabel, x_edges, fig_cnt, plot_stim=True)
self.data_to_store['xpos_grid.dat'] = {'data' : x_grid, 'edges': x_edges}
def plot_y_grid_vs_time(self, fig_cnt=1, ylabel=None):
print 'plot_y_grid_vs_time ...'
xlabel = 'Time [ms]'
if ylabel == None:
ylabel = '$y_{predicted}$'
title = ''#$y_{predicted}$ binned vs time'
y_grid, y_edges = self.bin_estimates(self.y_grid, index=1)
self.plot_grid_vs_time(y_grid, title, xlabel, ylabel, y_edges, fig_cnt)
self.data_to_store['ypos_grid.dat'] = {'data' : y_grid, 'edges': y_edges}
def plot_grid_vs_time(self, data, title='', xlabel='', ylabel='', yticks=[], fig_cnt=1, show_blank=None, plot_stim=False):
"""
Plots a colormap / grid versus time
"""
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_title(title)
ax.set_ylim((0, data[:, 0].size))
ax.set_xlim((0, data[0, :].size))
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
y_ticks = range(len(yticks))[::5]
ax.set_yticks(y_ticks)
ax.set_yticklabels(['%.2f' %i for i in yticks[::5]])
n_x_bins = len(self.t_ticks)
x_bin_labels = ['%d' % i for i in self.t_ticks]
ax.set_xticks(np.linspace(0, self.n_bins, n_x_bins))#range(self.n_bins)[::4])
ax.set_xticklabels(x_bin_labels)
# max_conf = min(data.mean() + .5 * data.std(), data.max())
max_conf = .5 * data.max()
print 'max_conf:', max_conf, ' data mean, std', data.mean(), data.std(), 'data max', data.max()
norm = matplotlib.colors.Normalize(vmin=0, vmax=max_conf)#, clip=True)
m = matplotlib.cm.ScalarMappable(norm=norm, cmap=cm.jet)
m.set_array(np.arange(0., max_conf, 0.01))
cb = pylab.colorbar(m, orientation='horizontal', aspect=40)#, anchor=(.5, .0))
cax = ax.pcolormesh(data, vmin=0., vmax=max_conf)
ticklabels = cb.ax.get_xticklabels()
ticklabel_texts = []
for text in ticklabels:
ticklabel_texts.append('%s' % text.get_text())
ticklabel_texts[-1] = '> %.2f' % max_conf
for i_, text in enumerate(ticklabels):
text.set_text(ticklabel_texts[i_])
cb.ax.set_xticklabels(ticklabel_texts)
cb.set_label('Prediction confidence')
if show_blank:
self.plot_blank_on_cmap(cax, txt='blank')
self.plot_start_stop(ax)
if plot_stim:
ax = cax.axes
y_pos_of_stim = np.zeros(self.n_bins)
y_pos_of_stim_delayed = np.zeros(self.n_bins)
for t_bin in xrange(self.n_bins):
y_pos_of_stim[t_bin] = utils.get_grid_pos_1d(self.x_stim[t_bin], yticks)
y_pos_of_stim_delayed[t_bin] = utils.get_grid_pos_1d(self.x_stim_delayed[t_bin], yticks)
# for t_bin in xrange(self.n_bins-1):
# ax.plot((t_bin, t_bin+1), (y_pos_of_stim[t_bin], y_pos_of_stim[t_bin+1]), ls='--', c='w', lw=3, label='$x_{stim}$')
# print '\nDEBUG\n', y_pos_of_stim
# ax.plot((0, self.n_bins), (y_pos_of_stim[0], y_pos_of_stim[-1]), ls='--', c='w', lw=3, label='$x_{stim}$')
n_delay_bins = self.params['sensory_delay'] / self.time_binsize
ax.plot((n_delay_bins, self.n_bins + n_delay_bins), (y_pos_of_stim[0], y_pos_of_stim[-1]), ls='--', c='k', lw=10, label='$x_{delay}$')
ax.plot((n_delay_bins, self.n_bins + n_delay_bins), (y_pos_of_stim_delayed[0], y_pos_of_stim_delayed[-1]), ls='--', c='w', lw=10, label='$x_{delay}$')
def plot_xdiff(self, fig_cnt=1, show_blank=None):
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_title('Position prediction error: \n $|\\vec{x}_{diff}(t)| = |\\vec{x}_{stim}(t) - \\vec{x}_{predicted}(t)|$')#, fontsize=self.plot_params['title_fs'])
ax.plot(self.t_axis, self.xdiff_avg, ls='-', lw=2, label='linear readout')
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$|\\vec{x}_{diff}|$')
ax.legend()#loc='upper right')
ny = self.t_axis.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
t_labels= ['%d' % i for i in self.t_ticks]
ax.set_xticks(self.t_ticks)
ax.set_xticklabels(t_labels)
ax.set_xlim((0, self.params['t_sim']))
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
print 'xdiff_avg.sum:', self.xdiff_avg.sum()
output_data = np.zeros((self.t_axis.size, 4))
output_data[:, 0] = self.t_axis
output_data[:, 1] = self.xdiff_avg
output_data[:, 2] = self.xdiff_moving_avg[:, 0]
output_data[:, 3] = self.xdiff_non_linear
output_fn = self.params['xdiff_vs_time_fn']
self.data_to_store[output_fn] = {'data' : output_data}
def plot_vdiff(self, fig_cnt=1, show_blank=None):
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_title('Velocity prediction error: \n $|\\vec{v}_{diff}(t)| = |\\vec{v}_{stim}-\\vec{v}_{predicted}(t)|$')#, fontsize=self.plot_params['title_fs'])
ax.plot(self.t_axis, self.vdiff_avg, ls='-', lw=2, label='linear readout')
# ax.plot(self.t_axis, self.vdiff_moving_avg[:, 0], ls='--', lw=2, label='moving avg')
# ax.errorbar(self.t_axis, self.vdiff_moving_avg[:, 0], yerr=self.vdiff_moving_avg[:, 1], ls='--', lw=2, label='moving avg')
# ax.plot(self.t_axis, self.vdiff_non_linear, ls=':', lw=2, label='soft-max')
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$|\\vec{v}_{diff}|$')
ax.legend()#loc='upper right')
ny = self.t_axis.size
# n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
# t_ticks = [self.t_axis[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
t_labels= ['%d' % i for i in self.t_ticks]
ax.set_xticks(self.t_ticks)
ax.set_xticklabels(t_labels)
ax.set_xlim((0, self.params['t_sim']))
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
print 'vdiff_avg.sum:', self.vdiff_avg.sum()
output_data = np.zeros((self.t_axis.size, 4))
output_data[:, 0] = self.t_axis
output_data[:, 1] = self.vdiff_avg
output_data[:, 2] = self.vdiff_moving_avg[:, 0]
output_data[:, 3] = self.vdiff_non_linear
output_fn = self.params['vdiff_vs_time_fn']
self.data_to_store[output_fn] = {'data' : output_data}
def plot_nspikes_binned(self):
ax = self.fig.add_subplot(421)
ax.set_title('Spiking activity over time')
self.cax = ax1.pcolormesh(self.nspikes_binned)
ax.set_ylim((0, self.nspikes_binned[:, 0].size))
ax.set_xlim((0, self.nspikes_binned[0, :].size))
ax.set_xlabel('Time [ms]')
ax.set_ylabel('GID')
ax.set_xticks(range(self.n_bins)[::2])
ax.set_xticklabels(['%d' %i for i in self.time_bins[::2]])
pylab.colorbar(self.cax)
self.plot_start_stop(ax)
def plot_nspikes_binned_normalized(self):
ax = self.fig.add_subplot(422)
ax.set_title('Normalized activity over time')
self.cax = ax2.pcolormesh(self.nspikes_binned_normalized)
ax.set_ylim((0, self.nspikes_binned_normalized[:, 0].size))
ax.set_xlim((0, self.nspikes_binned_normalized[0, :].size))
ax.set_xlabel('Time [ms]')
ax.set_ylabel('GID')
ax.set_xticks(range(self.n_bins)[::2])
ax.set_xticklabels(['%d' %i for i in self.time_bins[::2]])
pylab.colorbar(self.cax)
self.plot_start_stop(ax)
def plot_vx_confidence_binned(self):
ax = self.fig.add_subplot(423)
ax.set_title('Vx confidence over time')
self.cax = ax.pcolormesh(self.vx_confidence_binned)
ax.set_ylim((0, self.vx_confidence_binned[:, 0].size))
# ax.set_xlim((0, self.vx_confidence_binned[0, :].size))
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$u$')
ax.set_xticks(range(self.n_bins)[::2])
ax.set_xticklabels(['%d' %i for i in self.time_bins[::2]])
ny = self.vx_tuning.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
yticks = [self.vx_tuning[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
ylabels = ['%.1e' % i for i in yticks]
ax.set_yticks([int(i * ny/n_ticks) for i in xrange(n_ticks)])
ax.set_yticklabels(ylabels)
ax.set_xticks(range(self.n_bins)[::2])
ax.set_xticklabels(['%d' %i for i in self.time_bins[::2]])
ax.set_xlim(0, self.params['t_sim'])
pylab.colorbar(self.cax)
self.plot_start_stop(ax)
def plot_vy_confidence_binned(self):
ax = self.fig.add_subplot(424)
ax.set_title('vy confidence over time')
self.cax = ax.pcolormesh(self.vy_confidence_binned)
ax.set_ylim((0, self.vy_confidence_binned[:, 0].size))
# ax.set_xlim((0, self.vy_confidence_binned[0, :].size))
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$\vecv$')
ax.set_xticks(range(self.n_bins)[::2])
ax.set_xticklabels(['%d' %i for i in self.time_bins[::2]])
ny = self.vy_tuning.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
yticks = [self.vy_tuning[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
ylabels = ['%.1e' % i for i in yticks]
ax.set_yticks([int(i * ny/n_ticks) for i in xrange(n_ticks)])
ax.set_yticklabels(ylabels)
ax.set_xticks(range(self.n_bins)[::2])
ax.set_xticklabels(['%d' %i for i in self.time_bins[::2]])
ax.set_xlim(0, self.params['t_sim'])
self.plot_start_stop(ax)
pylab.colorbar(self.cax)
def plot_x_estimates(self, fig_cnt=1, show_blank=None):
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_title('$x$-predictions')#: avg, moving_avg, nonlinear')
ax.plot(self.t_axis, self.x_avg, ls='-', lw=2, label='linear readout')
# ax.plot(self.t_axis, self.x_moving_avg[:, 0], ls='--', lw=2, label='moving avg')
# ax.errorbar(self.t_axis, self.x_moving_avg[:, 0], yerr=self.x_moving_avg[:, 1], ls='--', lw=2, label='moving avg')
# ax.plot(self.t_axis, self.x_non_linear, ls=':', lw=2, label='soft-max')
ax.plot(self.t_axis, self.x_stim, ls='-', c='k', lw=2, label='$x_{stim}$')
ax.legend()#loc='upper left')
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$x$ position [a.u.]')
ny = self.t_axis.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
# t_ticks = [self.t_axis[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
t_labels= ['%d' % i for i in self.t_ticks]
ax.set_xticks(self.t_ticks)
ax.set_xticklabels(t_labels)
ax.set_xlim((0, self.params['t_sim']))
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
def plot_y_estimates(self, fig_cnt=1, show_blank=None):
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_title('$y$-predictions')#: avg, moving_avg, nonlinear')
ax.plot(self.t_axis, self.y_avg, ls='-', lw=2, label='linear readout')
# ax.plot(self.t_axis, self.y_moving_avg[:, 0], ls='--', lw=2, label='moving avg')
# ax.errorbar(self.t_axis, self.y_moving_avg[:, 0], yerr=self.y_moving_avg[:, 1], ls='--', lw=2, label='moving avg')
# ax.plot(self.t_axis, self.y_non_linear, ls=':', lw=2, label='soft-max')
ax.plot(self.t_axis, self.y_stim, ls='-', c='k', lw=2, label='$y_{stim}$')
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$y$ position [a.u.]')
# ax.legend()
ax.legend()#loc='lower right')
ny = self.t_axis.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
# t_ticks = [self.t_axis[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
t_labels= ['%d' % i for i in self.t_ticks]
ax.set_xticks(self.t_ticks)
ax.set_xticklabels(t_labels)
ax.set_xlim((0, self.params['t_sim']))
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
def plot_vx_estimates(self, fig_cnt=1, show_blank=None):
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_title('$v_{x}$-predictions')#: avg, moving_avg, nonlinear')
ax.plot(self.t_axis, self.vx_avg, ls='-', lw=2, label='linear readout')
# ax.plot(self.t_axis, self.vx_moving_avg[:, 0], ls='--', lw=2, label='moving avg')
# ax.errorbar(self.t_axis, self.vx_moving_avg[:, 0], yerr=self.vx_moving_avg[:, 1], ls='--', lw=2, label='moving avg')
# ax.plot(self.t_axis, self.vx_non_linear, ls=':', lw=2, label='soft-max')
vx = self.params['motion_params'][2] * np.ones(self.t_axis.size)
ax.plot(self.t_axis, vx, ls='-', c='k', lw=2, label='$v_{y, stim}$')
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$v_x$')
ax.legend()#loc='lower right')
ny = self.t_axis.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
# t_ticks = [self.t_axis[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
t_labels= ['%d' % i for i in self.t_ticks]
ax.set_xticks(self.t_ticks)
ax.set_xticklabels(t_labels)
ax.set_xlim((0, self.params['t_sim']))
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
output_data = np.array((self.t_axis, self.vx_avg))
self.data_to_store['vx_linear_vs_time.dat'] = {'data' : output_data}
def plot_vy_estimates(self, fig_cnt=1, show_blank=None):
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.plot(self.t_axis, self.vy_avg, ls='-', lw=2, label='linear readout')
# ax.plot(self.t_axis, self.vy_moving_avg[:, 0], lw=2, ls='--', label='moving avg')
# ax.errorbar(self.t_axis, self.vy_moving_avg[:, 0], yerr=self.vy_moving_avg[:, 1], lw=2, ls='--', label='moving avg')
# ax.plot(self.t_axis, self.vy_non_linear, ls=':', lw=2, label='soft-max')
vy = self.params['motion_params'][3] * np.ones(self.t_axis.size)
ax.plot(self.t_axis, vy, ls='-', c='k', lw=2, label='$v_{y, stim}$')
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$v_y$')
ax.set_title('$v_{y}$-predictions')#: avg, moving_avg, nonlinear')
ax.legend()#loc='lower right')
ny = self.t_axis.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
# t_ticks = [self.t_axis[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
t_labels= ['%d' % i for i in self.t_ticks]
ax.set_xticks(self.t_ticks)
ax.set_xticklabels(t_labels)
ax.set_xlim((0, self.params['t_sim']))
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
output_data = np.array((self.t_axis, self.vy_avg))
self.data_to_store['vy_linear_vs_time.dat'] = {'data' : output_data}
def plot_theta_estimates(self, fig_cnt=1, show_blank=None):
if show_blank == None:
show_blank = self.show_blank
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
ax.set_title('$\Theta$-predictions: avg, moving_avg, nonlinear')
ax.plot(self.t_axis, self.theta_avg, ls='-')
# ax.plot(self.t_axis, self.theta_moving_avg[:, 0], ls='--')
# ax.errorbar(self.t_axis, self.theta_moving_avg[:, 0], yerr=self.theta_moving_avg[:, 1], ls='--')
# ax.plot(self.t_axis, self.theta_non_linear, ls=':')
ax.set_xlabel('Time [ms]')
ax.set_ylabel('$\Theta$')
ny = self.t_axis.size
n_ticks = min(11, int(round(self.params['t_sim'] / 100.)))
# t_ticks = [self.t_axis[int(i * ny/n_ticks)] for i in xrange(n_ticks)]
t_labels= ['%d' % i for i in self.t_ticks]
ax.set_xticks(self.t_ticks)
ax.set_xticklabels(t_labels)
ax.set_xlim((0, self.params['t_sim']))
if show_blank:
self.plot_blank(ax)
self.plot_start_stop(ax)
output_data = np.array((self.t_axis, self.theta_avg))
self.data_to_store['theta_linear_vs_time.dat'] = {'data' : output_data}
self.plot_start_stop(ax)
def plot_fullrun_estimates_vx(self, fig_cnt=1):
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
bin_width = .5 * (self.vx_grid[1] - self.vx_grid[0])
vx_linear = (np.sum(self.vx_grid * self.vx_marginalized_binned), self.get_uncertainty(self.vx_marginalized_binned, self.vx_grid))
vx_nonlinear = (np.sum(self.vx_grid * self.vx_marginalized_binned_nonlinear), self.get_uncertainty(self.vx_marginalized_binned_nonlinear, self.vx_grid))
ax.bar(self.vx_grid, self.vx_marginalized_binned, width=bin_width, label='Linear votes: $v_x=%.2f \pm %.2f$' % (vx_linear[0], vx_linear[1]))
ax.bar(self.vx_grid+bin_width, self.vx_marginalized_binned_nonlinear, width=bin_width, facecolor='g', label='Non-linear votes: $v_x=%.2f \pm %.2f$' % (vx_nonlinear[0], vx_nonlinear[1]))
ax.set_title('Estimates based on full run activity with %s connectivity\nblue: linear marginalization over all positions, green: non-linear voting' % self.params['connectivity_code'])
ax.set_xlabel('$v_x$')
ax.set_ylabel('Confidence')
ax.legend()
def get_uncertainty(self, p, v):
"""
p, v are vectors storing the confidence of the voters in p, and the values they vote for in v.
The uncertainty is estimated as:
sum_i p_i * (1. - p_i) * v_i
Idea behind it:
(1. - p_i) * v_i gives the uncertainty for each vote of v_i
multiplying it with p_i takes into account how much weight this uncertainty should have in the overall vote
"""
uncertainties = (np.ones(len(p)) - p) * v
weighted_uncertainties = p * uncertainties
return np.sum(weighted_uncertainties)
def plot_fullrun_estimates_vy(self, fig_cnt=1):
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
bin_width = .5 * (self.vy_grid[1] - self.vy_grid[0])
vy_linear = (np.sum(self.vy_grid * self.vy_marginalized_binned), self.get_uncertainty(self.vy_marginalized_binned, self.vy_grid))
vy_nonlinear = (np.sum(self.vy_grid * self.vy_marginalized_binned_nonlinear), self.get_uncertainty(self.vy_marginalized_binned_nonlinear, self.vy_grid))
ax.bar(self.vy_grid, self.vy_marginalized_binned, width=bin_width, label='Linear votes: $v_y=%.2f \pm %.2f$' % (vy_linear[0], vy_linear[1]))
ax.bar(self.vy_grid+bin_width, self.vy_marginalized_binned_nonlinear, width=bin_width, facecolor='g', label='Non-linear votes: $v_y=%.2f \pm %.2f$' % (vy_nonlinear[0], vy_nonlinear[1]))
ax.set_xlabel('$v_y$')
ax.set_ylabel('Confidence')
ax.legend()
def plot_fullrun_estimates_theta(self, fig_cnt=1):
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
bin_width = .5 * (self.theta_grid[-1] - self.theta_grid[-2])
theta_linear = (np.sum(self.theta_grid * self.theta_marginalized_binned), self.get_uncertainty(self.theta_marginalized_binned, self.theta_grid))
theta_nonlinear = (np.sum(self.theta_grid * self.theta_marginalized_binned_nonlinear), self.get_uncertainty(self.theta_marginalized_binned_nonlinear, self.theta_grid))
ax.bar(self.theta_grid, self.theta_marginalized_binned, width=bin_width, label='Linear votes: $\Theta=%.2f \pm %.2f$' % (theta_linear[0], theta_linear[1]))
ax.bar(self.theta_grid+bin_width, self.theta_marginalized_binned_nonlinear, width=bin_width, facecolor='g', label='Non-linear votes: $\Theta=%.2f \pm %.2f$' % (theta_nonlinear[0], theta_nonlinear[1]))
ax.bar(self.theta_grid, self.theta_marginalized_binned, width=bin_width)
ax.bar(self.theta_grid+bin_width, self.theta_marginalized_binned_nonlinear, width=bin_width, facecolor='g')
ax.set_xlim((-np.pi, np.pi))
ax.legend()
# n_bins = 50
# count, theta_bins = np.histogram(self.theta_tuning, n_bins)
# pred_avg, x = np.histogram(self.theta_avg_fullrun, n_bins)
# pred_nonlinear, x = np.histogram(self.theta_nonlinear_fullrun, n_bins)
# bin_width = theta_bins[1]-theta_bins[0]
# ax.bar(theta_bins[:-1], pred_avg, width=bin_width*.5)
# ax.bar(theta_bins[:-1]-.5*bin_width, pred_nonlinear, width=bin_width*.5, facecolor='g')
# ax.set_xlim((self.theta_tuning.min() - bin_width, self.theta_tuning.max()))
ax.set_xlabel('$\Theta$')
ax.set_ylabel('Confidence')
def plot_nspike_histogram(self, fig_cnt=1):
ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
mean_nspikes = self.nspikes.mean()* 1000./self.params['t_sim']
std_nspikes = self.nspikes.std() * 1000./self.params['t_sim']
ax.bar(range(self.n_cells), self.nspikes* 1000./self.params['t_sim'], label='$f_{mean} = (%.1f \pm %.1f)$ Hz' % (mean_nspikes, std_nspikes))
ax.set_xlabel('Cell gids')
ax.set_ylabel('Output rate $f_{out}$')