Skip to main content

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.

Metadata

Release files for binary-ensemble 2.0.0

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

Source distribution (sdist)

Source distribution for binary-ensemble 2.0.0
File Size Uploaded Provenance
binary_ensemble-2.0.0.tar.gz 675.7 kB With provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for binary-ensemble 2.0.0
File
binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details
binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details
binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl CPython 3.11 abi3 Linux glibc 2.28+ x86-64 Details
binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl CPython 3.11 abi3 Linux glibc 2.28+ ARM64 Details
binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl CPython 3.11 abi3 macOS 11.0+ ARM64, macOS 10.13+ universal2 (ARM64, x86-64), macOS 10.13+ x86-64 Details

Total release size: 6.9 MB

Release files / binary_ensemble-2.0.0.tar.gz

Download URL binary_ensemble-2.0.0.tar.gz
Size 675.7 kB
Tags Source
SHA-256 checksum
How to use checksums
07d7bfd5e789d59166660f062bb2535d80fcadb4e9f092a153270eb393b89842
BLAKE2b-256 checksum
How to use checksums
fa64354cb80a202163511fc88184b547b00453a4cc6be11f1894ab1a32fcc6ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Release files / binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl

Download URL binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl
Size 880.3 kB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
a257da421b6b9943ca06b3e7b1fdd386b47972c74ca2dd29baada184066f137a
BLAKE2b-256 checksum
How to use checksums
891278c9050447afbefd6a27457b94ca8675d699b190a7680f18364e06dd67cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Release files / binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl

Download URL binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl
Size 928.1 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
a735e3ab496fbba0834b3a6a0458bf9130999e925bf81bfc80f58107f78c1abe
BLAKE2b-256 checksum
How to use checksums
e179e9c9c297685037d4ea737ce7fa49d39e261e52922bff1f4fff7a692fa4a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Release files / binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl

Download URL binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl
Size 1.2 MB
Tags CPython 3.11 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
5dd74654bf5895114bd3c2b2fd467aff689fa2c634134f82fb7b46041b6108bd
BLAKE2b-256 checksum
How to use checksums
14e7d66cbf9c9668e2a9a63d4a844d91d1c4968c5bc6052c6250c73188d3f71b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Release files / binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl

Download URL binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl
Size 1.1 MB
Tags CPython 3.11 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
f02efdf3034a4e2c5fccb28eebc93a3daf2292e346499e7ab93c0ca16c6b5a0b
BLAKE2b-256 checksum
How to use checksums
942f41444cc28cf6df22c9ff9e2fe13187065bc131b0d3d11e31c1655da37173
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Release files / binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl

Download URL binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl
Size 2.0 MB
Tags CPython 3.11 abi3 macOS 10.13+ universal2 (ARM64, x86-64) macOS 10.13+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
66b0055338e235f34c06372da98564924ae6a05e32c4398537d4e0626c20397f
BLAKE2b-256 checksum
How to use checksums
682e3bcec5eca33ae5225c8e8ba9ff7fe20b5fe3fba8cfd5e993818e07c2da03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Security for binary-ensemble 2.0.0

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.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for binary_ensemble-2.0.0.tar.gz

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 Jul 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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