Network#

class Network(key_dict=None, prenatal=False, postnatal=False, name=None, label=None, **kwargs)[source]#

Bases: Module

A class holding a single network of contact edges (connections) between people as well as methods for updating these.

The input is typically arrays including: person 1 of the connection, person 2 of the connection, the weight of the connection, the duration and start/end times of the connection.

Parameters:
  • p1 (array) – an array of length N, the number of connections in the network, with the indices of people on one side of the connection.

  • p2 (array) – an array of length N, the number of connections in the network, with the indices of people on the other side of the connection.

  • beta (array) – an array representing relative transmissibility of each connection for this network - TODO, do we need this?

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

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

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_contacts_pp = 10
n_people = 1000
n = n_contacts_pp * n_people
p1 = np.random.randint(n_people, size=n)
p2 = np.random.randint(n_people, size=n)
beta = np.ones(n)
network = ss.Network(p1=p1, p2=p2, beta=beta, label='rand')
network = ss.Network(dict(p1=p1, p2=p2, beta=beta), label='rand') # Alternate method

# Convert one network to another with extra columns
index = np.arange(n)
self_conn = p1 == p2
network2 = ss.Network(**network, index=index, self_conn=self_conn, label=network.label)

Attributes

beta

Relative transmission on each network edge

members

Return sorted array of all members

now

Return the current time, i.e. the time vector at the current timestep.

p1

The first half of a network edge (person 1)

p2

The second half of a network edge (person 2)

states

Return a flat list of all states

statesdict

Return a flat dictionary (objdict) of all states

Methods

property p1#

The first half of a network edge (person 1)

property p2#

The second half of a network edge (person 2)

property beta#

Relative transmission on each network edge

property members#

Return sorted array of all members

meta_keys()[source]#

Return the keys for the network’s meta information

set_network_states(people)[source]#

Many network states depend on properties of people – e.g. MSM depends on being male, age of debut varies by sex and over time, and participation rates vary by age. Each time states are dynamically grown, this function should be called to set the network states that depend on other states.

validate_uids()[source]#

Ensure that p1, p2 are both UID arrays

validate(force=True)[source]#

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

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

get_inds(inds, remove=False)[source]#

Get the specified indices from the edgelist and return them as a dict.

Parameters:
  • inds (int, array, slice) – the indices to find

  • remove (bool) – whether to remove the indices

pop_inds(inds)[source]#

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

Parameters:

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

append(edges=None, **kwargs)[source]#

Append edges to the current network.

Parameters:

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

to_graph()[source]#

Convert to a networkx DiGraph

Example:

import networkx as nx
sim = ss.Sim(n_agents=100, networks='mf').init()
G = sim.networks.randomnet.to_graph()
nx.draw(G)
to_dict()[source]#

Convert to dictionary

to_df()[source]#

Convert to dataframe

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

Convert from a dataframe

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 the edges associated with a subset of the people in this network. Since edges 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 Network 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 edges 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 network with - p1 = [1,2,3,4] - p2 = [2,3,1,4] Then find_edges([1,3]) would return {1,2,3}

add_pairs()[source]#

Define how pairs of people are formed

remove_uids(uids)[source]#

Remove interactions involving specified UIDs This method is typically called via People.remove() and is specifically used when removing agents from the simulation.

net_beta(disease_beta=None, inds=None, disease=None)[source]#

Calculate the beta for the given disease and network