Note
Go to the end to download the full example code.
Finding disconnected links#
In this example, we show how to find disconnected links in an AequilibraE network.
We use the Nauru example to find disconnected links.
See also
Several functions, methods, classes and modules are used in this example:
aequilibrae.routing.results.path_results()
# Imports
from uuid import uuid4
from tempfile import gettempdir
from os.path import join
from datetime import datetime
import pandas as pd
import numpy as np
from aequilibrae.examples import create_example
from aequilibrae.routing import PathResults
# We create an empty project on an arbitrary folder
fldr = join(gettempdir(), uuid4().hex)
# Let's use the Nauru example project for display
project = create_example(fldr, "nauru")
# Let's analyse the mode car or 'c' in our model
mode = "c"
We can create an uncompressed graph without any centroids or changes to the project.
# We get the project nodes for reporting and load an independent network snapshot.
nodes = project.tables.nodes
network = project.road.build_network()
# No centroid changes need reverting: the project nodes have not been modified.
We set the graph for computation
graph = network.build_graph(mode=mode, cost="distance", skimming=["distance"], block_centroid_flows=False)
Get the nodes that are part of the car network
missing_nodes = nodes.data.query("modes.str.contains(@mode)")["node_id"].values
Now we can compute all the path islands we have
islands = []
idx_islands = 0
while missing_nodes.shape[0] >= 2:
print(datetime.now().strftime("%H:%M:%S"), f" - Computing island: {idx_islands}")
res = PathResults(graph, missing_nodes[0], missing_nodes[1])
connected = res.node_ids[res.terminal_states != res.search_results.sentinel]
connected = np.intersect1d(missing_nodes, connected)
missing_nodes = np.setdiff1d(missing_nodes, connected)
print(f" Nodes to find: {missing_nodes.shape[0]:,}")
df = pd.DataFrame({"node_id": connected, "island": idx_islands})
islands.append(df)
idx_islands += 1
print(f"\nWe found {idx_islands} islands")
06:35:32 - Computing island: 0
Nodes to find: 2
06:35:32 - Computing island: 1
Nodes to find: 0
We found 2 islands
Let’s consolidate everything into a single DataFrame
islands = pd.concat(islands)
# And save to disk alongside our model
islands.to_csv(join(fldr, "island_outputs_complete.csv"), index=False)
If you join the node_id field in the CSV file generated above with the a_node or b_node
fields in the links table, you will have the corresponding links in each disjoint island found.
Total running time of the script: (0 minutes 0.092 seconds)