base
base
The v3 contact layers (cv.Layer, now a Starsim network, and cv.Contacts), and the v3 cv.Result (now Starsim’s).
Classes
| Name | Description |
|---|---|
| Contacts | The contact layers (i.e. networks), keyed by name, e.g. sim.people.contacts['h']. |
| FlexDict | A dict that allows more flexible element access: in addition to obj[‘a’], |
| Layer | A v3 contact layer: a static Starsim network, i.e. one whose edges don’t change unless modified, |
Contacts
base.Contacts(data=None, layer_keys=None, sim=None, **kwargs)The contact layers (i.e. networks), keyed by name, e.g. sim.people.contacts['h'].
sim.people.contacts is made from the sim’s networks each time it is read. Adding a layer adds a network to the sim (after it has been initialized, but before it has been run); layers can’t be removed from or replaced in a sim this way, so pass networks to cv.Sim() instead.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | dict | a dictionary of layers (or of dicts of p1, p2, and beta arrays) | None |
| layer_keys | list | if provided, create an empty Contacts object with these layers | None |
| sim | Sim | if provided, the sim whose networks these are | None |
| kwargs | dict | additional layer(s), merged with data | {} |
New in version 4.0.0: the layers are Starsim networks; sim argument.
Methods
| Name | Description |
|---|---|
| add_layer | Small method to add one or more layers to the contacts. Layers should |
| from_sim | Create the contacts from the sim’s networks |
| pop_layer | Remove the layer(s) from the contacts. Layers can’t be removed from a sim, so this |
| to_graph | Convert all layers to a networkx MultiDiGraph |
add_layer
base.Contacts.add_layer(**kwargs)Small method to add one or more layers to the contacts. Layers should be provided as keyword arguments.
Example::
hospitals_layer = cv.Layer(label='hosp')
sim.people.contacts.add_layer(hospitals=hospitals_layer)
from_sim
base.Contacts.from_sim(sim)Create the contacts from the sim’s networks
pop_layer
base.Contacts.pop_layer(*args)Remove the layer(s) from the contacts. Layers can’t be removed from a sim, so this is only for contacts that aren’t from a sim.
Example::
contacts = cv.Contacts(a=dict(p1=[0,1], p2=[1,2], beta=[1,1]), b=dict(p1=[2], p2=[3], beta=[1]))
contacts.pop_layer('b')
to_graph
base.Contacts.to_graph()Convert all layers to a networkx MultiDiGraph
Example::
import networkx as nx
sim = cv.Sim(pop_size=50, pop_type='hybrid').run()
G = sim.people.contacts.to_graph()
nx.draw(G)
FlexDict
base.FlexDict()A dict that allows more flexible element access: in addition to obj[‘a’], also allow obj[0]. Lightweight implementation of the Sciris odict class.
Layer
base.Layer()A v3 contact layer: a static Starsim network, i.e. one whose edges don’t change unless modified, e.g. by an intervention. See ss.Network for details.
Example::
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')
Methods
| Name | Description |
|---|---|
| keys | v3: a layer was a dict of its edge arrays (p1, p2, beta, and any others), so e.g. cv.Layer(**layer) copies it |
| step | The edges are static |
keys
base.Layer.keys()v3: a layer was a dict of its edge arrays (p1, p2, beta, and any others), so e.g. cv.Layer(**layer) copies it
step
base.Layer.step()The edges are static