poliosim.analysis module¶
Additional analysis functions.
- class Analyzer(label=None)[source]¶
Bases:
sciris.sc_utils.prettyobj
Base class for analyzers. Based on the Intervention class. Analyzers are used to provide more detailed information about a simulation than is available by default – for example, pulling states out of sim.people on a particular timestep before it gets updated in the next timestep.
To retrieve a particular analyzer from a sim, use sim.get_analyzer().
- Parameters
label (str) – a label for the Analyzer (used for ease of identification)
- finalize(sim=None)[source]¶
Finalize analyzer
This method is run once as part of sim.finalize() enabling the analyzer to perform any final operations after the simulation is complete (e.g. rescaling)
- apply(sim)[source]¶
Apply analyzer at each time point. The analyzer has full access to the sim object, and typically stores data/results in itself. This is the core method which each analyzer object needs to implement.
- Parameters
sim – the Sim instance
- shrink(in_place=False)[source]¶
Remove any excess stored data from the intervention; for use with sim.shrink().
- Parameters
in_place (bool) – whether to shrink the intervention (else shrink a copy)
- to_json()[source]¶
Return JSON-compatible representation
Custom classes can’t be directly represented in JSON. This method is a one-way export to produce a JSON-compatible representation of the intervention. This method will attempt to JSONify each attribute of the intervention, skipping any that fail.
- Returns
JSON-serializable representation
- class snapshot(days, *args, **kwargs)[source]¶
Bases:
poliosim.analysis.Analyzer
Analyzer that takes a “snapshot” of the sim.people array at specified points in time, and saves them to itself. To retrieve them, you can either access the dictionary directly, or use the get() method.
- Parameters
days (list) – list of ints/strings/date objects, the days on which to take the snapshot
kwargs (dict) – passed to Intervention()
Example:
sim = ps.Sim(analyzers=ps.snapshot('2020-04-04', '2020-04-14')) sim.run() snapshot = sim['analyzers'][0] people = snapshot.snapshots[0] # Option 1 people = snapshot.snapshots['2020-04-04'] # Option 2 people = snapshot.get('2020-04-14') # Option 3 people = snapshot.get(34) # Option 4 people = snapshot.get() # Option 5
- class save_states(states=None, full=False, sort_inds=None, **kwargs)[source]¶
Bases:
poliosim.analysis.Analyzer
Save the states of the people into a big array for polio.
- Parameters
states (list) the list of states to save (default, just shedding) –
full (bool) – whether to save the complete list of states
- plot(sort_inds=None, log_viral_shed=True, nonzero_only=False, filter_states=None, rows=None, **kwargs)[source]¶
- coplot(people_inds=None, filter_states=None, transform_state=None, **kwargs)[source]¶
Co-plot selected states of selected people, one plot per person.
- Parameters
people_inds – The indices of the people to create plots for.
filter_states – If specified, only these states will be shown. The states you want also need to be
object (specified when creating the analyzer) –
didn't (or else be one of the defaults if you) –
explicitly. (specify any) –
transform_state – An optional dictionary, keyed on state names. The dictionary value is a function
plotted. (to pass the state values through. The transformed value will be) –
**kwargs –
- Returns
Nothing.
Example of using transform_state:
# This function returns log10(x) when x > 0, otherwise np.nan: def snoop_loggy_log10(x): return np.log10(x, where=0 < x, out=np.nan * x) # Apply the special log function just to the state 'viral_shed'. my_analyzer.plot(people_inds=inds, transform_state={'viral_shed': snoop_loggy_log10})
- class track_events(states=None, full=True, curr_imm=False, **kwargs)[source]¶
Bases:
poliosim.analysis.Analyzer
Store a list of events for everyone.
- Parameters
states (list) the list of states to save (default, just shedding) –
full (bool) – whether to save the complete list of states
curr_imm (bool) – whether to store current immunity, which is updated on every timestep (default: false)
Data are stored in self.events for the events, and self.init_state for the initial state.
- class track_shedders(**kwargs)[source]¶
Bases:
poliosim.analysis.Analyzer
Keep a record of people who shed
- class infection_report(naive_immunity=8.0, age_breakpoints=None, **kwargs)[source]¶
Bases:
poliosim.analysis.Analyzer
Keep a record of who infects who.
- Parameters
naive_immunity (float) – immunity threshold for categorization as naive (units = Nab). Default is 8.0.
age_breakpoints (array of float) – age breakpoints for making categories.
[5 (Default is) –
u5' ('non-naive) –
u5' –
'5-15' –
'o15'. –
variants. (All categories with lower bound <5 will have naive and non-naive) –
raised. (The first age breakpoint must be >0 or an exception will be) –
- class Fit(sim, weights=None, keys=None, method=None, custom=None, compute=True, verbose=False, **kwargs)[source]¶
Bases:
sciris.sc_utils.prettyobj
A class for calculating the fit between the model and the data. Note the following terminology is used here:
fit: nonspecific term for how well the model matches the data
difference: the absolute numerical differences between the model and the data (one time series per result)
goodness-of-fit: the result of passing the difference through a statistical function, such as mean squared error
loss: the goodness-of-fit for each result multiplied by user-specified weights (one time series per result)
mismatch: the sum of all the loses (a single scalar value) – this is the value to be minimized during calibration
- Parameters
sim (Sim) – the sim object
weights (dict) – the relative weight to place on each result
keys (list) – the keys to use in the calculation
method (str) – the method to be used to calculate the goodness-of-fit
custom (dict) – a custom dictionary of additional data to fit; format is e.g. {‘<label>’:{‘data’:[1,2,3], ‘sim’:[1,2,4], ‘weights’:2.0}}
compute (bool) – whether to compute the mismatch immediately
verbose (bool) – detail to print
kwargs (dict) – passed to compute_gof()
Example:
sim = ps.Sim() # Needs data to work sim.run() fit = sim.compute_fit() fit.plot()
- plot(keys=None, width=0.8, font_size=18, fig_args=None, axis_args=None, plot_args=None)[source]¶
Plot the fit of the model to the data. For each result, plot the data and the model; the difference; and the loss (weighted difference). Also plots the loss as a function of time.
- Parameters
keys (list) – which keys to plot (default, all)
width (float) – bar width
font_size (float) – size of font
fig_args (dict) – passed to pl.figure()
axis_args (dict) – passed to pl.subplots_adjust()
plot_args (dict) – passed to pl.plot()
- class TransTree(sim, to_networkx=False)[source]¶
Bases:
sciris.sc_utils.prettyobj
A class for holding a transmission tree. There are several different representations of the transmission tree: “infection_log” is copied from the people object and is the simplest representation. “detailed h” includes additional attributes about the source and target. If NetworkX is installed (required for most methods), “graph” includes an NX representation of the transmission tree.
- Parameters
sim (Sim) – the sim object
to_networkx (bool) – whether to convert the graph to a NetworkX object
- property transmissions¶
Iterable over edges corresponding to transmission events
This excludes edges corresponding to seeded infections without a source
- count_targets(start_day=None, end_day=None)[source]¶
Count the number of targets each infected person has. If start and/or end days are given, it will only count the targets of people who got infected between those dates (it does not, however, filter on the date the target got infected).
- Parameters
start_day (int/str) – the day on which to start counting people who got infected
end_day (int/str) – the day on which to stop counting people who got infected
- make_detailed(people, reset=False)[source]¶
Construct a detailed transmission tree, with additional information for each person
- plot_histograms(start_day=None, end_day=None, bins=None, width=0.8, fig_args=None, font_size=18)[source]¶
Plots a histogram of the number of transmissions.
- Parameters
start_day (int/str) – the day on which to start counting people who got infected
end_day (int/str) – the day on which to stop counting people who got infected
bins (list) – bin edges to use for the histogram
width (float) – width of bars
fig_args (dict) – passed to pl.figure()
font_size (float) – size of font
- class calculate_contacts_infected(ignore_last_n_days=None, **kwargs)[source]¶
Bases:
poliosim.analysis.Analyzer
Calculate the contacts infected