run

run

Functions and classes for running multiple HPVsim runs.

Classes

Name Description
MultiSim Class for running multiple copies of a simulation. The parameter n_runs
Scenarios Class for running multiple sets of multiple simulations – e.g., scenarios.
Sweep Class for running parameter sweeps.

MultiSim

run.MultiSim(sims=None, base_sim=None, label=None, initialize=False, **kwargs)

Class for running multiple copies of a simulation. The parameter n_runs controls how many copies of the simulation there will be, if a list of sims is not provided. This is the main class that’s used to run multiple versions of a simulation (e.g., with different random seeds).

Parameters

Name Type Description Default
sims (Sim/list) a single sim or a list of sims required
base_sim (Sim) the sim used for shared properties; if not supplied, the first of the sims provided required
label (str) the name of the multisim required
initialize (bool) whether or not to initialize the sims (otherwise, initialize them during run) required
kwargs (dict) stored in run_args and passed to run() required

Returns

Name Type Description
msim a MultiSim object

Examples::

sim = hpv.Sim() # Create the sim
msim = hpv.MultiSim(sim, n_runs=5) # Create the multisim
msim.run() # Run them in parallel
msim.combine() # Combine into one sim
msim.plot() # Plot results

sim = hpv.Sim() # Create the sim
msim = hpv.MultiSim(sim, n_runs=11, noise=0.1, keep_people=True) # Set up a multisim with noise
msim.run() # Run
msim.reduce() # Compute statistics
msim.plot() # Plot

sims = [hpv.Sim(beta=0.015*(1+0.02*i)) for i in range(5)] # Create sims
for sim in sims: sim.run() # Run sims in serial
msim = hpv.MultiSim(sims) # Convert to multisim
msim.plot() # Plot as single sim

Methods

Name Description
brief Print a compact representation of the multisim. See also multisim.disp()
combine Combine multiple sims into a single sim with scaled results.
compare Create a dataframe compare sims at a single point in time.
disp Display a verbose description of a multisim. See also multisim.summarize()
init_sims Initialize the sims, but don’t actually run them. Syntax is the same
load Load from disk from a gzipped pickle.
mean Alias for reduce(use_mean=True). See reduce() for full description.
median Alias for reduce(use_mean=False). See reduce() for full description.
merge Convenience method for merging two MultiSim objects.
plot Plot all the sims – arguments passed to Sim.plot(). The
plot_compare Plot a comparison between sims, using bars to show different values for
plot_result Convenience method for plotting – arguments passed to sim.plot_result()
reduce Combine multiple sims into a single sim statistically: by default, use
reset Undo a combine() or reduce() by resetting the base sim, which, and results
result_keys Attempt to retrieve the results keys from the base sim
run Run the actual sims
save Save to disk as a gzipped pickle. Load with hpv.load(filename) or
shrink Not to be confused with reduce(), this shrinks each sim in the msim;
split Convenience method for splitting one MultiSim into several. You can specify
summarize Print a moderate length summary of the MultiSim. See also multisim.disp()
to_excel Shortcut for base_sim.to_excel()
to_json Shortcut for base_sim.to_json()
brief
run.MultiSim.brief(output=False)

Print a compact representation of the multisim. See also multisim.disp() (detailed output) and multisim.summarize() (medium length output).

Parameters
Name Type Description Default
output bool if true, return a string instead of printing output False

Example::

msim = hpv.MultiSim(hpv.Sim(verbose=0), label='Example multisim')
msim.run()
msim.brief() # Prints one-line output
combine
run.MultiSim.combine(output=False)

Combine multiple sims into a single sim with scaled results.

Example::

msim = hpv.MultiSim(hpv.Sim())
msim.run()
msim.combine()
msim.summarize()
compare
run.MultiSim.compare(
    t=None,
    sim_inds=None,
    output=False,
    do_plot=False,
    show_match=False,
    **kwargs,
)

Create a dataframe compare sims at a single point in time.

Parameters
Name Type Description Default
t (int/str) the day (or date) to do the comparison; default, the end required
sim_inds (list) list of integers of which sims to include (default: all) required
output (bool) whether or not to return the comparison as a dataframe required
do_plot (bool) whether or not to plot the comparison (see also plot_compare()) required
show_match (bool) whether to include a column for whether all sims match required
kwargs (dict) passed to plot_compare() required
Returns
Name Type Description
df dataframe a dataframe comparison
disp
run.MultiSim.disp(output=False)

