{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# T2 - Plotting, printing, and saving\n", "\n", "This tutorial provides a brief overview of options for plotting results, printing objects, and saving results." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "Click [here](https://mybinder.org/v2/gh/institutefordiseasemodeling/covasim/HEAD?urlpath=lab%2Ftree%2Fdocs%2Ftutorials%2Ftut_plotting.ipynb) to open an interactive version of this notebook.\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Global plotting configuration\n", "\n", "Covasim allows the user to set various options that apply to all plots. You can change the font size, default DPI, whether plots should be shown by default, etc. (for the full list, see `cv.options.help()`). For example, we might want higher resolution, to turn off automatic figure display, close figures after they're rendered, and to turn off the messages that print when a simulation is running. We can do this using built-in defaults for Jupyter notebooks (and then run a sim) with:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import covasim as cv\n", "\n", "cv.options(jupyter=True, verbose=0) # Standard options for Jupyter notebook\n", "sim = cv.Sim()\n", "sim.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Printing objects\n", "\n", "There are three levels of detail available for most objects (sims, multisims, scenarios, and people). The shortest is `brief()`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.brief()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get more detail with `summarize()`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.summarize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, to show the full object, including all methods and attributes, use `disp()`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.disp()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting options\n", "\n", "While a sim can be plotted using default settings simply by `sim.plot()`, this is just a small fraction of what's available. First, note that results can be plotted directly using e.g. Matplotlib. You can see what quantities are available for plotting with `sim.results.keys()` (remember, it's just a dict). A simple example of plotting using Matplotlib is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pylab as pl # Shortcut for import matplotlib.pyplot as plt\n", "pl.plot(sim.results['date'], sim.results['new_infections'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, as you can see, this isn't ideal since the default formatting is...not great. (Also, note that each result is a `Result` object, not a simple Numpy array; like a pandas dataframe, you can get the array of values directly via e.g. `sim.results.new_infections.values`.)\n", "\n", "An alternative, if you only want to plot a single result, such as new infections, is to use the `plot_result()` method:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot_result('new_infections')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also select one or more quantities to plot with the first (`to_plot`) argument, e.g." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot(to_plot=['new_infections', 'cum_infections'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another useful option is to plot an overview of everything in a simulation. We can do this with the `to_plot='overview'` argument. It's quite a lot of information so we might also want to make a larger figure for it, which we can do by passing additional arguments (which, if recognized, are passed to `pl.figure()`). We can also change the date format between `'sciris'` (the default), `'concise'` (better for cramped plots), and `'matplotlib'` (Matplotlib's default):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot('overview', n_cols=5, figsize=(20,20), dateformat='concise', dpi=50) # NB: dateformat='concise' is already the default for >2 columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While we can save this figure using Matplotlib's built-in `savefig()`, if we use Covasim's `cv.savefig()` we get a couple advantages:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.savefig(filename='my-fig.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, it saves the figure at higher resolution by default (which you can adjust with the `dpi` argument). But second, it stores information about the code that was used to generate the figure as metadata, which can be loaded later. Made an awesome plot but can't remember even what script you ran to generate it, much less what version of the code? You'll never have to worry about that again. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.get_png_metadata('my-fig.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Customizing plots\n", "\n", "We saw above how to set default plot configuration options for Jupyter. Covasim provides a lot of flexibility in customizing the appearance of plots as well. There are three different levels at which you can set plotting options: global, just for Covasim, or just for the current plot. To give an example with changing the figure DPI:\n", "- Change the setting globally (for both Covasim and Matplotlib): `sc.options(dpi=150)` or `pl.rc('figure', dpi=150)` (where `sc` is `import sciris as sc`)\n", "- Change for Covasim plots, but not for Matplotlib plots: `cv.options(dpi=150)`\n", "- Change for the current Covasim plot, but not other Covasim plots: `sim.plot(dpi=150)`\n", "\n", "The easiest way to change the style of Covasim plots is with the `style` argument. For example, to plot using a built-in Matplotlib style would simply be:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot(style='ggplot')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to the default style (`'covasim'`), there is also a \"simple\" style. You can combine built-in styles with additional overrides, including any valid Matplotlib commands:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot(style='simple', font='Rosario', legend_args={'frameon':True}, style_args={'ytick.direction':'in'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Note: Covasim comes bundled with two fonts, [Mulish](https://github.com/googlefonts/mulish) (sans-serif, the default) and [Rosario](https://fonts.adobe.com/fonts/rosario) (serif).)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although most style handling is done automatically, you can also use it yourself in a with block, e.g.:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with cv.options.with_style(fontsize=6):\n", " sim.plot() # This will have 6 point font\n", " pl.figure(); pl.plot(pl.rand(20), 'o') # So will this" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Saving options\n", "\n", "Saving sims is also pretty simple. The simplest way to save is simply" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.save('my-awesome-sim.sim')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Technically, this saves as a gzipped pickle file (via `sc.saveobj()` using the Sciris library). By default this does not save the people in the sim since they are very large (and since, if the random seed is saved, they can usually be regenerated). If you want to save the people as well, you can use the `keep_people` argument. For example, here's what it would look like to create a sim, run it halfway, save it, load it, change the overall transmissibility (beta), and finish running it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim_orig = cv.Sim(start_day='2020-04-01', end_day='2020-06-01', label='Load & save example')\n", "sim_orig.run(until='2020-05-01')\n", "sim_orig.save('my-half-finished-sim.sim') # Note: Covasim always saves the people if the sim isn't finished running yet\n", "\n", "sim = cv.load('my-half-finished-sim.sim')\n", "sim['beta'] *= 0.3\n", "sim.run()\n", "sim.plot(['new_infections', 'n_infectious', 'cum_infections'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aside from saving the entire simulation, there are other export options available. You can export the results and parameters to a JSON file (using `sim.to_json()`), but probably the most useful is to export the results to an Excel workbook, where they can easily be stored and processed with e.g. Pandas:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "sim.to_excel('my-sim.xlsx')\n", "df = pd.read_excel('my-sim.xlsx')\n", "print(df)" ] } ], "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.9.13" }, "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 }