
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_auto_examples/skimming/plot_skimming.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download__auto_examples_skimming_plot_skimming.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr__auto_examples_skimming_plot_skimming.py:


.. _example_usage_skimming:

Network skimming
================

In this example, we show how to perform network skimming for Coquimbo, a city in La Serena Metropolitan Area in Chile.

.. GENERATED FROM PYTHON SOURCE LINES 27-31

.. seealso::
    Several functions, methods, classes and modules are used in this example:

    * :func:`aequilibrae.paths.NetworkSkimming`

.. GENERATED FROM PYTHON SOURCE LINES 33-40

.. code-block:: Python


    # Imports
    from uuid import uuid4
    from tempfile import gettempdir
    from os.path import join
    from aequilibrae.utils.create_example import create_example








.. GENERATED FROM PYTHON SOURCE LINES 42-48

.. code-block:: Python


    # We create the example project inside our temp folder
    fldr = join(gettempdir(), uuid4().hex)

    project = create_example(fldr, "coquimbo")








.. GENERATED FROM PYTHON SOURCE LINES 49-52

.. code-block:: Python

    import logging
    import sys








.. GENERATED FROM PYTHON SOURCE LINES 53-54

When the project opens, we can tell the logger to direct all messages to the terminal as well

.. GENERATED FROM PYTHON SOURCE LINES 54-60

.. code-block:: Python

    logger = project.logger
    stdout_handler = logging.StreamHandler(sys.stdout)
    formatter = logging.Formatter("%(asctime)s;%(levelname)s ; %(message)s")
    stdout_handler.setFormatter(formatter)
    logger.addHandler(stdout_handler)








.. GENERATED FROM PYTHON SOURCE LINES 61-63

Network Skimming
----------------

.. GENERATED FROM PYTHON SOURCE LINES 65-68

.. code-block:: Python

    from aequilibrae.paths import NetworkSkimming
    import numpy as np








.. GENERATED FROM PYTHON SOURCE LINES 69-70

Let's build all graphs

.. GENERATED FROM PYTHON SOURCE LINES 70-74

.. code-block:: Python

    project.network.build_graphs()
    # We get warnings that several fields in the project are filled with ``NaN``s.
    # This is true, but we won't use those fields.





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    2024-11-04 00:38:01,880;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations
    2024-11-04 00:38:01,957;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations
    2024-11-04 00:38:02,053;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations
    2024-11-04 00:38:02,143;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations




.. GENERATED FROM PYTHON SOURCE LINES 75-76

We grab the graph for cars

.. GENERATED FROM PYTHON SOURCE LINES 76-92

.. code-block:: Python

    graph = project.network.graphs["c"]

    # we also see what graphs are available
    project.network.graphs.keys()

    # let's say we want to minimize the distance
    graph.set_graph("distance")

    # And will skim distance while we are at it, other fields like ``free_flow_time`` or ``travel_time`` 
    # can be added here as well
    graph.set_skimming(["distance"])

    # But let's say we only want a skim matrix for nodes 28-40, and 49-60 (inclusive), 
    # these happen to be a selection of western centroids.
    graph.prepare_graph(np.array(list(range(28, 41)) + list(range(49, 91))))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    2024-11-04 00:38:02,214;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations




.. GENERATED FROM PYTHON SOURCE LINES 93-94

And run the skimming

.. GENERATED FROM PYTHON SOURCE LINES 94-97

