File size: 28,599 Bytes
b940652
 
 
d29859a
2a9b164
 
 
b940652
2a9b164
efe30e8
 
 
 
 
 
 
ee5e9c0
 
 
 
efe30e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a76acf8
efe30e8
a76acf8
 
efe30e8
 
 
 
 
a76acf8
efe30e8
 
 
 
a76acf8
 
efe30e8
 
 
 
 
 
 
 
a76acf8
 
 
 
 
 
efe30e8
 
 
 
2a9b164
 
 
d923522
2a9b164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bfb630
d29859a
 
df1ef3c
d29859a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bfb630
 
 
 
 
 
 
 
d29859a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a89028e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f2ec5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efe30e8
fb187c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41058b5
 
 
a76acf8
41058b5
 
fb187c0
8f4683a
41058b5
 
 
 
0014df0
41058b5
 
 
 
 
 
efe30e8
 
 
2b3866a
270f8fe
 
efe30e8
2a9b164
a76acf8
 
 
 
 
 
 
efe30e8
2a9b164
a76acf8
 
 
2a9b164
a76acf8
fb187c0
a76acf8
 
 
 
 
 
 
 
fb187c0
 
 
a76acf8
 
41058b5
 
a76acf8
 
 
41058b5
a76acf8
 
 
41058b5
a76acf8
8739999
a76acf8
 
 
 
41058b5
a76acf8
 
 
 
 
41058b5
a76acf8
2b3866a
a76acf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d29859a
a76acf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb187c0
a76acf8
 
 
 
 
fb187c0
 
a76acf8
fb187c0
 
a76acf8
 
 
 
8739999
a76acf8
 
 
 
 
 
41058b5
966a139
fa6d354
 
 
 
 
 
 
 
 
fb187c0
 
 
 
 
a76acf8
fb187c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a76acf8
a2a8c23
a76acf8
 
fb187c0
a76acf8
 
 
 
 
 
 
 
 
 
 
 
 
41058b5
fb187c0
a76acf8
 
 
 
 
 
df1ef3c
a76acf8
 
 
 
 
 
 
 
fb187c0
a76acf8
 
 
 
0014df0
a76acf8
 
67720f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a76acf8
67720f8
0014df0
 
 
 
 
 
d657660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0014df0
d657660
41058b5
5bfb630
b940652
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import streamlit as st
import pandas as pd
import plotly.express as px
import altair as alt
import folium
from folium.plugins import HeatMap, MarkerCluster
from streamlit_folium import st_folium

@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
    )
    
    fig.update_layout(
        xaxis_tickangle=-45,
        legend_title='Severity Level',
        barmode='stack'
    )
    
    return fig

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_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 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=100),
    )

    return fig

