.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/network_manipulation/plot_find_disconnected.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__auto_examples_network_manipulation_plot_find_disconnected.py: .. _find_disconnected_links: 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. .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.routing.results.path_results` .. GENERATED FROM PYTHON SOURCE LINES 35-46 .. code-block:: Python # 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 .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python # 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" .. GENERATED FROM PYTHON SOURCE LINES 59-60 We can create an uncompressed graph without any centroids or changes to the project. .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: Python # 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. .. GENERATED FROM PYTHON SOURCE LINES 67-68 We set the graph for computation .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python graph = network.build_graph(mode=mode, cost="distance", skimming=["distance"], block_centroid_flows=False) .. GENERATED FROM PYTHON SOURCE LINES 71-72 Get the nodes that are part of the car network .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: Python missing_nodes = nodes.data.query("modes.str.contains(@mode)")["node_id"].values .. GENERATED FROM PYTHON SOURCE LINES 75-76 Now we can compute all the path islands we have .. GENERATED FROM PYTHON SOURCE LINES 76-93 .. code-block:: Python 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") .. rst-class:: sphx-glr-script-out .. code-block:: none 08:26:53 - Computing island: 0 Nodes to find: 2 08:26:53 - Computing island: 1 Nodes to find: 0 We found 2 islands .. GENERATED FROM PYTHON SOURCE LINES 94-95 Let's consolidate everything into a single DataFrame .. GENERATED FROM PYTHON SOURCE LINES 95-100 .. code-block:: Python islands = pd.concat(islands) # And save to disk alongside our model islands.to_csv(join(fldr, "island_outputs_complete.csv"), index=False) .. GENERATED FROM PYTHON SOURCE LINES 101-103 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. .. GENERATED FROM PYTHON SOURCE LINES 105-106 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.108 seconds) .. _sphx_glr_download__auto_examples_network_manipulation_plot_find_disconnected.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_find_disconnected.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_find_disconnected.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_find_disconnected.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_