#My model to describe the number of Corona cases in Germany, from data in March 2020
#Comparison with the data from JHU github
#Gerald Schuller, April 2020

import numpy as np
import matplotlib.pyplot as plt

def logistic(K, valueondate, factorincrease, t):
   #logistic function for exponential growth with saturation at population size K
   #starting from a value on a given date "valueondate"
   #factor for the increase from day to day is "factorincrease"
   #prediction for the number of days "t" after the given day,
   #prediction before the given date is obtained by using negative values for t
   return K/(1+ ((K-valueondate)/valueondate)*np.exp(-np.log(factorincrease)*t ))
   
#Read data from github:
import urllib.request
import pandas as pd

Retrievedata=False

#Cases
if Retrievedata:
   url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
   urllib.request.urlretrieve(url, './corona_cases.csv')
#('./corona_cases.csv', <http.client.HTTPMessage object at 0x7fc7d89c0f98>)

df = pd.read_csv('./corona_cases.csv')
#df.head()
df=df.drop(['Lat','Long','Province/State'], axis=1)
df=df.set_index('Country/Region')
countries = ['Italy', 'Germany', 'Spain', 'Brazil', 'US', 'Russia','Korea, South']
print("countries=", countries)
data=df.loc[countries,: ]
#data
#starting MARCH 1:
data.iloc[0,37:] #detected cases Italy
#Germanycases = data.iloc[1,37:].reset_index(drop=True)
Germanycases = data.loc['Germany','2/29/20':'3/31/20'].reset_index(drop=True)
Germanycases= np.array(Germanycases)
print("Cases March:", data.loc['Germany','2/29/20':'3/31/20']) #detected cases Germany
#print("Germany cases", Germanycases)

#Recovered
if Retrievedata:
   url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv'
   urllib.request.urlretrieve(url, './corona_recovered.csv')
df_recovered = pd.read_csv('./corona_recovered.csv')
df_recovered=df_recovered.drop(['Lat','Long','Province/State'], axis=1)
df_recovered=df_recovered.set_index('Country/Region')

data_recovered=df_recovered.loc[countries,: ]
Germanyrecovered=data_recovered.loc['Germany','2/29/20':'3/31/20'].reset_index(drop=True)
#print("data_recovered March", data_recovered.iloc[1,39:])
print("Germanyrecovered", data_recovered.loc['Germany','2/29/20':'3/31/20'])

#Deaths
if Retrievedata:
   url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv'
   urllib.request.urlretrieve(url, './corona_deaths.csv')
df_deaths = pd.read_csv('./corona_deaths.csv')
df_deaths=df_deaths.drop(['Lat','Long','Province/State'], axis=1)
df_deaths=df_deaths.set_index('Country/Region')

data_deaths=df_deaths.loc[countries,: ]
Germanydeaths=data_deaths.loc['Germany','2/29/20':'3/31/20'].reset_index(drop=True)
Germanydeaths=np.array(Germanydeaths)
#print("data_deaths March", data_deaths.iloc[1,39:])
print("Germanydeaths", data_deaths.loc['Germany','2/29/20':'3/31/20'])


#Model for Corona cases:
#639 cases on March 6th, 25% increase per day
#639*(1.25**3) #Days since March 6

factorincrease=1.25  #1.25increase factor day to day
valueondate=639  #639 value on a given day
dayofvalue=6 #the given day of the month of the value
K=83e6 #Population of Germany
timetorecovery=11.5#16 #days from dicovering to recovery

#Plot range in days:
t=np.arange(1,32) #prediction for this range of days of the month. For the next month, use day numbers larger that 30.
plt.plot(t, logistic(K, valueondate, factorincrease, t-dayofvalue) )
plt.plot(t, Germanycases[t])
plt.plot(t, logistic(K, valueondate, factorincrease, t-timetorecovery-dayofvalue) ) #estimated recovered
plt.plot(t, Germanyrecovered[t])

#plt.plot(t, valueondate*(factorincrease**(t-dayofvalue)))
plt.xlabel('Day after beginning of March')
plt.ylabel('Corona Cases')
plt.legend(('Predicted Cases', 'Detected Cases', 'Predicted Recovered Cases', 'Detected Recovered'))
plt.grid()
plt.title('Corona Cases in Germany, from logistic model')
plt.show()

#Plot of currently sick patients
#Difference cases minus recovered
plt.plot(t, logistic(K, valueondate, factorincrease, t-dayofvalue) -logistic(K, valueondate, factorincrease, t-timetorecovery-dayofvalue) )
plt.xlabel('Day after beginning of March')
plt.ylabel('Current Corona Cases')
plt.grid()
plt.title('Currently Sick Cases in Germany, from logistic model')
plt.show()

#Death cases: 2 on march 9, 5 on March 13. 
#20 days back for cases: 18% death rate, compensation for the undiscovered cases
#2*(1.25**4) #todesfaelle, tage seit 9.3.
#valueondate=2  #value on a given day
#dayofvalue=9 #the given day of the month of the value

#plt.plot(t, valueondate*(factorincrease**(t-dayofvalue)))
plt.plot(t,logistic(K, valueondate, factorincrease, t-timetorecovery-dayofvalue)*0.048) #0.07 early, 0.02 long term
plt.plot(t, Germanydeaths[t])
plt.xlabel('Day of March')
plt.ylabel('Accumulated Corona Death Cases')
plt.grid()
plt.legend(('Predicted Deaths', 'Detected Deaths'))
plt.title('Corona Death Cases in Germany, from logistic model')
plt.show()

#Factor of increase to previous day:
plt.plot(t, Germanycases[t]/Germanycases[t-1])
plt.plot(t, Germanydeaths[t]/(Germanydeaths[t-1]))
plt.xlabel('Day of March')
plt.ylabel('Factor of increase')
plt.grid()
plt.legend(('Factor for deteced cases','Factor for deaths')) 
plt.title('Factor of increase to previous day')
#plt.axis([1,31,0, 2])
plt.show()

countrycases=data.loc[countries,'2/29/20':'3/31/20'].reset_index(drop=True)
countrycases=np.array(countrycases)
factorsofincrease=countrycases[:,1:]/countrycases[:,:-1] #factor of increase f for each country
plt.plot(factorsofincrease.T)
plt.legend(countries)
plt.xlabel('Day of March')
plt.ylabel('Factor of increase')
plt.grid()
plt.title('Factor of increase to previous day internationally')
#plt.axis([1,31,0, 2])
plt.show()

