import streamlit as st
import pandas as pd
import plotly.express as px
import altair as alt
import folium
from streamlit_plotly_events import plotly_events # added for part3 interactivity
from folium.plugins import HeatMap, MarkerCluster
from streamlit_folium import st_folium
# To fix the color scheme in crash stats plot (asked ChatGPT for appropriate colors)
severity_colors = {
"No Injury": "#1f77b4",
"Possible Injury": "#aec7e8",
"Non Incapacitating Injury": "#ff7f0e",
"Incapacitating Injury": "#ffbb78",
"Suspected Minor Injury": "#2ca02c",
"Suspected Serious Injury": "#98df8a",
"Fatal": "#d62728",
}
@st.cache_data
def load_and_preprocess_data(file_path):
# Read the data
df = pd.read_csv(file_path)
# Basic preprocessing
df = df.drop(['X', 'Y'], axis=1)
df.dropna(subset=['Incidentid', 'DateTime', 'Year', 'Latitude', 'Longitude'], inplace=True)
# Convert Year to int
df['Year'] = df['Year'].astype(int)
# Fill missing values
numeric = ['Age_Drv1', 'Age_Drv2']
for col in numeric:
df[col].fillna(df[col].median(), inplace=True)
categorical = ['Gender_Drv1', 'Violation1_Drv1', 'AlcoholUse_Drv1', 'DrugUse_Drv1',
'Gender_Drv2', 'Violation1_Drv2', 'AlcoholUse_Drv2', 'DrugUse_Drv2',
'Unittype_Two', 'Traveldirection_Two', 'Unitaction_Two', 'CrossStreet']
for col in categorical:
df[col].fillna('Unknown', inplace=True)
# Remove invalid ages
df = df[
(df['Age_Drv1'] <= 90) &
(df['Age_Drv2'] <= 90) &
(df['Age_Drv1'] >= 16) &
(df['Age_Drv2'] >= 16)
]
# Create age groups
bins = [15, 25, 35, 45, 55, 65, 90]
labels = ['16-25', '26-35', '36-45', '46-55', '56-65', '65+']
df['Age_Group_Drv1'] = pd.cut(df['Age_Drv1'], bins=bins, labels=labels)
df['Age_Group_Drv2'] = pd.cut(df['Age_Drv2'], bins=bins, labels=labels)
return df
def create_severity_violation_chart(df, age_group=None):
# Apply age group filter if selected
if age_group != 'All Ages':
df = df[(df['Age_Group_Drv1'] == age_group) | (df['Age_Group_Drv2'] == age_group)]
# Combine violations from both drivers
violations_1 = df.groupby(['Violation1_Drv1', 'Injuryseverity']).size().reset_index(name='count')
violations_2 = df.groupby(['Violation1_Drv2', 'Injuryseverity']).size().reset_index(name='count')
violations_1.columns = ['Violation', 'Severity', 'count']
violations_2.columns = ['Violation', 'Severity', 'count']
violations = pd.concat([violations_1, violations_2])
violations = violations.groupby(['Violation', 'Severity'])['count'].sum().reset_index()
# Create visualization
fig = px.bar(
violations,
x='Violation',
y='count',
color='Severity',
title=f'Crash Severity Distribution by Violation Type - {age_group}',
labels={'count': 'Number of Incidents', 'Violation': 'Violation Type'},
height=600,
color_discrete_map=severity_colors, # --> for part 3
)
# fig.update_layout(
# xaxis_tickangle=-45,
# legend_title='Severity Level',
# barmode='stack'
# )
# modified the above code because x-axis labels were partially pruned
fig.update_layout(
xaxis_tickangle=-45,
legend_title='Severity Level',
barmode='stack',
margin=dict(t=50, b=150), # Increase bottom margin to avoid pruning
xaxis=dict(automargin=True)
)
# return fig
return fig, violations
def get_top_violations(df, age_group):
# Calculate total incidents for the age group
if age_group == 'All Ages':
total_incidents = len(df)
# Get violations for all ages
violations = pd.concat([
df['Violation1_Drv1'].value_counts(),
df['Violation1_Drv2'].value_counts()
]).groupby(level=0).sum()
else:
# Filter for specific age group
filtered_df = df[
(df['Age_Group_Drv1'] == age_group) |
(df['Age_Group_Drv2'] == age_group)
]
total_incidents = len(filtered_df)
# Get violations for specific age group
violations = pd.concat([
filtered_df['Violation1_Drv1'].value_counts(),
filtered_df['Violation1_Drv2'].value_counts()
]).groupby(level=0).sum()
# Convert to DataFrame and format
violations_df = violations.reset_index()
violations_df.columns = ['Violation Type', 'Count']
# Sort by Count in descending order
violations_df = violations_df.sort_values('Count', ascending=False)
# Calculate percentage of total incidents
violations_df['Percentage'] = (violations_df['Count'] / total_incidents * 100).round(2)
violations_df['Percentage'] = violations_df['Percentage'].map('{:.2f}%'.format)
return violations_df.head()
@st.cache_data
def create_interactive_pie_chart(violations, selected_violation, selected_age):
# Filter data based on selected violation
filtered_data = violations[violations['Violation'] == selected_violation]
# Create a pie chart for severity distribution of the selected violation type
fig = px.pie(
filtered_data,
names='Severity',
values='count',
# title=f'Severity Level Distribution for Violation: {selected_violation}',
title=f'Severity Level Distribution for Violation: {selected_violation} - {selected_age}', # dynamically update pie chart's title
height=600,
color_discrete_map=severity_colors
)
return fig
def create_map_bar_chart(df, selected_year):
# Create severity count bar chart
filtered_df = df[df['Year'] == selected_year]
severity_count = filtered_df['Injuryseverity'].value_counts().reset_index()
severity_count.columns = ['Injuryseverity', 'Count']
fig = px.bar(
severity_count,
x='Injuryseverity',
y='Count',
title="Accidents by Severity",
labels={'Injuryseverity': 'Severity', 'Count': 'Number of Accidents'} # Adjust height as needed
)
fig.update_traces(marker_color='blue')
fig.update_layout(
clickmode='event+select', # Enable interactivity
xaxis_tickangle=45, # Rotate x-axis labels 45 degrees
margin=dict(t=50, b=150), # Add bottom margin to prevent label cutoff
)
return fig
@st.cache_data
def create_map(df, selected_year, selected_severity=None):
# Filter data by selected year
filtered_df = df[df['Year'] == selected_year]
# Filter further by selected severity if provided
if selected_severity:
filtered_df = filtered_df[filtered_df['Injuryseverity'] == selected_severity]
# Remove rows with missing latitude or longitude
filtered_df = filtered_df.dropna(subset=['Latitude', 'Longitude'])
# Create the map
m = folium.Map(
location=[33.4255, -111.9400], # Default location (can be customized)
zoom_start=12,
control_scale=True,
tiles='CartoDB positron'
)
# Add marker cluster
marker_cluster = MarkerCluster(name="Accident Locations").add_to(m)
# Add accident markers
for _, row in filtered_df.iterrows():
folium.Marker(
location=[row['Latitude'], row['Longitude']],
popup=f"Accident at {row['Longitude']}, {row['Latitude']}
Date: {row['DateTime']}
Severity: {row['Injuryseverity']}",
icon=folium.Icon(color='red')
).add_to(marker_cluster)
# Add heatmap
heat_data = filtered_df[['Latitude', 'Longitude']].values.tolist()
HeatMap(heat_data, radius=15, max_zoom=13, min_opacity=0.3, name="Heat Map").add_to(m)
folium.LayerControl().add_to(m)
return m
def create_injuries_fatalities_chart(crash_data, unit_type):
# 5th visualization title
# st.header("5. Total Injuries and Fatalities by Month")
# Filter rows where we have valid data for all necessary columns
crash_data = crash_data[['DateTime', 'Totalinjuries', 'Totalfatalities', 'Unittype_One', 'Unittype_Two']].dropna()
# Convert "DateTime" to datetime type
crash_data['DateTime'] = pd.to_datetime(crash_data['DateTime'], errors='coerce')
crash_data['Month'] = crash_data['DateTime'].dt.month_name()
# sort months in order
month_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
crash_data['Month'] = pd.Categorical(crash_data['Month'], categories=month_order, ordered=True)
# Dropdown for Unit Type selection
# Dropdown for Unit Type selection
# st.sidebar.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist()) # previous location of dropdown in sidebar
# unit_type = st.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist())
# unit_type_pairs = set()
# for _, row in crash_data[['Unittype_One', 'Unittype_Two']].dropna().iterrows():
# if row['Unittype_One'] != 'Driverless' or row['Unittype_Two'] != 'Driverless':
# pair = ' vs '.join(sorted([row['Unittype_One'], row['Unittype_Two']]))
# unit_type_pairs.add(pair)
# # unit_type_pairs = list(unit_type_pairs) # modified as below to sort the dropdown options in alphabetical order
# unit_type_pairs = sorted(list(unit_type_pairs))
# unit_type = st.selectbox("Select Unit Type Pair", options=['Total'] + unit_type_pairs)
# Filter data based on the selected unit type
if unit_type == 'Total':
filtered_data = crash_data
else:
unit_one, unit_two = unit_type.split(' vs ')
filtered_data = crash_data[((crash_data['Unittype_One'] == unit_one) & (crash_data['Unittype_Two'] == unit_two)) |
((crash_data['Unittype_One'] == unit_two) & (crash_data['Unittype_Two'] == unit_one))]
# Group data by month and calculate total injuries and fatalities
monthly_sum = filtered_data.groupby('Month').agg({'Totalinjuries': 'sum', 'Totalfatalities': 'sum'}).reset_index()
# Reshape the data for easier plotting
injuries = monthly_sum[['Month', 'Totalinjuries']].rename(columns={'Totalinjuries': 'Value'})
injuries['Measure'] = 'Total Injuries'
fatalities = monthly_sum[['Month', 'Totalfatalities']].rename(columns={'Totalfatalities': 'Value'})
fatalities['Measure'] = 'Total Fatalities'
combined_data = pd.concat([injuries, fatalities])
# Originally tried to use bar chart but switched to line chart for better trend visualization
# alt.Chart(monthly_sum).mark_bar().encode(
# x=alt.X('Month', sort=month_order, title='Month'),
# y=alt.Y('Totalinjuries', title='Total Injuries', axis=alt.Axis(titleColor='blue', labelColor='blue', tickColor='blue')),
# color=alt.value('blue'),
# tooltip=['Month', 'Totalinjuries']
# ).properties(
# title='Total Injuries and Fatalities by Month',
# width=300,
# height=300
# ) + alt.Chart(monthly_sum).mark_bar().encode(
# x=alt.X('Month', sort=month_order, title='Month'),
# y=alt.Y('Totalfatalities', title='Total Fatalities', axis=alt.Axis(titleColor='red', labelColor='red', tickColor='red')),
# color=alt.value('red'),
# tooltip=['Month', 'Totalfatalities']
# )
# Tried to figure out how to plot a legend using altair
# line_chart = alt.Chart(monthly_sum).mark_line(point=True).encode(
# x=alt.X('Month', sort=month_order, title='Month'),
# y=alt.Y('Totalinjuries', title='Total Injuries & Fatalities', axis=alt.Axis(titleColor='black')),
# color=alt.value('blue'),
# tooltip=['Month', 'Totalinjuries']
# ).properties(
# title=f'Total Injuries and Fatalities by Month for Unit Type Pair: {unit_type}',
# width=600,
# height=400
# ) + alt.Chart(monthly_sum).mark_line(point=True).encode(
# x=alt.X('Month', sort=month_order, title='Month'),
# y=alt.Y('Totalfatalities', axis=alt.Axis(titleColor='red')),
# color=alt.value('red'),
# tooltip=['Month', 'Totalfatalities']
# ).configure_legend(
# titleFontSize=14,
# labelFontSize=12,
# titleColor='black',
# labelColor='black'
# )
# Plot line chart
line_chart = alt.Chart(combined_data).mark_line(point=True).encode(
x=alt.X('Month:N', sort=month_order, title='Month'),
y=alt.Y('Value:Q', title='Total Injuries & Fatalities'),
color=alt.Color('Measure:N', title='', scale=alt.Scale(domain=['Total Injuries', 'Total Fatalities'], range=['blue', 'red'])),
tooltip=['Month', 'Measure:N', 'Value:Q']
).properties(
title=f'Total Injuries and Fatalities by Month for Unit Type Pair: {unit_type}',
width=600,
height=400
)
# # Combine the charts (trying to make legend)
# combined_chart = alt.layer(line_chart_injuries, line_chart_fatalities).properties(
# title=f'Total Injuries and Fatalities by Month for Unit Type Pair: {unit_type}',
# width=600,
# height=400
# ).configure_legend(
# titleFontSize=14,
# labelFontSize=12,
# titleColor='black',
# labelColor='black'
# )
return line_chart
def create_crash_trend_chart(df, weather=None):
if weather and weather != 'All Conditions':
df = df[df['Weather'] == weather]
# Group data by year and count unique Incident IDs
trend_data = df.groupby('Year')['Incidentid'].nunique().reset_index()
trend_data.columns = ['Year', 'Crash Count']
# Create line graph
fig = px.line(
trend_data,
x='Year',
y='Crash Count',
title=f'Crash Trend Over Time ({weather})',
labels={'Year': 'Year', 'Crash Count': 'Number of Unique Crashes'},
markers=True,
height=600
)
fig.update_traces(line=dict(width=2), marker=dict(size=8))
fig.update_layout(legend_title_text='Trend')
return fig
def create_category_distribution_chart(df, selected_category, selected_year):
# Filter by selected year
if selected_year != 'All Years':
df = df[df['Year'] == int(selected_year)]
# Group by selected category and Injury Severity
grouped_data = df.groupby([selected_category, 'Injuryseverity']).size().reset_index(name='Count')
# Calculate percentages for each category value
total_counts = grouped_data.groupby(selected_category)['Count'].transform('sum')
grouped_data['Percentage'] = (grouped_data['Count'] / total_counts * 100).round(2)
# Create the stacked bar chart using Plotly
fig = px.bar(
grouped_data,
x=selected_category,
y='Count',
color='Injuryseverity',
text='Percentage',
title=f'Distribution of Incidents by {selected_category} ({selected_year})',
labels={'Count': 'Number of Incidents', selected_category: 'Category'},
height=600,
)
# Customize the chart appearance
fig.update_traces(texttemplate='%{text}%', textposition='inside')
fig.update_layout(
barmode='stack',
xaxis_tickangle=-45,
legend_title='Injury Severity',
margin=dict(t=50, b=150, l=50, r=50),
)
return fig
def main():
st.set_page_config(page_title="Terrific Tempe Traffic", layout="wide")
st.markdown("""
""", unsafe_allow_html=True)
st.markdown("""
""", unsafe_allow_html=True)
st.markdown("