Giter Site home page Giter Site logo

kdarby-cqg / tdigestc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ajwerner/tdigestc

0.0 0.0 0.0 688 KB

Polygot C implementation of compact approximate quantile estimation large data sets

License: MIT License

Shell 4.96% JavaScript 6.75% Python 29.83% C 38.41% Java 5.01% Go 15.03%

tdigestc's Introduction

tdigestc

This library is a C implementation of the t-digest data structure for approximate quantile estimation.

It represents an experiment in cross-language development. The build tool chosen is bazel..

The hope was to create an experience with the library where it was

  • natural to use from all targeted languaged
  • build for each language comfortably with a single build tool
  • produce easily includable artifacts

It's not clear that all of these goals have been totally acheived.

Library Implementations

C

C is the core implementation. The code comes in 1 .c file with 1 .h file. You need to just put this into your project or take a compiled so from a bazel build. I haven't figured out a better C distribution story but if you have one, let me know.

Usage

The API is straight forward.

#include <assert.h>
#include "tdigest.h"

int main(void) {
    td_histogram_t *h = td_new(100);
    td_add(h, 1);
    td_add(h, 2);
    assert(td_value_at(h, 0) == 1);
    assert(td_value_at(h, .5) == 1.5);
    assert(td_value_at(h, 1) == 2);
    td_histogram_t *h2 = td_new(100);
    td_add(h2, 0);
    td_add(h2, 3);
    td_merge(h, h2);
    assert(td_value_at(h, 0) == 0);
    assert(td_value_at(h, .5) == 1.5);
    assert(td_value_at(h, 1) == 3);
    td_free(h2);
    td_free(h);
}

JS

The javascript integration works pretty well except for one wart. The memory used by the object will need to manually freed to release the underlying memory.

Installation

The package should be installable from (npm)[https://www.npmjs.com/package/tdigestc]

Usage

The API seeks to be very similar to the tdigest npm library that exists.

For the testing I run a test to compare the output from that open source library and this one.

const TDigest = require('./tdigest.js').TDigest;
var td = new TDigest(100);
td.add(1);
td.add(2);
assert(td.valueAt(0) == 1);
assert(td.valueAt(.5) == 1.5);
assert(td.valueAt(1) == 2);
td.destroy(); // this is the wart, an error will occur if td is used after

Python

Python is packaged using ctypes and a bundled shared library. This has obvious cross platform problems. The python rules are set up to build and publish to PyPI but that has not yet been all the way set up.

Usage

from tdigestc import TDigest
f = TDigest(100)
f.add(1)
f.add(2)
assert f.value_at(0) == 1
assert f.value_at(.5) == 1.5
assert f.value_at(1) == 2

Go

For go it's just that the C is symlinked into the directory and it uses cgo.

The wrapper is not a lot of code. The downside here is the overhead of calling in to cgo but it seems like the data structure from very preliminary testing is pretty fast.

Installation

$ go get github.com/ajwerner/tdigestc/go

Example

package main

import (
    "math/rand"
    "github.com/ajwerner/tdigestc/go"
)

func main() {
     td := tdigestc.New(100);
     td.Add(1)
     td.Add(2)
}

Java

Java is hooked up through the JNI. The //java:TDigest target will compile a jar file which contains the TDigest class and self-loading library.

Usage

import com.ajwerner.tdigestc.TDigest;

public class Example {
     public static void main(String[] args) {
          TDigest td = new TDigest(100);
          td.add(1);
          td.add(2);
          assert td.valueAt(0) == 1;
          assert td.valueAt(.5) == 1.5;
          assert td.valueAt(1) == 2;               
     }
}

Building

Requires:

  • Bazel
  • Java
  • Python (I think 2.7 but am not 100% sure)
  • C Compiler

The below command will build and test everything. If it works, you should feel pretty good.

 bazel test --nocache_test_results //java/... //python/...  //c/... //go/... --test_output=streamed && bazel test --config=wasm //js/... --nocache_test_results 

In theory bazel should download node, emscripten, and all that jazz.

TODO

  • Add doxygen for the C header
  • Fix stamping for distribution of NPM and PyPI packages
  • Improve python packaging and distribution
  • Figure out java packaging and distribution
  • Way more testing
  • Consider using binary search in queries
  • Do more work to make the builds reproducible
  • Cross compile fat binaries for Python/Java
  • Finish adding merge and quantileOf to bindings

tdigestc's People

Contributors

ajwerner avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.