T2 - Plotting, printing, and saving#

This tutorial provides a brief overview of options for plotting results, printing objects, and saving results.

Click here to open an interactive version of this notebook.

Global plotting configuration#

HPVsim 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 hpv.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:

[1]:
import hpvsim as hpv

hpv.options(jupyter=True, verbose=0) # Standard options for Jupyter notebook

sim = hpv.Sim()
sim.run()
HPVsim 2.0.2 (2024-03-05) — © 2022-2024 by IDM
Loading location-specific demographic data for "nigeria"
[1]:
Sim(<no label>; 1995.0 to 2030.0; pop: 20000 default; epi: 8.74112e+08⚙, 510599♋︎)

Printing objects#

There are three levels of detail available for most objects (sims, multisims, scenarios, and people). The shortest is brief():

[2]:
sim.brief()
Sim(<no label>; 1995.0 to 2030.0; pop: 20000 default; epi: 8.74112e+08⚙, 510599♋︎)

You can get more detail with summarize():

[3]:
sim.summarize()
Simulation summary:
     874,111,895 total HPV infections
         510,599 total cancers
         286,811 total cancer deaths
            5.14 mean HPV prevalence (%)
           15.21 mean cancer incidence (per 100k)
           32.94 mean age of infection (years)
           47.79 mean age of cancer (years)

Finally, to show the full object, including all methods and attributes, use disp():

[4]:
sim.disp()
<hpvsim.sim.Sim at 0x7fb0e85b7550>
[<class 'hpvsim.sim.Sim'>, <class 'hpvsim.base.BaseSim'>, <class 'hpvsim.base.ParsObj'>, <class 'hpvsim.base.FlexPretty'>, <class 'sciris.sc_printing.prettyobj'>]
————————————————————————————————————————————————————————————
Methods:
  _brief()            get_intervention()  reset_layer_pars()
  _disp()             get_interventio...  result_keys()
  _get_ia()           get_t()             result_types()
  brief()             init_analyzers()    run()
  compute_age_mean()  init_genotypes()    save()
  compute_fit()       init_hiv()          set_metadata()
  compute_results()   init_immunity()     shrink()
  compute_states()    init_interventi...  step()
  compute_summary()   init_people()       summarize()
  copy()              init_results()      to_df()
  disp()              init_states()       to_excel()
  export_pars()       init_time_vecs()    to_json()
  export_results()    initialize()        update_pars()
  finalize()          layer_keys()        validate_dt()
  finalize_analyz...  load()              validate_init_c...
  get_analyzer()      load_data()         validate_layer_...
  get_analyzers()     plot()              validate_pars()
————————————————————————————————————————————————————————————
Properties:
  n
