metawards.Network

class metawards.Network(name: Optional[str] = None, nodes: Optional[metawards._nodes.Nodes] = None, links: Optional[metawards._links.Links] = None, play: Optional[metawards._links.Links] = None, nnodes: int = 0, nlinks: int = 0, nplay: int = 0, max_nodes: int = 16384, max_links: int = 4194304, info: metawards._wardinfo.WardInfos = WardInfos(wards=[], _index=None), to_seed: Optional[List[int]] = None, params: Optional[metawards._parameters.Parameters] = None, work_population: Optional[int] = None, play_population: Optional[int] = None)[source]

This class represents a network of wards. The network comprises nodes (representing wards), connected with links which represent work (predictable movements) and play (unpredictable movements)

__init__(name: Optional[str] = None, nodes: Optional[metawards._nodes.Nodes] = None, links: Optional[metawards._links.Links] = None, play: Optional[metawards._links.Links] = None, nnodes: int = 0, nlinks: int = 0, nplay: int = 0, max_nodes: int = 16384, max_links: int = 4194304, info: metawards._wardinfo.WardInfos = WardInfos(wards=[], _index=None), to_seed: Optional[List[int]] = None, params: Optional[metawards._parameters.Parameters] = None, work_population: Optional[int] = None, play_population: Optional[int] = None)None

Initialize self. See help(type(self)) for accurate signature.

Methods

__delattr__(name, /)

Implement delattr(self, name).

__dir__()

Default dir() implementation.

__eq__(other)

Return self==value.

__format__(format_spec, /)

Default object formatter.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__gt__(value, /)

Return self>value.

__init__([name, nodes, links, play, nnodes, …])

Initialize self.

__init_subclass__

This method is called when a class is subclassed.

__le__(value, /)

Return self<=value.

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.

__new__(**kwargs)

Create and return a new object.

__reduce__()

Helper for pickle.

__reduce_ex__(protocol, /)

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__sizeof__()

Size of object in memory, in bytes.

__str__()

Return str(self).

__subclasshook__

Abstract classes can override this to customize issubclass().

_add_lookup([lookup_function, nthreads])

Read in the ward lookup information that is used to locate wards by name or region

assert_sane(profiler)

Assert that this network is sane.

build(params[, population, max_nodes, …])

Builds and returns a new Network that is described by the passed parameters.

copy()

Return a copy of this Network.

from_wards(wards[, params, disease, …])

