From 51b0606e4daf2005a92137fed21bc5a3da7c10b9 Mon Sep 17 00:00:00 2001 From: Jawad Mahmud Hoque <35570340+jawadmhoque@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:52:59 -0500 Subject: [PATCH 1/3] Updating mtc.py Updates nodes_mtc_df to create a copy of model_node_id field as N. Updated pnr to be strings to conform to BCM coding. --- lasso/mtc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lasso/mtc.py b/lasso/mtc.py index 71c3b1e2..c125b37c 100644 --- a/lasso/mtc.py +++ b/lasso/mtc.py @@ -1751,8 +1751,11 @@ def roadway_standard_to_mtc_network( lambda g: g.y ) + roadway_network.nodes_mtc_df["pnr"] = np.where(roadway_network.nodes_mtc_df['pnr']==0, '0.0', '1.0') + # CUBE expect node id to be N - roadway_network.nodes_mtc_df.rename(columns={"model_node_id": "N"}, inplace=True) + # roadway_network.nodes_mtc_df.rename(columns={"model_node_id": "N"}, inplace=True) + roadway_network.nodes_mtc_df['model_node_id']=roadway_network.nodes_mtc_df['N'] return roadway_network From c9c749542d2a594a70f51afc25ab40858df43b40 Mon Sep 17 00:00:00 2001 From: Jawad Mahmud Hoque <35570340+jawadmhoque@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:53:30 -0500 Subject: [PATCH 2/3] Update 01-BCM_ApplyHighwayProjectCards.ipynb Deleted 2020 network creation step. --- scripts/01-BCM_ApplyHighwayProjectCards.ipynb | 108 +++--------------- 1 file changed, 15 insertions(+), 93 deletions(-) diff --git a/scripts/01-BCM_ApplyHighwayProjectCards.ipynb b/scripts/01-BCM_ApplyHighwayProjectCards.ipynb index a4147a53..257a1f27 100644 --- a/scripts/01-BCM_ApplyHighwayProjectCards.ipynb +++ b/scripts/01-BCM_ApplyHighwayProjectCards.ipynb @@ -125,36 +125,6 @@ "## List of Project Cards to be Applied to the 2015 Scenario" ] }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Added_networkforexternals_Sacto2_GN_11142022.yml',\n", - " 'assignable_and_ace_transit_connectors.yml',\n", - " 'CCTA.yml',\n", - " 'Externals_SJQ_Add_complete_network_10032022.yml',\n", - " 'Sonoma_Marin_Area_Rail_Transit_add_rail_link.yml',\n", - " 'Year 2015 Modify Express Lanes and USE on I-580 EB (segment 02) Hacienda Drive to Airway Blvd_v2.yml',\n", - " 'Year 2015 Modify HOV Lanes and USE on CA-237 EB US 101 to North 1st Street_v2.yml',\n", - " 'Year 2015 Modify HOV Lanes and USE on CA-237 WB North 1st Street to US 101_v2.yml',\n", - " 'Year 2015 Modify HOV Lanes and USE on I-680 NB from Alcosta Blvd to Livorna Road_v2.yml',\n", - " 'Year 2015 Modify HOV Lanes and USE on I-680 SB from Benicia Bridge to Alcosta Blvd_v2.yml',\n", - " 'year_2015_managed_lane_i680n_ca_242_to_benicia_bridge.yml']" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "os.listdir(os.path.join(card_dir, '2020'))" - ] - }, { "cell_type": "code", "execution_count": 72, @@ -8318,61 +8288,6 @@ "updated_net = mtc.roadway_standard_to_mtc_network(model_net, parameters=parameters)" ] }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [], - "source": [ - "updated_net.nodes_mtc_df['pnr']=updated_net.nodes_mtc_df['pnr'].apply(lambda x: 0 if x==\"\" else int(x))\n", - "updated_net.nodes_mtc_df['model_node_id']=updated_net.nodes_mtc_df['N']" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [], - "source": [ - "transit_links=pd.read_csv(os.path.join(transit_dir,'2020', \"all_transit_links.csv\"))\n", - "transit_links=transit_links.rename(columns={'line':'has_transit'})\n", - "transit_links['has_transit'] = 1\n", - "has_transit = pd.merge(transit_links, updated_net.links_mtc_df[['A','B','model_link_id']])\n", - "\n", - "updated_net.links_mtc_df['name']=updated_net.links_mtc_df['name'].apply(lambda x: \"\" if type(x)==int else x)\n", - "updated_net.links_mtc_df['name']=updated_net.links_mtc_df['name'].apply(lambda x: util.shorten_name(x))\n", - "updated_net.links_mtc_df['has_transit']=updated_net.links_mtc_df['model_link_id'].map(dict(zip(has_transit['model_link_id'],has_transit['has_transit']))).fillna(0)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "# curr_scenario.road_net.links_df = updated_net.links_mtc_df.copy()\n", - "# curr_scenario.road_net.nodes_df = updated_net.nodes_mtc_df.copy()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "curr_scenario.road_net.links_df=curr_scenario.road_net.links_df[curr_scenario.road_net.links_df['geometry'].notnull()]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "scenario_2020 = Scenario.create_scenario(base_scenario = curr_scenario)" - ] - }, { "cell_type": "code", "execution_count": 54, @@ -14071,21 +13986,18 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ - "updated_net.nodes_mtc_df['pnr']=updated_net.nodes_mtc_df['pnr'].apply(lambda x: 0 if x==\"\" else int(x))\n", - "updated_net.nodes_mtc_df['model_node_id']=updated_net.nodes_mtc_df['N']" + "updated_net.links_mtc_df=updated_net.links_mtc_df[updated_net.links_mtc_df['geometry'].notnull()]" ] }, { - "cell_type": "code", - "execution_count": 79, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "updated_net.links_mtc_df=updated_net.links_mtc_df[updated_net.links_mtc_df['geometry'].notnull()]" + "# Add has_transit attribute to links carrying transit" ] }, { @@ -18932,6 +18844,16 @@ " 'lanes_EA', 'lanes_AM', 'lanes_MD', 'lanes_PM', 'lanes_EV','geometry', 'has_transit']].to_file(os.path.join(pickle_dir, 'all_projects',\"complete_network_2035.shp\"))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outpur_network_2035_dir = os.path.join(pickle_dir, '2035')\n", + "os.makedirs(outpur_network_2035_dir, exist_ok=True)" + ] + }, { "cell_type": "code", "execution_count": 92, @@ -18979,7 +18901,7 @@ ], "source": [ "model_net_updated.write_roadway_as_fixedwidth(\n", - " output_dir = os.path.join(pickle_dir, 'all_projects'),\n", + " output_dir = outpur_network_2035_dir,\n", " output_link_txt = 'links.txt',\n", " output_node_txt = 'nodes.txt',\n", " output_link_header_width_txt = 'links_header_width.txt',\n", From 60052ac264d6ef5809b880c32a143143690c02d2 Mon Sep 17 00:00:00 2001 From: Jawad Mahmud Hoque <35570340+jawadmhoque@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:53:56 -0500 Subject: [PATCH 3/3] Create 02-BCM_ApplyTransitProjectCards.ipynb Added Yue's clean script to add transit project cards --- scripts/02-BCM_ApplyTransitProjectCards.ipynb | 278 ++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 scripts/02-BCM_ApplyTransitProjectCards.ipynb diff --git a/scripts/02-BCM_ApplyTransitProjectCards.ipynb b/scripts/02-BCM_ApplyTransitProjectCards.ipynb new file mode 100644 index 00000000..70e23917 --- /dev/null +++ b/scripts/02-BCM_ApplyTransitProjectCards.ipynb @@ -0,0 +1,278 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import sys\n", + "import yaml\n", + "\n", + "import numpy as np\n", + "import pandas as pd\n", + "\n", + "from network_wrangler import RoadwayNetwork\n", + "from network_wrangler import TransitNetwork\n", + "from network_wrangler import ProjectCard\n", + "from network_wrangler import Scenario\n", + "from network_wrangler import WranglerLogger\n", + "\n", + "from lasso import ModelRoadwayNetwork\n", + "from lasso import StandardTransit\n", + "from lasso import Parameters\n", + "from lasso import mtc\n", + "from lasso import util\n", + "\n", + "from lasso import Project\n", + "\n", + "from lasso import bicounty\n", + "\n", + "import pickle" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import logging\n", + "logger = logging.getLogger(\"WranglerLogger\")\n", + "logger.setLevel(logging.INFO)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Remote I/O" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "root_dir = \"\"\n", + "input_dir = os.path.join(root_dir, 'data', 'processed', 'version_03')\n", + "output_dir = os.path.join(root_dir, 'data', 'processed', 'version_04')\n", + "lasso_dir = os.path.join(f\"{root_dir}/software/Lasso\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "parameters = Parameters(lasso_base_dir = lasso_dir)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Load Scenario" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pickle_file_name = f\"{input_dir}/version_03_scenario.pickle\"\n", + "curr_scenario = pickle.load(open(pickle_file_name, 'rb'))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Make Travel Model Network" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net = ModelRoadwayNetwork.from_RoadwayNetwork(\n", + " roadway_network_object = curr_scenario.road_net, \n", + " parameters = parameters\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net.nodes_mtc_df = model_net.nodes_df.copy()\n", + "model_net.links_mtc_df = model_net.links_df.copy()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Write to disk" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Write model network as geopackage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# MODEL_ROADWAY_LINK_VARIABLES = [\n", + "# 'A','B','model_link_id','shstGeometryId','name','roadway', # IDs\n", + "# 'ft','assignable','cntype','distance','county', # Misc attributes\n", + "# 'bike_access','drive_access','walk_access','rail_only','bus_only','transit', # Mode attributes\n", + "# 'managed','tollbooth','tollseg','segment_id', # Managed roadway\n", + "# 'lanes_EA','lanes_AM','lanes_MD','lanes_PM','lanes_EV', # Lanes\n", + "# 'useclass_EA','useclass_AM','useclass_MD','useclass_PM','useclass_EV', # Use classes\n", + "# 'nmt2010', 'nmt2020', # bicounty nm variable \n", + "# 'geometry' # geometry\n", + "# ]\n", + "\n", + "# MODEL_ROADWAY_NODE_VARIABLES = [\n", + "# 'N','osm_node_id','tap_id', # IDs\n", + "# 'county', # Misc attributes\n", + "# 'bike_access','drive_access','walk_access','rail_only','farezone', 'pnr', # Mode attributes\n", + "# 'geometry' # geometry\n", + "# ]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# model_net.write_roadway_as_shp(\n", + "# output_dir= os.path.join(output_dir, 'model_networks', 'geopackages'),\n", + "# link_output_variables = MODEL_ROADWAY_LINK_VARIABLES,\n", + "# node_output_variables = MODEL_ROADWAY_NODE_VARIABLES,\n", + "# output_gpkg = 'model_net.gpkg',\n", + "# output_link_gpkg_layer = 'roadway_links',\n", + "# output_node_gpkg_layer = 'roadway_nodes',\n", + "# #output_gpkg_link_filter = args.gpkg_link_filter\n", + "# )" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Write model network for Cube" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model_net.write_roadway_as_fixedwidth(\n", + " output_dir = os.path.join(output_dir, 'model_networks'),\n", + " output_link_txt = 'links.txt',\n", + " output_node_txt = 'nodes.txt',\n", + " output_link_header_width_txt = 'links_header_width.txt',\n", + " output_node_header_width_txt = 'nodes_header_width.txt',\n", + " output_cube_network_script = 'make_complete_network_from_fixed_width_file.s',\n", + " #drive_only = True\n", + ")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### write pickle file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "working_scenario_filename = os.path.join(output_dir, 'scenario_2020.pickle')\n", + "pickle.dump(curr_scenario, open(working_scenario_filename, 'wb'))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### write to standard format" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# model_net.write(path = os.path.join(output_dir, 'standard_networks'))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.7.12 ('bicounty')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.12" + }, + "vscode": { + "interpreter": { + "hash": "e0dd45cdd72f7ee8b001e53dc4a07864935ca72b45255f9ee986f16092d7a114" + } + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}