-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1307 from cal-itp/hermosa-export
Hermosa export
- Loading branch information
Showing
8 changed files
with
366 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,301 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 24, | ||
"id": "46898b5c-b5cc-4096-ab68-8c3e42fab870", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%capture\n", | ||
"import warnings\n", | ||
"warnings.filterwarnings('ignore')\n", | ||
"import calitp_data_analysis.magics\n", | ||
"# from update_vars_index import ANALYSIS_DATE\n", | ||
"\n", | ||
"import speedmap_utils\n", | ||
"import pandas as pd\n", | ||
"import geopandas as gpd\n", | ||
"import numpy as np\n", | ||
"from siuba import *\n", | ||
"import shared_utils\n", | ||
"catalog = shared_utils.catalog_utils.get_catalog('gtfs_analytics_data')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5f100bb7-182c-4d78-a8f8-1b2eabf70650", | ||
"metadata": {}, | ||
"source": [ | ||
"## https://github.com/cal-itp/data-analyses/issues/1306" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "9f15ed6a-5ab7-4f57-9695-3f762781b74c", | ||
"metadata": { | ||
"tags": [ | ||
"parameters" | ||
] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"## parameters cell\n", | ||
"organization_source_record_ids = ['rec4pgjrmdhCh4z01', 'rec8zhnCPETu6qEiH', 'recPnGkwdpnr8jmHB',\n", | ||
" 'recvzE9NXgGMmqcTH']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "7c53d945-7295-4d86-9110-b6f4f52f9975", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dates = ['jan2024', 'feb2024', 'mar2024', 'apr2024',\n", | ||
" 'may2024', 'jun2024', 'jul2024', 'aug2024',\n", | ||
" 'sep2024', 'oct2024', 'nov2024']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "5843d0b3-a500-4c2e-8a65-bfe2935ce089", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dates = [shared_utils.rt_dates.DATES[date] for date in dates]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 25, | ||
"id": "1989aba3-8cf6-48c4-aceb-2399eea80de2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def read_segments(organization_source_record_ids: list, analysis_date) -> gpd.GeoDataFrame:\n", | ||
" '''\n", | ||
" Get filtered detailed speedmap segments for an organization, and relevant district SHN.\n", | ||
" '''\n", | ||
" path = f'{catalog.speedmap_segments.dir}{catalog.speedmap_segments.shape_stop_single_segment_detail}_{analysis_date}.parquet'\n", | ||
" # path = f'{catalog.stop_segments.dir}{catalog.stop_segments.route_dir_single_segment_detail}_{update_vars_index.ANALYSIS_DATE}.parquet'\n", | ||
" speedmap_segs = gpd.read_parquet(path, filters=[['organization_source_record_id', 'in', organization_source_record_ids],\n", | ||
" ['route_short_name', 'in', ['232', '109', '438', '13']]]) # aggregated\n", | ||
" assert (speedmap_segs >> select(-_.route_short_name)).isna().any().any() == False, 'no cols besides route_short_name should be nan'\n", | ||
" speedmap_segs['date'] = analysis_date\n", | ||
" \n", | ||
" # TODO move upstream and investigate\n", | ||
" speedmap_segs['fast_slow_ratio'] = speedmap_segs.p80_mph / speedmap_segs.p20_mph\n", | ||
" speedmap_segs.fast_slow_ratio = speedmap_segs.fast_slow_ratio.replace(np.inf, 3)\n", | ||
" speedmap_segs = speedmap_segs.round(1)\n", | ||
" # speedmap_segs = prepare_segment_gdf(speedmap_segs)\n", | ||
" # shn = gpd.read_parquet(rt_utils.SHN_PATH)\n", | ||
" # this_shn = shn >> filter(_.District.isin([int(x[:2]) for x in speedmap_segs.caltrans_district.unique()]))\n", | ||
" \n", | ||
" return speedmap_segs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 26, | ||
"id": "c3333da1-b90c-4ed3-8655-cd668ef33ed4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gdf = read_segments(organization_source_record_ids, dates[0])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 27, | ||
"id": "420b81e4-80db-4385-9961-58007bbdb5b5", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-01-17\n", | ||
"2024-02-14\n", | ||
"2024-03-13\n", | ||
"2024-04-17\n", | ||
"2024-05-22\n", | ||
"2024-06-12\n", | ||
"2024-07-17\n", | ||
"2024-08-14\n", | ||
"2024-09-18\n", | ||
"2024-10-16\n", | ||
"2024-11-13\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"lines = gpd.GeoDataFrame()\n", | ||
"for date in dates:\n", | ||
" print(date)\n", | ||
" lines = pd.concat([read_segments(organization_source_record_ids, date), lines])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 29, | ||
"id": "359dc68a-903b-455f-b419-920fa506ebed", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def read_process_segments(organization_source_record_ids: list, analysis_date) -> gpd.GeoDataFrame:\n", | ||
" '''\n", | ||
" Get filtered detailed speedmap segments for an organization, and relevant district SHN.\n", | ||
" '''\n", | ||
" path = f'{catalog.speedmap_segments.dir}{catalog.speedmap_segments.shape_stop_single_segment_detail}_{analysis_date}.parquet'\n", | ||
" # path = f'{catalog.stop_segments.dir}{catalog.stop_segments.route_dir_single_segment_detail}_{update_vars_index.ANALYSIS_DATE}.parquet'\n", | ||
" speedmap_segs = gpd.read_parquet(path, filters=[['organization_source_record_id', 'in', organization_source_record_ids],\n", | ||
" ['route_short_name', 'in', ['232', '109', '438', '13']]]) # aggregated\n", | ||
" assert (speedmap_segs >> select(-_.route_short_name)).isna().any().any() == False, 'no cols besides route_short_name should be nan'\n", | ||
" speedmap_segs['date'] = analysis_date\n", | ||
" speedmap_segs = speedmap_utils.prepare_segment_gdf(speedmap_segs)\n", | ||
" # shn = gpd.read_parquet(rt_utils.SHN_PATH)\n", | ||
" # this_shn = shn >> filter(_.District.isin([int(x[:2]) for x in speedmap_segs.caltrans_district.unique()]))\n", | ||
" \n", | ||
" return speedmap_segs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 30, | ||
"id": "bc4bc624-f5d1-42e0-89a1-858671753de6", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-01-17\n", | ||
"2024-02-14\n", | ||
"2024-03-13\n", | ||
"2024-04-17\n", | ||
"2024-05-22\n", | ||
"2024-06-12\n", | ||
"2024-07-17\n", | ||
"2024-08-14\n", | ||
"2024-09-18\n", | ||
"2024-10-16\n", | ||
"2024-11-13\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"polygons = gpd.GeoDataFrame()\n", | ||
"for date in dates:\n", | ||
" print(date)\n", | ||
" polygons = pd.concat([read_process_segments(organization_source_record_ids, date), polygons])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 32, | ||
"id": "b5786ff5-e33e-4bbd-acc2-b8459f4e1f13", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"polygons.to_file('hermosa_speedmap_polygons.geojson')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 33, | ||
"id": "fbd4f2b9-257a-4c60-b9d0-fa51f8fb2982", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"lines.to_file('hermosa_speedmap_lines.geojson')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d26b1dbf-15db-4d28-bac9-f3f3885d717a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 37, | ||
"id": "821bfbee-f2d4-4e06-aec0-aa877d27db4f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def write_gz(gdf, path):\n", | ||
" geojson_str = gdf.to_json()\n", | ||
" geojson_bytes = geojson_str.encode(\"utf-8\")\n", | ||
" # if verbose:\n", | ||
" # print(f\"writing to {path}\")\n", | ||
" with open(path, \"wb\") as writer:\n", | ||
" with gzip.GzipFile(fileobj=writer, mode=\"w\") as gz:\n", | ||
" gz.write(geojson_bytes)\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 38, | ||
"id": "7733fb48-5403-43b7-9ba2-a86992d1f79a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import gzip" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 39, | ||
"id": "4f1776af-65b8-413d-8d02-5aa66f0671b0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"write_gz(polygons, 'hermosa_speedmap_polygons.geojson.gz')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 40, | ||
"id": "88f8b256-4bce-4cc4-b878-d1fe1c512100", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"write_gz(lines, 'hermosa_speedmap_lines.geojson.gz')" | ||
] | ||
} | ||
], | ||
"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" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.