.. 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.paths.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.utils.create_example import create_example from aequilibrae.paths.results 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 analyze the mode car or 'c' in our model mode = "c" .. GENERATED FROM PYTHON SOURCE LINES 59-60 We need to create the graph, but before that, we need to have at least one centroid in our network. .. GENERATED FROM PYTHON SOURCE LINES 60-82 .. code-block:: Python # We get an arbitrary node to set as centroid and allow for the construction of graphs nodes = project.network.nodes centroid_count = nodes.data.query('is_centroid == 1').shape[0] if centroid_count == 0: arbitrary_node = nodes.data["node_id"][0] nd = nodes.get(arbitrary_node) nd.is_centroid = 1 nd.save() network = project.network network.build_graphs(modes=[mode]) graph = network.graphs[mode] graph.set_blocked_centroid_flows(False) if centroid_count == 0: # Let's revert to setting up that node as centroid in case we had to do it nd.is_centroid = 0 nd.save() .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:197: UserWarning: Found centroids not present in the graph! [1] properties = self._build_directed_graph(self.network, self.centroids) /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:218: UserWarning: Found centroids not present in the graph! [1] build_compressed_graph(self, remove_dead_ends) /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:218: ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment. Such chained assignment never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy (due to Copy-on-Write). Try using '.loc[row_indexer, col_indexer] = value' instead, to perform the assignment in a single step. See the documentation for a more detailed explanation: https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html#chained-assignment build_compressed_graph(self, remove_dead_ends) .. GENERATED FROM PYTHON SOURCE LINES 83-84 We set the graph for computation .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: Python graph.set_graph("distance") graph.set_skimming("distance") .. GENERATED FROM PYTHON SOURCE LINES 88-89 Get the nodes that are part of the car network .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python missing_nodes = nodes.data.query("modes.str.contains(@mode)")["node_id"].values .. GENERATED FROM PYTHON SOURCE LINES 92-93 And prepare the path computation structure .. GENERATED FROM PYTHON SOURCE LINES 93-96 .. code-block:: Python res = PathResults() res.prepare(graph) .. GENERATED FROM PYTHON SOURCE LINES 97-98 Now we can compute all the path islands we have .. GENERATED FROM PYTHON SOURCE LINES 98-117 .. 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.reset() res.compute_path(missing_nodes[0], missing_nodes[1]) res.predecessors[graph.nodes_to_indices[missing_nodes[0]]] = 0 connected = graph.all_nodes[np.where(res.predecessors >= 0)] 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 01:26:30 - Computing island: 0 Nodes to find: 2 01:26:30 - Computing island: 1 Nodes to find: 0 We found 2 islands .. GENERATED FROM PYTHON SOURCE LINES 118-119 Let's consolidate everything into a single DataFrame .. GENERATED FROM PYTHON SOURCE LINES 119-124 .. 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 125-127 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 129-130 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.003 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 `_