CallbackMixin

class passengersim.callbacks.base.CallbackMixin[source]

Bases: object

Methods

__init__()

add_callback_events()

Add callback events to the simulation event queue.

apply_callback_functions(sim)

begin_sample_callback(callback)

Register a function to be triggered at the beginning of each sample.

callback_functions()

Get all manually attached callback functions.

daily_callback(callback)

Register a function to be triggered each day during a sample.

end_sample_callback(callback)

Register a function to be triggered at the end of each sample.

finalize_sample_callback(callback)

Register a synchronous callback after all sample events finish.

initialize_sample_callback(callback)

Register a synchronous callback before sample demand generation.

use_registered_callbacks()

Adopt all globally registered callbacks.

initialize_sample_callback(callback: Callable[[CallbackMixin], dict | None] | GenericTracer) → Callable[[CallbackMixin], dict | None] | GenericTracer[source]

Register a synchronous callback before sample demand generation.

The sample number, random seed, and counters have been initialized, but the new sample’s RM steps have not run. Changes to demand inputs affect this sample. Callbacks run in registration order, with configured callbacks first; event priority does not apply.

Parameters:
callback : Callable[[Simulation], dict | None] or GenericTracer

Callback receiving the simulation. Returned dictionaries are stored under callback_data.initialize_sample with trial/sample keys.

Returns:

Callable or GenericTracer – Registered callback, copied fresh if it is a tracer. This method can also be used as a decorator.

finalize_sample_callback(callback: Callable[[CallbackMixin], dict | None] | GenericTracer) → Callable[[CallbackMixin], dict | None] | GenericTracer[source]

Register a synchronous callback after all sample events finish.

Callbacks run before sample diagnostics, departure statistics, history rollover, and other bookkeeping, and are included in sample timing. They run only on successful completion, not as exception cleanup. Configured callbacks run first, followed by manual callbacks, each in registration order. Event priority does not apply. Exceptions propagate.

Parameters:
callback : Callable[[Simulation], dict | None] or GenericTracer

Callback receiving the simulation. Returned dictionaries are stored under callback_data.finalize_sample with trial/sample keys.

Returns:

Callable or GenericTracer – Registered callback, copied fresh if it is a tracer. This method can also be used as a decorator.

end_sample_callback(callback: Callable[[CallbackMixin], dict | None] | GenericTracer)[source]

Register a function to be triggered at the end of each sample.

The callback function will be triggered before counters are reset or history buffers are rolled over.

Parameters:
callback : Callable[[Simulation], None]

The callback function to register. It should accept a single argument, which will be the Simulation object, and return a dictionary of interesting things to store, or nothing.

begin_sample_callback(callback: Callable[[CallbackMixin], dict | None] | GenericTracer)[source]

Register a function to be triggered at the beginning of each sample.

The callback function will be triggered after initial setup including all RM steps for the initial DCP, but before any customers can arrive.

Parameters:
callback : Callable[[Simulation], dict | None]

The callback function to register. It should accept a single argument, which will be the Simulation object, and return a dictionary of interesting things to store, or nothing.

daily_callback(callback: collections.abc.Callable[[CallbackMixin, int], dict | None] | GenericTracer | RmSys)[source]

Register a function to be triggered each day during a sample.

The callback function will be triggered after all RM steps when the day coincides with a DCP.

Parameters:
callback : Callable[[Simulation, int], dict | None]

The callback function to register. It should accept two arguments, which will be the Simulation object and the days_prior, and return None.

callback_functions() → dict[str, list[Callable]][source]

Get all manually attached callback functions.

Callbacks constructed from config.callbacks declarations are excluded. They are owned by this object’s config and are rebuilt by any simulation that runs that config, so copying them elsewhere (e.g. via apply_callback_functions() or worker forwarding) would attach them twice.

Returns:

dict[str, list[Callable]] – Mapping of "<hook>_callbacks" to the manual callbacks for each hook that has been used on this object.

apply_callback_functions(sim: CallbackMixin)[source]
use_registered_callbacks()[source]

Adopt all globally registered callbacks.

add_callback_events()[source]

Add callback events to the simulation event queue.