forked from yangsu/Synaptic-Wall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoma.pde
250 lines (217 loc) · 6.57 KB
/
Soma.pde
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
class Soma extends Cell {
private ArrayList<Signal> fReceivedSignals;
protected ThresholdSlider fThresholdSlider;
protected CircularSlider fSpeedSlider, fLengthSlider, fDecaySlider;
protected float fSpeed, fLength, fDecay;
// Control IDS
private static final int SPEED = 1;
private static final int LENGTH = 2;
private static final int DECAY = 3;
private static final int THRESHOLD = 4;
private int fType;
Soma(float x, float y) {
this(
x, y, SOMA_SIZE, EX_COLOR,
SOMA_INIT_NEG_THRESHOLD,
SOMA_INIT_POS_THRESHOLD
);
}
Soma(float x, float y, float size, color cc, float negt, float post) {
super(x, y, size, cc);
fReceivedSignals = new ArrayList<Signal>();
fSpeed = SIGNAL_DEFAULT_SPEED;
fLength = SIGNAL_DEFAULT_LENGTH;
fDecay = SIGNAL_DEFAULT_DECAY;
fType = EXCITATORY;
float controlSize = fSize + 3 * SLIDER_BAR_WIDTH;
fSpeedSlider = new CircularSlider(
fLoc.x, fLoc.y, controlSize,
0, TWO_PI/3,
fSpeed, 1, SIGNAL_MAX_SPEED,
SPEED, this
);
fControls.add(fSpeedSlider);
fLengthSlider = new DiscreteCircularSlider(
fLoc.x, fLoc.y, controlSize,
TWO_PI/3, 2 * TWO_PI/3,
fLength, 1, SIGNAL_MAX_LENGTH,
LENGTH, this
);
fControls.add(fLengthSlider);
fDecaySlider = new CircularSlider(
fLoc.x, fLoc.y, controlSize,
2 * TWO_PI/3, TWO_PI,
fDecay, SIGNAL_MAX_DECAY, 1.0, //1.0 = no decay
DECAY, this
);
fControls.add(fDecaySlider);
fThresholdSlider = new ThresholdSlider(
x, y, fSize + SLIDER_BAR_WIDTH,
0, negt, post,
THRESHOLD, this
);
fControls.add(fThresholdSlider);
fLastFired = 0;
}
public int getType() {
return SOMA;
}
public float getThresholdValue() {
return fThresholdSlider.getValue();
}
public float getMinThreshold() {
return fThresholdSlider.getMin();
}
public void setMinThreshold(float value) {
fThresholdSlider.setMin(value);
}
public float getMaxThreshold() {
return fThresholdSlider.getMax();
}
public void setMaxThreshold(float value) {
fThresholdSlider.setMax(value);
}
private void drawControlDisplays() {
pushStyle();
color cc = fHighlightColor;
//Speed
noStroke();
fill(cc);
float h = 3/SCALE;
float w = h * sqrt(3);
float y = fLoc.y - 7/SCALE;
float offset = -fSpeed/2 * w;
for (int i = 0; i < fSpeed; ++i) {
triangle(fLoc.x + w/3 + offset, y - h,
fLoc.x + w/3 + offset, y + h,
fLoc.x + w + offset, y);
offset += w;
}
//Length
noFill();
stroke(cc);
float l = fLength/SIGNAL_MAX_LENGTH * 9/SCALE;
float offsetL = (fSize - 2*l)/2;
y = fLoc.y + 4/SCALE;
float y2 = y - 5/SCALE;
beginShape();
vertex(fLoc.x - l - offsetL, y);
vertex(fLoc.x - l, y);
vertex(fLoc.x - l, y2);
vertex(fLoc.x + l, y2);
vertex(fLoc.x + l, y);
vertex(fLoc.x + l + offsetL, y);
endShape();
//DECAY
noStroke();
color newcolor = lerpColor(cc & 0xFFFFFF, cc, fDecay);
fill(newcolor);
ellipse(fLoc.x, fLoc.y + 12/SCALE, 3/SCALE, 3/SCALE);
popStyle();
}
private static final float c = 2; // capacitance
private static final float r = 500; // resistance
private static final float rc = r * c;
private static final float ur = -0.5; // reset potential
private static final float u0 = 0; // resting potential
private static final float q = 2.3; // total charge
private static final float tc = -100000;
private static final float th = 1; // the neuron threshold
private static final float taus = 500; // time constant
private void processSignals() {
double seps = 0, eps = 0;
int now = millis();
int timeSinceLastFiring = now - fLastFired;
for (int i = fReceivedSignals.size()-1; i >= 0; --i) {
Signal s = fReceivedSignals.get(i);
int diff = now - s.fFiringTime;
float actualLength = s.fLength*SIGNAL_FIRING_MULTIPLIER + SIGNAL_SINGLE_FIRING_TIME;
if (fLastFired <= s.fFiringTime && s.fFiringTime <= now) {
eps = Math.exp(-diff/rc) - Math.exp(-diff/taus);
}
if (s.fFiringTime < fLastFired && fLastFired <= now) {
eps = Math.exp(-(fLastFired - s.fFiringTime)/taus)*(Math.exp(-timeSinceLastFiring/rc) - Math.exp(-timeSinceLastFiring/taus));
}
if (eps <= 0.005) {
fReceivedSignals.remove(s);
}
seps += s.fStrength * eps;
// float val = Util.pulse(s.fStrength, diff, actualLength);
}
double value = u0 + (ur - u0) * Math.exp(-timeSinceLastFiring / rc) + q/c * 1/(1-taus/rc) * seps;
if (value >= th) { // generate spike
value = ur;
fire();
}
fThresholdSlider.setValue((float)value);
}
public void update() {
processSignals();
}
public void drawBackground() {
drawControls();
pushStyle();
float s = fSize - SOMA_RING_WIDTH;
fill(fColor);
ellipse(fLoc.x, fLoc.y, s, s);
drawControlDisplays();
noStroke();
color c = SHADOW_COLOR;
ring(s, fLoc.x + SHADOW_OFFSETX, fLoc.y + SHADOW_OFFSETY, SOMA_RING_WIDTH, c);
popStyle();
}
public void drawForeground() {
pushStyle();
float s = fSize - SOMA_RING_WIDTH;
fill(fColor);
noStroke();
color c = lerpColor(fHighlightColor, HIGHLIGHT_COLOR, fThresholdSlider.getValue());
ring(s, fLoc.x, fLoc.y, SOMA_RING_WIDTH, c);
popStyle();
}
public void flipColor() {
super.flipColor();
fType ^= 1; // Flip between EXCITATORY and INHIBITORY
}
public void onSignal(Signal s) {
fReceivedSignals.add(s);
}
public void copyAttributes(Cell c) {
Soma s = (Soma)c;
fSpeed = s.fSpeed;
fSpeedSlider.setValue(fSpeed);
fLength = s.fLength;
fLengthSlider.setValue(fLength);
fDecay = s.fDecay;
fDecaySlider.setValue(fDecay);
setMinThreshold(s.getMinThreshold());
setMaxThreshold(s.getMaxThreshold());
}
protected boolean fireSignals() {
if ((millis() - fLastFired) >= SOMA_FIRING_DELAY) {
float val = (fType == EXCITATORY) ? SIGNAL_DEFAULT_STRENGTH : -SIGNAL_DEFAULT_STRENGTH;
for (Path p : fAxons)
p.addSignal(new ActionPotential(fSpeed, fLength, fDecay, val, p));
return true;
}
return false;
}
public void onEvent(int controlID, float value) {
switch (controlID) {
case SPEED:
fSpeed = value;
break;
case LENGTH:
fLength = value;
break;
case DECAY:
fDecay = value;
break;
case THRESHOLD:
// fire();
break;
default:
break;
}
}
}