Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big Blue Bus speedmap segments deep dive #1164

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
359 changes: 359 additions & 0 deletions rt_segment_speeds/35_bbb_segment_backtrack.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f9f1baa5-2de0-4152-89ec-e43880ea043d",
"metadata": {},
"source": [
"# Speedmap segments \n",
"* The 20th, 50th, 80th percentiles look extremely tight, why?\n",
"* Is this happening in the trip files?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b18ab7df-9592-4897-8193-ca0ccc015930",
"metadata": {},
"outputs": [],
"source": [
"import geopandas as gpd\n",
"import pandas as pd\n",
"\n",
"from segment_speed_utils import helpers\n",
"from segment_speed_utils.project_vars import SEGMENT_GCS, GTFS_DATA_DICT\n",
"from shared_utils import rt_dates\n",
"\n",
"analysis_date = rt_dates.DATES[\"apr2024\"]\n",
"nov_date = rt_dates.DATES[\"nov2023\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48f4c06f-be94-440f-90ee-df365b4f06b4",
"metadata": {},
"outputs": [],
"source": [
"TRIP_FILE = GTFS_DATA_DICT.speedmap_segments.stage4\n",
"SHAPE_FILE = GTFS_DATA_DICT.speedmap_segments.shape_stop_single_segment"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab776438-86bc-4b86-a276-9b52aad3f454",
"metadata": {},
"outputs": [],
"source": [
"operator_name = \"Big Blue Bus Schedule\"\n",
"\n",
"operator_route_df = helpers.import_scheduled_trips(\n",
" analysis_date,\n",
" columns = [\"gtfs_dataset_key\", \"name\", \n",
" \"route_short_name\", \"route_long_name\", \"route_id\"],\n",
" filters = [[(\"name\", \"==\", operator_name)]],\n",
" get_pandas = True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61f76f27-8c20-45dd-8910-ed60fafbf2d1",
"metadata": {},
"outputs": [],
"source": [
"nov_trips = helpers.import_scheduled_trips(\n",
" nov_date,\n",
" columns = [\"gtfs_dataset_key\", \"name\", \"shape_id\", \"route_id\", \n",
" \"route_long_name\", \"route_short_name\"],\n",
" filters = [[(\"name\", \"==\", operator_name)]],\n",
" get_pandas = True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5a6a7ff-8466-403a-abf5-3db4a69f42b6",
"metadata": {},
"outputs": [],
"source": [
"if nov_trips.schedule_gtfs_dataset_key.iloc[0] == operator_route_df.schedule_gtfs_dataset_key.iloc[0]:\n",
" bbb_key = nov_trips.schedule_gtfs_dataset_key.iloc[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d448d865-2546-487b-83e8-eabeaceccdcd",
"metadata": {},
"outputs": [],
"source": [
"def nov_shape_to_apr_route(\n",
" nov_trips: pd.DataFrame,\n",
" apr_route_df: pd.DataFrame,\n",
" operator_key: str = bbb_key,\n",
" one_shape: str = \"\"\n",
"):\n",
"\n",
" nov_route_name = nov_trips[\n",
" #(nov_trips.schedule.str.contains(operator_substring)) & \n",
" (nov_trips.shape_id==one_shape)\n",
" ].route_short_name.iloc[0]\n",
" \n",
" return apr_route_df[\n",
" #(apr_route_df.name.str.contains(operator_substring)) & \n",
" (apr_route_df.route_short_name==nov_route_name)\n",
" ].route_id.iloc[0]"
]
},
{
"cell_type": "markdown",
"id": "ccee2dd5-e335-4bd3-9d83-7ebeec4bc422",
"metadata": {},
"source": [
"## Trip"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e04594e-18ff-4283-aa9f-bd811d577882",
"metadata": {},
"outputs": [],
"source": [
"trip_df = pd.read_parquet(\n",
" f\"{SEGMENT_GCS}{TRIP_FILE}_{analysis_date}.parquet\",\n",
" filters = [[(\"schedule_gtfs_dataset_key\", \"==\", bbb_key)]]\n",
")\n",
"\n",
"trip_df = trip_df.assign(\n",
" speed_mph = trip_df.speed_mph.round(2)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2216fd7-92cf-4dce-82bb-125e2ecce384",
"metadata": {},
"outputs": [],
"source": [
"olympic_shape1 = \"26450\"\n",
"olympic_route1 = nov_shape_to_apr_route(\n",
" nov_trips,\n",
" operator_route_df,\n",
" bbb_key,\n",
" olympic_shape1\n",
")\n",
"\n",
"santa_monica_shape1 = \"26437\"\n",
"santa_monica_route1 = nov_shape_to_apr_route(\n",
" nov_trips,\n",
" operator_route_df,\n",
" bbb_key,\n",
" santa_monica_shape1\n",
")\n",
"\n",
"santa_monica_shape2 = \"26509\"\n",
"santa_monica_route2 = nov_shape_to_apr_route(\n",
" nov_trips,\n",
" operator_route_df,\n",
" bbb_key,\n",
" santa_monica_shape2\n",
")\n",
"\n",
"fourth_shape1 = \"26464\"\n",
"fourth_route1 = nov_shape_to_apr_route(\n",
" nov_trips,\n",
" operator_route_df,\n",
" bbb_key,\n",
" fourth_shape1\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a45b3e7d-ad9e-4f3c-8d43-2821ca2e9aed",
"metadata": {},
"outputs": [],
"source": [
"def filter_to_route(trip_df, operator_key, one_route, one_stop):\n",
" return trip_df[\n",
" (trip_df.schedule_gtfs_dataset_key==operator_key) & \n",
" (trip_df.route_id==one_route) & \n",
" (trip_df.stop_id==one_stop)\n",
" ][[\"stop_pair_name\", \"time_of_day\", \"arrival_time\", \"speed_mph\", \n",
" \"meters_elapsed\", \"sec_elapsed\"]].sort_values(\"arrival_time\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85641403-384d-409c-8de4-ea3d525b5c1c",
"metadata": {},
"outputs": [],
"source": [
"green_olympic_speeds = {\n",
" \"721\": \"Olympic & Prosser\",\n",
" \"688\": \"Olympic & Veteran\",\n",
" \"716\": \"Olympic & Colby\",\n",
" \"800\": \"Olympic & Purdue\",\n",
" \"801\": \"Olympic & Colby\",\n",
" \"700\": \"Olympic & 3030\"\n",
"}\n",
"\n",
"green_santa_monica_blvd_speeds = {\n",
" \"370\": \"Santa Monica & 14th\",\n",
" \"117\": \"Santa Monica & 14th, under\",\n",
" \"1234\": \"Santa Monica & 17th\"\n",
"}\n",
"\n",
"green_fourth_speeds = {\n",
" \"668\": \"4th & San Vincente\",\n",
" \"666\": \"4th & Marguerita\",\n",
" \"665\": \"4th & Alta\",\n",
" \"664\": \"4th & Montana\",\n",
" \"505\": \"4th & Washington\",\n",
" \"504\": \"4th & California\",\n",
" \"502\": \"4th & Washington\",\n",
" \"503\":\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c8ba1d6-388b-498c-bfa5-49ea1b8cb0cb",
"metadata": {},
"outputs": [],
"source": [
"# There are several exact speeds across trips, take a look at\n",
"# interpolated stop arrivals, what are the chances this happens?\n",
"# is it actually interpolating between different vp_idx values?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22d67801-c7c4-4726-be0c-9702a20c9c1c",
"metadata": {},
"outputs": [],
"source": [
"filter_to_route(trip_df, bbb_key, fourth_route1, \"502\").query('time_of_day==\"AM Peak\"')\n",
"#.groupby(\"time_of_day\").agg(\n",
"#{\"speed_mph\": lambda x: sorted(list(x))})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f6e6bff-bdfc-42b7-a549-6d3c62c3af20",
"metadata": {},
"outputs": [],
"source": [
"# Why are there the same speeds there?\n",
"trip_df[\n",
" (trip_df.schedule_gtfs_dataset_key==bbb_key) & \n",
" (trip_df.route_id==fourth_route1) & \n",
" (trip_df.stop_id==\"502\") & \n",
" (trip_df.time_of_day==\"AM Peak\") & \n",
" (trip_df.speed_mph > 15) & (trip_df.speed_mph < 16)\n",
"].trip_instance_key.unique()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93087f88-18f1-4517-a9f3-0f35a695ef6f",
"metadata": {},
"outputs": [],
"source": [
"INTERP_FILE = GTFS_DATA_DICT.speedmap_segments.stage3b\n",
"NEAREST_VP_FILE = GTFS_DATA_DICT.rt_stop_times.stage2\n",
"subset_trips = [\n",
" '0d448c743a91bc96271d36ba4450ebc9',\n",
" '1fbea8d720efd0dd513e98eef5383dbf',\n",
" '3a2e5c9e7304d091406cb5bbdfcc27e4',\n",
" 'a0f65344cb59c750934aff210b325f7e',\n",
" 'b6fc33a3b002b0bc63b07b6f39d80cb0'\n",
"]\n",
"\n",
"stop_arrivals = pd.read_parquet(\n",
" f\"{SEGMENT_GCS}{INTERP_FILE}_{analysis_date}.parquet\",\n",
" filters = [[(\"trip_instance_key\", \"in\", subset_trips), \n",
" (\"stop_id\", \"==\", \"502\")]]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f85adf9-bc36-411f-a890-1aa42b655d5d",
"metadata": {},
"outputs": [],
"source": [
"nearest = gpd.read_parquet(\n",
" f\"{SEGMENT_GCS}{NEAREST_VP_FILE}_{analysis_date}.parquet\",\n",
" filters = [[(\"trip_instance_key\", \"in\", subset_trips), \n",
" (\"stop_id\", \"==\", \"502\")]]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a231783c-0eb6-47d6-879a-6840ecfaaaff",
"metadata": {},
"outputs": [],
"source": [
"for i in nearest.index:\n",
" print(i)\n",
" print(nearest.loc[i][\"location_timestamp_local_trio\"])\n",
" #print(nearest.loc[i][\"vp_coords_trio\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5a3c27f-cff5-4368-99d3-00c556239b8d",
"metadata": {},
"outputs": [],
"source": [
"nearest.columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36341936-6747-4ce6-aee2-aa47b6d3c283",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading