Example #1
0
def getsir(params):

    df_cn2, df_hubei2, df_wuhan, df_ca, df_it, df_sk, df_sg, df_uk = g.main()
    mydf = df_wuhan
    I = mydf.loc[:, 'confirmed'].values
    R = mydf.loc[:, 'cured'].values
    D = mydf.loc[:, 'dead'].values
    N = np.full(len(I), 37.59E+6)
    I = I - R - D
    S = N - I
    t = range(0, len(S))

    Shat, Ihat, Rhat = sir(mydf, *params)
    Dhat = N - Shat - Ihat - Rhat
    # -------------check
    #mat = pd.DataFrame(list(zip(S, I, R)), columns=['S', 'I', 'R'])
    #print(mat)

    # -------------plot things
    # fig, axs = plt.subplots(2)
    # fig.suptitle('Wuhan')
    # plt.figure(11)
    # #axs[0].set_yscale('log')
    # #axs[1].set_yscale('log')
    # #axs[0].plot(t, S, label='S')
    # axs[0].plot(t, I, label='I')
    # axs[0].plot(t, R, label='R')
    # axs[0].legend()
    # #axs[1].plot(t, Shat, label='Shat')
    # axs[1].plot(t, Ihat, label='Ihat')
    # axs[1].plot(t, Rhat, label='Rhat')
    # plt.title('Doing a SIR')
    # axs[1].legend()
    # fig.show()

    #plt.figure()
    #plt.plot(t, S)
    #plt.plot(t, Shat)
    #plt.show()
    return S, I, R, D, Shat, Ihat, Rhat, Dhat
import numpy as np
import matplotlib.pyplot as plt
import getfiles as g
from scipy.optimize import minimize, NonlinearConstraint
from scipy import interpolate
import pandas as pd

global I, R, D, title, population
df_cn2, df_hubei2, df_wuhan, df_ca, df_it, df_sk, df_sg, df_uk = g.main()
mydf = df_wuhan
title = 'Wuhan'
population = 1.18E+7
I = mydf.loc[:, 'confirmed'].values
R = mydf.loc[:, 'cured'].values
D = mydf.loc[:, 'dead'].values
I = I - R - D


def geterror(params):
    """"Returns total errors"""""
    print('params', params)
    S, I, R, D, Shat, Ihat, Rhat, Dhat = getsir(params)      # get key series
    N = params[-1]
    Imax = max(I)
    where = np.where(I == Imax)[0]
    where_hat = np.where(Ihat == max(Ihat))[0]
    error_i = ((Imax - max(Ihat))/Imax)**2
    k = ((where+1)/(where_hat+1))              # +1 to keep denominator non-zero
    t = np.arange(0, len(Rhat))                # these generated by simulated dt
    treal = np.arange(0, len(R))               # these are in days
Example #3
0
#!usr/bin/env python3
import getfiles
import sys
if __name__ == "__main__":
   filters = dict()   
   if len(sys.argv) > 2:
     i = iter(sys.argv[2:])
     filters = dict(zip(i, i))
   if '-r' not in filters:
     filters['-r'] = 0.0
   if '-g' not in filters:
     filters['-g'] = None
   if '-y' not in filters:
     filters['-y'] = 0
   if '-l' not in filters:
     filters['-l'] = 0

   getfiles.main(sys.argv[1],filters)
Example #4
0
def main():
    df_cn2, df_hubei2, df_wuhan, df_ca, df_it, df_sk, df_sg, df_uk = g.main()
    slope, intercept, r_value, p_value, std_err, plt = linlog_fit(df_wuhan, 'only pre-quarantine')
    popt, pcov = sigmoidfit(df_wuhan)
    popt, pcov = optimise()
