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)[source]

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 DATE type can actually represent date+time values, but we are only concerned with day precision at present. 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
LIST = 6
DICT = 10
class Specifiable(type_, description, categories=None)[source]

Bases: object

Base class for Parameter and Property.

PANDAS_TYPE_MAP = {<Types.DATE: 1>: 'datetime64[ns]', <Types.BOOL: 2>: <class 'bool'>, <Types.INT: 3>: 'int64', <Types.REAL: 4>: <class 'float'>, <Types.CATEGORICAL: 5>: 'category', <Types.LIST: 6>: <class 'object'>, <Types.SERIES: 7>: <class 'object'>, <Types.DATA_FRAME: 8>: <class 'object'>, <Types.STRING: 9>: <class 'object'>, <Types.DICT: 10>: <class 'object'>}

Map our Types to Python types.

PYTHON_TYPE_MAP = {<Types.DATE: 1>: <class 'pandas._libs.tslibs.timestamps.Timestamp'>, <Types.BOOL: 2>: <class 'bool'>, <Types.INT: 3>: <class 'int'>, <Types.REAL: 4>: <class 'float'>, <Types.CATEGORICAL: 5>: <class 'pandas.core.arrays.categorical.Categorical'>, <Types.LIST: 6>: <class 'list'>, <Types.SERIES: 7>: <class 'pandas.core.series.Series'>, <Types.DATA_FRAME: 8>: <class 'pandas.core.frame.DataFrame'>, <Types.STRING: 9>: <class 'object'>, <Types.DICT: 10>: <class 'dict'>}
property python_type
property pandas_type
delimiter = ' === '
class Parameter(type_, description, categories=None)[source]

Bases: Specifiable

Used to specify parameters for disease modules etc.

class Property(type_, description, categories=None, *, ordered=False)[source]

Bases: Specifiable

Used to specify properties of individuals.

PANDAS_TYPE_DEFAULT_VALUE_MAP = {'datetime64[ns]': NaT, <class 'bool'>: False, 'int64': 0, <class 'float'>: nan, 'category': nan, <class 'object'>: nan}
property default_value
create_series(name, size)[source]

Create a Pandas Series for this property.

The values will be left uninitialised.

  • name – The name for the series.

  • size – The length of the series.

class Module(name=None)[source]

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:


The unique name of this module within the simulation.


A dictionary of module parameters, derived from specifications in the PARAMETERS class attribute on a subclass.


A random number generator specific to this module, with its own internal state. It’s an instance of numpy.random.RandomState


The simulation this module is part of, once registered.

INIT_DEPENDENCIES = frozenset({})
ALTERNATIVE_TO = frozenset({})
load_parameters_from_dataframe(resource: DataFrame)[source]

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 (Any input will be converted into a boolean without warnings)

Will also make the parameter_name the index of the resource DataFrame.


resource (DataFrame) – DataFrame with a column of the parameter_name and a column of value


Read parameter values from file, if required.

Must be implemented by subclasses.


data_folder – path of a folder supplied to the Simulation containing data files. Typically modules would read a particular file within here.


Set our property values for the initial population.

Must be implemented by subclasses.

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.

TODO: We probably need to declare somehow which properties we ‘read’ here, so the simulation knows what order to initialise modules in!


population – the population of individuals


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.


Carry out any work before any populations have been initalised

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, child)[source]

Initialise our properties for a newborn individual.

Must be implemented by subclasses.

This is called by the simulation whenever a new person is born.

  • mother – the mother for this child (can be -1 if the mother is not identified).

  • child – the new child


This is called after the simulation has ended. Modules do not need to declare this.