{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Covid19 Projection\n", "The purpose of this notebook is to run Covid-19 case projections at State or Country levels. The outcome is the projection of the total confirmed cases for the target geography. \n", "\n", "This solution first tries to understand the approximate time to peak for the target entity (state/country), expected case rates and higher/lower bounds. It determines these parameters from countries that have exhibited similar trends in the past. \n", "\n", "Next, it determines the best simulation parameters in terms of transmission probability and weeks to peak by running the simulator iteratively with values from a parameter space.\n", "\n", "Finally, it runs a simulation with the optimized parameters to generate day by day case projections. The simulation assumes 2 waves of infection surges following gaussian distribution and tries to apply that in generating the case projections. It also incorporates various factors like transmission probability, testing efficiency, intervention impacts etc.\n", "\n", "Note: interventions_scorer.ipynb should be excuted prior to running the projections on new data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -r requirements.txt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.insert(1, 'src')\n", "import pandas as pd\n", "import numpy as np\n", "import random\n", "import config\n", "import state_data_loader\n", "import country_data_loader\n", "import urllib.request\n", "import os\n", "\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")\n", "\n", "pd.set_option('display.max_rows', 500)\n", "pd.set_option('display.max_columns', 500)\n", "pd.set_option('display.width', 1000)\n", "from simulation_orchestrator import run" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This simulation works best for a timeline when the infection is trending upward, i.e. it is able to find a greater than 1 transmission-rate and a postive time-to-peak from the recent data. As samples, we have used a few states from India and USA as the country for demonstration." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fetch latest data for target States / Countries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Set this flag to True if you want to download the latest COVID19 cases data from respective web sources\n", "LOAD_LATEST_DATA = True\n", "\n", "# * * * Run interventions_scorer.ipynb everytime you download the latest data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if LOAD_LATEST_DATA:\n", " \n", " # Function to refresh the local data file with the latest version from the web\n", " def download_latest_data (url, local_file):\n", " with urllib.request.urlopen(url) as response, open(local_file, 'wb') as out_file:\n", " data = response.read() # a `bytes` object\n", " out_file.write(data)\n", " \n", " # Mapping of online vs offline file locations to refresh\n", " online_offline_data = list()\n", " # Confirmed cases data maintained by Johns Hopkins University\n", " online_offline_data.append((config.confirmed_cases_global_online, \n", " os.path.join(config.base_data_dir, config.confirmed_cases_global_offline)))\n", " # Recovered cases data maintained by Johns Hopkins University\n", " online_offline_data.append((config.recovered_cases_global_online, \n", " os.path.join(config.base_data_dir, config.recovered_cases_global_offline)))\n", " # Deceased cases data maintained by Johns Hopkins University\n", " online_offline_data.append((config.deceased_cases_global_online, \n", " os.path.join(config.base_data_dir, config.deceased_cases_global_offline)))\n", " # Indian states specific cases maintained by COVID19INDIA (www.covid19india.org)\n", " online_offline_data.append((config.india_states_cases_online, \n", " os.path.join(config.base_data_dir, config.india_states_cases_offline)))\n", " \n", " # Refresh the local data files with the latest versions from respective web sources\n", " for path_pair in online_offline_data:\n", " try:\n", " download_latest_data (path_pair[0], path_pair[1])\n", " print ('Downloaded latest data from: {}'.format(path_pair[0]))\n", " except Exception as e:\n", " print ('Error while downloading {}: {}'.format(path_pair[0], e.__class__))\n", " \n", " # Transform and write the coutry specific data for further processing\n", " country_data_loader.load()\n", " \n", " target_states = ['KA', 'KL', 'MH', 'GJ', 'WB']\n", " # Transform and write the Indian states specific data for further processing\n", " state_data_loader.load('India', target_states)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Important parameters:\n", "\n", "#### projection_days\n", "- Number of days to project confirmed COVID19 cases for, including the test_days.\n", "\n", "#### country_code, state, state_population, actual_testing_capacity\n", "- Target location specific parameters\n", "- state, state_population parameters to represent the target country when projecting for a country\n", "\n", "##### These parameters should be configured based on the target location and current day before running the projections.\n", "\n", "*** Additional configurable parameters can be managed in src/config.python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "config.optimization_trials_low = 40" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Projections for Countries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'USA', 'US', 311556874, 700000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'CAN', 'Canada', 37000000, 90000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'ARG', 'Argentina', 44044811, 4000\n", "future_projection_days = 120\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'ITA', 'Italy', 59379449, 70000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'FRA', 'France', 66865144, 50000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'BEL', 'Belgium', 11200000, 50000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'ESP', 'Spain', 46200000, 100000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'GBR', 'United Kingdom', 65000000, 120000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'AUS', 'Australia', 24000000, 70000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'ISR', 'Israel', 8500000, 30000\n", "future_projection_days = 180\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'JPN', 'Japan', 125000000, 150000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'IND', 'India', 1300000000, 500000\n", "future_projection_days = 180\n", "init_infection = 3 #w.r.t. the configured simulation population size\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, \n", " country_level_projection=True, min_initial_infection = init_infection)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'PHL', 'Philippines', 105000000, 90000\n", "future_projection_days = 180\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'QAT', 'Qatar', 2700000, 10000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'RUS', 'Russia', 140000000, 150000\n", "future_projection_days = 120\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'SAU', 'Saudi Arabia', 33000000, 90000\n", "future_projection_days = 90\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_code, state, state_population, actual_testing_capacity = 'ZAF', 'South Africa', 57000000, 50000\n", "future_projection_days = 120\n", "\n", "run (country_code, state, state_population, actual_testing_capacity, future_projection_days, country_level_projection=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" } }, "nbformat": 4, "nbformat_minor": 4 }