BasePeople#

class BasePeople(*args, **kwargs)[source]#

Bases: FlexPretty

A class to handle all the boilerplate for people – note that as with the BaseSim vs Sim classes, everything interesting happens in the People class, whereas this class exists to handle the less interesting implementation details.

Methods

set_pars(pars=None)[source]#

Re-link the parameters stored in the people object to the sim containing it, and perform some basic validation.

validate(sim_pars=None, die=True, verbose=False)[source]#

Perform validation on the People object.

Parameters:
  • sim_pars (dict) – dictionary of parameters from the sim to ensure they match the current People object

  • die (bool) – whether to raise an exception if validation fails

  • verbose (bool) – detail to print

lock()[source]#

Lock the people object to prevent keys from being added

unlock()[source]#

Unlock the people object to allow keys to be added

__add__(people2)[source]#

Combine two people arrays

summarize(output=False)[source]#

Print a summary of the people – same as brief

set(key, value, die=True)[source]#

Ensure sizes and dtypes match

get(key)[source]#

Convenience method – key can be string or list of strings

true(key)[source]#

Return indices matching the condition

false(key)[source]#

Return indices not matching the condition

defined(key)[source]#

Return indices of people who are not-nan

undefined(key)[source]#

Return indices of people who are nan

count(key)[source]#

Count the number of people for a given key

count_by_variant(key, variant)[source]#

Count the number of people for a given key

count_not(key)[source]#

Count the number of people who do not have a property for a given key

keys()[source]#

Returns keys for all properties of the people object

person_keys()[source]#

Returns keys specific to a person (e.g., their age)

state_keys()[source]#

Returns keys for different states of a person (e.g., symptomatic)

date_keys()[source]#

Returns keys for different event dates (e.g., date a person became symptomatic)

dur_keys()[source]#

Returns keys for different durations (e.g., the duration from exposed to infectious)

layer_keys()[source]#

Get the available contact keys – try contacts first, then beta_layer

indices()[source]#

The indices of each people array

to_df()[source]#

Convert to a Pandas dataframe

to_arr()[source]#

Return as numpy array

person(ind)[source]#

Method to create person from the people

to_list()[source]#

Return all people as a list

from_list(people, resize=True)[source]#

Convert a list of people back into a People object

to_graph()[source]#

Convert all people to a networkx MultiDiGraph, including all properties of the people (nodes) and contacts (edges).

Example:

import covasim as cv
import networkx as nx
sim = cv.Sim(pop_size=50, pop_type='hybrid', contacts=dict(h=3, s=10, w=10, c=5)).run()
G = sim.people.to_graph()
nodes = G.nodes(data=True)
edges = G.edges(keys=True)
node_colors = [n['age'] for i,n in nodes]
layer_map = dict(h='#37b', s='#e11', w='#4a4', c='#a49')
edge_colors = [layer_map[G[i][j][k]['layer']] for i,j,k in edges]
edge_weights = [G[i][j][k]['beta']*5 for i,j,k in edges]
nx.draw(G, node_color=node_colors, edge_color=edge_colors, width=edge_weights, alpha=0.5)
save(filename=None, force=False, **kwargs)[source]#

Save to disk as a gzipped pickle.

Note: by default this function raises an exception if trying to save a run or partially run People object, since the changes that happen during a run are usually irreversible.

Parameters:
  • filename (str or None) – the name or path of the file to save to; if None, uses stored

  • force (bool) – whether to allow saving even of a run or partially-run People object

  • kwargs – passed to sc.makefilepath()

Returns:

the validated absolute path to the saved file

Return type:

filename (str)

Example:

sim = cv.Sim()
sim.initialize()
sim.people.save() # Saves to a .ppl file
static load(filename, *args, **kwargs)[source]#

Load from disk from a gzipped pickle.

Parameters:
  • filename (str) – the name or path of the file to load from

  • args (list) – passed to cv.load()

  • kwargs (dict) – passed to cv.load()

Returns:

the loaded people object

Return type:

people (People)

Example:

people = cv.people.load('my-people.ppl')
init_contacts(reset=False)[source]#

Initialize the contacts dataframe with the correct columns and data types

add_contacts(contacts, lkey=None, beta=None)[source]#

Add new contacts to the array. See also contacts.add_layer().

make_edgelist(contacts)[source]#

Parse a list of people with a list of contacts per person and turn it into an edge list.

static remove_duplicates(df)[source]#

Sort the dataframe and remove duplicates – note, not extensively tested