-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsynapseseg.py
350 lines (289 loc) · 13.9 KB
/
synapseseg.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
from imports import *
from HRneuronseg import *
c = constants()
class synapseseg:
def __init__(self, neurons_list):
self.neurons = neurons_list
self.neuron_dict = {}
self.t1 = 0
self.t2 = 0
self.testarr = []
self.testarr2 = []
self.warr = []
self.warr2 = []
self.peaks_arr = []
self.time_arr = []
self.time_arr1 = []
self.time_arr2 = []
self.everything1 = []
self.everything2 = []
self.swaps = []
self.peaks_arrx = []
self.time_arrx = []
self.time_arr1x = []
self.time_arr2x = []
self.everything1x = []
self.everything2x = []
self.swapsx = []
for neuron in self.neurons:
self.neuron_dict.update({str(neuron.id): neuron})
def add_neuron(self, neuron):
if neuron not in self.neurons:
self.neurons.append(neuron)
self.neuron_dict.update({str(neuron.id): neuron})
def attach_neurons(self, neuron, neuron_att):
neuron.add_connections(neuron_att.id)
def get_neuron(self, id):
return (self.neuron_dict.get(str(id)))
def calculate_x(self, neuron, time):
connections = neuron.get_connections()
dx = neuron.calculate_init_dx()
if (len(connections) > 0):
for i in connections:
dx -= neuron.g * (neuron.x - neuron.v_syn) * \
self.get_neuron(i).sigmoid
neuron.update_x(time, dx)
def calculate_max_x(self, neuron, time):
connections = neuron.get_connections()
dx = neuron.calculate_init_dx()
if (len(connections) > 0):
for i in connections:
dx -= neuron.g * (neuron.x - neuron.v_syn) * \
self.get_neuron(i).sigmoid
neuron.update_x(time, dx)
def calculate_sensory_x(self, neuron, time, gain, sensory, weight):
# - weight = excitatory, +weight = inhibitory
connections = neuron.get_connections()
dx = neuron.calculate_sensory_dx(gain, sensory)
if (len(connections) > 0):
for i in connections:
dx -= weight * (neuron.x - neuron.v_syn) * \
self.get_neuron(i).sigmoid
neuron.update_x(time, dx)
def calculate_weight_x(self, neuron, time):
connections = neuron.get_connections()
dx = neuron.calculate_init_dx()
if (len(connections) > 0):
for i in range(len(connections)):
dx -= neuron.weights[i] * (neuron.x - neuron.v_syn) * \
self.get_neuron(connections[i]).sigmoid
neuron.update_x(time, dx)
def calculate_all(self, time):
for neuron in self.neurons:
self.calculate_x(neuron, time)
neuron.calculate_y(time)
neuron.calculate_z(time)
neuron.sig_func(time)
def calculate_max_all(self, time):
for neuron in self.neurons:
self.calculate_x(neuron, time)
neuron.calculate_y(time)
neuron.calculate_z(time)
neuron.max_sig_func(time)
def calculate_sensory_all(self, time, gain, sensory, id):
for neuron in self.neurons:
if (neuron.id == id):
self.calculate_sensory_x(neuron, time, gain, sensory, 1.0)
else:
self.calculate_x(neuron, time)
neuron.calculate_y(time)
neuron.calculate_z(time)
neuron.sig_func(time)
def create_weight_mat(self, neuron, weight_list):
neuron.update_weights(weight_list)
def calculate_weight_all(self, time):
for neuron in self.neurons:
self.calculate_weight_x(neuron, time)
neuron.calculate_y(time)
neuron.calculate_z(time)
neuron.sig_func(time)
def update_dg(self, time, const):
dg = const * np.abs(self.neurons[0].current - self.neurons[1].current)
for neuron in self.neurons:
neuron.update_g(time, dg)
def calculate_if_peak(self, neuron, time):
if (time < 2):
return False
neg_deriv = neuron.xarr[time] - neuron.xarr[time-1] < 0
pos_deriv = neuron.xarr[time-1] - neuron.xarr[time-2] > 0
if (neg_deriv and pos_deriv and neuron.xarr[time] > 1):
return True
return False
def calculate_if_anti_peak(self, neuron, time):
if (time < 2):
return False
neg_deriv = neuron.xarr[time] - neuron.xarr[time-1] < 0
pos_deriv = neuron.xarr[time-1] - neuron.xarr[time-2] > 0
if (neg_deriv and pos_deriv and neuron.xarr[time] < 0):
return True
return False
def update_dg_peaks(self, time, const):
dg = 0
print(self.everything1)
print(self.everything2)
peak = self.calculate_if_peak(self.neurons[0], time)
# anti_peak = self.calculate_if_anti_peak(self.neurons[0], time)
anti_peak = self.calculate_if_peak(self.neurons[1], time)
if (peak and time > 20000):
self.time_arr.append(time)
self.time_arr1.append(time)
if (len(self.peaks_arr) > 0):
if (self.peaks_arr[-1] == -1):
self.swaps.append(time)
if (len(self.swaps) >= 3):
self.everything1.append(
self.swaps[-1] - self.swaps[-2])
self.everything2.append(
self.swaps[-2] - self.swaps[-3])
dg = (
np.exp(-1 * const * (((self.everything1[-1] - self.everything2[-1])*c.scale))) - np.exp(c.kappa * const))
self.peaks_arr.append(1)
if (anti_peak and time > 20000):
self.time_arr.append(time)
self.time_arr2.append(time)
if (len(self.peaks_arr) > 0):
if (self.peaks_arr[-1] == 1):
self.swaps.append(time)
self.peaks_arr.append(-1)
self.neurons[0].update_g(time, dg)
def update_dg_peaks2(self, time, const):
dg = 0
print(self.everything1x)
print(self.everything2x)
peak = self.calculate_if_peak(self.neurons[2], time)
# anti_peak = self.calculate_if_anti_peak(self.neurons[0], time)
anti_peak = self.calculate_if_peak(self.neurons[1], time)
if (peak and time > 20000):
self.time_arrx.append(time)
self.time_arr1x.append(time)
if (len(self.peaks_arrx) > 0):
if (self.peaks_arrx[-1] == -1):
self.swapsx.append(time)
if (len(self.swapsx) >= 3):
self.everything1x.append(
self.swapsx[-1] - self.swapsx[-2])
self.everything2x.append(
self.swapsx[-2] - self.swapsx[-3])
dg = (
np.exp(-1 * const * (((self.everything1x[-1] - self.everything2x[-1])*c.scale))) - np.exp(c.kappax * const))
self.peaks_arrx.append(1)
if (anti_peak and time > 20000):
self.time_arrx.append(time)
self.time_arr2x.append(time)
if (len(self.peaks_arrx) > 0):
if (self.peaks_arrx[-1] == 1):
self.swapsx.append(time)
self.peaks_arrx.append(-1)
self.neurons[2].update_gx(time, dg)
def update_new_dg2(self, time, const):
temp1 = []
temp2 = []
for i in range(int(len(self.neurons[0].zarr)/10000)):
temp1.append(self.neurons[0].zarr[10000 * i])
for i in range(int(len(self.neurons[1].zarr)/10000)):
temp2.append(self.neurons[1].zarr[10000 * i])
dg = 0
#temp1[int(time/10000)] - temp1[int(time/10000)-1], temp1[int(time/10000)-1] - temp1[int(time/10000)-2],
print(self.testarr, self.warr)
print(self.testarr2, self.warr2)
if (True):
# if (temp1[int(time/10000)] - temp1[int(time/10000)-1] < 0):
# self.t1 +=10000
if ((temp1[int(time/10000)] - temp1[int(time/10000)-1] > 0 and temp1[int(time/10000)-1] - temp1[int(time/10000)-2] < 0)):
if ((self.neurons[0].zarr[time] - self.neurons[0].zarr[time-1] > 0 and self.neurons[0].zarr[time-1] - self.neurons[0].zarr[time-2] < 0)):
# print("HI")
if (self.t1 > 10000 and len(self.warr) > 0):
# dg = -1 * const * ((self.t1*c.scale - self.testarr2[-1]*c.scale) + c.kappa)
dg = -1 * const * \
((self.t1*c.scale - (time -
self.warr[-1]/c.scale - self.t1)*c.scale) + c.kappa)
self.testarr2.append(
time - self.warr[-1]/c.scale - self.t1)
if (self.t1 > 10000):
self.testarr.append(self.t1)
self.warr.append(int(time*c.scale))
self.t1 = 0
elif (self.neurons[0].zarr[(time)] - self.neurons[0].zarr[(time)-1] < 0):
self.t1 += 1
# else:
# self.t1 = 0
elif (self.neurons[0].zarr[(time)] - self.neurons[0].zarr[(time)-1] < 0):
self.t1 += 1
# if ((temp2[int(time/10000)] - temp2[int(time/10000)-1] > 0 and temp2[int(time/10000)-1] - temp2[int(time/10000)-2] < 0)):
# if ((self.neurons[1].zarr[time] - self.neurons[1].zarr[time-1] > 0 and self.neurons[1].zarr[time-1] - self.neurons[1].zarr[time-2] < 0)):
# # print("HI")
# # if (len(self.te) > 0 and self.t1 > 10000):
# # dg = -1 * const * ((self.t1*c.scale - self.testarr2[-1]*c.scale) + c.kappa)
# if (self.t2 > 3000):
# self.testarr2.append(self.t2)
# self.warr2.append(int(time*c.scale))
# self.t2 = 0
# elif (self.neurons[1].zarr[(time)] - self.neurons[1].zarr[(time)-1] < 0):
# self.t2 += 1
# # else:
# # self.t1 = 0
# elif (self.neurons[1].zarr[(time)] - self.neurons[1].zarr[(time)-1] < 0):
# self.t2 += 1
# if ((temp1[int(time/10000)] - temp1[int(time/10000)-1] < 0 and temp1[int(time/10000)-1] - temp1[int(time/10000)-2] > 0)):
# if ((self.neurons[0].zarr[time] - self.neurons[0].zarr[time-1] < 0 and self.neurons[0].zarr[time-1] - self.neurons[0].zarr[time-2] > 0)):
# print("HI")
# # if (len(self.testarr2) > 0 and self.t2 > 10000):
# # dg = -1 * const * ((self.t1*c.scale - self.testarr2[-1]*c.scale) + c.kappa)
# self.testarr2.append(self.t2)
# self.warr2.append(int(time*c.scale))
# self.t2 = 0
# else:
# self.t2 += 1
# elif (self.neurons[0].zarr[(time)] - self.neurons[0].zarr[(time)-1] > 0):
# self.t2 += 1
# else:
# self.t1 = 0
# elif (self.neurons[0].zarr[(time)] - self.neurons[0].zarr[(time)-1] > 0):
# self.t2 += 1
# if ((temp2[int(time/10000)] - temp2[int(time/10000)-1] > 0 and temp2[int(time/10000)-1] - temp2[int(time/10000)-2] < 0)):
# if ((self.neurons[1].zarr[time] - self.neurons[1].zarr[time-1] > 0 and self.neurons[1].zarr[time-1] - self.neurons[1].zarr[time-2] < 0)):
# # print("HI")
# # if (len(self.testarr) > 0 and self.t1 > 10000):
# # dg = const * (np.abs(self.t1*c.scale - self.testarr[-1]*c.scale) + self.neurons[0].r)
# if (self.t2 > 10000):
# self.testarr2.append(self.t2)
# self.warr2.append(int(time*c.scale))
# self.t2 = 0
# elif (self.neurons[1].zarr[(time)] - self.neurons[1].zarr[(time)-1] < 0):
# self.t2 += 1
# # else:
# # self.t2 = 0
# elif (self.neurons[1].zarr[(time)] - self.neurons[1].zarr[(time)-1] < 0):
# self.t2 += 1
# if (temp2[int(time/10000)] - temp2[int(time/10000)-1] < 0):
# self.t2 +=10000
# if ((temp2[int(time/10000)] - temp2[int(time/10000)-1] > 0 and temp2[int(time/10000)-1] - temp2[int(time/10000)-2] < 0)
# or (temp2[int(time/10000)] - temp2[int(time/10000)-1] < 0 and temp2[int(time/10000)-1] - temp2[int(time/10000)-2] > 0)):
# dg = const * (np.abs(self.t1*c.scale - self.*c.scale) + self.neurons[0].r)
# self.t2 = 0
# else:
# self.t2 += 10000
self.neurons[0].update_g(time, dg)
self.neurons[0].update_tarr(time, self.t1*c.scale)
self.neurons[1].update_tarr(time, self.t2*c.scale)
# print (self.neurons[0].weights[0])
def update_new_dg(self, time, const):
dg = 0
if (self.neurons[0].x < c.inhib):
self.t1 += 1
elif(self.neurons[0].xarr[time] > c.inhib and self.neurons[0].xarr[time-1] < c.inhib):
self.t1 = 0
dg = const * ((self.t1*c.scale - self.t2 *
c.scale) + self.neurons[0].r)
if (self.neurons[1].x < c.inhib):
self.t2 += 1
elif(self.neurons[1].xarr[time] > c.inhib and self.neurons[1].xarr[time-1] < c.inhib):
self.t2 = 0
dg = const * (np.abs(self.t1*c.scale - self.t2 *
c.scale) + self.neurons[0].r)
# for neuron in self.neurons:
# neuron.update_g(time, dg)
self.neurons[0].update_g(time, dg)
self.neurons[0].update_tarr(time, self.t1*c.scale)
self.neurons[1].update_tarr(time, self.t2*c.scale)
print(self.neurons[0].weights[0])