Jiranuwat commited on
Commit
85e83d0
·
1 Parent(s): 6cf7faa

Upload App.py

Browse files
Files changed (1) hide show
  1. App.py +114 -0
App.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
7
+ #dowload file
8
+ !wget https://raw.githubusercontent.com/owid/monkeypox/main/owid-monkeypox-data.csv
9
+
10
+ #read files
11
+ data = pd.read_csv('owid-monkeypox-data.csv')
12
+ data = data[['location','date','new_cases','total_cases','new_deaths','total_deaths']]
13
+
14
+ #preprocessiong data
15
+ all_location = {}
16
+ for i in data['location'].unique():
17
+ all_location[i] = data[data['location'] == i].reset_index(drop=True)
18
+
19
+ # SIR model differential equations.
20
+ def deriv(x, t, beta, gamma):
21
+ s, i, r = x
22
+ dsdt = -beta * s * i
23
+ didt = beta * s * i - gamma * i
24
+ drdt = gamma * i
25
+ return [dsdt, didt, drdt]
26
+
27
+ #plot model
28
+ def plotdata(t, s, i, e=None):
29
+ # plot the data
30
+ fig = plt.figure(figsize=(12,6))
31
+ ax = [fig.add_subplot(221, axisbelow=True),
32
+ fig.add_subplot(223),
33
+ fig.add_subplot(122)]
34
+
35
+ ax[0].plot(t, s, lw=3, label='Fraction Susceptible')
36
+ ax[0].plot(t, i, lw=3, label='Fraction Infective')
37
+ ax[0].plot(t, r, lw=3, label='Recovered')
38
+ ax[0].set_title('Susceptible and Recovered Populations')
39
+ ax[0].set_xlabel('Time /days')
40
+ ax[0].set_ylabel('Fraction')
41
+
42
+ ax[1].plot(t, i, lw=3, label='Infective')
43
+ ax[1].set_title('Infectious Population')
44
+ if e is not None: ax[1].plot(t, e, lw=3, label='Exposed')
45
+ ax[1].set_ylim(0, 1.0)
46
+ ax[1].set_xlabel('Time /days')
47
+ ax[1].set_ylabel('Fraction')
48
+
49
+ ax[2].plot(s, i, lw=3, label='s, i trajectory')
50
+ ax[2].plot([1/R0, 1/R0], [0, 1], '--', lw=3, label='di/dt = 0')
51
+ ax[2].plot(s[0], i[0], '.', ms=20, label='Initial Condition')
52
+ ax[2].plot(s[-1], i[-1], '.', ms=20, label='Final Condition')
53
+ ax[2].set_title('State Trajectory')
54
+ ax[2].set_aspect('equal')
55
+ ax[2].set_ylim(0, 1.05)
56
+ ax[2].set_xlim(0, 1.05)
57
+ ax[2].set_xlabel('Susceptible')
58
+ ax[2].set_ylabel('Infectious')
59
+
60
+ for a in ax:
61
+ a.grid(True)
62
+ a.legend()
63
+
64
+ plt.tight_layout()
65
+
66
+ #final model
67
+ def SIR(country,t_infective):
68
+ # parameter values
69
+ R0 = (all_location[country]['new_cases'].sum()/len(all_location[country]['date'].unique()))/t_infective
70
+ t_infective = t_infective
71
+
72
+ # initial number of infected and recovered individuals
73
+ i_initial = 1/20000
74
+ r_initial = 0.00
75
+ s_initial = 1 - i_initial - r_initial
76
+
77
+ gamma = 1/t_infective
78
+ beta = R0*gamma
79
+
80
+ t = np.linspace(0, 100, 1000)
81
+ x_initial = s_initial, i_initial, r_initial
82
+ soln = odeint(deriv, x_initial, t, args=(beta, gamma))
83
+ s, i, r = soln.T
84
+ e = None
85
+
86
+ plotdata(t, s, i)
87
+
88
+ return R0,t_infective,gamma,beta
89
+
90
+ def main():
91
+ st.title("SIR Model for Monkeypox")
92
+
93
+ with st.form("questionaire"):
94
+ country = st.selectbox("Country")# user's input
95
+ recovery = st.slider("How long Monkeypox recover?", 21, 31, 21)# user's input
96
+
97
+ # clicked==True only when the button is clicked
98
+ clicked = st.form_submit_button("Show Graph")
99
+ if clicked:
100
+
101
+ #show total cases graph
102
+ all_location[country]['total_cases'].plot()
103
+
104
+ # Show SIR
105
+ SIR_param = SIR(country,recovery)
106
+
107
+ st.success("SIR model parameters for "+str(country)+" is")
108
+ st.success("R0 = "+str(SIR_param[0]))
109
+ st.success("Beta = "+str(SIR_param[3]))
110
+ st.success("Gamma = "+str(SIR_param[2]))
111
+
112
+ # Run main()
113
+ if __name__ == "__main__":
114
+ main()