File size: 7,780 Bytes
7369ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import plotly.express as px
import altair as alt

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_injuries_fatalities_chart(crash_data):
    # 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
    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 = 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])

    # 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
    )

    return line_chart

def main():
    st.title('Traffic Crash Analysis')
    
    # Load data
    df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv')
    
    # Create simple dropdown for age groups
    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 crash severity vs violation type chart
    fig = create_severity_violation_chart(df, selected_age)
    st.plotly_chart(fig, use_container_width=True)

    # Create and display injuries and fatalities chart
    injuries_fatalities_chart = create_injuries_fatalities_chart(df)
    st.altair_chart(injuries_fatalities_chart, 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}")
    
    # Display top violations table
    with col2:
        st.markdown("### Top Violations")
        top_violations = get_top_violations(df, selected_age)
        st.table(top_violations)

if __name__ == "__main__":
    main()