Display a verbose description of a multisim. See also multisim.summarize() (medium length output) and multisim.brief() (short output).

Parameters
Name Type Description Default
output bool if true, return a string instead of printing output False

Example::

msim = hpv.MultiSim(hpv.Sim(verbose=0), label='Example multisim')
msim.run()
msim.disp() # Displays detailed output
init_sims
run.MultiSim.init_sims(**kwargs)

Initialize the sims, but don’t actually run them. Syntax is the same as MultiSim.run(). Note: in most cases you can just call run() directly, there is no need to call this separately.

Parameters
Name Type Description Default
kwargs dict passed to multi_run() {}
load
run.MultiSim.load(msimfile, *args, **kwargs)

Load from disk from a gzipped pickle.

Parameters
Name Type Description Default
msimfile str the name or path of the file to load from required
kwargs passed to hpv.load() {}
Returns
Name Type Description
msim MultiSim the loaded MultiSim object

Example::

msim = hpv.MultiSim.load('my-multisim.msim')
mean
run.MultiSim.mean(bounds=None, **kwargs)

Alias for reduce(use_mean=True). See reduce() for full description.

Parameters
Name Type Description Default
bounds float multiplier on the standard deviation for the upper and lower bounds (default, 2) None
kwargs dict passed to reduce() {}
median
run.MultiSim.median(quantiles=None, **kwargs)

Alias for reduce(use_mean=False). See reduce() for full description.

Parameters
Name Type Description Default
quantiles list or dict upper and lower quantiles (default, 0.1 and 0.9) None
kwargs dict passed to reduce() {}
merge
run.MultiSim.merge(*args, base=False)

Convenience method for merging two MultiSim objects.

Parameters
Name Type Description Default
args MultiSim the MultiSims to merge (either a list, or separate) ()
base bool if True, make a new list of sims from the multisim’s two base sims; otherwise, merge the multisim’s lists of sims False
Returns
Name Type Description
msim MultiSim a new MultiSim object

Examples:

mm1 = hpv.MultiSim.merge(msim1, msim2, base=True)
mm2 = hpv.MultiSim.merge([m1, m2, m3, m4], base=False)
plot
run.MultiSim.plot(
    to_plot=None,
    inds=None,
    plot_sims=False,
    color_by_sim=None,
    max_sims=5,
    colors=None,
    labels=None,
    alpha_range=None,
    plot_args=None,
    show_args=None,
    **kwargs,
)

Plot all the sims – arguments passed to Sim.plot(). The behavior depends on whether or not combine() or reduce() has been called. If so, this function by default plots only the combined/reduced sim (which you can override with plot_sims=True). Otherwise, it plots a separate line for each sim.

Note that this function is complex because it aims to capture the flexibility of both sim.plot() and scens.plot(). By default, if combine() or reduce() has been used, it will resemble sim.plot(); otherwise, it will resemble scens.plot(). This can be changed via color_by_sim, together with the other options.

Parameters
Name Type Description Default
to_plot (list) list or dict of which results to plot; see hpv.get_default_plots() for structure required
inds (list) if not combined or reduced, the indices of the simulations to plot (if None, plot all) required
plot_sims (bool) whether to plot individual sims, even if combine() or reduce() has been used required
color_by_sim (bool) if True, set colors based on the simulation type; otherwise, color by result type; True implies a scenario-style plotting, False implies sim-style plotting required
max_sims (int) maximum number of sims to use with color-by-sim; can be overridden by other options required
colors (list) if supplied, override default colors for color_by_sim required
labels (list) if supplied, override default labels for color_by_sim required
alpha_range (list) a 2-element list/tuple/array providing the range of alpha values to use to distinguish the lines required
plot_args (dict) passed to sim.plot() required
show_args (dict) passed to sim.plot() required
kwargs (dict) passed to sim.plot() required
Returns
Name Type Description
fig Figure handle

Examples::

