Skip to content

Commit

Permalink
expand notebook to look at estimated arrival times
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanychu90 committed Oct 24, 2023
1 parent ef11dd9 commit 27f354d
Showing 1 changed file with 55 additions and 38 deletions.
93 changes: 55 additions & 38 deletions rt_segment_speeds/24_plot_interpolation_results.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "cd8110e6-c29e-4e00-af33-367c1f9d9e63",
"metadata": {},
"source": [
"# Maps to check that `shapely_interpolate` script works as expected"
"## Plot `nearest_vp_to_stop` and `interpolate_stop_arrival` results"
]
},
{
Expand All @@ -15,6 +15,7 @@
"metadata": {},
"outputs": [],
"source": [
"import dask.dataframe as dd\n",
"import folium\n",
"import geopandas as gpd\n",
"import numpy as np\n",
Expand Down Expand Up @@ -51,17 +52,37 @@
{
"cell_type": "code",
"execution_count": null,
"id": "92ff2637-3a4c-4fc8-bb20-b323335867b7",
"id": "c3b8aab4-ac9b-4659-b605-fe6ab68f88c6",
"metadata": {},
"outputs": [],
"source": [
"#test_trips = df[[\"trip_instance_key\"]].sample(10).trip_instance_key.tolist()\n",
"interp_results = pd.read_parquet(\n",
" f\"{SEGMENT_GCS}projection/nearest_vp_normal_{analysis_date}.parquet\",\n",
" filters = [[(\"trip_instance_key\", \"in\", test_trips)]]\n",
") \n",
"\n",
"test_shapes = interp_results.shape_array_key.unique().tolist()"
"stop_arrivals_interp = pd.read_parquet(\n",
" f\"{SEGMENT_GCS}stop_arrivals_{analysis_date}.parquet\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f4251c9-d112-440a-be6b-a26f509e3462",
"metadata": {},
"outputs": [],
"source": [
"stop_arrivals_interp = segment_calcs.convert_timestamp_to_seconds(\n",
" stop_arrivals_interp, \n",
" [\"arrival_time\"]\n",
").drop(columns = [\n",
" \"nearest_location_timestamp_local\", \n",
" \"subseq_location_timestamp_local\", \n",
" \"arrival_time\"\n",
"])"
]
},
{
"cell_type": "markdown",
"id": "2449e28e-215d-4559-88a9-a25e56d0c325",
"metadata": {},
"source": [
"### Merge in interpolated stop arrivals with vp and stop geometry"
]
},
{
Expand All @@ -74,16 +95,9 @@
"vp = pd.read_parquet(\n",
" f\"{SEGMENT_GCS}vp_usable_{analysis_date}/\",\n",
" filters = [[(\"trip_instance_key\", \"in\", test_trips)]],\n",
" columns = [\"vp_idx\", \"x\", \"y\", \n",
" \"vp_dir_xnorm\", \"vp_dir_ynorm\", \n",
" \"vp_primary_direction\", \n",
" \"location_timestamp_local\"\n",
" ]\n",
")\n",
"\n",
"vp = segment_calcs.convert_timestamp_to_seconds(\n",
" vp, [\"location_timestamp_local\"]\n",
").drop(columns = \"location_timestamp_local\")\n",
" columns = [\"vp_idx\", \"x\", \"y\", \"location_timestamp_local\"]\n",
").pipe(segment_calcs.convert_timestamp_to_seconds, \n",
" [\"location_timestamp_local\"]).drop(columns = \"location_timestamp_local\")\n",
"\n",
"vp_gdf = gpd.GeoDataFrame(\n",
" vp,\n",
Expand All @@ -101,11 +115,9 @@
"source": [
"stops_projected = gpd.read_parquet(\n",
" f\"{SEGMENT_GCS}stops_projected_{analysis_date}.parquet\",\n",
" filters = [[(\"shape_array_key\", \"in\", test_shapes)]],\n",
").rename(\n",
" columns = {\"shape_meters\": \"stop_meters\"}\n",
").drop(columns = [\n",
" \"schedule_gtfs_dataset_key\", \"st_trip_instance_key\"]\n",
" columns = [\"shape_array_key\", \"stop_sequence\", \"stop_id\", \n",
" \"stop_geometry\", \n",
" \"loop_or_inlining\"]\n",
")"
]
},
Expand All @@ -119,8 +131,8 @@
"# Merge selected vp with stops_projected\n",
"gdf = pd.merge(\n",
" stops_projected,\n",
" interp_results,\n",
" on = [\"shape_array_key\", \"stop_sequence\"],\n",
" stop_arrivals_interp,\n",
" on = [\"shape_array_key\", \"stop_sequence\", \"stop_id\"],\n",
" how = \"inner\"\n",
")"
]
Expand All @@ -136,8 +148,6 @@
" gdf,\n",
" vp_gdf.rename(columns = {\n",
" \"vp_idx\": \"nearest_vp_idx\",\n",
" \"vp_dir_xnorm\": \"nearest_vp_dir_xnorm\",\n",
" \"vp_dir_ynorm\": \"nearest_vp_dir_ynorm\",\n",
" \"location_timestamp_local_sec\": \"nearest_sec\",\n",
" \"geometry\": \"nearest_vp_geometry\"\n",
" }),\n",
Expand All @@ -157,8 +167,6 @@
" gdf2,\n",
" vp_gdf.rename(columns = {\n",
" \"vp_idx\": \"subseq_vp_idx\",\n",
" \"vp_dir_xnorm\": \"subseq_vp_dir_xnorm\",\n",
" \"vp_dir_ynorm\": \"subseq_vp_dir_ynorm\",\n",
" \"location_timestamp_local_sec\": \"subseq_sec\",\n",
" \"geometry\": \"subseq_vp_geometry\"\n",
" }),\n",
Expand Down Expand Up @@ -243,9 +251,21 @@
"metadata": {},
"outputs": [],
"source": [
"gdf3[(gdf3.trip_instance_key==test_trips[0]) & \n",
" (gdf3.stop_sequence==68)\n",
" ][[\"nearest_vp_idx\", \"subseq_vp_idx\"]]"
"def look_at_arrival_time(df, one_trip):\n",
" subset_df = df[(df.trip_instance_key==one_trip)\n",
" ].sort_values(\"stop_sequence\").reset_index()\n",
" \n",
" cols = [\"stop_sequence\", \n",
" \"nearest_vp\", \"arrival_time\", \"subseq_vp\"]\n",
" \n",
" subset_df = subset_df.assign(\n",
" nearest_vp = pd.to_datetime(subset_df.nearest_sec, unit=\"s\").dt.time,\n",
" arrival_time = pd.to_datetime(subset_df.arrival_time_sec, unit=\"s\").dt.time,\n",
" subseq_vp = pd.to_datetime(subset_df.subseq_sec, unit=\"s\").dt.time,\n",
" )\n",
" \n",
" display(subset_df[cols])\n",
" "
]
},
{
Expand All @@ -255,11 +275,8 @@
"metadata": {},
"outputs": [],
"source": [
"vp_gdf[vp_gdf.vp_idx.isin([10515692, 10515693])].explore(\n",
" \"vp_idx\",\n",
" categorical = True,\n",
" tiles = \"CartoDB Positron\",\n",
")"
"for t in test_trips:\n",
" look_at_arrival_time(gdf3, t)"
]
},
{
Expand Down

0 comments on commit 27f354d

Please sign in to comment.