Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tSNE vector compression optimised using wasm-bhtsne #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"react-router-dom": "^6.8.1",
"vite": "^4.3.3",
"vite-plugin-svgr": "^2.4.0",
"wasm-bhtsne": "^0.3.3",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/VisualizeChart/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const VisualizeChart = ({ scrollResult }) => {
],
});

const worker = new Worker(new URL('./worker.js', import.meta.url), {
const worker = new Worker(new URL('./worker2.js', import.meta.url), {
type: 'module',
});

Expand Down
109 changes: 109 additions & 0 deletions src/components/VisualizeChart/worker2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* eslint-disable no-restricted-globals */

import get from 'lodash/get';
import init, { tSNE as Tsne } from 'wasm-bhtsne';

const MESSAGE_INTERVAL = 200;

self.onmessage = async function (e) {
await init();

let now = new Date().getTime();
const data1 = e.data;
const data = [];

if (data1?.result?.points?.length === 0) {
self.postMessage({
data: [],
error: 'No data found',
});
return;
} else if (data1?.result?.points?.length === 1) {
self.postMessage({
data: [],
error: 'cannot perform tsne on single point',
});
return;
} else if (typeof data1?.result?.points[0].vector.length === 'number') {
data1?.result?.points?.forEach((point) => {
data.push(point.vector);
});
} else if (typeof data1?.result?.points[0].vector === 'object') {
if (data1.vector_name === undefined) {
self.postMessage({
data: [],
error: 'No vector name found, select a vaild vector_name',
});
return;
} else if (data1?.result?.points[0].vector[data1?.vector_name] === undefined) {
self.postMessage({
data: [],
error: 'No vector found with name ' + data1?.vector_name,
});
return;
} else if (data1?.result?.points[0].vector[data1?.vector_name]) {
data1?.result?.points?.forEach((point) => {
data.push(point.vector[data1?.vector_name]);
});
} else {
self.postMessage({
data: [],
error: 'Unexpected Error Occured',
});
return;
}
} else {
self.postMessage({
data: [],
error: 'Unexpected Error Occured',
});
return;
}
if (data.length) {
const tsneencoder = new Tsne(data);
let j = {};
for (let i = 0; i < 500; i++) {
j = tsneencoder.barnes_hut(1);
if (Date.now() - now > MESSAGE_INTERVAL) {
now = Date.now();
self.postMessage({ result: getDataset(data1, j), error: null });
}
}
self.postMessage({ result: getDataset(data1, j), error: null });
}
};

function getDataset(data, reducedPoint) {
const dataset = [];
const labelby = data.color_by;
if (labelby) {
data.labelByArrayUnique.forEach((label) => {
dataset.push({
label: label,
data: [],
});
});

data.result?.points?.forEach((point, index) => {
const label = get(point.payload, labelby);
dataset[data.labelByArrayUnique.indexOf(label)].data.push({
x: reducedPoint[index][0],
y: reducedPoint[index][1],
point: point,
});
});
} else {
dataset.push({
label: 'data',
data: [],
});
data.result?.points?.forEach((point, index) => {
dataset[0].data.push({
x: reducedPoint[index][0],
y: reducedPoint[index][1],
point: point,
});
});
}
return dataset;
}
5 changes: 5 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export default defineConfig({
build: {
outDir: 'dist',
},
optimizeDeps: {
exclude: [
"wasm-bhtsne",
],
},
plugins: [
reactRefresh(),
svgrPlugin({
Expand Down