.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/trip_distribution/plot_skimming.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_trip_distribution_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 11-18 .. 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 20-21 We create the example project inside our temp folder .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr, "coquimbo") .. GENERATED FROM PYTHON SOURCE LINES 26-29 .. code-block:: Python import logging import sys .. GENERATED FROM PYTHON SOURCE LINES 30-38 .. code-block:: Python # We the project opens, we can tell the logger to direct all messages to the terminal as well 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 39-41 Network Skimming ---------------- .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python from aequilibrae.paths import NetworkSkimming import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 47-48 Let's build all graphs .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: Python project.network.build_graphs() # We get warnings that several fields in the project are filled with NaNs. # This is true, but we won't use those fields. .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/aequilibrae/project/network/network.py:327: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)` df = pd.read_sql(sql, conn).fillna(value=np.nan) 2024-08-19 06:14:04,699;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2024-08-19 06:14:04,772;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2024-08-19 06:14:04,860;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2024-08-19 06:14:04,946;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 53-54 We grab the graph for cars .. GENERATED FROM PYTHON SOURCE LINES 54-70 .. 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-08-19 06:14:05,017;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 71-72 And run the skimming .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python skm = NetworkSkimming(graph) skm.execute() .. GENERATED FROM PYTHON SOURCE LINES 76-77 The result is an AequilibraEMatrix object .. GENERATED FROM PYTHON SOURCE LINES 77-82 .. 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 83-84 Or access each matrix, lets just look at the first 3x3 .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. 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 87-88 We can save it to the project if we want .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python skm.save_to_project("base_skims") .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-08-19 06:14:05,132;WARNING ; Matrix Record has been saved to the database .. GENERATED FROM PYTHON SOURCE LINES 91-92 We can also retrieve this skim record to write something to its description .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. 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 98-99 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.822 seconds) .. _sphx_glr_download__auto_examples_trip_distribution_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 ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_skimming.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_skimming.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_