CallbackContext#

class sklearn.callback.CallbackContext[source]#

Task level context for the callbacks.

This class is responsible for managing the callbacks and holding the tree structure of an estimator’s tasks. Each instance corresponds to a task of the estimator.

This class should not be instantiated directly, but through the _init_callback_context method of the estimator to create the root context or using the subcontext method of this class to create sub-contexts.

These contexts are passed to the callback hooks to be able to keep track of the position of a task in the task tree from within the callbacks.

Attributes:
task_namestr

The name of the task this context is responsible for.

task_idint

The identifier of the task this context is responsible for. It uniquely identifies the task among its siblings.

max_subtasksint or None

The maximum number of children tasks for this task. 0 means it’s a leaf. None means the maximum number of subtasks is not known in advance.

sequential_subtasksbool

Whether this context’s subtasks are sequential. When True, children contexts’ have consecutive integer task_ids starting from 0.

estimator_namestr

The name of the estimator that holds this context.

parentCallbackContext or None

The parent context of this context. None if this context is the root.

root_uuiduuid.UUID instance

The UUID of the root context. All contexts in the same task tree have the same root UUID that is used to identify the task tree itself.

init_timedatetime.datetime

The time when the context was initialised, in the UTC timezone.

source_estimator_namestr or None

The name of the estimator that holds the parent task this task was merged with. None if it was not merged with another context.

source_task_namestr or None

The task name of the parent task this task was merged with. None if it was not merged with another context.

call_on_fit_task_begin(*, estimator, X=None, y=None, metadata=None, reconstruction_attributes=None)[source]#

Call the on_fit_task_begin hook of the callbacks.

Parameters:
estimatorestimator instance

The estimator calling the callback hook.

Xarray-like or None, default=None

The training data of the current task.

yarray-like or None, default=None

The training targets of the current task.

metadatadict or None, default=None

A dictionary containing training metadata for the current task.

reconstruction_attributesdict or None, default=None

A dictionary of the sufficient fitted attributes needed to construct a fitted_estimator from the current state of the estimator, i.e. an estimator instance ready to predict, transform, etc … as if the fit had stopped at the beginning of this task. The fitted_estimator is the object that will be passed to the callbacks, if required.

call_on_fit_task_end(*, estimator, X=None, y=None, metadata=None, reconstruction_attributes=None)[source]#

Call the on_fit_task_end hook of the callbacks.

Parameters:
estimatorestimator instance

The estimator calling the callback hook.

Xarray-like or None, default=None

The training data of the current task.

yarray-like or None, default=None

The training targets of the current task.

metadatadict or None, default=None

A dictionary containing training metadata of the current task.

reconstruction_attributesdict or None, default=None

A dictionary of the sufficient fitted attributes needed to construct a fitted_estimator from the current state of the estimator, i.e. an estimator instance ready to predict, transform, etc … as if the fit had stopped at the end of this task. The fitted_estimator is the object that will be passed to the callbacks, if required.

Returns:
stopbool

Whether or not to stop the current level of iterations at this end of this task.

propagate_callback_context(sub_estimator)[source]#

Propagate the context and callbacks to a sub-estimator.

Clear the propagated callbacks from the sub-estimator on exit.

Only auto-propagated callbacks are propagated to the sub-estimator. An error is raised if the sub-estimator already holds auto-propagated callbacks.

The sub-estimator receives this context as an attribute named _parent_callback_ctx so that the meta-estimator’s task tree can be merged with the sub-estimator’s one.

Parameters:
sub_estimatorestimator instance

The estimator to propagate the callbacks and context to.

subcontext(task_name='', task_id=None, max_subtasks=0, sequential_subtasks=True)[source]#

Create a context for a subtask of the current task.

Parameters:
task_namestr, default=””

The name of the subtask.

task_idint or None, default=None

An identifier of the subtask. It must be distinct from the task_ids of its siblings. If None, task_id is automatically set to the next available integer task_id.

max_subtasksint or None, default=0

The maximum number of tasks that can be children of the subtask. 0 means it’s a leaf. None means the maximum number of subtasks is not known in advance.

sequential_subtasksbool, default=True

Whether the new context’s subtasks are sequential. If True, children contexts of the new context, created via subcontext, will have automatically assigned consecutive integer task_ids starting from 0.