Skip to content

Instantly share code, notes, and snippets.

@kuanb
Last active June 18, 2019 22:01
Show Gist options
  • Save kuanb/7ed57925888cea03ce028af971b8cd42 to your computer and use it in GitHub Desktop.
Save kuanb/7ed57925888cea03ce028af971b8cd42 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import io\n",
"import requests\n",
"import zipfile\n",
"\n",
"url = 'http://www.sdmts.com/google_transit_files/google_transit.zip'\n",
"target_path = 'sdmtsfeed.zip'\n",
"response = requests.get(url, stream=True)\n",
"handle = open(target_path, 'wb')\n",
"for chunk in response.iter_content(chunk_size=512):\n",
" if chunk: # filter out keep-alive new chunks\n",
" handle.write(chunk)\n",
"handle.close()"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [],
"source": [
"import partridge as ptg\n",
"feed = ptg.load_feed(target_path)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>intersection_code</th>\n",
" <th>stop_lat</th>\n",
" <th>wheelchair_boarding</th>\n",
" <th>zone_id</th>\n",
" <th>zip</th>\n",
" <th>stop_lon</th>\n",
" <th>stop_id</th>\n",
" <th>stop_url</th>\n",
" <th>stop_name_short</th>\n",
" <th>parent_station</th>\n",
" <th>stop_name</th>\n",
" <th>reference_place</th>\n",
" <th>location_type</th>\n",
" <th>stop_place</th>\n",
" <th>stop_code</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>N-W/B</td>\n",
" <td>32.576640</td>\n",
" <td>1</td>\n",
" <td>NaN</td>\n",
" <td>91932</td>\n",
" <td>-117.109169</td>\n",
" <td>60140</td>\n",
" <td>https://www.sdmts.com/schedules-real-time?frag...</td>\n",
" <td>Imperial Bch&amp;Florida</td>\n",
" <td>NaN</td>\n",
" <td>Imperial Beach Bl &amp; Florida St</td>\n",
" <td>NaN</td>\n",
" <td>0</td>\n",
" <td>NaN</td>\n",
" <td>60140</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>F-W/B</td>\n",
" <td>32.615403</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>91911</td>\n",
" <td>-117.063182</td>\n",
" <td>30180</td>\n",
" <td>https://www.sdmts.com/schedules-real-time?frag...</td>\n",
" <td>Naples&amp;Alpine</td>\n",
" <td>NaN</td>\n",
" <td>Naples St &amp; Alpine Av</td>\n",
" <td>NaN</td>\n",
" <td>0</td>\n",
" <td>NaN</td>\n",
" <td>30180</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>N-W/B</td>\n",
" <td>32.743484</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>92103</td>\n",
" <td>-117.181207</td>\n",
" <td>13356</td>\n",
" <td>https://www.sdmts.com/schedules-real-time?frag...</td>\n",
" <td>Wshngtn&amp;India</td>\n",
" <td>NaN</td>\n",
" <td>Washington St &amp; India St</td>\n",
" <td>NaN</td>\n",
" <td>0</td>\n",
" <td>NaN</td>\n",
" <td>13356</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>N-S/B</td>\n",
" <td>32.798164</td>\n",
" <td>1</td>\n",
" <td>NaN</td>\n",
" <td>92109</td>\n",
" <td>-117.240431</td>\n",
" <td>11543</td>\n",
" <td>https://www.sdmts.com/schedules-real-time?frag...</td>\n",
" <td>Ingraham&amp;Grand</td>\n",
" <td>NaN</td>\n",
" <td>Ingraham St &amp; Grand Av</td>\n",
" <td>NaN</td>\n",
" <td>0</td>\n",
" <td>inggra</td>\n",
" <td>11543</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>N-S/B</td>\n",
" <td>32.710596</td>\n",
" <td>0</td>\n",
" <td>NaN</td>\n",
" <td>92106</td>\n",
" <td>-117.238380</td>\n",
" <td>11546</td>\n",
" <td>https://www.sdmts.com/schedules-real-time?frag...</td>\n",
" <td>Rosecrans&amp;McCall</td>\n",
" <td>NaN</td>\n",
" <td>Rosecrans St &amp; McCall St</td>\n",
" <td>NaN</td>\n",
" <td>0</td>\n",
" <td>NaN</td>\n",
" <td>11546</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" intersection_code stop_lat wheelchair_boarding zone_id zip \\\n",
"0 N-W/B 32.576640 1 NaN 91932 \n",
"1 F-W/B 32.615403 2 NaN 91911 \n",
"2 N-W/B 32.743484 2 NaN 92103 \n",
"3 N-S/B 32.798164 1 NaN 92109 \n",
"4 N-S/B 32.710596 0 NaN 92106 \n",
"\n",
" stop_lon stop_id stop_url \\\n",
"0 -117.109169 60140 https://www.sdmts.com/schedules-real-time?frag... \n",
"1 -117.063182 30180 https://www.sdmts.com/schedules-real-time?frag... \n",
"2 -117.181207 13356 https://www.sdmts.com/schedules-real-time?frag... \n",
"3 -117.240431 11543 https://www.sdmts.com/schedules-real-time?frag... \n",
"4 -117.238380 11546 https://www.sdmts.com/schedules-real-time?frag... \n",
"\n",
" stop_name_short parent_station stop_name \\\n",
"0 Imperial Bch&Florida NaN Imperial Beach Bl & Florida St \n",
"1 Naples&Alpine NaN Naples St & Alpine Av \n",
"2 Wshngtn&India NaN Washington St & India St \n",
"3 Ingraham&Grand NaN Ingraham St & Grand Av \n",
"4 Rosecrans&McCall NaN Rosecrans St & McCall St \n",
"\n",
" reference_place location_type stop_place stop_code \n",
"0 NaN 0 NaN 60140 \n",
"1 NaN 0 NaN 30180 \n",
"2 NaN 0 NaN 13356 \n",
"3 NaN 0 inggra 11543 \n",
"4 NaN 0 NaN 11546 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"feed.stops.head()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['530', '520', '510']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mask = feed.routes['route_type'] == 0\n",
"target_route_ids = feed.routes[mask].route_id.tolist()\n",
"target_route_ids"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>service_id</th>\n",
" <th>start_date</th>\n",
" <th>end_date</th>\n",
" <th>monday</th>\n",
" <th>tuesday</th>\n",
" <th>wednesday</th>\n",
" <th>thursday</th>\n",
" <th>friday</th>\n",
" <th>saturday</th>\n",
" <th>sunday</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>11119</td>\n",
" <td>2019-06-09</td>\n",
" <td>2019-08-31</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>11118</td>\n",
" <td>2019-01-27</td>\n",
" <td>2019-06-08</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6849625-0000010-25</td>\n",
" <td>2019-02-18</td>\n",
" <td>2019-02-18</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" service_id start_date end_date monday tuesday wednesday \\\n",
"0 11119 2019-06-09 2019-08-31 1 1 1 \n",
"1 11118 2019-01-27 2019-06-08 1 1 1 \n",
"2 6849625-0000010-25 2019-02-18 2019-02-18 0 0 0 \n",
"\n",
" thursday friday saturday sunday \n",
"0 1 1 1 1 \n",
"1 1 1 1 1 \n",
"2 0 0 1 0 "
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"feed.calendar"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['68481-1111100-0',\n",
" '6847729-1111100-29',\n",
" '68477-1111100-0',\n",
" '68310-1111100-0',\n",
" '6852715-1111100-15',\n",
" '62114',\n",
" '68518-1111100-0',\n",
" '6848227-1111100-27',\n",
" '6852721-1111100-21',\n",
" '6847719-1111100-19',\n",
" '68480-1111100-0',\n",
" '68527-1111100-0',\n",
" '68482-1111100-0',\n",
" '68313-1111100-0',\n",
" '962114']"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"mask = feed.calendar_dates['date'] == datetime.date(2019, 5, 15)\n",
"target_service_ids = feed.calendar_dates[mask]['service_id'].unique().tolist()\n",
"target_service_ids"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"511\n"
]
}
],
"source": [
"mask_1 = feed.trips['route_id'].isin(target_route_ids)\n",
"mask_2 = feed.trips['service_id'].isin(target_service_ids)\n",
"target_trip_ids = feed.trips[mask_1 & mask_2]['trip_id'].unique()\n",
"print(len(target_trip_ids))"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trip_id</th>\n",
" <th>arrival_time</th>\n",
" <th>departure_time</th>\n",
" <th>stop_id</th>\n",
" <th>stop_sequence</th>\n",
" <th>stop_headsign</th>\n",
" <th>pickup_type</th>\n",
" <th>drop_off_type</th>\n",
" <th>shape_dist_traveled</th>\n",
" <th>timepoint</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>3973</th>\n",
" <td>13669959</td>\n",
" <td>50580.0</td>\n",
" <td>50580.0</td>\n",
" <td>75020</td>\n",
" <td>1</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3974</th>\n",
" <td>13669959</td>\n",
" <td>50820.0</td>\n",
" <td>50820.0</td>\n",
" <td>75023</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.082</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3975</th>\n",
" <td>13669959</td>\n",
" <td>51060.0</td>\n",
" <td>51060.0</td>\n",
" <td>75025</td>\n",
" <td>3</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>2.851</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3976</th>\n",
" <td>13669959</td>\n",
" <td>51240.0</td>\n",
" <td>51240.0</td>\n",
" <td>75027</td>\n",
" <td>4</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>3.739</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3977</th>\n",
" <td>13669959</td>\n",
" <td>51480.0</td>\n",
" <td>51480.0</td>\n",
" <td>75028</td>\n",
" <td>5</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>5.965</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" trip_id arrival_time departure_time stop_id stop_sequence \\\n",
"3973 13669959 50580.0 50580.0 75020 1 \n",
"3974 13669959 50820.0 50820.0 75023 2 \n",
"3975 13669959 51060.0 51060.0 75025 3 \n",
"3976 13669959 51240.0 51240.0 75027 4 \n",
"3977 13669959 51480.0 51480.0 75028 5 \n",
"\n",
" stop_headsign pickup_type drop_off_type shape_dist_traveled timepoint \n",
"3973 NaN NaN NaN NaN NaN \n",
"3974 NaN NaN NaN 1.082 NaN \n",
"3975 NaN NaN NaN 2.851 NaN \n",
"3976 NaN NaN NaN 3.739 NaN \n",
"3977 NaN NaN NaN 5.965 NaN "
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mask = feed.stop_times['trip_id'].isin(target_trip_ids)\n",
"feed.stop_times[mask].head()"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trip_id</th>\n",
" <th>arrival_time</th>\n",
" <th>departure_time</th>\n",
" <th>stop_id</th>\n",
" <th>stop_sequence</th>\n",
" <th>stop_headsign</th>\n",
" <th>pickup_type</th>\n",
" <th>drop_off_type</th>\n",
" <th>shape_dist_traveled</th>\n",
" <th>timepoint</th>\n",
" <th>time_to_measure</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>3973</th>\n",
" <td>13669959</td>\n",
" <td>50580.0</td>\n",
" <td>50580.0</td>\n",
" <td>75020</td>\n",
" <td>1</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3974</th>\n",
" <td>13669959</td>\n",
" <td>50820.0</td>\n",
" <td>50820.0</td>\n",
" <td>75023</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.082</td>\n",
" <td>NaN</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3975</th>\n",
" <td>13669959</td>\n",
" <td>51060.0</td>\n",
" <td>51060.0</td>\n",
" <td>75025</td>\n",
" <td>3</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>2.851</td>\n",
" <td>NaN</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3976</th>\n",
" <td>13669959</td>\n",
" <td>51240.0</td>\n",
" <td>51240.0</td>\n",
" <td>75027</td>\n",
" <td>4</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>3.739</td>\n",
" <td>NaN</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3977</th>\n",
" <td>13669959</td>\n",
" <td>51480.0</td>\n",
" <td>51480.0</td>\n",
" <td>75028</td>\n",
" <td>5</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>5.965</td>\n",
" <td>NaN</td>\n",
" <td>14</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" trip_id arrival_time departure_time stop_id stop_sequence \\\n",
"3973 13669959 50580.0 50580.0 75020 1 \n",
"3974 13669959 50820.0 50820.0 75023 2 \n",
"3975 13669959 51060.0 51060.0 75025 3 \n",
"3976 13669959 51240.0 51240.0 75027 4 \n",
"3977 13669959 51480.0 51480.0 75028 5 \n",
"\n",
" stop_headsign pickup_type drop_off_type shape_dist_traveled timepoint \\\n",
"3973 NaN NaN NaN NaN NaN \n",
"3974 NaN NaN NaN 1.082 NaN \n",
"3975 NaN NaN NaN 2.851 NaN \n",
"3976 NaN NaN NaN 3.739 NaN \n",
"3977 NaN NaN NaN 5.965 NaN \n",
"\n",
" time_to_measure \n",
"3973 14 \n",
"3974 14 \n",
"3975 14 \n",
"3976 14 \n",
"3977 14 "
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"st_sub = feed.stop_times[mask].copy()\n",
"a = st_sub['arrival_time'] / 3600\n",
"a = np.floor(a).astype(int)\n",
"a[a > 24] = a - 24\n",
"st_sub['time_to_measure'] = a\n",
"\n",
"st_sub.head()"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>count</th>\n",
" </tr>\n",
" <tr>\n",
" <th>stop_id</th>\n",
" <th>time_to_measure</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"20\" valign=\"top\">75000</th>\n",
" <th>4</th>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" count\n",
"stop_id time_to_measure \n",
"75000 4 7\n",
" 5 16\n",
" 6 16\n",
" 7 16\n",
" 8 11\n",
" 9 8\n",
" 10 8\n",
" 11 8\n",
" 12 8\n",
" 13 8\n",
" 14 8\n",
" 15 15\n",
" 16 16\n",
" 17 16\n",
" 18 11\n",
" 19 8\n",
" 20 8\n",
" 21 4\n",
" 22 4\n",
" 23 4"
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def process_smallest(sub):\n",
" return len(sub)\n",
"\n",
"g1 = st_sub.groupby(['stop_id', 'time_to_measure'])\n",
"processed = g1.apply(process_smallest).to_frame()\n",
"processed.columns = ['count']\n",
"processed.head(20)"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"stop_id_ix = processed.index.get_level_values('stop_id')\n",
"time_ix = processed.index.get_level_values('time_to_measure')\n",
"\n",
"res_dict = {}\n",
"for sii, tii in zip(stop_id_ix, time_ix):\n",
" c = processed.loc[(sii, tii), 'count']\n",
" \n",
" sii = str(sii)\n",
" tii = str(tii)\n",
" if sii not in res_dict:\n",
" res_dict[sii] = {}\n",
" res_dict[sii]['counts'] = {}\n",
" res_dict[sii]['counts'][tii] = int(c)"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"for sid in set(stop_id_ix):\n",
" tempsub = feed.stops[feed.stops['stop_id'] == sid]\n",
" assert len(tempsub) == 1\n",
" tss = tempsub.squeeze()\n",
" lat = tss.stop_lat\n",
" lon = tss.stop_lon\n",
" \n",
" res_dict[str(sid)]['props'] = {\n",
" 'lat': lat,\n",
" 'lon': lon}"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [],
"source": [
"fc = {\n",
" \"type\" : \"FeatureCollection\",\n",
" \"features\" : []\n",
"}\n",
"\n",
"for sid in set(stop_id_ix):\n",
" p = res_dict[str(sid)]['props']\n",
" \n",
" props = {}\n",
" \n",
" cs = res_dict[sii]['counts']\n",
" for i in range(0, 25):\n",
" if str(i) in cs:\n",
" props[f'Arrivals at hour: {i}'] = cs[str(i)]\n",
"\n",
" feat = { \n",
" \"type\" : \"Feature\", \n",
" \"properties\" : props, \n",
" \"geometry\" : { \n",
" \"type\" : \"Point\", \n",
" \"coordinates\" : [ p['lon'], p['lat'] ] \n",
" }\n",
" }\n",
" \n",
" fc['features'].append(feat)\n",
"\n",
"\n",
"import json\n",
"with open('sdmts_by_hour.json', 'w') as outfile:\n",
" json.dump(fc, outfile)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment