umap-js
umap-js copied to clipboard
Add worker api
Here's a very minimal proof-of-concept for an additional build that exposes an async API on the model of the normal UMAP API but putting all the compute in a Web Worker. So usage is like this:
import { UMAP } from "umap-js/lib/umap-worker-js";
const umap = new UMAP();
// even .fit() becomes async bc of the Web Worker message-passing behind the scenes
const embedding = await umap.fit(data);
On this approach, I think it will be possible to mirror all of the normal UMAP API, though with some functionality exceptions. For example, while it should be possible to get iteration feedback with some additional messages from the Worker for fitAsync()
, we can't actually pass a callback through to a Worker (functions are not structured clone-able), so terminating the compute won't be possible with a return false;
from a callback, though we could make a slightly different API for it based on a threshold epochNumber
.
The obvious alternative here is to just expose a UMAPWorker with its own separate API defined as types of messages you can pass to the Worker and receive back, but I thought it'd be interesting to try the approach here that's easier on the user since it doesn't require users to implement message handlers and posting, though again it comes with the downside of a nearly but not quite identical API...
Would love to hear your thoughts! And no sweat if you're not that interested in adding this to published umap-js. I've been working for a while on a React wrapper around UMAP (and t-SNE, originally) to make it easier to drop a UMAP viz into a React app, and if you're not a fan of this approach, I'll probably do it anyway there either as the implementation details behind the custom hook that'll be the main export or even as an additional export API as well.
Some comments inline because the code deserves a few to clarify the currently-hackiest bits 🙃
Ack, sorry for the slow reply! Glad we're basically on the same page; will keep chipping away at this and let you know when I've got substantially more progress to look at!