diff --git a/modelhub/notebooks/19_05_2020_Africa_SEIRD.ipynb b/modelhub/notebooks/19_05_2020_Africa_SEIRD.ipynb new file mode 100644 index 0000000..d773e18 --- /dev/null +++ b/modelhub/notebooks/19_05_2020_Africa_SEIRD.ipynb @@ -0,0 +1,465 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "from scipy.integrate import odeint\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "%matplotlib inline \n", + "\n", + "#!pip install mpld3\n", + "import mpld3\n", + "mpld3.enable_notebook()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def plotseird(t, S, E, I, R, D=None, L=None, R0=None, Alpha=None, CFR=None):\n", + " f, ax = plt.subplots(1,1,figsize=(10,4))\n", + " ax.plot(t, S, 'b', alpha=0.7, linewidth=2, label='Susceptible')\n", + " #ax.plot(t, E, 'y', alpha=0.7, linewidth=2, label='Exposed')\n", + " ax.plot(t, I, 'r', alpha=0.7, linewidth=2, label='Infected')\n", + " ax.plot(t, R, 'g', alpha=0.7, linewidth=2, label='Recovered')\n", + " if D is not None:\n", + " ax.plot(t, D, 'k', alpha=0.7, linewidth=2, label='Dead')\n", + " ax.plot(t, S+E+I+R+D, 'c--', alpha=0.7, linewidth=2, label='Total')\n", + " else:\n", + " ax.plot(t, S+E+I+R, 'c--', alpha=0.7, linewidth=2, label='Total')\n", + "\n", + " ax.set_xlabel('Time (days)')\n", + "\n", + " ax.yaxis.set_tick_params(length=0)\n", + " ax.xaxis.set_tick_params(length=0)\n", + " ax.grid(b=True, which='major', c='w', lw=2, ls='-')\n", + " legend = ax.legend(borderpad=2.0)\n", + " legend.get_frame().set_alpha(0.5)\n", + " for spine in ('top', 'right', 'bottom', 'left'):\n", + " ax.spines[spine].set_visible(False)\n", + " if L is not None:\n", + " plt.title(\"Lockdown after {} days\".format(L))\n", + " plt.show();\n", + "\n", + " if R0 is not None or CFR is not None:\n", + " f = plt.figure(figsize=(12,4))\n", + "\n", + " if R0 is not None:\n", + " # sp1\n", + " ax1 = f.add_subplot(121)\n", + " ax1.plot(t, R0, 'b--', alpha=0.7, linewidth=2, label='R_0')\n", + "\n", + " ax1.set_xlabel('Time (days)')\n", + " ax1.title.set_text('R_0 over time')\n", + " # ax.set_ylabel('Number (1000s)')\n", + " # ax.set_ylim(0,1.2)\n", + " ax1.yaxis.set_tick_params(length=0)\n", + " ax1.xaxis.set_tick_params(length=0)\n", + " ax1.grid(b=True, which='major', c='w', lw=2, ls='-')\n", + " legend = ax1.legend()\n", + " legend.get_frame().set_alpha(0.5)\n", + " for spine in ('top', 'right', 'bottom', 'left'):\n", + " ax.spines[spine].set_visible(False)\n", + "\n", + " if Alpha is not None:\n", + " # sp2\n", + " ax2 = f.add_subplot(122)\n", + " ax2.plot(t, Alpha, 'r--', alpha=0.7, linewidth=2, label='alpha')\n", + "\n", + " ax2.set_xlabel('Time (days)')\n", + " ax2.title.set_text('fatality rate over time')\n", + " # ax.set_ylabel('Number (1000s)')\n", + " # ax.set_ylim(0,1.2)\n", + " ax2.yaxis.set_tick_params(length=0)\n", + " ax2.xaxis.set_tick_params(length=0)\n", + " ax2.grid(b=True, which='major', c='w', lw=2, ls='-')\n", + " legend = ax2.legend()\n", + " legend.get_frame().set_alpha(0.5)\n", + " for spine in ('top', 'right', 'bottom', 'left'):\n", + " ax.spines[spine].set_visible(False)\n", + "\n", + " plt.show();" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Create SEIR PDEs\n", + "\n", + "def deriv(y, t, N, beta, gamma, delta, alpha, rho):\n", + " S, E, I, R, D = y\n", + " dSdt = -beta * S * I / N\n", + " dEdt = beta * S * I / N - delta * E\n", + " dIdt = delta * E - (1 - alpha) * gamma * I - alpha * rho * I\n", + " dRdt = (1 - alpha) * gamma * I\n", + " dDdt = alpha * rho * I\n", + " return dSdt, dEdt, dIdt, dRdt, dDdt" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# Load infections, deaths and recover data into dataframes\n", + "\n", + "import pandas as pd\n", + "\n", + "africa_infections_url = \"https://raw.githubusercontent.com/dsfsi/covid19africa/master/data/time_series/africa_daily_time_series_cases.csv\"\n", + "africa_deaths_url = \"https://raw.githubusercontent.com/dsfsi/covid19africa/master/data/time_series/africa_daily_time_series_deaths.csv\"\n", + "africa_recoveries_url = \"https://raw.githubusercontent.com/dsfsi/covid19africa/master/data/time_series/africa_daily_time_series_recovered.csv\"\n", + "\n", + "africa_infections_df = pd.read_csv(africa_infections_url).transpose()\n", + "africa_infections_df.columns = africa_infections_df.iloc[0]\n", + "africa_infections_df = africa_infections_df.drop(\"Country/Region\")\n", + "africa_infections_df = africa_infections_df.drop([\"Lat\",\"Long\"])\n", + "\n", + "africa_deaths_df = pd.read_csv(africa_deaths_url).transpose()\n", + "africa_deaths_df.columns = africa_deaths_df.iloc[0]\n", + "africa_deaths_df = africa_deaths_df.drop(\"Country/Region\")\n", + "africa_deaths_df = africa_deaths_df.drop([\"Lat\",\"Long\"])\n", + "\n", + "africa_recoveries_df = pd.read_csv(africa_recoveries_url).transpose()\n", + "africa_recoveries_df.columns = africa_recoveries_df.iloc[0]\n", + "africa_recoveries_df = africa_recoveries_df.drop(\"Country/Region\")\n", + "africa_recoveries_df = africa_recoveries_df.drop([\"Lat\",\"Long\"])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Country/Region | \n", + "Algeria | \n", + "Angola | \n", + "Benin | \n", + "Burkina Faso | \n", + "Cabo Verde | \n", + "Cameroon | \n", + "CAR | \n", + "Chad | \n", + "Congo | \n", + "DRC | \n", + "... | \n", + "Uganda | \n", + "Zambia | \n", + "Zimbabwe | \n", + "Botswana | \n", + "Burundi | \n", + "Sierra Leone | \n", + "Malawi | \n", + "South Sudan | \n", + "Western Sahara | \n", + "Sao Tome and Principe | \n", + "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2020-01-22 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "... | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "
2020-01-23 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "... | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "
2020-01-24 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "... | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "
2020-01-25 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "... | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "
2020-01-26 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "... | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "0 | \n", + "
5 rows × 53 columns
\n", + "