.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "route_choice/_auto_examples/plot_subarea_analysis.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_route_choice__auto_examples_plot_subarea_analysis.py: .. _example_usage_sub_area_analysis: Route Choice with sub-area analysis =================================== In this example, we show how to perform sub-area analysis using route choice assignment, for a city in La Serena Metropolitan Area in Chile. .. admonition:: References * :doc:`../../route_choice` .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.paths.Graph` * :func:`aequilibrae.paths.RouteChoice` * :func:`aequilibrae.paths.SubAreaAnalysis` * :func:`aequilibrae.matrix.AequilibraeMatrix` .. GENERATED FROM PYTHON SOURCE LINES 23-37 .. code-block:: Python # Imports from uuid import uuid4 from tempfile import gettempdir from os.path import join import itertools import pandas as pd import numpy as np import folium from aequilibrae.utils.create_example import create_example .. GENERATED FROM PYTHON SOURCE LINES 39-45 .. 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 46-49 .. code-block:: Python import logging import sys .. GENERATED FROM PYTHON SOURCE LINES 50-58 .. 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 59-64 Model parameters ---------------- We'll set the parameters for our route choice model. These are the parameters that will be used to calculate the utility of each path. In our example, the utility is equal to :math:`distance * theta`, and the path overlap factor (PSL) is equal to :math:`beta`. .. GENERATED FROM PYTHON SOURCE LINES 64-71 .. code-block:: Python # Distance factor theta = 0.011 # PSL parameter beta = 1.1 .. GENERATED FROM PYTHON SOURCE LINES 72-73 Let's build all graphs .. GENERATED FROM PYTHON SOURCE LINES 73-77 .. 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 2025-01-15 19:56:36,898;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2025-01-15 19:56:36,988;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2025-01-15 19:56:37,094;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2025-01-15 19:56:37,199;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 78-79 We grab the graph for cars .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python graph = project.network.graphs["c"] .. GENERATED FROM PYTHON SOURCE LINES 82-83 We also see what graphs are available .. GENERATED FROM PYTHON SOURCE LINES 83-84 .. code-block:: Python project.network.graphs.keys() .. rst-class:: sphx-glr-script-out .. code-block:: none dict_keys(['b', 'c', 't', 'w']) .. GENERATED FROM PYTHON SOURCE LINES 85-87 Let's say that utility is just a function of distance. So we build our *utility* field as the :math:`distance * theta`. .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python graph.network = graph.network.assign(utility=graph.network.distance * theta) .. GENERATED FROM PYTHON SOURCE LINES 90-91 Prepare the graph with all nodes of interest as centroids .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python graph.prepare_graph(graph.centroids) .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-01-15 19:56:37,276;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 94-95 And set the cost of the graph the as the utility field just created .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: Python graph.set_graph("utility") .. GENERATED FROM PYTHON SOURCE LINES 98-101 We allow flows through "centroid connectors" because our centroids are not really centroids. If we have actual centroid connectors in the network (and more than one per centroid), then we should remove them from the graph. .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: Python graph.set_blocked_centroid_flows(False) graph.graph.head() .. raw:: html
link_id a_node b_node direction id distance speed travel_time capacity osm_id lanes modes utility __supernet_id__ __compressed_id__
0 34806 0 8743 1 0 957.271672 NaN NaN 99999.0 NaN NaN ct 10.529988 34273 0
1 34807 1 1180 1 1 1446.491380 NaN NaN 99999.0 NaN NaN ct 15.911405 34275 1
2 34808 2 1222 1 2 1646.723896 NaN NaN 99999.0 NaN NaN ct 18.113963 34277 2
3 34809 3 5674 1 3 1196.124943 NaN NaN 99999.0 NaN NaN ct 13.157374 34279 3
4 34810 4 6339 1 4 536.920082 NaN NaN 99999.0 NaN NaN ct 5.906121 34281 4