sim = hpv.Sim()
msim = hpv.MultiSim(sim)
msim.run()
msim.plot() # Plots individual sims
msim.reduce()
msim.plot() # Plots the combined sim
plot_compare
run.MultiSim.plot_compare(t=-1, sim_inds=None, log_scale=True, **kwargs)

Plot a comparison between sims, using bars to show different values for each result. For an explanation of other available arguments, see Sim.plot().

Parameters
Name Type Description Default
t (int) index of results, passed to compare() required
sim_inds (list) which sims to include, passed to compare() required
log_scale (bool) whether to plot with a logarithmic x-axis required
kwargs (dict) standard plotting arguments, see Sim.plot() for explanation required
Returns
Name Type Description
fig Figure handle
plot_result
run.MultiSim.plot_result(key, colors=None, labels=None, *args, **kwargs)

Convenience method for plotting – arguments passed to sim.plot_result()

reduce
run.MultiSim.reduce(quantiles=None, use_mean=False, bounds=None, output=False)

Combine multiple sims into a single sim statistically: by default, use the median value and the 10th and 90th percentiles for the lower and upper bounds. If use_mean=True, then use the mean and ±2 standard deviations for lower and upper bounds.

Parameters
Name Type Description Default
quantiles dict the quantiles to use, e.g. [0.1, 0.9] or {‘low : ’0.1, ’high’ : 0.9} None
use_mean bool whether to use the mean instead of the median False
bounds float if use_mean=True, the multiplier on the standard deviation for upper and lower bounds (default 2) None
output bool whether to return the “reduced” sim (in any case, modify the multisim in-place) False

Example::

msim = hpv.MultiSim(hpv.Sim())
msim.run()
msim.reduce()
msim.summarize()
reset
run.MultiSim.reset()

Undo a combine() or reduce() by resetting the base sim, which, and results

result_keys
run.MultiSim.result_keys()

Attempt to retrieve the results keys from the base sim

run
run.MultiSim.run(reduce=False, combine=False, **kwargs)

Run the actual sims

Parameters
Name Type Description Default
reduce bool whether or not to reduce after running (see reduce()) False
combine bool whether or not to combine after running (see combine(), not compatible with reduce) False
kwargs dict passed to multi_run(); use run_args to pass arguments to sim.run() {}
Returns
Name Type Description
None (modifies MultiSim object in place)

Examples::

msim.run()
msim.run(run_args=dict(until='2020-0601', restore_pars=False))
save
run.MultiSim.save(filename=None, keep_people=False, **kwargs)

Save to disk as a gzipped pickle. Load with hpv.load(filename) or hpv.MultiSim.load(filename).

Parameters
Name Type Description Default
filename (str) the name or path of the file to save to; if None, uses default required
keep_people (bool) whether or not to store the population in the Sim objects (NB, very large) required
kwargs (dict) passed to sc.makefilepath() required
Returns
Name Type Description
scenfile str the validated absolute path to the saved file

Example::

msim.save() # Saves to an .msim file
shrink
run.MultiSim.shrink(**kwargs)

Not to be confused with reduce(), this shrinks each sim in the msim; see sim.shrink() for more information.

Parameters
Name Type Description Default
kwargs dict passed to sim.shrink() for each sim {}
split
run.MultiSim.split(inds=None, chunks=None)

Convenience method for splitting one MultiSim into several. You can specify either individual indices of simulations to extract, via inds, or consecutive chunks of indices, via chunks. If this function is called on a merged MultiSim, the chunks can be retrieved automatically and no arguments are necessary.

Parameters
Name Type Description Default
inds list a list of lists of indices, with each list turned into a MultiSim None
chunks int or list if an int, split the MultiSim into that many chunks; if a list return chunks of that many sims None
Returns
Name Type Description
A list of MultiSim objects

Examples::

m1 = hpv.MultiSim(hpv.Sim(label='sim1'), initialize=True)
m2 = hpv.MultiSim(hpv.Sim(label='sim2'), initialize=True)
m3 = hpv.MultiSim.merge(m1, m2)
m3.run()
m1b, m2b = m3.split()

