graph¶
Build NetworkX graphs from flow frames and convert them back.
Build NetworkX graphs from flow frames.
A graph represents exactly one slice of the data: one time value and one
category, unless you ask for an explicit aggregation. Every graph stores the
slice it came from in G.graph, and every edge carries its amount in the
"weight" attribute, which is the default weight name used by NetworkX
algorithms.
Aggregate
module-attribute
¶
How to combine flows across slices: "sum" totals them; "mean"
divides the total by the number of slices present, so a pair that is missing
from a slice counts as zero for that slice.
build_graph
¶
build_graph(
flows: FlowFrame,
*,
time: int | Iterable[int] | None = None,
category: Hashable | Iterable[Hashable] | None = None,
aggregate: Aggregate | None = None,
directed: bool = True,
self_loops: bool = False,
node_attrs: DataFrame | None = None,
) -> DiGraph[Any] | Graph[Any]
Build a weighted graph from one slice of a flow frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flows
|
FlowFrame
|
A flow frame, or any table with |
required |
time
|
int | Iterable[int] | None
|
Keep only flows with this |
None
|
category
|
Hashable | Iterable[Hashable] | None
|
Keep only flows with this |
None
|
aggregate
|
Aggregate | None
|
Required when the (filtered) flows contain more than one |
None
|
directed
|
bool
|
Return a |
True
|
self_loops
|
bool
|
Keep flows whose source equals their target. Dropped by default. |
False
|
node_attrs
|
DataFrame | None
|
Optional table indexed by node label. Its columns become node
attributes for the nodes present in the graph, for example
|
None
|
Returns:
| Type | Description |
|---|---|
DiGraph or Graph
|
Edge amounts are stored under |
Raises:
| Type | Description |
|---|---|
MixedSliceError
|
If the flows span several |
MixedUnitError
|
If the flows have more than one |
ValueError
|
If a |
SchemaError
|
If a required column is missing or weights are negative or missing. |
Examples:
graphs_by
¶
graphs_by(
flows: FlowFrame,
by: str | Sequence[str] = ("time", "category"),
**kwargs: Any,
) -> dict[Hashable, DiGraph[Any] | Graph[Any]]
Build one graph per group of a flow frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flows
|
FlowFrame
|
A flow frame. |
required |
by
|
str | Sequence[str]
|
Column or columns to group on. The default builds one graph per (time, category) slice. |
('time', 'category')
|
**kwargs
|
Any
|
Passed to |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Keys are group values in sorted order: scalars when grouping on one
column, tuples otherwise. Every graph carries |
Examples:
graph_to_flows
¶
Convert a graph back into a flow table.
Scalar slice metadata stored by build_graph (time,
category, unit) is added as constant columns, so a graph built
from one slice converts back into a valid flow frame. G.graph["labels"],
when present, is copied into df.attrs["labels"].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
Graph[Any]
|
A graph whose edges carry |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per edge with the columns of |