Skip to main content

Python bindings for the Binary Ensemble Package.

Project description

binary-ensemble

PyPI Python versions Documentation License: MIT

Compress, store, and stream massive ensembles of districting plans.

Redistricting samplers like GerryChain's ReCom, ForestReCom, and Sequential Monte Carlo emit millions of plans. Stored as JSONL, a single ensemble can run to tens of gigabytes — most of it redundant. BEN (Binary-Ensemble) is a compression format and toolkit built for exactly this data: it turns those JSONL mountains into compact binary files you can store, share, and stream sample-by-sample without unpacking the whole thing.

binary-ensemble is the Python interface to the binary-ensemble Rust library.

A real 50k-plan ensemble on Colorado's ~140k census blocks is 13.5 GB as JSONL. Reordered by GEOID20 it compresses to a ~280 MB BEN stream, and then to a 5.6 MB XBEN file — over a 2,400× reduction, fully lossless.

Install

pip install binary-ensemble

Requires Python 3.11+. Pre-built wheels are available for Linux, macOS, and Windows. The only runtime dependency is NetworkX, and the API is fully type-annotated (py.typed).

Quick example

Write an ensemble into one self-describing .bendl file, then read it back:

from binary_ensemble import BendlEncoder, BendlDecoder

plans = [[1, 1, 2, 2], [1, 2, 2, 2], [1, 1, 1, 2]]

# The stream context finalizes the bundle when it closes.
encoder = BendlEncoder("ensemble.bendl", overwrite=True)
encoder.add_metadata({"sampler": "demo", "seed": 1234})
with encoder.ben_stream() as ensemble:
    for assignment in plans:
        ensemble.write(assignment)

# Iterate the assignments straight back out, one at a time.
for assignment in BendlDecoder("ensemble.bendl"):
    print(assignment)

The graph travels with the data

An assignment is just integers — it only means something in a dual graph's node order. A .bendl file embeds the graph, so a collaborator can open one file and reconstruct plans with no risk of pairing the wrong graph:

import networkx as nx
from binary_ensemble import BendlEncoder, BendlDecoder

dual_graph = nx.convert_node_labels_to_integers(nx.grid_2d_graph(4, 4))

encoder = BendlEncoder("run.bendl", overwrite=True)
ordered = encoder.add_graph(nx.adjacency_data(dual_graph))  # preserves the graph's node order
with encoder.ben_stream() as ensemble:
    for step in range(1000):
        ensemble.write([(node + step) % 4 + 1 for node in range(16)])

decoder = BendlDecoder("run.bendl")
graph = decoder.read_graph()        # back as a live networkx.Graph, in assignment order
print(len(decoder))                 # 1000 — read from the header, no scan

for assignment in decoder.subsample_every(100):
    ...                             # every 100th plan, without decoding the rest

More than the basics

  • Whole-file converters for existing JSONL ensembles:

    from binary_ensemble import decode_xben_to_ben, encode_ben_to_xben, encode_jsonl_to_ben
    
    encode_jsonl_to_ben("plans.jsonl", "plans.ben")   # fast working format
    encode_ben_to_xben("plans.ben", "plans.xben")     # smallest, for storage
    decode_xben_to_ben("plans.xben", "plans.ben", overwrite=True)
    
  • Shrink for sharing — recompress a bundle's stream to XBEN, or decompress it back to BEN, keeping every asset:

    from binary_ensemble import compress_stream, decompress_stream, relabel_bundle
    
    relabel_bundle("run.bendl", out_file="run-sorted.bendl", sort="mlc")
    compress_stream("run-sorted.bendl", out_file="run-archive.bendl")
    decompress_stream("run-archive.bendl", out_file="run-working.bendl")
    
  • Subsampling by stride, range, or explicit indices on both bundles and plain .ben/.xben streams — skipped samples are never materialized.

  • Custom assets: attach scores, notes, run manifests, or arbitrary binary blobs (a zipped shapefile, a GeoPackage) alongside the stream. Every asset is checksummed (CRC32C), large payloads are xz-compressed transparently, and BendlDecoder.verify() validates a whole file in one call.

  • Sampler-agnostic: encoders take plain list[int] assignments, so the same API works for GerryChain, ForestReCom, SMC, or your own code.

