-
Notifications
You must be signed in to change notification settings - Fork 28
/
sp-params.js
227 lines (212 loc) · 6.89 KB
/
sp-params.js
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
$(function() {
// Handlebars templates
var spParamTmpl;
// SP scalar params we'll turn into sliders for adjustment
var spScalarParams = {
potentialRadius: {
val: 16,
min: 0,
max: 128,
name: 'potential radius'
},
potentialPct: {
val: 0.85,
min: 0.0,
max: 1.0,
name: 'potential percent'
},
localAreaDensity: {
val: 0,
min: 0,
max: 1.0,
name: 'local area density'
},
numActiveColumnsPerInhArea: {
val: 40.0,
min: 0.0,
max: 100.0,
name: 'number of active columns per inhibition area'
},
stimulusThreshold: {
val: 1,
min: 0,
max: 10,
name: 'stimulus threshold'
},
synPermInactiveDec: {
val: 0.008,
min: 0.0,
max: 1.0,
name: 'synaptic permanence inactive decrement'
},
synPermActiveInc: {
val: 0.05,
min: 0.0,
max: 1.0,
name: 'synaptic permanence active increment'
},
synPermConnected: {
val: 0.10,
min: 0.0,
max: 1.0,
name: 'synaptic permanence connected'
},
minPctOverlapDutyCycle: {
val: 0.001,
min: 0.0,
max: 1.0,
name: 'minimum percent overlap duty cycle'
},
minPctActiveDutyCycle: {
val: 0.001,
min: 0.0,
max: 1.0,
name: 'minimum percent active duty cycle'
},
dutyCyclePeriod: {
val: 1000,
min: 0,
max: 10000,
name: 'duty cycle period'
},
maxBoost: {
val: 1.0,
min: 0.0,
max: 10.0,
name: 'max boost'
}
};
function calculateSliderStep(min, max, val) {
var range = max - min;
if (Number.isInteger(val)) {
if (val >= 1000) {
return 10;
} else {
return 1;
}
} else {
if (range <= 1) {
if (val < 0.01) {
return 0.001;
} else {
return 0.01;
}
} else if (range < 10) {
return 0.1;
}
}
}
function loadCss() {
$('head').append('<link rel="stylesheet" href="/static/css/lib/sp-params.css">');
}
function loadTemplates(callback) {
$.get('/static/tmpl/sp-params.hbs', function(tmpl) {
spParamTmpl = Handlebars.compile(tmpl);
if (callback) callback();
});
}
function SPParams(el, inputDimensions, columnDimensions) {
this.el = el;
this.params = _.merge({}, spScalarParams);
_.each(this.params, function(p) {
p.type = 'scalar';
});
this.params.globalInhibition = {val: true};
this.params.wrapAround = {val: true};
this.params.inputDimensions = {val: inputDimensions};
this.params.columnDimensions = {val: columnDimensions};
// The potentialRadius max is determined by the input space.
this.params.potentialRadius.max = _.reduce(inputDimensions, function(a, b) {
return a * b;
}, 1);
this.params.potentialRadius.val = this.params.potentialRadius.max;
// This sets sparsity of SP to 2%.
this.params.numActiveColumnsPerInhArea.max = _.multiply(columnDimensions);
this.params.numActiveColumnsPerInhArea.val
= Math.floor(this.params.numActiveColumnsPerInhArea.max * 0.02);
}
SPParams.prototype._updateUi = function() {
_.each(this.params, function(val) {
if (val.displayEl) {
val.displayEl.html(val.val);
}
});
};
SPParams.prototype.getParams = function() {
var out = {};
_.each(this.params, function(val, key) {
out[key] = val.val;
});
return out;
};
SPParams.prototype.setParam = function(key, value) {
if (this.params[key] == undefined) {
throw Error("Missing SP Param '" + key + "'");
}
this.params[key].val = value;
};
SPParams.prototype.render = function(renderDone, valuesChanged) {
var me = this;
var data = {sides: [[],[]]};
var count = 0;
loadCss();
loadTemplates(function() {
_.each(spScalarParams, function(val, codeName) {
var viewObj = {
id: codeName,
name: val.name
};
data.sides[count % 2].push(viewObj);
count++;
});
$('#' + me.el).html(spParamTmpl(data));
// Render sliders and capture DOM elements associated with these params
// after rendering.
_.each(me.params, function(val, codeName) {
var step;
if (val.type && val.type == 'scalar') {
step = calculateSliderStep(val.min, val.max, val.val);
val.sliderEl = $('#' + codeName);
val.displayEl = $('#' + codeName + '-display');
val.sliderEl.slider({
value: val.val,
min: val.min,
max: val.max,
step: step,
change: function(event, ui) {
if (val.val == ui.value) {
event.preventDefault();
} else {
val.val = ui.value;
me._updateUi();
valuesChanged();
}
},
slide: function(event, ui) {
val.displayEl.html(ui.value);
}
});
}
});
var $globalInhibitionSwitch = $('#globalInhibition').bootstrapSwitch({state: globalInhibition});
var $wrapAroundSwitch = $('#wrapAround').bootstrapSwitch({state: wrapAround});
$globalInhibitionSwitch.on('switchChange.bootstrapSwitch', function(event, state) {
me.params.globalInhibition.val = state;
me._updateUi();
valuesChanged();
});
$wrapAroundSwitch.on('switchChange.bootstrapSwitch', function(event, state) {
me.params.wrapAround.val = state;
me._updateUi();
valuesChanged();
});
me._updateUi();
if (renderDone) renderDone();
});
};
window.HTM.utils = {
sp: {
Params: SPParams
}
};
});