msim = hpv.MultiSim(hpv.Sim(), n_runs=6)
msim.run()
m1, m2 = msim.split(inds=[[0,2,4], [1,3,5]])
mlist1 = msim.split(chunks=[2,4]) # Equivalent to inds=[[0,1], [2,3,4,5]]
mlist2 = msim.split(chunks=2) # Equivalent to inds=[[0,1,2], [3,4,5]]
summarize
run.MultiSim.summarize(output=False)

Print a moderate length summary of the MultiSim. See also multisim.disp() (detailed output) and multisim.brief() (short output).

Parameters
Name Type Description Default
output bool if true, return a string instead of printing output False

Example::

msim = hpv.MultiSim(hpv.Sim(verbose=0), label='Example multisim')
msim.run()
msim.summarize() # Prints moderate length output
to_excel
run.MultiSim.to_excel(*args, **kwargs)

Shortcut for base_sim.to_excel()

to_json
run.MultiSim.to_json(*args, **kwargs)

Shortcut for base_sim.to_json()

Scenarios

run.Scenarios(
    sim=None,
    metapars=None,
    scenarios=None,
    basepars=None,
    scenfile=None,
    label=None,
)

Class for running multiple sets of multiple simulations – e.g., scenarios. Note that most users are recommended to use MultiSim rather than Scenarios, as it gives more control over run options. Scenarios should be used primarily for quick investigations. See the examples folder for example usage.

Parameters

Name Type Description Default
sim (Sim) if supplied, use a pre-created simulation as the basis for the scenarios required
metapars (dict) meta-parameters for the run, e.g. number of runs; see make_metapars() for structure required
scenarios (dict) a dictionary defining the scenarios; see examples folder for examples; see below for baseline required
basepars (dict) a dictionary of sim parameters to be used for the basis of the scenarios (not required if sim is provided) required
scenfile (str) a filename for saving (defaults to the creation date) required
label (str) the name of the scenarios required

Example::

scens = hpv.Scenarios()

Returns

Name Type Description
scens a Scenarios object

Methods

Name Description
brief Print a compact representation of the scenarios. See also scenarios.disp()
compare Print out a comparison of each scenario.
disp Display a verbose description of the scenarios. See also scenarios.summarize()
load Load from disk from a gzipped pickle.
merge Merge two or more scenarios to create a single scenario object
plot Plot the results of a scenario. For an explanation of available arguments,
plot_age_results Plot all age_results analyzers in a scenario together.
result_keys Attempt to retrieve the results keys from the base sim
run Run the specified scenarios.
save Save to disk as a gzipped pickle.
summarize Print a moderate length summary of the scenarios. See also scenarios.disp()
to_excel Export results as XLSX
to_json Export results as JSON.
brief
run.Scenarios.brief(output=False)

Print a compact representation of the scenarios. See also scenarios.disp() (detailed output) and scenarios.summarize() (medium length output).

Parameters
Name Type Description Default
output bool if true, return a string instead of printing output False

Example::

scens = hpv.Scenarios(label='Example scenarios')
scens.run()
scens.brief() # Prints one-line output
compare
run.Scenarios.compare(t=None, output=False)

Print out a comparison of each scenario.

Parameters
Name Type Description Default
t (int/str) the day (or date) to do the comparison; default, the end required
output (bool) if true, return the dataframe instead of printing output required

Example::

scenarios = {'base': {'name':'Base','pars': {}}, 'beta': {'name':'Beta', 'pars': {'beta': 0.020}}}
scens = hpv.Scenarios(scenarios=scenarios, label='Example scenarios')
scens.run()
scens.compare(t=30) # Prints comparison for day 30
disp
run.Scenarios.disp(output=False)

Display a verbose description of the scenarios. See also scenarios.summarize() (medium length output) and scenarios.brief() (short output).

Parameters
Name Type Description Default
output bool if true, return a string instead of printing output False

Example::

scens = hpv.Scenarios(hpv.Sim(), label='Example scenarios')
scens.run(verbose=0) # Run silently
scens.disp() # Displays detailed output
load
run.Scenarios.load(scenfile, *args, **kwargs)

Load from disk from a gzipped pickle.

Parameters
Name Type Description Default
scenfile str the name or path of the file to load from required
kwargs passed to hpv.load() {}
Returns
Name Type Description
scens Scenarios the loaded scenarios object

Example::

