code: def confusion_matrix_oof(
      on_run=True, on_fold=True, on_rep=True, on_exp=True
  ):
      """Callback function to produce confusion matrices for out-of-fold predictions at each stage of
      the Experiment
  
      Parameters
      ----------
      on_run: Boolean, default=True
          If False, skip making confusion matrices for individual Experiment runs
      on_fold: Boolean, default=True
          If False, skip making confusion matrices for individual Experiment folds
      on_rep: Boolean, default=True
          If False, skip making confusion matrices for individual Experiment repetitions
      on_exp: Boolean, default=True
          If False, skip making final confusion matrix for the Experiment
  
      Returns
      -------
      LambdaCallback
          An uninitialized :class:`LambdaCallback` to generate confusion matrices, produced by
          :func:`hyperparameter_hunter.callbacks.bases.lambda_callback`
  
      Notes
      -----
      Unlike :func:`hyperparameter_hunter.callbacks.recipes.confusion_matrix_holdout`, this callback
      function allows `lambda_callback` to automatically aggregate the stats returned by each of the
      "on..." functions given to `lambda_callback`
  
      If the size of this `lambda_callback` implementation is daunting, minimize the helper functions'
      docstrings. It's surprisingly simple"""
  
      # noinspection PyUnusedLocal
      def _on_run_end(data_oof, **kwargs):
          """Callback to execute upon ending an Experiment's run. Note that parameters are
          named after Experiment attributes
  
          Parameters
          ----------
          data_oof: BaseDataset
              :attr:`hyperparameter_hunter.experiments.BaseCVExperiment.data_oof`
          **kwargs: Dict
              If this is given as a parameter to one of `lambda_callback`\'s functions, the entirety
              of the Experiment's attributes will be given as a dict. This can be used for debugging
  
          Returns
          -------
          Array-like"""
          return _confusion_matrix(
              data_oof.target.fold, data_oof.prediction.run
          )
  
      def _on_fold_end(data_oof, validation_index):
          """Callback to execute upon ending an Experiment's fold. Note that parameters are
          named after Experiment attributes
  
          Parameters
          ----------
          data_oof: BaseDataset
              :attr:`hyperparameter_hunter.experiments.BaseCVExperiment.data_oof`
          validation_index: Array-like
              :attr:`hyperparameter_hunter.experiments.BaseCVExperiment.validation_index`
  
          Returns
          -------
          Array-like"""
          return _confusion_matrix(
              data_oof.target.fold,
              data_oof.prediction.rep.iloc[validation_index],
          )
  
      def _on_rep_end(data_train, data_oof):
          """Callback to execute upon ending an Experiment's repetition. Note that parameters are
          named after Experiment attributes
  
          Parameters
          ----------
          data_train: BaseDataset
              :attr:`hyperparameter_hunter.experiments.BaseCVExperiment.data_train`
          data_oof: BaseDataset
              :attr:`hyperparameter_hunter.experiments.BaseCVExperiment.data_oof`
  
          Returns
          -------
          Array-like"""
          # return _confusion_matrix(data_oof.target.final, data_oof.prediction.rep)  # TODO: Should use this when collecting transformed targets
          return _confusion_matrix(
              data_train.target.d, data_oof.prediction.rep
          )  # FLAG: TEMPORARY
  
      def _on_exp_end(data_train, data_oof):
          """Callback to execute upon ending an Experiment. Note that parameters are
          named after Experiment attributes
  
          Parameters
          ----------
          data_train: BaseDataset
              :attr:`hyperparameter_hunter.experiments.BaseCVExperiment.data_train`
          data_oof: BaseDataset
              :attr:`hyperparameter_hunter.experiments.BaseCVExperiment.data_oof`
  
          Returns
          -------
          Array-like"""
          # return _confusion_matrix(data_oof.target.final, data_oof.prediction.final)  # TODO: Should use this when collecting transformed targets
          return _confusion_matrix(
              data_train.target.d, data_oof.prediction.final
          )  # FLAG: TEMPORARY
  
      return lambda_callback(
          on_run_end=_on_run_end if on_run else None,
          on_fold_end=_on_fold_end if on_fold else None,
          on_rep_end=_on_rep_end if on_rep else None,
          on_exp_end=_on_exp_end if on_exp else None,
          agg_name="confusion_matrix_oof",
      )
  
file_path: hyperparameter_hunter/hyperparameter_hunter/callbacks/recipes.py
imports: from hyperparameter_hunter.callbacks.bases import lambda_callback
  from hyperparameter_hunter.data.data_chunks.target_chunks import TrainTargetChunk
  from hyperparameter_hunter.metrics import get_clean_prediction
  from copy import deepcopy
  import numpy
  from typing import List, Optional
  from sklearn.metrics import confusion_matrix
label_desc: Use image node_id 1 for calling a global function with example usage: confusion_matrix_oof(on_run, on_fold, on_rep, on_exp) and returns: lambda_callback, _confusion_matrix, _confusion_matrix, _confusion_matrix, _confusion_matrix