code: def generate_metrics(model, partition):
      r"""Generate model evaluation metrics for all estimators.
  
      Parameters
      ----------
      model : alphapy.Model
          The model object with stored predictions.
      partition : alphapy.Partition
          Reference to the dataset.
  
      Returns
      -------
      model : alphapy.Model
          The model object with the completed metrics.
  
      Notes
      -----
      AlphaPy takes a brute-force approach to calculating each metric.
      It calls every scikit-learn function without exception. If the
      calculation fails for any reason, then the evaluation will still
      continue without error.
  
      References
      ----------
      For more information about model evaluation and the associated metrics,
      refer to [EVAL]_.
  
      .. [EVAL] http://scikit-learn.org/stable/modules/model_evaluation.html
  
      """
  
      logger.info("=" * 80)
      logger.info("Metrics for: %s", partition)
  
      # Extract model paramters.
  
      model_type = model.specs["model_type"]
  
      # Extract model data.
  
      if partition == Partition.train:
          expected = model.y_train
      else:
          expected = model.y_test
  
      # Generate Metrics
  
      if expected.any():
          # Add blended model to the list of algorithms.
          if len(model.algolist) > 1:
              algolist = copy(model.algolist)
              algolist.append("BLEND")
          else:
              algolist = model.algolist
  
          # get the metrics for each algorithm
          for algo in algolist:
              # get predictions for the given algorithm
              predicted = model.preds[(algo, partition)]
              # classification metrics
              if model_type == ModelType.classification:
                  probas = model.probas[(algo, partition)]
                  try:
                      model.metrics[
                          (algo, partition, "accuracy")
                      ] = accuracy_score(expected, predicted)
                  except:
                      logger.info("Accuracy Score not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "average_precision")
                      ] = average_precision_score(expected, probas)
                  except:
                      logger.info(
                          "Average Precision Score not calculated"
                      )
                  try:
                      model.metrics[
                          (algo, partition, "balanced_accuracy")
                      ] = balanced_accuracy_score(expected, predicted)
                  except:
                      logger.info("Accuracy Score not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "brier_score_loss")
                      ] = brier_score_loss(expected, probas)
                  except:
                      logger.info("Brier Score not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "cohen_kappa")
                      ] = cohen_kappa_score(expected, predicted)
                  except:
                      logger.info("Cohen's Kappa Score not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "confusion_matrix")
                      ] = confusion_matrix(expected, predicted)
                  except:
                      logger.info("Confusion Matrix not calculated")
                  try:
                      model.metrics[(algo, partition, "f1")] = f1_score(
                          expected, predicted
                      )
                  except:
                      logger.info("F1 Score not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "neg_log_loss")
                      ] = log_loss(expected, probas)
                  except:
                      logger.info("Log Loss not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "precision")
                      ] = precision_score(expected, predicted)
                  except:
                      logger.info("Precision Score not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "recall")
                      ] = recall_score(expected, predicted)
                  except:
                      logger.info("Recall Score not calculated")
                  try:
                      fpr, tpr, _ = roc_curve(expected, probas)
                      model.metrics[(algo, partition, "roc_auc")] = auc(
                          fpr, tpr
                      )
                  except:
                      logger.info("ROC AUC Score not calculated")
              # regression metrics
              elif model_type == ModelType.regression:
                  try:
                      model.metrics[
                          (algo, partition, "explained_variance")
                      ] = explained_variance_score(expected, predicted)
                  except:
                      logger.info(
                          "Explained Variance Score not calculated"
                      )
                  try:
                      model.metrics[
                          (algo, partition, "neg_mean_absolute_error")
                      ] = mean_absolute_error(expected, predicted)
                  except:
                      logger.info("Mean Absolute Error not calculated")
                  try:
                      model.metrics[
                          (algo, partition, "neg_median_absolute_error")
                      ] = median_absolute_error(expected, predicted)
                  except:
                      logger.info(
                          "Median Absolute Error not calculated"
                      )
                  try:
                      model.metrics[
                          (algo, partition, "neg_mean_squared_error")
                      ] = mean_squared_error(expected, predicted)
                  except:
                      logger.info("Mean Squared Error not calculated")
                  try:
                      model.metrics[
                          (
                              algo,
                              partition,
                              "neg_mean_squared_log_error",
                          )
                      ] = mean_squared_log_error(expected, predicted)
                  except:
                      logger.info(
                          "Mean Squared Log Error not calculated"
                      )
                  try:
                      model.metrics[(algo, partition, "r2")] = r2_score(
                          expected, predicted
                      )
                  except:
                      logger.info("R-Squared Score not calculated")
          # log the metrics for each algorithm
          for algo in model.algolist:
              logger.info("-" * 80)
              logger.info("Algorithm: %s", algo)
              metrics = [
                  (k[2], v)
                  for k, v in list(model.metrics.items())
                  if k[0] == algo and k[1] == partition
              ]
              for key, value in sorted(metrics):
                  svalue = str(value)
                  svalue.replace("\n", " ")
                  logger.info("%s: %s", key, svalue)
      else:
          logger.info("No labels for generating %s metrics", partition)
  
      return model
  
imports: from alphapy.estimators import scorers
  from alphapy.estimators import xgb_score_map
  from alphapy.features import feature_scorers
  from alphapy.frame import read_frame
  from alphapy.frame import write_frame
  from alphapy.globals import Encoders
  from alphapy.globals import ModelType
  from alphapy.globals import Objective
  from alphapy.globals import Partition, datasets
  from alphapy.globals import PSEP, SSEP, USEP
  from alphapy.globals import SamplingMethod
  from alphapy.globals import Scalers
  from alphapy.utilities import get_datestamp
  from alphapy.utilities import most_recent_file
  from copy import copy
  from datetime import datetime
  import itertools
  import joblib
  from keras.models import load_model
  import logging
  import numpy
  import pandas
  from sklearn.calibration import CalibratedClassifierCV
  from sklearn.linear_model import LogisticRegression
  from sklearn.linear_model import RidgeCV
  from sklearn.metrics import accuracy_score
  from sklearn.metrics import auc
  from sklearn.metrics import average_precision_score
  from sklearn.metrics import balanced_accuracy_score
  from sklearn.metrics import brier_score_loss
  from sklearn.metrics import classification_report
  from sklearn.metrics import cohen_kappa_score
  from sklearn.metrics import confusion_matrix
  from sklearn.metrics import explained_variance_score
  from sklearn.metrics import f1_score
  from sklearn.metrics import log_loss
  from sklearn.metrics import mean_absolute_error
  from sklearn.metrics import mean_squared_error
  from sklearn.metrics import mean_squared_log_error
  from sklearn.metrics import median_absolute_error
  from sklearn.metrics import precision_score
  from sklearn.metrics import r2_score
  from sklearn.metrics import recall_score
  from sklearn.metrics import roc_auc_score
  from sklearn.metrics import roc_curve
  from sklearn.metrics.cluster import adjusted_rand_score
  from sklearn.model_selection import cross_val_score
  from sklearn.model_selection import train_test_split
  import sys
  import yaml
label_desc: Use image node_id 10 for calling a global function with example usage: generate_metrics(model, partition) and returns: model