welch / tdigest

tdigest: javascript implementation of Dunning's T-Digest for streaming quantile approximation
MIT License
69 stars 11 forks source link

Question: What is the best way to instantiate a new T Digest object from JSON? #1

Closed oekekezie closed 9 years ago

oekekezie commented 9 years ago

First of all, thanks for writing this excellent library!

I'm currently using compress and then saving the result as JSON. I would like to be able to use said JSON to recreate the TDigest object at a later point in time so that I can compute percentiles.

I'm not terribly familiar with OOP in JavaScript, so I'm not sure I fully understand your source code. Any advice for how to best do this would be very much appreciated.

Thanks!

welch commented 9 years ago

Glad to hear this is useful to you. I like your use case and will add something for it next time I'm at the code.

Meanwhile, you should be able to save a digest using the output of toArray() (this returns a list of centroid centers and weights). You can later feed such an array to a fresh TDigest like this: var digest = new TDigest(); digest.push_centroid(my_json_array); digest.compress();

This pattern also works for combining multiple TDigests, with multiple calls to push_centroid.

oekekezie commented 9 years ago

It works! Thanks!