tlo.events module

Support for creating different kinds of events.

class Priority(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

Enumeration for the Priority, which is used in sorting the events in the simulation queue.

START_OF_DAY = 0
FIRST_HALF_OF_DAY = 25
LAST_HALF_OF_DAY = 75
END_OF_DAY = 100
class Event(module, *args, priority=Priority.FIRST_HALF_OF_DAY, **kwargs)

Bases: object

Base event class, from which all others inherit.

Concrete subclasses should also inherit from one of the EventMixin classes defined below, and implement at least an apply method.

Create a new event.

Note that just creating an event does not schedule it to happen; that must be done by calling Simulation.schedule_event.

Parameters:
  • module – the module that created this event. All subclasses of Event take this as the first argument in their constructor, but may also take further keyword arguments.

  • priority – a keyword-argument to set the priority (see Priority enum)

post_apply_hook()

Do any required processing after apply() completes.

apply(target)

Apply this event to the given target.

Must be implemented by subclasses.

Parameters:

target – the target of the event

run()

Make the event happen.

class RegularEvent(module, *, frequency, end_date=None, **kwargs)

Bases: Event

An event that automatically reschedules itself at a fixed frequency.

Create a new regular event.

Parameters:
  • module – the module that created this event

  • frequency (pandas.tseries.offsets.DateOffset) – the interval from one occurrence to the next (must be supplied as a keyword argument)

apply(target)

Apply this event to the given target.

This is a no-op; subclasses should override this method.

Parameters:

target – the target of the event

post_apply_hook()

Schedule the next occurrence of this event.

class PopulationScopeEventMixin(*args, **kwargs)

Bases: object

Makes an event operate on the entire population.

This class is designed to be used via multiple inheritance along with one of the main event classes. It indicates that when an event happens, it is applied to the entire population, rather than a single individual. Contrast IndividualScopeEventMixin.

Subclasses should implement apply(self, population) to contain their behaviour.

Create a new population-scoped event.

This calls the base class constructor, passing any arguments through, and sets the event target as the whole population.

apply(population)

Apply this event to the population.

Must be implemented by subclasses.

Parameters:

population – the current population

class IndividualScopeEventMixin(*args, person_id, **kwargs)

Bases: object

Makes an event operate on a single individual.

This class is designed to be used via multiple inheritance along with one of the main event classes. It indicates that when an event happens, it is applied to a single individual, rather than the entire population. Contrast PopulationScopeEventMixin.

Subclasses should implement apply(self, person) to contain their behaviour.

Create a new individual-scoped event.

This calls the base class constructor, passing any arguments through, and sets the event target as the provided person.

Parameters:

person_id – the id of the person this event applies to (must be supplied as a keyword argument)

apply(person_id)

Apply this event to the given person.

Must be implemented by subclasses.

Parameters:

person_id – the person the event happens to