{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# T1 - Getting started\n", "\n", "Installing and getting started with Covasim is quite simple. \n", "\n", "To install, just type `pip install covasim`. If it worked, you should be able to import Covasim with `import covasim as cv`.\n", "\n", "The basic design philosophy of Covasim is: **common tasks should be simple**. For example:\n", "\n", "- Defining parameters\n", "- Running a simulation\n", "- Plotting results\n", "\n", "This tutorial walks you through how to do these things." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "Click [here](https://mybinder.org/v2/gh/institutefordiseasemodeling/covasim/HEAD?urlpath=lab%2Ftree%2Fdocs%2Ftutorials%2Ftut_intro.ipynb) to open an interactive version of this notebook.\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hello world\n", "\n", "To create, run, and plot a sim with default options is just:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import covasim as cv\n", "\n", "sim = cv.Sim()\n", "sim.run()\n", "fig = sim.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining parameters and running simulations\n", "\n", "Parameters are defined as a dictionary. The most common parameters to modify are the population size, the initial number of people infected, and the start and end dates of the simulation. We can define those as:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pars = dict(\n", " pop_size = 50e3,\n", " pop_infected = 100,\n", " start_day = '2020-04-01',\n", " end_day = '2020-06-01',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running a simulation is pretty easy. In fact, running a sim with the parameters we defined above is just:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim = cv.Sim(pars)\n", "sim.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will generate a results dictionary `sim.results`. For example, the number of new infections per day is `sim.results['new_infections']`.\n", "\n", "Rather than creating a parameter dictionary, any valid parameter can also be passed to the sim directly. For example, exactly equivalent to the above is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim = cv.Sim(pop_size=50e3, pop_infected=100, start_day='2020-04-01', end_day='2020-06-01')\n", "sim.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can mix and match too – pass in a parameter dictionary with default options, and then include other parameters as keywords (including overrides; keyword arguments take precedence). For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim = cv.Sim(pars, pop_infected=10) # Use parameters defined above, except start with 10 infected people\n", "sim.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting results\n", "\n", "As you saw above, plotting the results of a simulation is rather easy too:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = sim.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Full usage example\n", "\n", "Many of the details of this example will be explained in later tutorials, but to give you a taste, here's an example of how you would run two simulations to determine the impact of a custom intervention aimed at protecting the elderly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import covasim as cv\n", "\n", "# Custom intervention -- see Tutorial 5\n", "def protect_elderly(sim):\n", " if sim.t == sim.day('2020-04-01'):\n", " elderly = sim.people.age>70\n", " sim.people.rel_sus[elderly] = 0.0\n", "\n", "pars = dict(\n", " pop_type = 'hybrid', # Use a more realistic population model\n", " location = 'japan', # Use population characteristics for Japan\n", " pop_size = 50e3, # Have 50,000 people total in the population\n", " pop_infected = 100, # Start with 100 infected people\n", " n_days = 90, # Run the simulation for 90 days\n", " verbose = 0, # Do not print any output\n", ")\n", "\n", "# Running with multisims -- see Tutorial 3\n", "s1 = cv.Sim(pars, label='Default')\n", "s2 = cv.Sim(pars, interventions=protect_elderly, label='Protect the elderly')\n", "msim = cv.MultiSim([s1, s2])\n", "msim.run()\n", "fig = msim.plot(['cum_deaths', 'cum_infections'])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.3" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }