-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
445 lines (319 loc) · 16.1 KB
/
classes.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
"""
Includes classes for all basic elements of the networks.
"""
# imports
import numpy as np
from scipy import signal
import copy as cp
# Global variables
image_size = np.array([250, 250]) # in um
pixel = 5 # in um
img_size = (image_size/pixel).astype(int) # number
temporal_res = 2 # in msec
t_time = 100 # in sec
###############################################################################
# Generic parent class for anything that is common across the building blocks
# (elements) of the circuits
class Element:
# Every element contains the name of its inputs and the corresponding weights
def __init__(self, inputs, weights, center):
# Inputs: array of input elements to this element
# Weights: corresponding weights from these elements
# Center: tuple indicating position in 2-D neural sheet. It is also center
# of the receptive field for bipolar cells
self.n_in = len(inputs)
self.w = weights
# Inputs is a list of objects. I want to compute their
# output and use it is the output method
self.inputs = inputs
# Needed to compute which elements to connect to this element
self.center = center
# preallocate matrix of activity for this cell
self.output = np.nan
# Can be used to see if the cell has computed its output, to avoid
# uneccesary computations. Can also be replaced by a time marker if the
# network cannot be computed all at once
###############################################################################
class BipolarCell(Element):
def __init__(self, inputs, weights, center, attributes):
# Attributes: contains a list of attributes needed to define the cell. Can contain:
# type: can be "On" or "Off"
# separable: determines whether spatiotemporal field is separable, boolean
# spatiotemporal: contains given spatiotemporal receptive field
# spatial: contains spatial receptive field or function name to produce it
# width: width of the spatial receptive field
# temporal: contains temporal receptive field or function name to produce it
# duration: "time constant" of the temporal receptive field
# activation: the nonlinear activation function of the output
# threshold: threshold for the activation function
# Can also contain other parameters required to define the receptive
# field, look at respective function for what their name and
# specification should be
super().__init__(inputs, weights, center)
self.type = attributes["type"]
self.separable = attributes["separable"]
if self.separable:
if isinstance(attributes["spatial"],np.ndarray):
self.spatial = attributes["spatial"]
else:
self.spatial = Spatial(self.center,attributes)
if isinstance(attributes["temporal"],np.ndarray):
self.temporal = attributes["temporal"]
else:
self.temporal = Temporal(attributes)
# Spatiotemporal receptive field is spatial * temporal
temp1 = self.temporal[np.newaxis,:]; temp1 = temp1[np.newaxis,:]
temp2 = np.expand_dims(self.spatial,2)
self.spatiotemporal = temp2*temp1
else:
self.spatiotemporal = attributes["spatiotemporal"]
self.activation = attributes["activation"]
self.threshold = attributes["threshold"]
def out(self):
# Since there is no recurrent connectivity involving bipolar cells,
# we can compute all the output and then sample it from the other cells
if not np.any(np.isnan(self.output)):
pass
else:
# the first element of the list 'inputs' should contain the image
temp = signal.fftconvolve(self.inputs[0],self.spatiotemporal,'full',axes = 2)[:,:,0:np.size(self.inputs[0],2)]
self.output = activation(np.sum(temp,axis = (0,1)),self.activation,self.threshold)
return self.output
###############################################################################
class AmacrineCell(Element):
def __init__(self, inputs, weights, center, attributes):
# Attributes: contains a list of attributes needed to define the cell. Can contain:
# temporal: contains temporal receptive fields as matrix or list
# of function names to produce them. Each input has a different
# corresponding receptive field
# duration: array of "time constants" of temporal receptive fields
# activation: the nonlinear activation function of the output
# threshold: threshold for the activation function
# Can also contain other parameters required to define the receptive
# field, look at respective function for what their name should be
super().__init__(inputs, weights, center)
if isinstance(attributes["temporal"],np.ndarray):
self.temporal = attributes["temporal"]
else:
self.temporal = Temporal_multiple(attributes,self.n_in)
self.activation = attributes["activation"]
self.threshold = attributes["threshold"]
if "recurrent" in attributes:
self.recurrent = attributes["recurrent"]
else:
self.recurrent = np.nan
def out(self):
# Amacrine cells receive recurrent connections
# assuming that inputs is a list of input objects
if not np.any(np.isnan(self.output)):
pass
else:
values = np.asarray(list(map(lambda x: x.out(),self.inputs)))
for i in range(self.n_in):
# Different temporal receptive field for each input
values[i,:] = signal.fftconvolve(values[i,:],self.temporal[i],'full')[0:np.size(values[i,:])]
# Use transpose to do multiplication with np.dot
temp = np.dot(values.transpose(),self.w).transpose()
try:
if np.isnan(self.recurrent):
self.output = activation(temp,self.activation,self.threshold)
except:
time_p = np.size(temp,0); l = np.size(self.recurrent)
self.output = np.zeros((time_p,))
for t in range(time_p):
if t < l:
self.output[t] = activation(temp[t],self.activation,self.threshold)
else:
temp[t] = temp[t] + np.dot(self.output[t-l:t],self.recurrent)
self.output[t] = activation(temp[t],self.activation,self.threshold)
return self.output
###############################################################################
class GanglionCell(Element):
def __init__(self, inputs, weights, center, attributes):
# Attributes: contains a list of attributes needed to define the cell. Can contain:
# temporal: contains temporal receptive fields as matrix or list
# of function names to produce them. Each input has a different
# corresponding receptive field
# duration: array of "time constants" of temporal receptive fields
# activation: the nonlinear activation function of the output
# threshold: threshold for the activation function
# recurrent: recurrent filter coefficients
# Can also contain other parameters required to define the receptive
# field, look at respective function for what their name should be
super().__init__(inputs, weights, center)
if isinstance(attributes["temporal"],np.ndarray):
self.temporal = attributes["temporal"]
else:
self.temporal = Temporal_multiple(attributes,self.n_in)
self.activation = attributes["activation"]
self.threshold = attributes["threshold"]
if "recurrent" in attributes:
self.recurrent = attributes["recurrent"]
else:
self.recurrent = np.nan
def out(self):
# Ganglion cells receive recurrent connections
# assuming that inputs is a list of input objects
if not np.any(np.isnan(self.output)):
pass
else:
# care must be taken that the length of all arrays is equal. So do it
# for values, after this it is ok
values = np.asarray(list(map(lambda x: x.out(),self.inputs)))
for i in range(self.n_in):
# Different temporal receptive field for each input
values[i,:] = signal.fftconvolve(values[i,:],self.temporal[i],'full')[0:np.size(values[i,:])]
# Use transpose to do multiplication with np.dot
temp = np.dot(values.transpose(),self.w).transpose()
try:
if np.isnan(self.recurrent):
self.output = activation(temp,self.activation,self.threshold)
except:
time_p = np.size(temp); l = np.size(self.recurrent)
self.output = np.zeros((time_p,))
for t in range(time_p):
if t < l:
self.output[t] = activation(temp[t],self.activation,self.threshold)
else:
temp[t] = temp[t] + np.dot(self.output[t-l:t],self.recurrent)
self.output[t] = activation(temp[t],self.activation,self.threshold)
return self.output
###############################################################################
class Delay(Element):
def __init__(self, inputs, weights, center, attributes):
# Attributes can contain:
# t_delay : time delay the element introduces, in time units
super().__init__(inputs, weights, center)
# convert time to count
self.delay = int(attributes["t_delay"])
def out(self):
if not np.any(np.isnan(self.output)):
pass
else:
self.output = np.roll(self.inputs[0].out(),self.delay)
self.output[0:self.delay] = 0
return self.output
###############################################################################
class PresynapticSilencer(Element):
# Used so that amacrine cells can silence bipolar cells before reaching
# cells. Necessary component for OMS cells
def __init__(self, inputs, weights, center, attributes):
super().__init__(inputs, weights, center)
def out(self):
if not np.any(np.isnan(self.output)):
pass
else:
values = np.asarray(list(map(lambda x: x.out(),self.inputs)))
self.output = np.dot(values.transpose(),self.w).transpose()
return self.output
###############################################################################
# Functions for spatial attributes
def Spatial(center,attributes):
# Define spatial receptive fields. Options:
# difference of gaussians: "spatial" should be "DoG", other parameters
# needed in "attributes": "width", "center", "on_off_ratio"
# Access global variables used throughout
global img_size
if np.array_equal(attributes["spatial"],'DoG'):
spatial = DoG(attributes["width"],attributes["on_off_ratio"],center,img_size)
if np.array_equal(attributes["spatial"],'Gauss'):
spatial = Gauss(attributes["width"],center,img_size)
spatial = norm(spatial)
return spatial
def DoG(sigmas,ratio,center,img_size):
# Sigmas contain the standard deviations the positive (center) and negative
# (surround) part. Ratio is the ratio of the peaks of the gaussians (center/surround)
x = np.arange(0,img_size[0])
y = np.arange(0,img_size[1])
X, Y = np.meshgrid(x,y)
norm_dist1 = 1/2*(((X-center[0])**2+(Y-center[1])**2)/sigmas[0]**2)
norm_dist2 = 1/2*(((X-center[0])**2+(Y-center[1])**2)/sigmas[1]**2)
gauss1 = ratio/(2*np.pi*sigmas[0]**2)*np.exp(-norm_dist1)
gauss2 = 1/(2*np.pi*sigmas[1]**2)*np.exp(-norm_dist2)
# Normalization? Should it not be zero sum? (no reaction to constant input)
return (gauss1 - gauss2)/(1+ratio)
def Gauss(sigma,center,img_size):
# Sigma: standard deviation of the receptive field
x = np.arange(0,img_size[0])
y = np.arange(0,img_size[1])
X, Y = np.meshgrid(x,y)
norm_dist = 1/2*(((X-center[0])**2+(Y-center[1])**2)/sigma**2)
gauss =1/(2*np.pi*sigma**2)*np.exp(-norm_dist)
return gauss
def Gaussian(x,y,sigmax,sigmay,peak):
norm_dist = 1/2*(x**2/sigmax**2+y**2/sigmay**2)
return peak*np.exp(-norm_dist)
###############################################################################
# Functions for temporal attributes
def Temporal_multiple(attributes,n):
# Unpacks contents of attributes and passes them one at a time to Temporal()
# Returns list of receptive fields, each one corresponding to one input
temporals = [None]*n
atts = cp.deepcopy(attributes) # Changes in atts should not affect attributes
for i in range(n):
atts["temporal"] = attributes["temporal"][i]
atts["duration"] = attributes["duration"][i]
atts["coeffs"] = attributes["coeffs"][i]
temporals[i] = Temporal(atts)
return temporals
def Temporal(attributes):
# Define temporal receptive fields. Options:
# Adelson and Bergen 1985: "temporal" should be "Adelson_Bergen", other
# parameters needed in "attributes": "duration"
# Stretched sin: "temporal" should be "stretched_sin", other parameters
# needed: "duration", "coeffs"
if np.array_equal(attributes["temporal"],'Adelson_Bergen'):
temporal = Adelson_Bergen(attributes["duration"])
elif np.array_equal(attributes["temporal"],'stretched_sin'):
temporal = stretched_sin(attributes["duration"],attributes["coeffs"])
temporal = norm(temporal)
return temporal
def Adelson_Bergen(duration):
# Alpha acts as an inverse time constant. Equation (2.29) from Dayan & Abbott
alpha = 20/duration
t = np.arange(duration)
norm_t = alpha*t
return alpha*np.exp(-norm_t)*(norm_t**5/np.math.factorial(5)-norm_t**7/np.math.factorial(7))
def stretched_sin(tf,coeffs):
# Computes equation (5) from Keat et al 2001
# tf is the maximal length of the filter
# coeffs should be a numpy array with the corresponding coefficient of the
# n-th term of (5) in position n-1
filt = 0
for i in range(len(coeffs)):
filt = filt + coeffs[i]*stretched_sin_basis(tf,i+1)
return filt
def stretched_sin_basis(tf,order):
# Equation (6) from Keat et al 2001
t = np.arange(tf)
norm_t = t/tf
return np.sin(np.pi*order*(2*norm_t-norm_t**2))
###############################################################################
# Functions for activations
def activation(h,function,threshold):
# Computes output of elements. Options:
# "relu", "sigmoid"
if np.array_equal(function,"relu"):
out = relu(h,threshold)
elif np.array_equal(function,"sigmoid"):
out = sigmoid(h,threshold)
return out
def relu(h,threshold,gain = 1):
# could define gain for each cell
out = h-threshold;
if np.isscalar(out):
if out<0:
out = 0
else:
out[out<0] = 0
return gain*out
def sigmoid(h,threshold,k=.5,b=5,s=1):
# could define k, b and s separately for each cell
return 1/(1+k*np.exp(-b*(h-threshold)))
###############################################################################
# Utils
def norm(array):
coeff = np.linalg.norm(array)
if coeff>0:
array = array/coeff
return array