-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
utils-misc.js
332 lines (273 loc) · 7.19 KB
/
utils-misc.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
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
/*****************************************************************************
*
* This file is part of the Turing-Drawings project. The project is
* distributed at:
* https://github.com/maximecb/Turing-Drawings
*
* Copyright (c) 2012, Maxime Chevalier-Boisvert. All rights reserved.
*
* This software is licensed under the following license (Modified BSD
* License):
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
/**
Assert that a condition holds true
*/
function assert(condition, errorText)
{
if (!condition)
{
error(errorText);
}
}
/**
Abort execution because a critical error occurred
*/
function error(errorText)
{
alert('ERROR: ' + errorText);
throw errorText;
}
/**
Test that a value is integer
*/
function isInt(val)
{
return (
Math.floor(val) === val
);
}
/**
Test that a value is a nonnegative integer
*/
function isNonNegInt(val)
{
return (
isInt(val) &&
val >= 0
);
}
/**
Test that a value is a strictly positive (nonzero) integer
*/
function isPosInt(val)
{
return (
isInt(val) &&
val > 0
);
}
/**
Get the current time in millisseconds
*/
function getTimeMillis()
{
return (new Date()).getTime();
}
/**
Get the current time in seconds
*/
function getTimeSecs()
{
return (new Date()).getTime() / 1000;
}
/**
Generate a random integer within [a, b]
*/
function randomInt(a, b)
{
assert (
isInt(a) && isInt(b) && a <= b,
'invalid params to randomInt'
);
var range = b - a;
var rnd = a + Math.floor(Math.random() * (range + 1));
return rnd;
}
/**
Generate a random boolean
*/
function randomBool()
{
return (randomInt(0, 1) === 1);
}
/**
Generate a random floating-point number within [a, b]
*/
function randomFloat(a, b)
{
if (a === undefined)
a = 0;
if (b === undefined)
b = 1;
assert (
a <= b,
'invalid params to randomFloat'
);
var range = b - a;
var rnd = a + Math.random() * range;
return rnd;
}
/**
Generate a random value from a normal distribution
*/
function randomNorm(mean, variance)
{
// Declare variables for the points and radius
var x1, x2, w;
// Repeat until suitable points are found
do
{
x1 = 2.0 * randomFloat() - 1.0;
x2 = 2.0 * randomFloat() - 1.0;
w = x1 * x1 + x2 * x2;
} while (w >= 1.0 || w == 0);
// compute the multiplier
w = Math.sqrt((-2.0 * Math.log(w)) / w);
// compute the gaussian-distributed value
var gaussian = x1 * w;
// Shift the gaussian value according to the mean and variance
return (gaussian * variance) + mean;
}
/**
Choose a random argument value uniformly randomly
*/
function randomChoice()
{
assert (
arguments.length > 0,
'must supply at least one possible choice'
);
var idx = randomInt(0, arguments.length - 1);
return arguments[idx];
}
/**
Generate a weighed random choice function
*/
function genChoiceFn()
{
assert (
arguments.length > 0 && arguments.length % 2 === 0,
'invalid argument count: ' + arguments.length
);
var numChoices = arguments.length / 2;
var choices = [];
var weights = [];
var weightSum = 0;
for (var i = 0; i < numChoices; ++i)
{
var choice = arguments[2*i];
var weight = arguments[2*i + 1];
choices.push(choice);
weights.push(weight);
weightSum += weight;
}
assert (
weightSum > 0,
'weight sum must be positive'
);
var limits = [];
var limitSum = 0;
for (var i = 0; i < weights.length; ++i)
{
var normWeight = weights[i] / weightSum;
limitSum += normWeight;
limits[i] = limitSum;
}
function choiceFn()
{
var r = Math.random();
for (var i = 0; i < numChoices; ++i)
{
if (r < limits[i])
return choices[i];
}
return choices[numChoices-1];
}
return choiceFn;
}
/**
Left-pad a string to a minimum length
*/
function leftPadStr(str, minLen, padStr)
{
if (padStr === undefined)
padStr = ' ';
var str = String(str);
while (str.length < minLen)
str = padStr + str;
return str;
}
/**
Resample and normalize an array of data points
*/
function resample(data, numSamples, outLow, outHigh, inLow, inHigh)
{
// Compute the number of data points per samples
var ptsPerSample = data.length / numSamples;
// Compute the number of samples
var numSamples = Math.floor(data.length / ptsPerSample);
// Allocate an array for the output samples
var samples = new Array(numSamples);
// Extract the samples
for (var i = 0; i < numSamples; ++i)
{
samples[i] = 0;
var startI = Math.floor(i * ptsPerSample);
var endI = Math.min(Math.ceil((i+1) * ptsPerSample), data.length);
var numPts = endI - startI;
for (var j = startI; j < endI; ++j)
samples[i] += data[j];
samples[i] /= numPts;
}
// If the input range is not specified
if (inLow === undefined && inHigh === undefined)
{
// Min and max sample values
var inLow = Infinity;
var inHigh = -Infinity;
// Compute the min and max sample values
for (var i = 0; i < numSamples; ++i)
{
inLow = Math.min(inLow, samples[i]);
inHigh = Math.max(inHigh, samples[i]);
}
}
// Compute the input range
var iRange = (inHigh > inLow)? (inHigh - inLow):1;
// Compute the output range
var oRange = outHigh - outLow;
// Normalize the samples
samples.forEach(
function (v, i)
{
var normVal = (v - inLow) / iRange;
samples[i] = outLow + (normVal * oRange);
}
);
// Return the normalized samples
return samples;
}