-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crashes.py
155 lines (132 loc) · 5.16 KB
/
Crashes.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
import dash
from dash import dcc, html
import pandas as pd
import plotly.express as px
from dash.dependencies import Input, Output
# Import the data
df = pd.read_csv('Data_Processed.csv')
hourly_counts = pd.read_csv('assets/hourly_counts.csv')
post_covid = pd.read_csv('assets/post_covid.csv')
county_counts = pd.read_csv('assets/county_counts.csv')
monthly_counts = pd.read_csv('assets/monthly_counts.csv')
daily_counts = pd.read_csv('assets/daily_counts.csv')
# Create the Dash app
app = dash.Dash(__name__)
server = app.server
app.layout = html.Div([
html.Div([
html.H1("Annapolis Car Crash Analysis", style={'display': 'inline-block'}),
html.Img(src=app.get_asset_url('maryland.png'), style={'position': 'absolute', 'right': '0', 'top': '0'})
]),
html.P(
"This dashboard uses python's plotly dash to show car crashes trends in Maryland over the last several years."
"Data on from Maryland. https://opendata.maryland.gov/Public-Safety/Maryland-Statewide-Vehicle-Crashes/65du-s3qu"
"Check out the code at https://github.com/ballard11/Maryland_DS"
, style={"margin": "20px"}),
html.Div([
dcc.Dropdown(
id='map-dropdown',
options=[{'label': i, 'value': i} for i in df['REPORT_TYPE'].dropna().unique()],
value='Fatal Crash',
style={'width': '50%'}
),
dcc.Graph(id='map-graph')
]),
html.Div([
dcc.Dropdown(
id='county-dropdown',
options=[{'label': i, 'value': i} for i in df['COUNTY_DESC'].dropna().unique()],
value='Anne Arundel',
style={"width": "50%"}
),
]),
html.Div([ # Container for the hourly graph
dcc.Graph(id='hourly-graph')
], style={"width": "33%", "float": "left"}),
html.Div([ # Container for the day graph
dcc.Graph(id='day-graph')
], style={"width": "33%", "float": "left"}),
html.Div([ # Container for the monthly graph
dcc.Graph(id='monthly-graph')
], style={"width": "33%", "float": "left"}),
html.Div([
html.H2("COVID-19 Crash Analysis"),
dcc.Graph(id='covid-graph')
])
])
@app.callback(
[Output('hourly-graph', 'figure'),
Output('day-graph', 'figure'),
Output('monthly-graph', 'figure')],
[Input('county-dropdown', 'value')]
)
def update_graphs(County):
# Hourly graph update
filtered_df_hourly = hourly_counts[hourly_counts['COUNTY_DESC'] == County]
fig_hourly = px.line(filtered_df_hourly, x='HOUR', y='COUNT', color='REPORT_TYPE', title='Hourly Trend in Crashes')
fig_hourly.update_layout(legend=dict(
yanchor="top",
y=1,
xanchor="right",
x=1,
bgcolor='rgba(255,255,255,0.1)'
))
# Day graph update
filtered_df_day = daily_counts[daily_counts['COUNTY_DESC'] == County]
fig_day = px.line(filtered_df_day, x='DAY', y='COUNT', color='REPORT_TYPE', title='Daily Trend in Crashes')
fig_day.update_layout(legend=dict(
yanchor="top",
y=1,
xanchor="right",
x=1,
bgcolor='rgba(255,255,255,0.1)'
))
# Monthly graph update
filtered_df_monthly = monthly_counts[monthly_counts['COUNTY_DESC'] == County]
fig_monthly = px.bar(filtered_df_monthly, x='MONTH', y='COUNT', color='REPORT_TYPE', title='Monthly Trend in Crashes')
fig_monthly.update_layout(legend=dict(
yanchor="top",
y=1,
xanchor="right",
x=1,
bgcolor='rgba(255,255,255,0.1)'
))
return fig_hourly, fig_day, fig_monthly
#COVID Crash Analysis
@app.callback(
Output('covid-graph', 'figure'),
[Input('map-dropdown', 'value')])
def update_covid_graph(dummy_input):
fig = px.line(post_covid, x='Day_of_Year', y='percentage_of_pre_covid_smooth', color='Year',
title='30-Day Smoothed Daily Crashes as a Percentage of Pre-COVID Baseline')
fig.add_shape(type='line', xref='paper', yref='y', x0=0, x1=1, y0=100, y1=100, line=dict(color='Red', dash='dot'))
months_in_year = [i*30 for i in range(13)]
month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', '']
fig.update_xaxes(tickvals=months_in_year, ticktext=month_names)
return fig
#Map Crash
@app.callback(
Output('map-graph', 'figure'),
Input('map-dropdown', 'value'))
def update_map_graph(crash_type):
df_filtered = df[df['REPORT_TYPE'] == crash_type]
fig_map = px.scatter_mapbox(
df_filtered,
lat='LATITUDE',
lon='LONGITUDE',
width=1600,
height=800,
color='REPORT_TYPE',
color_continuous_scale=px.colors.cyclical.IceFire,
title='Crash locations in Maryland',
opacity=0.25,
category_orders={'REPORT_TYPE': ['Fatal Crash', 'Injury Crash', 'Property Damage Crash', 'Unknown']}
)
fig_map.update_layout(
mapbox_style="mapbox://styles/mapbox/light-v10",
mapbox_accesstoken='pk.eyJ1IjoiYmVuZ2JhbGxhcmQiLCJhIjoiY2xrMWp6dXFmMDZzZDNocGJ3Zjh5amMwZiJ9.YGDUZddiJp0uKNBG68Dhlw'
)
return fig_map
# Run the app
if __name__ == '__main__':
app.run_server(debug=True, port=8052)