Spaces:
Sleeping
Sleeping
import pandas as pd | |
import streamlit as st | |
import numpy as np | |
from scipy.integrate import odeint | |
import matplotlib.pyplot as plt | |
#dowload file | |
#read files | |
data = pd.read_csv('myfile.csv') | |
data = data[['location','date','new_cases','total_cases','new_deaths','total_deaths']] | |
#preprocessiong data | |
all_location = {} | |
for i in data['location'].unique(): | |
all_location[i] = data[data['location'] == i].reset_index(drop=True) | |
# SIR model differential equations. | |
def deriv(x, t, beta, gamma): | |
s, i, r = x | |
dsdt = -beta * s * i | |
didt = beta * s * i - gamma * i | |
drdt = gamma * i | |
return [dsdt, didt, drdt] | |
#plot model | |
def plotdata(t, s, i,r,R0, e=None): | |
# plot the data | |
fig = plt.figure(figsize=(12,6)) | |
ax = [fig.add_subplot(221, axisbelow=True), | |
fig.add_subplot(223), | |
fig.add_subplot(122)] | |
ax[0].plot(t, s, lw=3, label='Fraction Susceptible') | |
ax[0].plot(t, i, lw=3, label='Fraction Infective') | |
ax[0].plot(t, r, lw=3, label='Recovered') | |
ax[0].set_title('Susceptible and Recovered Populations') | |
ax[0].set_xlabel('Time /days') | |
ax[0].set_ylabel('Fraction') | |
ax[1].plot(t, i, lw=3, label='Infective') | |
ax[1].set_title('Infectious Population') | |
if e is not None: ax[1].plot(t, e, lw=3, label='Exposed') | |
ax[1].set_ylim(0, 1.0) | |
ax[1].set_xlabel('Time /days') | |
ax[1].set_ylabel('Fraction') | |
ax[2].plot(s, i, lw=3, label='s, i trajectory') | |
ax[2].plot([1/R0, 1/R0], [0, 1], '--', lw=3, label='di/dt = 0') | |
ax[2].plot(s[0], i[0], '.', ms=20, label='Initial Condition') | |
ax[2].plot(s[-1], i[-1], '.', ms=20, label='Final Condition') | |
ax[2].set_title('State Trajectory') | |
ax[2].set_aspect('equal') | |
ax[2].set_ylim(0, 1.05) | |
ax[2].set_xlim(0, 1.05) | |
ax[2].set_xlabel('Susceptible') | |
ax[2].set_ylabel('Infectious') | |
for a in ax: | |
a.grid(True) | |
a.legend() | |
plt.tight_layout() | |
st.pyplot(fig) | |
#final model | |
def SIR(country,t_infective): | |
# parameter values | |
R0 = (all_location[country]['new_cases'].sum()/len(all_location[country]['date'].unique()))/t_infective | |
t_infective = t_infective | |
# initial number of infected and recovered individuals | |
i_initial = 1/20000 | |
r_initial = 0.00 | |
s_initial = 1 - i_initial - r_initial | |
gamma = 1/t_infective | |
beta = R0*gamma | |
t = np.linspace(0, 100, 1000) | |
x_initial = s_initial, i_initial, r_initial | |
soln = odeint(deriv, x_initial, t, args=(beta, gamma)) | |
s, i, r = soln.T | |
e = None | |
return R0,t_infective,beta,gamma | |
def main(): | |
st.title("SIR Model for Monkeypox") | |
with st.form("questionaire"): | |
country = st.selectbox("Country",data['location'].unique())# user's input | |
recovery = st.slider("How long Monkeypox recover?", 21, 31, 21)# user's input | |
# clicked==True only when the button is clicked | |
clicked = st.form_submit_button("Show Graph") | |
if clicked: | |
#show total cases graph | |
all_location[country]['total_cases'].plot() | |
# Show SIR | |
SIR_param = SIR(country,recovery) | |
st.success(st.pyplot(all_location[country]['total_cases'])) | |
st.success(st.pyplot(SIR(country,recovery))) | |
st.success("SIR model parameters for "+str(country)+" is") | |
st.success("R0 = "+str(SIR_param[0])) | |
st.success("Beta = "+str(SIR_param[2])) | |
st.success("Gamma = "+str(SIR_param[3])) | |
# Run main() | |
if __name__ == "__main__": | |
main() |