synthpops.plotting module

This module provides plotting methods including methods to plot the age-specific contact matrix in different contact layers.

synthpops.plotting.calculate_contact_matrix(population, density_or_frequency='density', layer='H')

Calculate the symmetric age-specific contact matrix from the connections for all people in the population. density_or_frequency sets the type of contact matrix calculated.

When density_or_frequency is set to ‘frequency’ each person is assumed to have a fixed amount of contact with others they are connected to in a setting so each person will split their contact amount equally among their connections. This means that if a person has links to 4 other individuals then 1/4 will be added to the matrix element matrix[age_i][age_j] for each link, where age_i is the age of the individual and age_j is the age of the contact. This follows the mass action principle such that increased density or number of people a person is in contact with leads to decreased per-link or connection contact rate.

When density_or_frequency is set to ‘density’ the amount of contact each person has with others scales with the number of people they are connected to. This means that a person with connections to 4 individuals has a higher total contact rate than a person with connection to 3 individuals. For this definition if a person has links to 4 other individuals then 1 will be added to the matrix element matrix[age_i][age_j] for each contact. This follows the ‘pseudo’mass action principle such that the per-link or connection contact rate is constant.

Parameters
  • population (dict) – A dictionary of a population with attributes.

  • density_or_frequency (str) – option for the type of contact matrix calculated.

  • layer (str) – name of the physial contact setting, see notes.

Returns

Symmetric age specific contact matrix.

Return type

np.ndarray

Note

H for households, S for schools, W for workplaces, C for community or other, and ‘LTCF’ for long term care facilities.

synthpops.plotting.plot_contacts(pop, **kwargs)

Plot the age mixing matrix for a specific contact layer.

Parameters
  • pop (pop object) – population, either synthpops.pop.Pop or dict

  • **layer (str) – name of the physial contact layer: H for households, S for schools, W for workplaces, C for community or other

  • **aggregate_flag (bool) – If True, plot the contact matrix for aggregate age brackets, else single year age contact matrix.

  • **logcolors_flag (bool) – If True, plot heatmap in logscale

  • **density_or_frequency (str) – If ‘density’, then each contact counts for 1/(group size -1) of a person’s contact in a group, elif ‘frequency’ then count each contact. This means that more people in a group leads to higher rates of contact/exposure.

  • **state_location (string) – name of the state the location is in

  • **country_location (string) – name of the country the location is in

  • **cmap (str or matplotlib cmap) – colormap

  • **fontsize (int) – base font size

  • **rotation (int) – rotation for x axis labels

  • **title_prefix (str) – optional title prefix for the figure

  • **fig (matplotlib.figure) – If supplied, use this figure instead of generating one

  • **ax (matplotlib.axes) – If supplied, use these axes instead of generating one

  • **do_show (bool) – If True, show the plot

  • **do_save (bool) – If True, save the plot to disk

Returns

Matplotlib figure.

synthpops.plotting.plot_ages(pop, **kwargs)

Plot a comparison of the expected and generated age distribution.

Parameters
  • pop (pop object) – population, either synthpops.pop.Pop, covasim.people.People, or dict

  • **left (float) – Matplotlib.figure.subplot.left

  • **right (float) – Matplotlib.figure.subplot.right

  • **top (float) – Matplotlib.figure.subplot.top

  • **bottom (float) – Matplotlib.figure.subplot.bottom

  • **color_1 (str) – color for expected data

  • **color_2 (str) – color for data from generated population

  • **fontsize (float) – Matplotlib.figure.fontsize

  • **figname (str) – name to save figure to disk

  • **comparison (bool) – If True, plot comparison to the generated population

Returns

Matplotlib figure and axes.

Note

If using pop with type covasim.people.Pop or dict, args must be supplied for the location parameters to get the expected distribution.

Example:

pars = {'n': 10e3, location='seattle_metro', state_location='Washington', country_location='usa'}
pop = sp.Pop(**pars)
fig, ax = pop.plot_age_distribution_comparison()

popdict = pop.to_dict()
kwargs = pars.copy()
kwargs['datadir'] = sp.datadir
fig, ax = sp.plot_age_distribution_comparison(popdict, **kwargs)
synthpops.plotting.plot_school_sizes(pop, **kwargs)

Plot a comparison of the expected and generated school size distribution for each type of school expected.

Parameters
  • pop (pop object) – population, either synthpops.pop.Pop, or dict

  • **with_school_types (type) – If True, plot school size distributions by type, else plot overall school size distributions

  • **keys_to_exclude (str or list) – school types to exclude

  • **left (float) – Matplotlib.figure.subplot.left

  • **right (float) – Matplotlib.figure.subplot.right

  • **top (float) – Matplotlib.figure.subplot.top

  • **bottom (float) – Matplotlib.figure.subplot.bottom

  • **hspace (float) – Matplotlib.figure.subplot.hspace

  • **subplot_height (float) – height of subplot in inches

  • **subplot_width (float) – width of subplot in inches

  • **screen_height_factor (float) – fraction of the screen height to use for display

  • **location_text_y (float) – height to add location text to figure

  • **fontsize (float) – Matplotlib.figure.fontsize

  • **rotation (float) – rotation angle for xticklabels

  • **cmap (str) – colormap

  • **figname (str) – name to save figure to disk

  • **comparison (bool) – If True, plot comparison to the generated population

Returns

Matplotlib figure and axes.

Note

If using pop with type covasim.people.Pop or dict, args must be supplied for the location parameters to get the expected distribution.

Example:

pars = {'n': 10e3, location='seattle_metro', state_location='Washington', country_location='usa'}
pop = sp.Pop(**pars)
fig, ax = pop.plot_school_sizes_by_type()

popdict = pop.to_dict()
kwargs = pars.copy()
kwargs['datadir'] = sp.datadir
fig, ax = sp.plot_school_sizes(popdict, **kwargs)
class synthpops.plotting.plotting_kwargs(*args, **kwargs)

Bases: sciris.sc_odict.objdict

A class to set and operate on plotting kwargs throughout synthpops.

Parameters

kwargs (dict) – dictionary of plotting parameters to be used.

initialize()

Initialize plot settings.

set_font(*args, **font)

Set font styles.

default_plotting_kwargs()

Define default plotting kwrgs to be used in plotting methods.

set_figure_display_size(*args, **kwargs)

Update plotting kwargs with new calculated display sizes.

Parameters

kwargs (sc.objdict) – new values to update with

Returns

Updated kwargs and recalculating the display sizes.

set_default_pop_pars()

Check if method has some key pop parameters to call on data. If not, use defaults and warn user of their use and value.

restore_defaults()

Reset matplotlib defaults.

update_defaults(method_defaults, kwargs)

Update defaults with method defaults and kwargs.

property axis

Dictionary of axis settings.