/
Neuron.m
541 lines (297 loc) · 18.1 KB
/
Neuron.m
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
538
539
540
541
classdef Neuron < handle
properties
id = 'neuron';
t = 1;
u = 0; % activation state of a Neuron
v = 0; % change detection state
a = 0; % adaptation state
aFr = 0; % firing rate adaptation state
fu = 0; % interaction state of a Neuron
fu2 = 0;
h = -10; % resting level of a Neuron
hV = 0; % resting level of change detection state
hA = 0; % resting level of adaptation state
tau = 30; % time constant of a Neuron
tauV = 60; % time constant change detection
tauA = 100; % time constant adaptation
tauAFr = 100; % time constant adaption firing rate
vCoefficient = 0;
aCoefficient = 0;
aFrCoefficient = 0;
noiseCoefficient = 7;%7 for v7 and v9; %10 is the sweet spot for v4
interaction = struct('slope', 1, 'midpoint', 0); % parameters for interaction function
interaction2 = struct('slope', 1, 'midpoint', -10); % parameters for interaction function
input = {}; % input
changeDetectionBypassInput = {}; % use this for e.g. feedback to preactivate change detectors that itseld does not go through change detection
end
methods
function obj = Neuron(dim1, dim2, dim3, dim4)
if nargin == 1
for i = 1:dim1
obj(i) = Neuron;
obj(i).id = [num2str(i)];
end
end
if nargin == 2
for i = 1:dim1
for j = 1:dim2
obj(i,j) = Neuron;
obj(i,j).id = [num2str(i) num2str(j)];
end
end
end
if nargin == 3
for i = 1:dim1
for j = 1:dim2
for k = 1:dim3
obj(i,j,k) = Neuron;
obj(i,j,k).id = [num2str(i) num2str(j) num2str(k)];
end
end
end
end
if nargin == 4
for i = 1:dim1
for j = 1:dim2
for k = 1:dim3
for l = 1:dim4
obj(i,j,k,l) = Neuron;
obj(i,j,k,l).id = [num2str(i) num2str(j) num2str(k) num2str(l)];
end
end
end
end
end
end % Constructor Method
function [] = init(obj,T) % initiate t to 1 and all state variables to their resting levels
for i = 1:numel(obj)
if nargin == 2
obj(i).u = zeros(1,T+1) ;
obj(i).v = zeros(1,T+1) ;
obj(i).a = zeros(1,T+1) ;
obj(i).aFr = zeros(1,T+1) ;
obj(i).fu = zeros(1,T+1);
obj(i).fu2 = zeros(1,T+1);
end
obj(i).t = 1;
obj(i).u(1) = obj(i).h;
obj(i).v(1) = obj(i).hV;
obj(i).a(1) = obj(i).hA;
obj(i).aFr(1) = 0;
obj(i).fu(1) = 1/(1 + exp(-obj(i).interaction.slope.*(obj(i).h-obj(i).interaction.midpoint)));
obj(i).fu2(1) = 1/(1 + exp(-obj(i).interaction2.slope.*(obj(i).h-obj(i).interaction2.midpoint)));
end
end
function outputArray = output(obj,t)
outputArray = zeros(size(obj));
for i = 1:numel(obj)
if nargin == 1 % if no time argument use current time of Neuron
t = obj(i).t;
end
outputArray(i) = obj(i).fu(t);
end
end
function outputSum = sum(obj,t)
if nargin == 1 % if no time argument use current time of the first Neuron in the array
t = obj(1).t;
end
outputSum = sum(output(obj, t));
for i = 1:ndims(output(obj, t))
outputSum = sum(outputSum);
end
% outputSum = sum(output(obj));
end
function outputPlus = plus(a,b)
outputPlus = output(a) + output(b);
end
function [] = addInput(obj,newInput)
for i = 1:numel(obj)
obj(i).input{size(obj(i).input,1)+1,1} = newInput;
end
end
function [] = addBypassInput(obj,newInput)
for i = 1:numel(obj)
obj(i).changeDetectionBypassInput{size(obj(i).changeDetectionBypassInput,1)+1,1} = newInput;
end
end
function [] = step(obj) % function that steps a neuron forward in time
for i = 1:numel(obj)
if iscell(obj(i).input) %check if input is cell array
tempInput = 0; % reset the temporary input value to 0
% calculate temporary input based on input cell array
for currentInput = 1:size(obj(i).input,1)
tempSubInput = 0; % tempSubInput keeps track of values to multiply for a given multiplicative synapse
for currentSubInput = 1:numel(obj(i).input{currentInput})
if isfloat(obj(i).input{currentInput}{currentSubInput}) % check if it is a numerical input
if numel(obj(i).input{currentInput}{currentSubInput}) > 1 %check if it is a static input or a time-series
if obj(i).t <= numel(obj(i).input{currentInput}{currentSubInput}) % if it is a time series, either take the value from current t, or 0 if simulation runs longer than stimulus
tempSubInput(currentSubInput) = obj(i).input{currentInput}{currentSubInput}(obj(i).t);
else
tempSubInput(currentSubInput) = 0;
end
else
tempSubInput(currentSubInput) = obj(i).input{currentInput}{currentSubInput}; % static inputs are delivered as continuous input
end
else
tempSubInput(currentSubInput) = sum(obj(i).input{currentInput}{currentSubInput}, obj(i).t); % if it is not a float, hopefully it is a neuron, take its output at time t
end
end
tempInput = tempInput + prod(tempSubInput);
end
else
tempInput = obj(i).input;
end
% calculate inputs that bypass change detection
if iscell(obj(i).changeDetectionBypassInput)
tempBypassInput = 0;
for currentInput = 1:size(obj(i).changeDetectionBypassInput,1)
tempBypassSubInput = 0; % tempSubInput keeps track of values to multiply for a given multiplicative synapse
for currentSubInput = 1:numel(obj(i).changeDetectionBypassInput{currentInput})
if isfloat(obj(i).changeDetectionBypassInput{currentInput}{currentSubInput}) % check if it is a numerical input
if numel(obj(i).changeDetectionBypassInput{currentInput}{currentSubInput}) > 1 %check if it is a static input or a time-series
if obj(i).t <= numel(obj(i).changeDetectionBypassInput{currentInput}{currentSubInput}) % if it is a time series, either take the value from current t, or 0 if simulation runs longer than stimulus
tempBypassSubInput(currentSubInput) = obj(i).changeDetectionBypassInput{currentInput}{currentSubInput}(obj(i).t);
else
tempBypassSubInput(currentSubInput) = 0;
end
else
tempBypassSubInput(currentSubInput) = obj(i).changeDetectionBypassInput{currentInput}{currentSubInput}; % static inputs are delivered as continuous input
end
else
tempBypassSubInput(currentSubInput) = sum(obj(i).changeDetectionBypassInput{currentInput}{currentSubInput}, obj(i).t); % if it is not a float, hopefully it is a neuron, take its output at time t
end
end
tempBypassInput = tempBypassInput + prod(tempBypassSubInput);
end
else
tempBypassInput = obj(i).changeDetectionBypassInput;
end
% implement step
obj(i).u(obj(i).t+1) = obj(i).u(obj(i).t)...
+ 1/obj(i).tau * (-obj(i).u(obj(i).t)...
+ obj(i).h...
- obj(i).vCoefficient * obj(i).v(obj(i).t)...
- obj(i).aCoefficient * obj(i).a(obj(i).t)...
- obj(i).aFrCoefficient * obj(i).aFr(obj(i).t)...
+ tempInput...
+ tempBypassInput...
+ obj(i).noiseCoefficient*randn(1));
%
%
obj(i).v(obj(i).t+1) = obj(i).v(obj(i).t)...
+ 1/obj(i).tauV * (-obj(i).v(obj(i).t)...
+ obj(i).hV + tempInput);
obj(i).a(obj(i).t+1) = obj(i).a(obj(i).t)...
+ 1/obj(i).tauA * (-obj(i).a(obj(i).t)...
+ obj(i).hA + obj(i).u(obj(i).t));
obj(i).aFr(obj(i).t+1) = obj(i).aFr(obj(i).t)...
+ 1/obj(i).tauAFr * (-obj(i).aFr(obj(i).t)...
+ output(obj(i), obj(i).t));
obj(i).fu(obj(i).t+1) = 1/(1 + exp(-obj(i).interaction.slope.*(obj(i).u(obj(i).t+1)-obj(i).interaction.midpoint)));
obj(i).fu2(obj(i).t+1) = 1/(1 + exp(-obj(i).interaction2.slope.*(obj(i).u(obj(i).t+1)-obj(i).interaction2.midpoint)));
obj(i).t = obj(i).t + 1;
end
end
function [] = plot(obj)
for i = 1:numel(obj)
hold on
plot(obj(i).u,'k')
if obj(i).vCoefficient ~= 0
%plot(obj(i).vCoefficient*obj(i).v,'r')
end
if obj(i).aCoefficient ~= 0
plot(obj(i).aCoefficient*obj(i).a,'b')
end
if obj(i).aFrCoefficient ~= 0
plot(obj(i).aFrCoefficient*obj(i).aFr,'g')
end
end
end
function [] = map(obj, threshold, t) % find activations above threshold in 4d array and map them with arrows
% cla
if ndims(obj) ==2
n = size(obj,1);
motionIndices = find(output(obj, t)>threshold);
if isempty(motionIndices)
else
for i = 1:length(motionIndices)
[mTemp(i,1) mTemp(i,2)] = ind2sub([n n], motionIndices(i));
end
mTemp(:,2) = mTemp(:,2) - mTemp(:,1);
m = [mTemp(:,1) ones(size(mTemp,1),1) mTemp(:,2) zeros(size(mTemp,1),1)];
for i = 1:length(motionIndices)
hold on
quiver(m(i,1), m(i,2), m(i,3), m(i,4),'b', 'LineWidth',2, 'MaxHeadSize', 0.5)
axis([.5 n+.5 .5 1.5])
end
end
end
if ndims(obj) == 4
n = size(obj,1);
if nargin == 2
motionIndices = find(output(obj)>threshold);
elseif nargin == 3
motionIndices = find(output(obj, t)>threshold);
if isempty(motionIndices)
% plot(0,0)
else
for i = 1:length(motionIndices)
[m(i,1) m(i,2) m(i,3) m(i,4)] = ind2sub([n n n n], motionIndices(i));
end
m(:,3) = m(:,3) - m(:,1);
m(:,4) = m(:,4) - m(:,2);
m = [m(:,2) m(:,1) m(:,4) m(:,3)];
for i = 1:length(motionIndices)
hold on
quiver(m(i,1), m(i,2), m(i,3), m(i,4),'b', 'LineWidth',2)
axis([.5 n+.5 .5 n+.5])
%quiver(m(i,1), m(i,2), m(i,3), m(i,4),'b', 'LineWidth',2)
end
end
end
end
end
function [] = connectStim(obj, stim, synapticStrength)
for i = 1:size(obj,1)
for j = 1:size(obj,2)
addInput(obj(i,j), {synapticStrength, stim(i,j,:)});
end
end
end
function [] = setvCoefficient(obj, vCoefficient)
for i = 1:numel(obj)
obj(i).vCoefficient = vCoefficient;
end
end
function [] = setaFrCoefficient(obj, aFrCoefficient)
for i = 1:numel(obj)
obj(i).aFrCoefficient = aFrCoefficient;
end
end
function [] = settauAFr(obj, tauAFr)
for i = 1:numel(obj)
obj(i).tauAFr = tauAFr;
end
end
function [] = copyU(obj1, obj2)
if size(obj1) ~= size(obj2)
error('not the same size, cant copy u')
else
for i = 1:numel(obj1)
obj2(i).u(obj1(i).t) = obj1(i).u(obj1(i).t);
obj2(i).fu(obj1(i).t) = 1/(1 + exp(-obj2(i).interaction.slope.*(obj2(i).u(obj1(i).t) -obj2(i).interaction.midpoint)));
end
end
end
function [] = setInteractionMidpoint(obj, midpoint)
for i = 1:numel(obj)
obj(i).interaction.midpoint = midpoint;
end
end
function [] = setH(obj, h)
for i = 1:numel(obj)
obj(i).h = h;
end
end
end
end