-
Notifications
You must be signed in to change notification settings - Fork 0
/
clnoisyneurons.py
252 lines (244 loc) · 11.4 KB
/
clnoisyneurons.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
##################################
#
# clnoisyneurons.py
# Copyright (C) Louisiana State University, Health Sciences Center
# Written by 2011-2013 Ruben Tikidji-Hamburyan <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
# NON INFRINGEMENT. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###################################
import random as rnd
import string
import sys
import math
#########################################################
# process:
# 0 - OUP
# 1 - Feller
# 2 - Period Additive noise
# 3 - Phase Additive noise
# 4 - OUP + event updates
# 5 - Play from table
# 6 - Random choice from table
# >
# 100 - OUP + resetting
# 101 - Feller + resetting
#########################################################
class clnoisyneurons:
def __init__(self, object="noisyneurons", attr={}):
#### Default paramters
self.number = 1
self.period_mu = 1
self.period_sd = 1
self.period_tau = 1
self.name="*****"
self.object=object
self.saturation = -1
self.process = 0 #0 - OU, 1 - Feller, 2 - OU+PRC, 3 - Feller+PRC
self.f2='summ'
#### Test for implicit attributes:
for atr in attr.keys():
if atr == "number": continue
if atr == "period_mu": continue
if atr == "period_sd": continue
if atr == "period_tau": continue
if atr == "process": continue
if atr == "name": continue
if atr == "f2": continue
sys.stderr.write("Unexpected attribut \'%s\'for tag <neurons>\nABBORT\n\n"%atr)
sys.exit(1)
#### Reset from attrebuts
if attr.get("number"): self.number = int( attr["number"] )
if attr.get("period_mu"): self.period_mu = float( attr["period_mu"] )
if attr.get("period_sd"): self.period_sd = float( attr["period_sd"] )
if attr.get("period_tau"): self.period_tau = float( attr["period_tau"] )
if attr.get("process"): self.process = int( attr["process"] )
if attr.get("name"): self.name = attr["name"]
if attr.get("f2"): self.f2 = attr["f2"]
#Array of neurons the numbers are
# phase, second order correction and last period
self.neurons = [ [0, 0, 0] for x in xrange(self.number) ]
if self.process != 3:
self.periods = [ rnd.gauss(self.period_mu,self.period_sd) for x in xrange(self.number) ]
else :
self.periods = [ self.period_mu for x in xrange(self.number) ]
self.timetospike = [ 0 for x in xrange(self.number) ]
#### Dic of named connections
self.connections = {}
#### Output buffers
self.op = [ 0 for x in xrange(self.number) ]
self.__lastspike = [ 0 for x in xrange(self.number) ]
def startpoint(self,object,attr):
if object == "init" :
if attr.get("ph0_all") :
for i in xrange(self.number):
self.neurons[i][0] = float(attr["ph0_all"])
if attr.get("ph1_all") :
for i in xrange(self.number):
self.neurons[i][1] = float(attr["ph1_all"])
if attr.get("ph0_all") and attr.get("ph0_sd") :
for i in xrange(self.number):
self.neurons[i][0] = rnd.normalvariate(
float(attr["ph0_all"]), float(attr["ph0_sd"])
)
if self.neurons[i][0] > 1.0: self.neurons[i][0] -= abs( math.floor(self.neurons[i][0]) ) + 1.0
if attr.get("ph1_all") and attr.get("ph1_sd") :
for i in xrange(self.number):
self.neurons[i][1] = rnd.normalvariate(
float(attr["ph1_all"]), float(attr["ph1_sd"])
)
if self.neurons[i][1] > 1.0: self.neurons[i][1] -= abs( math.floor(self.neurons[i][1]) ) + 2.0
if attr.get("ph0"):
setstrl=string.split(attr["ph0"],",")
if len(setstrl) < self.number:
for i in xrange(len(setstrl)):
self.neurons[i][0] = float( setstrl[i] )
else :
for i in xrange(self.number):
self.neurons[i][0] = float( setstrl[i] )
if attr.get("ph1"):
setstrl=string.split(attr["ph1"],",")
if len(setstrl) < self.number:
for i in xrange(len(setstrl)):
self.neurons[i][1] = float( setstrl[i] )
else :
for i in xrange(self.number):
self.neurons[i][1] = float( setstrl[i] )
for i in xrange(self.number):
self.timetospike[i] = self.periods[i]*(1.0 - self.neurons[i][0])
elif object == "saturation":
return
elif object == "periodtable":
self.pindex = 0
self.ptable = [ float(x) for x in attr["items"].split(",") ]
else:
sys.stderr.write("Unexpected tag <%s> in <neuron> expression\nABBORT\n\n"%object);
sys.exit(1)
def stoppoint(self, object):
if object == "neuron" or object == "init" or object == "saturation" or object == "periodtable":
return
else:
sys.stderr.write("Unexpected close tag <%s> in <neurons> expression\nABBORT\n\n"%object);
sys.exit(1)
def write(self):
result=["<noisyneurons name=\"%s\" number=\"%d\" period_mu=\"%g\" period_sd=\"%g\" period_tau=\"%g\" process=\"%d\">"%(self.name, self.number, self.period_mu, self.period_sd, self.period_tau, self.process)]
for i in xrange(self.number):
result.append(
"\t <condition ph=\"%g\" correction=\"%g\" timetospike=\"%g\" />"
% (self.neurons[i][0],self.neurons[i][1],self.timetospike[i])
)
if self.process == 5 or self.process == 6:
result.append("\t<periodtable items=\""+reduce(lambda x,y: x+","+"{}".format(y), self.ptable[:-1],"")+"{}\">".format(self.ptable[-1]) )
result.append("</noisyneurons>")
return result
def getnames(self):
result = []
for cnt in xrange(self.number):
result.append("%s:%d"%(self.name,cnt+1))
return result
def calculate(self,model):
for cont in self.connections.items():
gsyn_sum = 0
for con in cont[1]:
gsyn_sum += con.gsyn * reduce(lambda x,y:x+y, con.op)
for nrn in xrange(self.number):
#Insert here saturation code!
#----------------------------
correction = cont[1][0].prc.getvl( gsyn_sum, self.neurons[nrn][0])
if self.process < 3 or self.process == 4 or self.process == 5 or self.process == 6:
self.neurons[nrn][0] -= correction[0] #f1 contribution (why - ?)
elif self.process == 3:
self.neurons[nrn][0] -= correction[0] + rnd.gauss(0,self.period_sd)
if self.neurons[nrn][0] > 1: self.neurons[nrn][0] = 1
else:
sys.stderr.write("Name:%s; #:%d; Process:%d; P[n]=%g; ph[n]=%g; cor=%g; =="%(self.name,nrn,self.process,self.periods[nrn],self.neurons[nrn][0], correction[0]) )
if self.process == 100:
self.periods[nrn] += (self.period_mu - self.periods[nrn]) * model.timetospike/self.period_tau + rnd.gauss(0,self.period_sd) * math.sqrt(model.timetospike) + correction[0] * self.periods[nrn]
else: # process == 101:
self.periods[nrn] += (self.period_mu - self.periods[nrn]) * model.timetospike/self.period_tau + rnd.gauss(0,self.period_sd) * math.sqrt(self.periods[nrn]*model.timetospike) + correction[0] * self.periods[nrn]
pln = (self.period_tau*self.periods[nrn]-(self.period_mu-self.periods[nrn])*(model.elapsed_time - self.__lastspike[nrn]))/(self.period_tau+self.periods[nrn]+self.period_mu)
self.neurons[nrn][0] = (model.elapsed_time - self.__lastspike[nrn])/pln
#self.neurons[nrn][0] = (model.elapsed_time - self.__lastspike[nrn])/self.periods[nrn]
sys.stderr.write("=> P[n+1]=%g; ph[n+1]=%g tos=%g\n"%(self.periods[nrn],self.neurons[nrn][0],self.periods[nrn] * (1.0 - self.neurons[nrn][0])) )
if self.f2 == "off": continue
elif self.f2 == "last":
self.neurons[nrn][1] = correction[1] #replace f2 contribution
else :
self.neurons[nrn][1] += correction[1] #accumulate f2 contribution
for nrn in xrange(self.number):
self.timetospike[nrn] = self.periods[nrn] * (1.0 - self.neurons[nrn][0])
### RK2 ????
###self.timetospike[nrn] = self.periods[nrn] * (1.0 - self.neurons[nrn][0])
def update(self,model):
for nrn in xrange(self.number):
if self.process >= 100:
sys.stderr.write("mintos:%e; tos:%e; diff:%d\n"%(model.timetospike,self.timetospike[nrn],self.timetospike[nrn] <= model.timetospike))
if (self.timetospike[nrn] - model.timetospike) < 1e-7:
if self.process >= 100: sys.stderr.write("SPIKE\n")
self.op[nrn] = 1
self.neurons[nrn][0] = 0.0
if self.f2 != "off":
self.neurons[nrn][0] -= self.neurons[nrn][1]
self.neurons[nrn][1] = 0.0
self.neurons[nrn][2] = model.elapsed_time - self.__lastspike[nrn]
self.__lastspike[nrn] = model.elapsed_time
if self.process == 0 or self.process == 100:
# Deterministic part
self.periods[nrn] += (self.period_mu - self.periods[nrn]) * (1.0 - math.exp( - self.neurons[nrn][2]/self.period_tau) )
# Stochastic part
self.periods[nrn] += self.period_sd*math.sqrt(self.period_tau/2*(1-math.exp(-2*self.periods[nrn]/self.period_tau)))*rnd.gauss(0,1.0)
#while self.periods[nrn] <= 0.0 :
# self.periods[nrn] += (self.period_mu - self.periods[nrn]) * (1.0 - math.exp( - self.neurons[nrn][2]/self.period_tau) ) + rnd.gauss(0,self.period_sd) * math.sqrt(self.neurons[nrn][2])
elif self.process == 1 or self.process == 101:
self.periods[nrn] += (self.period_mu - self.periods[nrn]) * (1.0 - math.exp( - self.neurons[nrn][2]/self.period_tau) ) + rnd.gauss(0,self.period_sd) * math.sqrt(self.periods[nrn]*self.neurons[nrn][2])
#while self.periods[nrn] <= 0.0 :
# self.periods[nrn] += (self.period_mu - self.periods[nrn]) * (1.0 - math.exp( - self.neurons[nrn][2]/self.period_tau) ) + rnd.gauss(0,self.period_sd) * math.sqrt(self.periods[nrn]*self.neurons[nrn][2])
elif self.process == 2:
self.periods[nrn] = rnd.gauss(self.period_mu,self.period_sd)
elif self.process == 3:
self.periods[nrn] = self.period_mu
elif self.process == 4:
# Deterministic part
self.periods[nrn] += (self.period_mu - self.periods[nrn]) * (1.0 - math.exp( - model.timetospike/self.period_tau) )
# Stochastic part
self.periods[nrn] += self.period_sd*math.sqrt(self.period_tau/2*(1-math.exp(-2*model.timetospike/self.period_tau)))*rnd.gauss(0,1.0)
elif self.process == 5:
self.periods[nrn] = self.ptable[self.pindex]
self.pindex += 1
if self.pindex >= len(self.ptable): self.pindex = 0
elif self.process == 6:
self.periods[nrn] = rnd.choice(self.ptable)
else:
if self.process == 3:
self.neurons[nrn][0] += model.timetospike/self.periods[nrn] + rnd.gauss(0,self.period_sd)
if self.neurons[nrn][0] > 1: self.neurons[nrn][0] = 1
elif self.process == 4: #update period!
# Deterministic part
self.periods[nrn] += (self.period_mu - self.periods[nrn]) * (1.0 - math.exp( - model.timetospike/self.period_tau) )
# Stochastic part
self.periods[nrn] += self.period_sd*math.sqrt(self.period_tau/2*(1-math.exp(-2*model.timetospike/self.period_tau)))*rnd.gauss(0,1.0)
self.neurons[nrn][0] += model.timetospike/self.periods[nrn]
if self.neurons[nrn][0] >= 1:# self.neurons[nrn][0] = 1
self.neurons[nrn][0] = 0.0
if self.f2 != "off":
self.neurons[nrn][0] -= self.neurons[nrn][1]
self.neurons[nrn][1] = 0.0
self.neurons[nrn][2] = model.elapsed_time - self.__lastspike[nrn]
self.__lastspike[nrn] = model.elapsed_time
else: # 0 1 2 5 6 100, 101
self.neurons[nrn][0] += model.timetospike/self.periods[nrn]
self.op[nrn] = 0