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
- 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)
- 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().