Overview

Task-focused recipes for common jobs. Each one is short and assumes you’ve met the basics in the Quickstart.

Sample data for these guides

The recipes below assume a small ensemble.bendl, a plans.jsonl, a chain.ben / chain.xben pair, and a gerrymandria.json dual graph in your working directory. To follow along, create them all with this snippet:

import json

import networkx as nx

from binary_ensemble import BendlEncoder, BenEncoder, encode_ben_to_xben

# A small dual graph: an 8x8 grid with unit population, contiguous stripe districts,
# and a GEOID20-style key to sort on.
SIDE = 8
graph = nx.convert_node_labels_to_integers(nx.grid_2d_graph(SIDE, SIDE))
for node in graph.nodes:
    _row, col = divmod(node, SIDE)
    graph.nodes[node].update(TOTPOP=1, district=col // 2 + 1, GEOID20=f"{node:04d}")
adjacency = nx.adjacency_data(graph)
n_nodes = SIDE * SIDE

# The GerryChain how-to reads the dual graph from this file.
with open("gerrymandria.json", "w") as handle:
    json.dump(adjacency, handle)

# 120 toy plans on the grid's nodes.
plans = [[(node + step) % 4 + 1 for node in range(n_nodes)] for step in range(120)]

# A self-describing bundle (graph + metadata + the plans)...
encoder = BendlEncoder("ensemble.bendl", overwrite=True)
encoder.add_graph(adjacency, sort=None)
encoder.add_metadata({"sampler": "demo", "seed": 0})
with encoder.ben_stream() as ensemble:
    for plan in plans:
        ensemble.write(plan)

# ...the same plans as JSONL...
with open("plans.jsonl", "w") as handle:
    for sample, plan in enumerate(plans, start=1):
        handle.write(json.dumps({"assignment": plan, "sample": sample}) + "\n")

# ...and as plain BEN / XBEN streams.
with BenEncoder("chain.ben", overwrite=True) as ensemble:
    for plan in plans:
        ensemble.write(plan)
encode_ben_to_xben("chain.ben", "chain.xben", overwrite=True)
End-to-end workflow

Build a working .bendl file, inspect it, attach provenance, and archive it with XBEN.

End-to-end workflow
API cookbook

Copy focused snippets for the most common Python API tasks.

API cookbook
Examples gallery

Small standalone patterns for minimal bundles, rich bundles, conversion, subsampling, and archival.

Examples gallery
Anti-patterns

Avoid node-order mistakes, repeated bundle extensions, wrong working formats, and fragile sharing.

Anti-patterns
Compress a GerryChain run

Stream a ReCom chain straight into a self-describing .bendl file.

Compress a GerryChain run
Read and iterate an ensemble

Open a bundle, recover its graph and metadata, and walk its assignments.

Read and iterate an ensemble
Analyze with NumPy and pandas

Stack an ensemble into an array or DataFrame and score every plan vectorized.

Analyze an ensemble with NumPy and pandas
Subsample a large ensemble

Pull a subset of plans by index, range, or stride — without decoding the whole file.

Subsample a large ensemble
Convert between formats

Whole-file transforms between JSONL, BEN, and XBEN.

Convert between formats
Shrink a bundle for sharing

Reorder, relabel, and recompress a bundle to its smallest shareable form.

Shrink a bundle for sharing
Custom assets and appending

Attach metadata and arbitrary blobs, then add more to a finalized bundle.

Custom assets and appending
Troubleshooting

Diagnose wrong readers, incomplete bundles, missing graphs, and node-order mismatches.

Troubleshooting
Error reference

Map common exceptions and confusing symptoms to causes and fixes.

Error reference