networkx_function#
- generate_and_connect_segment_from_linestring_list(linestring_list: list[LineString]) list[LineString] [source]#
Generate unitary segments from a list of LineString objects and ensure every segment is connected in a NetworkX graph sense:
Break down the LineString objects: Divide each LineString into its individual segments, each defined by two consecutive points (start and end).
Build a graph: Create a NetworkX graph where each segment is an edge, and the endpoints of the segments serve as nodes.
Define every connected subgraph.
Connect every subgraph finding the smallest segments needed.
- Parameters:
linestring_list (list[LineString]) – The list of LineString objects.
- Returns:
The list of connected LineString segments.
- Return type:
list[LineString]
- generate_bfs_tree_with_edge_data(graph: Graph, source)[source]#
Create a BFS tree from a graph while retaining edge data.
- Parameters:
graph (nx.Graph) – The input graph.
source (node) – The starting node for BFS.
- Returns:
A directed BFS tree with edge data preserved.
- Return type:
nx.DiGraph
- generate_nx_edge(data: Expr, nx_graph: Graph) Expr [source]#
Generate edges in a NetworkX graph from a Polars expression.
- Parameters:
data (pl.Expr) – The Polars expression containing edge data.
nx_graph (nx.Graph) – The NetworkX graph.
- Returns:
The Polars expression with edges added to the graph.
- Return type:
pl.Expr
- generate_shortest_path_length_matrix(nx_grid: Graph, weight_name: str | None = None, forced_weight: int | float | None = None)[source]#
Generate a matrix of shortest path lengths between all pairs of nodes in a graph.
- Parameters:
nx_graph (nx.Graph) – The graph to process.
weight (str, optional) – The edge attribute to use as weight. Default is ‘weight’.
forced_weight (Optional[Union[int, float]], optional) – The weight to use for all edges. Default is None.
- Returns:
A GraphBlas matrix where the rows and columns represent the nodes (from and to), and the values represent the shortest path lengths.
- Return type:
gb.Matrix
- generate_tree_graph_from_edge_data(edge_data: DataFrame, slack_node_id: str | int | float, data_name: list[str] | None = None) DiGraph [source]#
Generate a tree graph from edge data and a specified slack node.
- Parameters:
edge_data (pl.DataFrame) – The Polars DataFrame containing edge data.
slack_node_id (Union[str, int, float]) – The ID of the slack node.
data_name (Optional[list[str]], optional) – The list of edge data names to include. Defaults to None.
- Returns:
The generated tree graph with edge data preserved.
- Return type:
nx.DiGraph
- Raises:
ValueError – If the edge data names are invalid or if the grid is not a connected tree.
Example: >>> import polars as pl >>> import networkx as nx >>> edge_data = pl.DataFrame({ … “u_of_edge”: [“A”, “C”, “C”], … “v_of_edge”: [“B”, “B”, “D”], … “weight”: [1, 2, 3] … }) >>> slack_node_id = “A” >>> tree_graph = generate_tree_graph_from_edge_data(edge_data, slack_node_id) >>> print(tree_graph.edges(data=True)) [(“A”, “B”, {“weight”: 1}), (“B”, “C”, {“weight”: 2}), (“C”, “D”, {“weight”: 3})]
- get_all_edge_data(nx_graph: Graph) DataFrame [source]#
Get every edge data from a NetworkX graph.
- Parameters:
nx_graph (nx.Graph) – The NetworkX graph.
- Returns:
Polar DataFrame containing every edge data.
- Return type:
pl.DataFrame
- get_connected_edges_data(nx_graph: Graph) DataFrame [source]#
Group all edges that are connected together in a NetworkX graph and return the result as a Polars DataFrame.
- Parameters:
nx_graph (nx.Graph) – The NetworkX graph.
- Returns:
A Polars DataFrame containing the connected edges with columns graph_id , u_of_edge, v_of_edge, and every edge attribute.
- Return type:
pl.DataFrame
- get_edge_data_from_node_list(node_list: list, nx_graph: Graph) list[dict] [source]#
Get edge data for a list of nodes from a NetworkX graph.
- Parameters:
node_list (list) – The list of node IDs.
nx_graph (nx.Graph) – The NetworkX graph.
- Returns:
The list of dictionaries containing every edge data.
- Return type:
list[dict]
- get_edge_data_list(nx_graph: Graph, data_name: str) list [source]#
Get a list of edge data from a NetworkX graph.
- Parameters:
nx_graph (nx.Graph) – The NetworkX graph.
data_name (str) – The name of the edge data to retrieve.
- Returns:
The list of edge data.
- Return type:
list
- get_edge_param_from_node_list(node_list: list, nx_graph: Graph, data_name: str) list[dict] [source]#
Get edge parameter from a list of nodes from a NetworkX graph.
- Parameters:
node_list (list) – The list of node IDs.
nx_graph (nx.Graph) – The NetworkX graph.
data_name (str) – The name of the edge parameter to retrieve.
- Returns:
The list of dictionaries containing every edge data.
- Return type:
list[dict]
- get_node_neighbor_edge_data(nx_graph: Graph, node)[source]#
Get the edge data for all neighbors of a specified node in a graph.
- Parameters:
nx_graph (nx.Graph) – The graph to search.
node (node) – The node whose neighbors’ edge data is to be retrieved.
- Returns:
A list of all neighbors edge data of the specified node.
- Return type:
list
- get_shortest_path_dijkstra_col_from_multisource(target: Expr, nx_graph: Graph, source: list, weight='weight') Expr [source]#
Get the shortest path between source and target nodes using Dijkstra’s algorithm. Targets are stored in a polars columns and return it as a column.
- Parameters:
nx_graph (nx.Graph) – The graph to search.
source (list) – Starting node for the path.
target (pl.Expr) – Ending node for the path.
weight (str, optional) – The edge attribute to use as weight. Default is ‘weight’.
- Returns:
The Polars expression containing lists of shortest path nodes.
- Return type:
pl.Expr
- get_shortest_path_dijkstra_from_multisource(nx_graph: Graph, source: list, target, weight: str = 'weight')[source]#
Get the shortest path between source and target nodes using Dijkstra’s algorithm.
- Parameters:
nx_graph (nx.Graph) – The graph to search.
source (list) – Starting node for the path.
target – Ending node for the path.
weight (str, optional) – The edge attribute to use as weight. Default is ‘weight’.
Returns: list: A list of nodes in the shortest path.
- get_shortest_path_edge_data(nx_graph: Graph, source, target, data_name: str, weight: str = 'weight')[source]#
Get the edge data along the shortest path between source and target nodes in a graph.
- Parameters:
nx_graph (nx.Graph) – The graph to search.
source (node) – Starting node for the path.
target (node) – Ending node for the path.
data_name (str) – The name of the edge attribute to retrieve.
weight (str, optional) – The edge attribute to use as weight. Default is ‘weight’.
- Returns:
A list of edge data along the shortest path.
- Return type:
list