def getsir(params):

    df_cn2, df_hubei2, df_wuhan, df_ca, df_it, df_sk, df_sg, df_uk = g.main()
    mydf = df_wuhan
    I = mydf.loc[:, 'confirmed'].values
    R = mydf.loc[:, 'cured'].values
    D = mydf.loc[:, 'dead'].values

    N = 37.59E+6
    I = (I - R - D) / N
    S = 1 - I
    R = R / N
    D = D / N
    t = range(0, len(S))

    Shat, Ihat, Rhat = sir(mydf, *params)
    Dhat = 1 - Shat - Ihat - Rhat
    print('maxI:{}, lastDeath:{}'.format(max(I), D[-1]))
    print('maxIhat:{}, lastDHat:{}'.format(max(Ihat), Dhat[-1]))

    fig, axs = plt.subplots(2, 2)
    axs[0, 0].plot(t, S, label='S')
    axs[0, 0].plot(t, Shat, linestyle='--', label='Shat')
    axs[0, 0].set_title('Susceptible')
    axs[0, 0].legend()

    axs[0, 1].plot(t, I, label='I')
    axs[0, 1].plot(t, Ihat, linestyle='--', label='Ihat')
    axs[0, 1].set_title('Infected')
    axs[0, 1].legend()

    axs[1, 0].plot(t, R, label='R')
    axs[1, 0].plot(t, Rhat, linestyle='--', label='Rhat')
    axs[1, 0].set_title('Recovered')
    axs[1, 0].legend()

    axs[1, 1].plot(t, D, label='D')
    axs[1, 1].plot(t, Dhat, linestyle='--', label='Dhat')
    axs[1, 1].set_title('Dead')
    axs[1, 1].legend()

    fig.suptitle('Wuhan')
    fig.show()
    fig.savefig('plots/Wuhan_fitted.png')
    #
    # plt.figure()
    # plt.plot(t, I, label='I', color='r')
    # plt.plot(t, R, label='R', color='g')
    # plt.plot(t, D, label='D', color='k')
    # plt.plot(t, Ihat, label='Ihat', linestyle='--', color='r')
    # plt.plot(t, Rhat, label='Rhat', linestyle='--', color='g')
    # plt.plot(t, Dhat, label='Dhat', linestyle='--', color='k')
    # plt.ylim(1E-10,0.002)
    # #plt.yscale('log')
    # plt.legend()
    # plt.show()

    #plt.figure()
    #plt.plot(t, I, label='I')
    #plt.plot(t, Ihat, label='Ihat')
    #plt.legend()
    #plt.show()
    # -------------check
    #mat = pd.DataFrame(list(zip(S, Shat, I, Ihat, R, Rhat, D, Dhat)), columns=['S', 'Shat', 'I', 'Ihat', 'R', 'Rhat', 'D', 'Dhat'])
    #print(mat)

    return S, I, R, D, Shat, Ihat, Rhat, Dhat
# this is a sheet to simulate the SIR model and inspect its fit with the data
# lots of plots in this sheet to visually inspect fit
import matplotlib.pyplot as plt
import getfiles as g

df_cn2, df_hubei2, df_wuhan, df_ca, df_it, df_sk, df_sg, df_uk = g.main()

print(df_wuhan.head(1))
for df in g.main():
    df.reset_index(inplace=True)
    x = range(0, df.shape[0])
    sus = df.loc[:, 'suspected']
    conf = df.loc[:, 'confirmed']
    recov = df.loc[:, 'cured']
    dead = df.loc[:, 'dead']
    fig, axs = plt.subplots(2, 2)
    print('-----')
    try:
        if not str(df.loc[0, 'provinceCode']) == 'nan':
            title = "%s %d %d" % (df.loc[0, 'countryCode'],
                                  int(df.loc[0, 'provinceCode']),
                                  int(df.loc[0, 'cityCode']))
            fig.suptitle(title)
        else:
            title = "%s" % (df.loc[0, 'countryCode'])
            fig.suptitle(df.loc[0, 'countryCode'])
    except KeyError:
        title = "%s" % (df.loc[0, 'countryCode'])
        fig.suptitle(df.loc[0, 'countryCode'])
    print(title)
    axs[0, 0].plot(x, sus)