Documentation

Full docs are at binary-ensemble.readthedocs.io:

  • Quickstart — your first ensemble in a few lines.
  • Concepts — dual graphs, the BEN/XBEN/BENDL formats, encoding variants, and the compression levers.
  • Quick Help — compress a GerryChain run, analyze with NumPy/pandas, subsample, convert formats, shrink a file for sharing, recover a crashed run.
  • API reference — every public class and function.
  • Tutorial notebooks — executed end to end in CI against the live API, as is every code snippet in the docs.

Command-line tools

The same engine ships as the ben and bendl CLI tools via Cargo:

cargo install binary-ensemble

License

MIT — see LICENSE.

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

binary_ensemble-2.0.0.tar.gz (675.7 kB view details)

Uploaded Source

Built Distributions

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

binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl (880.3 kB view details)

Uploaded CPython 3.11+Windows ARM64

binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl (928.1 kB view details)

Uploaded CPython 3.11+Windows x86-64

binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.28+ x86-64

binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl (1.1 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.28+ ARM64

binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl (2.0 MB view details)

Uploaded CPython 3.11+macOS 10.13+ universal2 (ARM64, x86-64)macOS 10.13+ x86-64macOS 11.0+ ARM64

File details

Details for the file binary_ensemble-2.0.0.tar.gz.

File metadata

  • Download URL: binary_ensemble-2.0.0.tar.gz
  • Upload date:
  • Size: 675.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for binary_ensemble-2.0.0.tar.gz
Algorithm Hash digest
SHA256 07d7bfd5e789d59166660f062bb2535d80fcadb4e9f092a153270eb393b89842
MD5 ecb6116af31e182fbb74a708e949cdd9
BLAKE2b-256 fa64354cb80a202163511fc88184b547b00453a4cc6be11f1894ab1a32fcc6ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-2.0.0.tar.gz:

Publisher: ci_cd.yml on peterrrock2/binary-ensemble

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

File details

Details for the file binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 a257da421b6b9943ca06b3e7b1fdd386b47972c74ca2dd29baada184066f137a
MD5 98a6e6962a6d1e1a9e2b0ac3c61ed319
BLAKE2b-256 891278c9050447afbefd6a27457b94ca8675d699b190a7680f18364e06dd67cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl:

Publisher: ci_cd.yml on peterrrock2/binary-ensemble

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

File details

Details for the file binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a735e3ab496fbba0834b3a6a0458bf9130999e925bf81bfc80f58107f78c1abe
MD5 6ab05b5aaed531d1906fb74dd9e47fa5
BLAKE2b-256 e179e9c9c297685037d4ea737ce7fa49d39e261e52922bff1f4fff7a692fa4a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl:

Publisher: ci_cd.yml on peterrrock2/binary-ensemble

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

File details

Details for the file binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5dd74654bf5895114bd3c2b2fd467aff689fa2c634134f82fb7b46041b6108bd
MD5 f17ce586040ce3716b3090ac52b2d0d8
BLAKE2b-256 14e7d66cbf9c9668e2a9a63d4a844d91d1c4968c5bc6052c6250c73188d3f71b

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl:

Publisher: ci_cd.yml on peterrrock2/binary-ensemble

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

File details

Details for the file binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f02efdf3034a4e2c5fccb28eebc93a3daf2292e346499e7ab93c0ca16c6b5a0b
MD5 54e8be9243121f25377bcd163e8e6459
BLAKE2b-256 942f41444cc28cf6df22c9ff9e2fe13187065bc131b0d3d11e31c1655da37173

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl:

Publisher: ci_cd.yml on peterrrock2/binary-ensemble

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

File details

Details for the file binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 66b0055338e235f34c06372da98564924ae6a05e32c4398537d4e0626c20397f
MD5 620d0ea95caa4fbba28d14c74fcff1a9
BLAKE2b-256 682e3bcec5eca33ae5225c8e8ba9ff7fe20b5fe3fba8cfd5e993818e07c2da03

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl:

Publisher: ci_cd.yml on peterrrock2/binary-ensemble

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