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))  # reordered for compression
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 encode_jsonl_to_ben, encode_ben_to_xben
    
    encode_jsonl_to_ben("plans.jsonl", "plans.ben")   # fast working format
    encode_ben_to_xben("plans.ben", "plans.xben")     # smallest, for storage
    
  • Shrink for sharing — reorder a finished file and recompress its stream to XBEN, keeping every asset:

    from binary_ensemble import relabel_bundle, compress_stream
    
    relabel_bundle("run.bendl", out_file="run-sorted.bendl", sort="mlc")
    compress_stream("run-sorted.bendl", out_file="run-archive.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-1.0.0.tar.gz (668.4 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-1.0.0-cp311-abi3-win_arm64.whl (865.2 kB view details)

Uploaded CPython 3.11+Windows ARM64

binary_ensemble-1.0.0-cp311-abi3-win_amd64.whl (909.3 kB view details)

Uploaded CPython 3.11+Windows x86-64

binary_ensemble-1.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-1.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-1.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-1.0.0.tar.gz.

File metadata

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

File hashes

Hashes for binary_ensemble-1.0.0.tar.gz
Algorithm Hash digest
SHA256 203484f7a2d578fa5cfee1c1f1175dfeaa299c5eab6265619a22754f1924a7d4
MD5 e3ebc38322c9cdea600ad0f3a753a25d
BLAKE2b-256 f7328d6b221b7b53fdbb867f92862a7f51b7677195269a158807c270a864b210

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-1.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-1.0.0-cp311-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for binary_ensemble-1.0.0-cp311-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 1d42f840998cd3f879c0ee5889d433fab4fb8d15c8e4d1c158f8f7068c02d2c5
MD5 98b4bd07ef21fccb1db625770fec73de
BLAKE2b-256 0be4839861ed8469890dd7bc8ac348b6a6d535375a9b19708a76a161cd940d79

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-1.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-1.0.0-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for binary_ensemble-1.0.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a568eb8233189280d127d16abc1a646f696e1889527c9bb1e9760e98d615b338
MD5 68687fdda6b20ac035c18bbb7da44d91
BLAKE2b-256 923b4dbb8fd964adc8a1939a6411797c4d3b21ab2eea65c904e57b22304331b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-1.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-1.0.0-cp311-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for binary_ensemble-1.0.0-cp311-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1aff06a312c3e41aa9b9c47931248c0a875148973fc77dcb66ab1a3361b5bab8
MD5 269f19a156c21b869202c8d374b73451
BLAKE2b-256 3c57d204371f7600fa6932cd5ef10fae42fff577f2c76ef2dda59cb77f72d505

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-1.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-1.0.0-cp311-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for binary_ensemble-1.0.0-cp311-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e9a8e964b3328c45007dcd68c23e46af4ffb6a099175eaa842dd0cd97ab7297e
MD5 0297114ea7cd7d1001a4ebfffdf4a47b
BLAKE2b-256 63127c9c7ca5b672e26d9efd356958e02db942ba84b3463ff9c26fbfa7fae4a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-1.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-1.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-1.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 94f7d0b1a26bf297c7434145b7f0d52343232ff1120de5d29598d7ba150dcb7b
MD5 c99876b81f1cf3e7710d21ae1cce2c3a
BLAKE2b-256 7c9ad008f5a2b440da7cebdb8e4a8ee5f58b38066d5a7c721ca70050af2685b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_ensemble-1.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