-
Notifications
You must be signed in to change notification settings - Fork 1
/
inputs.py
284 lines (208 loc) · 6.35 KB
/
inputs.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 27 16:28:09 2016
@author: tomasz, Bartosz, Romain
"""
from __future__ import print_function
import numpy as np
import numba
from scipy import stats
import math
import pylab as plt
#from PoissonPointProcess import SNCP_corr, corr_poisson, CCVF
def generate_poisson_old(freq, tmax):
"""Draw spike times from Poisson distribution"""
t = np.cumsum(stats.expon.rvs(scale=1/freq, size=round(freq * tmax)))
t = t[:t.searchsorted(tmax)]
return t
# this function might be slower than the previous implementation - please check
def generate_poisson(freq, tmax):
return np.sort(np.random.uniform(0,tmax,np.random.poisson(lam = freq*tmax)))
def generate_correlated(mother, r):
"""Generate correlated spike train from a mother spike train
process"""
rn = np.random.rand(len(mother))
return mother[rn<r]
def jitter_spikes(spt, tau):
"""add normaly distributed jitter to spike trains"""
delta_t = stats.expon.rvs(scale=tau, size=len(spt))
# to have symmetric distribution around 0 uncomment the following lines
# sign = (np.random.rand(len(spt)) > 0.5) * 2 - 1
# delta_t = sign * delta_t
return np.sort(spt + delta_t)
def spike_trains_hierarch(nb_comp, nb_syn, rate, tmax, corr_coef_L, corr_coef_G, jtr):
N = rate * tmax
if corr_coef_L == 0. and corr_coef_G == 0.:
spike_trains = [[np.sort(generate_poisson(rate, tmax)) for i in range(nb_syn)] for j in range(nb_comp)]
return spike_trains
if corr_coef_G == 0.:
r_LC = corr_coef_L
M = round(N / r_LC)
freq_mother = float(M) / tmax
mother_loc = [np.sort(generate_poisson(freq_mother, tmax)) for j in range(nb_comp)]
spike_trains = [[jitter_spikes(generate_correlated(mother_loc[j], r_LC), jtr) for i in range(nb_syn)] for j
in range(nb_comp)]
return spike_trains
else:
if corr_coef_L < 0. or corr_coef_L > 1.:
raise ValueError('\n\n *** Local correlation outside [0,1]! ***')
if corr_coef_G < 0. or corr_coef_G > 1.:
raise ValueError('\n\n *** Global correlation outside [0,1]! ***')
if corr_coef_L < corr_coef_G:
raise ValueError('\n\n *** Global correlation cannot be lower than local correlation! ***')
if corr_coef_G == 0.:
"print generation"
r_LC = corr_coef_L
M = round(N / r_LC)
freq_mother = float(M) / tmax
mother_loc = [np.sort(generate_poisson(freq_mother, tmax)) for j in range(nb_comp)]
spike_trains = [[jitter_spikes(generate_correlated(mother_loc[j], r_LC), jtr) for i in range(nb_syn)] for j
in range(nb_comp)]
return spike_trains
r_LC = corr_coef_L
r_GL = corr_coef_G / corr_coef_L
M = round(N / (r_GL * r_LC))
freq_mother = float(M) / tmax
mother_glob = generate_poisson(freq_mother, tmax)
mother_loc = [generate_correlated(mother_glob, r_GL) for j in range(nb_comp)]
spike_trains = [[jitter_spikes(generate_correlated(mother_loc[j], r_LC), jtr) for i in range(nb_syn)] for j in range(nb_comp)]
return spike_trains
# def spike_trains_hierarch_ind_global_bad(nc, ns, rate, tmax, rL, rG, jtr):
#
# N = rate * tmax
#
# M = round(N * nc * ns / (1 + rG * (nc - 1) + rL * (ns - 1)))
#
# fM = M / tmax
#
# ts = generate_poisson(fM, tmax)
#
# C = np.random.randint(nc, size=len(ts))
#
# S = np.random.randint(ns, size=len(ts))
#
# ts_loc = [[[ts[i]], [[C[i], S[i]]]] for i in range(len(ts))]
#
#
# for i in range(len(ts_loc)):
#
# for s in range(ns):
#
# if s != ts_loc[i][1][0][1]:
#
# r = np.random.rand()
#
# if r < rL:
#
# ts_loc[i][1] = ts_loc[i][1] + [[ts_loc[i][1][0][0],s]]
#
# for c in range(nc):
#
# if c != ts_loc[i][1][0][0]:
#
# r = np.random.rand()
#
# if r < rG:
#
# ts_loc[i][1] = ts_loc[i][1] + [[c, np.random.randint(ns)]]
#
#
# ts_loc_a = [[[] for i in range(ns)] for j in range(nc)]
#
# total_number = 0
#
# for i in range(len(ts_loc)):
#
# for j in range(len(ts_loc[i][1])):
# ts_loc_a[ts_loc[i][1][j][0]][ts_loc[i][1][j][1]] = ts_loc_a[ts_loc[i][1][j][0]][ts_loc[i][1][j][1]] + ts_loc[i][0]
#
# total_number = total_number + len(ts_loc[i][0])
#
# print('Number of spikes: ' + str(total_number))
# print('Number of spikes per synapse: ' + str(total_number/(nc * ns)))
# print(N)
#
# return ts_loc_a
def connect_matrix(t_length, nc, ns, rL, rG):
from random import shuffle
C = np.random.randint(nc, size=t_length)
mat0 = np.zeros((t_length, nc * ns))
glob_array = [rG] + [0] * (ns - 1)
loc_array = [1.] + [rL] * (ns - 1)
for i in range(t_length):
shuffle(glob_array)
shuffle(loc_array)
mat0[i, :] = glob_array * nc
mat0[i, (C[i])*ns : (C[i]+1) * ns] = loc_array
return mat0
def generate_correlated_new(mother, vec):
rn = np.random.rand(len(mother))
return mother[rn<vec]
def spike_trains_hierarch_ind_global(nc, ns, rate, tmax, rL, rG, jtr):
N = rate * tmax
M = round(N * nc * ns / (1 + rG * (nc - 1) + rL * (ns - 1)))
fM = M / tmax
ts = generate_poisson(fM, tmax)
cm = connect_matrix(len(ts), nc, ns, rL, rG)
spike_train = [[jitter_spikes(generate_correlated_new(ts, cm[:,j*ns + i]), jtr) for i in range(ns)] for j in range(nc)]
return spike_train
# import time
#
# nc = 3
# ns = 2
#
# tmax = 1
#
# rate = 1
#
# rL = 1.
#
# rG = 0.5
#
# jtr = 0.1
#
#
# t0 = time.time()
#
# sp1 = spike_trains_hierarch_ind_global(nc, ns, rate, tmax, rL, rG, jtr)
#
# print('new new method: ' + str(time.time() - t0))
#
# print(len(sp1[0][0]))
#
#
#
#
# import matplotlib.pyplot as plt
#
# plt.figure()
#
# for i in range(len(sp1)):
#
# for j in range(len(sp1[0])):
#
# plt.plot([i, sp1[i][j][0]],'+')
#
# plt.show()
#
#
#
#
#
# t0 = time.time()
#
# sp2 = spike_trains_hierarch_ind_global_bad(nc, ns, rate, tmax, rL, rG, jtr)
#
# print('new method: ' + str(time.time() - t0))
#
# print(len(sp2[0][0]))
#
#
#
# t0 = time.time()
#
# sp3 = spike_trains_hierarch(nc, ns, rate, tmax, rL, rG, jtr)
#
# print('old method: ' + str(time.time() - t0))
#
# print(len(sp3[0][0]))