————————————————————————————————————————————————————————————
 _default_ver: None
   _orig_pars: {'n_agents': 20000, 'total_pop': None, 'pop_scale':
               5340.99025, 'ms [...]
    analyzers: []
 art_datafile: None
     complete: True
      created: None
         data: None
     datafile: None
 hiv_datafile: None
       hivsim: <hpvsim.hiv.HIVsim at 0x7fb0b4db0130>

  initialized: True
interventions: []
        label: None
         npts: 144
         pars: {'n_agents': 20000, 'total_pop': None, 'pop_scale':
               5340.99025, 'ms [...]
       people: People(n=70242; layers: m, c)
      popdict: None
      popfile: None
     res_npts: 36
     res_tvec: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,
               12, 13, 14,  [...]
  res_yearvec: array([1995., 1996., 1997., 1998., 1999., 2000., 2001.,
               2002., 2003 [...]
      resfreq: 4
      results: #0. 'infections':
               <hpvsim.base.Result at 0x7fb0b4db01c0>
               [<class 'h [...]
results_ready: True
short_summary: #0: 'total HPV infections':             874111895.0625
               #1: 'total c [...]
      summary: #0. 'infections':                31485140.96875
               #1. 'dysplasias':   [...]
            t: 143
         tvec: array([  0,   1,   2,   3,   4,   5,   6,   7,   8,
               9,  10,  11,  [...]
        years: array([1995., 1996., 1997., 1998., 1999., 2000., 2001.,
               2002., 2003 [...]
      yearvec: array([1995.  , 1995.25, 1995.5 , 1995.75, 1996.  ,
               1996.25, 1996.5 [...]
————————————————————————————————————————————————————————————

Plotting options#

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:

[5]:
import pylab as plt # Shortcut for import matplotlib.pyplot as plt
plt.plot(sim.results['year'], sim.results['infections']);
../_images/tutorials_tut_plotting_11_0.png

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.infections.values.)

An alternative, you can also select one or more quantities to plot with the first (to_plot) argument, e.g.

[6]:
sim.plot(to_plot=['infections', 'hpv_incidence']);
../_images/tutorials_tut_plotting_13_0.png

While we can save this figure using Matplotlib’s built-in savefig(), if we use HPVsim’s hpv.savefig() we get a couple of advantages:

[7]:
hpv.savefig('my-fig.png')
[7]:
'my-fig.png'
<Figure size 640x480 with 0 Axes>

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.

[8]:
hpv.get_png_metadata('my-fig.png')
HPVsim version: 2.0.2
HPVsim branch: Branch N/A
HPVsim hash: Hash N/A
HPVsim date: Date N/A
HPVsim caller branch: Branch N/A
HPVsim caller hash: Hash N/A
HPVsim caller date: Date N/A
HPVsim caller filename: /home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/latest/lib/python3.9/site-packages/hpvsim/misc.py
HPVsim current time: 2024-Mar-15 03:55:58
HPVsim calling file: /tmp/ipykernel_1327/1797766398.py

Customizing plots#

We saw above how to set default plot configuration options for Jupyter. HPVsim 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 HPVsim, or just for the current plot. To give an example with changing the figure DPI: - Change the setting globally (for both HPVsim and Matplotlib): sc.options(dpi=150) or pl.rc('figure', dpi=150) (where sc is import sciris as sc) - Change for HPVsim plots, but not for Matplotlib plots: hpv.options(dpi=150) - Change for the current HPVsim plot, but not other HPVsim plots: sim.plot(dpi=150)

The easiest way to change the style of HPVsim plots is with the style argument. For example, to plot using a built-in Matplotlib style would simply be:

[9]:
sim.plot(style='ggplot');
../_images/tutorials_tut_plotting_19_0.png

In addition to the default style ('hpvsim'), there is also a “simple” style. You can combine built-in styles with additional overrides, including any valid Matplotlib commands:

[10]:
sim.plot(style='simple', legend_args={'frameon':True}, style_args={'ytick.direction':'in'});
../_images/tutorials_tut_plotting_21_0.png

Although most style handling is done automatically, you can also use it yourself in a with block, e.g.:

[11]:
import numpy as np
with hpv.options.with_style(fontsize=6):
    sim.plot() # This will have 6 point font
    plt.figure(); plt.plot(np.random.rand(20), 'o') # So will this
../_images/tutorials_tut_plotting_23_0.png
../_images/tutorials_tut_plotting_23_1.png

Saving options#

Saving sims is also pretty simple. The simplest way to save is simply

[12]:
sim.save('my-sim.sim')
[12]:
'/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/checkouts/latest/docs/tutorials/my-sim.sim'

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:

[13]:
sim_orig = hpv.Sim(start=2000, end=2030, label='Load & save example')
sim_orig.run(until='2015')
sim_orig.save('my-half-finished-sim.sim') # Note: HPVsim always saves the people if the sim isn't finished running yet

sim = hpv.load('my-half-finished-sim.sim')
sim['beta'] *= 0.3
sim.run()
sim.plot(['infections', 'hpv_incidence', 'cancer_incidence']);
Loading location-specific demographic data for "nigeria"
../_images/tutorials_tut_plotting_27_1.png

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:

[14]:
import pandas as pd

sim.to_excel('my-sim.xlsx')
df = pd.read_excel('my-sim.xlsx')
print(df)
Object saved to /home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/checkouts/latest/docs/tutorials/my-sim.xlsx.
    year   t    infections  dysplasias  precins          cins       cancers  \
0   2000   0  3.028098e+07           0        0  3.491582e+05      0.000000
1   2001   1  2.619777e+07           0        0  6.807372e+05      0.000000
2   2002   2  2.694640e+07           0        0  8.850191e+05      0.000000
3   2003   3  2.712886e+07           0        0  9.729148e+05    606.177429
4   2004   4  2.650147e+07           0        0  9.523047e+05   3637.064575
5   2005   5  2.603532e+07           0        0  1.008679e+06   1818.532288
6   2006   6  2.569950e+07           0        0  1.034745e+06   2424.709717
7   2007   7  2.461626e+07           0        0  1.024440e+06   3637.064575
8   2008   8  2.425195e+07           0        0  1.060204e+06   4849.419373
9   2009   9  2.350271e+07           0        0  9.341195e+05   8486.484009
10  2010  10  2.365547e+07           0        0  9.577604e+05   7880.306519
11  2011  11  2.214790e+07           0        0  9.032044e+05   8486.484131
12  2012  12  2.190968e+07           0        0  9.104785e+05  10305.016235
13  2013  13  2.127440e+07           0        0  7.268067e+05   9092.661377
14  2014  14  2.047485e+07           0        0  8.037913e+05  10305.016357
15  2015  15  1.674020e+07           0        0  9.044167e+05  13942.080566
16  2016  16  1.535750e+07           0        0  8.516793e+05  15760.613220
17  2017  17  1.521202e+07           0        0  7.934863e+05  14548.258240
18  2018  18  1.426032e+07           0        0  6.486099e+05  15760.613037
19  2019  19  1.413788e+07           0        0  6.667952e+05  15154.435791
20  2020  20  1.308313e+07           0        0  6.801311e+05  16366.790894
21  2021  21  1.290976e+07           0        0  4.667566e+05  24853.274353
22  2022  22  1.181985e+07           0        0  6.455790e+05  29702.693909
23  2023  23  1.124641e+07           0        0  5.352547e+05  23640.919983
24  2024  24  1.062750e+07           0        0  4.734245e+05  23640.919922
25  2025  25  1.015893e+07           0        0  4.528145e+05  26671.807556
26  2026  26  1.006073e+07           0        0  4.910037e+05  23034.742615
27  2027  27  1.019530e+07           0        0  4.509960e+05  19397.677795
28  2028  28  1.011892e+07           0        0  3.958338e+05  18185.323120
29  2029  29  1.049354e+07           0        0  4.206871e+05  21822.387695
30  2030  30  1.006739e+07           0        0  4.309921e+05  21216.210022

    detected_cancers  cancer_deaths  detected_cancer_deaths  ...  \
0                  0       0.000000                       0  ...
1                  0       0.000000                       0  ...
2                  0       0.000000                       0  ...
3                  0       0.000000                       0  ...
4                  0       0.000000                       0  ...
5                  0       0.000000                       0  ...
6                  0       0.000000                       0  ...
7                  0       0.000000                       0  ...
8                  0       0.000000                       0  ...
9                  0    1212.354858                       0  ...
10                 0       0.000000                       0  ...
11                 0    1212.354858                       0  ...
12                 0    2424.709717                       0  ...
13                 0    3030.887085                       0  ...
14                 0    3637.064514                       0  ...
15                 0    4849.419312                       0  ...
16                 0    5455.596741                       0  ...
17                 0    8486.484009                       0  ...
18                 0    6667.951599                       0  ...
19                 0    7274.129150                       0  ...
20                 0    6667.951599                       0  ...
21                 0   12729.725586                       0  ...
22                 0    8486.484070                       0  ...
23                 0    9092.661499                       0  ...
24                 0    9092.661377                       0  ...
25                 0   18185.323242                       0  ...
26                 0   18791.500000                       0  ...
27                 0   13942.081055                       0  ...
28                 0   18185.322754                       0  ...
29                 0   23034.741211                       0  ...
30                 0   17579.145264                       0  ...

    cum_cancer_treated  detected_cancer_incidence  cancer_mortality  \
0                    0                          0          0.000000
1                    0                          0          0.000000
2                    0                          0          0.000000
3                    0                          0          0.000000
4                    0                          0          0.000000
5                    0                          0          0.000000
6                    0                          0          0.000000
7                    0                          0          0.000000
8                    0                          0          0.000000
9                    0                          0          1.611747
10                   0                          0          0.000000
11                   0                          0          1.530292
12                   0                          0          2.976789
13                   0                          0          3.621719
14                   0                          0          4.229850
15                   0                          0          5.486969
16                   0                          0          6.016083
17                   0                          0          9.106817
18                   0                          0          6.971203
19                   0                          0          7.421334
20                   0                          0          6.637062
21                   0                          0         12.384194
22                   0                          0          8.062473
23                   0                          0          8.424222
24                   0                          0          8.230769
25                   0                          0         16.044755
26                   0                          0         16.144826
27                   0                          0         11.709186
28                   0                          0         14.923221
29                   0                          0         18.456727
30                   0                          0         13.809130

      n_alive       cdr       cbr  hpv_prevalence  precin_prevalence  \
0   120716592  0.016270  0.042984        0.062541           0.059004
1   123808088  0.013171  0.042058        0.062840           0.059316
2   127039624  0.011996  0.042181        0.061959           0.057311
3   130340872  0.012245  0.042042        0.062149           0.056264
4   133787600  0.012813  0.041775        0.062572           0.055574
5   137421632  0.012373  0.041729        0.061582           0.054169
6   141088384  0.013023  0.041590        0.060060           0.051769
7   144849728  0.011948  0.041472        0.058248           0.049805
8   148751696  0.011451  0.041484        0.056759           0.047369
9   152804592  0.011576  0.041257        0.055723           0.045760
10  156990256  0.010676  0.040968        0.053764           0.043611
11  161281984  0.011903  0.040629        0.051466           0.041167
12  165779216  0.011061  0.040112        0.049408           0.039207
13  170442528  0.012131  0.039441        0.046782           0.037176
14  175020992  0.010719  0.039033        0.045586           0.035397
15  179598240  0.007287  0.038646        0.040054           0.032341
16  184113648  0.011171  0.038159        0.037210           0.029231
17  188909728  0.009890  0.037768        0.034825           0.027119
18  193777936  0.010639  0.037320        0.033122           0.025857
19  198635856  0.009952  0.036956        0.030950           0.024027
20  203576192  0.010377  0.036923        0.028982           0.021790
21  208628688  0.010867  0.036900        0.027145           0.021045
22  213783008  0.010574  0.036890        0.025650           0.019640
23  219034928  0.009708  0.036946        0.024668           0.018881
24  224305632  0.009559  0.036943        0.022390           0.017041
25  229660624  0.009734  0.036952        0.021159           0.016198
26  235216224  0.008257  0.036981        0.020177           0.015476
27  240693040  0.010139  0.036921        0.019676           0.014845
28  246307456  0.009829  0.036916        0.019076           0.014950
29  251974016  0.008728  0.036976        0.018818           0.014701
30  257683584  0.009212  0.036956        0.018433           0.014366

    cin_prevalence  lsil_prevalence
0         0.001706                0
1         0.003723                0
2         0.005839                0
3         0.007317                0
4         0.008413                0
5         0.009179                0
6         0.009632                0
7         0.009897                0
8         0.010123                0
9         0.010334                0
10        0.010339                0
11        0.009990                0
12        0.009962                0
13        0.009081                0
14        0.009411                0
15        0.009108                0
16        0.009211                0
17        0.008519                0
18        0.008135                0
19        0.007368                0
20        0.007468                0
21        0.006528                0
22        0.006187                0
23        0.005845                0
24        0.005617                0
25        0.005252                0
26        0.004845                0
27        0.004561                0
28        0.004125                0
29        0.004044                0
30        0.003911                0

[31 rows x 65 columns]