Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
binary-ensemble
binary-ensemble

Getting started

  • Installation
  • Quickstart

Tutorials

  • Working with .bendl files
  • Plain BEN & XBEN streams

Quick Help

  • Overview
  • End-to-end workflow
  • API cookbook
  • Examples gallery
  • Anti-patterns
  • Compress a GerryChain run
  • Read and iterate an ensemble
  • Analyze an ensemble with NumPy and pandas
  • Subsample a large ensemble
  • Convert between formats
  • Shrink a bundle for sharing
  • Custom assets and appending
  • Troubleshooting
  • Error reference

Concepts

  • Overview
    • Vocabulary
    • The data contract
    • JSONL input schema
    • Formats: BEN vs XBEN vs BENDL
    • Encoding variants
    • Why reordering shrinks files
    • Graph ordering deep dive
    • Performance guide
    • The API map
    • Python and CLI parity
    • Limitations and invariants
    • Compatibility and stability
    • Release and versioning

API reference

  • Overview
    • binary_ensemble.bundle
    • binary_ensemble.stream
    • binary_ensemble.codec
    • binary_ensemble.graph

Project

  • Changelog
  • format stability
  • Rust crate source
  • GitHub
Back to top
View this page
Edit this page

Examples gallery¶

Small standalone patterns you can paste into scripts. For longer explanations, follow the links from each example.

Minimal bundle¶

from binary_ensemble import BendlEncoder, BendlDecoder

encoder = BendlEncoder("gallery-minimal.bendl", overwrite=True)
with encoder.ben_stream() as ensemble:
    ensemble.write([1, 1, 2, 2])
    ensemble.write([1, 2, 2, 2])

assert len(BendlDecoder("gallery-minimal.bendl")) == 2

See Quickstart.

Bundle with graph, metadata, and notes¶

import networkx as nx

from binary_ensemble import BendlDecoder, BendlEncoder

graph = nx.convert_node_labels_to_integers(nx.path_graph(4))

encoder = BendlEncoder("gallery-rich.bendl", overwrite=True)
encoder.add_graph(nx.adjacency_data(graph), sort=None)
encoder.add_metadata({"seed": 2026, "sampler": "demo"})
encoder.add_asset("notes.txt", "Toy gallery bundle.", content_type="text")

with encoder.ben_stream() as ensemble:
    ensemble.write([1, 1, 2, 2])

decoder = BendlDecoder("gallery-rich.bendl")
assert decoder.read_graph().number_of_nodes() == 4
assert decoder.read_metadata()["seed"] == 2026

See Custom assets and appending.

Plain stream conversion¶

from binary_ensemble import decode_xben_to_ben, encode_ben_to_xben

encode_ben_to_xben("chain.ben", "gallery-chain.xben", overwrite=True)
decode_xben_to_ben("gallery-chain.xben", "gallery-chain.ben", overwrite=True)

See Convert between formats.

Subsample for diagnostics¶

from binary_ensemble import BendlDecoder

diagnostic_plans = list(BendlDecoder("ensemble.bendl").subsample_every(40))
assert len(diagnostic_plans) > 0

See Subsample a large ensemble.

Archive a final bundle¶

from binary_ensemble import compress_stream, relabel_bundle

relabel_bundle("ensemble.bendl", out_file="gallery-sorted.bendl", sort="mlc")
compress_stream("gallery-sorted.bendl", out_file="gallery-archive.bendl")

See Shrink a bundle for sharing.

Next
Anti-patterns
Previous
API cookbook
Copyright © 2025, Peter Rock
Made with Sphinx and @pradyunsg's Furo
On this page
  • Examples gallery
    • Minimal bundle
    • Bundle with graph, metadata, and notes
    • Plain stream conversion
    • Subsample for diagnostics
    • Archive a final bundle