The Vivarium Event Framework

vivarium constructs and manages the flow of time through the emission of regularly scheduled events. The tools in this module manage the relationships between event emitters and listeners and provide an interface for user components to register themselves as emitters or listeners to particular events.

The EventManager maintains a mapping between event types and channels. Each event type (and event types must be unique so event type is equivalent to event name, e.g., time_step_prepare) corresponds to an EventChannel, which tracks listeners to that event in prioritized levels and passes on the event to those listeners when emitted.

The EventInterface is exposed off the builder and provides two methods: get_emitter, which returns a callable emitter for the given event type and register_listener, which adds the given listener to the event channel for the given event. This is the only part of the event framework with which client code should interact.

For more information, see the associated event concept note.

class vivarium.framework.event.Event(index, user_data, time, step_size)[source]

An Event object represents the context of an event.

Events themselves are just a bundle of data. They must be emitted along an EventChannel in order for other simulation components to respond to them.

Parameters
index: Index

An index into the population table containing all simulants affected by this event.

user_data: Dict[str, Any]

Any additional data provided by the user about the event.

time: Union[Timestamp, datetime, Number]

The simulation time at which this event will resolve. The current simulation size plus the current time step size.

step_size: Union[Timedelta, timedelta, Number]

The current step size at the time of the event.

split(new_index)[source]

Create a copy of this event with a new index.

This function should be used to emit an event in a new EventChannel in response to an event emitted from a different channel.

Parameters

new_index (Index) – An index into the population table containing all simulants affected by this event.

Returns

The new event.

Return type

Event

class vivarium.framework.event.EventChannel(manager, name)[source]

A named subscription channel that passes events to event listeners.

emit(index, user_data=None)[source]

Notifies all listeners to this channel that an event has occurred.

Events are emitted to listeners in order of priority (with order 0 being first and order 9 last), with no ordering within a particular priority level guaranteed.

Parameters
  • index (Index) – An index into the population table containing all simulants affected by this event.

  • user_data (Optional[Dict]) – Any additional data provided by the user about the event.

Return type

Event

class vivarium.framework.event.EventManager[source]

The configuration for the event system.

Notes

Client code should never need to interact with this class except through the decorators in this module and the emitter function exposed on the builder during the setup phase.

property name

The name of this component.

get_channel(name)[source]
setup(builder)[source]

Performs this component’s simulation setup.

Parameters

builder – Object giving access to core framework functionality.

on_post_setup(event)[source]
get_emitter(name)[source]

Get an emitter function for the named event.

Parameters

name (str) – The name of the event.

Returns

  • A function that accepts an index and optional user data. This function

  • creates and timestamps an Event and distributes it to all interested

  • listeners

Return type

Callable[[Index, Optional[Dict]], Event]

register_listener(name, listener, priority=5)[source]

Registers a new listener to the named event.

Parameters
  • name (str) – The name of the event.

  • listener (Callable) – The consumer of the named event.

  • priority (int) – Number in range(10) used to assign the ordering in which listeners process the event.

get_listeners(name)[source]

Get all listeners registered for the named event.

Parameters

name (str) – The name of the event.

Returns

  • A dictionary that maps each priority level of the named event’s

  • listeners to a list of listeners at that level.

Return type

Dict[int, List[Callable]]

list_events()[source]

List all event names known to the event system.

Returns

A list of all known event names.

Return type

List[Event]

Notes

This value can change after setup if components dynamically create new event labels.

class vivarium.framework.event.EventInterface(manager)[source]

The public interface for the event system.

Parameters

manager (EventManager) –

get_emitter(name)[source]

Gets an emitter for a named event.

Parameters

name (str) – The name of the event the requested emitter will emit. Users may provide their own named events by requesting an emitter with this function, but should do so with caution as it makes time much more difficult to think about.

Returns

  • An emitter for the named event. The emitter should be called by

  • the requesting component at the appropriate point in the simulation

  • lifecycle.

Return type

Callable[[Index, Optional[Dict]], Event]

register_listener(name, listener, priority=5)[source]

Registers a callable as a listener to a events with the given name.

The listening callable will be called with a named Event as its only argument any time the event emitter is invoked from somewhere in the simulation.

The framework creates the following events and emits them at different points in the simulation:

  • At the end of the setup phase: post_setup

  • Every time step: - time_step__prepare - time_step - time_step__cleanup - collect_metrics

  • At simulation end: simulation_end

Parameters
  • name (str) – The name of the event to listen for.

  • listener (Callable[[Event], None]) – The callable to be invoked any time an Event with the given name is emitted.

  • priority (int) – One of {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. An indication of the order in which event listeners should be called. Listeners with smaller priority values will be called earlier. Listeners with the same priority have no guaranteed ordering. This feature should be avoided if possible. Components should strive to obey the Markov property as they transform the state table (the state of the simulation at the beginning of the next time step should only depend on the current state of the system).

Return type

None