Skip to main content

Python implementation of Context Tree Weighting (CTW) lossless compression

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zctw-0.1.1.tar.gz (106.6 kB view details)

Uploaded Source

File details

Details for the file zctw-0.1.1.tar.gz.

File metadata

  • Download URL: zctw-0.1.1.tar.gz
  • Upload date:
  • Size: 106.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zctw-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9a42e5f6a8cd1c1db10eb4710cb9d5829c8f8c6086abb1d19a6d582b058349d4
MD5 70d0668738b2c60ec0991adc0f7cf74e
BLAKE2b-256 220ab46aef8282a6082cc71736a21e4eeb92d5db9110ee0597c90f3ffb793c78

See more details on using hashes here.

Provenance

The following attestation bundles were made for zctw-0.1.1.tar.gz:

Publisher: pypi-publish.yml on daedalus/zctw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page