def main():
    st.set_page_config(page_title="Terrific Tempe Traffic", layout="wide")

    st.markdown("""
    <style>
    .reportview-container {
        font-size: 20px;
    }
    h1, h2, h3, h4, h5, h6 {
        font-size: 150%; 
    }
    p {
        font-size: 125%; 
    }
    </style>
    """, unsafe_allow_html=True)
    

    st.markdown("""
        <style>
        .title {
            text-align: center;
            padding: 25px;
        }
        </style>
        """, unsafe_allow_html=True)
    
    st.markdown("<div class='title'><h1> Accident Analysis for City of Tempe,Arizona </h1></div>", unsafe_allow_html=True)
    

    st.markdown("""
     **Team Members:**
    - Janhavi Tushar Zarapkar ([email protected])
    - Hangyue Zhang ([email protected])
    - Andrew Nam ([email protected])
    - Nirmal Attarde ([email protected])
    - Maanas Sandeep Agrawal ([email protected])
    """)
    

    st.markdown("""
    # Introduction to the Traffic Accident Dataset
    This dataset contains detailed information about traffic accidents in the city of **Tempe**. It includes various attributes of the accidents, such as the severity of injuries, the demographics of the drivers involved, the locations of the incidents, and the conditions at the time of the accidents. The dataset covers accidents that occurred over several years, with data on factors like **weather conditions**, **road surface conditions**, the **time of day**, and the type of **violations** (e.g., alcohol or drug use) that may have contributed to the accident.
    
    The data was sourced from **Tempe City's traffic incident reports** and provides a comprehensive view of the factors influencing road safety and accident severity in the city. By analyzing this dataset, we can gain insights into the key contributors to traffic incidents and uncover trends that could help improve traffic safety measures, urban planning, and law enforcement policies in the city.
    """)
    
    
    
    # Load data
    df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv')

    if 'Weather' not in df.columns:
        df['Weather'] = 'Unknown'
    
    # Create tabs for different visualizations
    tab1, tab2, tab3, tab4, tab5 = st.tabs([
    "Crash Trend", 
    "Crash Statistics", 
    "Distribution by Category",
    "Crash Injuries/Fatalities",
    "Crash Map"
])
    
    with tab1:
        # Weather condition filter
        weather = ['All Conditions'] + sorted(df['Weather'].unique())
        selected_weather = st.selectbox('Select Weather Condition:', weather)
        
        trend_col, desc_col = st.columns([7, 3])
        
        with trend_col:
            trend_fig = create_crash_trend_chart(df, selected_weather)
            trend_fig.update_layout(
                height=800,
                width=None,
                margin=dict(l=50, r=50, t=50, b=50)
            )
            st.plotly_chart(trend_fig, use_container_width=True)

        with desc_col:
            st.markdown("""
            ## **Crash Trend Over Time**
            This interactive line chart visualizes the trend of unique traffic crashes over the years, optionally filtered by weather conditions. It highlights how crash frequency changes over time, helping identify trends and potential contributing factors.
            
            **Key Features:**
            * **Time Trend Analysis**: Displays the total number of unique crashes for each year, showing long-term patterns.
            * **Weather Filter**: Users can filter the data by weather conditions (e.g., "Rainy", "Sunny") to analyze how weather impacts crash trends.
            * **Interactive Tooltips**: Hovering over data points reveals the exact crash count for each year, providing detailed insights.
            
            **Color Scheme and Design:**
            * **Line and Markers**: A smooth line connects data points, with prominent markers for each year to highlight trends clearly.
            * **Dynamic Title**: The chart updates its title to reflect the selected weather condition or "All Conditions" for the overall trend.
            
            **Insights:**
            
            This chart helps uncover:
               * Annual fluctuations in crash incidents.
               * Correlations between weather conditions and crash frequencies.
               * Historical patterns that can guide future safety measures and urban planning decisions
            """)

    with tab2:
        # 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)
        
        chart_col, desc_col = st.columns([7, 3])
        
        with chart_col:
            # 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 desc_col:
            st.markdown("""
            ## Severity of Violations Across Age Groups
            
            This section provides an interactive visualization of crash severities linked to specific violation types, segmented by driver age groups. It enables a comprehensive analysis of how age influences crash severity and violation trends.

            **Key Features:**
            1. **Age Group Analysis**:
               * Select specific age groups (e.g., "16-25", "65+") or analyze all ages to explore correlations between age, violation type, and crash severity.
               * Understand how different age groups are involved in various types of violations.

            2. **Violation Breakdown**:
               * Examine the most frequent violations contributing to traffic accidents for each age group.
               * View detailed statistics showing the distribution of violation types.

            **Insights:**
            * Identifies high-risk behaviors within specific age groups, such as reckless driving in younger drivers or impaired driving in older groups.
            * Highlights which violations are associated with more severe outcomes, aiding targeted safety interventions and public awareness campaigns.
            * Supports data-driven decision making for age-specific traffic safety programs.
            """)

    with tab3:
        # Dropdown for category selection
        categories = [
            'Collisionmanner',
            'Lightcondition',
            'Weather',
            'SurfaceCondition',
            'AlcoholUse_Drv1',
            'Gender_Drv1',
        ]
        selected_category = st.selectbox("Select Category:", categories)

        # Dropdown for year selection
        years = ['All Years'] + sorted(df['Year'].dropna().unique().astype(int).tolist())
        selected_year = st.selectbox("Select Year:", years)

        chart_col, desc_col = st.columns([7, 3])
        
        with chart_col:
            distribution_chart = create_category_distribution_chart(df, selected_category, selected_year)
            distribution_chart.update_layout(
                height=800,
                width=None,
                margin=dict(l=50, r=50, t=50, b=50)
            )
            st.plotly_chart(distribution_chart, use_container_width=True)

        with desc_col:
            st.markdown(f"""
            ## Distribution of Incidents by {selected_category}
            This visualization explores the distribution of traffic incidents across various categories, such as Collision Manner, Weather, Surface Condition, Alcohol Use, and Driver Gender. Each bar represents a specific category value (e.g., "Male" or "Female" for Gender), and the bars are divided into segments based on Injury Severity (e.g., Minor, Moderate, Serious, Fatal).
    
            **Key Features:**
            * Interactive Filters: Select a category and filter by year to analyze trends over time.
            * Insightful Tooltips: Hover over each segment to view the exact count and percentage of incidents for a given severity level.
            * Comparative Analysis: Quickly identify how different conditions or behaviors correlate with injury severity.
    
            This chart provides actionable insights into factors contributing to traffic incidents and their outcomes, helping stakeholders target interventions and improve road safety.
            """)

    with tab4:
        # Dropdown for Unit Type selection
        unit_type_pairs = set()
        for _, row in df[['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)
    
        chart_col, desc_col = st.columns([7, 3])
    
        with chart_col:
            injuries_fatalities_chart = create_injuries_fatalities_chart(df, unit_type)
            injuries_fatalities_chart = injuries_fatalities_chart.properties(
                height=800
            )
            st.altair_chart(injuries_fatalities_chart, use_container_width=True)
    
        with desc_col:
            st.markdown("""
            ## Injuries and Fatalities Trends
            
            This line chart shows the **total number of injuries and fatalities by month for the selected unit type pair**. The visualization helps identify seasonal patterns and critical trends in traffic incidents involving specific unit types.
    
            **Key Features:**
            * **Injuries Trend** (Blue Line)
                - Tracks monthly injury counts
                - Shows seasonal variations
                - Identifies peak incident periods
    
            * **Fatalities Trend** (Red Line)
                - Monitors monthly fatality counts
                - Generally lower than injuries
                - Highlights critical safety concerns
    
            * **Interactive Selection**
                - Filter by specific unit type pairs
                - Compare different vehicle combinations
                - View overall trends across all types
    
            **Applications:**
            - Identify high-risk months
            - Guide seasonal safety measures
            - Inform emergency response planning
            - Support targeted intervention strategies
            
            This visualization aids stakeholders in developing effective safety measures and resource allocation strategies throughout the year.
            """)
    
    with tab5:
        years = sorted(df['Year'].unique())
        selected_year = st.selectbox('Select Year:', years)
        
        map_col, desc_col = st.columns([7, 3])
        
        with map_col:
            map_placeholder = st.empty()
            with map_placeholder:
                m = create_map(df, selected_year)
                map_data = st_folium(
                    m,
                    width=None,
                    height=800,
                    key=f"map_{selected_year}",
                    returned_objects=["null_drawing"]
                )

        with desc_col:
            st.markdown("""
            ### Traffic Crash Location Map
            This interactive map visualizes traffic accidents in Tempe for the selected year. It combines **marker clustering** and a **heatmap** to show:
            1. **Accident Markers**: Red markers indicate individual accidents, with popups displaying the coordinates, date/time, and severity of each incident.
            2. **Heatmap**: The heatmap highlights accident hotspots with colors ranging from blue (low frequency) to yellow (moderate) and red (high frequency), showing areas with more frequent accidents.
            
            **Key Features:**
            * **Interactive Year Selection**: Users can select a year to view accidents for that specific time.
            * **Accident Patterns**: The map reveals accident-prone areas and severity patterns, helping identify dangerous locations.
            
            **Color Scheme:**
            * **Red**: Individual accident markers.
            * **Blue to Red**: Heatmap colors indicate accident frequency, from low (blue) to high (red).
            
            This map provides insights into accident trends and can help guide safety improvements in the city.
            """)
    
    st.markdown("---")
    
    # Add TODO section title
    st.markdown("# To-Do List for Part 3")
    st.markdown("For the final project part 3, we plan to create two pairs of linked interactive visualizations for analyzing traffic accident data as follows:")
    
    st.markdown("""
    ### Planned Visualizations
    
    1. **Severity-Location Analysis**
        * A bar chart displaying accident severity counts
        * A map visualizing accident locations with marker clusters and heatmaps
        * Interactions in one visualization (e.g., clicking a bar in the chart) will dynamically update the other
        * Enables seamless exploration of data
    
    2. **Violation-Severity Analysis**
        * An interactive bar and pie chart system
        * Shows the distribution of severity levels for selected violation types
        * Clicking a specific bar from the "Crash Severity Distribution by Violation Type" bar plot
        * Dynamically updates a pie plot showing detailed distribution of the chosen violation type
        * Based on the selected age group
    """)
    

    st.markdown("---")
    
    # Add conclusion section
    st.markdown("# FP2 Conclusion")
    
    st.markdown("""
    In FP2, we created interactive visualizations to analyze traffic accident data, focusing on trends, contributing factors, and safety implications. Each visualization provides specific insights and helps users make data-driven decisions to improve road safety.

    - **Crash Trend Over Time**: An interactive line chart showing annual crash patterns with an optional weather filter, helping identify trends and weather-related correlations.

    - **Severity of Violations Across Age Groups**: Visualizes crash severities by violation types and driver age groups, aiding targeted safety campaigns and interventions.

    - **Distribution of Incidents by Collision Manner**: A bar chart linking traffic incidents with factors like surface conditions and gender, offering insights into injury severity trends.

    - **Injuries and Fatalities Trends**: Displays monthly injuries and fatalities, highlighting seasonal variations and high-risk periods, with unit-type filtering for detailed analysis.

    - **Traffic Crash Location Map**: Combines marker clusters and heatmaps to reveal accident hotspots and severity patterns, guiding safety improvements and urban planning.

    In Part 3, we plan to enhance interactivity by linking visualizations, such as dynamically updating a map and bar chart for a more seamless data exploration experience.

    These tools empower stakeholders to address risks, implement safety measures, and prioritize infrastructure upgrades for safer roads.
    """)



if __name__ == "__main__":
    main()