An innovative compression tool utilizing context mixing for superior performance.
Project details
cmzip is an experimental compression tool built from scratch, leveraging advanced techniques including context mixing and logistic blending. With both Python and Numba engines, it offers exceptional speed and compression ratios, often outperforming standard tools like zlib and bz2, making it an ideal choice for serious compression tasks.
cmzip is a general-purpose data compressor crafted from scratch, featuring a blend of advanced technologies including a binary arithmetic coder, various order-N context models, a match model similar to LZP, a logistic mixer, and dual APM calibration. This innovative compressor consistently outperforms commonly used compression methods such as zlib (ZIP) and bz2, achieving comparable performance to high-end lzma (7z/RAR) compression, occasionally exceeding it in efficiency.
cmzip operates with two engines within a single file, cmzip.py:
pip install numpy numba.By default, cmzip selects the Numba engine if available, reverting to the Python engine when it is not detected. The engine utilized is recorded in the .cmz header, ensuring that decompression is seamless and requires no additional information about the compression process.
The repository includes:
cmzip.py <- Standalone file, ready to use
src/ <- Modular source code for ease of reading and understanding
arith.py <- Binary arithmetic coder
models.py <- Context models, logistic mixer, match model, APM
compressor.py <- Pure Python engine (integrates all components)
numba_compressor.py <- Numba engine (same algorithm, fixed-size arrays)
imagefilter.py <- Paeth filter (spatial predictor, similar trick to PNG)
tests/
test_arith.py <- Round-trip test for the arithmetic coder
Here are some simple commands to get started with cmzip:
pip install numpy numba # optional for faster engine
python3 cmzip.py c input.txt output.cmz # compress (automatic)
python3 cmzip.py d output.cmz restored.txt # decompress
python3 cmzip.py c input.txt output.cmz --engine python # force pure engine
python3 cmzip.py c image.raw output.cmz --image 120x120x3 # image mode (Paeth filter)
The first compression using the Numba engine compiles code (taking a few seconds), but it caches the result on disk, making subsequent runs much quicker.
The cmzip compression process predicts the probability of each bit in the file using several order-N models based on the last N bytes seen. A match model identifies repetitive sequences, and a logistic mixer combines these predictions while learning online how much to trust each source. A final calibration stage (APM/SSE) adjusts the resulting probability. The arithmetic coder encodes the actual bit with this probability—better predictions lead to fewer bits required, achieving adaptiveness without transmitting any tables, allowing the decoder to reconstruct the same learning bit by bit in sync with the encoder.
cmzip has been tested with the following results, confirming its efficiency across various use cases:
| Method | Corpus 1 | Corpus 2 |
|---|---|---|
| zlib (ZIP) | 3.84x | 4.01x |
| bz2 (BWT) | 4.06x | 4.52x |
| lzma (7z/rar high-end) | 4.07x | 4.84x |
| cmzip, Python Engine | 4.21x | 4.78x |
| cmzip, Numba Engine | 3.98x | 4.45x |
| Method | Size | Ratio |
|---|---|---|
| PNG (optimized) | 28.131 B | 1.54x |
| cmzip, Python Engine | 24.511 B | 1.76x |
| cmzip, Numba Engine | 26.244 B | 1.65x |
| Engine | Time | Speedup |
|---|---|---|
| Python | ~14s | 1x |
| Numba | ~0.3s | ~45x |
--image mode, users must specify the dimensions and channels of the raw file, as it does not parse headers from formats like PNG or JPEG, which are already compressed.
Comments
0Start the conversation
Share the first comment.