metawards.Demographics

class metawards.Demographics(demographics: List[metawards._demographic.Demographic] = <factory>, random_seed: Optional[int] = None, interaction_matrix: Optional[List[List[int]]] = None, _names: Dict[str, int] = <factory>, _name: Optional[str] = None, _version: Optional[str] = None, _authors: Optional[str] = None, _contacts: Optional[str] = None, _references: Optional[str] = None, _filename: Optional[str] = None, _repository: Optional[str] = None, _repository_version: Optional[str] = None, _repository_branch: Optional[str] = None)[source]

This class holds metadata about all of the demographics being modelled

__init__(demographics: List[metawards._demographic.Demographic] = <factory>, random_seed: Optional[int] = None, interaction_matrix: Optional[List[List[int]]] = None, _names: Dict[str, int] = <factory>, _name: Optional[str] = None, _version: Optional[str] = None, _authors: Optional[str] = None, _contacts: Optional[str] = None, _references: Optional[str] = None, _filename: Optional[str] = None, _repository: Optional[str] = None, _repository_version: Optional[str] = None, _repository_branch: Optional[str] = None) → None

Methods

__init__(demographics, random_seed, …)
add(demographic) Add a demographic to the set to be modelled
build(params, population, max_nodes, …) Build the set of networks described by these demographics and the passed parameters
copy() Return a copy of this demographics object that should allow a safe reset between runs.
from_data(data[, json_dir]) Construct and return a Demographics object constructed from a (json-deserialised) data dictionary
from_json(s) Construct and return Demographics loaded from the passed json file
get_index(item) Return the index of the passed item
get_name(item) Return the name of the demographic at ‘item’
is_multi_network() Return whether or not these demographics need to use multiple custom networks (e.g.
load(name, repository, folder, filename) Load the parameters for the specified set of demographics.
specialise(network, profiler, nthreads) Build the set of networks that will model this set of demographics applied to the passed Network.
to_data() Return a data dictionary for this object that can be serialised to json
to_json(filename, indent, auto_bzip) Serialise the Demographics to JSON.
uses_named_network() Return whether or not at least one of these demographics specifies the use of a named network model

Attributes

interaction_matrix The interaction matrix between demographics.
random_seed The random seed to used when using any random number generator to resolve decisions needed when allocating individuals to demographics.
__eq__(other)[source]

Return self==value.

__str__()[source]

Return str(self).

add(demographic: metawards._demographic.Demographic)[source]

Add a demographic to the set to be modelled

build(params: Parameters, population: Population = None, max_nodes: int = 16384, max_links: int = 4194304, nthreads: int = 1, profiler: Profiler = None) → _Union[Network, Networks][source]

Build the set of networks described by these demographics and the passed parameters

Parameters:
  • params (Parameters) – Parameters used to help build the model networks
  • max_nodes (int) – Initial guess for the maximum number of nodes(wards)
  • max_links (int) – Initial guess for the maximum number of links between wards
  • profiler (Profiler) – Profiler used to profile the specialisation
  • nthreads (int) – Number of threads over which to parallelise the work
Returns:

The set of Networks that represent the model run over the full set of different demographics(or Network if there is just a single demographic)

Return type:

Network or Networks

copy()[source]

Return a copy of this demographics object that should allow a safe reset between runs. This deepcopies things that may change, while shallow copying things that won’t

demographics = None

The list of individual Demographic objects, one for each demographic being modelled

static from_data(data, json_dir=None) → metawards._demographics.Demographics[source]

Construct and return a Demographics object constructed from a (json-deserialised) data dictionary

static from_json(s: str)[source]

Construct and return Demographics loaded from the passed json file

get_index(item)[source]

Return the index of the passed item

get_name(item)[source]

Return the name of the demographic at ‘item’

interaction_matrix = None

The interaction matrix between demographics. This should be a list of lists that shows how demographic ‘i’ affects demographic ‘j’

is_multi_network()[source]

Return whether or not these demographics need to use multiple custom networks (e.g. refer to different network models)

static load(name: Optional[str] = None, repository: Optional[str] = None, folder: str = 'demographics', filename: Optional[str] = None)[source]

Load the parameters for the specified set of demographics. This will look for a file called f”{name}.json” in the directory f”{repository}/{folder}/{name}.json”

By default this will load nothing.

Alternatively you can provide the full path to the json file via the “filename” argument

Parameters:
  • name (str) – The name of the demographics to load. This is the name that will be searched for in the METAWARDSDATA diseases directory
  • repository (str) – The location of the cloned METAWARDSDATA repository
  • folder (str) – The name of the folder within the METAWARDSDATA repository that contains the diseases
  • filename (str) – The name of the file to load the disease from - this directly loads this file without searching through the METAWARDSDATA repository
Returns:

demographics – The constructed and validated demographics

Return type:

Demographics

random_seed = None

The random seed to used when using any random number generator to resolve decisions needed when allocating individuals to demographics. This is set here so that the Demographics are uniquely determined and reproducible across runs

specialise(network: Network, profiler: Profiler = None, nthreads: int = 1)[source]

Build the set of networks that will model this set of demographics applied to the passed Network.

Parameters:
  • network (Network) – The overall population model - this contains the base parameters, wards, work and play links that define the model outbreak
  • profiler (Profiler) – Profiler used to profile the specialisation
  • nthreads (int) – Number of threads over which to parallelise the work
Returns:

networks – The set of Networks that represent the model run over the full set of different demographics

Return type:

Networks

to_data()[source]

Return a data dictionary for this object that can be serialised to json

to_json(filename: Optional[str] = None, indent: Optional[int] = None, auto_bzip: bool = True) → str[source]

Serialise the Demographics to JSON. This will write to a file if filename is set, otherwise it will return a JSON string.

Parameters:
  • filename (str) – The name of the file to write the JSON to. The absolute path to the written file will be returned. If filename is None then this will serialise to a JSON string which will be returned.
  • indent (int) – The number of spaces of indent to use when writing the json
  • auto_bzip (bool) – Whether or not to automatically bzip2 the written json file
Returns:

Returns either the absolute path to the written file, or the json-serialised string

Return type:

str

uses_named_network()[source]

Return whether or not at least one of these demographics specifies the use of a named network model