Python bindings for the Binary Ensemble Package.
Project description
binary-ensemble
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
GEOID20it 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/.xbenstreams — 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07d7bfd5e789d59166660f062bb2535d80fcadb4e9f092a153270eb393b89842
|
|
| MD5 |
ecb6116af31e182fbb74a708e949cdd9
|
|
| BLAKE2b-256 |
fa64354cb80a202163511fc88184b547b00453a4cc6be11f1894ab1a32fcc6ad
|
Provenance
The following attestation bundles were made for binary_ensemble-2.0.0.tar.gz:
Publisher:
ci_cd.yml on peterrrock2/binary-ensemble
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binary_ensemble-2.0.0.tar.gz -
Subject digest:
07d7bfd5e789d59166660f062bb2535d80fcadb4e9f092a153270eb393b89842 - Sigstore transparency entry: 2204892844
- Sigstore integration time:
-
Permalink:
peterrrock2/binary-ensemble@70096f2d43f151902df26d3602a4126a7133a3e1 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/peterrrock2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@70096f2d43f151902df26d3602a4126a7133a3e1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl.
File metadata
- Download URL: binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl
- Upload date:
- Size: 880.3 kB
- Tags: CPython 3.11+, Windows ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a257da421b6b9943ca06b3e7b1fdd386b47972c74ca2dd29baada184066f137a
|
|
| MD5 |
98a6e6962a6d1e1a9e2b0ac3c61ed319
|
|
| BLAKE2b-256 |
891278c9050447afbefd6a27457b94ca8675d699b190a7680f18364e06dd67cb
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binary_ensemble-2.0.0-cp311-abi3-win_arm64.whl -
Subject digest:
a257da421b6b9943ca06b3e7b1fdd386b47972c74ca2dd29baada184066f137a - Sigstore transparency entry: 2204892920
- Sigstore integration time:
-
Permalink:
peterrrock2/binary-ensemble@70096f2d43f151902df26d3602a4126a7133a3e1 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/peterrrock2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@70096f2d43f151902df26d3602a4126a7133a3e1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl.
File metadata
- Download URL: binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl
- Upload date:
- Size: 928.1 kB
- Tags: CPython 3.11+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a735e3ab496fbba0834b3a6a0458bf9130999e925bf81bfc80f58107f78c1abe
|
|
| MD5 |
6ab05b5aaed531d1906fb74dd9e47fa5
|
|
| BLAKE2b-256 |
e179e9c9c297685037d4ea737ce7fa49d39e261e52922bff1f4fff7a692fa4a4
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binary_ensemble-2.0.0-cp311-abi3-win_amd64.whl -
Subject digest:
a735e3ab496fbba0834b3a6a0458bf9130999e925bf81bfc80f58107f78c1abe - Sigstore transparency entry: 2204892858
- Sigstore integration time:
-
Permalink:
peterrrock2/binary-ensemble@70096f2d43f151902df26d3602a4126a7133a3e1 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/peterrrock2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@70096f2d43f151902df26d3602a4126a7133a3e1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.11+, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5dd74654bf5895114bd3c2b2fd467aff689fa2c634134f82fb7b46041b6108bd
|
|
| MD5 |
f17ce586040ce3716b3090ac52b2d0d8
|
|
| BLAKE2b-256 |
14e7d66cbf9c9668e2a9a63d4a844d91d1c4968c5bc6052c6250c73188d3f71b
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_x86_64.whl -
Subject digest:
5dd74654bf5895114bd3c2b2fd467aff689fa2c634134f82fb7b46041b6108bd - Sigstore transparency entry: 2204892941
- Sigstore integration time:
-
Permalink:
peterrrock2/binary-ensemble@70096f2d43f151902df26d3602a4126a7133a3e1 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/peterrrock2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@70096f2d43f151902df26d3602a4126a7133a3e1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.11+, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f02efdf3034a4e2c5fccb28eebc93a3daf2292e346499e7ab93c0ca16c6b5a0b
|
|
| MD5 |
54e8be9243121f25377bcd163e8e6459
|
|
| BLAKE2b-256 |
942f41444cc28cf6df22c9ff9e2fe13187065bc131b0d3d11e31c1655da37173
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binary_ensemble-2.0.0-cp311-abi3-manylinux_2_28_aarch64.whl -
Subject digest:
f02efdf3034a4e2c5fccb28eebc93a3daf2292e346499e7ab93c0ca16c6b5a0b - Sigstore transparency entry: 2204892896
- Sigstore integration time:
-
Permalink:
peterrrock2/binary-ensemble@70096f2d43f151902df26d3602a4126a7133a3e1 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/peterrrock2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@70096f2d43f151902df26d3602a4126a7133a3e1 -
Trigger Event:
push
-
Statement type:
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
- Download URL: binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.11+, macOS 10.13+ universal2 (ARM64, x86-64), macOS 10.13+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66b0055338e235f34c06372da98564924ae6a05e32c4398537d4e0626c20397f
|
|
| MD5 |
620d0ea95caa4fbba28d14c74fcff1a9
|
|
| BLAKE2b-256 |
682e3bcec5eca33ae5225c8e8ba9ff7fe20b5fe3fba8cfd5e993818e07c2da03
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binary_ensemble-2.0.0-cp311-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl -
Subject digest:
66b0055338e235f34c06372da98564924ae6a05e32c4398537d4e0626c20397f - Sigstore transparency entry: 2204892878
- Sigstore integration time:
-
Permalink:
peterrrock2/binary-ensemble@70096f2d43f151902df26d3602a4126a7133a3e1 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/peterrrock2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@70096f2d43f151902df26d3602a4126a7133a3e1 -
Trigger Event:
push
-
Statement type: