Layer#

class Layer(*args, label=None, **kwargs)[source]#

Bases: FlexDict

A small class holding a single layer of contact edges (connections) between people.

The input is typically three arrays: person 1 of the connection, person 2 of the connection, and the weight of the connection. Connections are undirected; each person is both a source and sink.

This class is usually not invoked directly by the user, but instead is called as part of the population creation.

Parameters:
  • p1 (array) – an array of N connections, representing people on one side of the connection

  • p2 (array) – an array of people on the other side of the connection

  • beta (array) – an array of weights for each connection

  • label (str) – the name of the layer (optional)

  • kwargs (dict) – other keys copied directly into the layer

Note that all arguments (except for label) must be arrays of the same length, although not all have to be supplied at the time of creation (they must all be the same at the time of initialization, though, or else validation will fail).

Examples:

# Generate an average of 10 contacts for 1000 people
n = 10_000
n_people = 1000
p1 = np.random.randint(n_people, size=n)
p2 = np.random.randint(n_people, size=n)
beta = np.ones(n)
layer = cv.Layer(p1=p1, p2=p2, beta=beta, label='rand')
layer = cv.Layer(dict(p1=p1, p2=p2, beta=beta), label='rand') # Alternate method

# Convert one layer to another with extra columns
index = np.arange(n)
self_conn = p1 == p2
layer2 = cv.Layer(**layer, index=index, self_conn=self_conn, label=layer.label)

New in version 3.1.2: allow a single dictionary input

Attributes

members

Return sorted array of all members

Methods

property members#

Return sorted array of all members

meta_keys()[source]#

Return the keys for the layer’s meta information – i.e., p1, p2, beta

validate(force=True)[source]#

Check the integrity of the layer: right types, right lengths.

If dtype is incorrect, try to convert automatically; if length is incorrect, do not.

pop_inds(inds)[source]#

“Pop” the specified indices from the edgelist and return them as a dict. Returns in the right format to be used with layer.append().

Parameters:

inds (int, array, slice) – the indices to be removed

append(contacts)[source]#

Append contacts to the current layer.

Parameters:

contacts (dict) – a dictionary of arrays with keys p1,p2,beta, as returned from layer.pop_inds()

to_df()[source]#

Convert to dataframe

from_df(df, keys=None)[source]#

Convert from a dataframe

to_graph()[source]#

Convert to a networkx DiGraph

Example:

import networkx as nx
sim = cv.Sim(pop_size=20, pop_type='hybrid').run()
G = sim.people.contacts['h'].to_graph()
nx.draw(G)
find_contacts(inds, as_array=True)[source]#

Find all contacts of the specified people

For some purposes (e.g. contact tracing) it’s necessary to find all of the contacts associated with a subset of the people in this layer. Since contacts are bidirectional it’s necessary to check both P1 and P2 for the target indices. The return type is a Set so that there is no duplication of indices (otherwise if the Layer has explicit symmetric interactions, they could appear multiple times). This is also for performance so that the calling code doesn’t need to perform its own unique() operation. Note that this cannot be used for cases where multiple connections count differently than a single infection, e.g. exposure risk.

Parameters:
  • inds (array) – indices of people whose contacts to return

  • as_array (bool) – if true, return as sorted array (otherwise, return as unsorted set)

Returns:

a set of indices for pairing partners

Return type:

contact_inds (array)

Example: If there were a layer with - P1 = [1,2,3,4] - P2 = [2,3,1,4] Then find_contacts([1,3]) would return {1,2,3}

update(people, frac=1.0)[source]#

Regenerate contacts on each timestep.

This method gets called if the layer appears in sim.pars['dynam_layer']. The Layer implements the update procedure so that derived classes can customize the update e.g. implementing over-dispersion/other distributions, random clusters, etc.

Typically, this method also takes in the people object so that the update can depend on person attributes that may change over time (e.g. changing contacts for people that are severe/critical).

Parameters:
  • people (People) – the Covasim People object, which is usually used to make new contacts

  • frac (float) – the fraction of contacts to update on each timestep