Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import plotly.express as px | |
import folium | |
from folium.plugins import HeatMap, MarkerCluster | |
from streamlit_folium import st_folium | |
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 | |
) | |
fig.update_layout( | |
xaxis_tickangle=-45, | |
legend_title='Severity Level', | |
barmode='stack' | |
) | |
return fig | |
def get_top_violations(df, age_group): | |
if age_group == 'All Ages': | |
violations = pd.concat([ | |
df['Violation1_Drv1'].value_counts(), | |
df['Violation1_Drv2'].value_counts() | |
]).groupby(level=0).sum() | |
else: | |
filtered_df = df[ | |
(df['Age_Group_Drv1'] == age_group) | | |
(df['Age_Group_Drv2'] == 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'] | |
violations_df['Percentage'] = (violations_df['Count'] / violations_df['Count'].sum() * 100).round(2) | |
violations_df['Percentage'] = violations_df['Percentage'].map('{:.2f}%'.format) | |
return violations_df.head() | |
def create_map(df, selected_year): | |
filtered_df = df[df['Year'] == selected_year] | |
m = folium.Map( | |
location=[33.4255, -111.9400], | |
zoom_start=12, | |
control_scale=True, | |
tiles='CartoDB positron' | |
) | |
marker_cluster = MarkerCluster().add_to(m) | |
for _, row in filtered_df.iterrows(): | |
folium.Marker( | |
location=[row['Latitude'], row['Longitude']], | |
popup=f"Accident at {row['Longitude']}, {row['Latitude']}<br>Date: {row['DateTime']}<br>Severity: {row['Injuryseverity']}", | |
icon=folium.Icon(color='red') | |
).add_to(marker_cluster) | |
heat_data = filtered_df[['Latitude', 'Longitude']].values.tolist() | |
HeatMap(heat_data, radius=15, max_zoom=13, min_opacity=0.3).add_to(m) | |
return m | |
def main(): | |
st.title('Traffic Crash Analysis') | |
# Load data | |
df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv') | |
# Create tabs for different visualizations | |
tab1, tab2 = st.tabs(["Crash Statistics", "Crash Map"]) | |
with tab1: | |
# Age group selection | |
age_groups = ['All Ages', '16-25', '26-35', '36-45', '46-55', '56-65', '65+'] | |
selected_age = st.selectbox('Select Age Group:', age_groups) | |
# Create and display chart | |
fig = create_severity_violation_chart(df, selected_age) | |
st.plotly_chart(fig, use_container_width=True) | |
# Display statistics | |
if selected_age == 'All Ages': | |
total_incidents = len(df) | |
else: | |
total_incidents = len(df[ | |
(df['Age_Group_Drv1'] == selected_age) | | |
(df['Age_Group_Drv2'] == selected_age) | |
]) | |
# Create two columns for statistics | |
col1, col2 = st.columns(2) | |
with col1: | |
st.markdown(f"### Total Incidents") | |
st.markdown(f"**{total_incidents:,}** incidents for {selected_age}") | |
with col2: | |
st.markdown("### Top Violations") | |
top_violations = get_top_violations(df, selected_age) | |
st.table(top_violations) | |
with tab2: | |
# Year selection for map | |
years = sorted(df['Year'].unique()) | |
selected_year = st.selectbox('Select Year:', years) | |
# Create and display map | |
st.markdown("### Crash Location Map") | |
map_placeholder = st.empty() | |
with map_placeholder: | |
m = create_map(df, selected_year) | |
map_data = st_folium( | |
m, | |
width=800, | |
height=600, | |
key=f"map_{selected_year}", | |
returned_objects=["null_drawing"] | |
) | |
if __name__ == "__main__": | |
main() |