Skip to main content

zctw

Python implementation of Context Tree Weighting (CTW) lossless compression.

PyPI Python Coverage Ruff

Install

pip install zctw

Usage

from zctw import CTWCompressor, CTWSettings

# Use default settings
compressor = CTWCompressor()

# Or customize settings
settings = CTWSettings(treedepth=8, maxnrnodes=4194304)
compressor = CTWCompressor(settings)

# Compress data
original = b"Hello, World! This is a test of the CTW compression algorithm."
compressed = compressor.encode(original)

# Decompress
decompressed = compressor.decode(compressed)

print(f"Original: {len(original)} bytes")
print(f"Compressed: {len(compressed)} bytes")
print(f"Decompressed matches: {original == decompressed}")

CLI

# Encode a file
zctw e input.txt output.zctw

# Decode a file
zctw d output.zctw input_decoded.txt

# Show file info
zctw i output.zctw

CLI Options

  • -dX: Set maximum tree depth (1-12)
  • -tX: Set maximum number of tries in tree array (1-32)
  • -nX: Set maximum number of nodes (supports K/M suffix, e.g., 4M)
  • -fX: Set maximum file buffer size (supports K/M suffix)
  • -bX: Set maximum value of log beta
  • -s: Disable strict tree pruning
  • -r: Enable weighting at root nodes
  • -k: Use Krichevski-Trofimov estimator instead of Zero-Redundancy
  • -y: Force overwriting of existing files
  • -lX: Enable logging to file X

API

CTWSettings

@dataclass
class CTWSettings:
    treedepth: int          # max depth of trees, excluding root (default: 6)
    maxnrnodes: int         # max nodes in tree array (default: 4194304)
    maxnrtries: int        # max tries in hash table (default: 32)
    filebufsize: int       # actual file buffer size (default: 4194304)
    maxfilebufsize: int    # max file buffer size (default: 4194304)
    strictpruning: bool    # use strict pruning method (default: True)
    maxlogbeta: int        # max value of logbeta (default: 1024)
    rootweighting: bool    # perform weighting at root node (default: False)
    use_zeroredundancy: bool # use zero-redundancy estimator (default: True)

CTWCompressor

class CTWCompressor:
    def __init__(self, settings: CTWSettings | None = None)
    def encode(self, data: bytes) -> bytes
    def decode(self, compressed: bytes) -> bytes

Development

git clone https://github.com/daedalus/zctw.git
cd zctw
pip install -e ".[test]"

# run tests
pytest

# format
ruff format src/ tests/

# lint
ruff check src/ tests/

# type check
mypy src/

License

MIT License - see LICENSE file.

Metadata

Release files for zctw 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zctw 0.1.1
File Size Uploaded
zctw-0.1.1.tar.gz 106.6 kB Details

Release files / zctw-0.1.1.tar.gz

Download URL zctw-0.1.1.tar.gz
Size 106.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9a42e5f6a8cd1c1db10eb4710cb9d5829c8f8c6086abb1d19a6d582b058349d4
BLAKE2b-256 checksum
How to use checksums
220ab46aef8282a6082cc71736a21e4eeb92d5db9110ee0597c90f3ffb793c78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 9, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page