wasm-bhtsne 0.1.4 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/package.json +1 -1
- package/wasm_bhtsne.d.ts +11 -10
- package/wasm_bhtsne_bg.js +184 -85
- package/wasm_bhtsne_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -21,16 +21,16 @@ npm i wasm-bhtsne
|
|
|
21
21
|
```javascript
|
|
22
22
|
import {tSNE} from "wasm-bhtsne";
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
data.push(randomNumber);
|
|
24
|
+
function createRandomMatrix(rows, columns) {
|
|
25
|
+
return Array.from({ length: rows }, () =>
|
|
26
|
+
Array.from({ length: columns }, () => Math.random())
|
|
27
|
+
);
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
const
|
|
30
|
+
const data = createRandomMatrix(500, 4);
|
|
31
|
+
|
|
32
|
+
const tsne_encoder = new tSNE(data);
|
|
33
|
+
const compressed_vectors = tsne_encoder.barnes_hut(0.5);
|
|
34
34
|
|
|
35
|
-
console.log(
|
|
35
|
+
console.log(compressed_vectors);
|
|
36
36
|
```
|
package/package.json
CHANGED
package/wasm_bhtsne.d.ts
CHANGED
|
@@ -7,15 +7,9 @@
|
|
|
7
7
|
export class tSNE {
|
|
8
8
|
free(): void;
|
|
9
9
|
/**
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {number} d
|
|
10
|
+
* @param {Array<any>} vectors
|
|
12
11
|
*/
|
|
13
|
-
constructor(
|
|
14
|
-
/**
|
|
15
|
-
* Returns the computed embedding.
|
|
16
|
-
* @returns {Float32Array}
|
|
17
|
-
*/
|
|
18
|
-
embedding(): Float32Array;
|
|
12
|
+
constructor(vectors: Array<any>);
|
|
19
13
|
/**
|
|
20
14
|
* Performs a parallel exact version of the t-SNE algorithm. Pairwise distances between samples
|
|
21
15
|
* in the input space will be computed accordingly to the supplied function `distance_f`.
|
|
@@ -27,8 +21,9 @@ export class tSNE {
|
|
|
27
21
|
* **Do note** that such a distance function needs not to be a metric distance, i.e. it is not
|
|
28
22
|
* necessary for it so satisfy the triangle inequality. Consequently, the squared euclidean
|
|
29
23
|
* distance, and many other, can be used.
|
|
24
|
+
* @returns {Array<any>}
|
|
30
25
|
*/
|
|
31
|
-
exact():
|
|
26
|
+
exact(): Array<any>;
|
|
32
27
|
/**
|
|
33
28
|
* Performs a parallel Barnes-Hut approximation of the t-SNE algorithm.
|
|
34
29
|
*
|
|
@@ -46,8 +41,14 @@ export class tSNE {
|
|
|
46
41
|
* **Do note that** `metric_f` **must be a metric distance**, i.e. it must
|
|
47
42
|
* satisfy the [triangle inequality](https://en.wikipedia.org/wiki/Triangle_inequality).
|
|
48
43
|
* @param {number} theta
|
|
44
|
+
* @returns {Array<any>}
|
|
45
|
+
*/
|
|
46
|
+
barnes_hut(theta: number): Array<any>;
|
|
47
|
+
/**
|
|
48
|
+
* Returns the computed embedding.
|
|
49
|
+
* @returns {Array<any>}
|
|
49
50
|
*/
|
|
50
|
-
|
|
51
|
+
embedding(): Array<any>;
|
|
51
52
|
/**
|
|
52
53
|
* Sets a new value for the embedding dimension.
|
|
53
54
|
*
|
package/wasm_bhtsne_bg.js
CHANGED
|
@@ -24,6 +24,28 @@ function takeObject(idx) {
|
|
|
24
24
|
return ret;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
function isLikeNone(x) {
|
|
28
|
+
return x === undefined || x === null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let cachedFloat64Memory0 = null;
|
|
32
|
+
|
|
33
|
+
function getFloat64Memory0() {
|
|
34
|
+
if (cachedFloat64Memory0 === null || cachedFloat64Memory0.byteLength === 0) {
|
|
35
|
+
cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer);
|
|
36
|
+
}
|
|
37
|
+
return cachedFloat64Memory0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let cachedInt32Memory0 = null;
|
|
41
|
+
|
|
42
|
+
function getInt32Memory0() {
|
|
43
|
+
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
|
|
44
|
+
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
|
|
45
|
+
}
|
|
46
|
+
return cachedInt32Memory0;
|
|
47
|
+
}
|
|
48
|
+
|
|
27
49
|
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
|
|
28
50
|
|
|
29
51
|
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
@@ -53,37 +75,72 @@ function addHeapObject(obj) {
|
|
|
53
75
|
return idx;
|
|
54
76
|
}
|
|
55
77
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
|
|
78
|
+
function debugString(val) {
|
|
79
|
+
// primitive types
|
|
80
|
+
const type = typeof val;
|
|
81
|
+
if (type == 'number' || type == 'boolean' || val == null) {
|
|
82
|
+
return `${val}`;
|
|
61
83
|
}
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
let WASM_VECTOR_LEN = 0;
|
|
66
|
-
|
|
67
|
-
function passArrayF32ToWasm0(arg, malloc) {
|
|
68
|
-
const ptr = malloc(arg.length * 4, 4) >>> 0;
|
|
69
|
-
getFloat32Memory0().set(arg, ptr / 4);
|
|
70
|
-
WASM_VECTOR_LEN = arg.length;
|
|
71
|
-
return ptr;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let cachedInt32Memory0 = null;
|
|
75
|
-
|
|
76
|
-
function getInt32Memory0() {
|
|
77
|
-
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
|
|
78
|
-
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
|
|
84
|
+
if (type == 'string') {
|
|
85
|
+
return `"${val}"`;
|
|
79
86
|
}
|
|
80
|
-
|
|
87
|
+
if (type == 'symbol') {
|
|
88
|
+
const description = val.description;
|
|
89
|
+
if (description == null) {
|
|
90
|
+
return 'Symbol';
|
|
91
|
+
} else {
|
|
92
|
+
return `Symbol(${description})`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (type == 'function') {
|
|
96
|
+
const name = val.name;
|
|
97
|
+
if (typeof name == 'string' && name.length > 0) {
|
|
98
|
+
return `Function(${name})`;
|
|
99
|
+
} else {
|
|
100
|
+
return 'Function';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// objects
|
|
104
|
+
if (Array.isArray(val)) {
|
|
105
|
+
const length = val.length;
|
|
106
|
+
let debug = '[';
|
|
107
|
+
if (length > 0) {
|
|
108
|
+
debug += debugString(val[0]);
|
|
109
|
+
}
|
|
110
|
+
for(let i = 1; i < length; i++) {
|
|
111
|
+
debug += ', ' + debugString(val[i]);
|
|
112
|
+
}
|
|
113
|
+
debug += ']';
|
|
114
|
+
return debug;
|
|
115
|
+
}
|
|
116
|
+
// Test for built-in
|
|
117
|
+
const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
|
|
118
|
+
let className;
|
|
119
|
+
if (builtInMatches.length > 1) {
|
|
120
|
+
className = builtInMatches[1];
|
|
121
|
+
} else {
|
|
122
|
+
// Failed to match the standard '[object ClassName]'
|
|
123
|
+
return toString.call(val);
|
|
124
|
+
}
|
|
125
|
+
if (className == 'Object') {
|
|
126
|
+
// we're a user defined class or Object
|
|
127
|
+
// JSON.stringify avoids problems with cycles, and is generally much
|
|
128
|
+
// easier than looping through ownProperties of `val`.
|
|
129
|
+
try {
|
|
130
|
+
return 'Object(' + JSON.stringify(val) + ')';
|
|
131
|
+
} catch (_) {
|
|
132
|
+
return 'Object';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// errors
|
|
136
|
+
if (val instanceof Error) {
|
|
137
|
+
return `${val.name}: ${val.message}\n${val.stack}`;
|
|
138
|
+
}
|
|
139
|
+
// TODO we could test for more things here, like `Set`s and `Map`s.
|
|
140
|
+
return className;
|
|
81
141
|
}
|
|
82
142
|
|
|
83
|
-
|
|
84
|
-
ptr = ptr >>> 0;
|
|
85
|
-
return getFloat32Memory0().subarray(ptr / 4, ptr / 4 + len);
|
|
86
|
-
}
|
|
143
|
+
let WASM_VECTOR_LEN = 0;
|
|
87
144
|
|
|
88
145
|
const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;
|
|
89
146
|
|
|
@@ -173,16 +230,53 @@ export class tSNE {
|
|
|
173
230
|
wasm.__wbg_tsne_free(ptr);
|
|
174
231
|
}
|
|
175
232
|
/**
|
|
176
|
-
* @param {
|
|
177
|
-
* @param {number} d
|
|
233
|
+
* @param {Array<any>} vectors
|
|
178
234
|
*/
|
|
179
|
-
constructor(
|
|
180
|
-
const
|
|
181
|
-
const len0 = WASM_VECTOR_LEN;
|
|
182
|
-
const ret = wasm.tsne_new(ptr0, len0, d);
|
|
235
|
+
constructor(vectors) {
|
|
236
|
+
const ret = wasm.tsne_new(addHeapObject(vectors));
|
|
183
237
|
return tSNE.__wrap(ret);
|
|
184
238
|
}
|
|
185
239
|
/**
|
|
240
|
+
* Performs a parallel exact version of the t-SNE algorithm. Pairwise distances between samples
|
|
241
|
+
* in the input space will be computed accordingly to the supplied function `distance_f`.
|
|
242
|
+
*
|
|
243
|
+
* # Arguments
|
|
244
|
+
*
|
|
245
|
+
* `distance_f` - distance function.
|
|
246
|
+
*
|
|
247
|
+
* **Do note** that such a distance function needs not to be a metric distance, i.e. it is not
|
|
248
|
+
* necessary for it so satisfy the triangle inequality. Consequently, the squared euclidean
|
|
249
|
+
* distance, and many other, can be used.
|
|
250
|
+
* @returns {Array<any>}
|
|
251
|
+
*/
|
|
252
|
+
exact() {
|
|
253
|
+
const ret = wasm.tsne_exact(this.__wbg_ptr);
|
|
254
|
+
return takeObject(ret);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Performs a parallel Barnes-Hut approximation of the t-SNE algorithm.
|
|
258
|
+
*
|
|
259
|
+
* # Arguments
|
|
260
|
+
*
|
|
261
|
+
* * `theta` - determines the accuracy of the approximation. Must be **strictly greater than
|
|
262
|
+
* 0.0**. Large values for θ increase the speed of the algorithm but decrease its accuracy.
|
|
263
|
+
* For small values of θ it is less probable that a cell in the space partitioning tree will
|
|
264
|
+
* be treated as a single point. For θ equal to 0.0 the method degenerates in the exact
|
|
265
|
+
* version.
|
|
266
|
+
*
|
|
267
|
+
* * `metric_f` - metric function.
|
|
268
|
+
*
|
|
269
|
+
*
|
|
270
|
+
* **Do note that** `metric_f` **must be a metric distance**, i.e. it must
|
|
271
|
+
* satisfy the [triangle inequality](https://en.wikipedia.org/wiki/Triangle_inequality).
|
|
272
|
+
* @param {number} theta
|
|
273
|
+
* @returns {Array<any>}
|
|
274
|
+
*/
|
|
275
|
+
barnes_hut(theta) {
|
|
276
|
+
const ret = wasm.tsne_barnes_hut(this.__wbg_ptr, theta);
|
|
277
|
+
return takeObject(ret);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
186
280
|
* Sets a new learning rate.
|
|
187
281
|
*
|
|
188
282
|
* # Arguments
|
|
@@ -279,59 +373,25 @@ export class tSNE {
|
|
|
279
373
|
}
|
|
280
374
|
/**
|
|
281
375
|
* Returns the computed embedding.
|
|
282
|
-
* @returns {
|
|
376
|
+
* @returns {Array<any>}
|
|
283
377
|
*/
|
|
284
378
|
embedding() {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
wasm.tsne_embedding(retptr, this.__wbg_ptr);
|
|
288
|
-
var r0 = getInt32Memory0()[retptr / 4 + 0];
|
|
289
|
-
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
|
290
|
-
var v1 = getArrayF32FromWasm0(r0, r1).slice();
|
|
291
|
-
wasm.__wbindgen_free(r0, r1 * 4);
|
|
292
|
-
return v1;
|
|
293
|
-
} finally {
|
|
294
|
-
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Performs a parallel exact version of the t-SNE algorithm. Pairwise distances between samples
|
|
299
|
-
* in the input space will be computed accordingly to the supplied function `distance_f`.
|
|
300
|
-
*
|
|
301
|
-
* # Arguments
|
|
302
|
-
*
|
|
303
|
-
* `distance_f` - distance function.
|
|
304
|
-
*
|
|
305
|
-
* **Do note** that such a distance function needs not to be a metric distance, i.e. it is not
|
|
306
|
-
* necessary for it so satisfy the triangle inequality. Consequently, the squared euclidean
|
|
307
|
-
* distance, and many other, can be used.
|
|
308
|
-
*/
|
|
309
|
-
exact() {
|
|
310
|
-
wasm.tsne_exact(this.__wbg_ptr);
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Performs a parallel Barnes-Hut approximation of the t-SNE algorithm.
|
|
314
|
-
*
|
|
315
|
-
* # Arguments
|
|
316
|
-
*
|
|
317
|
-
* * `theta` - determines the accuracy of the approximation. Must be **strictly greater than
|
|
318
|
-
* 0.0**. Large values for θ increase the speed of the algorithm but decrease its accuracy.
|
|
319
|
-
* For small values of θ it is less probable that a cell in the space partitioning tree will
|
|
320
|
-
* be treated as a single point. For θ equal to 0.0 the method degenerates in the exact
|
|
321
|
-
* version.
|
|
322
|
-
*
|
|
323
|
-
* * `metric_f` - metric function.
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* **Do note that** `metric_f` **must be a metric distance**, i.e. it must
|
|
327
|
-
* satisfy the [triangle inequality](https://en.wikipedia.org/wiki/Triangle_inequality).
|
|
328
|
-
* @param {number} theta
|
|
329
|
-
*/
|
|
330
|
-
barnes_hut(theta) {
|
|
331
|
-
wasm.tsne_barnes_hut(this.__wbg_ptr, theta);
|
|
379
|
+
const ret = wasm.tsne_embedding(this.__wbg_ptr);
|
|
380
|
+
return takeObject(ret);
|
|
332
381
|
}
|
|
333
382
|
}
|
|
334
383
|
|
|
384
|
+
export function __wbindgen_object_drop_ref(arg0) {
|
|
385
|
+
takeObject(arg0);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
export function __wbindgen_number_get(arg0, arg1) {
|
|
389
|
+
const obj = getObject(arg1);
|
|
390
|
+
const ret = typeof(obj) === 'number' ? obj : undefined;
|
|
391
|
+
getFloat64Memory0()[arg0 / 8 + 1] = isLikeNone(ret) ? 0 : ret;
|
|
392
|
+
getInt32Memory0()[arg0 / 4 + 0] = !isLikeNone(ret);
|
|
393
|
+
};
|
|
394
|
+
|
|
335
395
|
export function __wbg_new_abda76e883ba8a5f() {
|
|
336
396
|
const ret = new Error();
|
|
337
397
|
return addHeapObject(ret);
|
|
@@ -357,10 +417,6 @@ export function __wbg_error_f851667af71bcfc6(arg0, arg1) {
|
|
|
357
417
|
}
|
|
358
418
|
};
|
|
359
419
|
|
|
360
|
-
export function __wbindgen_object_drop_ref(arg0) {
|
|
361
|
-
takeObject(arg0);
|
|
362
|
-
};
|
|
363
|
-
|
|
364
420
|
export function __wbg_randomFillSync_dc1e9a60c158336d() { return handleError(function (arg0, arg1) {
|
|
365
421
|
getObject(arg0).randomFillSync(takeObject(arg1));
|
|
366
422
|
}, arguments) };
|
|
@@ -415,6 +471,21 @@ export function __wbg_msCrypto_bcb970640f50a1e8(arg0) {
|
|
|
415
471
|
return addHeapObject(ret);
|
|
416
472
|
};
|
|
417
473
|
|
|
474
|
+
export function __wbg_get_44be0491f933a435(arg0, arg1) {
|
|
475
|
+
const ret = getObject(arg0)[arg1 >>> 0];
|
|
476
|
+
return addHeapObject(ret);
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
export function __wbg_length_fff51ee6522a1a18(arg0) {
|
|
480
|
+
const ret = getObject(arg0).length;
|
|
481
|
+
return ret;
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
export function __wbg_new_898a68150f225f2e() {
|
|
485
|
+
const ret = new Array();
|
|
486
|
+
return addHeapObject(ret);
|
|
487
|
+
};
|
|
488
|
+
|
|
418
489
|
export function __wbindgen_is_function(arg0) {
|
|
419
490
|
const ret = typeof(getObject(arg0)) === 'function';
|
|
420
491
|
return ret;
|
|
@@ -455,6 +526,16 @@ export function __wbindgen_is_undefined(arg0) {
|
|
|
455
526
|
return ret;
|
|
456
527
|
};
|
|
457
528
|
|
|
529
|
+
export function __wbg_isArray_4c24b343cb13cfb1(arg0) {
|
|
530
|
+
const ret = Array.isArray(getObject(arg0));
|
|
531
|
+
return ret;
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
export function __wbg_push_ca1c26067ef907ac(arg0, arg1) {
|
|
535
|
+
const ret = getObject(arg0).push(getObject(arg1));
|
|
536
|
+
return ret;
|
|
537
|
+
};
|
|
538
|
+
|
|
458
539
|
export function __wbg_call_01734de55d61e11d() { return handleError(function (arg0, arg1, arg2) {
|
|
459
540
|
const ret = getObject(arg0).call(getObject(arg1), getObject(arg2));
|
|
460
541
|
return addHeapObject(ret);
|
|
@@ -479,6 +560,16 @@ export function __wbg_set_5cf90238115182c3(arg0, arg1, arg2) {
|
|
|
479
560
|
getObject(arg0).set(getObject(arg1), arg2 >>> 0);
|
|
480
561
|
};
|
|
481
562
|
|
|
563
|
+
export function __wbg_newwithbyteoffsetandlength_69193e31c844b792(arg0, arg1, arg2) {
|
|
564
|
+
const ret = new Float32Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
|
|
565
|
+
return addHeapObject(ret);
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
export function __wbg_new_d086a66d1c264b3f(arg0) {
|
|
569
|
+
const ret = new Float32Array(getObject(arg0));
|
|
570
|
+
return addHeapObject(ret);
|
|
571
|
+
};
|
|
572
|
+
|
|
482
573
|
export function __wbg_newwithlength_e5d69174d6984cd7(arg0) {
|
|
483
574
|
const ret = new Uint8Array(arg0 >>> 0);
|
|
484
575
|
return addHeapObject(ret);
|
|
@@ -494,6 +585,14 @@ export function __wbindgen_object_clone_ref(arg0) {
|
|
|
494
585
|
return addHeapObject(ret);
|
|
495
586
|
};
|
|
496
587
|
|
|
588
|
+
export function __wbindgen_debug_string(arg0, arg1) {
|
|
589
|
+
const ret = debugString(getObject(arg1));
|
|
590
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
591
|
+
const len1 = WASM_VECTOR_LEN;
|
|
592
|
+
getInt32Memory0()[arg0 / 4 + 1] = len1;
|
|
593
|
+
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
|
|
594
|
+
};
|
|
595
|
+
|
|
497
596
|
export function __wbindgen_throw(arg0, arg1) {
|
|
498
597
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
499
598
|
};
|
package/wasm_bhtsne_bg.wasm
CHANGED
|
Binary file
|