scens = hpv.Scenarios.load('my-scenarios.scens')
merge
run.Scenarios.merge(*args)

Merge two or more scenarios to create a single scenario object Args: args (Scenarios): the Scenarios to merge (either a list, or separate) Returns: scen (Scenarios): a new Scenario object

plot
run.Scenarios.plot(*args, **kwargs)

Plot the results of a scenario. For an explanation of available arguments, see Sim.plot().

Returns
Name Type Description
fig Figure handle

Example::

scens = hpv.Scenarios()
scens.run()
scens.plot()
plot_age_results
run.Scenarios.plot_age_results(*args, **kwargs)

Plot all age_results analyzers in a scenario together. Returns: fig: Figure handle

Example::

az = hpv.age_results(timepoints=['2015', '2020'], results=['hpv_incidence', 'total_cancers'])
base_sim = hpv.Sim(analyzers=az)
scens = hpv.Scenarios(sim=base_sim)
scens.run()
scens.plot_age_results()
result_keys
run.Scenarios.result_keys(which='all')

Attempt to retrieve the results keys from the base sim

run
run.Scenarios.run(
    n_runs=None,
    debug=False,
    keep_people=False,
    verbose=None,
    **kwargs,
)

Run the specified scenarios.

Parameters
Name Type Description Default
debug (bool) if True, runs a single run instead of multiple, which makes debugging easier required
verbose (int) level of detail to print, passed to sim.run() required
kwargs (dict) passed to multi_run() and thence to sim.run() required
Returns
Name Type Description
None (modifies Scenarios object in place)
save
run.Scenarios.save(scenfile=None, keep_sims=True, keep_people=False, **kwargs)

Save to disk as a gzipped pickle.

Parameters
Name Type Description Default
scenfile (str) the name or path of the file to save to; if None, uses stored required
keep_sims (bool) whether or not to store the actual Sim objects in the Scenarios object required
keep_people (bool) whether or not to store the population in the Sim objects (NB, very large) required
kwargs (dict) passed to makefilepath() required
Returns
Name Type Description
scenfile str the validated absolute path to the saved file

Example::

scens.save() # Saves to a .scens file with the date and time of creation by default
summarize
run.Scenarios.summarize(output=False)

Print a moderate length summary of the scenarios. See also scenarios.disp() (detailed output) and scenarios.brief() (short output).

Parameters
Name Type Description Default
output bool if true, return a string instead of printing output False

Example::

scens = hpv.Scenarios(hpv.Sim(), label='Example scenarios')
scens.run(verbose=0) # Run silently
scens.summarize() # Prints moderate length output
to_excel
run.Scenarios.to_excel(filename=None)

Export results as XLSX

Parameters
Name Type Description Default
filename str if None, return string; else, write to file None
Returns
Name Type Description
An sc.Spreadsheet with an Excel file, or writes the file to disk
to_json
run.Scenarios.to_json(
    filename=None,
    tostring=True,
    indent=2,
    verbose=False,
    *args,
    **kwargs,
)

Export results as JSON.

Parameters
Name Type Description Default
filename str if None, return string; else, write to file None
Returns
Name Type Description
A unicode string containing a JSON representation of the results,
or writes the JSON file to disk

Sweep

run.Sweep(
    base_sim=None,
    sweep_pars=None,
    sweep_vars=None,
    n_draws=4,
    initialize=False,
    label=None,
    **kwargs,
)

Class for running parameter sweeps.

Parameters

Name Type Description Default
base_sim (Sim) the sim used for shared properties required
label (str) the name of the multisim required
kwargs (dict) stored in run_args and passed to run() required

Returns

Name Type Description
sweep a Sweep object

Examples: sim = hpv.Sim() # Create the sim sweep = hpv.Sweep(sim, sweep_pars={‘beta’:[0.4, 0.8]}, n_runs=5) # Create the sweep sweep.run() # Run the sims in parallel sweep.plot() # Plot results

Methods

Name Description
reduce Create a dataframe holding reduced results
run Run the sims
reduce
run.Sweep.reduce(sum_totals=True, from_year=None, to_year=None)

Create a dataframe holding reduced results

run
run.Sweep.run(
    shrink=True,
    reduce=True,
    sum_totals=True,
    from_year=None,
    to_year=None,
    **kwargs,
)

