-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
executable file
·1354 lines (1138 loc) · 50.5 KB
/
utils.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
"""
This file contains a bunch of helper functions (in alphabetic order).
"""
import numpy as np
import numpy.random as rnd
import os
import copy
import simulation_parameters
def distance(x,y):
# alternative: np.linalg.norm(a-b)
return np.sqrt(np.sum((x - y)**2))
def load_params(param_fn):
if os.path.isdir(param_fn):
param_fn = os.path.abspath(param_fn) + '/Parameters/simulation_parameters.json'
ps = simulation_parameters.parameter_storage(param_fn)
folder_name = ps.params['folder_name']
ps.set_filenames(folder_name)
return ps.params
def load_spiketimes(params, cell_type):
fn = params['%s_spiketimes_fn_merged' % cell_type] + '.ras'
if not os.path.exists(fn):
merge_files(params['%s_spike_times_fn_base' % (cell_type)], params['%s_spike_times_fn_merged' % cell_type])
n_cells = params['n_%s' % cell_type]
nspikes, spiketimes = get_nspikes(fn, n_cells, get_spiketrains=True)
np.savetxt(params['%s_nspikes_fn_merged' % cell_type] + '.dat', nspikes)
idx = np.nonzero(nspikes)[0]
np.savetxt(params['%s_nspikes_nonzero_fn' % cell_type], np.array((idx, nspikes[idx])).transpose())
return spiketimes, nspikes
def filter_spike_train(spikes, dt=1., tau=30., t_max=None):
"""
spikes: list or array of spikes
"""
if t_max == None:
t_max = spikes[-1] + tau
t_vec = np.arange(0, t_max, dt)
y = np.zeros(t_vec.size)
spike_idx = []
for spike in spikes:
spike_idx.append((t_vec < spike).nonzero()[0][-1])
for i_, spike in enumerate(spikes):
y[spike_idx[i_]:] += np.exp(-(t_vec[spike_idx[i_]:] - spike) / tau)
return t_vec, y
def convert_connlist_to_matrix(fn, n_src, n_tgt):
"""
Convert the connlist which is in format (src, tgt, weight, delay) to a weight matrix.
"""
conn_list = np.loadtxt(fn)
m = np.zeros((n_src, n_tgt))
delays = np.zeros((n_src, n_tgt))
print 'utils.convert_connlist_to_matrix(%s, %d, %d)' % (fn, n_src, n_tgt)
# print 'utils.convert_connlist_to_matrix(%s, %d, %d) conn_list size: %d' % (fn, n_src, n_tgt, conn_list[:, 0].size)
for i in xrange(conn_list[:,0].size):
src = conn_list[i, 0]
tgt = conn_list[i, 1]
m[src, tgt] = conn_list[i, 2]
delays[src, tgt] = conn_list[i, 3]
return m, delays
def convert_connlist_to_adjlist_srcidx(fn, n_src):
"""
Convert the connlist which is in format (src, tgt, weight, delay) to an
adjacency list:
src : [tgt_0, ..., tgt_n]
"""
conn_list = np.loadtxt(fn)
print 'utils.convert_connlist_to_adjlist(%s, %d, %d)' % (fn, n_src)
adj_list = [[] for i in xrange(n_src)]
for src in xrange(n_src):
targets = get_targets(conn_list, src)
adj_list[src] = targets[:, 1:].tolist()
return adj_list
def convert_connlist_to_adjlist_tgtidx(fn, n_tgt):
"""
Convert the connlist which is in format (src, tgt, weight, delay) to an
adjacency list:
src : [tgt_0, ..., tgt_n]
"""
conn_list = np.loadtxt(fn)
print 'utils.convert_connlist_to_adjlist(%s, %d, %d)' % (fn, n_tgt)
adj_list = [[] for i in xrange(n_tgt)]
for tgt in xrange(n_src):
targets = get_targets(conn_list, tgt)
adj_list[tgt] = targets[:, [0, 2, 3]].tolist()
return adj_list
def extract_trace(d, gid):
"""
d : voltage trace from a saved with compatible_output=False
gid : cell_gid
"""
mask = gid * np.ones(d[:, 0].size)
indices = mask == d[:, 0]
time_axis, volt = d[indices, 1], d[indices, 2]
return time_axis, volt
def convert_spiketrain_to_trace(st, n):
"""
st: spike train in the format [time, id]
n : size of the trace to be returned
To be used with spike train inputs.
Returns a np.array with st[i] = 1 if i in st[:, 0], st[i] = 0 else.
"""
trace = np.zeros(n)
for i in st:
trace[int(i)] = 1
return trace
def low_pass_filter(trace, tau=10, initial_value=0.001, dt=1., spike_height=1.):
"""
trace can be e.g. a spike train trace, i.e. all elements are 0 except spike times = 1,
"""
eps = 0.0001
n = len(trace)
zi = np.ones(n) * initial_value
for i in xrange(1, n):
# pre-synaptic trace zi follows trace
dzi = dt * (trace[i] * spike_height - zi[i-1] + eps) / tau
zi[i] = zi[i-1] + dzi
return zi
def create_spike_trains_for_motion(tuning_prop, params, contrast=.9, my_units=None, seed=None):
"""
This function writes spike trains to a dedicated path specified in the params dict
Spike trains are generated for each unit / minicolumn based on the function's arguments the following way:
The strength of stimulation for a column is computed based on the motion parameters and the tuning properties of the column.
This strength determines the envelope the non-homogeneous Poisson process to create the spike train.
Arguments:
tuning_prop = np.array((n_cells, 4))
tp[:, 0] : x-position
tp[:, 1] : y-position
tp[:, 2] : u-position (speed in x-direction)
tp[:, 3] : v-position (speed in y-direction)
params: dictionary storing all simulation parameters
my_units: tuple of integers (start, begin), in case of parallel execution each processor creates spike trains for its own units or columns
"""
if seed == None:
seed = params['input_spikes_seed']
rnd.seed(seed)
dt = params['dt_rate'] # [ms] time step for the non-homogenous Poisson process
# time = np.arange(0, params['t_stimulus'], dt)
time = np.arange(0, params['t_sim'], dt)
blank_idx = np.arange(1./dt * params['t_stimulus'], 1. / dt * (params['t_stimulus'] + params['t_blank']))
if (my_units == None):
my_units = xrange(tp.shape[0])
else:
my_units = xrange(my_units[0], my_units[1])
n_cells = len(my_units)
L_input = np.zeros((n_cells, time.shape[0]))
for i_time, time_ in enumerate(time):
if (i_time % 100 == 0):
print "t:", time_
L_input[:, i_time] = get_input(tuning_prop[my_units, :], params, time_/params['t_stimulus'])
L_input[:, i_time] *= params['f_max_stim']
for i_time in blank_idx:
L_input[:, i_time] = 0.
for i_, unit in enumerate(my_units):
rate_of_t = np.array(L_input[i_, :])
output_fn = params['input_rate_fn_base'] + str(unit) + '.npy'
np.save(output_fn, rate_of_t)
# each cell will get its own spike train stored in the following file + cell gid
n_steps = rate_of_t.size
st = []
for i in xrange(n_steps):
r = rnd.rand()
if (r <= ((rate_of_t[i]/1000.) * dt)): # rate is given in Hz -> 1/1000.
st.append(i * dt)
# output_fn = tgt_fn_base + str(column)
output_fn = params['input_st_fn_base'] + str(unit) + '.npy'
np.save(output_fn, np.array(st))
def get_plus_minus(rnd):
"""
Returns either -1., or +1. as float.
rnd -- should be your numpy.random RNG
"""
return (rnd.randint(-1, 1) + .5) * 2
#
# def get_input_delay(tuning_prop, params, t, motion_params=None, delay=.00, motion='dot'):
# """
# Similar to get_input but simpler + with a delay
#
# """
# t -= delay
# if motion_params == None:
# motion_params = params['motion_params']
# n_cells = tuning_prop[:, 0].size
# blur_X, blur_V = params['blur_X'], params['blur_V'] #0.5, 0.5
# # get the current stimulus parameters
# x0, y0, u0, v0 = motion_params[0], motion_params[1], motion_params[2], motion_params[3]
# x_stim = (x0 + u0 * t) % params['torus_width']
# y_stim = (y0 + v0 * t) % params['torus_height']
# if params['n_grid_dimensions'] == 2:
# d_ij = torus_distance2D_vec(tuning_prop[:, 0], x_stim * np.ones(n_cells), tuning_prop[:, 1], y_stim * np.ones(n_cells))
# L = np.exp(-.5 * (d_ij)**2 / blur_X**2
# -.5 * (tuning_prop[:, 2] - u0)**2 / blur_V**2
# -.5 * (tuning_prop[:, 3] - v0)**2 / blur_V**2)
# else:
# # d_ij = np.sqrt((tuning_prop[:, 0] - x * np.ones(n_cells))**2)
# d_ij = torus_distance_array(tuning_prop[:, 0], x_stim * np.ones(n_cells))
# L = np.exp(-.5 * (d_ij)**2 / blur_X**2 \
# -.5 * (tuning_prop[:, 2] - u0)**2 / blur_V**2)
# return L
#
#
def get_input(tuning_prop, params, t, motion_params=None, delay=0., delay_compensation=0., contrast=.9, motion='dot'):
"""
This function computes the input to each cell for one point in time t based on the given tuning properties.
Knowing the velocity one can estimate the analytical response to
- motion energy detectors
- to a gaussian blob
as a function of the distance between
- the center of the receptive fields,
- the current position of the blob.
# TODO : prove this analytically to disentangle the different blurs (size of RF / size of dot)
L range between 0 and 1
Arguments:
tuning_prop: 2-dim np.array;
dim 0 is number of cells
tuning_prop[:, 0] : x-position
tuning_prop[:, 1] : y-position
tuning_prop[:, 2] : u-position (speed in x-direction)
tuning_prop[:, 3] : v-position (speed in y-direction)
t: time (NOT in [ms]) in the period (not restricted to 0 .. 1)
motion: type of motion
"""
#t -= delay
if motion_params == None:
motion_params = params['motion_params']
n_cells = tuning_prop[:, 0].size
blur_X, blur_V = params['blur_X'], params['blur_V']
# get the current stimulus parameters
x0, y0, u0, v0 = motion_params[0], motion_params[1], motion_params[2], motion_params[3]
# compensate for sensory delay
x_stim = (x0 + u0 * t + tuning_prop[:, 2] * delay_compensation ) % params['torus_width']
y_stim = (y0 + v0 * t + tuning_prop[:, 3] * delay_compensation ) % params['torus_height']
if motion=='dot':
if params['n_grid_dimensions'] == 2:
d_ij = torus_distance2D_vec(tuning_prop[:, 0], x_stim * np.ones(n_cells), tuning_prop[:, 1], y_stim * np.ones(n_cells))
L = np.exp(-.5 * (d_ij)**2 / blur_X**2
-.5 * (tuning_prop[:, 2] - u0)**2 / blur_V**2
-.5 * (tuning_prop[:, 3] - v0)**2 / blur_V**2)
else:
# d_ij = np.sqrt((tuning_prop[:, 0] - x * np.ones(n_cells))**2)
d_ij = torus_distance_array(tuning_prop[:, 0], x_stim * np.ones(n_cells))
L = np.exp(-.5 * (d_ij)**2 / blur_X**2 \
-.5 * (tuning_prop[:, 2] - u0)**2 / blur_V**2)
elif motion=='bar':
blur_theta = params['blur_theta']
if params['n_grid_dimensions'] == 2:
d_ij = torus_distance2D_vec(tuning_prop[:, 0], x_stim * np.ones(n_cells), tuning_prop[:, 1], y_stim * np.ones(n_cells))
else:
d_ij = torus_distance_array(tuning_prop[:, 0], x_stim * np.ones(n_cells))
# compute the motion energy input to all cells
# then for all cells we have to check if they get stimulate by any x and y on the bar
L = np.exp(-.5 * (d_ij)**2 / blur_X**2
-.5 * (tuning_prop[:, 2] - u0)**2 / blur_V**2
-.5 * (tuning_prop[:, 3] - v0)**2 / blur_V**2
-.5 * (tuning_prop[:, 4] - orientation)**2 / blur_theta**2)
else: # to be implemented: 'oriented dot' (bar)
print 'Unspecified motion in get_input:', motion
return L
# OLD
# define the parameters of the motion
# x0, y0, u0, v0 = motion_params
# blur_X, blur_V = params['blur_X'], params['blur_V'] #0.5, 0.5
# x, y = (x0 + u0*t) % params['torus_width'], (y0 + v0*t) % params['torus_height'] # current position of the blob at time t assuming a perfect translation
# compute the motion energy input to all cells
# L = np.exp(-.5 * ((torus_distance2D_vec(tuning_prop[:, 0], x*np.ones(n_cells), tuning_prop[:, 1], y*np.ones(n_cells)))**2 / blur_X**2)
# -.5 * (tuning_prop[:, 2] - u0)**2 / blur_V**2
# -.5 * (tuning_prop[:, 3] - v0)**2 / blur_V**2
# )
# L = (1. - contrast) + contrast * L
def set_receptive_fields(self, params, tuning_prop):
"""
Can be called only after set_tuning_prop.
Receptive field sizes increase linearly depending on their relative position.
"""
# rfs[:, 0] = params['rf_size_x_gradient'] * np.abs(tuning_prop[:, 0] - .5) + params['rf_size_x_min']
# rfs[:, 1] = params['rf_size_y_gradient'] * np.abs(tuning_prop[:, 1] - .5) + params['rf_size_y_min']
rfs = np.zeros((tuning_prop[:, 0].size, 4))
rfs[:, 0] = params['blur_X']
rfs[:, 1] = params['blur_X']
rfs[:, 2] = params['rf_size_vx_gradient'] * np.abs(tuning_prop[:, 2])# + params['rf_size_vx_min']
rfs[:, 3] = params['rf_size_vy_gradient'] * np.abs(tuning_prop[:, 3])# + params['rf_size_vy_min']
rfs[rfs[:, 2] < params['rf_size_vx_min'], 2] = params['rf_size_vx_min']
rfs[rfs[:, 3] < params['rf_size_vy_min'], 3] = params['rf_size_vy_min']
return rfs
def select_well_tuned_cells_2D_with_orientation(tp, mp, n_cells):
"""
mp -- [x, y, vx, vy, orientation]
"""
w_pos = 10.
x_diff = (tp[:, 0] - mp[0])**2 * w_pos + (tp[:, 1] - mp[1])**2 * w_pos + (tp[:, 2] - mp[2])**2 + (tp[:, 3] - mp[3])**2 + (tp[:, 4] - mp[4])**2
idx_sorted = np.argsort(x_diff)
return idx_sorted[:n_cells]
def select_well_tuned_cells_1D(tp, mp, n_cells):
"""
mp -- [x, y, vx, vy, orientation]
"""
w_pos = 1.
x_diff = torus_distance_array(tp[:, 0], mp[0]) * w_pos + np.abs(tp[:, 2] - mp[2])
idx_sorted = np.argsort(x_diff)
return idx_sorted[:n_cells]
def distribute_list(l, n_proc, pid):
"""
l: list of elements to be distributed among n_proc processors
pid: (int) process id of the process calling this function
n_proc: total number of processors
Returns a list to be assigned to the processor with id pid
"""
n_files = len(l)
n_files_per_proc = int(n_files / n_proc)
R = n_files % n_proc
offset = min(pid, R)
file_id_min = int(pid * n_files_per_proc + offset)
if (pid < R):
file_id_max = file_id_min + n_files_per_proc + 1
else:
file_id_max = file_id_min + n_files_per_proc
sublist = [l[i] for i in range(file_id_min, file_id_max)]
return sublist
def distribute_n(n, n_proc, pid):
"""
l: list of elements to be distributed among n_proc processors
pid: (int) process id of the process calling this function
n_proc: total number of processors
Returns the min and max index to be assigned to the processor with id pid
"""
n_per_proc = int(n / n_proc)
R = n % n_proc
offset = min(pid, R)
n_min = int(pid * n_per_proc + offset)
if (pid < R):
n_max = int(n_min + n_per_proc + 1)
else:
n_max = int(n_min + n_per_proc)
return (n_min, n_max)
def gauss(x, mu, sigma):
return np.exp( - (x - mu)**2 / (2 * sigma ** 2))
def get_time_of_max_stim(tuning_prop, motion_params):
"""
This function assumes motion with constant velocity, starting at x0 y0.
Based on the spatial receptive field (RF: mu_x, mu_y) of the cell (column) the time when the stimulus is closest
to the RF.
t_min = (mu_x * u0 + mu_y * v0 - v0 * y0 + u0 * x0) / (v0**2 + u0**2)
"""
x_i, y_i, u_i, v_i = tuning_prop
x_stim, y_stim, u_stim, v_stim = motion_params
t_min = (u_stim * (x_i - x_stim) + v_stim * (y_i - y_stim)) / (u_stim**2 + v_stim**2)
return t_min
def get_time_of_max_response(spikes, range=None, n_binsizes=1):
"""
For n_binsizes the average max response will be computed.
Average max response is the mean of those bins that have the maximum number of spikes in it.
"""
binsizes = np.linspace(5, 50, n_binsizes)
t_max_depending_on_binsize = np.zeros((n_binsizes, 2))
for i in xrange(n_binsizes):
n_bins = binsizes[i]
n, bins = np.histogram(spikes, bins=n_bins, range=range)
binsize = round(bins[1] - bins[0])
bins_with_max_height = (n == n.max()).nonzero()[0]
times_with_max_response = binsize * bins_with_max_height + .5 * binsize
t_max, t_max_std = times_with_max_response.mean(), times_with_max_response.std()
t_max_depending_on_binsize[i, 0] = t_max
t_max_depending_on_binsize[i, 1] = t_max_std
return t_max_depending_on_binsize[:, 0].mean(), t_max_depending_on_binsize[:, 1].mean()
def set_limited_tuning_properties(params, y_range=(0, 1.), x_range=(0, 1.), u_range=(0, 1.), v_range=(0, 1.), cell_type='exc'):
"""
This function uses the same algorithm as set_tuning_prop, but discards those cells
that are out of the given parameter range.
Purpose of this is to simulate a sub-network of cells with only limited tuning properties
and tune this network.
"""
rnd.seed(params['tuning_prop_seed'])
if cell_type == 'exc':
n_cells = params['n_exc']
n_theta = params['N_theta']
n_v = params['N_V']
n_rf_x = params['N_RF_X']
n_rf_y = params['N_RF_Y']
v_max = params['v_max_tp']
v_min = params['v_min_tp']
else:
n_cells = params['n_inh']
n_theta = params['N_theta_inh']
n_v = params['N_V_INH']
n_rf_x = params['N_RF_X_INH']
n_rf_y = params['N_RF_Y_INH']
if n_v == 1:
v_min = params['v_min_tp'] + .5 * (params['v_max_tp'] - params['v_min_tp'])
v_max = v_min
else:
v_max = params['v_max_tp']
v_min = params['v_min_tp']
tuning_prop = np.zeros((n_cells, 4))
if params['log_scale']==1:
v_rho = np.linspace(v_min, v_max, num=n_v, endpoint=True)
else:
v_rho = np.logspace(np.log(v_min)/np.log(params['log_scale']),
np.log(v_max)/np.log(params['log_scale']), num=n_v,
endpoint=True, base=params['log_scale'])
v_theta = np.linspace(0, 2*np.pi, n_theta, endpoint=False)
parity = np.arange(params['N_V']) % 2
RF = np.zeros((2, n_rf_x * n_rf_y))
X, Y = np.mgrid[0:1:1j*(n_rf_x+1), 0:1:1j*(n_rf_y+1)]
# It's a torus, so we remove the first row and column to avoid redundancy (would in principle not harm)
X, Y = X[1:, 1:], Y[1:, 1:]
# Add to every even Y a half RF width to generate hex grid
Y[::2, :] += (Y[0, 0] - Y[0, 1])/2 # 1./N_RF
RF[0, :] = X.ravel()
RF[1, :] = Y.ravel()
# wrapping up:
index = 0
random_rotation = 2*np.pi*rnd.rand(n_rf_x * n_rf_y) * params['sigma_RF_direction']
neuron_in_range = np.zeros((n_cells, 1), dtype=bool)
for i_RF in xrange(n_rf_x * n_rf_y):
for i_v_rho, rho in enumerate(v_rho):
for i_theta, theta in enumerate(v_theta):
x_pos = (RF[0, i_RF] + params['sigma_RF_pos'] * rnd.randn()) % params['torus_width']
y_pos = (RF[1, i_RF] + params['sigma_RF_pos'] * rnd.randn()) % params['torus_height']
v_x = np.cos(theta + random_rotation[i_RF] + parity[i_v_rho] * np.pi / n_theta) \
* rho * (1. + params['sigma_RF_speed'] * rnd.randn())
v_y = np.sin(theta + random_rotation[i_RF] + parity[i_v_rho] * np.pi / n_theta) \
* rho * (1. + params['sigma_RF_speed'] * rnd.randn())
tuning_prop[index, 0] = x_pos
tuning_prop[index, 1] = y_pos
tuning_prop[index, 2] = v_x
tuning_prop[index, 3] = v_y
if ((x_pos > x_range[0]) and (x_pos <= x_range[1]) \
and (y_pos > y_range[0]) and (y_pos <= y_range[1]) \
and (v_x > u_range[0]) and (v_x <= u_range[1]) \
and (v_y > v_range[0]) and (v_y <= v_range[1])):
neuron_in_range[index] = True
index += 1
n_cells_in_range = neuron_in_range.nonzero()[0].size
tp_good = np.zeros((n_cells_in_range, 4))
tp_good = tuning_prop[neuron_in_range.nonzero()[0], :]
idx_out_of_range = np.ones((n_cells, 1), dtype=int)
idx_out_of_range -= neuron_in_range
n_cells_out_of_range = idx_out_of_range.nonzero()[0].size
assert (n_cells_out_of_range + n_cells_in_range == n_cells), 'Number of cells in/out of range do not sum to one'
tp_out_of_range = np.zeros((n_cells_out_of_range, 4))
tp_out_of_range = tuning_prop[idx_out_of_range, :]
return tp_good, tp_out_of_range
def set_tuning_prop(params, mode, cell_type):
if params['n_grid_dimensions'] == 2:
return set_tuning_prop_2D(params, mode, cell_type)
else:
return set_tuning_prop_1D(params, cell_type)
def set_tuning_prop_1D(params, cell_type='exc'):
rnd.seed(params['tuning_prop_seed'])
if cell_type == 'exc':
n_cells = params['n_exc']
n_v = params['N_V']
n_rf_x = params['N_RF_X']
v_max = params['v_max_tp']
v_min = params['v_min_tp']
else:
n_cells = params['n_inh']
n_v = params['N_V_INH']
n_rf_x = params['N_X_INH']
v_max = params['v_max_tp']
v_min = params['v_min_tp']
tuning_prop = np.zeros((n_cells, 4))
if params['log_scale']==1:
v_rho = np.linspace(v_min, v_max, num=n_v, endpoint=True)
else:
v_rho = np.logspace(np.log(v_min)/np.log(params['log_scale']),
np.log(v_max)/np.log(params['log_scale']), num=n_v,
endpoint=True, base=params['log_scale'])
n_orientation = params['n_orientation']
orientations = np.linspace(0, np.pi, n_orientation, endpoint=False)
xlim = (0, params['torus_width'])
RF = np.linspace(0, params['torus_width'], n_rf_x, endpoint=False)
index = 0
random_rotation_for_orientation = np.pi*rnd.rand(n_rf_x * n_v * n_orientation) * params['sigma_RF_orientation']
for i_RF in xrange(n_rf_x):
for i_v_rho, rho in enumerate(v_rho):
for orientation in orientations:
for i_cell in xrange(params['n_exc_per_mc']):
tuning_prop[index, 0] = (RF[i_RF] + params['sigma_RF_pos'] * rnd.randn()) % params['torus_width']
tuning_prop[index, 1] = 0.
tuning_prop[index, 2] = get_plus_minus(rnd) * rho * (1. + params['sigma_RF_speed'] * rnd.randn())
tuning_prop[index, 3] = 0.
index += 1
if index != n_cells:
print '\nWARNING\n: \tutils.set_tuning_prop_1D mismatch between n_cells=%d and index=%d of tuning properties set for cell type %s\n\n' % (n_cells, index, cell_type)
return tuning_prop
def set_tuning_prop_2D(params, mode='hexgrid', cell_type='exc'):
"""
Place n_exc excitatory cells in a 4-dimensional space by some mode (random, hexgrid, ...).
The position of each cell represents its excitability to a given a 4-dim stimulus.
The radius of their receptive field is assumed to be constant (TODO: one coud think that it would depend on the density of neurons?)
return value:
tp = set_tuning_prop(params)
tp[:, 0] : x-position
tp[:, 1] : y-position
tp[:, 2] : u-position (speed in x-direction)
tp[:, 3] : v-position (speed in y-direction)
All x-y values are in range [0..1]. Positios are defined on a torus and a dot moving to a border reappears on the other side (as in Pac-Man)
By convention, velocity is such that V=(1,0) corresponds to one horizontal spatial period in one temporal period.
This implies that in one frame, a translation is of ``1. / N_frame`` in cortical space.
"""
rnd.seed(params['tuning_prop_seed'])
if cell_type == 'exc':
n_cells = params['n_exc']
n_theta = params['N_theta']
n_v = params['N_V']
n_rf_x = params['N_RF_X']
n_rf_y = params['N_RF_Y']
v_max = params['v_max_tp']
v_min = params['v_min_tp']
else:
n_cells = params['n_inh']
n_theta = params['N_theta_inh']
n_v = params['N_V_INH']
n_rf_x = params['N_RF_X_INH']
n_rf_y = params['N_RF_Y_INH']
if n_v == 1:
v_min = params['v_min_tp'] + .5 * (params['v_max_tp'] - params['v_min_tp'])
v_max = v_min
else:
v_max = params['v_max_tp']
v_min = params['v_min_tp']
tuning_prop = np.zeros((n_cells, 4))
if mode=='random':
# place the columns on a grid with the following dimensions
x_max = int(round(np.sqrt(n_cells)))
y_max = int(round(np.sqrt(n_cells)))
if (params['n_cells'] > x_max * y_max):
x_max += 1
for i in xrange(params['n_cells']):
tuning_prop[i, 0] = (i % x_max) / float(x_max) # spatial rf centers are on a grid
tuning_prop[i, 1] = (i / x_max) / float(y_max)
tuning_prop[i, 2] = v_max * rnd.randn() + v_min
tuning_prop[i, 3] = v_max * rnd.randn() + v_min
elif mode=='hexgrid':
if params['log_scale']==1:
v_rho = np.linspace(v_min, v_max, num=n_v, endpoint=True)
else:
v_rho = np.logspace(np.log(v_min)/np.log(params['log_scale']),
np.log(v_max)/np.log(params['log_scale']), num=n_v,
endpoint=True, base=params['log_scale'])
v_theta = np.linspace(0, 2*np.pi, n_theta, endpoint=False)
parity = np.arange(params['N_V']) % 2
xlim = (0, params['torus_width'])
ylim = (0, np.sqrt(3) * params['torus_height'])
RF = np.zeros((2, n_rf_x * n_rf_y))
X, Y = np.mgrid[0:1:1j*(n_rf_x+1), 0:1:1j*(n_rf_y+1)]
X, Y = np.mgrid[xlim[0]:xlim[1]:1j*(n_rf_x+1), ylim[0]:ylim[1]:1j*(n_rf_y+1)]
# It's a torus, so we remove the first row and column to avoid redundancy (would in principle not harm)
X, Y = X[1:, 1:], Y[1:, 1:]
if n_rf_y > 1:
# Add to every even Y a half RF width to generate hex grid
Y[::2, :] += (Y[0, 0] - Y[0, 1])/2 # 1./N_RF
RF[0, :] = X.ravel()
RF[1, :] = Y.ravel()
RF[1, :] /= np.sqrt(3) # scale to get a regular hexagonal grid
# wrapping up:
index = 0
random_rotation = 2*np.pi*rnd.rand(n_rf_x * n_rf_y * n_v * n_theta) * params['sigma_RF_direction']
# todo do the same for v_rho?
for i_RF in xrange(n_rf_x * n_rf_y):
for i_v_rho, rho in enumerate(v_rho):
for i_theta, theta in enumerate(v_theta):
# for plotting this looks nicer, and due to the torus property it doesn't make a difference
tuning_prop[index, 0] = (RF[0, i_RF] + params['sigma_RF_pos'] * rnd.randn())# % params['torus_width']
tuning_prop[index, 1] = (RF[1, i_RF] + params['sigma_RF_pos'] * rnd.randn())# % params['torus_height']
tuning_prop[index, 2] = np.cos(theta + random_rotation[index] + parity[i_v_rho] * np.pi / n_theta) \
* rho * (1. + params['sigma_RF_speed'] * rnd.randn())
tuning_prop[index, 3] = np.sin(theta + random_rotation[index] + parity[i_v_rho] * np.pi / n_theta) \
* rho * (1. + params['sigma_RF_speed'] * rnd.randn())
index += 1
return tuning_prop
def set_hexgrid_positions(params, NX, NY):
RF = np.zeros((2, NX*NY))
X, Y = np.mgrid[0:1:1j*(NX+1), 0:1:1j*(NY+1)]
# It's a torus, so we remove the first row and column to avoid redundancy (would in principle not harm)
X, Y = X[1:, 1:], Y[1:, 1:]
# Add to every even Y a half RF width to generate hex grid
Y[::2, :] += (Y[0, 0] - Y[0, 1])/2 # 1./N_RF
RF[0, :] = X.ravel()
RF[1, :] = Y.ravel()
for i in xrange(RF[0, :].size):
RF[0, i] *= (1. + params['sigma_RF_pos'] * rnd.randn())
RF[1, i] *= (1. + params['sigma_RF_pos'] * rnd.randn())
return RF.transpose()
def get_predicted_stim_pos(tp):
"""
For each cell this function calculates the target position based on the tuning_prop of the cell:
x_predicted = (x_0 + v_0) % 1
"""
n_pos = tp[:, 0].size
pos = np.zeros((n_pos, 2))
for i in xrange(n_pos):
x_predicted = (tp[i, 0] + tp[i, 2]) % 1
y_predicted = (tp[i, 1] + tp[i, 3]) % 1
pos[i, 0], pos[i, 1] = x_predicted, y_predicted
return pos
def spatial_readout(particles, N_X, N_Y, hue, hue_zoom, fig_width, width, ywidth, display=True):
"""
Reads-out particles into a probability density function in spatial space.
Instead of a quiver plot, it makes an histogram of the density of particles by: 1) transforming a particle set in a 3 dimensional (x,y, \theta) density (let's forget about speed norm), (2) showing direction spectrum as hue and spatial density as transparency
Marginalization over all speeds.
Input
-----
N particles as a particles.shape array
Output
------
a position PDF
"""
x = particles[0, ...].ravel()
y = particles[1, ...].ravel()
# we weight the readout by the weight of the particles
weights = particles[4, ...].ravel()
x_edges = np.linspace(0., 1., N_X)
y_edges = np.linspace(0., 1., N_Y)
if hue:
N_theta_=3 # the 3 RGB channels
# TODO : velocity angle histogram as hue
u = particles[3, ...].ravel()
v = particles[2, ...].ravel()
# TODO: rotate angle because we are often going on the horizontal on the right /// check in other plots, hue = np.arctan2(u+v, v-u)/np.pi/2 + .5 /// np.arctan2 is in the [np.pi, np.pi] range, cm.hsv takes an argument in [0, 1]
v_theta = np.arctan2(u+v, v-u)
if hue_zoom:
v_theta_edges = np.linspace(-np.pi/4-np.pi/8, -np.pi/4+np.pi/8, N_theta_ + 1 )
else:
v_theta_edges = np.linspace(-np.pi, np.pi, N_theta_ + 1 )# + pi/N_theta
sample = np.hstack((x[:,np.newaxis], y[:,np.newaxis], v_theta[:,np.newaxis]))
# print v_theta.shape, sample.shape
bin_edges = (x_edges, y_edges, v_theta_edges)
# print np.histogramdd(sample, bins =bin_edges, normed=True, weights=weights)
v_hist, edges_ = np.histogramdd(sample, bins=bin_edges, normed=True, weights=weights)
v_hist /= v_hist.sum()
else:
v_hist, x_edges_, y_edges_ = np.histogram2d(x, y, (x_edges, y_edges), normed=True, weights=weights)
v_hist /= v_hist.sum()
# print fig_width * np.float(N_Y) / N_X, width * np.float(N_Y) / N_X
ywidth = width * np.float(N_Y) / N_X
if display:
# print fig_width, fig_width * np.float(N_Y) / N_X
# fig = pylab.figure()#figsize=(fig_width, fig_width * np.float(N_Y) / N_X))
# BK: the following code has been modified to remove import pylab from the utils.py file in order to run it on the cluster
fig = None
a = fig.add_axes([0., 0., 1., 1.])
if hue:
# TODO : overlay image and use RGB(A) information
# print v_hist, v_hist.min(), v_hist.max(), np.flipud( np.fliplr(im_).T
# a.imshow(-np.log(np.rot90(v_hist)+eps_hist), interpolation='nearest')
a.imshow(np.fliplr(np.rot90(v_hist/v_hist.max(),3)), interpolation='nearest', origin='lower', extent=(-width/2, width/2, -ywidth/2., ywidth/2.))#, vmin=0., vmax=v_hist.max())
# pylab.axis('image')
else:
# a.pcolor(x_edges, y_edges, v_hist, cmap=pylab.bone(), vmin=0., vmax=v_hist.max(), edgecolor='k')
pass
a.axis([-width/2, width/2, -ywidth/2., ywidth/2.])
return fig, a
else:
return v_hist, x_edges, y_edges
def threshold_weights(connection_matrix, w_thresh):
"""
Elements in connection_matrix below w_thresh will be set to zero.
"""
for i in xrange(connection_matrix[:, 0].size):
for j in xrange(connection_matrix[0, :].size):
if connection_matrix[i, j] < w_thresh:
connection_matrix[i, j] = 0.0
return connection_matrix
def get_spiketimes(all_spikes, gid, gid_idx=0, time_idx=1):
"""
Returns the spikes fired by the cell with gid the
all_spikes: 2-dim array containing all spiketimes (raw data, if NEST: gids are 1-aligned)
gid_idx: is the column index in the all_spikes array containing GID information
time_idx: is the column index in the all_spikes array containing time information
"""
if all_spikes.size == 0:
return np.array([])
else:
idx_ = (all_spikes[:, gid_idx] == gid).nonzero()[0]
spiketimes = all_spikes[idx_, time_idx]
return spiketimes
def get_nspikes(spiketimes_fn_merged, n_cells=0, get_spiketrains=False):
"""
Returns an array with the number of spikes fired by each cell.
nspikes[gid]
if n_cells is not given, the length of the array will be the highest gid (not advised!)
"""
d = np.loadtxt(spiketimes_fn_merged)
if (n_cells == 0):
n_cells = 1 + int(np.max(d[:, 1]))# highest gid
nspikes = np.zeros(n_cells)
spiketrains = [[] for i in xrange(n_cells)]
if (d.size == 0):
if get_spiketrains:
return nspikes, spiketrains
else:
return spiketrains
# seperate spike trains for all the cells
if d.shape == (2,):
nspikes[int(d[1])] = 1
spiketrains[int(d[1])] = [d[0]]
else:
# for i in xrange(d[:, 0].size):
# spiketrains[int(d[i, 1])].append(d[i, 0])
for gid in xrange(n_cells):
idx = d[:, 1] == gid
spiketrains[gid] = d[idx, 0]
nspikes[gid] = d[idx, 0].size
if get_spiketrains:
return nspikes, spiketrains
else:
return nspikes
def get_connection_center_of_mass(connection_gids, weights, tp):
cms = [0., 0.]
tp_conn = tp[connection_gids, :]
M = np.sum(weights)
cms[0] = np.sum(tp_conn[:, 0] * weights) / M
cms[1] = np.sum(tp_conn[:, 2] * weights) / M
return cms
def get_sources(conn_list, target_gid):
idx = conn_list[:, 1] == target_gid
sources = conn_list[idx, :]
return sources
def get_targets(conn_list, source_gid):
idx = conn_list[:, 0] == source_gid
targets = conn_list[idx, :]
return targets
def get_cond_in(nspikes, conn_list, target_gid):
cond_in = 0.
srcs = get_sources(conn_list, target_gid)
for i in xrange(len(srcs)):
src_id = srcs[i, 0]
cond_in += nspikes[src_id] * srcs[i, 2]
return cond_in
def get_spiketrains(spiketimes_fn_or_array, n_cells=0):
"""
Returns an list of spikes fired by each cell
if n_cells is not given, the length of the array will be the highest gid (not recommended!)
"""
if type(spiketimes_fn_or_array) == type(np.array([])):
d = spiketimes_fn_or_array
else:
d = np.loadtxt(spiketimes_fn_or_array)
if (n_cells == 0):
n_cells = 1 + np.max(d[:, 1])# highest gid
spiketrains = [[] for i in xrange(n_cells)]
# seperate spike trains for all the cells
if d.size == 0:
return spiketrains
elif d.shape == (2,):
spiketrains[int(d[1])] = [d[0]]
else:
for i in xrange(d[:, 0].size):
spiketrains[int(d[i, 1])].append(d[i, 0])
return spiketrains
def get_grid_pos(x0, y0, xedges, yedges):
x_index, y_index = len(xedges)-1, len(yedges)-1
for (ix, x) in enumerate(xedges[1:]):
if x0 <= x:
x_index = ix
break
for (iy, y) in enumerate(yedges[1:]):
if y0 <= y:
y_index = iy
break
return (x_index, y_index)
def get_grid_pos_1d(x0, xedges):
x_index = len(xedges)-1
for (ix, x) in enumerate(xedges[1:]):
if x0 <= x:
x_index = ix
break
return x_index
def convert_hsl_to_rgb(h, s, l):
"""
h : [0, 360) degree
s : [0, 1]
l : [0, 1]
returns (r,g,b) tuple with values in range [0, 1]
Source of the formula: http://en.wikipedia.org/wiki/HSV_color_space#Conversion_from_RGB_to_HSL_or_HSV
"""
c = (1. - np.abs(2*l - 1.)) * s # c = chroma
h_ = h / 60.
x = c * (1 - np.abs(h_ % 2 - 1))
if 0 <= h_ and h_ < 1:
r, g, b = c, x, 0
elif 1 <= h_ and h_ < 2:
r, g, b = x, c, 0
elif 2 <= h_ and h_ < 3:
r, g, b = 0, c, x
elif 3 <= h_ and h_ < 4:
r, g, b = 0, x, c
elif 4 <= h_ and h_ < 5:
r, g, b = x, 0, c
elif 5 <= h_ and h_ < 6:
r, g, b = c, 0, x
else:
r, g, b = 0, 0, 0
# match lightness
m = l - .5 * c
r_, g_, b_ = r+m, g+m, b+m
# avoid negative values due to precision problems
r_ = max(r_, 0)
g_ = max(g_, 0)
b_ = max(b_, 0)
return (r_, g_, b_)
def sort_gids_by_distance_to_stimulus(tp, mp, params, local_gids=None):
"""
This function return a list of gids sorted by the distances between cells and the stimulus.
It calculates the minimal distances between the moving stimulus and the spatial receptive fields of the cells
and adds the distances between the motion_parameters and the tuning_properties of each cell.
Arguments:
tp: tuning_properties array
tp[:, 0] : x-pos
tp[:, 1] : y-pos
tp[:, 2] : x-velocity
tp[:, 3] : y-velocity
mp: motion_parameters (x0, y0, u0, v0)
"""
if local_gids == None:
n_cells = tp[:, 0].size
else:
n_cells = len(local_gids)
x_dist = np.zeros(n_cells) # stores minimal distance in space between stimulus and cells
for i in xrange(n_cells):
x_dist[i], spatial_dist = get_min_distance_to_stim(mp, tp[i, :], params)
cells_closest_to_stim_pos = x_dist.argsort()
if local_gids != None:
gids_closest_to_stim = local_gids[cells_closest_to_stim_pos]
return gids_closest_to_stim, x_dist[cells_closest_to_stim_pos]#, cells_closest_to_stim_velocity
else:
return cells_closest_to_stim_pos, x_dist[cells_closest_to_stim_pos]#, cells_closest_to_stim_velocity
def get_min_distance_to_stim(mp, tp_cell, params):
"""
mp : motion_parameters (x,y,u,v)
tp_cell : same format as mp
n_steps: steps for calculating the motion path
"""
try:
if params['abstract'] == False:
time = np.arange(0, params['t_sim'], params['dt_rate'])
else:
time = np.arange(0, params['t_sim'], 50 * params['dt_rate'])
except:# use larger time step to numerically find minimum distance --> faster
time = np.arange(0, params['t_sim'], 50 * params['dt_rate'])
spatial_dist = np.zeros(time.shape[0])
x_pos_stim = mp[0] + mp[2] * time / params['t_stimulus']
y_pos_stim = mp[1] + mp[3] * time / params['t_stimulus']
spatial_dist = torus_distance_array(tp_cell[0], x_pos_stim)**2 + torus_distance_array(tp_cell[1], y_pos_stim)**2
min_spatial_dist = np.sqrt(np.min(spatial_dist))
velocity_dist = np.sqrt((tp_cell[2] - mp[2])**2 + (tp_cell[3] - mp[3])**2)
dist = min_spatial_dist + velocity_dist