-
Notifications
You must be signed in to change notification settings - Fork 0
/
commuters.py
352 lines (277 loc) · 14 KB
/
commuters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# _________________________
#
# ----- START
# Import libraries
import streamlit as st
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import re
import pandas as pd
import numpy as np
#from IPython.display import HTML
#import matplotlib as mpl
import matplotlib.pyplot as plt
from plotly import graph_objs as go
import seaborn as sns
# Setup ssl
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
def main():
"""Interactive EDA: Commuters in the US"""
# _________________________
#
# ----- FUNCTIONS
# LOAD DATA
# @st.cache
def load(url):
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
return soup('body')[0]
# PARSE HTML
def parse(name, rawdata, br):
# Create empty dataframes
df = pd.DataFrame(columns=['city', 'state_name', name + ' percentage'])
# Check if relevant lines contain line break
if br:
lines = re.findall(r"(?<=href).*?(?=<br\/>)", rawdata.decode())
else:
lines = re.findall(r"(?<=href).*?\%", rawdata.decode())
# Find data in lines
for line in lines:
city = re.search(r"(?<=title=\").*?(?=(\,|\">))", line)
state = re.search(r"(?<=, )(\.| |[a-zA-Z0-9])*?(?=</a>)", line)
percentage = re.search(r"([0-9]|\.)*?(?=%)", line)
# Add data to dataframe
df = df.append({'city': city.group(0), 'state_name': state.group(0), name + ' percentage': float(percentage.group(0))}, ignore_index=True)
return df
# ADD TRACE TO CHOROPLETH MAP
def choro_trace(percentage, legend_name, selected_color):
fig.add_trace(go.Scattergeo(
lon = commuters_df.loc[commuters_df[percentage] != 0]['lng'],
lat = commuters_df.loc[commuters_df[percentage] != 0]['lat'],
name = legend_name,
text = commuters_df.loc[commuters_df[percentage] != 0]['city']+
': '+commuters_df.loc[commuters_df[percentage] != 0][percentage].astype(str) + '%',
showlegend = True,
marker = dict(
size=commuters_df.loc[commuters_df[percentage] != 0][percentage]*2,
color=colors[selected_color],
line_width=0)
))
# _________________________
#
# ----- LAYOUT
colors = ['#70ABBA', '#B5DB95', '#FFD882', '#393E46', '#ED7771', '#B8AFC9']
# _________________________
#
# ----- INTRODUCTION
st.title("Non-Motorized Commuters in the US")
# st.subheader("Use the following interactive visualizations to explore the non-motorized commuter culture in US cities.")
st.markdown("To improve performance, all data is pre-loaded and formatted by default.")
real_time_data = False
if st.checkbox("Load data in real time", value=False, key=None):
real_time_data = True
st.subheader("Data Visualization")
st.markdown("Use the following dropdown menu to display cities with high percentages of certain commuter types. \
If nothing is selected, the total number of non-motorized commuters is displayed.")
# _________________________
#
# ----- LOAD DATA
if real_time_data:
# Source: Wikipedia
bicycle_url = ('https://en.wikipedia.org/wiki/List_of_U.S._cities_with_most_bicycle_commuters')
pedestrian_url = ('https://en.wikipedia.org/wiki/List_of_U.S._cities_with_most_pedestrian_commuters')
public_transit_url = ('https://en.wikipedia.org/wiki/List_of_U.S._cities_with_high_transit_ridership')
# Load data from Wikipedia
bicycles_raw = load(bicycle_url)
pedestrians_raw = load(pedestrian_url)
public_transit_raw = load(public_transit_url)
# Load data from simpledata
# us_data = pd.read_csv('../us-commuters-data/simplemaps_uscities_basicv1/uscities.csv')
us_data = pd.read_csv('https://raw.githubusercontent.com/bauhofer/data/master/uscities.csv')
#Load US universities and colleges data
# Local
# us_colleges = pd.read_csv('../us-commuters-data/us-colleges-and-universities.csv', sep=';')
# Global
# us_colleges = pd.read_csv('https://raw.githubusercontent.com/bauhofer/data/master/us-colleges-and-universities.csv', sep=';')
# _________________________
#
# ----- DATA CLEANING
# Parse data
bicycles_df = parse('bicycle', bicycles_raw, br=True)
pedestrians_df = parse('pedestrian', pedestrians_raw, br=True)
public_transit_df = parse('public_transit', public_transit_raw, br=False)
# Merge dataframes
commuters_df = pd.merge(bicycles_df,pedestrians_df,how='outer',on='city').fillna(np.nan)
commuters_df['state_name_x'] = commuters_df['state_name_x'].where(pd.notnull, commuters_df['state_name_y'])
commuters_df = commuters_df.drop(['state_name_y'], axis=1).rename(columns = {"state_name_x": "state_name"})
commuters_df = pd.merge(commuters_df,public_transit_df,how='outer',on='city').fillna(np.nan)
commuters_df['state_name_x'] = commuters_df['state_name_x'].where(pd.notnull, commuters_df['state_name_y'])
commuters_df = commuters_df.drop(['state_name_y'], axis=1).rename(columns = {"state_name_x": "state_name"})
# Adjust city names to match format
commuters_df.replace(to_replace='New York City', value='New York', inplace=True)
commuters_df.replace(to_replace='D.C.', value='District of Columbia', inplace=True)
commuters_df.replace(to_replace='Arlington County', value='Arlington', inplace=True)
# Skip entry that is not in US Data
commuters_df = commuters_df.drop(commuters_df[commuters_df['city']=='Edison'].index)
# Merge data from Wikipedia with US Data
commuters_df = pd.merge(commuters_df,us_data[['city','state_name','lat','lng','population','density']],how='left',on=['city','state_name']).fillna(0)
commuters_df['total percentage'] = commuters_df['bicycle percentage'] + commuters_df['pedestrian percentage'] + commuters_df['public_transit percentage']
commuters_df['bike and pedestrian percentage'] = commuters_df['bicycle percentage'] + commuters_df['pedestrian percentage']
else:
# Local
# commuters_df = pd.read_csv('../us-commuters-data/commuters_clean.csv', sep=';')
# Global
commuters_df = pd.read_csv('https://raw.githubusercontent.com/bauhofer/data/master/commuters_clean.csv', sep=';')
# OPTIONAL: Save data to csv
# commuters_df.to_csv(path_or_buf='../us-commuters-data/commuters_clean.csv', sep=';')
# _________________________
#
# ----- CHOROPLETH MAP
# Presentation
options = st.multiselect(label='Commuters types:',
options=('Bicyclists', 'Pedestrians', 'Public Transport'),
default=['Public Transport'])
# Figure
fig = go.Figure(
layout=dict(
geo_scope='usa',
geo_landcolor="lightgray"
)
)
# Reference marker
fig.add_trace(go.Scattergeo(
lon = [-85],
lat = [51.2],
name = 'reference marker',
text = 'Reference marker: 5%',
mode = 'markers+text',
textposition = 'middle right',
showlegend = False,
marker = dict(
size=10,
color=colors[3],
line_width=0)
))
# Scatter Markers
if 'Public Transport' in options:
choro_trace(percentage='public_transit percentage',
legend_name='Public transportation commuters',
selected_color=0)
if 'Pedestrians' in options:
choro_trace(percentage='pedestrian percentage',
legend_name='Pedestrian commuters',
selected_color=1)
if 'Bicyclists' in options:
choro_trace(percentage='bicycle percentage',
legend_name='Bicycle commuters',
selected_color=2)
if not options:
choro_trace(percentage='total percentage',
legend_name='Total non-car commuters',
selected_color=4)
# Layout
fig.update_layout(
margin=dict(l=0, r=0, t=25, b=40),
)
st.plotly_chart(fig, height=290)
# _________________________
#
# ----- BAR PLOT
# Presentation
st.markdown("In the following bar plot, the cities are sorted with respect to the total number of non-car commuters.\
Adjust the number of compared cities with the following field.")
picks = st.number_input('Number of compared cities:',
min_value=0, max_value=commuters_df.shape[0], value=15)
# Sort data
commuters_df = commuters_df.sort_values(by=['total percentage'], ascending=False)
# Plot data
# picks = 15
pos = np.arange(picks)
plt.figure(figsize=(12.4,7))
bars1 = plt.bar(pos, commuters_df['public_transit percentage'][:picks], capsize=5, align='center'\
, color=colors[0], alpha=0.7, edgecolor='black', linewidth=0, label='public transit commuters')
bars2 = plt.bar(pos, commuters_df['pedestrian percentage'][:picks], capsize=5, align='center'\
, bottom=list(commuters_df['public_transit percentage'][:picks])\
, color=colors[1], alpha=0.7, edgecolor='black', linewidth=0, label='pedestrian commuters')
bars3 = plt.bar(pos, commuters_df['bicycle percentage'][:picks], capsize=5, align='center'\
, bottom=list(commuters_df['pedestrian percentage'][:picks] + commuters_df['public_transit percentage'][:picks])\
, color=colors[2], alpha=0.7, edgecolor='black', linewidth=0, label='bicycle commuters')
# Remove frame, labels and ticks
plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=False, labelbottom=True)
for spine in plt.gca().spines.values():
spine.set_visible(False)
# Label bars
for b1, b2, b3 in zip(bars1, bars2, bars3):
h1 = b1.get_height()
h2 = b2.get_height()
h3 = b3.get_height()
if float(h1) > 0:
plt.gca().text(b1.get_x() + b1.get_width()/2, h1 - 2.2\
, str(float(round(h1, 1))) + '%', ha='center', color=colors[3], fontsize=11)
if float(h2) > 0:
plt.gca().text(b1.get_x() + b1.get_width()/2, h1+h2 - 2.2\
, str(float(round(h2, 1))) + '%', ha='center', color=colors[3], fontsize=11)
if float(h3) > 0:
plt.gca().text(b3.get_x() + b3.get_width()/2, h1+h2+h3 - 2.2\
, str(float(round(h3, 1))) + '%', ha='center', color=colors[3], fontsize=11)
# Include Title, ticks and legend
plt.legend(fontsize=12)
plt.xticks(pos, commuters_df['city'][:picks], rotation=45, ha='right', fontsize=12)
plt.title('US cities with the highest total percentage of non-motorized commuters'\
, fontsize=15, fontweight='bold')
plt.tight_layout()
st.pyplot()
# _________________________
#
# ----- DATAFRAME
if st.checkbox("Show data in tabular form", value=False, key=None):
st.write(commuters_df.drop(columns=["Unnamed: 0","lat","lng","bike and pedestrian percentage"]).
rename(columns = {'city': 'City',
'state_name': 'State',
'bicycle percentage': 'Bicycles [%]',
'pedestrian percentage': 'Pedestrians [%]',
'public_transit percentage': 'Public Transport [%]',
'population': 'Population',
'density': 'Density',
'total percentage': 'Total [%]'}).sort_values(by=['Total [%]'], ascending=False).reset_index(drop=True))
# _________________________
#
# ----- CORRELATIONS
st.subheader("Correlations")
st.markdown("In order to find indicators for the presence of certain commuter types, \
correlations between different factors are considered.")
if st.checkbox("Show correlation of all factors", value=False, key=None):
plt.figure(figsize=(10,8), dpi= 80)
sns.pairplot(commuters_df[['pedestrian percentage', 'bicycle percentage',
'public_transit percentage', 'density',
'population']], kind="scatter",
plot_kws=dict(s=80, edgecolor="black", linewidth=1))
st.pyplot()
st.markdown("From the given factors, only the population density gives a slight indication for the percentage of people commuting via public transport.")
# # Plot
sns.set(style="white", font_scale=1.2)
plt.figure(figsize=(10,8), dpi= 80)
ax = sns.lmplot(x="density", y="public_transit percentage",
data=commuters_df.loc[commuters_df['public_transit percentage'] != 0],
height=7, aspect=1.6, robust=True, palette='tab10',
scatter_kws=dict(s=60, linewidths=.7, edgecolors='black'))
ax.set(xlabel="Density [People per $\mathregular{km^2}$]", ylabel="Public Transport [%]")
st.pyplot()
# _________________________
#
# ----- FEATURE ENGINEERING
# ----- IN PROGRESS !!!
# Find indicators
# Local
# us_colleges = pd.read_csv('../us-commuters-data/us-colleges-and-universities.csv', sep=';')
# Global
# us_colleges = pd.read_csv('https://raw.githubusercontent.com/bauhofer/data/master/us-colleges-and-universities.csv', sep=';')
# st.write(us_colleges)
# Number of colleges in cities
# Number of students and lecturers in cities
st.markdown("Data Sources: Wikipedia, simplemaps.com")
if __name__ == '__main__':
main()