-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateMachineClass.py
485 lines (401 loc) · 21.5 KB
/
stateMachineClass.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
#This is the class for construcing State Machines. The individual CAs
#(both inputs and states) run at 5 ms. See the notes for running faster or
#slower.
#There are test functions at the end.
#Note that to externally activate a CA State, you should only send
#excitatory connections to the first 8 neurons.
#Using the nealNRPCover so no nealParams
import pickle
class FSAHelperFunctions:
def __init__(self, simName, sim, neal,spinnVersion = None):
self.simName = simName
self.sim = sim
self.neal = neal
if spinnVersion is None:
self.spinnVersion = -1
else:
self.spinnVersion = spinnVersion
self.initParams()
#FSA Parmaeters
def initParams(self):
self.CA_SIZE: int = 10 #This will (almost certainly) not work with a
#different sized CA.
self.CA_INHIBS: int = 2
#normal weights
self.INPUT_WEIGHT: float = 0.12
self.HALF_INPUT_WEIGHT: float = 0.08
self.INTRA_CA_TO_INHIB_WEIGHT: float = 0.002
#if you over inhib on my spinnaker, it fires.
self.INTRA_CA_FROM_INHIB_WEIGHT: float = 0.15
self.CA_STOPS_CA_WEIGHT: float = 0.15
self.ONE_NEURON_STOPS_CA_WEIGHT: float = 1.0
self.ONE_NEURON_HALF_CA_WEIGHT: float = 0.02
if (self.simName =="spinnaker"):
self.INTRA_CA_WEIGHT: float = 0.025
elif (self.simName == "nest"):
self.INTRA_CA_WEIGHT: float = 0.022
else:
self.INTRA_CA_WEIGHT: float = 0.022
self.FULL_ON_WEIGHT: float = 0.01
self.FULL_ON_WEIGHT_SLOW: float = 0.0022
self.HALF_ON_WEIGHT: float = 0.0012
self.HALF_ON_ONE_WEIGHT: float = 0.002
self.STATE_TO_ONE_WEIGHT: float = .015
self.ONE_HALF_ON_WEIGHT: float = 0.016
self.ONE_HALF_ON_ONE_WEIGHT: float = 0.01
#self.ONE_NEURON_STARTS_CA_WEIGHT = self.INPUT_WEIGHT
self.CELL_PARAMS = {'v_thresh':-48.0, 'v_reset' : -70.0,
'tau_refrac': 2.0 , 'tau_syn_E': 5.0,
'tau_syn_I' : 5.0,
'v_rest' : -65.0,'i_offset':0.0}
#--------Finite State Automata Functions ------------
#states can be turned and off by a spikeSource, and can be stimulate or
#inhibited by one.
#states can be turned on and off by a neuron, and can stimulate or inhibit
#one
#states can stimulate or inhibit neurons
#states can be turned on and off by a state, and can stimulate or inhibit
#them. States can also slow turn on other states, and
#half turn them on
#---Functions that turn on states by one item
#-- Function to ignite a state from a spike source
#-- Uses INPUT_WEIGHT
#use this when your using a spikesource
def turnOnStateFromSpikeSource(self,spikeSource, toNeurons, toCA):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(0,toNeuron,self.INPUT_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'excitatory')
def halfTurnOnStateFromSpikeSource(self,spikeSource, toNeurons, toCA):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(0,toNeuron,self.HALF_INPUT_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'excitatory')
def turnOffStateFromSpikeSource(self,spikeSource, toNeurons, toCA):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(0,toNeuron,
self.ONE_NEURON_STOPS_CA_WEIGHT,self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'inhibitory')
def stimulateStateFromSpikeSource(self,spikeSource, toNeurons, toCA, weight):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(0,toNeuron,weight,
self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'excitatory')
def inhibitStateFromSpikeSource(self,spikeSource, toNeurons, toCA, weight):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(0,toNeuron,weight,
self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'inhibitory')
def turnOnOneNeuronFromSpikeSource(self,spikeSource, toNeurons, toNeuron):
connector = []
connector = connector + [(0,toNeuron,self.INPUT_WEIGHT,self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'excitatory')
#undone needs a test
def turnOffOneNeuronFromSpikeSource(self,spikeSource, toNeurons, toNeuron):
connector = []
connector = connector + [(0,toNeuron,self.CA_STOPS_CA_WEIGHT,self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'inhibitory')
#undone
def inhibitOneNeuronFromSpikeSource(self,spikeSource, toNeurons, toNeuron,
weight):
connector = []
connector = connector + [(0,toNeuron,weight,self.neal.DELAY)]
self.neal.nealProjection(spikeSource, toNeurons, connector,'inhibitory')
#---states can be turned on and off by a neuron, can stimulate or
#inhibit one, and can half turn on one.
def oneNeuronTurnsOnState(self,fromNeurons,fromNeuron, toNeurons, toCA: int):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,self.INPUT_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons, toNeurons, connector,'excitatory')
def oneNeuronHalfTurnsOnState(self,fromNeurons,fromNeuron, toNeurons, toCA):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,self.ONE_HALF_ON_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons, toNeurons, connector,'excitatory')
def oneNeuronTurnsOffState(self,fromNeurons, fromNeuron, toNeurons, toCA):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,
self.ONE_NEURON_STOPS_CA_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'inhibitory')
def oneNeuronHalfTurnsOffState(self,fromNeurons,fromNeuron, toNeurons, toCA):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,self.ONE_HALF_ON_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons, toNeurons, connector,'inhibitory')
def oneNeuronStimulatesState(self,fromNeurons,fromNeuron, toNeurons, toCA,
weight):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,weight,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons, toNeurons, connector,'excitatory')
#the rbs has these, but they're not accurate weights for what they
#say they do. So, leave them out for now.
#def oneNeuronHalfTurnsOnState(self,fromNeurons,fromNeuron,toNeurons,toCA):
#def oneNeuronHalfTurnsOnOneNeuron(self,fromNeurons,fromCA,toNeurons,toCA):
#def stateHalfTurnsOnOneNeuron(self,fromNeurons,fromCA,toNeurons,toNeuron):
#Neurons can also directly interact with each other.
def oneNeuronStimulatesOneNeuron(self,fromNeurons,fromNeuron, toNeurons,
toNeuron,weight):
connector = []
connector = connector + [(fromNeuron,toNeuron,weight,self.neal.DELAY)]
self.neal.nealProjection(fromNeurons, toNeurons, connector,'excitatory')
def oneNeuronTurnsOnOneNeuron(self,fromNeurons,fromCA,toNeurons,toCA):
self.oneNeuronStimulatesOneNeuron(fromNeurons,fromCA,toNeurons,toCA,
self.INPUT_WEIGHT)
def oneNeuronHalfTurnsOnOneNeuron(self,fromNeurons,fromCA,toNeurons,toCA):
self.oneNeuronStimulatesOneNeuron(fromNeurons,fromCA,toNeurons,toCA,
self.ONE_HALF_ON_ONE_WEIGHT)
def oneNeuronInhibitsState(self,fromNeurons,fromNeuron, toNeurons, toCA,
weight):
connector = []
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,weight,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons, toNeurons, connector,'inhibitory')
#states can stimulate or inhibit neurons
def stateTurnsOnOneNeuron(self,fromNeurons,fromCA,toNeurons,toNeuron):
connector = []
#uses STATE_TO_ONE_WEIGHT
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,
self.STATE_TO_ONE_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'excitatory')
def stateHalfTurnsOnOneNueron(self,fromNeurons,fromCA,toNeurons,toNeuron):
connector = []
#uses STATE_TO_ONE_WEIGHT
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,
self.HALF_ON_ONE_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'excitatory')
def stateStimulatesOneNeuron(self,fromNeurons,fromCA,toNeurons,toNeuron,
weight):
connector = []
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,weight,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'excitatory')
def stateInhibitsOneNeuron(self,fromNeurons,fromCA,toNeurons,toNeuron,
weight):
connector = []
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,weight,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'inhibitory')
#states can be turned on and off by a state, and can stimulate or inhibit
#them. States can also slow turn on other states, and
#half turn them on
#Function to turn on one state from another
#Call with fromPopulation, fromCA, toPopulation and toCA
def stateTurnsOnState(self,fromNeurons,fromCA,toNeurons,toCA):
connector = []
#uses FULL_ON_WEIGHT
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,
self.FULL_ON_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'excitatory')
#When stateTurnsOnState and the preState remains on, the post
#state runs hot.
def stateTurnsOnStateSlow(self,fromNeurons,fromCA,toNeurons,toCA):
connector = []
#uses FULL_ON_WEIGHT_SLOW
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,
self.FULL_ON_WEIGHT_SLOW,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'excitatory')
#---- One State or other set of neurons turns off another
#-- Uses CA_STOPS_CA_WEIGHT
def stateTurnsOffState(self,fromNeurons, fromCA, toNeurons, toCA):
connector = []
for fromOffset in range (0,self.CA_SIZE):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,
self.CA_STOPS_CA_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'inhibitory')
#--Functions when two inputs are needed to turn on a third
#-- Two states are needed to turn on a third.
#-- This connects one of the inputs to the the third.
#-- Uses HALFs_ON_WEIGHT
def stateHalfTurnsOnState(self,fromNeurons,fromCA,toNeurons,toCA):
connector = []
#uses HALF_ON_WEIGHT
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,
self.HALF_ON_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'excitatory')
def stateHalfTurnsOffState(self,fromNeurons,fromCA,toNeurons,toCA):
connector = []
#uses HALF_ON_WEIGHT
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,
self.HALF_ON_WEIGHT,
self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'inhibitory')
def stateStimulatesState(self,fromNeurons,fromCA,toNeurons,toCA,weight):
connector = []
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector=connector+[(fromNeuron,toNeuron,
weight, self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'excitatory')
def stateInhibitsState(self,fromNeurons, fromCA, toNeurons, toCA, wt):
connector = []
for fromOffset in range (0,self.CA_SIZE):
fromNeuron = fromOffset + (fromCA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE):
toNeuron = toOffset + (toCA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,
wt,self.neal.DELAY)]
self.neal.nealProjection(fromNeurons,toNeurons,connector,'inhibitory')
#---Create a CA that will persistently fire.
#-- Assumes neurons in the same population
#-- Uses INTRA_CA_WEIGHT
def getCAConnectors(self, CA):
connector = []
#excitatory turn each other on
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (CA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (CA*self.CA_SIZE)
if (toNeuron != fromNeuron):
connector = connector + [(fromNeuron,toNeuron,
self.INTRA_CA_WEIGHT, self.neal.DELAY)]
#excitatory turn on inhibitory
for fromOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
fromNeuron = fromOffset + (CA*self.CA_SIZE)
for toOffset in range (self.CA_SIZE-self.CA_INHIBS,self.CA_SIZE):
toNeuron = toOffset + (CA*self.CA_SIZE)
connector = connector + [(fromNeuron,toNeuron,
self.INTRA_CA_TO_INHIB_WEIGHT, self.neal.DELAY)]
#inhibitory slows excitatory
inhConnector = []
for fromOffset in range (self.CA_SIZE-self.CA_INHIBS,self.CA_SIZE):
fromNeuron = fromOffset + (CA*self.CA_SIZE)
for toOffset in range (0,self.CA_SIZE-self.CA_INHIBS):
toNeuron = toOffset + (CA*self.CA_SIZE)
inhConnector = inhConnector + [(fromNeuron,toNeuron,
self.INTRA_CA_FROM_INHIB_WEIGHT, self.neal.DELAY)]
return[connector,inhConnector]
def makeCA(self,neurons, CA):
#print('makeCA)
connectors = self.getCAConnectors(CA)
self.neal.nealProjection(neurons,neurons,connectors[0],'excitatory')
self.neal.nealProjection(neurons,neurons,connectors[1],'inhibitory')
def nopMakeCA(self,neurons, CA):
connector = []
fromNeuron = CA
toNeuron = CA + 1
connector = connector + [(fromNeuron,toNeuron, 1.2, self.neal.DELAY)]
self.neal.nealProjection(neurons,neurons,connector,'excitatory')
#------test functions
#initialize the simulator.
def testInit(self):
#print("spin" or nest)
self.sim.setup(timestep=self.neal.DELAY,
min_delay=self.neal.DELAY,
max_delay=self.neal.DELAY, debug=0)
def testCreateTwoInputs(self):
inputSpikeTimes0 = [10.0]
inputSpikeTimes1 = [50.0]
spikeArray0 = {'spike_times': [inputSpikeTimes0]}
spikeGen0=self.sim.Population(1,self.sim.SpikeSourceArray,spikeArray0,
label='inputSpikes_0')
spikeArray1 = {'spike_times': [inputSpikeTimes1]}
spikeGen1=self.sim.Population(1, self.sim.SpikeSourceArray, spikeArray1,
label='inputSpikes_1')
return [spikeGen0,spikeGen1]
def testCreateNeurons(self):
if (self.simName == 'nest'):
numNeurons = self.CA_SIZE * 3
elif (self.simName == 'spinnaker'):
numNeurons = 100
cells=self.sim.Population(numNeurons,self.sim.IF_cond_exp,self.CELL_PARAMS)
return cells
def testSetupRecording(self,cells):
if ((self.neal.simulator == 'nest') or
((self.neal.simulator == 'spinnaker') and (self.neal.spinnVersion == 8))):
cells.record({'spikes','v'})
elif ((self.neal.simulator == 'spinnaker') and (self.neal.spinnVersion == 7)):
cells.record()
def test3StateFSA(self, firstSpikeGenerator, secondSpikeGenerator,
stateCells):
#Build the FSA
self.turnOnStateFromSpikeSource(firstSpikeGenerator,stateCells,0)
self.turnOnStateFromSpikeSource(secondSpikeGenerator,stateCells,1)
self.makeCA(stateCells,0)
self.makeCA(stateCells,1)
self.makeCA(stateCells,2)
self.stateHalfTurnsOnState(stateCells,0,stateCells,2)
self.stateTurnsOffState(stateCells,2,stateCells,0)
#comment below out to check state 0 alone does not turn on state 2
self.stateHalfTurnsOnState(stateCells,1,stateCells,2)
self.stateTurnsOffState(stateCells,2,stateCells,1)
def testRunFSA(self,duration):
self.sim.run(duration)
def testPrintPklSpikes(self,fileName):
fileHandle = open(fileName)
neoObj = pickle.load(fileHandle)
segments = neoObj.segments
segment = segments[0]
spikeTrains = segment.spiketrains
neurons = len(spikeTrains)
for neuronNum in range (0,neurons):
if len(spikeTrains[neuronNum])>0:
spikes = spikeTrains[neuronNum]
for spike in range (0,len(spikes)):
print(neuronNum, spikes[spike])
fileHandle.close()
def testPrintResults(self,simCells):
if ((self.neal.simulator == 'nest') or
((self.neal.simulator == 'spinnaker') and (self.neal.spinnVersion == 8))):
simCells.printSpikes('temp.pkl')
elif ((self.neal.simulator == 'spinnaker') and (self.neal.spinnVersion == 7)):
simCells.printSpikes('temp.sp')