Skip to main content

Lightweight InTerface for Lossy compression

Project description

litl 🗜️

Lightweight InTerface for Lossy compression

A flexible Python framework for implementing, testing, and deploying lossy compression algorithms.

Features

  • 🔧 Simple Compressors Interface: Simple and universal interface for implementing and sharing compressors
  • 📊 Built-in Evaluation: Comprehensive quality metrics with batteries included (MSE, PSNR, SSIM, L1)
  • 💾 Standardized Storage Format: Custom .litl file format for any compressor with user-defined metadata
  • 🎯 Multiple Data Formats: Support for NumPy, PyTorch, and NetCDF
  • 📈 Rich CLI: Beautiful command-line interface, in addition to rich Python API

Installation

pip install litl

Optional Dependencies

For NetCDF support:

pip install litl[netcdf]

CLI

1. Compress Data

litl compress <path_to_compressor.py:CompressorClassName> <data_to_compress.npy> <config.json> <output.litl>

2. Decompress Data

litl decompress <output.litl> <decompressed.npy>

3. Evaluate Compression (compress and decompress, recording quality metrics)

litl evaluate <path_to_compressor.py:CompressorClassName> <data_to_compress.npy> <config.json>

4. Inspect Compressed Files

litl info <output.litl>

Python API

Compress Data

import litl

compressed_blob, meta, metrics = litl.compress(
    compressor_name="path/to/compressor.py:ClassName",
    original_data_path="data/hurricane.npy",
    litl_file_path="path/to/compressed.litl"
)

print(f"Compressed data saved to {compressed_path}")
print(f"Metadata: {meta}")
print(f"Metrics: {metrics}")

litl.DotLitl.save(
    litl_file_path="path/to/compressed.litl",
    blob=compressed_blob,
    compressor_name="path/to/compressor.py:ClassName",
    meta=meta
)

Decompress Data

import litl

decompressed_data = litl.decompress(
    litl_file_path="path/to/compressed.litl",
    decompressed_path="data/decompressed.npy"
)

print(f"Decompressed data saved to {decompressed_path}")

data_numpy = decompressed_data.numpy()

Creating a Custom Compressor

Implement the Compressor interface:

from litl.compressors import Compressor, CompressorAbout
from litl.datawrapper import DataWrapper
from litl.blobs import ArrayBlob, Blob
import pydantic

class MyCompressor(Compressor):
    
    @classmethod
    def about(cls) -> CompressorAbout:
        return CompressorAbout(
            name="MyCompressor",
            description="A simple lossy compressor",
            version="1.0.0",
            author="Your Name"
        )
    
    @classmethod
    def blob_class(cls):
        # Specify the blob class used for compressed data. This type will be passed into your decompressed function
        return ArrayBlob
    
    @classmethod
    def compress(cls, data: DataWrapper, config: dict) -> tuple[Blob, dict]:
        # Your compression logic here

        # Return a blob containing compressed data.
        # Built-ins are:
        # - ArrayBlob for NumPy and PyTorch arrays
        # - ModelBlob for PyTorch models
        # - ByteBlob for byte arrays
        # If none of these work - extend the Blob class and implement a custom blob

        meta = {}  # Metadata required to reconstruct the data, can be any serializable dictionary
        return ArrayBlob(compressed_array), meta
    
    @classmethod
    def decompress(cls, blob: ArrayBlob, meta: dict) -> DataWrapper:
        # Your decompression logic here

        # Return Reconstructed data wrapped in DataWrapper
        return DataWrapper(decompressed)

DataWrapper

The DataWrapper class supports multiple formats for loading and saving data:

  • NumPy: .npy, .np files
  • PyTorch: .pt files
  • NetCDF: .nc files (requires netCDF4)
from litl import DataWrapper

# Load data
data = DataWrapper.from_file("data.npy")

# Save data
data.save_to_file("output.nc")

# Access underlying arrays
numpy_array = data.numpy()
torch_tensor = data.tensor()

Built-in Blob Types

Choose the appropriate blob type for your compressed data:

Evaluation Metrics

The Evaluator provides standard compression metrics:

Quality Metrics:

  • Mean Squared Error (MSE)
  • Peak Signal-to-Noise Ratio (PSNR)
  • Structural Similarity Index (SSIM)
  • L1 Loss

Size Metrics:

  • Original size
  • Compressed size
  • Compression ratio

File Format

The .litl format (DotLitl) stores:

  • Compressed data with Zstandard compression
  • Compressor metadata and version
  • Integrity checksums
  • Configuration parameters

License

MIT License - see LICENSE for details.

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

litl-0.1.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

litl-0.1.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file litl-0.1.0.tar.gz.

File metadata

  • Download URL: litl-0.1.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for litl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f4c5c24b37887bcc6c89602940cb410d0b75641ec81fefd885c2156741939bc4
MD5 4bc11dac96ceba3e3849637a39ee1a9f
BLAKE2b-256 b12a35e9d266106f29dbd11dee6248c0b29af4e899484f9271e6bbe43535d31d

See more details on using hashes here.

File details

Details for the file litl-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: litl-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for litl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89e395164810a4b27a46416269a64b2ace6f87e8ecb9266b1291c9d39e9484cf
MD5 6536df59c3bdd2df9eeeb5f2aa3c8a60
BLAKE2b-256 32dddf35645a51bb48a20d4cc82ac8e095e642f05a6561fccf8ab59dde5b7ec8

See more details on using hashes here.

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