Skip to content

Commit b2f1069

Browse files
committed
Major refactoring effort, added random neuron, need to fix todos tomorrow
1 parent c018d6a commit b2f1069

12 files changed

+134
-118
lines changed

actionPotential.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ class ActionPotential {
88
this.charge = charge;
99

1010
skew -= this.size/2;
11-
this.xCoord = sourceNeuron.xCoord + skew;
12-
this.yCoord = sourceNeuron.yCoord + skew;
13-
this.destinyX = destinyNeuron.xCoord + skew;
14-
this.destinyY = destinyNeuron.yCoord + skew;
15-
16-
let length = Math.sqrt(Math.pow(this.destinyX-this.xCoord, 2) + Math.pow(this.destinyY-this.yCoord, 2));
17-
this.xSlope = (this.destinyX-this.xCoord)/length * 2;
18-
this.ySlope = (this.destinyY-this.yCoord)/length * 2;
19-
this.maxSteps = this.xSlope === 0 ? (this.destinyY-this.yCoord)/this.ySlope : (this.destinyX-this.xCoord)/this.xSlope;
11+
this.x = sourceNeuron.x + skew;
12+
this.y = sourceNeuron.y + skew;
13+
this.destinyX = destinyNeuron.x + skew;
14+
this.destinyY = destinyNeuron.y + skew;
15+
16+
let length = Math.sqrt(Math.pow(this.destinyX-this.x, 2) + Math.pow(this.destinyY-this.y, 2));
17+
this.xSlope = (this.destinyX-this.x)/length * 2;
18+
this.ySlope = (this.destinyY-this.y)/length * 2;
19+
this.maxSteps = this.xSlope === 0 ? (this.destinyY-this.y)/this.ySlope : (this.destinyX-this.x)/this.xSlope;
2020
this.steps = 0;
2121
}
2222

@@ -25,13 +25,13 @@ class ActionPotential {
2525
noStroke();
2626

2727
this.charge > 0 ? fill(255) : fill(0);
28-
rect(this.xCoord, this.yCoord, this.size, this.size);
28+
rect(this.x, this.y, this.size, this.size);
2929
this.move();
3030
}
3131

