-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprojector.js
130 lines (106 loc) · 2.9 KB
/
projector.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
import {PCA} from 'https://esm.sh/[email protected]';
import {UMAP} from 'https://esm.sh/[email protected]';
let seed = 1234;
function mulberry32(seed) {
return function () {
var t = (seed += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
class Projector {
constructor(pcaParams = {}, umapParams = {}) {
this.projectionMethod = null;
// PCA
const pcaDefaults = {
nComponents: 3
};
this.pcaParams = {...pcaDefaults, ...pcaParams};
this.pca = null;
// UMAP
const umapDefaults = {
nEpochs: 400,
nComponents: 3,
nNeighbors: 15,
minDist: 0.1,
spread: 1.0,
random: mulberry32(seed)
};
this.umapParams = {...umapDefaults, ...umapParams};
this.umap = null;
// Plotly utilities
this.numPoints = 0;
this.markerSize = 5;
this.trace = null;
}
fitPCA(data) {
this.pca = new PCA(data);
return this.pca.predict(data, this.pcaParams).data;
}
projectWithPCA(data) {
return this.pca.predict(data, this.pcaParams).data;
}
getPcaExplainedVariance() {
return this.pca.getExplainedVariance()
.slice(0, this.pcaParams.nComponents)
.reduce((acc, item) => acc + item, 0)
}
fitUMAP(data) {
this.umap = new UMAP(this.umapParams);
return this.umap.fit(data);
}
projectWithUMAP(data, neighbors) {
// return this.umap.transform(data); // computationally inefficient
return [[]]; // Hack to avoid plotting query.
}
static plainMarkerStyle = (numMarkers, markerSize) => {
return {
color: Array(numMarkers).fill("white"),
size: Array(numMarkers).fill(markerSize),
symbol: Array(numMarkers).fill("circle"),
line: {
color: "white",
width: 1
},
opacity: 0.8
}
}
static layout = () => {
return {
plot_bgcolor: 'black',
paper_bgcolor: 'black',
margin: {
l: 0,
r: 0,
b: 0,
t: 0
}
};
}
setTrace(trace) {
this.trace = trace;
}
getTrace() {
return this.trace;
}
getNumPoints() {
return this.numPoints;
}
setNumPoints(numPoints) {
this.numPoints = numPoints;
this.markerSize = Math.max(2, 20 - Math.log2(numPoints));
}
getMarkerSize() {
return this.markerSize;
}
setProjectionMethod(projectionMethod) {
this.projectionMethod = projectionMethod;
}
getProjectionMethod() {
return this.projectionMethod;
}
}
export {
Projector
};