File size: 7,048 Bytes
270ae03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import plotly.express as px
from datetime import datetime

excel_file_name = 'updated_dataset.csv'
# Streamlit title
st.title("Bangladesh Accident Monitoring System (BAMS)")


# Display a note to the user
st.write("Please Note, First Date must be smaller than Last date. Example: First Date = 25-08-2024 and Last Date = 28-08-2024")

# Get today's date
today = datetime.strptime(datetime.today().strftime('%d-%m-%Y'), '%d-%m-%Y')

# Input fields for date range
start = st.date_input("Enter first date", max_value=today, format="DD-MM-YYYY")
start_string = start.strftime('%d-%m-%Y')
end = st.date_input("Enter last date", max_value=today, format="DD-MM-YYYY")
end_string = end.strftime('%d-%m-%Y')

# Button to generate dataset based on date range
if st.button("Generate Dataset"):
    # Read the selected excel file
    df3 = pd.read_csv(excel_file_name)

    # Convert 'Publish Date' column to datetime with 'day-month-year' format
    df3['Publish Date'] = pd.to_datetime(df3['Publish Date'], format='%d-%m-%Y')

    # Fixing date formats
    for i in range(len(df3)):
        if '/' in df3['Accident Date'][i]:
            day=int(df3['Accident Date'][i].split('/')[0])
            mon=int(df3['Accident Date'][i].split('/')[1])
            yr=int(df3['Accident Date'][i].split('/')[2])
            df3['Publish Date'][i]=f"{day}-{mon}-{yr}"
    print(df3.tail())
    # Convert user input dates to datetime
    start_date = pd.to_datetime(start_string, format='%d-%m-%Y')
    end_date = pd.to_datetime(end_string, format='%d-%m-%Y')

    # Filter rows based on the specified date range
    filtered_entries = df3[(df3['Publish Date'] >= start_date) & (df3['Publish Date'] <= end_date)]
    filtered_entries.reset_index(inplace=True, drop=True)

    # Display the filtered data
    st.dataframe(filtered_entries)
    # Create a bar chart for accident count over days
    if not filtered_entries.empty:
        # Create a bar chart for accident count over days
        if not filtered_entries.empty:
            import plotly.express as px

            # Convert 'Accident Date' to datetime format
            filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')

            # Count accidents per date and sort by date
            accident_counts = filtered_entries['Accident Date'].value_counts().sort_index()

            # Reset the index and rename columns
            accident_counts = accident_counts.reset_index()
            accident_counts.columns = ['Accident Date', 'Accident Count']

            # Convert 'Accident Date' back to string format
            accident_counts['Accident Date'] = accident_counts['Accident Date'].dt.strftime('%d-%m-%Y')
            filtered_entries['Accident Date'] = accident_counts['Accident Date']
            fig1 = px.bar(accident_counts, 
                        x='Accident Date', 
                        y='Accident Count', 
                        title="Accident Count Over Days",
                        labels={'Accident Date': 'Date', 'Accident Count': 'Number of Accidents'},
                        color='Accident Count',
                        color_continuous_scale='Viridis')
            st.plotly_chart(fig1)
        # Convert 'Accident Date' to datetime format
            filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')

            # Group by 'Accident Date' and sum the 'Killed' column
            killed_per_day = filtered_entries.groupby('Accident Date')['Killed'].sum().reset_index()
            killed_per_day.columns = ['Accident Date', 'Total Killed']

            # Sort the dates in ascending order
            killed_per_day = killed_per_day.sort_values(by='Accident Date')

            # Convert 'Accident Date' back to string format
            killed_per_day['Accident Date'] = killed_per_day['Accident Date'].dt.strftime('%d-%m-%Y')

            fig2 = px.bar(killed_per_day, 
                            x='Accident Date', 
                            y='Total Killed', 
                            title="Number of People Killed Each Day",
                            labels={'Accident Date': 'Date', 'Total Killed': 'Number of People Killed'},
                            color='Total Killed',
                            color_continuous_scale='Reds')
            st.plotly_chart(fig2)

            # Bar chart showing the number of accidents in each district
            district_accidents = filtered_entries['District'].value_counts().reset_index()
            district_accidents.columns = ['District', 'Number of Accidents']
            fig3 = px.bar(district_accidents, 
                            x='District', 
                            y='Number of Accidents',
                            title="Accidents in Each District",
                            labels={'Number of Accidents': 'Number of Accidents', 'District': 'District'},
                            color='Number of Accidents',
                            color_continuous_scale='Cividis')
            st.plotly_chart(fig3)

            ### Pie Chart Code ###
            yes_count=0
            no_count=0
            not_available_count=0
            for i in range(len(filtered_entries)):
                if ('Yes' in filtered_entries['Pedestrian_Involved'][i] or 'yes' in filtered_entries['Pedestrian_Involved'][i]): yes_count+=1
                if ('No' in filtered_entries['Pedestrian_Involved'][i] or 'no' in filtered_entries['Pedestrian_Involved'][i]): no_count+=1
                if ('Not Available' in filtered_entries['Pedestrian_Involved'][i]): not_available_count+=1
            Pedestrian_Involved_list = ['Yes', 'No', 'Not Available']
            Count_list = [yes_count, no_count, not_available_count]
            # dictionary of lists 
            dict = {'Pedestrian Involved': Pedestrian_Involved_list, 'Count':Count_list} 
            pedestrian_involvement = pd.DataFrame(dict)
            # Pie chart showing the percentage of accidents involving pedestrians vs. those that don't
            # pedestrian_involvement = filtered_entries['Pedestrian_Involved'].value_counts().reset_index()
            # pedestrian_involvement.columns = ['Pedestrian Involved', 'Count']

            fig4 = px.pie(pedestrian_involvement, 
                            names='Pedestrian Involved', 
                            values='Count', 
                            title="Accidents Involving Pedestrians",
                            labels={'Pedestrian Involved': 'Pedestrian Involved'},
                            color_discrete_sequence=['Green', 'Red', 'Blue'])
            st.plotly_chart(fig4)
        
    else:
        st.write("No data available for the selected date range.")

# Display selected start and end dates
st.write("Start date is:", start)
st.write("End date is:", end)