-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
211 lines (174 loc) · 7.54 KB
/
app.py
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
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.metrics import mean_squared_error, r2_score
import seaborn as sns
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose
st.set_page_config(page_title="Diarrhea Analysis Dashboard", layout="wide")
st.title("Bangladesh Regional Diarrhea Analysis & Prediction Dashboard")
st.markdown("""
This dashboard presents a comprehensive analysis of diarrhea cases across four major divisions
in Bangladesh, examining the relationship between environmental factors and disease occurrence.
""")
# Data Loading Function
@st.cache_data
def load_data():
# Loading the datasets
rajshahi = pd.read_csv('datasets/Rajshahi.csv')
khulna = pd.read_csv('datasets/Khulna.csv')
dhaka = pd.read_csv('datasets/Dhaka.csv')
chattogram = pd.read_csv('datasets/Chattogram.csv')
# Converting dates
for df in [rajshahi, khulna, dhaka, chattogram]:
df['Date'] = pd.to_datetime(df['Date'])
# Adding division labels
rajshahi['Division'] = 'Rajshahi'
khulna['Division'] = 'Khulna'
dhaka['Division'] = 'Dhaka'
chattogram['Division'] = 'Chattogram'
# Combining data
combined_df = pd.concat([rajshahi, khulna, dhaka, chattogram])
return combined_df, rajshahi, khulna, dhaka, chattogram
# Load data
combined_df, rajshahi, khulna, dhaka, chattogram = load_data()
# Sidebar
st.sidebar.title("Navigation")
page = st.sidebar.radio("Select Page",
["Overview", "Time Series Analysis", "Correlation Analysis", "Prediction Model"])
if page == "Overview":
st.header("Regional Overview")
# Summary statistics
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Records", len(combined_df))
with col2:
st.metric("Date Range", f"{combined_df['Date'].min().year} - {combined_df['Date'].max().year}")
with col3:
st.metric("Average Cases", int(combined_df['Diarrhea'].mean()))
with col4:
st.metric("Max Cases", int(combined_df['Diarrhea'].max()))
# Time series plot
st.subheader("Diarrhea Cases Across Divisions")
fig = px.line(combined_df, x='Date', y='Diarrhea', color='Division',
title='Diarrhea Cases Over Time by Division')
st.plotly_chart(fig, use_container_width=True)
# Distribution plot
st.subheader("Distribution of Cases by Division")
fig = px.box(combined_df, x='Division', y='Diarrhea',
title='Distribution of Diarrhea Cases by Division')
st.plotly_chart(fig, use_container_width=True)
elif page == "Time Series Analysis":
st.header("Time Series Analysis")
# Division selector
division = st.selectbox("Select Division",
['Rajshahi', 'Khulna', 'Dhaka', 'Chattogram'])
# Get corresponding dataframe
df_dict = {'Rajshahi': rajshahi, 'Khulna': khulna,
'Dhaka': dhaka, 'Chattogram': chattogram}
df = df_dict[division]
# Time series decomposition
st.subheader("Seasonal Decomposition")
decomposition = seasonal_decompose(df['Diarrhea'], period=30)
# Plot components
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(12, 10))
decomposition.observed.plot(ax=ax1)
ax1.set_title('Observed')
decomposition.trend.plot(ax=ax2)
ax2.set_title('Trend')
decomposition.seasonal.plot(ax=ax3)
ax3.set_title('Seasonal')
decomposition.resid.plot(ax=ax4)
ax4.set_title('Residual')
plt.tight_layout()
st.pyplot(fig)
elif page == "Correlation Analysis":
st.header("Correlation Analysis")
# Division selector
division = st.selectbox("Select Division",
['Rajshahi', 'Khulna', 'Dhaka', 'Chattogram'])
# Get corresponding dataframe
df_dict = {'Rajshahi': rajshahi, 'Khulna': khulna,
'Dhaka': dhaka, 'Chattogram': chattogram}
df = df_dict[division]
# Correlation matrix
correlation = df[['Diarrhea', 'Minimum Temperature', 'Maximum Temperature',
'Humidity', 'Preceptation']].corr()
# Heatmap
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(correlation, annot=True, cmap='coolwarm', ax=ax)
plt.title(f'Correlation Matrix for {division}')
st.pyplot(fig)
# Scatter plots
st.subheader("Feature Relationships")
feature = st.selectbox("Select Feature",
['Minimum Temperature', 'Maximum Temperature',
'Humidity', 'Preceptation'])
fig = px.scatter(df, x=feature, y='Diarrhea',
trendline="ols",
title=f'{feature} vs Diarrhea Cases')
st.plotly_chart(fig, use_container_width=True)
elif page == "Prediction Model":
st.header("Prediction Model")
# Model selection
model_type = st.selectbox("Select Model",
["Random Forest", "Gradient Boosting"])
# Feature selection
st.subheader("Select Features for Prediction")
use_min_temp = st.checkbox("Minimum Temperature", value=True)
use_max_temp = st.checkbox("Maximum Temperature", value=True)
use_humidity = st.checkbox("Humidity", value=True)
use_precip = st.checkbox("Precipitation", value=True)
# Prepare features
features = []
if use_min_temp:
features.append('Minimum Temperature')
if use_max_temp:
features.append('Maximum Temperature')
if use_humidity:
features.append('Humidity')
if use_precip:
features.append('Preceptation')
if len(features) > 0:
X = combined_df[features]
y = combined_df['Diarrhea']
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=42)
# Train model
if model_type == "Random Forest":
model = RandomForestRegressor(random_state=42)
else:
model = GradientBoostingRegressor(random_state=42)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Display metrics
col1, col2 = st.columns(2)
with col1:
st.metric("R² Score", round(r2_score(y_test, y_pred), 3))
with col2:
st.metric("RMSE", round(np.sqrt(mean_squared_error(y_test, y_pred)), 3))
# Plot actual vs predicted
fig = px.scatter(x=y_test, y=y_pred,
labels={'x': 'Actual Cases', 'y': 'Predicted Cases'},
title='Actual vs Predicted Diarrhea Cases')
fig.add_trace(px.line(x=[y_test.min(), y_test.max()],
y=[y_test.min(), y_test.max()]).data[0])
st.plotly_chart(fig, use_container_width=True)
# Feature importance
if len(features) > 1:
importance_df = pd.DataFrame({
'Feature': features,
'Importance': model.feature_importances_
}).sort_values('Importance', ascending=True)
fig = px.bar(importance_df, x='Importance', y='Feature',
title='Feature Importance',
orientation='h')
st.plotly_chart(fig, use_container_width=True)
else:
st.warning("Please select at least one feature for prediction.")