.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/plot_find_disconnected.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__auto_examples_plot_find_disconnected.py: Finding disconnected links ========================== On 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 11-19 .. code-block:: python import numpy as np import pandas as pd from PIL import Image import matplotlib.pyplot as plt img = Image.open("disconnected_network.png") plt.imshow(img) .. image-sg:: /_auto_examples/images/sphx_glr_plot_find_disconnected_001.png :alt: plot find disconnected :srcset: /_auto_examples/images/sphx_glr_plot_find_disconnected_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 20-21 # Imports .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: python from uuid import uuid4 from tempfile import gettempdir from os.path import join from datetime import datetime from aequilibrae.utils.create_example import create_example from aequilibrae.paths.results import PathResults .. GENERATED FROM PYTHON SOURCE LINES 29-30 We create an empty project on an arbitrary folder .. GENERATED FROM PYTHON SOURCE LINES 30-38 .. code-block:: python 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 39-40 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 40-62 .. code-block:: python # We get an arbitrary node to set as centroid and allow for the construction of graphs centroid_count = project.conn.execute("select count(*) from nodes where is_centroid=1").fetchone()[0] if centroid_count == 0: arbitrary_node = project.conn.execute("select node_id from nodes limit 1").fetchone()[0] nodes = project.network.nodes 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 of 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:354: FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)` df.loc[:, "id"] = np.arange(df.shape[0]) /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:371: FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)` df.loc[:, "direction"] = df.direction.values.astype(np.int8) /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:354: FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)` df.loc[:, "id"] = np.arange(df.shape[0]) /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:371: FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)` df.loc[:, "direction"] = df.direction.values.astype(np.int8) .. GENERATED FROM PYTHON SOURCE LINES 63-64 We set the graph for computation .. GENERATED FROM PYTHON SOURCE LINES 64-78 .. code-block:: python graph.set_graph("distance") graph.set_skimming("distance") # Get the nodes that are part of the car network missing_nodes = [ x[0] for x in project.conn.execute(f"Select node_id from nodes where instr(modes, '{mode}')").fetchall() ] missing_nodes = np.array(missing_nodes) # And prepare the path computation structure res = PathResults() res.prepare(graph) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:445: FutureWarning: The default value of numeric_only in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function. df = self.__graph_groupby.sum()[[cost_field]].reset_index() /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:479: FutureWarning: The default value of numeric_only in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function. df = self.__graph_groupby.sum()[skim_fields].reset_index() .. GENERATED FROM PYTHON SOURCE LINES 79-80 Now we can compute all the path islands we have .. GENERATED FROM PYTHON SOURCE LINES 80-98 .. 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 07:34:54 - Computing island: 0 Nodes to find: 2 07:34:54 - Computing island: 1 Nodes to find: 0 We found 2 islands .. GENERATED FROM PYTHON SOURCE LINES 99-100 consolidate everything into a single DataFrame .. GENERATED FROM PYTHON SOURCE LINES 100-108 .. 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) # 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 109-110 .. code-block:: python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.560 seconds) .. _sphx_glr_download__auto_examples_plot_find_disconnected.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_find_disconnected.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_find_disconnected.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_