.. GENERATED FROM PYTHON SOURCE LINES 105-108 Mock demand matrix ------------------ We'll create a mock demand matrix with demand ``10`` for every zone and prepare it for computation. .. GENERATED FROM PYTHON SOURCE LINES 108-118 .. code-block:: Python from aequilibrae.matrix import AequilibraeMatrix names_list = ["demand"] mat = AequilibraeMatrix() mat.create_empty(zones=graph.num_zones, matrix_names=names_list, memory_only=True) mat.index = graph.centroids[:] mat.matrices[:, :, 0] = np.full((graph.num_zones, graph.num_zones), 10.0) mat.computational_view() .. GENERATED FROM PYTHON SOURCE LINES 119-124 Sub-area preparation -------------------- We need to define some polygon for out sub-area analysis, here we'll use a section of zones and create out polygon as the union of their geometry. It's best to choose a polygon that avoids any unnecessary intersections with links as the resource requirements of this approach grow quadratically with the number of links cut. .. GENERATED FROM PYTHON SOURCE LINES 124-129 .. code-block:: Python zones_of_interest = [29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 49, 50, 51, 52, 57, 58, 59, 60] zones = project.zoning.data.set_index("zone_id") zones = zones.loc[zones_of_interest] zones.head() .. raw:: html
ogc_fid area name population employment geometry
zone_id
29 29 None None 4665.233898 None MULTIPOLYGON (((-71.32613 -29.97973, -71.32665...
30 30 None None 4190.454424 None MULTIPOLYGON (((-71.33702 -29.98829, -71.33754...
31 31 None None 4107.854460 None MULTIPOLYGON (((-71.3308 -29.98206, -71.33131 ...
32 32 None None 4815.264246 None MULTIPOLYGON (((-71.34687 -29.99529, -71.34791...
33 33 None None 4556.225468 None MULTIPOLYGON (((-71.33909 -29.97428, -71.33857...


.. GENERATED FROM PYTHON SOURCE LINES 130-135 Sub-area analysis ----------------- From here there are two main paths to conduct a sub-area analysis, manual or automated. AequilibraE ships with a small class that handle most of the details regarding the implementation and extract of the relevant data. It also exposes all the tools necessary to conduct this analysis yourself if you need fine grained control. .. GENERATED FROM PYTHON SOURCE LINES 137-142 Automated sub-area analysis ~~~~~~~~~~~~~~~~~~~~~~~~~~~ We first construct out SubAreaAnalysis object from the graph, zones, and matrix we previously constructed, then configure the route choice assignment and execute it. From there the ``post_process`` method is able to use the route choice assignment results to construct the desired demand matrix as a DataFrame. .. GENERATED FROM PYTHON SOURCE LINES 142-150 .. code-block:: Python from aequilibrae.paths import SubAreaAnalysis subarea = SubAreaAnalysis(graph, zones, mat) subarea.rc.set_choice_set_generation("lp", max_routes=5, penalty=1.02, store_results=False) subarea.rc.execute(perform_assignment=True) demand = subarea.post_process() demand .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-01-15 19:56:38,149;INFO ; Created: 650 edge pairs from 26 edges /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 31425 and direction -1 with compressed id 9483 warnings.warn( /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 31425 and direction 1 with compressed id 14421 warnings.warn( /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 21724 and direction 1 with compressed id 14421 warnings.warn( /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 21724 and direction -1 with compressed id 9483 warnings.warn( .. raw:: html
demand
origin id destination id
29 29 10.000000
30 10.000000
31 10.000000
32 10.000000
33 10.000000
... ... ...
79892 67891 47.673554
72092 1604.171242
72161 461.310580
73381 33.512579
73541 33.512579

593 rows × 1 columns



.. GENERATED FROM PYTHON SOURCE LINES 151-152 We'll re-prepare our graph but with our new "external" ODs. .. GENERATED FROM PYTHON SOURCE LINES 152-157 .. code-block:: Python new_centroids = np.unique(demand.reset_index()[["origin id", "destination id"]].to_numpy().reshape(-1)) graph.prepare_graph(new_centroids) graph.set_graph("utility") new_centroids .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-01-15 19:56:57,636;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations array([ 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 49, 50, 51, 52, 57, 58, 59, 60, 61044, 67891, 68671, 72081, 72092, 72096, 72134, 72161, 73381, 73394, 73432, 73506, 73541, 73565, 73589, 75548, 77285, 77287, 77289, 79297, 79892]) .. GENERATED FROM PYTHON SOURCE LINES 158-159 We can then perform an assignment using our new demand matrix on the limited graph .. GENERATED FROM PYTHON SOURCE LINES 159-166 .. code-block:: Python from aequilibrae.paths import RouteChoice rc = RouteChoice(graph) rc.add_demand(demand) rc.set_choice_set_generation("link-penalisation", max_routes=5, penalty=1.02, store_results=False, seed=123) rc.execute(perform_assignment=True) .. GENERATED FROM PYTHON SOURCE LINES 167-168 And plot the link loads for easy viewing .. GENERATED FROM PYTHON SOURCE LINES 168-211 .. code-block:: Python subarea_zone = folium.Polygon( locations=[(x[1], x[0]) for x in zones.unary_union.boundary.coords], fill_color="blue", fill_opacity=0.5, fill=True, stroke=False, ) def plot_results(link_loads): link_loads = link_loads[link_loads.tot > 0] max_load = link_loads["tot"].max() links = project.network.links.data loaded_links = links.merge(link_loads, on="link_id", how="inner") loads_lyr = folium.FeatureGroup("link_loads") # Maximum thickness we would like is probably a 10, so let's make sure we don't go over that factor = 10 / max_load # Let's create the layers for _, rec in loaded_links.iterrows(): points = rec.geometry.wkt.replace("LINESTRING ", "").replace("(", "").replace(")", "").split(", ") points = "[[" + "],[".join([p.replace(" ", ", ") for p in points]) + "]]" # we need to take from x/y to lat/long points = [[x[1], x[0]] for x in eval(points)] _ = folium.vector_layers.PolyLine( points, tooltip=f"link_id: {rec.link_id}, Flow: {rec.tot:.3f}", color="red", weight=factor * rec.tot, ).add_to(loads_lyr) long, lat = project.conn.execute("select avg(xmin), avg(ymin) from idx_links_geometry").fetchone() map_osm = folium.Map(location=[lat, long], tiles="Cartodb Positron", zoom_start=12) loads_lyr.add_to(map_osm) folium.LayerControl().add_to(map_osm) return map_osm map = plot_results(rc.get_load_results()["demand"]) subarea_zone.add_to(map) map .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/docs/source/route_choice/examples/plot_subarea_analysis.py:169: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. locations=[(x[1], x[0]) for x in zones.unary_union.boundary.coords], .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 212-214 Sub-area further preparation ```````````````````````````` .. GENERATED FROM PYTHON SOURCE LINES 216-217 We take the union of this GeoDataFrame as our polygon. .. GENERATED FROM PYTHON SOURCE LINES 217-220 .. code-block:: Python poly = zones.union_all() poly .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 221-222 It's useful later on to know which links from the network cross our polygon. .. GENERATED FROM PYTHON SOURCE LINES 222-226 .. code-block:: Python links = project.network.links.data inner_links = links[links.crosses(poly.boundary)].sort_index() inner_links.head() .. raw:: html
ogc_fid link_id a_node b_node direction distance modes link_type name speed_ab speed_ba travel_time_ab travel_time_ba capacity_ab capacity_ba osm_id lanes_ab lanes_ba geometry
560 734 734 75548 75321 1 118.672175 ct primary Avenida Costanera 50.0 NaN None None NaN NaN 1.087246e+09 1.0 NaN LINESTRING (-71.31995 -29.96046, -71.32004 -29...
599 777 777 68671 77289 1 98.456777 ct trunk None NaN NaN None None NaN NaN 1.087084e+09 1.0 NaN LINESTRING (-71.31844 -29.96439, -71.31855 -29...
1821 3799 3799 73589 72898 1 38.022984 ct residential Las Violetas NaN NaN None None NaN NaN 7.925576e+08 NaN NaN LINESTRING (-71.3339 -29.98877, -71.33423 -29....
9462 20823 20823 66687 73506 1 36.529160 ct residential Las Orquídeas NaN NaN None None NaN NaN 2.322910e+08 NaN NaN LINESTRING (-71.33567 -29.98926, -71.33536 -29...
10112 21724 21724 77269 77266 0 105.722347 ct residential Suiza NaN NaN None None NaN NaN 2.253009e+08 NaN NaN LINESTRING (-71.31896 -29.96747, -71.31838 -29...


.. GENERATED FROM PYTHON SOURCE LINES 227-228 As well as which nodes are interior. .. GENERATED FROM PYTHON SOURCE LINES 228-232 .. code-block:: Python nodes = project.network.nodes.data.set_index("node_id") inside_nodes = nodes.sjoin(zones, how="inner").sort_index() inside_nodes.head() .. raw:: html
ogc_fid_left is_centroid modes link_types osm_id geometry zone_id ogc_fid_right area name population employment
node_id
29 70084 1 ct z NaN POINT (-71.3234 -29.97278) 29 29 None None 4665.233898 None
30 70085 1 ct z NaN POINT (-71.33567 -29.98311) 30 30 None None 4190.454424 None
31 70086 1 ct z NaN POINT (-71.33088 -29.97783) 31 31 None None 4107.854460 None
32 70087 1 ct z NaN POINT (-71.34347 -29.98923) 32 32 None None 4815.264246 None
33 70088 1 ct z NaN POINT (-71.33627 -29.96964) 33 33 None None 4556.225468 None


.. GENERATED FROM PYTHON SOURCE LINES 233-234 Here we filter those network links to graph links, dropping any dead ends and creating a `link_id, dir` multi-index. .. GENERATED FROM PYTHON SOURCE LINES 234-243 .. code-block:: Python g = ( graph.graph.set_index("link_id") .loc[inner_links.link_id] .drop(graph.dead_end_links, errors="ignore") .reset_index() .set_index(["link_id", "direction"]) ) g.head() .. raw:: html
a_node b_node id distance speed travel_time capacity osm_id lanes modes utility __supernet_id__ __compressed_id__
link_id direction
734 1 33 12522 51 118.672175 50.0 NaN NaN 1.087246e+09 1.0 ct 1.305394 703 49
777 1 20 36 25 98.456777 NaN NaN NaN 1.087084e+09 1.0 ct 1.083025 760 23
3799 1 32 11071 50 38.022984 NaN NaN NaN 7.925576e+08 NaN ct 0.418253 2775 48
20823 1 8070 29 16220 36.529160 NaN NaN NaN 2.322910e+08 NaN ct 0.401821 15745 10070
21724 -1 13709 13710 29612 105.722347 NaN NaN NaN 2.253009e+08 NaN ct 1.162946 16781 9234


.. GENERATED FROM PYTHON SOURCE LINES 244-246 Sub-area visualisation `````````````````````` .. GENERATED FROM PYTHON SOURCE LINES 248-250 Here we'll quickly visualise what out sub-area is looking like. We'll plot the polygon from our zoning system and the links that it cuts. .. GENERATED FROM PYTHON SOURCE LINES 250-270 .. code-block:: Python points = [(link_id, list(x.coords)) for link_id, x in zip(inner_links.link_id, inner_links.geometry)] subarea_layer = folium.FeatureGroup("Cut links") for link_id, line in points: _ = folium.vector_layers.PolyLine( [(x[1], x[0]) for x in line], tooltip=f"link_id: {link_id}", color="red", ).add_to(subarea_layer) long, lat = project.conn.execute("select avg(xmin), avg(ymin) from idx_links_geometry").fetchone() map_osm = folium.Map(location=[lat, long], tiles="Cartodb Positron", zoom_start=12) subarea_zone.add_to(map_osm) subarea_layer.add_to(map_osm) _ = folium.LayerControl().add_to(map_osm) map_osm .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 271-273 Manual sub-area analysis ~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 275-278 In order to perform out analysis we need to know what OD pairs have flow that enters and/or exists our polygon. To do so we perform a select link analysis on all links and pairs of links that cross the boundary. We create them as tuples of tuples to make represent the select link AND sets. .. GENERATED FROM PYTHON SOURCE LINES 278-282 .. code-block:: Python edge_pairs = {x: (x,) for x in itertools.permutations(g.index, r=2)} single_edges = {x: ((x,),) for x in g.index} f"Created: {len(edge_pairs)} edge pairs from {len(single_edges)} edges" .. rst-class:: sphx-glr-script-out .. code-block:: none 'Created: 650 edge pairs from 26 edges' .. GENERATED FROM PYTHON SOURCE LINES 283-284 Here we'll construct and use the Route Choice class to generate our route sets .. GENERATED FROM PYTHON SOURCE LINES 284-286 .. code-block:: Python from aequilibrae.paths import RouteChoice .. GENERATED FROM PYTHON SOURCE LINES 287-288 We'll re-prepare out graph quickly .. GENERATED FROM PYTHON SOURCE LINES 288-295 .. code-block:: Python project.network.build_graphs() graph = project.network.graphs["c"] graph.network = graph.network.assign(utility=graph.network.distance * theta) graph.prepare_graph(graph.centroids) graph.set_graph("utility") graph.set_blocked_centroid_flows(False) .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-01-15 19:57:03,185;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2025-01-15 19:57:03,266;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2025-01-15 19:57:03,363;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2025-01-15 19:57:03,459;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations 2025-01-15 19:57:03,531;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 296-300 This object construction might take a minute depending on the size of the graph due to the construction of the compressed link to network link mapping that's required. This is a one time operation per graph and is cached. We need to supply a Graph and an AequilibraeMatrix or DataFrame via the ``add_demand`` method, if demand is not provided link loading cannot be preformed. .. GENERATED FROM PYTHON SOURCE LINES 300-303 .. code-block:: Python rc = RouteChoice(graph) rc.add_demand(mat) .. GENERATED FROM PYTHON SOURCE LINES 304-305 Here we add the union of edges as select link sets. .. GENERATED FROM PYTHON SOURCE LINES 305-307 .. code-block:: Python rc.set_select_links(single_edges | edge_pairs) .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 31425 and direction -1 with compressed id 9483 warnings.warn( /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 31425 and direction 1 with compressed id 14421 warnings.warn( /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 21724 and direction 1 with compressed id 14421 warnings.warn( /opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/aequilibrae/paths/route_choice.py:464: UserWarning: Two input links map to the same compressed link in the network, removing superfluous link 21724 and direction -1 with compressed id 9483 warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 308-310 For the sake of demonstration we limit out demand matrix to a few OD pairs. This filter is also possible with the automated approach, just edit the ``subarea.rc.demand.df`` DataFrame, however make sure the index remains intact. .. GENERATED FROM PYTHON SOURCE LINES 310-321 .. code-block:: Python ods_pairs_of_interest = [ (4, 39), (92, 37), (31, 58), (4, 19), (39, 34), ] ods_pairs_of_interest = ods_pairs_of_interest + [(x[1], x[0]) for x in ods_pairs_of_interest] rc.demand.df = rc.demand.df.loc[ods_pairs_of_interest].sort_index().astype(np.float32) rc.demand.df .. raw:: html
demand
origin id destination id
4 19 10.0
39 10.0
19 4 10.0
31 58 10.0
34 39 10.0
37 92 10.0
39 4 10.0
34 10.0
58 31 10.0
92 37 10.0


.. GENERATED FROM PYTHON SOURCE LINES 322-323 Perform the assignment .. GENERATED FROM PYTHON SOURCE LINES 323-327 .. code-block:: Python rc.set_choice_set_generation("link-penalisation", max_routes=5, penalty=1.02, store_results=False, seed=123) rc.execute(perform_assignment=True) .. GENERATED FROM PYTHON SOURCE LINES 328-329 We can visualise the current links loads .. GENERATED FROM PYTHON SOURCE LINES 329-333 .. code-block:: Python map = plot_results(rc.get_load_results()["demand"]) subarea_zone.add_to(map) map .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 334-336 We'll pull out just OD matrix results as well we need it for the post-processing, we'll also convert the sparse matrices to SciPy COO matrices. .. GENERATED FROM PYTHON SOURCE LINES 336-340 .. code-block:: Python sl_od = rc.get_select_link_od_matrix_results() edge_totals = {k: sl_od[k]["demand"].to_scipy() for k in single_edges} edge_pair_values = {k: sl_od[k]["demand"].to_scipy() for k in edge_pairs} .. GENERATED FROM PYTHON SOURCE LINES 341-345 For the post processing, we are interested in the demand of OD pairs that enter or exit the sub-area, or do both. For the single enters and exists we can extract that information from the single link select link results. We also need to map the links that cross the boundary to the origin/destination node and the node that appears on the outside of the sub-area. .. GENERATED FROM PYTHON SOURCE LINES 345-366 .. code-block:: Python from collections import defaultdict entered = defaultdict(float) exited = defaultdict(float) for (link_id, dir), v in edge_totals.items(): link = g.loc[link_id, dir] for (o, d), load in v.todok().items(): o = graph.all_nodes[o] d = graph.all_nodes[d] o_inside = o in inside_nodes.index d_inside = d in inside_nodes.index if o_inside and not d_inside: exited[o, graph.all_nodes[link.b_node]] += load elif not o_inside and d_inside: entered[graph.all_nodes[link.a_node], d] += load elif not o_inside and not d_inside: pass .. GENERATED FROM PYTHON SOURCE LINES 367-368 Here he have the load that entered the sub-area .. GENERATED FROM PYTHON SOURCE LINES 368-370 .. code-block:: Python entered .. rst-class:: sphx-glr-script-out .. code-block:: none defaultdict(, {(34, 37): 10.0, (20, 39): 9.88913345336914, (23, 39): 0.1108664870262146}) .. GENERATED FROM PYTHON SOURCE LINES 371-372 and the load that exited the sub-area .. GENERATED FROM PYTHON SOURCE LINES 372-374 .. code-block:: Python exited .. rst-class:: sphx-glr-script-out .. code-block:: none defaultdict(, {(39, 20): 9.873265266418457, (37, 36): 0.07007550448179245, (39, 23): 0.12673552334308624, (37, 19): 9.929924011230469}) .. GENERATED FROM PYTHON SOURCE LINES 375-376 To find the load that both entered and exited we can look at the edge pair select link results. .. GENERATED FROM PYTHON SOURCE LINES 376-390 .. code-block:: Python through = defaultdict(float) for (l1, l2), v in edge_pair_values.items(): link1 = g.loc[l1] link2 = g.loc[l2] for (o, d), load in v.todok().items(): o_inside = o in inside_nodes.index d_inside = d in inside_nodes.index if not o_inside and not d_inside: through[graph.all_nodes[link1.a_node], graph.all_nodes[link2.b_node]] += load through .. rst-class:: sphx-glr-script-out .. code-block:: none defaultdict(, {(21, 23): 4.1274213790893555, (35, 22): 0.2188785821199417, (35, 25): 9.781121253967285, (22, 37): 0.435827374458313, (26, 36): 0.2188785821199417, (23, 36): 5.337530136108398, (23, 38): 4.443591117858887, (25, 37): 9.564172744750977, (39, 26): 0.435827374458313, (39, 23): 5.436751365661621}) .. GENERATED FROM PYTHON SOURCE LINES 391-393 With these results we can construct a new demand matrix. Usually this would be now transplanted onto another network, however for demonstration purposes we'll reuse the same network. .. GENERATED FROM PYTHON SOURCE LINES 393-402 .. code-block:: Python demand = pd.DataFrame( list(entered.values()) + list(exited.values()) + list(through.values()), index=pd.MultiIndex.from_tuples( list(entered.keys()) + list(exited.keys()) + list(through.keys()), names=["origin id", "destination id"] ), columns=["demand"], ).sort_index() demand.head() .. raw:: html
demand
origin id destination id
20 39 9.889133
21 23 4.127421
22 37 0.435827
23 36 5.337530
38 4.443591


.. GENERATED FROM PYTHON SOURCE LINES 403-404 We'll re-prepare our graph but with our new "external" ODs. .. GENERATED FROM PYTHON SOURCE LINES 404-409 .. code-block:: Python new_centroids = np.unique(demand.reset_index()[["origin id", "destination id"]].to_numpy().reshape(-1)) graph.prepare_graph(new_centroids) graph.set_graph("utility") new_centroids .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-01-15 19:57:08,142;WARNING ; Field(s) speed, travel_time, capacity, osm_id, lanes has(ve) at least one NaN value. Check your computations array([19, 20, 21, 22, 23, 25, 26, 34, 35, 36, 37, 38, 39]) .. GENERATED FROM PYTHON SOURCE LINES 410-411 Re-perform our assignment .. GENERATED FROM PYTHON SOURCE LINES 411-416 .. code-block:: Python rc = RouteChoice(graph) rc.add_demand(demand) rc.set_choice_set_generation("link-penalisation", max_routes=5, penalty=1.02, store_results=False, seed=123) rc.execute(perform_assignment=True) .. GENERATED FROM PYTHON SOURCE LINES 417-418 And plot the link loads for easy viewing .. GENERATED FROM PYTHON SOURCE LINES 418-421 .. code-block:: Python map = plot_results(rc.get_load_results()["demand"]) subarea_zone.add_to(map) map .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 35.167 seconds) .. _sphx_glr_download_route_choice__auto_examples_plot_subarea_analysis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_subarea_analysis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_subarea_analysis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_subarea_analysis.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_