.. code-block:: Python

    skm = NetworkSkimming(graph)
    skm.execute()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


                                                      :   0%|          | 0/55 [00:00<?, ?it/s]
    1/55                                              :   2%|▏         | 1/55 [00:00<00:00, 255.21it/s]
    2/55                                              :   4%|▎         | 2/55 [00:00<00:00, 478.88it/s]
    3/55                                              :   5%|▌         | 3/55 [00:00<00:00, 680.93it/s]
    4/55                                              :   7%|▋         | 4/55 [00:00<00:00, 855.02it/s]
    5/55                                              :   9%|▉         | 5/55 [00:00<00:00, 1004.96it/s]
    6/55                                              :  11%|█         | 6/55 [00:00<00:00, 1144.47it/s]
    7/55                                              :  13%|█▎        | 7/55 [00:00<00:00, 1272.65it/s]
    8/55                                              :  15%|█▍        | 8/55 [00:00<00:00, 1389.59it/s]
    9/55                                              :  16%|█▋        | 9/55 [00:00<00:00, 1484.88it/s]
    10/55                                             :  18%|█▊        | 10/55 [00:00<00:00, 1580.61it/s]
    11/55                                             :  20%|██        | 11/55 [00:00<00:00, 1672.92it/s]
    12/55                                             :  22%|██▏       | 12/55 [00:00<00:00, 1756.16it/s]
    13/55                                             :  24%|██▎       | 13/55 [00:00<00:00, 1821.60it/s]
    14/55                                             :  25%|██▌       | 14/55 [00:00<00:00, 1892.98it/s]
    15/55                                             :  27%|██▋       | 15/55 [00:00<00:00, 1961.24it/s]
    16/55                                             :  29%|██▉       | 16/55 [00:00<00:00, 2017.22it/s]
    17/55                                             :  31%|███       | 17/55 [00:00<00:00, 2064.72it/s]
    18/55                                             :  33%|███▎      | 18/55 [00:00<00:00, 2117.44it/s]
    19/55                                             :  35%|███▍      | 19/55 [00:00<00:00, 2172.33it/s]
    20/55                                             :  36%|███▋      | 20/55 [00:00<00:00, 2222.91it/s]
    21/55                                             :  38%|███▊      | 21/55 [00:00<00:00, 2258.01it/s]
    22/55                                             :  40%|████      | 22/55 [00:00<00:00, 2302.26it/s]
    23/55                                             :  42%|████▏     | 23/55 [00:00<00:00, 2345.58it/s]
    24/55                                             :  44%|████▎     | 24/55 [00:00<00:00, 2385.33it/s]
    25/55                                             :  45%|████▌     | 25/55 [00:00<00:00, 2413.90it/s]
    26/55                                             :  47%|████▋     | 26/55 [00:00<00:00, 2447.36it/s]
    27/55                                             :  49%|████▉     | 27/55 [00:00<00:00, 2484.45it/s]
    28/55                                             :  51%|█████     | 28/55 [00:00<00:00, 2507.59it/s]
    29/55                                             :  53%|█████▎    | 29/55 [00:00<00:00, 2538.02it/s]
    30/55                                             :  55%|█████▍    | 30/55 [00:00<00:00, 2560.47it/s]
    31/55                                             :  56%|█████▋    | 31/55 [00:00<00:00, 2581.67it/s]
    32/55                                             :  58%|█████▊    | 32/55 [00:00<00:00, 2565.47it/s]
    33/55                                             :  60%|██████    | 33/55 [00:00<00:00, 2591.99it/s]
    34/55                                             :  62%|██████▏   | 34/55 [00:00<00:00, 2627.53it/s]
    35/55                                             :  64%|██████▎   | 35/55 [00:00<00:00, 2643.20it/s]
    36/55                                             :  65%|██████▌   | 36/55 [00:00<00:00, 2665.68it/s]
    37/55                                             :  67%|██████▋   | 37/55 [00:00<00:00, 2687.17it/s]
    38/55                                             :  69%|██████▉   | 38/55 [00:00<00:00, 2708.90it/s]
    39/55                                             :  71%|███████   | 39/55 [00:00<00:00, 2699.22it/s]
    40/55                                             :  73%|███████▎  | 40/55 [00:00<00:00, 2717.18it/s]
    41/55                                             :  75%|███████▍  | 41/55 [00:00<00:00, 2734.23it/s]
    42/55                                             :  76%|███████▋  | 42/55 [00:00<00:00, 2754.71it/s]
    43/55                                             :  78%|███████▊  | 43/55 [00:00<00:00, 2770.86it/s]
    44/55                                             :  80%|████████  | 44/55 [00:00<00:00, 2777.77it/s]
    45/55                                             :  82%|████████▏ | 45/55 [00:00<00:00, 2791.82it/s]
    46/55                                             :  84%|████████▎ | 46/55 [00:00<00:00, 2807.19it/s]
    47/55                                             :  85%|████████▌ | 47/55 [00:00<00:00, 2822.14it/s]
    48/55                                             :  87%|████████▋ | 48/55 [00:00<00:00, 2830.40it/s]
    49/55                                             :  89%|████████▉ | 49/55 [00:00<00:00, 2843.16it/s]
    50/55                                             :  91%|█████████ | 50/55 [00:00<00:00, 2860.00it/s]
    51/55                                             :  93%|█████████▎| 51/55 [00:00<00:00, 2874.43it/s]
    52/55                                             :  95%|█████████▍| 52/55 [00:00<00:00, 2880.36it/s]
    53/55                                             :  96%|█████████▋| 53/55 [00:00<00:00, 2904.87it/s]
    54/55                                             :  98%|█████████▊| 54/55 [00:00<00:00, 2924.26it/s]
    55/55                                             : 100%|██████████| 55/55 [00:00<00:00, 2949.09it/s]
    Saving Outputs                                    : 100%|██████████| 55/55 [00:00<00:00, 2864.96it/s]
                                                                                                     



.. GENERATED FROM PYTHON SOURCE LINES 98-99

The result is an AequilibraEMatrix object

.. GENERATED FROM PYTHON SOURCE LINES 99-104

.. code-block:: Python

    skims = skm.results.skims

    # Which we can manipulate directly from its temp file, if we wish
    skims.matrices[:3, :3, :]





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    array([[[   0.        ],
            [4166.92919206],
            [5532.32681478]],

           [[3733.4499255 ],
            [   0.        ],
            [3311.30654014]],

           [[5446.26074416],
            [3596.12274848],
            [   0.        ]]])



.. GENERATED FROM PYTHON SOURCE LINES 105-106

Or access each matrix, lets just look at the first 3x3

.. GENERATED FROM PYTHON SOURCE LINES 106-108

.. code-block:: Python

    skims.distance[:3, :3]





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    array([[   0.        , 4166.92919206, 5532.32681478],
           [3733.4499255 ,    0.        , 3311.30654014],
           [5446.26074416, 3596.12274848,    0.        ]])



.. GENERATED FROM PYTHON SOURCE LINES 109-110

We can save it to the project if we want

.. GENERATED FROM PYTHON SOURCE LINES 110-112

.. code-block:: Python

    skm.save_to_project("base_skims")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    2024-11-04 00:38:02,312;WARNING ; Matrix Record has been saved to the database




.. GENERATED FROM PYTHON SOURCE LINES 113-114

We can also retrieve this skim record to write something to its description

.. GENERATED FROM PYTHON SOURCE LINES 114-119

.. code-block:: Python

    matrices = project.matrices
    mat_record = matrices.get_record("base_skims")
    mat_record.description = "minimized distance while also skimming distance for just a few nodes"
    mat_record.save()








.. GENERATED FROM PYTHON SOURCE LINES 120-121

.. code-block:: Python

    project.close()








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.815 seconds)


.. _sphx_glr_download__auto_examples_skimming_plot_skimming.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_skimming.ipynb <plot_skimming.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_skimming.py <plot_skimming.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_skimming.zip <plot_skimming.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
