Basic workflow

This page demonstrates how to perform basic operations in Atomica. First, we will set up the notebook environment - the commands below are typically not required in user scripts:

[1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
import sys
sys.path.append('..')

To start with, import Atomica itself. It is often also useful to import numpy and matplotlib

[2]:
import atomica as at
import numpy as np
import matplotlib.pyplot as plt

Starting an application

The first step in starting a new application is to write a Framework file. This can be done by copying one of the templates in the atomica/library folder (either framework_template.xlsx or framework_template_advanced.xlsx) and implementing your model. Further guidance on this is provided separately in the framework documentation.

After writing the Framework, the next step is to generate a databook. This is performed in three steps

  1. Load the framework into a ProjectFramework Python instance

  2. Use the framework to make a new ProjectData instance

  3. Save the ProjectData instance to a spreadsheet

In this example, we will load an existing framework from the library. You can use at.LIBRARY_PATH to refer to the folder containing the library Excel files shipped with Atomica:

[3]:
F = at.ProjectFramework(at.LIBRARY_PATH / 'tb_framework.xlsx') # Load the Framework
D = at.ProjectData.new(F,pops=2, tvec=np.arange(2000,2018), transfers=0)
D.save('new_databook.xlsx')
Object saved to /home/vsts/work/1/s/docs/examples/new_databook.xlsx.

The ProjectData class in Python can be thought of as an equivalent representation of the databook - you can edit the databook in Excel, which will result in changes to the ProjectData variable when the spreadsheet is loaded, and you can modify the ProjectData in Python and then write a modified spreadsheet. ProjectData has a number of methods that you can use to modify the databook, to do things like

  • Add or remove populations

  • Change the time span of the databook

To perform these operations, you can load in a databook using ProjectData.from_spreadsheet(). This lets you load in a databook given a particular framework. It is not required that the databook be completed prior to loading - you only need to complete the databook in its entirity if you want to use the databook in a project. So for example, to add an additional population and a transfer to this newly created databook, we could use:

[4]:
D = at.ProjectData.from_spreadsheet('new_databook.xlsx', framework=F)
D.add_pop('pris','Prisoners')
D.add_transfer('aging','Aging')
D.save('new_databook_2.xlsx')
Object saved to /home/vsts/work/1/s/docs/examples/new_databook_2.xlsx.

Creating a project

Once you have completed the framework file and databook, you can create a project that can be used to run simulations and analyses. To do this, simply create a Project instance, passing in the file names for the framework and databook. Here we will use a pre-filled databook from the library:

[5]:
P = at.Project(framework=at.LIBRARY_PATH / 'tb_framework.xlsx', databook=at.LIBRARY_PATH / 'tb_databook.xlsx')
Elapsed time for running "default": 0.918s

When you create a project, a default simulation is automatically run. You can subsequently run simulations using P.run_sim()

[6]:
res = P.run_sim(parset='default', result_name='Default parset')
P.results.keys()
Elapsed time for running "default": 1.03s
[6]:
['parset_default']

When you run a simulation, by default it is automatically copied into the project, as well as being returned. Specifying the result name is optional, but recommended because it helps to keep track of the simulations when comparing and plotting them. We can now plot the result to show the compartment sizes:

[7]:
d = at.PlotData(res,pops='0-4',project=P)
at.plot_series(d,plot_type='stacked', data=P.data, legend_mode='separate');
../_images/examples_Basic-workflow_13_0.svg
../_images/examples_Basic-workflow_13_1.svg

For full details on plotting, please refer to the full plotting documentation here.

Calibrating the model

Model calibration can be performed in one of two ways - either manually, or automatically

Manual calibration

Manual calibration of the model proceeds in three steps

  1. Make a new ParameterSet (e.g., by copying an existing one)

  2. Modify the calibration scale factors in that ParameterSet

  3. Run a simulation using the new parameter set

The commands to do this are shown below, for an example where the force of infection has been decreased:

[8]:
new_parset = P.parsets.copy('default','manually_calibrated')
new_parset.pars['foi_out'].meta_y_factor = 0.8 # Decrease infectiousness of all populations
new_parset.pars['foi_in'].y_factor['0-4'] = 2.0 # Increase susceptibility of young children
res_manually_calibrated = P.run_sim(parset='manually_calibrated', result_name='Manually calibrated')
d = at.PlotData([res,res_manually_calibrated],outputs='ac_inf',project=P)
at.plot_series(d, axis='results');
Elapsed time for running "default": 1.10s
../_images/examples_Basic-workflow_16_1.svg
../_images/examples_Basic-workflow_16_2.svg
../_images/examples_Basic-workflow_16_3.svg
../_images/examples_Basic-workflow_16_4.svg
../_images/examples_Basic-workflow_16_5.svg

Automatic calibration

To perform an automatic calibration, simply use P.calibrate() specifying the amount of time to run the calibration for, and the name of the new calibrated parset to create.

[9]:
P.calibrate(max_time=10, parset='default', new_name="auto_calibrated", save_to_project=True);
ASD: Launching with random seed None
Elapsed time for running "default": 0.918s
Elapsed time for running "default": 0.659s
    step=1 choice=175, par=45, pm=1.0, origval=1.0, newval=0.9
     step 1 (0.7 s) -- (orig:10.14 | best:10.14 | new:10.15 | diff:0.01090)
Elapsed time for running "default": 0.848s
    step=2 choice=153, par=23, pm=1.0, origval=1.0, newval=0.9
     step 2 (1.5 s) ++ (orig:10.14 | best:10.14 | new:10.13 | diff:-0.004219)
Elapsed time for running "default": 0.662s
    step=3 choice=57, par=57, pm=0.0, origval=1.0, newval=1.1
     step 3 (2.2 s) -- (orig:10.14 | best:10.13 | new:10.15 | diff:0.01625)
Elapsed time for running "default": 0.654s
    step=4 choice=26, par=26, pm=0.0, origval=1.0, newval=1.1
     step 4 (2.9 s) -- (orig:10.14 | best:10.13 | new:10.14 | diff:0.005574)
Elapsed time for running "default": 0.641s
    step=5 choice=206, par=76, pm=1.0, origval=1.0, newval=0.9
     step 5 (3.5 s) ++ (orig:10.14 | best:10.13 | new:10.13 | diff:-0.00000003738)
Elapsed time for running "default": 0.701s
    step=6 choice=199, par=69, pm=1.0, origval=1.0, newval=0.9
     step 6 (4.2 s) -- (orig:10.14 | best:10.13 | new:10.18 | diff:0.04418)
Elapsed time for running "default": 0.904s
    step=7 choice=73, par=73, pm=0.0, origval=1.0, newval=1.1
     step 7 (5.1 s) -- (orig:10.14 | best:10.13 | new:10.13 | diff:0.00007811)
Elapsed time for running "default": 0.661s
    step=8 choice=249, par=119, pm=1.0, origval=1.0, newval=0.9
     step 8 (5.8 s) -- (orig:10.14 | best:10.13 | new:10.24 | diff:0.1072)
Elapsed time for running "default": 0.699s
    step=9 choice=2, par=2, pm=0.0, origval=1.0, newval=1.1
     step 9 (6.5 s) -- (orig:10.14 | best:10.13 | new:10.68 | diff:0.5476)
Elapsed time for running "default": 0.657s
    step=10 choice=58, par=58, pm=0.0, origval=1.0, newval=1.1
     step 10 (7.2 s) -- (orig:10.14 | best:10.13 | new:10.13 | diff:0.0002898)
Elapsed time for running "default": 0.876s
    step=11 choice=250, par=120, pm=1.0, origval=1.0, newval=0.9
     step 11 (8.0 s) ++ (orig:10.14 | best:10.13 | new:9.671 | diff:-0.4610)
Elapsed time for running "default": 0.879s
    step=12 choice=7, par=7, pm=0.0, origval=1.0, newval=1.1
     step 12 (8.9 s) -- (orig:10.14 | best:9.671 | new:10.49 | diff:0.8228)
Elapsed time for running "default": 0.823s
    step=13 choice=130, par=0, pm=1.0, origval=1.0, newval=0.9
     step 13 (9.8 s) ++ (orig:10.14 | best:9.671 | new:9.643 | diff:-0.02795)
Elapsed time for running "default": 1.07s
    step=14 choice=24, par=24, pm=0.0, origval=1.0, newval=1.1
     step 14 (10.8 s) ++ (orig:10.14 | best:9.643 | new:9.636 | diff:-0.006634)
===  Time limit reached (10.83 > 10.00) (14 steps, orig: 10.14 | best: 9.636 | ratio: 0.9507) ===

You can then run a simulation with the calibrated parset by passing the name of the new parset to run_sim

[10]:
res_auto_calibrated = P.run_sim(parset='auto_calibrated',result_name='Automatically calibrated')
Elapsed time for running "default": 0.921s

Adding programs

The programs system allows parameter values to be overwritten based on spending on a set of programs. To get started, you will first need a program spreadsheet (progbook). The progbook is specific to a framework and a databook, because it refers to both the compartments and parameters of the model (from the framework) as well as the populations (from the databook).

You can make a new progbook using the .make_progbook() method of the project:

[11]:
P.make_progbook(progbook_path='example_progbook.xlsx', progs=4, data_start=2014, data_end=2018)
Object saved to /home/vsts/work/1/s/docs/examples/example_progbook.xlsx.
[11]:
'/home/vsts/work/1/s/docs/examples/example_progbook.xlsx'

After filling out the progbook, you can load it into the project using the .load_progbook() method. Here, we will load in a pre-filled progbook from the library:

[12]:
P.load_progbook(at.LIBRARY_PATH / 'tb_progbook.xlsx')
[12]:
<atomica.programs.ProgramSet at 0x7f1e5c3a9d90>
[<class 'atomica.programs.ProgramSet'>, <class 'atomica.utils.NamedItem'>]
————————————————————————————————————————————————————————————————————————
Methods:
  _get_code_name()        add_pop()               remove_par()
  _normalize_inputs()     add_program()           remove_pop()
  _read_effects()         copy()                  remove_program()
  _read_spending()        from_spreadsheet()      sample()
  _read_targeting()       get_alloc()             save()
  _write_effects()        get_capacities()        to_spreadsheet()
  _write_spending()       get_outcomes()          to_workbook()
  _write_targeting()      get_prop_coverage()     validate()
  add_comp()              new()                   add_par()
  remove_comp()
————————————————————————————————————————————————————————————————————————
               _book: None
            _formats: None
          _pop_types: ['default']
         _references: None
               comps: #0: 'initj':  {'label': 'Initialization
                      population size', 't [...]
             covouts: #0: ('v_num', '0-4'):
                         Parameter: v_num
                        Population: 0-4
                       [...]
             created: datetime.datetime(2024, 6, 30, 22, 53, 12,
                      712409, tzinfo=tz [...]
            currency: '$'
             gitinfo: {'branch': 'Detached head (no branch)', 'hash':
                      'e19dee1', ' [...]
            modified: datetime.datetime(2024, 6, 30, 22, 53, 14,
                      400811, tzinfo=tz [...]
                name: 'default'
                pars: #0: 'v_num':       {'label': 'Number of
                      vaccinations adminis [...]
                pops: #0: '0-4':       {'label': 'Children 0-4',
                      'type': 'default' [...]
            programs: #0: 'BCG':
                      <atomica.programs.Program at 0x7f1e54a042c0>
                      [<cl [...]
etc. (time exceeded): 1 entries not shown
————————————————————————————————————————————————————————————————————————

Note: the object did not finish printing within maxtime=3 s.
To see the full object, call prepr() with increased maxtime.    Program set name: default
            Programs: ['BCG', 'PCF', 'ACF', 'ACF-p', 'HospDS', 'HospMDR', 'HospXDR', 'AmbDS', 'AmbMDR', 'XDRnew', 'PrisDS', 'PrisDR']
        Date created: 2024-Jun-30 22:53:12 UTC
       Date modified: 2024-Jun-30 22:53:14 UTC
============================================================

This progbook has been added to the list of available progsets:

[13]:
P.progsets.keys()
[13]:
['default']

Running a simulation with programs requires one additional piece of information - a ProgramInstructions instance that specifies

  • What years the programs are active

  • Any overwrites to spending or coverage

In our case, we might just want to run a simulation with programs starting in 2018, so we can create a ProgramInstructions instance accordingly, and then use it to run the simulation:

[14]:
instructions = at.ProgramInstructions(start_year=2018)
res_progs = P.run_sim(parset='default',progset='default',progset_instructions=instructions)
Elapsed time for running "default": 1.57s

Reconciliation

Reconciliation is an operation that aims to change the properties of programs (such as their unit costs) such that the program-calculated parameter values optimally match the databook parameter values in the year the programs become active (or some other specified year). The reconciliation operation can therefore be treated as a mapping from one progset to another. To perform reconciliation, use the reconcile function directly, passing in:

  • the parameter set you want to match

  • the program set to modify

  • the reconciliation year

  • a specification of which aspects of the program set to modify (e.g. unit cost, program outcomes)

The reconcile function returns a new progset, which you can store in the project if desired, or otherwise work with independently:

[15]:
P.progsets['reconciled'] = at.reconcile(project=P, parset='default', progset='default', reconciliation_year=2018, unit_cost_bounds=0.05)[0]
WARNING {reconciliation.py:243} - Reconcilation when parameter is in number units not fully tested
Program set 'default' will be ignored while running project 'default' due to the absence of program set instructions
Elapsed time for running "default": 1.21s
Reconciling in 2018.00, evaluating from 2018.00 up to 2018.25
ASD: Launching with random seed None
    step=1 choice=8, par=8, pm=0.0, origval=4500.0, newval=4725.0
     step 1 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0)
    step=2 choice=17, par=5, pm=1.0, origval=7500.0, newval=7125.0
     step 2 (0.0 s) ++ (orig:61.58 | best:61.58 | new:61.50 | diff:-0.08149)
    step=3 choice=0, par=0, pm=0.0, origval=2.5, newval=2.625
     step 3 (0.0 s) -- (orig:61.58 | best:61.50 | new:61.50 | diff:0.00004553)
    step=4 choice=10, par=10, pm=0.0, origval=5500.0, newval=5775.0
     step 4 (0.0 s) -- (orig:61.58 | best:61.50 | new:61.51 | diff:0.004171)
    step=5 choice=15, par=3, pm=1.0, origval=2500.0, newval=2375.0
     step 5 (0.0 s) ++ (orig:61.58 | best:61.50 | new:61.50 | diff:-0.005951)
    step=6 choice=6, par=6, pm=0.0, origval=10000.0, newval=10500.0
     step 6 (0.0 s) -- (orig:61.58 | best:61.50 | new:61.52 | diff:0.02779)
    step=7 choice=3, par=3, pm=0.0, origval=2375.0, newval=2625.0
     step 7 (0.0 s) -- (orig:61.58 | best:61.50 | new:61.51 | diff:0.01134)
    step=8 choice=18, par=6, pm=1.0, origval=10000.0, newval=9500.0
     step 8 (0.0 s) ++ (orig:61.58 | best:61.50 | new:61.47 | diff:-0.02985)
    step=9 choice=20, par=8, pm=1.0, origval=4500.0, newval=4275.0
     step 9 (0.0 s) -- (orig:61.58 | best:61.47 | new:61.47 | diff:0)
    step=10 choice=11, par=11, pm=0.0, origval=8000.0, newval=8400.0
     step 10 (0.0 s) -- (orig:61.58 | best:61.47 | new:61.47 | diff:0)
    step=11 choice=20, par=8, pm=1.0, origval=4500.0, newval=4275.0
     step 11 (0.0 s) -- (orig:61.58 | best:61.47 | new:61.47 | diff:0)
    step=12 choice=12, par=0, pm=1.0, origval=2.5, newval=2.375
     step 12 (0.0 s) ++ (orig:61.58 | best:61.47 | new:61.47 | diff:-0.00002755)
    step=13 choice=5, par=5, pm=0.0, origval=7125.0, newval=7875.0
     step 13 (0.0 s) -- (orig:61.58 | best:61.47 | new:61.65 | diff:0.1803)
    step=14 choice=16, par=4, pm=1.0, origval=4900.0, newval=4655.0
     step 14 (0.0 s) ++ (orig:61.58 | best:61.47 | new:61.40 | diff:-0.06943)
    step=15 choice=3, par=3, pm=0.0, origval=2375.0, newval=2500.0
     step 15 (0.0 s) -- (orig:61.58 | best:61.40 | new:61.40 | diff:0.005951)
    step=16 choice=2, par=2, pm=0.0, origval=4000.0, newval=4200.0
     step 16 (0.0 s) -- (orig:61.58 | best:61.40 | new:61.40 | diff:0.00002363)
    step=17 choice=8, par=8, pm=0.0, origval=4500.0, newval=4725.0
     step 17 (0.0 s) -- (orig:61.58 | best:61.40 | new:61.40 | diff:0)
    step=18 choice=22, par=10, pm=1.0, origval=5500.0, newval=5225.0
     step 18 (0.0 s) -- (orig:61.58 | best:61.40 | new:61.40 | diff:0.003359)
    step=19 choice=2, par=2, pm=0.0, origval=4000.0, newval=4200.0
     step 19 (0.0 s) -- (orig:61.58 | best:61.40 | new:61.40 | diff:0.00002363)
    step=20 choice=13, par=1, pm=1.0, origval=1000.0, newval=950.0
     step 20 (0.0 s) ++ (orig:61.58 | best:61.40 | new:61.39 | diff:-0.004674)
    step=21 choice=9, par=9, pm=0.0, origval=18000.0, newval=18900.0
     step 21 (0.1 s) -- (orig:61.58 | best:61.39 | new:61.40 | diff:0.01213)
    step=22 choice=7, par=7, pm=0.0, origval=2700.0, newval=2835.0
     step 22 (0.1 s) -- (orig:61.58 | best:61.39 | new:61.39 | diff:0)
    step=23 choice=4, par=4, pm=0.0, origval=4655.0, newval=5145.0
     step 23 (0.1 s) -- (orig:61.58 | best:61.39 | new:61.54 | diff:0.1476)
    step=24 choice=1, par=1, pm=0.0, origval=950.0, newval=1050.0
     step 24 (0.1 s) -- (orig:61.58 | best:61.39 | new:61.40 | diff:0.009105)
    step=25 choice=23, par=11, pm=1.0, origval=8000.0, newval=7600.0
     step 25 (0.1 s) -- (orig:61.58 | best:61.39 | new:61.39 | diff:0)
    step=26 choice=6, par=6, pm=0.0, origval=9500.0, newval=10000.0
     step 26 (0.1 s) -- (orig:61.58 | best:61.39 | new:61.42 | diff:0.02985)
    step=27 choice=21, par=9, pm=1.0, origval=18000.0, newval=17100.0
     step 27 (0.1 s) ++ (orig:61.58 | best:61.39 | new:61.38 | diff:-0.01325)
    step=28 choice=5, par=5, pm=0.0, origval=7125.0, newval=7500.0
     step 28 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.46 | diff:0.08149)
    step=29 choice=4, par=4, pm=0.0, origval=4655.0, newval=4900.0
     step 29 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.45 | diff:0.06943)
    step=30 choice=1, par=1, pm=0.0, origval=950.0, newval=1000.0
     step 30 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004674)
    step=31 choice=0, par=0, pm=0.0, origval=2.375, newval=2.5
     step 31 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00002755)
    step=32 choice=4, par=4, pm=0.0, origval=4655.0, newval=4777.5
     step 32 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.41 | diff:0.03338)
    step=33 choice=7, par=7, pm=0.0, origval=2700.0, newval=2835.0
     step 33 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=34 choice=4, par=4, pm=0.0, origval=4655.0, newval=4716.25
     step 34 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.40 | diff:0.01632)
    step=35 choice=10, par=10, pm=0.0, origval=5500.0, newval=5775.0
     step 35 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004171)
    step=36 choice=14, par=2, pm=1.0, origval=4000.0, newval=3800.0
     step 36 (0.1 s) ++ (orig:61.58 | best:61.38 | new:61.38 | diff:-0.00002487)
    step=37 choice=9, par=9, pm=0.0, origval=17100.0, newval=18000.0
     step 37 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.01325)
    step=38 choice=2, par=2, pm=0.0, origval=3800.0, newval=3900.0
     step 38 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001275)
    step=39 choice=5, par=5, pm=0.0, origval=7125.0, newval=7312.5
     step 39 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.42 | diff:0.03816)
    step=40 choice=8, par=8, pm=0.0, origval=4500.0, newval=4612.5
     step 40 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=41 choice=3, par=3, pm=0.0, origval=2375.0, newval=2437.5
     step 41 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003052)
    step=42 choice=7, par=7, pm=0.0, origval=2700.0, newval=2767.5
     step 42 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=43 choice=11, par=11, pm=0.0, origval=8000.0, newval=8400.0
     step 43 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=44 choice=19, par=7, pm=1.0, origval=2700.0, newval=2565.0
     step 44 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=45 choice=19, par=7, pm=1.0, origval=2700.0, newval=2565.0
     step 45 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=46 choice=2, par=2, pm=0.0, origval=3800.0, newval=3850.0
     step 46 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000006460)
    step=47 choice=1, par=1, pm=0.0, origval=950.0, newval=975.0
     step 47 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.002367)
    step=48 choice=7, par=7, pm=0.0, origval=2700.0, newval=2733.75
     step 48 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=49 choice=8, par=8, pm=0.0, origval=4500.0, newval=4556.25
     step 49 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=50 choice=22, par=10, pm=1.0, origval=5500.0, newval=5225.0
     step 50 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003359)
    step=51 choice=6, par=6, pm=0.0, origval=9500.0, newval=9750.0
     step 51 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.01499)
    step=52 choice=10, par=10, pm=0.0, origval=5500.0, newval=5637.5
     step 52 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001094)
    step=53 choice=7, par=7, pm=0.0, origval=2700.0, newval=2716.875
     step 53 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=54 choice=22, par=10, pm=1.0, origval=5500.0, newval=5362.5
     step 54 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001209)
    step=55 choice=19, par=7, pm=1.0, origval=2700.0, newval=2632.5
     step 55 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=56 choice=3, par=3, pm=0.0, origval=2375.0, newval=2406.25
     step 56 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001546)
    step=57 choice=20, par=8, pm=1.0, origval=4500.0, newval=4387.5
     step 57 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=58 choice=23, par=11, pm=1.0, origval=8000.0, newval=7600.0
     step 58 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=59 choice=10, par=10, pm=0.0, origval=5500.0, newval=5568.75
     step 59 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0002804)
    step=60 choice=11, par=11, pm=0.0, origval=8000.0, newval=8200.0
     step 60 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=61 choice=20, par=8, pm=1.0, origval=4500.0, newval=4443.75
     step 61 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=62 choice=2, par=2, pm=0.0, origval=3800.0, newval=3825.0
     step 62 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000003251)
    step=63 choice=10, par=10, pm=0.0, origval=5500.0, newval=5534.375
     step 63 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00007096)
    step=64 choice=8, par=8, pm=0.0, origval=4500.0, newval=4528.125
     step 64 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=65 choice=19, par=7, pm=1.0, origval=2700.0, newval=2666.25
     step 65 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=66 choice=11, par=11, pm=0.0, origval=8000.0, newval=8100.0
     step 66 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=67 choice=6, par=6, pm=0.0, origval=9500.0, newval=9625.0
     step 67 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.007562)
    step=68 choice=23, par=11, pm=1.0, origval=8000.0, newval=7800.0
     step 68 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=69 choice=3, par=3, pm=0.0, origval=2375.0, newval=2390.625
     step 69 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0007779)
    step=70 choice=9, par=9, pm=0.0, origval=17100.0, newval=17550.0
     step 70 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.006771)
    step=71 choice=0, par=0, pm=0.0, origval=2.375, newval=2.4375
     step 71 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001114)
    step=72 choice=7, par=7, pm=0.0, origval=2700.0, newval=2708.4375
     step 72 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=73 choice=1, par=1, pm=0.0, origval=950.0, newval=962.5
     step 73 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001191)
    step=74 choice=23, par=11, pm=1.0, origval=8000.0, newval=7900.0
     step 74 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=75 choice=11, par=11, pm=0.0, origval=8000.0, newval=8050.0
     step 75 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=76 choice=23, par=11, pm=1.0, origval=8000.0, newval=7950.0
     step 76 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=77 choice=22, par=10, pm=1.0, origval=5500.0, newval=5431.25
     step 77 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0002947)
    step=78 choice=9, par=9, pm=0.0, origval=17100.0, newval=17325.0
     step 78 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003424)
    step=79 choice=23, par=11, pm=1.0, origval=8000.0, newval=7975.0
     step 79 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=80 choice=1, par=1, pm=0.0, origval=950.0, newval=956.25
     step 80 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0005974)
    step=81 choice=0, par=0, pm=0.0, origval=2.375, newval=2.40625
     step 81 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000004857)
    step=82 choice=1, par=1, pm=0.0, origval=950.0, newval=953.125
     step 82 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0002992)
    step=83 choice=4, par=4, pm=0.0, origval=4655.0, newval=4685.625
     step 83 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.008065)
    step=84 choice=10, par=10, pm=0.0, origval=5500.0, newval=5517.1875
     step 84 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001785)
    step=85 choice=19, par=7, pm=1.0, origval=2700.0, newval=2683.125
     step 85 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=86 choice=22, par=10, pm=1.0, origval=5500.0, newval=5465.625
     step 86 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00007276)
    step=87 choice=2, par=2, pm=0.0, origval=3800.0, newval=3812.5
     step 87 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000001631)
    step=88 choice=5, par=5, pm=0.0, origval=7125.0, newval=7218.75
     step 88 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.40 | diff:0.01837)
    step=89 choice=22, par=10, pm=1.0, origval=5500.0, newval=5482.8125
     step 89 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001808)
    step=90 choice=19, par=7, pm=1.0, origval=2700.0, newval=2691.5625
     step 90 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=91 choice=2, par=2, pm=0.0, origval=3800.0, newval=3806.25
     step 91 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0000008167)
    step=92 choice=3, par=3, pm=0.0, origval=2375.0, newval=2382.8125
     step 92 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0003902)
    step=93 choice=7, par=7, pm=0.0, origval=2700.0, newval=2704.21875
     step 93 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=94 choice=19, par=7, pm=1.0, origval=2700.0, newval=2695.78125
     step 94 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=95 choice=8, par=8, pm=0.0, origval=4500.0, newval=4514.0625
     step 95 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=96 choice=20, par=8, pm=1.0, origval=4500.0, newval=4471.875
     step 96 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=97 choice=0, par=0, pm=0.0, origval=2.375, newval=2.390625
     step 97 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000002243)
    step=98 choice=9, par=9, pm=0.0, origval=17100.0, newval=17212.5
     step 98 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001722)
    step=99 choice=5, par=5, pm=0.0, origval=7125.0, newval=7171.875
     step 99 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.009000)
    step=100 choice=20, par=8, pm=1.0, origval=4500.0, newval=4485.9375
     step 100 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=101 choice=4, par=4, pm=0.0, origval=4655.0, newval=4670.3125
     step 101 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004008)
    step=102 choice=0, par=0, pm=0.0, origval=2.375, newval=2.3828125
     step 102 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000001074)
    step=103 choice=11, par=11, pm=0.0, origval=8000.0, newval=8025.0
     step 103 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=104 choice=1, par=1, pm=0.0, origval=950.0, newval=951.5625
     step 104 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0001497)
    step=105 choice=23, par=11, pm=1.0, origval=8000.0, newval=7987.5
     step 105 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=106 choice=16, par=4, pm=1.0, origval=4655.0, newval=4655.0
     step 106 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=107 choice=9, par=9, pm=0.0, origval=17100.0, newval=17156.25
     step 107 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0008633)
    step=108 choice=14, par=2, pm=1.0, origval=3800.0, newval=3800.0
     step 108 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=109 choice=6, par=6, pm=0.0, origval=9500.0, newval=9562.5
     step 109 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003798)
    step=110 choice=1, par=1, pm=0.0, origval=950.0, newval=950.78125
     step 110 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00007488)
    step=111 choice=4, par=4, pm=0.0, origval=4655.0, newval=4662.65625
     step 111 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001998)
    step=112 choice=8, par=8, pm=0.0, origval=4500.0, newval=4507.03125
     step 112 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=113 choice=3, par=3, pm=0.0, origval=2375.0, newval=2378.90625
     step 113 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0001954)
    step=114 choice=2, par=2, pm=0.0, origval=3800.0, newval=3803.125
     step 114 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0000004087)
    step=115 choice=20, par=8, pm=1.0, origval=4500.0, newval=4492.96875
     step 115 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=116 choice=5, par=5, pm=0.0, origval=7125.0, newval=7148.4375
     step 116 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004453)
    step=117 choice=9, par=9, pm=0.0, origval=17100.0, newval=17128.125
     step 117 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0004323)
    step=118 choice=6, par=6, pm=0.0, origval=9500.0, newval=9531.25
     step 118 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001903)
    step=119 choice=20, par=8, pm=1.0, origval=4500.0, newval=4496.484375
     step 119 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=120 choice=8, par=8, pm=0.0, origval=4500.0, newval=4503.515625
     step 120 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=121 choice=22, par=10, pm=1.0, origval=5500.0, newval=5491.40625
     step 121 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000004505)
    step=122 choice=11, par=11, pm=0.0, origval=8000.0, newval=8012.5
     step 122 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=123 choice=15, par=3, pm=1.0, origval=2375.0, newval=2375.0
     step 123 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=124 choice=12, par=0, pm=1.0, origval=2.375, newval=2.375
     step 124 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=125 choice=5, par=5, pm=0.0, origval=7125.0, newval=7136.71875
     step 125 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.002214)
    step=126 choice=17, par=5, pm=1.0, origval=7125.0, newval=7125.0
     step 126 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=127 choice=19, par=7, pm=1.0, origval=2700.0, newval=2697.890625
     step 127 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=128 choice=18, par=6, pm=1.0, origval=9500.0, newval=9500.0
     step 128 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=129 choice=10, par=10, pm=0.0, origval=5500.0, newval=5508.59375
     step 129 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000004477)
    step=130 choice=14, par=2, pm=1.0, origval=3800.0, newval=3800.0
     step 130 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=131 choice=0, par=0, pm=0.0, origval=2.375, newval=2.37890625
     step 131 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0000005250)
    step=132 choice=6, par=6, pm=0.0, origval=9500.0, newval=9515.625
     step 132 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0009528)
    step=133 choice=13, par=1, pm=1.0, origval=950.0, newval=950.0
     step 133 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
    step=134 choice=18, par=6, pm=1.0, origval=9500.0, newval=9500.0
     step 134 (0.4 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
===  Relative improvement too small (0.000002436 < 0.001000) (134 steps, orig: 61.58 | best: 61.38 | ratio: 0.9967) ===

You can then run simulations with the modified program set. You can also save the new programset to a progbook if you wish to edit it further in Excel:

[16]:
P.progsets['reconciled'].save('reconciled_progset.xlsx');
Object saved to /home/vsts/work/1/s/docs/examples/reconciled_progset.xlsx.

Scenarios

A scenario involves overriding some aspect of the simulation that would otherwise be specified in the databook or progbook. There are three kinds of scenarios

  • Parameter scenarios, when you want to test the effect of a specific parameter value

  • Budget scenarios, when you want to examine the outcomes of specific spending values

  • Coverage scenarios, when you want to examine the effect of specific program coverages irrespective of spending

Examples of these scenarios are shown below:

Parameter scenarios

[17]:
scvalues = dict()
scvalues['b_rate'] = dict()
scvalues['b_rate']['0-4'] = dict()
scvalues['b_rate']['0-4']["t"] = [2015, 2020, 2035]
scvalues['b_rate']['0-4']["y"] = [270000, 220000, 220000]
scen = P.make_scenario(which='parameter', name="Reduced births", scenario_values=scvalues)
res_par_scen = scen.run(P, P.parsets["default"]);

# Plot the parameter and compare to scenario input values
d = at.PlotData(res_par_scen,outputs='b_rate',pops='0-4')
at.plot_series(d)
plt.scatter(scvalues['b_rate']['0-4']["t"],scvalues['b_rate']['0-4']["y"],color='r')
Elapsed time for running "default": 0.933s
[17]:
<matplotlib.collections.PathCollection at 0x7f1e5c5486b0>
../_images/examples_Basic-workflow_36_2.svg

Budget scenarios

To run a program-related scenario, such as a budget or coverage scenario, it is not necessary to construct a Scenario object. Instead, you can directly create and use the program instructions that define the scenario:

[18]:
alloc = P.progsets[0].get_alloc(2018)
doubled_budget = {x:v*2 for x,v in alloc.items()}
instructions = at.ProgramInstructions(start_year=2018,alloc=doubled_budget)
res_baseline = P.run_sim(parset='default',progset='default',progset_instructions=at.ProgramInstructions(start_year=2018),result_name='Baseline')
res_budget_scen = P.run_sim(parset='default',progset='default',progset_instructions=instructions,result_name='Doubled');

d = at.PlotData.programs([res_baseline,res_budget_scen]).interpolate(2018)
at.plot_bars(d,stack_outputs='all');
Elapsed time for running "default": 1.35s
Elapsed time for running "default": 1.60s
../_images/examples_Basic-workflow_38_1.svg

Alternatively, you can create a full scenario object by storing the instructions in a CombinedScenario. The CombinedScenario optionally allows you to mix parameter and program scenarios.

[19]:
scen = P.make_scenario(which='combined', name="Doubled (scen)", instructions=instructions)
res_combined_scen = scen.run(P, parset='default',progset='default')

d = at.PlotData.programs([res_baseline,res_budget_scen, res_combined_scen]).interpolate(2018)
at.plot_bars(d,stack_outputs='all');
Elapsed time for running "default": 1.32s
../_images/examples_Basic-workflow_40_1.svg

Coverage scenarios

With coverage scenarios, the program instructions override a program’s coverage. Therefore, the spending values and coverage values may not match up with what is entered in the program book. If running coverage scenarios, take care not to use the spending values for such results - typically this is not a problem, because if you did have a particular spending amount in mind, then it would be better to use a budget scenario.

[20]:
half_coverage = {x:0.5 for x in P.progsets[0].programs.keys()}
instructions = at.ProgramInstructions(start_year=2018,coverage=half_coverage)
scen = at.CombinedScenario(name='Reduced coverage',instructions=instructions)
res_cov_scen = scen.run(P,parset='default',progset='default');
Elapsed time for running "default": 1.30s

Optimization

The role of optimization is to produce a set of program spending overwrites that improves the model output in some way. It is thus an operation that maps one set of program instructions to another, where the optimized program instructions contain the optimized allocation. An optimization consists of three parts

  • adjustments that specify what parts of the program instructions to change, and how to change them

  • measurables that define optimality (e.g. reducing new infections, maximizing people alive)

  • constraints that must be satisfied, such as fixed total spending

An Optimization object contains these three items, as well any additional parameters specific to the optimization algorithm (e.g. the optimization method, the maximum run time).

The optimize function uses the Optimization to modify a particular set of program instructions. It therefore takes in

  • A parset and progset to use

  • A program instructions instance to optimize

  • An optimization object, that specifies how to perform the optimization

[21]:
instructions = at.ProgramInstructions(alloc=P.progsets[0],start_year=2020) # Instructions for default spending
adjustments = [at.SpendingAdjustment(x,2020,'rel',0.,2.) for x in instructions.alloc.keys()]
measurables = at.MaximizeCascadeStage(None,2020)
constraints = at.TotalSpendConstraint() # Cap total spending in all years
optimization = at.Optimization(name='default', adjustments=adjustments, measurables=measurables,constraints=constraints,maxtime=10) # Evaluate from 2020 to end of simulation
optimized_instructions = at.optimize(P, optimization, parset=P.parsets["default"], progset=P.progsets['default'], instructions=instructions)
ASD: Launching with random seed None
    step=1 choice=3, par=3, pm=0.0, origval=893333.3333333334, newval=982666.6666666667
     step 1 (1.3 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
    step=2 choice=5, par=5, pm=0.0, origval=8205000.0, newval=9025500.0
     step 2 (2.8 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
    step=3 choice=17, par=5, pm=1.0, origval=8205000.0, newval=7384500.0
     step 3 (4.0 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
    step=4 choice=2, par=2, pm=0.0, origval=25282133.33333333, newval=27810346.66666666
     step 4 (5.2 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
    step=5 choice=18, par=6, pm=1.0, origval=1246000.0, newval=1121400.0
     step 5 (6.4 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
    step=6 choice=2, par=2, pm=0.0, origval=25282133.33333333, newval=26546239.999999996
     step 6 (7.6 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
    step=7 choice=13, par=1, pm=1.0, origval=25568000.0, newval=23011200.0
     step 7 (9.1 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
    step=8 choice=9, par=9, pm=0.0, origval=961200.0, newval=1057320.0
     step 8 (10.3 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
===  Time limit reached (10.33 > 10.00) (8 steps, orig: -11730 | best: -11730 | ratio: 1.000) ===

The function returns a set of optimized instructions, that can then be used to run a simulation

[22]:
res_optimized = P.run_sim(parset='default',progset='default',progset_instructions=optimized_instructions)
Elapsed time for running "default": 1.31s

For more details on the optimization system, see the general documentation on optimization.