Repository navigation
Expand file tree
/
Copy pathwrapper.py
More file actions
executable file
·279 lines (231 loc) · 9.73 KB
/
Copy pathwrapper.py
File metadata and controls
executable file
·279 lines (231 loc) · 9.73 KB
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
__author__ = "hanyi"
from TwoLayerTopology import TwoLayerTopology
import numpy as np
from types import FunctionType
import random
import scipy.io
import pylab
import matplotlib.pyplot as plt
#--------------------------------------------------#
# Generating the training E-Pockets from recorded data
#--------------------------------------------------#
print "---------------------------------------------"
print "Generating Trajectory E-Pockets: Need Processed Data"
last_episodes = np.repeat(np.array([0, 9, 18, 27]), 1)
order = []
num_training = 80
# This function selects the E-pocket randomly according to the num_training value
def create_order(num_copy):
order = np.array([1])
order_temp = []
for j in range(num_copy):
order_temp.append(random.randrange(0, 9))
for j in range(num_copy):
order_temp.append(random.randrange(9, 18))
for j in range(num_copy):
order_temp.append(random.randrange(18, 27))
for j in range(num_copy):
order_temp.append(random.randrange(27, 36))
order = np.concatenate((order, order_temp))
return order
order = create_order(num_training)
# order = np.concatenate((order, last_episodes))
print "---------------------------------------------"
print "Importing pre-recorded data"
#--------------------------------------------------#
# Importing pre-recorded data
#--------------------------------------------------#
argw = []
def get_data(filename):
dvs_data = scipy.io.loadmat(filename)
ts = dvs_data['ts'][0]
ts = (ts - ts[0]) # from us to ms
x = dvs_data['X'][0]
y = dvs_data['Y'][0]
p = dvs_data['t'][0]
return x, y, p, ts
def data_set(prefix):
postfix = '.mat'
for i in range(1, 10):
argw.append(get_data(prefix + str(i) + postfix))
data_set('l_to_r')
data_set('r_to_l')
data_set('t_to_b')
data_set('b_to_t')
print "---------------------------------------------"
#--------------------------------------------------#
# Raster plot of input recording
#--------------------------------------------------#
print "---------------------------------------------"
print "Raster plot of input recording"
# This function creates a raster plot of 4 input recordings in 4 direction
def raster_plot_4_dir():
pylab.figure()
pylab.xlabel('Time/ms')
pylab.ylabel('spikes')
for ii in range(0, 4):
pylab.plot(argw[9*ii][3]+200*(2*ii+1), argw[9*ii][0]+argw[9*ii][1]*16, ".")
pylab.title('Raster Plot of Virtual Retina Neuron Population in 4 Direction')
pylab.xlim((0, 1800))
pylab.ylim((0, 270))
pylab.show()
# This function creates a raster plot of 36 input recordings in 4 direction
def raster_plot():
pylab.figure()
pylab.xlabel('Time/ms')
pylab.ylabel('spikes')
for ii in range(0, len(argw)):
pylab.plot(argw[ii][3]+200*(ii+ii/9), argw[ii][0]+argw[ii][1]*16, ".")
pylab.axvline(1900, 0, 1, linewidth=4, color='r', alpha=0.75, linestyle='dashed')
pylab.axvline(3900, 0, 1, linewidth=4, color='r', alpha=0.75, linestyle='dashed')
pylab.axvline(5900, 0, 1, linewidth=4, color='r', alpha=0.75, linestyle='dashed')
pylab.title('Raster Plot of 36 Neuron Population Training Sets')
pylab.ylim((0, 270))
pylab.show()
raster_plot()
#--------------------------------------------------#
# Excitatory Mode: Observe membrane potential change of one pre-synaptic neuron
# Inhibitory Mode: Observe membrane potential change of one pre-synaptic neuron
# Training Mode: Used for training the system
#--------------------------------------------------#
excitatory_mode = False
inhibitory_mode = False
training_mode = True
print "---------------------------------------------"
print "Setting up neural network parameters"
#--------------------------------------------------#
# Defining Network Parameters
#--------------------------------------------------#
# STDP learning rule parameters
stdp_param = {'tau_plus': 50.0,
'tau_minus': 60.0,
'w_min': 0,
'w_max': 1,
'A_plus': 0.05,
'A_minus': 0.05}
# Leaky integrate and fire model parameters
cell_params_lif = {'cm': 12,
'i_offset': 0.0,
'tau_m': 110,
'tau_refrac': 40.0,
'tau_syn_E': 5.0,
'tau_syn_I': 10.0,
'v_reset': -70.0,
'v_rest': -65.0,
'v_thresh': -61.0}
# Synaptic delay
delay = 1
# Imports the trained weights to see the evolution of the weight
NetworkInfo = scipy.io.loadmat('trained_weight4dir.mat')
weights_import = NetworkInfo['trained_weight']
# This function converts the weights from a MatLab file to a list
def convert_weights_to_list(matrix, delay):
def build_list(indices):
# Extract weights from matrix using indices
weights = matrix[indices]
# Build np array of delays
delays = np.repeat(delay, len(weights))
# Zip x-y coordinates of non-zero weights with weights and delays
return zip(indices[0], indices[1], weights, delays)
# Get indices of non-nan i.e. connected weights
connected_indices = np.where(~np.isnan(matrix))
# Return connection lists
return build_list(connected_indices)
# This function build the input spike events from the trajectory E-Pockets
def BuildTrainingSpike_with_noise(order, ONOFF, noise_spikes):
noise_nid = []
for i in range(0, len(order)):
noisetemp = np.random.randint(0, 256, noise_spikes)
noisetemp.sort()
noisetemp = noisetemp.tolist()
noise_nid.append(noisetemp)
# print len(noise_nid)
complete_Time = []
for nid in range(0, pre_pop_size):
SpikeTimes = []
for tid in range(0, len(order)):
# print dead_zone_cnt
temp = []
loc = order[tid]
j = np.repeat(nid, len(argw[loc][1]))
p = np.repeat(ONOFF, len(argw[loc][1]))
temp = 200*(2*tid+1)+argw[loc][3][(j % 16 == argw[loc][0]) &
(j / 16 == argw[loc][1]) &
(p == argw[loc][2])]
if(nid in noise_nid[tid]):
t_noise = 200*(2*tid+1) + np.random.uniform(0, 200, 1)
temp = np.concatenate((temp, t_noise))
temp.sort()
if temp.size > 0:
SpikeTimes = np.concatenate((SpikeTimes, temp))
if type(SpikeTimes) is not list:
complete_Time.append(SpikeTimes.tolist())
else:
complete_Time.append([])
return complete_Time
# period of trajectory time
animation_time = 200
# on off event selection: ON 1, OFF 0
# CANNOT USE OFF EVENT, FILTERED BY INPUT_NORMALISER.PY
ON_OFF = 1
num_noise = 11
if(training_mode):
pre_pop_size = 256
post_pop_size = 4
test_STDP = False
stdp_mode = True
inhibitory_spike_mode = False
allsameweight = False
self_import = False
in_spike = BuildTrainingSpike_with_noise(order, ON_OFF, num_noise)
print(in_spike)
sim_time = (2 * len(order) + 1) * animation_time
else:
test_STDP = True
sim_time = 4000
if(inhibitory_mode):
pre_pop_size = 4
post_pop_size = 1
stdp_mode = False
inhibitory_spike_mode = True
allsameweight = True
self_import = False
in_spike = [[10], [], [], []]
if(excitatory_mode):
pre_pop_size = 256
post_pop_size = 1
stdp_mode = True
inhibitory_spike_mode = False
allsameweight = False
self_import = True
# use the following line to show the membrane potential change from 1 pre-synaptic spike
# in_spike = [[10], [], [], []]
# use the following line to show the membrane potential change from spikes in 1 E-Pockets trajectory
in_spike = BuildTrainingSpike_with_noise(order, ON_OFF, num_noise)
sim_time = (2 * len(order) + 1) * animation_time
# e_syn_weight = np.random.normal(0.2, 0.003, pre_pop_size*post_pop_size).tolist()
e_syn_weight = [0.2, 0.1, 0.2, 0.0]
i_syn_weight = 15.0
print "---------------------------------------------"
print "Simulation Setup & Initialisation"
#--------------------------------------------------#
# Simulation Setup & Initialisation
#--------------------------------------------------#
setup_cond = {'timestep': 1,
'min_delay': 1,
'max_delay': 144}
list = convert_weights_to_list(weights_import, delay)
simulation = TwoLayerTopology(pre_pop_size=pre_pop_size, post_pop_size=post_pop_size,
e_syn_weight=e_syn_weight, i_syn_weight=i_syn_weight,
cell_params_lif=cell_params_lif, setup_cond=setup_cond,
stdp_param=stdp_param, stdp_mode=stdp_mode,
inhibitory_spike_mode=inhibitory_spike_mode,
allsameweight=allsameweight)
simulation.input_spike(in_spike)
simulation.connection_list_converter_uniform(self_import=self_import, conn_list=list, min=0.1, max=0.4, delay=1.0)
simulation.start_sim(sim_time)
simulation.plot_spikes("input", pre_pop_size, "Spike Pattern of Pre-Synaptic Population")
simulation.plot_spikes("output", post_pop_size, "Spike Pattern of Post-Synaptic Population")
'''sim1.display_membrane_potential("Membrane Potential of one Post-Synaptic Neuron",
xmin=0, xmax=400, ymin=-75, ymax=-57)'''
simulation.Plot_WeightDistribution(pre_pop_size,'Histogram of Trained Weight')