-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_sorn.py
326 lines (307 loc) · 10.2 KB
/
test_sorn.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
import unittest
import pickle
import numpy as np
from sorn.sorn import Trainer, Simulator
from sorn.utils import Plotter, Statistics
# Getting back the pickled matrices:
with open("sample_matrices.pkl", "rb") as f:
(
state_dict,
Exc_activity,
Inh_activity,
Rec_activity,
num_active_connections,
) = pickle.load(f)
# Default Test inputs
simulation_inputs = np.random.rand(10, 2)
gym_input = np.random.rand(10, 1)
sequence_input = np.random.rand(10, 1)
# Overriding defaults: Sample input
num_features = 10
timesteps = 1000
inputs = np.random.rand(num_features, timesteps)
class TestSorn(unittest.TestCase):
def test_runsorn(self):
"""Test SORN initialization, simulation and training methods with different
initialization schemes and plasticity rules
"""
# Initialize and simulate SORN with the default hyperparameters
self.assertRaises(
Exception,
Simulator.run(
inputs=simulation_inputs,
phase="plasticity",
matrices=None,
timesteps=2,
noise=True,
nu=num_features,
),
)
# Initilize and resume the simulation of SORN using the state dictionary, state_dict
self.assertRaises(
Exception,
Simulator.run(
inputs=simulation_inputs,
phase="plasticity",
state=state_dict,
timesteps=2,
noise=False,
),
)
# Freeze a particular plasticity during simulation
self.assertRaises(
Exception,
Simulator.run(
inputs=simulation_inputs,
phase="plasticity",
state=state_dict,
timesteps=2,
noise=False,
freeze=["ip"],
),
)
# Freeze multiple plasticity mechanisms during simulation
self.assertRaises(
Exception,
Simulator.run(
inputs=simulation_inputs,
phase="plasticity",
state=state_dict,
timesteps=2,
noise=False,
freeze=["stdp", "istdp", "ss", "sp"],
),
)
# Train SORN with all plasticity mechanisms active
self.assertRaises(
Exception,
Trainer.run(
inputs=gym_input,
phase="plasticity",
state=state_dict,
timesteps=1,
noise=True,
),
)
# Freeze multiple plasticity mechanisms during training
self.assertRaises(
Exception,
Trainer.run(
inputs=sequence_input,
phase="training",
state=state_dict,
timesteps=1,
noise=True,
freeze=["stdp", "istdp", "ss", "sp"],
),
)
# Override the default hyperparameters, initialize SORN and simulate under all plasticity mechanisms
self.assertRaises(
Exception,
Simulator.run(
inputs=inputs,
phase="plasticity",
matrices=None,
noise=True,
timesteps=timesteps,
ne=26,
lambda_ee=4,
lambda_ei=4,
nu=num_features,
),
)
# Override the default hyperparameters, initialize SORN and train under all plasticity mechanisms
self.assertRaises(
Exception,
Trainer.run(
inputs=inputs,
phase="plasticity",
matrices=None,
noise=True,
timesteps=timesteps,
ne=40,
lambda_ee=5,
lambda_ei=5,
nu=num_features,
),
)
# Test Callbacks
self.assertRaises(
Exception,
Simulator.run(
inputs=inputs,
phase="plasticity",
matrices=None,
noise=True,
timesteps=timesteps,
ne=50,
nu=10,
callbacks=[
"ExcitatoryActivation",
"InhibitoryActivation",
"RecurrentActivation",
"WEE",
"EIConnectionCounts",
"TI",
"EEConnectionCounts",
"TE",
"WEI",
],
),
),
self.assertRaises(
Exception,
Trainer.run(
inputs=inputs,
phase="plasticity",
matrices=None,
noise=True,
timesteps=timesteps,
ne=50,
nu=10,
callbacks=[
"ExcitatoryActivation",
"InhibitoryActivation",
"RecurrentActivation",
"WEE",
"EIConnectionCounts",
"TI",
"EEConnectionCounts",
"TE",
"WEI",
],
),
)
def test_plotter(self):
"""Test the Plotter class methods in utils module"""
# Histogram of number of postsynaptic connections per neuron in the excitatory pool
self.assertRaises(
Exception,
Plotter.hist_outgoing_conn(
weights=state_dict["Wee"], bin_size=5, histtype="bar", savefig=False
),
)
# Histogram of number of presynaptic connections per neuron in the excitatory pool
self.assertRaises(
Exception,
Plotter.hist_incoming_conn(
weights=state_dict["Wee"], bin_size=5, histtype="bar", savefig=False
),
)
# Plot number of positive connection strengths (weights>0) in the network at each time step
self.assertRaises(
Exception,
Plotter.network_connection_dynamics(
connection_counts=num_active_connections,
savefig=False,
),
)
# Histogram of firing rate of the network
self.assertRaises(
Exception,
Plotter.hist_firing_rate_network(
spike_train=np.asarray(Exc_activity), bin_size=5, savefig=False
),
)
# Plot Spike train of all neurons in the network
self.assertRaises(
Exception,
Plotter.scatter_plot(spike_train=np.asarray(Exc_activity), savefig=False),
)
# Raster plot of activity of neurons in the excitatory pool
self.assertRaises(
Exception,
Plotter.raster_plot(spike_train=np.asarray(Exc_activity), savefig=False),
)
# Inter spike intervals with exponential curve fit for neuron 10 in the Excitatory pool
self.assertRaises(
Exception,
Plotter.isi_exponential_fit(
spike_train=np.asarray(Exc_activity),
neuron=10,
bin_size=5,
savefig=False,
),
)
# Plot weight distribution in the network
self.assertRaises(
Exception,
Plotter.weight_distribution(
weights=state_dict["Wee"], bin_size=5, savefig=False
),
)
# Distribution of connection weights in linear and lognormal scale
self.assertRaises(
Exception,
Plotter.linear_lognormal_fit(
weights=state_dict["Wee"], num_points=10, savefig=False
),
)
self.assertRaises(
Exception,
Plotter.hamming_distance(
hamming_dist=[0, 0, 0, 1, 1, 1, 1, 1, 1], savefig=False
),
)
def test_statistics(self):
"""Test the functions in Statistics class"""
# Firing rate of a neuron
self.assertRaises(
Exception,
Statistics.firing_rate_neuron(
spike_train=np.asarray(Exc_activity), neuron=10, bin_size=5
),
)
# Firing rate of the network
self.assertRaises(
Exception,
Statistics.firing_rate_network(spike_train=np.asarray(Exc_activity)),
)
# Smoothness of the firing rate curve
self.assertRaises(
Exception,
Statistics.scale_dependent_smoothness_measure(
firing_rates=[1, 1, 5, 6, 3, 7]
),
)
# t lagged auto correlation between neurons given their firing rates
self.assertRaises(
Exception, Statistics.autocorr(firing_rates=[1, 1, 5, 6, 3, 7], t=2)
)
# Average of pearson correlation between neurons
self.assertRaises(
Exception, Statistics.avg_corr_coeff(spike_train=np.asarray(Exc_activity))
)
# Return the spike event times of each neuron in the pool
self.assertRaises(
Exception, Statistics.spike_times(spike_train=np.asarray(Exc_activity))
)
# Hamming distance measure for stability analysis
self.assertRaises(
Exception,
Statistics.hamming_distance(
actual_spike_train=np.asarray(Exc_activity),
perturbed_spike_train=np.asarray(Exc_activity),
),
)
# Inter spike interval of neurons
self.assertRaises(
Exception,
Statistics.spike_time_intervals(spike_train=np.asarray(Exc_activity)),
)
# Verify whether the neural spiking obeys poisson
self.assertRaises(
Exception,
Statistics.fanofactor(
spike_train=np.asarray(Exc_activity), neuron=10, window_size=10
),
)
# Degree of uncertainty in the origin of spiking
self.assertRaises(
Exception,
Statistics.spike_source_entropy(
spike_train=np.asarray(Exc_activity), num_neurons=200
),
)
if __name__ == "__main__":
unittest.main(argv=["first-arg-is-ignored"], exit=False)