{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plot unit conversions\n", "\n", "This notebook demonstrates some examples of different kinds of units, and the circumstances under which they are converted and displayed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import sys\n", "import atomica as at\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import sciris as sc\n", "from IPython.display import display, HTML" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "testdir = at.parent_dir()\n", "P = at.Project(framework='unit_demo_framework.xlsx',databook='unit_demo_databook.xlsx')\n", "P.load_progbook('unit_demo_progbook.xlsx')\n", "res = P.run_sim('default','default',at.ProgramInstructions(start_year=2018))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This test example has examples of parameters with different timescales, and different types of programs.\n", "\n", "##### Parameters\n", "\n", "- `recrate` - Duration in months\n", "- `infdeath` - Weekly probability\n", "- `susdeath` - Daily probability\n", "- `foi` - Annual probability" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = at.PlotData(res,outputs=['recrate','infdeath','susdeath','foi','sus:inf','susdeath:flow','dead'],pops='adults')\n", "at.plot_series(d,axis='pops');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that parameters are plotted in their native units. For example, a probability per day is shown as probability per day, matching the numbers that were entered in the databook.\n", "\n", "Aggregating these units without specifying the aggregation method will result in either integration or averaging as most appropriate for the units of the underlying quantity:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for output in ['recrate','infdeath','susdeath','foi','sus:inf','susdeath:flow','dead']:\n", " d = at.PlotData(res,outputs=output,pops='adults',t_bins=10)\n", " at.plot_bars(d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accumulation will result in the units and output name being updated appropriately:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = at.PlotData(res,outputs='sus:inf',pops='adults',accumulate='integrate',project=P)\n", "at.plot_series(d);\n", "d = at.PlotData(res,outputs='sus',pops='adults',accumulate='integrate',project=P)\n", "at.plot_series(d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Programs\n", "\n", " - `Risk avoidance` - Continuous\n", " - `Harm reduction 1` - Continuous\n", " - `Harm reduction 2` - Continuous\n", " - `Treatment 1` - One-off\n", " - `Treatment 2` - One-off\n", " \n", " Programs with continuous coverage cover a certain number of people every year:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = at.PlotData.programs(res,outputs='Risk avoidance',quantity='coverage_number')\n", "at.plot_series(d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Programs with one-off coverage cover a number of people at each time step. This is the number that gets returned by `Result.get_coverage()` but it is automatically annualized for plotting:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "annual_coverage = res.model.progset.programs['Treatment 1'].spend_data.vals[0]/res.model.progset.programs['Treatment 1'].unit_cost.vals[0]\n", "timestep_coverage = res.get_coverage('number')['Treatment 1'][0]\n", "print('Annual coverage = %g, Timestep coverage = %g' % (annual_coverage, timestep_coverage))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = at.PlotData.programs(res,outputs='Treatment 1',quantity='coverage_number')\n", "at.plot_series(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These units are handled automatically when aggregating. For example, consider computing the number of people covered over a period of time:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = at.PlotData.programs(res,outputs='Treatment 1',quantity='coverage_number',t_bins=[2000,2000.5])\n", "at.plot_bars(d);\n", "\n", "d = at.PlotData.programs(res,outputs='Treatment 1',quantity='coverage_number',t_bins=[2000,2002])\n", "at.plot_bars(d);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = at.PlotData.programs(res,outputs='Treatment 1',quantity='coverage_eligible',t_bins=[2000,2000.5])\n", "at.plot_bars(d);\n", "\n", "d = at.PlotData.programs(res,outputs='Treatment 1',quantity='coverage_number',t_bins=[2000,2002])\n", "at.plot_bars(d);" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:optima37]", "language": "python", "name": "conda-env-optima37-py" }, "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.7.0" }, "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": 2 }