Converters#
Hiveplotlib streamlines two different types of conversion:
Moving to and from NetworkX (building hive plots from NetworkX graphs and exporting hive plots to NetworkX graphs).
Serialization (exporting hive plots to JSON for downstream visualization in other libraries or outside Python).
Much of the low-level conversion machinery lives in hiveplotlib.converters (rendered in full under “Low-Level
Converters Module Reference” below), but the JSON serializer exists only as a class method. The
curated discussion below makes these converters easy to find from this page.
NetworkX Conversion#
Hiveplotlib supports bidirectional conversion between hiveplotlib data structures and NetworkX graphs.
Converting from NetworkX to a Hive Plot#
Hive plots can be built from any flavor of NetworkX graph (directed, undirected, multigraph, or directed multigraph)
most easily via the HivePlot class, which accepts a networkx graph via the graph
parameter as an alternative to the usual nodes and edges parameters.
Users can also use the lower-level conversion function hiveplotlib.converters.networkx_to_nodes_edges() to
build a (NodeCollection, Edges) pair from a networkx graph.
Converting from a Hive Plot to NetworkX#
Hive plots can be exported to NetworkX graphs via the HivePlot inherited class method
to_networkx(), which returns a networkx graph with the hive plot’s node and edge
information. The type of returned graph is selectable by the directed and multigraph parameters.
Users can also use the lower-level conversion function hiveplotlib.converters.nodes_edges_to_networkx() to
build a networkx graph from a (NodeCollection, Edges) pair.
JSON Serialization#
hiveplotlib.HivePlot.to_json() serializes a hive plot’s axes, node placements, edge curves, and resolved
node-viz kwargs (all in Cartesian coordinates) to a JSON string, intended for downstream visualization in other
libraries or outside Python.
Low-Level Converters Module Reference#
Converters from various data structures to hiveplotlib-ready structures.
- hiveplotlib.converters.networkx_to_nodes_edges(graph: Graph, unique_id_name: str = 'unique_id', check_uniqueness: bool = True) tuple[NodeCollection, Edges]#
Take a
networkxgraph and returnhiveplotlib-friendly data structures.Specifically, returns a
NodeCollectioninstance of node data and anEdgesinstance of edge data. These outputs can be fed directly intoHivePlot().- Parameters:
graph –
networkxgraph (any ofGraph,DiGraph,MultiGraph, orMultiDiGraph).unique_id_name – name to use for unique IDs.
check_uniqueness – whether or not to check that provided
Nodeinstances have unique IDs.
- Returns:
a
(NodeCollection, Edges)pair ofhiveplotlibdata structures.
- hiveplotlib.converters.nodes_edges_to_networkx(nodes: NodeCollection, edges: Edges, *, directed: bool = True, multigraph: bool = True, tag_attribute_name: str = 'tag', source_attribute_name: str | None = None) Graph#
Convert a (
NodeCollection,Edges) pair to anetworkxgraph.The concrete return type is determined by
directedandmultigraph:directed=True,multigraph=True→networkx.MultiDiGraph(default).directed=True,multigraph=False→networkx.DiGraph.directed=False,multigraph=True→networkx.MultiGraph.directed=False,multigraph=False→networkx.Graph.
The defaults preserve duplicate edges faithfully and assume directed edges.
All non-(from / to)
EdgesDataFrame columns become edge attributes on the resulting graph. All non-unique_id_columnnode DataFrame columns become node attributes. The visualization-only attributesnode_viz_kwargs/edge_viz_kwargsare not included.Multi-tag
hiveplotlib.Edges: when more than one tag is present, the original tag is written as an edge attribute undertag_attribute_name(default"tag"). If that name collides with an existing column on any tag’s edge DataFrame, aValueErroris raised. Single-tag input does not write a tag attribute.Optional source-row annotation: pass a string for
source_attribute_nameto tag each edge with a(tag, row_index)tuple pointing back to its source row in the inputhiveplotlib.Edges. This is useful for joining graph-level computations (e.g. edge metrics) back onto the original edge DataFrame (including when there are multiple parallel edges in a multigraph). DefaultNoneskips this annotation.Note
When
multigraph=Falsebut duplicate(from, to)pairs exist,networkx’snetworkx.Graph/networkx.DiGraphwill silently merge duplicates (last write wins). For an undirectednetworkx.Graph, reciprocal(a, b)/(b, a)rows likewise merge into a single undirected edge (last write wins), so the merged edge carries only the surviving row’s attributes (e.g. aweight). Build withmultigraph=Trueto retain both edges as parallel edges, or pre-aggregate the weights into a single edge beforehand.- Parameters:
nodes –
hiveplotlib.NodeCollectionof node data.edges –
hiveplotlib.Edgesof edge data (single-tag or multi-tag).directed – whether to return a
networkx.DiGraph-family graph. DefaultTrue.multigraph – whether to return a
Multi*-family graph. DefaultTrue.tag_attribute_name – edge-attribute name to use when storing the original tag on multi-tag inputs. Default
"tag".source_attribute_name – optional edge-attribute name under which to store a
(tag, row_index)source-row identifier for every edge, useful for joining graph-level computations back onto the original edge DataFrame. DefaultNoneskips annotation.
- Returns:
networkxgraph (one ofnetworkx.Graph,networkx.DiGraph,networkx.MultiGraph, ornetworkx.MultiDiGraph).- Raises:
ValueError – if
tag_attribute_namecollides with an existing edge column on a multi-taghiveplotlib.Edgesinstance.ValueError – if
source_attribute_nameis a string and collides with an existing edge column on any tag.