Git Product home page Git Product logo

binomial_compression's Introduction

Binomial compression

We propose a method for lossless compression based on a two-fold strategy: (i) decomposition of a window of arbitray bytes to a series of reduced bitsets; (ii) compression of bitsets using colexicographic ranking.

The algorithm is presently incapable of outperforming standard compression utilities like gzip and xz. However, it works. We see it, therefore, as only a proof of concept - a starting point for bigger things.

Window decomposition

Consider, in a universe of size 3, a window of five elements, say $[y, y, x, z, z]$. We can project this window onto three bitsets (in ascending order): $[00100]_{x}$, $[11000]_{y}$, and $[00011]_{z}$. Rather than use these bitsets of equal length as input to a bitset compressor, we can fist perform a reduction. A simple scheme involves removing bitset positions from previous bitsets: $[00100]_{\bar{x}}$, $[1100]_{\bar{y}}$, and $[11]_{\bar{z}}$. Such a scheme is reversible.

In practice, we deal with byte windows which have a universe size of 256, but the methodology is similar to the synthetic example above. If each byte present in a byte window can be represented by a sparse (and diminishing) bitset, then these bitsets can be compressed and stored as a concatentation, potentially with fewer bytes than the original byte window.

Bitset ranking

One way to compress a bitset is to consider its length, $N$ say, and its population count, $k$ say. With $N$ and $k$ we can compute a rank for this bitset from the positions of the bitset's populated bits. If this computed rank (and any info required to support it, e.g., $k$) can be stored with fewer bits than the original bitset, then we have compression.

Bitset $b$ is one of many possible bitsets associated with the tuple $(N,k)$, or $b\in{B_{N,k}}={b_0, b_1, ...}$. The number of elements in $B_{N,k}$ is given by the usual binomial coefficient:

$$|B_{N,k}|=\binom{N}{k}=\frac{N!}{k!(N-k)!}.$$

We seek a function to uniquely index each $b$ in $B_{N,k}$. Using colexicographic ordering, there is a ranking function to obtain the index value:

$$r(b)=\sum^{k}_{i=1}\binom{p(b,i)}{i}$$

where $p(b,i)\in{[0,N-1]}$ gives the $i^{th}$ index of $1$ within $b$.

We can recover $b$ from $r(b)$ by successively applying an unranking function:

$$p(b,i)=\max_j\binom{j}{i}, \text{s.t.}\binom{j}{i}\leqslant r(b)-\sum^{k}_{\ell=i+1}p(b,\ell)$$

where $i=k,...,1$.

Note: ranking/unranking heavily depend on binomial coefficients. Rather than compute these quantities on demand (a potentially expensive proposition), they can be efficiently pre-computed using Pascal's Rule. For small window size, gmpy2.bincoef is sufficiently fast.

Compressed file spec

The file format is fairly rudimentary. The beginning of the file contains magic bytes and the number of uncompressed bytes for each window. Then a succession of compressed windows. The end of the file contains the MD5 digest of the original.

Usage

usage: main.py [-h] [-d] [-k] [-s SIZE] [-v] file

Compress/decompress a file

positional arguments:
  file                  the file to process

optional arguments:
  -h, --help            show this help message and exit
  -d, --decompress      run in decompression mode
  -k, --keep            retain files
  -s SIZE, --size SIZE  number of bytes per processing window (default 1024, max 4096)
  -v, --verbose         run verbosely

(Requires Python 3.9.)

Future directions

  • Variable/optimal length byte windows
  • Port from Python to C++
  • Parallelisation
  • Somehow use Gosper's Hack for ranking/unranking?

Other stuff

I am more or less retired from full-time development. But I'm happy to talk to potential employers about niche R&D roles. rasmusaj at mac dot com

References

  1. Ranking and Unranking of Combinations and Permutations, Derrick Stolee (2012).
  2. Binary Image Compression Based on Binomial Numbers, O. Borysenko, I. Kulyk, S. Kostel, O. Skordina (2010).

binomial_compression's People

Contributors

ajr74 avatar

Stargazers

Ilan Schnell avatar  avatar

Watchers

 avatar  avatar  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.