binary_ensemble.graph¶
The graph module exposes the same reordering algorithms used by
BendlEncoder.add_graph() and relabel_bundle(). Use it when you want to inspect or manage
the graph order yourself before writing assignments.
Each function returns (reordered_graph, node_permutation_map).
Function |
Ordering |
|---|---|
|
Dispatch helper for all orderings |
|
Recursive topology-based clustering |
|
Reverse Cuthill-McKee bandwidth reduction |
|
Sort by a node attribute, or |
Graph reordering utilities used by ben sort-graph and bundle relabeling.
Reordering a dual graph before building a chain (or a bundle) can dramatically improve BEN/XBEN
compression. Each function takes a graph (a live networkx.Graph, or adjacency-format JSON
as a dict/list, raw bytes, a file-like with .read(), or a path) and returns
(reordered_graph, node_permutation_map):
reordered_graphis a live NetworkX graph in its new node ordering (the same shapebinary_ensemble.bundle.BendlEncoder.add_graph()andbinary_ensemble.bundle.BendlDecoder.read_graph()return).node_permutation_mapis the parsednode_permutation_map.jsonpayload, an object with anode_permutation_old_to_newfield mapping original zero-based node positions to their new positions.
To reorder and embed the result in a bundle in one step, pass sort / key to
binary_ensemble.bundle.BendlEncoder.add_graph().
Reordering functions¶
import networkx as nx
from binary_ensemble import graph
dual_graph = nx.convert_node_labels_to_integers(nx.grid_2d_graph(4, 4))
for node in dual_graph.nodes:
dual_graph.nodes[node]["GEOID20"] = f"{node:04d}"
adjacency = nx.adjacency_data(dual_graph)
reordered, permutation_map = graph.reorder(adjacency, sort="key", key="GEOID20")
assert reordered.number_of_nodes() == dual_graph.number_of_nodes()
assert "node_permutation_old_to_new" in permutation_map
The returned reordered graph is a NetworkX graph in the new node order. If you write an
assignment stream against this graph, emit assignment values in list(reordered.nodes)
order.
Tip
If you are creating a bundle, BendlEncoder.add_graph(..., sort=...) is usually simpler:
it reorders the graph, stores graph.json, stores node_permutation_map.json, and returns
the reordered graph in one call.
- reorder(graph, sort='mlc', key=None)[source]¶
Reorder
graphand return(reordered_graph, node_permutation_map).- Parameters:
graph (GraphInput) – The dual graph (
GraphInput): a livenetworkx.Graph(subclasses such asgerrychain.Graphcount), or adjacency-format JSON as a parseddictorlist, rawbytes, a file-like object with.read(), or astr/os.PathLikepath to a JSON file. A plainstris a path here.sort (SortMethod, optional) – The ordering (
SortMethod):"mlc"(multi-level clustering),"rcm"(reverse Cuthill-McKee), or"key"(sort by the node attribute named inkey). Default is"mlc".key (str | None, optional) – Node attribute to sort by, e.g.
key="GEOID";key="id"sorts by the NetworkX node id. Required with (and only valid with)sort="key". Default isNone.
- Returns:
tuple[networkx.Graph, NodePermutationMap] – The reordered graph (a live NetworkX graph, the shape
BendlEncoder.add_graphandBendlDecoder.read_graphreturn) and the parsed permutation map (NodePermutationMap), whosenode_permutation_old_to_newfield maps original zero-based node positions to their new positions.- Raises:
ValueError – If
sort/keyis invalid.- Return type:
tuple[nx.Graph, NodePermutationMap]
- reorder_multi_level_cluster(graph)[source]¶
Reorder
graphusing recursive multi-level clustering.Equivalent to
reorder()withsort="mlc".- Parameters:
graph (GraphInput) – The dual graph (
GraphInput): a livenetworkx.Graph, or adjacency-format JSON as a parseddictorlist, rawbytes, a file-like object with.read(), or astr/os.PathLikepath to a JSON file.- Returns:
tuple[networkx.Graph, NodePermutationMap] – The reordered graph and the parsed permutation map (see
reorder()).- Return type:
tuple[nx.Graph, NodePermutationMap]
- reorder_reverse_cuthill_mckee(graph)[source]¶
Reorder
graphusing Reverse Cuthill-McKee.Equivalent to
reorder()withsort="rcm".- Parameters:
graph (GraphInput) – The dual graph (
GraphInput): a livenetworkx.Graph, or adjacency-format JSON as a parseddictorlist, rawbytes, a file-like object with.read(), or astr/os.PathLikepath to a JSON file.- Returns:
tuple[networkx.Graph, NodePermutationMap] – The reordered graph and the parsed permutation map (see
reorder()).- Return type:
tuple[nx.Graph, NodePermutationMap]
- reorder_by_key(graph, key)[source]¶
Reorder
graphby sorting on a node attribute.Equivalent to
reorder()withsort="key".- Parameters:
graph (GraphInput) – The dual graph (
GraphInput): a livenetworkx.Graph, or adjacency-format JSON as a parseddictorlist, rawbytes, a file-like object with.read(), or astr/os.PathLikepath to a JSON file.key (str) – Node attribute to sort by, e.g.
key="GEOID"; the specialkey="id"sorts by the NetworkX node id.
- Returns:
tuple[networkx.Graph, NodePermutationMap] – The reordered graph and the parsed permutation map (see
reorder()).- Return type:
tuple[nx.Graph, NodePermutationMap]