Skip to content

Commit

Permalink
Create gtfs_kit.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
PatGendre authored Dec 10, 2024
1 parent 38bbaa2 commit e5a8109
Showing 1 changed file with 248 additions and 0 deletions.
248 changes: 248 additions & 0 deletions content/gtfs_kit.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from pathlib import Path\n",
"import json\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import geopandas as gp\n",
"import matplotlib\n",
"\n",
"DIR = Path('..')\n",
"sys.path.append(str(DIR))\n",
"\n",
"import gtfs_kit as gk\n",
"\n",
"DATA_DIR = DIR/'data/'\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# List feed\n",
"\n",
"path = DATA_DIR/'cairns_gtfs.zip'\n",
"gk.list_feed(path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read feed and describe\n",
"\n",
"path = DATA_DIR/'cairns_gtfs.zip'\n",
"feed = gk.read_feed(path, dist_units='km')\n",
"feed.describe()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Validate\n",
"\n",
"feed.validate()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Append shape_dist_traveled column to stop times\n",
"\n",
"display(feed.stop_times.head().T)\n",
"\n",
"feed = feed.append_dist_to_stop_times()\n",
"feed.stop_times.head().T"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Choose study dates\n",
"\n",
"week = feed.get_first_week()\n",
"dates = [week[4], week[6]] # First Friday and Sunday\n",
"dates"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compute feed time series\n",
"\n",
"trip_stats = feed.compute_trip_stats()\n",
"trip_stats.head().T\n",
"fts = feed.compute_feed_time_series(trip_stats, dates, freq='6h')\n",
"fts"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gk.downsample(fts, freq='12h')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compute feed stats for first week\n",
"\n",
"feed_stats = feed.compute_feed_stats(trip_stats, week)\n",
"feed_stats"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compute route time series\n",
"\n",
"rts = feed.compute_route_time_series(trip_stats, dates, freq='12h')\n",
"rts"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Slice time series\n",
"\n",
"inds = ['service_distance', 'service_duration', 'service_speed']\n",
"rids = ['110-423', '110N-423']\n",
"\n",
"rts.loc[:, (inds, rids)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Slice again by cross-section\n",
"\n",
"rts.xs(rids[0], axis=\"columns\", level=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compute trip locations for every hour\n",
"\n",
"rng = pd.date_range('1/1/2000', periods=24, freq='h')\n",
"times = [t.strftime('%H:%M:%S') for t in rng]\n",
"loc = feed.locate_trips(dates[0], times)\n",
"loc.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Build a route timetable\n",
"\n",
"route_id = feed.routes['route_id'].iat[0]\n",
"feed.build_route_timetable(route_id, dates).T"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compute screen line counts\n",
"\n",
"path = DATA_DIR/'cairns_screen_lines.geojson'\n",
"lines = gp.read_file(path)\n",
"\n",
"display(lines)\n",
"\n",
"feed.compute_screen_line_counts(lines, dates)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Map routes\n",
"\n",
"rsns = feed.routes[\"route_short_name\"].iloc[2:4]\n",
"feed.map_routes(route_short_names=rsns, show_stops=True)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit e5a8109

Please sign in to comment.