.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/path_computation/plot_graph_from_arbitrary_data.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_path_computation_plot_graph_from_arbitrary_data.py: .. _plot_graph_from_arbitrary_data: Graph from arbitrary data ========================= In this example, we demonstrate how to create an AequilibraE Graph from an arbitrary network. We are using `Sioux Falls data `_, from TNTP. .. GENERATED FROM PYTHON SOURCE LINES 15-19 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.paths.Graph` .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: Python # Imports import numpy as np import pandas as pd from aequilibrae.paths import Graph .. GENERATED FROM PYTHON SOURCE LINES 30-31 We start by adding the path to load our arbitrary network. .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: Python net_file = "https://raw.githubusercontent.com/bstabler/TransportationNetworks/master/SiouxFalls/SiouxFalls_net.tntp" .. GENERATED FROM PYTHON SOURCE LINES 34-37 Let's read our data! We'll be using Sioux Falls transportation network data, but without geometric information. The data will be stored in a Pandas DataFrame containing information about initial and final nodes, link distances, travel times, etc. .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python net = pd.read_csv(net_file, skiprows=8, sep="\t", lineterminator="\n", usecols=np.arange(1, 11)) .. GENERATED FROM PYTHON SOURCE LINES 40-44 The Graph object requires several default fields: link_id, a_node, b_node, and direction. We need to manipulate the data to add the missing fields (link_id and direction) and rename the node columns accordingly. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: Python net.insert(0, "link_id", np.arange(1, net.shape[0] + 1)) net = net.assign(direction=1) net.rename(columns={"init_node": "a_node", "term_node": "b_node"}, inplace=True) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Now we can take a look in our network file .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python net.head() .. raw:: html
link_id a_node b_node capacity length free_flow_time b power speed toll link_type direction
0 1 1 2 25900.200640 6 6 0.15 4 0 0 1 1
1 2 1 3 23403.473190 4 4 0.15 4 0 0 1 1
2 3 2 1 25900.200640 6 6 0.15 4 0 0 1 1
3 4 2 6 4958.180928 5 5 0.15 4 0 0 1 1
4 5 3 1 23403.473190 4 4 0.15 4 0 0 1 1


.. GENERATED FROM PYTHON SOURCE LINES 53-55 Building an AequilibraE graph from our network is pretty straightforward. We assign our network to be the graph's network ... .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python graph = Graph() graph.network = net .. GENERATED FROM PYTHON SOURCE LINES 59-60 ... and then set the graph's configurations. .. GENERATED FROM PYTHON SOURCE LINES 60-70 .. code-block:: Python graph.prepare_graph(np.arange(1, 25)) # sets the centroids for which we will perform computation graph.set_graph("length") # sets the cost field for path computation graph.set_skimming(["length", "free_flow_time"]) # sets the skims to be computed graph.set_blocked_centroid_flows(False) # we don't block flows through centroids because all nodes # in the Sioux Falls network are centroids .. rst-class:: sphx-glr-script-out .. code-block:: none Cost field with wrong type. Converting to float64 .. GENERATED FROM PYTHON SOURCE LINES 71-74 Two of AequilibraE's new features consist in directly computing path or skims. Let's compute the path between nodes 1 and 17... .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python res = graph.compute_path(1, 17) .. GENERATED FROM PYTHON SOURCE LINES 77-78 ... and print the corresponding nodes... .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python res.path_nodes .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 1, 2, 6, 8, 16, 17]) .. GENERATED FROM PYTHON SOURCE LINES 81-82 ... and the path links. .. GENERATED FROM PYTHON SOURCE LINES 82-83 .. code-block:: Python res.path .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 1, 4, 16, 22, 49]) .. GENERATED FROM PYTHON SOURCE LINES 84-89 For path computation, when we call the method ``graph.compute_path(1, 17)``, we are calling the class ``PathComputation`` and storing its results into a variable. Notice that other methods related to path computation, such as ``milepost`` can also be used with ``res``. .. GENERATED FROM PYTHON SOURCE LINES 91-93 For skim computation, the process is quite similar. When calligng the method ``graph.compute_skims()`` we are actually calling the class ``NetworkSkimming``, and storing its results into ``skm``. .. GENERATED FROM PYTHON SOURCE LINES 93-96 .. code-block:: Python skm = graph.compute_skims() .. rst-class:: sphx-glr-script-out .. code-block:: none : 0%| | 0/24 [00:00`_ .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.166 seconds) .. _sphx_glr_download__auto_examples_path_computation_plot_graph_from_arbitrary_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_graph_from_arbitrary_data.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_graph_from_arbitrary_data.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_graph_from_arbitrary_data.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_