metawards.Network¶
-
class
metawards.
Network
(name: str = None, nodes: metawards._nodes.Nodes = None, links: metawards._links.Links = None, play: 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: List[int] = None, params: metawards._parameters.Parameters = None, work_population: int = None, play_population: 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: str = None, nodes: metawards._nodes.Nodes = None, links: metawards._links.Links = None, play: 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: List[int] = None, params: metawards._parameters.Parameters = None, work_population: int = None, play_population: int = None) → None¶
Methods
__init__
(name, nodes, links, play, nnodes, …)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[, profiler])Construct a Network from the passed Wards object (e.g. get_min_max_distances
(nthreads[, profiler])Calculate and return the minimum and maximum distances between nodes in the network get_node_index
(index, int])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 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, play_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])Return a copy of this network that has been specialised for the passed demographic. to_wards
([profiler])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
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: 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: metawards._parameters.Parameters = None, disease: 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_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
= 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
-
links
= None¶ The links between nodes (work)
-
max_links
= 4194304¶ The maximum allowable number of links in the network
-
max_nodes
= 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
= None¶ The name of the Network. This equals the name of the demographic if this is a multi-demographic sub-network
-
nlinks
= 0¶ The number of links in the network
-
nnodes
= 0¶ The number of nodes in the network
-
nodes
= None¶ The list of nodes (wards) in the network
-
nplay
= 0¶ The number of play links in the network
-
params
= None¶ The parameters used to generate this network
-
play
= None¶ The links between nodes (play)
-
play_population
= None¶ The number of players
-
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
-
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: int = None, nsteps: int = None, nthreads: 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: any = None, work_ratio: any = None, play_ratio: 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
= 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:
-
work_population
= None¶ The number of workers
-