Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,165 +0,0 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
import streamlit as st
|
3 |
-
import numpy as np
|
4 |
-
from scipy.integrate import odeint
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
from sklearn.metrics import mean_absolute_percentage_error
|
7 |
-
import warnings
|
8 |
-
warnings.filterwarnings("ignore")
|
9 |
-
|
10 |
-
#read files
|
11 |
-
data = pd.read_csv('owid-monkeypox-data.csv')
|
12 |
-
data = data[['location','iso_code','date','new_cases','total_cases','new_deaths','total_deaths']]
|
13 |
-
|
14 |
-
pop = pd.read_csv('API_SP.POP.TOTL_DS2_en_csv_v2_4578059.csv')
|
15 |
-
#preprocessiong data
|
16 |
-
all_location = {}
|
17 |
-
for i in data['iso_code'].unique():
|
18 |
-
all_location[i] = data[data['iso_code'] == i].reset_index(drop=True)
|
19 |
-
|
20 |
-
popu = pop[['Country Code','2021']].to_dict('index')
|
21 |
-
pop_dict = {}
|
22 |
-
for i in popu.values():
|
23 |
-
pop_dict[i['Country Code']] = i['2021']
|
24 |
-
|
25 |
-
pop_dict['GLP'] = 400000
|
26 |
-
pop_dict['MTQ'] = 376480
|
27 |
-
pop_dict['OWID_WRL'] = 7836630792
|
28 |
-
|
29 |
-
code = dict(data.groupby('location')['iso_code'].unique())
|
30 |
-
|
31 |
-
# SIR model differential equations.
|
32 |
-
def deriv(x, t, beta, gamma):
|
33 |
-
s, i, r = x
|
34 |
-
dsdt = -beta * s * i
|
35 |
-
didt = beta * s * i - gamma * i
|
36 |
-
drdt = gamma * i
|
37 |
-
return [dsdt, didt, drdt]
|
38 |
-
|
39 |
-
#plot model
|
40 |
-
def plotdata(t, s, i,r,R0, e=None):
|
41 |
-
# plot the data
|
42 |
-
fig = plt.figure(figsize=(12,6))
|
43 |
-
ax = [fig.add_subplot(221, axisbelow=True),
|
44 |
-
fig.add_subplot(223),
|
45 |
-
fig.add_subplot(122)]
|
46 |
-
|
47 |
-
ax[0].plot(t, s, lw=3, label='Fraction Susceptible')
|
48 |
-
ax[0].plot(t, i, lw=3, label='Fraction Infective')
|
49 |
-
ax[0].plot(t, r, lw=3, label='Recovered')
|
50 |
-
ax[0].set_title('Susceptible and Recovered Populations')
|
51 |
-
ax[0].set_xlabel('Time /days')
|
52 |
-
ax[0].set_ylabel('Fraction')
|
53 |
-
|
54 |
-
ax[1].plot(t, i, lw=3, label='Infective')
|
55 |
-
ax[1].set_title('Infectious Population')
|
56 |
-
if e is not None: ax[1].plot(t, e, lw=3, label='Exposed')
|
57 |
-
ax[1].set_ylim(0, 1.0)
|
58 |
-
ax[1].set_xlabel('Time /days')
|
59 |
-
ax[1].set_ylabel('Fraction')
|
60 |
-
|
61 |
-
ax[2].plot(s, i, lw=3, label='s, i trajectory')
|
62 |
-
ax[2].plot([1/R0, 1/R0], [0, 1], '--', lw=3, label='di/dt = 0')
|
63 |
-
ax[2].plot(s[0], i[0], '.', ms=20, label='Initial Condition')
|
64 |
-
ax[2].plot(s[-1], i[-1], '.', ms=20, label='Final Condition')
|
65 |
-
ax[2].set_title('State Trajectory')
|
66 |
-
ax[2].set_aspect('equal')
|
67 |
-
ax[2].set_ylim(0, 1.05)
|
68 |
-
ax[2].set_xlim(0, 1.05)
|
69 |
-
ax[2].set_xlabel('Susceptible')
|
70 |
-
ax[2].set_ylabel('Infectious')
|
71 |
-
|
72 |
-
for a in ax:
|
73 |
-
a.grid(True)
|
74 |
-
a.legend()
|
75 |
-
|
76 |
-
plt.tight_layout()
|
77 |
-
|
78 |
-
return fig
|
79 |
-
|
80 |
-
def compare_plt(country,i,pop):
|
81 |
-
fig = plt.figure(figsize=(12,6))
|
82 |
-
ax = [fig.add_subplot(121, axisbelow=True),fig.add_subplot(122)]
|
83 |
-
ax[0].set_title('Monkeypox confirmed cases')
|
84 |
-
ax[0].plot(all_location[country]['total_cases'],lw=3,label='Infective')
|
85 |
-
ax[0].set_xlabel('Days')
|
86 |
-
ax[0].set_ylabel('Number of cases')
|
87 |
-
ax[0].legend()
|
88 |
-
|
89 |
-
scaler = all_location[country]['total_cases'].apply(lambda x : x/pop)
|
90 |
-
ax[1].set_title('Monkeypox confirmed cases compare with model')
|
91 |
-
ax[1].plot(scaler,lw=3,label='Real Infective')
|
92 |
-
ax[1].plot(i,lw=3,label='SIR model Infective')
|
93 |
-
ax[1].set_ylim(0,0.00005)
|
94 |
-
ax[1].set_xlim(0,200)
|
95 |
-
ax[1].set_xlabel('Days')
|
96 |
-
ax[1].set_ylabel('Fraction Number of cases')
|
97 |
-
ax[1].legend()
|
98 |
-
plt.tight_layout()
|
99 |
-
|
100 |
-
return fig
|
101 |
-
|
102 |
-
#final model
|
103 |
-
def SIR(country,R0,t_infective,pop):
|
104 |
-
#R0 = 0.57 - 1.25
|
105 |
-
|
106 |
-
# parameter values
|
107 |
-
R0 = R0
|
108 |
-
t_infective = t_infective
|
109 |
-
|
110 |
-
# initial number of infected and recovered individuals
|
111 |
-
i_initial = all_location[country]['total_cases'].iloc[0]/pop
|
112 |
-
r_initial = 0.00
|
113 |
-
s_initial = 1 - i_initial - r_initial
|
114 |
-
|
115 |
-
gamma = 1/t_infective
|
116 |
-
beta = R0*gamma
|
117 |
-
|
118 |
-
t = np.linspace(0, 3000, 3000)
|
119 |
-
x_initial = s_initial, i_initial, r_initial
|
120 |
-
soln = odeint(deriv, x_initial, t, args=(beta, gamma))
|
121 |
-
s, i, r = soln.T
|
122 |
-
e = None
|
123 |
-
|
124 |
-
scaler = all_location[country]['total_cases'].apply(lambda x : x/pop)
|
125 |
-
rangee = len(all_location[country]['total_cases'])
|
126 |
-
rmpe = mean_absolute_percentage_error(scaler,i[0:rangee])*100
|
127 |
-
|
128 |
-
|
129 |
-
return R0,t_infective,beta,gamma,rmpe,plotdata(t, s, i,r,R0),compare_plt(country,i,pop)
|
130 |
-
|
131 |
-
def main():
|
132 |
-
st.title("SIR Model for Monkeypox in Thailand")
|
133 |
-
st.subheader("Latest updated : 10/02/2023")
|
134 |
-
st.subheader("Reference : https://jckantor.github.io/CBE30338/03.09-COVID-19.html")
|
135 |
-
st.caption("Display graph of SIR model of monkeypox and comparison between the model and actual data. Try to find the best R0 that fit for the actual data (lowest MAPE).")
|
136 |
-
|
137 |
-
with st.form("questionaire"):
|
138 |
-
recovery = st.slider("How long Monkeypox last until recovery(days)? ", 14, 31, 21)
|
139 |
-
R0 = st.slider("Basic Reproduction Number (R0)", 0.57, 3.00, 0.57)# user's input
|
140 |
-
country_code = code["Thailand"][0]
|
141 |
-
pop = pop_dict[country_code]
|
142 |
-
|
143 |
-
# clicked==True only when the button is clicked
|
144 |
-
clicked = st.form_submit_button("Show Graph")
|
145 |
-
if clicked:
|
146 |
-
|
147 |
-
# Show SIR
|
148 |
-
SIR_param = SIR(country_code,R0,recovery,pop)
|
149 |
-
|
150 |
-
if SIR_param[0] <= 1:
|
151 |
-
a = 'No epidemic.'
|
152 |
-
else:
|
153 |
-
a = 'Epidemic has began.'
|
154 |
-
|
155 |
-
st.pyplot(SIR_param[-2])
|
156 |
-
st.pyplot(SIR_param[-1])
|
157 |
-
st.success("SIR model parameters of Thailand "+" is")
|
158 |
-
st.success("R0 (Basic Reproduction Number) = "+str(SIR_param[0])+' '+a)
|
159 |
-
st.success("Beta (Rate of transmission) = "+str(round(SIR_param[2],3)))
|
160 |
-
st.success("Gamma (Rate of Recovery) = "+str(round(SIR_param[3],3)))
|
161 |
-
st.success("MAPE = "+str(round(SIR_param[4],3))+"%")
|
162 |
-
|
163 |
-
# Run main()
|
164 |
-
if __name__ == "__main__":
|
165 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|