-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnnBase.py
537 lines (396 loc) · 14.5 KB
/
SnnBase.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
import math
import random
# TODO: I think when a spike arrives could still be order dependant, it could still take one step or two.
# going to have to deliver queued spikes during the prepare step
class SnnError(Exception):
pass
class SpikingNeuron:
def __init__(self, threshold, magnitude, leak_eql, leak_tau):
self.threshold = threshold
self.magnitude = magnitude
self.eql = leak_eql
self.tau = leak_tau
self.tau_mult = 1.0 / leak_tau
self.currents = []
self.charge = self.eql
self.synapses = []
self.spike_listeners = []
self.received_spikes = []
self.spike = False
def step(self, dt):
delta = -1.0 * (self.charge - self.eql) * (self.tau_mult) * dt
for current in self.currents:
delta += (current.eql - self.charge) * dt * current.conductance
for spike in self.received_spikes:
delta += spike
self.received_spikes = []
self.charge += delta
if self.charge >= self.threshold:
self.charge = self.eql
self.spike = True
def exchange(self):
if self.spike == True:
for ps in self.synapses:
ps.add_spike(self.magnitude)
for listener in self.spike_listeners:
listener.notify_of_spike()
self.spike = False
def add_spike(self, magnitude):
self.received_spikes.append(magnitude)
def add_synapse(self, synapse):
self.synapses.append(synapse)
def add_current(self, current):
self.currents.append(current)
# order-dependant, value will be different before and after step()
def get_charge(self):
return self.charge
def get_sample(self):
return self.charge
def add_spike_listener(self, listener):
self.spike_listeners.append(listener)
class DelayedSpike:
def __init__(self, delay, magnitude):
self.remaining_delay = delay
self.magnitude = magnitude
class Synapse:
def __init__(self, delay, efficiency=1.0):
self.delay = delay
self.efficiency = efficiency
self.waiting_spikes = []
self.outgoing_spikes = []
self.targets = []
def step(self, dt):
# probably some clever list-comprehending could do this more concisely
temp_spikes = self.waiting_spikes
self.waiting_spikes = []
self.outgoing_spikes = []
for spike in temp_spikes:
spike.remaining_delay -= dt
if spike.remaining_delay <= 0.0:
self.outgoing_spikes.append(spike)
else:
self.waiting_spikes.append(spike)
def exchange(self):
for s in self.outgoing_spikes:
for t in self.targets:
t.add_spike(self.efficiency * s.magnitude)
def add_spike(self, magnitude):
ds = DelayedSpike(self.delay, magnitude)
self.waiting_spikes.append(ds)
def add_target(self, target):
self.targets.append(target)
@staticmethod
def connect(source, target, delay=0.0, efficiency=1.0):
s = Synapse(delay, efficiency)
s.add_target(target)
source.add_synapse(s)
return s
class Pulsar:
def __init__(self, magnitude, frequency):
self.magnitude = magnitude
self.frequency = frequency # not used after construction at present
self.delay = 1.0 / frequency
self.remaining = self.delay
self.synapses = []
self.spike_listeners = []
self._spike = False
def step(self, dt):
self.remaining -= dt
if self.remaining <= 0.0:
self.remaining = self.delay
self._spike = True
def exchange(self):
if self._spike:
for s in self.synapses:
s.add_spike(self.magnitude)
for listener in self.spike_listeners:
listener.notify_of_spike()
self._spike = False
def add_synapse(self, ps):
self.synapses.append(ps)
def add_spike_listener(self, listener):
self.spike_listeners.append(listener)
class NaiveRandomSpiker:
def __init__(self, magnitude, freq):
self.magnitude = magnitude
self.freq = freq
self.synapses = []
self.spike_listeners = []
self._gap = random.uniform(0.0, 2.0 / freq)
self._spike = False
def step(self, dt):
self._gap -= dt
if self._gap <= 0.0:
self._spike = True
self._gap = random.uniform(0.0, 2.0 / self.freq)
def exchange(self):
if self._spike == True:
for synapse in self.synapses:
synapse.add_spike(self.magnitude)
for listener in self.spike_listeners:
listener.notify_of_spike()
self._spike = False
def add_synapse(self, ps):
self.synapses.append(ps)
def add_spike_listener(self, listener):
self.spike_listeners.append(listener)
class PoissonSpiker:
# NB: possibly almost the same as the NaiveRandomSpiker
def __init__(self, magnitude, frequency):
self.magnitude = magnitude
self.frequency = frequency
self.synapses = []
self.spike_listeners = []
self._spike = False
def step(self, dt):
u = random.uniform(0.0, 1.0)
if u <= dt * self.frequency:
self._spike = True
def exchange(self):
if self._spike:
for synapse in self.synapses:
synapse.add_spike(self.magnitude)
for listener in self.spike_listeners:
listener.notify_of_spike()
self._spike = False
def add_synapse(self, syn):
self.synapses.append(syn)
def add_spike_listener(self, listener):
self.spike_listeners.append(listener)
class DrivenPoissonSpiker:
# driving function should be an object with a __call__ method
# __call__ should have no parameters (it won't be given any)
# if it needs to be stepped, it should be stepped seperately
# TODO: evaluate how good an idea this really is
def __init__(self, magnitude, alpha, threshold, driving_function):
self.magnitude = magnitude
self.alpha = alpha
self.threshold = threshold
self.driving_function = driving_function
self.synapses = []
self.spike_listeners = []
self._spike = False
def step(self, dt):
u = random.uniform(0.0, 1.0)
r = self.alpha * (self.driving_function() - self.threshold) # effective freq is function of driving function
if u <= dt * r:
self._spike = True
def exchange(self):
if self._spike:
for synapse in self.synapses:
synapse.add_spike(self.magnitude)
for listener in self.spike_listeners:
listener.notify_of_spike()
self._spike = False
def add_synapse(self, syn):
self.synapses.append(syn)
def add_spike_listener(self, listener):
self.spike_listeners.append(listener)
# TODO: there's an order-dependance if this thing steps before or after Synapses
class Current:
"""A trivial, constant current
has a step, because other currents might need to step
"""
def __init__(self, eql, conductance):
self.eql = eql
self.conductance = conductance
def step(self, dt):
pass
class SpikeRecord:
def __init__(self, time, magnitude):
self.time = time
self.magnitude = magnitude
class Counter:
def __init__(self, name=None):
self.time = 0.0
self.spikes = []
self.name = name
def step(self, dt):
self.time += dt
def add_spike(self, magnitude):
sr = SpikeRecord(self.time, magnitude)
self.spikes.append(sr)
def notify_of_spike(self):
sr = SpikeRecord(self.time, None) # no magnitude when using "spike-listener protocol"
self.spikes.append(sr)
def dump_records(self):
if self.name is not None:
print("spike record for counter {}".format(self.name))
for record in self.spikes:
if record.magnitude is None:
print("t{}: spike".format(record.time))
else:
print("t{}: {}mV".format(record.time, record.magnitude))
def write_records(self, ostream):
for record in self.spikes:
if record.magnitude is None:
ostream.write("t{}: spike\n".format(record.time))
else:
ostream.write("t{}: {}mv\n".format(record.time, record.magnitude))
def report(self, fname=None):
# fname overrides self.name when picking file name
out_path = fname
if out_path is None:
if self.name is not None:
out_path = self.name + ".dat"
else:
raise SnnError("Could not pick a unique file name")
with open(out_path, "w") as ofile:
self.write_records(ofile)
class Sample:
def __init__(self, time, charge):
self.time = time
self.charge = charge
class Sampler:
def __init__(self, source, frequency, name=None):
self.source = source
#self.frequency = frequency
self.interval = 1.0 / frequency
self.remaining = 0.0 # have samplers record on first tick
self.samples = []
self.time = 0.0
self.name = name
self.sample = False
def step(self, dt):
self.time += dt
self.remaining -= dt
if self.remaining <= 0.0:
self.remaining = self.interval
self.sample = True
def exchange(self):
if self.sample == True:
s = Sample(self.time, self.source.get_sample())
self.samples.append(s)
self.sample = False
def write_samples(self, ostream):
for sample in self.samples:
ostream.write("{} {}\n".format(sample.time, sample.charge))
def report(self, fname=None):
# fname overrides self.name when picking file name
out_path = fname
if out_path is None:
if self.name is not None:
out_path = self.name + ".dat"
else:
raise SnnError("Could not pick a unique file name")
with open(out_path, "w") as ofile:
self.write_samples(ofile)
## Easy enough to implement if useful
#class SinusoidSource:
# def __init__(self, frequency, amplitude):
# self.frequency = frequency
# self.freq_mult = 2.0 * 3.14159 * frequency
# self.amplitude = amplitude
#
# self.time = 0.0
#
# def step(self, dt):
# self.time += dt
#
# def get_current(self):
# return self.amplitude * math.sin(self.freq_mult * self.time)
#
# def get_sample(self):
# return self.get_current()
#
#class SumOfSines:
# def __init__(self):
# self.sinusoids = []
#
# def step(self, dt):
# for sine in self.sinusoids:
# sine.step(dt)
#
# def get_current(self):
# sum = 0.0
#
# for sine in self.sinusoids:
# sum += sine.get_current()
#
# return sum
#
# def get_sample(self):
# return self.get_current()
#
# def add_sinusoid(self, frequency, amplitude):
# s = SinusoidSource(frequency, amplitude)
#
# self.sinusoids.append(s)
class Delayer:
def __init__(self, entity, delay):
self.entity = entity
self.delay = delay
def step(self, dt):
if self.delay > 0.0:
self.delay -= dt
else:
self.entity.step(dt)
## Moved to Utilities
# class CallbackManager:
# def __init__(self, freq):
# self.t = 0.0
# self.freq = freq
# self.wait = 1.0 / freq
# self.run_callbacks = False
# self.callbacks = []
# def step(self, dt):
# self.t += dt
# self.wait -= dt
# if self.wait <= 0.0:
# self.wait = 1.0 / self.freq
# self.run_callbacks = True
# def exchange(self):
# if self.run_callbacks:
# self.run_callbacks = False
# for callback in self.callbacks:
# callback(self.t)
# def add_callback(self, callback):
# self.callbacks.append(callback)
def linspace(minimum, maximum, count):
"""a lazy reimplementation of linspace
because I often need linspace but I'm too lazy to import a module that would provide it.
NB this version includes both end-points, which might make the step not what you expect.
"""
if maximum <= minimum:
raise ValueError("minimum must be less than maximum")
if count <= 1:
raise ValueError("count must be at least 2")
step = (maximum - minimum) / (count - 1) # step should be a float
return [ minimum + step * x for x in range(count)]
def run_simulation(stop_time, step, entities):
time = 0.0
while time < stop_time:
for entity in entities:
# call entity.compute() if it exists
# try:
# entity.prepare()
# except AttributeError:
# pass
#
if hasattr(entity, "prepare") and callable(entity.prepare):
entity.prepare()
entity.step(step)
# call entity.exchange() if it exists
# try:
# entity.exchange()
# except AttributeError:
# pass
if hasattr(entity, "exchange") and callable(entity.exchange):
entity.exchange()
# TODO: this fails if exchange raises an attribute error!
# and it did so, one too many times
time += step
#class _SimulationManagerIterator:
# def __init__(self, manager):
# self.manager = manager
#
# def __next__(self):
# pass # todo - got lazy
#
#class SimulationmManager:
# def __init__(self):
# self.independant_entities = list()
# self.entity_sources = list()
#
# def __iter__(self):
# return _SimulationManagerIterator(self)