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: pd.Index[int]
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: ClockTime
The simulation time at which this event will resolve. The current simulation size plus the current time step size.
- step_size: ClockStepSize
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.
- class vivarium.framework.event.EventChannel(manager, name)[source]
A named subscription channel that passes events to event listeners.
- Parameters:
manager (EventManager)
name (str)
- 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.
- 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:
- 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: :rtype:
None
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