Run the sims Args: shrink (bool): whether or not to shrink after running kwargs (dict): passed to multi_run(); use run_args to pass arguments to sim.run()

Returns
Name Type Description
None (modifies MultiSim object in place)

Examples:: sweep.run()

Functions

Name Description
make_metapars Create default metaparameters for a Scenarios run
multi_run For running multiple runs in parallel. If the first argument is a list of sims,
parallel A shortcut to hpv.MultiSim(), allowing the quick running of multiple simulations
single_run Convenience function to perform a single simulation run. Mostly used for

make_metapars

run.make_metapars()

Create default metaparameters for a Scenarios run

multi_run

run.multi_run(
    sim,
    n_runs=4,
    reseed=None,
    noise=0.0,
    noisepar=None,
    iterpars=None,
    combine=False,
    keep_people=None,
    run_args=None,
    sim_args=None,
    par_args=None,
    do_run=True,
    parallel=True,
    n_cpus=None,
    verbose=None,
    **kwargs,
)

For running multiple runs in parallel. If the first argument is a list of sims, exactly these will be run and most other arguments will be ignored.

Parameters

Name Type Description Default
sim (Sim) the sim instance to be run, or a list of sims. required
n_runs (int) the number of parallel runs required
reseed (bool) whether or not to generate a fresh seed for each run (default: true for single, false for list of sims) required
noise (float) the amount of noise to add to each run required
noisepar (str) the name of the parameter to add noise to required
iterpars (dict) any other parameters to iterate over the runs; see sc.parallelize() for syntax required
combine (bool) whether or not to combine all results into one sim, rather than return multiple sim objects required
keep_people (bool) whether to keep the people after the sim run (default false) required
run_args (dict) arguments passed to sim.run() required
sim_args (dict) extra parameters to pass to the sim required
par_args (dict) arguments passed to sc.parallelize() required
do_run (bool) whether to actually run the sim (if not, just initialize it) required
parallel (bool) whether to run in parallel using multiprocessing (else, just run in a loop) required
n_cpus (int) the number of CPUs to run on (if blank, set automatically; otherwise, passed to par_args) required
verbose (int) detail to print required
kwargs (dict) also passed to the sim required

Returns

Name Type Description
If combine is True, a single sim object with the combined results from each sim.
Otherwise, a list of sim objects (default).

Example::

import hpvsim as hpv
sim = hpv.Sim()
sims = hpv.multi_run(sim, n_runs=6, noise=0.2)

parallel

run.parallel(*args, **kwargs)

A shortcut to hpv.MultiSim(), allowing the quick running of multiple simulations at once.

Parameters

Name Type Description Default
args list The simulations to run ()
kwargs dict passed to multi_run() {}

Returns

Name Type Description
A run MultiSim object.

Examples::

s1 = hpv.Sim(beta=0.01, label='Low')
s2 = hpv.Sim(beta=0.02, label='High')
hpv.parallel(s1, s2).plot()
msim = hpv.parallel([s1, s2], keep_people=True)

single_run

run.single_run(
    sim,
    ind=0,
    reseed=True,
    noise=0.0,
    noisepar=None,
    keep_people=False,
    run_args=None,
    sim_args=None,
    verbose=None,
    do_run=True,
    **kwargs,
)

Convenience function to perform a single simulation run. Mostly used for parallelization, but can also be used directly.

Parameters

Name Type Description Default
sim (Sim) the sim instance to be run required
ind (int) the index of this sim required
reseed (bool) whether or not to generate a fresh seed for each run required
noise (float) the amount of noise to add to each run required
noisepar (str) the name of the parameter to add noise to required
keep_people (bool) whether to keep the people after the sim run required
run_args (dict) arguments passed to sim.run() required
sim_args (dict) extra parameters to pass to the sim, e.g. ‘n_infected’ required
verbose (int) detail to print required
do_run (bool) whether to actually run the sim (if not, just initialize it) required
kwargs (dict) also passed to the sim required

Returns

Name Type Description
sim Sim a single sim object with results

Example::

import hpvsim as hp
sim = hpv.Sim() # Create a default simulation
sim = hpv.single_run(sim) # Run it, equivalent(ish) to sim.run()