3232
move() {
33-
this.xCoord += this.xSlope;
34-
this.yCoord += this.ySlope;
33+
this.x += this.xSlope;
34+
this.y += this.ySlope;
3535
this.steps++;
3636

3737
if (this.steps >= this.maxSteps) {

characterNeuron.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class CharacterNeuron extends Neuron {
77
display() {
88
this.selected ? fill(200, 200, 60) : fill(255, 255, 255);
99
stroke(sketchOptions.gridColor);
10-
rect(this.xCoord, this.yCoord, this.size, this.size);
10+
rect(this.x, this.y, this.size, this.size);
1111
fill(0);
1212
textAlign(CENTER);
1313
textSize(this.size);
1414

1515
let letter = '';
1616
this.curCharge < 0 ? letter = this.curCharge : letter = this.charIndex[this.curCharge];
17-
text(letter, this.xCoord + this.size/2, this.yCoord + this.size/1.15);
17+
text(letter, this.x + this.size/2, this.y + this.size/1.15);
1818
}
1919
}

exampleNeuronNets.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src='characterNeuron.js'></script>
1212
<script src='negativeNeuron.js'></script>
1313
<script src='timerNeuron.js'></script>
14+
<script src='randomNeuron.js'></script>
1415
<script src='exampleNeuronNets.js'></script>
1516
<script src='sketch.js'></script>
1617
<script src='userDomInteractions.js'></script>
@@ -27,11 +28,6 @@
2728
<p>Place/Delete Neurons</p>
2829
Neuron Type:
2930
<select id='neuronType'>
30-
<option value='normal'>Normal</option>
31-
<option value='character'>Character</option>
32-
<option value='negative'>Negative</option>
33-
<option value='timer'>Timer</option>
34-
<option value='note'>Note</option>
3531
</select><br>
3632

3733
<span id='activationLevelSpan'>

jasmine/spec/neuron_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ describe('Neuron Class Methods', () => {
55
});
66

77
it('should create a neuron with specific attributes', () => {
8-
expect(this.neuron.xCoord).toEqual(1);
9-
expect(this.neuron.yCoord).toEqual(1);
8+
expect(this.neuron.x).toEqual(1);
9+
expect(this.neuron.y).toEqual(1);
1010
expect(this.neuron.size).toEqual(5);
1111
expect(this.neuron.activationLevel).toEqual(5);
1212
});

negativeNeuron.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class NegativeNeuron extends Neuron {
77
display() {
88
this.selected ? fill(200, 200, 60) : fill(0, 100, 200);
99
stroke(sketchOptions.gridColor);
10-
rect(this.xCoord, this.yCoord, this.size, this.size);
10+
rect(this.x, this.y, this.size, this.size);
1111
}
1212
}

neuron.js

+34-78
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Neuron {
22
constructor(x, y, neuronSize, actlvl) {
3-
this.xCoord = x;
4-
this.yCoord = y;
3+
this.x = x;
4+
this.y = y;
55
this.size = neuronSize;
66
this.activationLevel = actlvl;
77
this.curCharge = 0;
@@ -15,7 +15,7 @@ class Neuron {
1515
display() {
1616
this.selected ? fill(200, 200, 60) : fill(100, 200, 0);
1717
stroke(sketchOptions.gridColor);
18-
rect(this.xCoord, this.yCoord, this.size, this.size);
18+
rect(this.x, this.y, this.size, this.size);
1919
}
2020

2121
displayConnections() {
@@ -24,14 +24,14 @@ class Neuron {
2424
// skew centers the connection to the middle of the neuron
2525
var skew = this.size/2.0;
2626
_.each(this.outBoundConnections, outBoundNeuron => {
27-
line(this.xCoord+skew, this.yCoord+skew, outBoundNeuron.xCoord+skew, outBoundNeuron.yCoord+skew);
27+
line(this.x+skew, this.y+skew, outBoundNeuron.x+skew, outBoundNeuron.y+skew);
2828
});
2929
}
3030

3131
erase() {
3232
stroke(sketchOptions.gridColor);
3333
fill(sketchOptions.bgColor);
34-
rect(this.xCoord, this.yCoord, this.size, this.size);
34+
rect(this.x, this.y, this.size, this.size);
3535
}
3636

3737
reset() {
@@ -60,7 +60,7 @@ class Neuron {
6060

6161
// Toggles an inbound connection between two neurons
6262
inBoundConnection(otherNeuron) {
63-
var index = `${otherNeuron.xCoord} ${otherNeuron.yCoord}`;
63+
var index = `${otherNeuron.x} ${otherNeuron.y}`;
6464

6565
if (!this.inBoundConnections[index]) {
6666
this.inBoundConnections[index] = otherNeuron;
@@ -72,7 +72,7 @@ class Neuron {
7272

7373
// Toggles an outbound connection between two neurons
7474
outBoundConnection(otherNeuron) {
75-
var index = `${otherNeuron.xCoord} ${otherNeuron.yCoord}`;
75+
var index = `${otherNeuron.x} ${otherNeuron.y}`;
7676

7777
if (!this.outBoundConnections[index]) {
7878
this.outBoundConnections[index] = otherNeuron;
@@ -84,7 +84,7 @@ class Neuron {
8484

8585
// Removes any connections to a neuron that no longer exists
8686
removeConnection(otherNeuron) {
87-
var index = `${otherNeuron.xCoord} ${otherNeuron.yCoord}`;
87+
var index = `${otherNeuron.x} ${otherNeuron.y}`;
8888
if (this.inBoundConnections[index]) {
8989
delete this.inBoundConnections[index];
9090
clearScreen();
@@ -169,78 +169,39 @@ class Neuron {
169169
}
170170

171171
static createNeuron(x, y) {
172-
var activationLevel = parseInt($('#activationLevel').val());
173-
var timeInSeconds = parseFloat($('#timerActivationInput').val());
174-
var maxSteps = parseFloat(parseInt($('#timesInput').val()));
175-
var noteMIDI = parseInt($('#noteInput').val());
176-
177-
var tempNeuron;
178-
switch (neuronType) {
179-
case 'normal':
180-
tempNeuron = new Neuron(x, y, sketchOptions.cellSize, activationLevel);
181-
break;
182-
case 'character':
183-
tempNeuron = new CharacterNeuron(x, y, sketchOptions.cellSize, activationLevel);
184-
break;
185-
case 'negative':
186-
tempNeuron = new NegativeNeuron(x, y, sketchOptions.cellSize, activationLevel);
187-
break;
188-
case 'timer':
189-
tempNeuron = new TimerNeuron(x, y, sketchOptions.cellSize, activationLevel, timeInSeconds, maxSteps);
190-
break;
191-
case 'note':
192-
tempNeuron = new NoteNeuron(x, y, sketchOptions.cellSize, activationLevel, noteMIDI);
193-
break;
194-
}
172+
var neuronType = $('#neuronType').val();
173+
var neuronObj = {x:x, y:y};
174+
175+
_.each(neuronInfo.fields, (field, key) => {
176+
neuronObj[key] = field();
177+
});
195178

196-
neuronList[`${x} ${y}`] = tempNeuron;
179+
neuronList[`${x} ${y}`] = neuronInfo[neuronType].Create(neuronObj);
197180
}
198181

199182
static loadExample(exampleName) {
200183
var exampleNeuronNet = JSON.parse(exampleNeuronNets[exampleName]);
201184
neuronList = {};
202185
actionPotentialList = {};
203186

204-
// Adds the neurons to the neuronLists without their connections
205-
_.each(exampleNeuronNet, neuron => {
206-
let neuronType = neuron.type;
207-
let tempNeuron;
208-
209-
switch (neuronType) {
210-
case 'Neuron':
211-
tempNeuron = new Neuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel);
212-
break;
213-
case 'CharacterNeuron':
214-
tempNeuron = new CharacterNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel);
215-
break;
216-
case 'NegativeNeuron':
217-
tempNeuron = new NegativeNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel);
218-
break;
219-
case 'TimerNeuron':
220-
neuron.maxSteps = neuron.maxSteps ? neuron.maxSteps : -1;
221-
tempNeuron = new TimerNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel, neuron.timeInSeconds, neuron.maxSteps);
222-
break;
223-
case 'NoteNeuron':
224-
tempNeuron = new NoteNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel, neuron.noteMIDI);
225-
break;
226-
227-
}
228-
187+
// Adds the neurons to the neuronLists without their connections
188+
_.each(exampleNeuronNet, neuron => {
189+
let tempNeuron = neuronInfo[neuron.type].Create(neuron);
229190
neuronList[`${neuron.x} ${neuron.y}`] = tempNeuron;
230-
});
191+
});
231192

232-
// Adds connections
233-
_.each(exampleNeuronNet, neuron => {
234-
let tempNeuron = neuronList[`${neuron.x} ${neuron.y}`];
193+
// Adds connections
194+
_.each(exampleNeuronNet, neuron => {
195+
let tempNeuron = neuronList[`${neuron.x} ${neuron.y}`];
235196

236-
_.each(neuron.outBounds, outBoundNeuron =>{
237-
tempNeuron.outBoundConnection(neuronList[outBoundNeuron]);
238-
});
197+
_.each(neuron.outBounds, outBoundNeuron =>{
198+
tempNeuron.outBoundConnection(neuronList[outBoundNeuron]);
199+
});
239200

240-
_.each(neuron.inBounds, inBoundNeuron =>{
241-
tempNeuron.inBoundConnection(neuronList[inBoundNeuron]);
201+
_.each(neuron.inBounds, inBoundNeuron =>{
202+
tempNeuron.inBoundConnection(neuronList[inBoundNeuron]);
203+
});
242204
});
243-
});
244205
}
245206

246207
static saveExample() {
@@ -251,22 +212,17 @@ class Neuron {
251212
let neuronObj = {};
252213

253214
neuronObj.type = neuronType;
254-
neuronObj.x = neuron.xCoord;
255-
neuronObj.y = neuron.yCoord;
215+
neuronObj.x = neuron.x;
216+
neuronObj.y = neuron.y;
256217
neuronObj.size = neuron.size;
257218
neuronObj.activationLevel = neuron.activationLevel;
258219
neuronObj.outBounds = _.keys(neuron.outBoundConnections);
259220
neuronObj.inBounds = _.keys(neuron.inBoundConnections);
260221

261-
switch (neuronType) {
262-
case 'TimerNeuron':
263-
neuronObj.timeInSeconds = neuron.timeInSeconds;
264-
neuronObj.maxSteps = neuron.maxSteps;
265-
break;
266-
case 'NoteNeuron':
267-
neuronObj.noteMIDI = neuron.note;
268-
break;
269-
}
222+
// TODO FIX THIS
223+
_.each(neuronInfo[neuronType], extraInfo => {
224+
neuronObj[extraInfo] = neuron[extraInfo];
225+
});
270226

271227
neurons.push(neuronObj);
272228
});

neuronInfo.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1-
// Will hold all neurony info
2-
var neuronTypes = {
3-
1+
var neuronInfo = {
2+
fields: {
3+
activationLevel: () => parseInt($('#activationLevel').val()),
4+
timeInSeconds: () => parseFloat($('#timerActivationInput').val()),
5+
maxSteps: () => parseFloat(parseInt($('#timesInput').val())),
6+
note: () => parseInt($('#noteInput').val()),
7+
size: () => sketchOptions.cellSize
8+
},
9+
Neuron: {
10+
Create: (neuron) => new Neuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel),
11+
selectName: 'Normal'
12+
},
13+
CharacterNeuron: {
14+
Create: (neuron) => new CharacterNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel),
15+
selectName: 'Character'
16+
},
17+
NegativeNeuron: {
18+
Create: (neuron) => new NegativeNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel),
19+
selectName: 'Negative'
20+
},
21+
RandomNeuron: {
22+
Create: (neuron) => new RandomNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel),
23+
selectName: 'Random'
24+
},
25+
TimerNeuron: {
26+
Create: (neuron) => new TimerNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel, neuron.timeInSeconds, neuron.maxSteps),
27+
selectName: 'Timer',
28+
extraFields: ['timeInSeconds', 'maxSteps']
29+
},
30+
NoteNeuron: {
31+
Create: (neuron) => new NoteNeuron(neuron.x, neuron.y, neuron.size, neuron.activationLevel, neuron.note),
32+
selectName: 'Note',
33+
extraFields: ['note']
34+
}
435
};
536

noteNeuron.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class NoteNeuron extends Neuron {
1616
display() {
1717
this.selected ? fill(200, 200, 60) : fill(0, 255, 255);
1818
stroke(sketchOptions.gridColor);
19-
rect(this.xCoord, this.yCoord, this.size, this.size);
19+
rect(this.x, this.y, this.size, this.size);
2020
fill(0);
2121
textAlign(CENTER);
2222
textSize(this.size);
2323
let letter = '♪';
24-
text(letter, this.xCoord + this.size/2, this.yCoord + this.size/1.15);
24+
text(letter, this.x + this.size/2, this.y + this.size/1.15);
2525
}
2626

2727
activate() {

randomNeuron.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class RandomNeuron extends Neuron {
2+
constructor(x, y, neuronSize, actlvl) {
3+
super(x, y, neuronSize, actlvl);
4+
}
5+
6+
display() {
7+
this.selected ? fill(200, 200, 60) : fill(255, 205, 205);
8+
stroke(sketchOptions.gridColor);
9+
rect(this.x, this.y, this.size, this.size);
10+
fill(0);
11+
textAlign(CENTER);
12+
textSize(this.size);
13+
14+
let letter = 'R';
15+
text(letter, this.x + this.size/2, this.y + this.size/1.15);
16+
}
17+
18+
activate() {
19+
var randNeuron = _.sample(this.outBoundConnections);
20+
let id = _.uniqueId();
21+
actionPotentialList[`${id}`] = new ActionPotential(this, randNeuron, this.size/2, id, this.actionPotentialCharge, this.size);
22+
}
23+
}

timerNeuron.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class TimerNeuron extends Neuron {
1414
display() {
1515
this.selected ? fill(200, 200, 60) : fill(160, 130, 250);
1616
stroke(sketchOptions.gridColor);
17-
rect(this.xCoord, this.yCoord, this.size, this.size);
17+
rect(this.x, this.y, this.size, this.size);
1818
fill(0);
1919
textAlign(CENTER);
2020
textSize(this.size);
21-
text('T', this.xCoord + this.size/2, this.yCoord + this.size/1.15);
21+
text('T', this.x + this.size/2, this.y + this.size/1.15);
2222
}
2323

2424
neuronSpecificRoutine() {

0 commit comments

Comments
 (0)