forked from janhuenermann/neurojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vis.js
executable file
·195 lines (161 loc) · 5.68 KB
/
vis.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
// contains various utility functions
var cnnvis = (function(exports){
// can be used to graph loss, or accuract over time
var Graph = function(options) {
var options = options || {};
this.step_horizon = options.step_horizon || 1000;
this.pts = [];
this.maxy = -9999;
this.miny = 9999;
}
Graph.prototype = {
// canv is the canvas we wish to update with this new datapoint
add: function(step, y) {
var time = new Date().getTime(); // in ms
if(y>this.maxy*0.99) this.maxy = y*1.05;
if(y<this.miny*1.01) this.miny = y*0.95;
this.pts.push({step: step, time: time, y: y});
if(step > this.step_horizon) this.step_horizon *= 2;
},
// elt is a canvas we wish to draw into
drawSelf: function(canv) {
var pad = 25;
var H = canv.height;
var W = canv.width;
var ctx = canv.getContext('2d');
ctx.clearRect(0, 0, W, H);
ctx.font="10px Georgia";
var f2t = function(x) {
var dd = 1.0 * Math.pow(10, 2);
return '' + Math.floor(x*dd)/dd;
}
// draw guidelines and values
ctx.strokeStyle = "#999";
ctx.beginPath();
var ng = 10;
for(var i=0;i<=ng;i++) {
var xpos = i/ng*(W-2*pad)+pad;
ctx.moveTo(xpos, pad);
ctx.lineTo(xpos, H-pad);
ctx.fillText(f2t(i/ng*this.step_horizon/1000)+'k',xpos,H-pad+14);
}
for(var i=0;i<=ng;i++) {
var ypos = i/ng*(H-2*pad)+pad;
ctx.moveTo(pad, ypos);
ctx.lineTo(W-pad, ypos);
ctx.fillText(f2t((ng-i)/ng*(this.maxy-this.miny) + this.miny), 0, ypos);
}
ctx.stroke();
var N = this.pts.length;
if(N<2) return;
// draw the actual curve
var t = function(x, y, s) {
var tx = x / s.step_horizon * (W-pad*2) + pad;
var ty = H - ((y-s.miny) / (s.maxy-s.miny) * (H-pad*2) + pad);
return {tx:tx, ty:ty}
}
ctx.strokeStyle = "red";
ctx.beginPath()
for(var i=0;i<N;i++) {
// draw line from i-1 to i
var p = this.pts[i];
var pt = t(p.step, p.y, this);
if(i===0) ctx.moveTo(pt.tx, pt.ty);
else ctx.lineTo(pt.tx, pt.ty);
}
ctx.stroke();
}
}
// same as graph but draws multiple lines. For now I'm lazy and duplicating
// the code, but in future I will merge these two more nicely.
var MultiGraph = function(legend, options) {
var options = options || {};
this.step_horizon = options.step_horizon || 1000;
if(typeof options.maxy !== 'undefined') this.maxy_forced = options.maxy;
if(typeof options.miny !== 'undefined') this.miny_forced = options.miny;
this.pts = [];
this.maxy = -9999;
this.miny = 9999;
this.numlines = 0;
this.numlines = legend.length;
this.legend = legend;
this.styles = ["red", "blue", "green", "black", "magenta", "cyan", "purple", "aqua", "olive", "lime", "navy"];
// 17 basic colors: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, red, silver, teal, white, and yellow
}
MultiGraph.prototype = {
// canv is the canvas we wish to update with this new datapoint
add: function(step, yl) {
var time = new Date().getTime(); // in ms
var n = yl.length;
for(var k=0;k<n;k++) {
var y = yl[k];
if(y>this.maxy*0.99) this.maxy = y*1.05;
if(y<this.miny*1.01) this.miny = y*0.95;
}
if(typeof this.maxy_forced !== 'undefined') this.maxy = this.maxy_forced;
if(typeof this.miny_forced !== 'undefined') this.miny = this.miny_forced;
this.pts.push({step: step, time: time, yl: yl});
if(step > this.step_horizon) this.step_horizon *= 2;
},
// elt is a canvas we wish to draw into
drawSelf: function(canv) {
var pad = 25;
var H = canv.height;
var W = canv.width;
var ctx = canv.getContext('2d');
ctx.clearRect(0, 0, W, H);
ctx.font="10px Georgia";
var f2t = function(x) {
var dd = 1.0 * Math.pow(10, 2);
return '' + Math.floor(x*dd)/dd;
}
// draw guidelines and values
ctx.strokeStyle = "#999";
ctx.beginPath();
var ng = 10;
for(var i=0;i<=ng;i++) {
var xpos = i/ng*(W-2*pad)+pad;
ctx.moveTo(xpos, pad);
ctx.lineTo(xpos, H-pad);
ctx.fillText(f2t(i/ng*this.step_horizon/1000)+'k',xpos,H-pad+14);
}
for(var i=0;i<=ng;i++) {
var ypos = i/ng*(H-2*pad)+pad;
ctx.moveTo(pad, ypos);
ctx.lineTo(W-pad, ypos);
ctx.fillText(f2t((ng-i)/ng*(this.maxy-this.miny) + this.miny), 0, ypos);
}
ctx.stroke();
var N = this.pts.length;
if(N<2) return;
// draw legend
for(var k=0;k<this.numlines;k++) {
ctx.fillStyle = this.styles[k % this.styles.length];
ctx.fillText(this.legend[k], W-pad-100, pad+20+k*16);
}
ctx.fillStyle = "black";
// draw the actual curve
var t = function(x, y, s) {
var tx = x / s.step_horizon * (W-pad*2) + pad;
var ty = H - ((y-s.miny) / (s.maxy-s.miny) * (H-pad*2) + pad);
return {tx:tx, ty:ty}
}
for(var k=0;k<this.numlines;k++) {
ctx.strokeStyle = this.styles[k % this.styles.length];
ctx.beginPath()
for(var i=0;i<N;i++) {
// draw line from i-1 to i
var p = this.pts[i];
var pt = t(p.step, p.yl[k], this);
if(i===0) ctx.moveTo(pt.tx, pt.ty);
else ctx.lineTo(pt.tx, pt.ty);
}
ctx.stroke();
}
}
}
exports = exports || {};
exports.Graph = Graph;
exports.MultiGraph = MultiGraph;
return exports;
})(typeof module != 'undefined' && module.exports); // add exports to module.exports if in node.js