tlo.core module
Core framework classes.
This contains things that didn’t obviously go in their own file, such as specification for parameters and properties, and the base Module class for disease modules.
- class Types(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
Enum
Possible types for parameters and properties.
This lets us hide the details of numpy & Pandas dtype strings and give users an easy list to reference instead.
Most of these should be intuitive. The CATEGORICAL type is useful for things like sex where there are a fixed number of options to choose from. The LIST type is used for properties where the value is a collection, e.g. the set of children of a person.
- DATE = 1
- BOOL = 2
- INT = 3
- REAL = 4
- CATEGORICAL = 5
- LIST = 6
- SERIES = 7
- DATA_FRAME = 8
- STRING = 9
- DICT = 10
- BITSET = 11
- class Specifiable(type_: Types, description: str, categories: List[str] = None)
Bases:
object
Base class for Parameter and Property.
Create a new Specifiable.
- Parameters:
type – an instance of Types giving the type of allowed values
description – textual description of what this Specifiable represents
categories – list of strings which will be the available categories
- PANDAS_TYPE_MAP = {Types.BITSET: <class 'numpy.uint32'>, Types.BOOL: <class 'bool'>, Types.CATEGORICAL: 'category', Types.DATA_FRAME: <class 'object'>, Types.DATE: 'datetime64[ns]', Types.DICT: <class 'object'>, Types.INT: 'int64', Types.LIST: <class 'object'>, Types.REAL: <class 'float'>, Types.SERIES: <class 'object'>, Types.STRING: <class 'object'>}
- PYTHON_TYPE_MAP = {Types.BITSET: <class 'int'>, Types.BOOL: <class 'bool'>, Types.CATEGORICAL: <class 'pandas.core.arrays.categorical.Categorical'>, Types.DATA_FRAME: <class 'pandas.core.frame.DataFrame'>, Types.DATE: <class 'pandas._libs.tslibs.timestamps.Timestamp'>, Types.DICT: <class 'dict'>, Types.INT: <class 'int'>, Types.LIST: <class 'list'>, Types.REAL: <class 'float'>, Types.SERIES: <class 'pandas.core.series.Series'>, Types.STRING: <class 'object'>}
- property python_type: type
Return the Python type corresponding to this Specifiable.
- property pandas_type: type
Return the Pandas type corresponding to this Specifiable.
- class Parameter(type_: Types, description: str, categories: List[str] = None)
Bases:
Specifiable
Used to specify parameters for disease modules etc.
Create a new Specifiable.
- Parameters:
type – an instance of Types giving the type of allowed values
description – textual description of what this Specifiable represents
categories – list of strings which will be the available categories
- class Property(type_: Types, description: str, categories: List[str] = None, *, ordered: bool = False, default_value: Any | None = None)
Bases:
Specifiable
Used to specify properties of individuals.
Create a new property specification.
- Parameters:
type – An instance of
Types
giving the type of allowed values of this property.description – Textual description of what this property represents.
categories – Set of categories this property can take if
type_
isTypes.CATEGORICAL
.ordered – Whether categories are ordered if
type_
isTypes.CATEGORICAL
.default_value – The default value for the property.
- PANDAS_TYPE_DEFAULT_VALUE_MAP = {'category': nan, 'datetime64[ns]': NaT, 'int64': 0, <class 'bool'>: False, <class 'float'>: nan, <class 'numpy.uint32'>: 0, <class 'object'>: nan}
- create_series(name: str, size: int) Series
Create a Pandas Series for this property.
The values will be left uninitialised.
- Parameters:
name – The name for the series.
size – The length of the series.
- class Module(name: str | None = None)
Bases:
object
The base class for disease modules.
This declares the methods which individual modules must implement, and contains the core functionality linking modules into a simulation. Useful properties available on instances are:
- name
The unique name of this module within the simulation.
- parameters
A dictionary of module parameters, derived from specifications in the PARAMETERS class attribute on a subclass.
- rng
A random number generator specific to this module, with its own internal state. It’s an instance of numpy.random.RandomState
- sim
The simulation this module is part of, once registered.
Construct a new disease module ready to be included in a simulation.
Initialises an empty parameters dictionary and module-specific random number generator.
- Parameters:
name – the name to use for this module. Defaults to the concrete subclass’ name.
- INIT_DEPENDENCIES: FrozenSet[str] = frozenset({})
- OPTIONAL_INIT_DEPENDENCIES: FrozenSet[str] = frozenset({})
- ADDITIONAL_DEPENDENCIES: FrozenSet[str] = frozenset({})
- ALTERNATIVE_TO: FrozenSet[str] = frozenset({})
- CAUSES_OF_DEATH: Dict[str, Cause] = {}
- CAUSES_OF_DISABILITY: Dict[str, Cause] = {}
- parameters
- load_parameters_from_dataframe(resource: DataFrame) None
Automatically load parameters from resource dataframe, updating the class parameter dictionary
Goes through parameters dict self.PARAMETERS and updates the self.parameters with values Automatically updates the values of data types: - Integers - Real numbers - Lists - Categorical - Strings - Dates (Any numbers will be converted into dated without warnings) - Booleans (An value in the csv ‘0’, 0, ‘False’, ‘false’, ‘FALSE’, or None will be interpreted as False;
everything else as True)
Will also make the parameter_name the index of the resource DataFrame.
- Parameters:
resource (DataFrame) – DataFrame with a column of the parameter_name and a column of value
- read_parameters(data_folder: str | Path) None
Read parameter values from file, if required.
Must be implemented by subclasses.
- Parameters:
data_folder – path of a folder supplied to the Simulation containing data files. Typically, modules would read a particular file within here.
- initialise_population(population: Population) None
Set our property values for the initial population.
This method is called by the simulation when creating the initial population, and is responsible for assigning initial values, for every individual, of those properties ‘owned’ by this module, i.e. those declared in its PROPERTIES dictionary.
By default, all
Property``s in ``self.PROPERTIES
will have their columns in the population dataframe set to the default value.Modules that wish to implement this behaviour do not need to implement this method, it will be inherited automatically. Modules that wish to perform additional steps during the initialise_population stage should reimplement this method and call
`python super().initialise_population(population=population) `
at the beginning of the method, then proceed with their additional steps. Modules that do not wish to inherit this default behaviour should re-implement initialise_population without the call to
super()
above.TODO: We probably need to declare somehow which properties we ‘read’ here, so the simulation knows what order to initialise modules in!
- Parameters:
population – The population of individuals in the simulation.
- initialise_simulation(sim: Simulation) None
Get ready for simulation start.
Must be implemented by subclasses.
This method is called just before the main simulation loop begins, and after all modules have read their parameters and the initial population has been created. It is a good place to add initial events to the event queue.
- pre_initialise_population() None
Carry out any work before any populations have been initialised
This optional method allows access to all other registered modules, before any of the modules have initialised a population. This is expected to be useful for when a module’s properties rely upon information from other modules.
- on_birth(mother_id: int, child_id: int) None
Initialise our properties for a newborn individual.
Must be implemented by subclasses.
This is called by the simulation whenever a new person is born.
- Parameters:
mother_id – the person id for the mother of this child (can be -1 if the mother is not identified).
child_id – the person id of new child
- on_simulation_end() None
This is called after the simulation has ended. Modules do not need to declare this.