Construct a Network from the passed Wards object(e.g.

get_index(id)

Return the index of the Node or Link(s) that corresponds to the passed WardID.

get_min_max_distances([nthreads, profiler])

Calculate and return the minimum and maximum distances between nodes in the network

get_node_index(index)

Return the index of the node in this network that matches ‘index’.

get_work_index()

Return the mapping from the index in this sub-networks work matrix to the mapping in the overall network’s work matrix

has_different_work_matrix()

Return whether or not the sub-network work matrix is different to that of the overall network

initialise_infections([nthreads])

Initialise and return the space that will be used to track infections

move_from_play_to_work([nthreads, profiler])

Move the population from play to work

num_demographics()

Return the number of demographics (always 1 for a Network).

recalculate_denominators([nthreads, profiler])

Recalculate the denominators used in the calculation.

rescale_play_matrix([nthreads, profiler])

Rescale the play matrix

reset_everything([nthreads, profiler])

Resets the network ready for a new run of the model

run(population, output_dir[, seed, nsteps, …])

Run the model simulation for the passed population.

scale_susceptibles([ratio, work_ratio, …])

Scale the number of susceptibles in this Network by the passed scale ratios.

single(params, population[, profiler])

Builds and returns a new Network that contains just a single ward, in which ‘population’ individuals are resident.

specialise(demographic[, profiler, nthreads])

Return a copy of this network that has been specialised for the passed demographic.

to_wards([profiler, nthreads])

Return the ward-level data in this network converted to a Wards object.

update(params[, demographics, population, …])

Update this network with a new set of parameters ( and optionally demographics).

Attributes

__annotations__

__dataclass_fields__

__dataclass_params__

__dict__

__doc__

__hash__

__module__

__weakref__

list of weak references to the object (if defined)

_work_index

The index in the overall network’s work matrix of the ith index in this subnetworks work matrix.

info

The metadata for all of the wards

links

The links between nodes (work)

max_links

The maximum allowable number of links in the network

max_nodes

The maximum allowable number of nodes in the network

name

The name of the Network.

nlinks

The number of links in the network

nnodes

The number of nodes in the network

nodes

The list of nodes (wards) in the network

nplay

The number of play links in the network

params

The parameters used to generate this network

play

The links between nodes (play)

play_population

The number of players

population

Return the total population in the network

to_seed

To seed provides additional seeding information

work_population

The number of workers

assert_sane(profiler: None)[source]

Assert that this network is sane. This checks that the network is laid out correctly in memory and that it doesn’t have anything unexpected. Checking here will prevent us from having to check every time the network is accessed

static build(params: metawards._parameters.Parameters, population: Optional[metawards._population.Population] = None, max_nodes: int = 16384, max_links: int = 4194304, nthreads: int = 1, profiler=None)[source]

Builds and returns a new Network that is described by the passed parameters.

The network is built in allocated memory, so you need to specify the maximum possible number of nodes and links. The memory buffers will be shrunk back after building.

copy()[source]

Return a copy of this Network. Use this to hold a copy of the network that you can use to reset between runs

static from_wards(wards, params: Optional[metawards._parameters.Parameters] = None, disease: Optional[metawards._disease.Disease] = None, profiler=None, nthreads: int = 1)[source]

Construct a Network from the passed Wards object(e.g. after editing, or restoring from JSON

get_index(id: metawards._wardid.WardID)Tuple[metawards._network.PersonType, int, int][source]

Return the index of the Node or Link(s) that corresponds to the passed WardID.

This returns a tuple of three values;

(PersonType, start_idx, end_idx)

If this is a worker, then it will either return the index of the Link for a specific work-link connection, or the range of indicies for all of the work links to this ward, so

(PersonType.WORKER, link_idx, link_idx+!) for a single link, or

(PersonType.WORKER, link.begin_to, link.end_to) for all links

If this is a player, then it will return the ID of the Node (which is the index of the Node in Nodes), and so

(PersonType.PLAYER, node_index, node_index+1)

This raises a KeyError if there is no ward or ward-link that matches the WardID

get_min_max_distances(nthreads: int = 1, profiler=None)[source]

Calculate and return the minimum and maximum distances between nodes in the network

get_node_index(index: Union[str, int])[source]

Return the index of the node in this network that matches ‘index’. This could be an integer, in which case this will directly look up the index of the node in the Nodes, or else it could be a string, in which case the WardInfo will be used to identify the node and look up the index from there.

get_work_index()[source]

Return the mapping from the index in this sub-networks work matrix to the mapping in the overall network’s work matrix

has_different_work_matrix()[source]

Return whether or not the sub-network work matrix is different to that of the overall network

info: metawards._wardinfo.WardInfos = WardInfos(wards=[], _index=None)

The metadata for all of the wards

initialise_infections(nthreads: int = 1)[source]

Initialise and return the space that will be used to track infections

The links between nodes (work)

The maximum allowable number of links in the network

max_nodes: int = 16384

The maximum allowable number of nodes in the network

move_from_play_to_work(nthreads: int = 1, profiler=None)[source]

Move the population from play to work

name: str = None

The name of the Network. This equals the name of the demographic if this is a multi-demographic sub-network

The number of links in the network

nnodes: int = 0

The number of nodes in the network

nodes: metawards._nodes.Nodes = None

The list of nodes (wards) in the network

nplay: int = 0

The number of play links in the network

num_demographics()int[source]

Return the number of demographics (always 1 for a Network). This function is added so that a single Network has the same functions as a multi-demographics Networks

params: metawards._parameters.Parameters = None

The parameters used to generate this network

play: metawards._links.Links = None

The links between nodes (play)

play_population: int = None

The number of players

property population

Return the total population in the network

recalculate_denominators(nthreads: int = 1, profiler=None)[source]

Recalculate the denominators used in the calculation. This should be called after you have changed the population of the network, e.g. during a move function

rescale_play_matrix(nthreads: int = 1, profiler=None)[source]

Rescale the play matrix

reset_everything(nthreads: int = 1, profiler=None)[source]

Resets the network ready for a new run of the model

run(population: metawards._population.Population, output_dir: metawards._outputfiles.OutputFiles, seed: Optional[int] = None, nsteps: Optional[int] = None, nthreads: Optional[int] = None, iterator=None, extractor=None, mixer=None, mover=None, profiler=None)metawards._population.Population[source]

Run the model simulation for the passed population. The random number seed is given in ‘seed’. If this is None, then a random seed is used.

All output files are written to ‘output_dir’

The simulation will continue until the infection has died out or until ‘nsteps’ has passed(keep as ‘None’ to prevent exiting early).

Parameters - ——— population: Population

The initial population at the start of the model outbreak. This is also used to set start date and day of the model outbreak

output_dir: OutputFiles

The directory to write all of the output into

seed: int

The random number seed used for this model run. If this is None then a very random random number seed will be used

nsteps: int

The maximum number of steps to run in the outbreak. If None then run until the outbreak has finished

profiler: Profiler

The profiler to use - a new one is created if one isn’t passed

nthreads: int

Number of threads over which to parallelise this model run

iterator: function

Function that is called at each iteration to get the functions that are used to advance the model

extractor: function

Function that is called at each iteration to get the functions that are used to extract data for analysis or writing to files

mixer: function

Function that is used to mix demographic data. Not used by a single Network(used by Networks)

mover: function

Function that is used to move the population between different demographics. Not used by a single Network(used by Networks)

scale_susceptibles(ratio: Optional[any] = None, work_ratio: Optional[any] = None, play_ratio: Optional[any] = None)[source]

Scale the number of susceptibles in this Network by the passed scale ratios. These can be values, e.g. ratio = 2.0 will scale the total number of susceptibles in each ward by 2.0. They can also be lists of values, where ward[i] will be scaled by ratio[i]. They can also be dictionaries, e.g. ward[i] scaled by ratio[i]

Parameters
  • ratio (None, float, list or dict) – The amount by which to scale the total population of susceptibles - evenly scales the work and play populations

  • work_ratio (None, float, list or dict) – Scale only the work population of susceptibles

  • play_ratio (None, float, list or dict) – Scale only the play population of susceptibles

Returns

Return type

None

static single(params: metawards._parameters.Parameters, population: metawards._population.Population, profiler=None)[source]

Builds and returns a new Network that contains just a single ward, in which ‘population’ individuals are resident.

specialise(demographic, profiler=None, nthreads: int = 1)[source]

Return a copy of this network that has been specialised for the passed demographic. The returned network will contain only members of that demographic, with the parameters of the network adjusted according to the rules of that demographic

Parameters

demographic (Demographic) – The demographic with which to specialise

Returns

network – The specialised network

Return type

Network

to_seed: List[int] = None

To seed provides additional seeding information

to_wards(profiler=None, nthreads: int = 1)[source]

Return the ward-level data in this network converted to a Wards object. This supports editing and save/restore to JSON

update(params: metawards._parameters.Parameters, demographics=None, population=None, nthreads: int = 1, profiler=None)[source]

Update this network with a new set of parameters ( and optionally demographics).

This is used to update the parameters for the network for a new run. The network will be reset and ready for a new run.

Parameters
  • params (Parameters) – The new parameters with which to update this Network

  • demographics (Demographics) – The new demographics with which to update this Network. Note that this will return a Network object that contains the specilisation of this Network

  • nthreads (int) – Number of threads over which to parallelise this update

  • profiler (Profiler) – The profiler used to profile this update

Returns

network – Either this Network after it has been updated, or the resulting Networks from specialising this Network using Demographics

Return type

Network or Networks

work_population: int = None

The number of workers