Callbacks#
Callback hooks that plug into the nested cross-validation loop.
- class nestkit.callbacks.FoldCallback(*args, **kwargs)[source]#
Bases:
ProtocolRuntime-checkable protocol for nested CV fold callbacks.
Any object that implements the five hook methods below can be used as a callback. The nested CV engine calls these hooks at well- defined points during execution.
- on_outer_fold_start(fold_idx, train_idx, test_idx)[source]#
Called before inner search begins for an outer fold.
- on_inner_search_complete(fold_idx, search)[source]#
Called after the inner hyperparameter search finishes.
- on_post_processing_complete(fold_idx, artifacts)[source]#
Called after any post-processing (e.g., threshold tuning).
- on_outer_fold_complete(fold_idx, result)[source]#
Called after the outer fold evaluation is complete.
Examples
>>> class MyCallback: ... def on_outer_fold_start(self, fold_idx, train_idx, test_idx): ... print(f"Starting fold {fold_idx}") ... def on_inner_search_complete(self, fold_idx, search): ... ... def on_post_processing_complete(self, fold_idx, artifacts): ... ... def on_outer_fold_complete(self, fold_idx, result): ... ... def on_nested_cv_complete(self, results): ...
See also
- class nestkit.callbacks.ProgressCallback(n_outer_folds=None)[source]#
Bases:
objectDisplay a tqdm progress bar during nested cross-validation.
The progress bar is created lazily on the first
on_outer_fold_startcall and advances by one step after each outer fold completes. Iftqdmis not installed the callback silently does nothing.- Parameters:
n_outer_folds (int or None, optional) – Total number of outer folds. Passed as the
totalargument totqdm. IfNone, the progress bar will have indeterminate length.
Examples
>>> cb = ProgressCallback(n_outer_folds=5)
See also
LoggingCallbackText-based logging alternative.
- class nestkit.callbacks.CheckpointCallback(path)[source]#
Bases:
objectPickle intermediate fold results to disk after each outer fold.
After every outer fold, the fold result is saved as
fold_<idx>.pklinside the given directory. When the full nested CV completes, the final results object is saved asfinal_results.pkl.- Parameters:
path (str or pathlib.Path) – Directory in which checkpoint files are written. Created automatically (including parents) if it does not exist.
- path#
Resolved checkpoint directory.
- Type:
Examples
>>> cb = CheckpointCallback("/tmp/ncv_checkpoints")
- class nestkit.callbacks.LoggingCallback(level=20)[source]#
Bases:
objectEmit structured log messages at each nested CV lifecycle event.
Logs fold start (with train/test sizes), inner search completion (with best parameters and score), post-processing completion, fold completion (with elapsed time), and overall completion.
- Parameters:
level (int, default=logging.INFO) – Python logging level for all emitted messages.
- _fold_start_times#
Mapping from fold index to wall-clock start time, used to compute elapsed seconds.
Examples
>>> import logging >>> cb = LoggingCallback(level=logging.DEBUG)
See also
ProgressCallbackVisual progress bar alternative.