Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
4 |
import folium
|
5 |
from folium.plugins import HeatMap, MarkerCluster
|
6 |
from streamlit_folium import st_folium
|
@@ -128,6 +129,120 @@ def create_map(df, selected_year):
|
|
128 |
|
129 |
return m
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
def main():
|
132 |
st.title('Traffic Crash Analysis')
|
133 |
|
@@ -185,5 +300,11 @@ def main():
|
|
185 |
returned_objects=["null_drawing"]
|
186 |
)
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
if __name__ == "__main__":
|
189 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
+
import altair as alt
|
5 |
import folium
|
6 |
from folium.plugins import HeatMap, MarkerCluster
|
7 |
from streamlit_folium import st_folium
|
|
|
129 |
|
130 |
return m
|
131 |
|
132 |
+
def 5_create_injuries_fatalities_chart(crash_data):
|
133 |
+
|
134 |
+
# 5th visualization title
|
135 |
+
st.header("5. Total Injuries and Fatalities by Month")
|
136 |
+
|
137 |
+
# Filter rows where we have valid data for all necessary columns
|
138 |
+
crash_data = crash_data[['DateTime', 'Totalinjuries', 'Totalfatalities', 'Unittype_One', 'Unittype_Two']].dropna()
|
139 |
+
|
140 |
+
# Convert "DateTime" to datetime type
|
141 |
+
crash_data['DateTime'] = pd.to_datetime(crash_data['DateTime'], errors='coerce')
|
142 |
+
crash_data['Month'] = crash_data['DateTime'].dt.month_name()
|
143 |
+
|
144 |
+
# sort months in order
|
145 |
+
month_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
|
146 |
+
crash_data['Month'] = pd.Categorical(crash_data['Month'], categories=month_order, ordered=True)
|
147 |
+
|
148 |
+
# Dropdown for Unit Type selection
|
149 |
+
# Dropdown for Unit Type selection
|
150 |
+
# st.sidebar.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist()) # previous location of dropdown in sidebar
|
151 |
+
# unit_type = st.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist())
|
152 |
+
unit_type_pairs = set()
|
153 |
+
for _, row in crash_data[['Unittype_One', 'Unittype_Two']].dropna().iterrows():
|
154 |
+
if row['Unittype_One'] != 'Driverless' or row['Unittype_Two'] != 'Driverless':
|
155 |
+
pair = ' vs '.join(sorted([row['Unittype_One'], row['Unittype_Two']]))
|
156 |
+
unit_type_pairs.add(pair)
|
157 |
+
# unit_type_pairs = list(unit_type_pairs) # modified as below to sort the dropdown options in alphabetical order
|
158 |
+
unit_type_pairs = sorted(list(unit_type_pairs))
|
159 |
+
unit_type = st.selectbox("Select Unit Type Pair", options=['Total'] + unit_type_pairs)
|
160 |
+
|
161 |
+
# Filter data based on the selected unit type
|
162 |
+
if unit_type == 'Total':
|
163 |
+
filtered_data = crash_data
|
164 |
+
else:
|
165 |
+
unit_one, unit_two = unit_type.split(' vs ')
|
166 |
+
filtered_data = crash_data[((crash_data['Unittype_One'] == unit_one) & (crash_data['Unittype_Two'] == unit_two)) |
|
167 |
+
((crash_data['Unittype_One'] == unit_two) & (crash_data['Unittype_Two'] == unit_one))]
|
168 |
+
|
169 |
+
# Group data by month and calculate total injuries and fatalities
|
170 |
+
monthly_sum = filtered_data.groupby('Month').agg({'Totalinjuries': 'sum', 'Totalfatalities': 'sum'}).reset_index()
|
171 |
+
|
172 |
+
# Reshape the data for easier plotting
|
173 |
+
injuries = monthly_sum[['Month', 'Totalinjuries']].rename(columns={'Totalinjuries': 'Value'})
|
174 |
+
injuries['Measure'] = 'Total Injuries'
|
175 |
+
|
176 |
+
fatalities = monthly_sum[['Month', 'Totalfatalities']].rename(columns={'Totalfatalities': 'Value'})
|
177 |
+
fatalities['Measure'] = 'Total Fatalities'
|
178 |
+
|
179 |
+
combined_data = pd.concat([injuries, fatalities])
|
180 |
+
|
181 |
+
# Originally tried to use bar chart but switched to line chart for better trend visualization
|
182 |
+
# alt.Chart(monthly_sum).mark_bar().encode(
|
183 |
+
# x=alt.X('Month', sort=month_order, title='Month'),
|
184 |
+
# y=alt.Y('Totalinjuries', title='Total Injuries', axis=alt.Axis(titleColor='blue', labelColor='blue', tickColor='blue')),
|
185 |
+
# color=alt.value('blue'),
|
186 |
+
# tooltip=['Month', 'Totalinjuries']
|
187 |
+
# ).properties(
|
188 |
+
# title='Total Injuries and Fatalities by Month',
|
189 |
+
# width=300,
|
190 |
+
# height=300
|
191 |
+
# ) + alt.Chart(monthly_sum).mark_bar().encode(
|
192 |
+
# x=alt.X('Month', sort=month_order, title='Month'),
|
193 |
+
# y=alt.Y('Totalfatalities', title='Total Fatalities', axis=alt.Axis(titleColor='red', labelColor='red', tickColor='red')),
|
194 |
+
# color=alt.value('red'),
|
195 |
+
# tooltip=['Month', 'Totalfatalities']
|
196 |
+
# )
|
197 |
+
|
198 |
+
# Tried to figure out how to plot a legend using altair
|
199 |
+
# line_chart = alt.Chart(monthly_sum).mark_line(point=True).encode(
|
200 |
+
# x=alt.X('Month', sort=month_order, title='Month'),
|
201 |
+
# y=alt.Y('Totalinjuries', title='Total Injuries & Fatalities', axis=alt.Axis(titleColor='black')),
|
202 |
+
# color=alt.value('blue'),
|
203 |
+
# tooltip=['Month', 'Totalinjuries']
|
204 |
+
# ).properties(
|
205 |
+
# title=f'Total Injuries and Fatalities by Month for Unit Type Pair: {unit_type}',
|
206 |
+
# width=600,
|
207 |
+
# height=400
|
208 |
+
# ) + alt.Chart(monthly_sum).mark_line(point=True).encode(
|
209 |
+
# x=alt.X('Month', sort=month_order, title='Month'),
|
210 |
+
# y=alt.Y('Totalfatalities', axis=alt.Axis(titleColor='red')),
|
211 |
+
# color=alt.value('red'),
|
212 |
+
# tooltip=['Month', 'Totalfatalities']
|
213 |
+
# ).configure_legend(
|
214 |
+
# titleFontSize=14,
|
215 |
+
# labelFontSize=12,
|
216 |
+
# titleColor='black',
|
217 |
+
# labelColor='black'
|
218 |
+
# )
|
219 |
+
|
220 |
+
# Plot line chart
|
221 |
+
line_chart = alt.Chart(combined_data).mark_line(point=True).encode(
|
222 |
+
x=alt.X('Month:N', sort=month_order, title='Month'),
|
223 |
+
y=alt.Y('Value:Q', title='Total Injuries & Fatalities'),
|
224 |
+
color=alt.Color('Measure:N', title='', scale=alt.Scale(domain=['Total Injuries', 'Total Fatalities'], range=['blue', 'red'])),
|
225 |
+
tooltip=['Month', 'Measure:N', 'Value:Q']
|
226 |
+
).properties(
|
227 |
+
title=f'Total Injuries and Fatalities by Month for Unit Type Pair: {unit_type}',
|
228 |
+
width=600,
|
229 |
+
height=400
|
230 |
+
)
|
231 |
+
|
232 |
+
# # Combine the charts (trying to make legend)
|
233 |
+
# combined_chart = alt.layer(line_chart_injuries, line_chart_fatalities).properties(
|
234 |
+
# title=f'Total Injuries and Fatalities by Month for Unit Type Pair: {unit_type}',
|
235 |
+
# width=600,
|
236 |
+
# height=400
|
237 |
+
# ).configure_legend(
|
238 |
+
# titleFontSize=14,
|
239 |
+
# labelFontSize=12,
|
240 |
+
# titleColor='black',
|
241 |
+
# labelColor='black'
|
242 |
+
# )
|
243 |
+
|
244 |
+
return line_chart
|
245 |
+
|
246 |
def main():
|
247 |
st.title('Traffic Crash Analysis')
|
248 |
|
|
|
300 |
returned_objects=["null_drawing"]
|
301 |
)
|
302 |
|
303 |
+
# Create 5th Visualization: Injuries and fatalities chart
|
304 |
+
injuries_fatalities_chart = 5_create_injuries_fatalities_chart(df)
|
305 |
+
st.altair_chart(injuries_fatalities_chart, use_container_width=True)
|
306 |
+
st.markdown("#### TODO: add write-up for this 5th chart.")
|
307 |
+
|
308 |
+
|
309 |
if __name__ == "__main__":
|
310 |
main()
|