code: def get_mean_errors(
      bin_predictions: Dict[str, "pd.DataFrame"],
      uncertainty_pairs: List,
      num_bins: int,
      targets: List[int],
      num_folds: int = 8,
      error_scaling_factor: float = 1.0,
      combine_middle_bins: bool = False,
  ) -> Dict:
      """
      Evaluate uncertainty estimation's mean error of each bin.
      For each bin, we calculate the mean localization error for each target and for all targets.
      We calculate the mean error for each dictionary in the bin_predictions dict. For each bin, we calculate: a) the mean
      and std over all folds and all targets b) the mean and std for each target over all folds.
  
      Args:
          bin_predictions (Dict): Dict of Pandas DataFrames where each DataFrame has errors, predicted bins for all
          uncertainty measures for a model.
          uncertainty_pairs (List[Tuple[str, str]]): List of tuples describing the different uncertainty combinations to test.
          num_bins (int): Number of quantile bins.
          targets (List[str]): List of targets to measure uncertainty estimation.
          num_folds (int, optional): Number of folds. Defaults to 8.
          error_scaling_factor (int, optional): Scale error factor. Defaults to 1.
          combine_middle_bins (bool, optional): Combine middle bins if True. Defaults to False.
  
      Returns:
          Dict[str, Union[Dict[str, List[List[float]]], List[Dict[str, List[float]]]]]: Dictionary with mean error for all
           targets combined and targets separated.
              Keys that are returned:
                  "all mean error bins nosep":  For every fold, the mean error for each bin. All targets are combined in the same list.
                  "all mean error bins targets sep":   For every fold, the mean error for each bin. Each target is in a separate list.
                  "all error concat bins targets nosep":  For every fold, every error value in a list. Each target is in the same list. The list is flattened for all the folds.
                  "all error concat bins targets sep foldwise":  For every fold, every error value in a list. Each target is in a separate list. Each list has a list of results by fold.
                  "all error concat bins targets sep all": For every fold, every error value in a list. Each target is in a separate list. The list is flattened for all the folds.
  
      """
      # If we are combining the middle bins, we only have the 2 edge bins and the middle bins are combined into 1 bin.
      if combine_middle_bins:
          num_bins = 3
  
      # initialize empty dicts
      all_mean_error_bins = {}
      all_mean_error_bins_targets_sep = {}
      all_concat_error_bins_target_sep_foldwise: List[Dict] = [
          {} for x in range(len(targets))
      ]
      all_concat_error_bins_target_sep_all: List[Dict] = [
          {} for x in range(len(targets))
      ]
  
      all_concat_error_bins_target_nosep = {}
      # Loop over models (model) and uncertainty methods (uncert_pair)
      for i, (model, data_structs) in enumerate(
          bin_predictions.items()
      ):
          for (
              uncert_pair
          ) in (
              uncertainty_pairs
          ):  # uncert_pair = [pair name, error name , uncertainty name]
              uncertainty_type = uncert_pair[0]
  
              # Initialize lists to store fold-wise results
              fold_mean_targets = []
              fold_mean_bins: List[List[float]] = [
                  [] for x in range(num_bins)
              ]
              fold_all_bins: List[List[float]] = [
                  [] for x in range(num_bins)
              ]
              fold_all_bins_concat_targets_sep_foldwise: List[
                  List[List[float]]
              ] = [
                  [[] for y in range(num_bins)]
                  for x in range(len(targets))
              ]
              fold_all_bins_concat_targets_sep_all: List[
                  List[List[float]]
              ] = [
                  [[] for y in range(num_bins)]
                  for x in range(len(targets))
              ]
  
              fold_all_bins_concat_targets_nosep: List[List[float]] = [
                  [] for x in range(num_bins)
              ]
  
              for fold in range(num_folds):
                  # Get the errors and predicted bins for this fold
                  fold_errors = data_structs[
                      (data_structs["Testing Fold"] == fold)
                  ][["uid", "target_idx", uncertainty_type + " Error"]]
                  fold_bins = data_structs[
                      (data_structs["Testing Fold"] == fold)
                  ][
                      [
                          "uid",
                          "target_idx",
                          uncertainty_type + " Uncertainty bins",
                      ]
                  ]
  
                  return_dict = bin_wise_errors(
                      fold_errors,
                      fold_bins,
                      num_bins,
                      targets,
                      uncertainty_type,
                      error_scaling_factor=error_scaling_factor,
                  )
                  fold_mean_targets.append(
                      return_dict["mean all targets"]
                  )
  
                  for idx_bin in range(
                      len(return_dict["mean all bins"])
                  ):
                      fold_mean_bins[idx_bin].append(
                          return_dict["mean all bins"][idx_bin]
                      )
                      fold_all_bins[idx_bin] = (
                          fold_all_bins[idx_bin]
                          + return_dict["all bins"][idx_bin]
                      )
  
                      concat_no_sep = [
                          x[idx_bin]
                          for x in return_dict[
                              "all bins concatenated targets seperated"
                          ]
                      ]
  
                      flattened_concat_no_sep = [
                          x
                          for sublist in concat_no_sep
                          for x in sublist
                      ]
                      flattened_concat_no_sep = [
                          x
                          for sublist in flattened_concat_no_sep
                          for x in sublist
                      ]
  
                      fold_all_bins_concat_targets_nosep[idx_bin] = (
                          fold_all_bins_concat_targets_nosep[idx_bin]
                          + flattened_concat_no_sep
                      )
  
                      for target_idx in range(len(targets)):
                          fold_all_bins_concat_targets_sep_foldwise[
                              target_idx
                          ][idx_bin] = (
                              fold_all_bins_concat_targets_sep_foldwise[
                                  target_idx
                              ][idx_bin]
                              + return_dict[
                                  "all bins concatenated targets seperated"
                              ][target_idx][idx_bin]
                          )
  
                          if (
                              return_dict[
                                  "all bins concatenated targets seperated"
                              ][target_idx][idx_bin]
                              != []
                          ):
                              combined = (
                                  fold_all_bins_concat_targets_sep_all[
                                      target_idx
                                  ][idx_bin]
                                  + return_dict[
                                      "all bins concatenated targets seperated"
                                  ][target_idx][idx_bin][0]
                              )
                          else:
                              combined = (
                                  fold_all_bins_concat_targets_sep_all[
                                      target_idx
                                  ][idx_bin]
                              )
  
                          fold_all_bins_concat_targets_sep_all[
                              target_idx
                          ][idx_bin] = combined
  
              # reverse orderings
              fold_mean_bins = fold_mean_bins[::-1]
              fold_all_bins = fold_all_bins[::-1]
              fold_all_bins_concat_targets_nosep = (
                  fold_all_bins_concat_targets_nosep[::-1]
              )
              fold_all_bins_concat_targets_sep_foldwise = [
                  x[::-1]
                  for x in fold_all_bins_concat_targets_sep_foldwise
              ]
              fold_all_bins_concat_targets_sep_all = [
                  x[::-1] for x in fold_all_bins_concat_targets_sep_all
              ]
  
              all_mean_error_bins[
                  model + " " + uncertainty_type
              ] = fold_mean_bins
              all_mean_error_bins_targets_sep[
                  model + " " + uncertainty_type
              ] = fold_all_bins
  
              all_concat_error_bins_target_nosep[
                  model + " " + uncertainty_type
              ] = fold_all_bins_concat_targets_nosep
  
              for target_idx in range(
                  len(fold_all_bins_concat_targets_sep_foldwise)
              ):
                  all_concat_error_bins_target_sep_foldwise[target_idx][
                      model + " " + uncertainty_type
                  ] = fold_all_bins_concat_targets_sep_foldwise[
                      target_idx
                  ]
                  all_concat_error_bins_target_sep_all[target_idx][
                      model + " " + uncertainty_type
                  ] = fold_all_bins_concat_targets_sep_all[target_idx]
  
      return {
          "all mean error bins nosep": all_mean_error_bins,
          "all mean error bins targets sep": all_mean_error_bins_targets_sep,
          "all error concat bins targets nosep": all_concat_error_bins_target_nosep,
          "all error concat bins targets sep foldwise": all_concat_error_bins_target_sep_foldwise,
          "all error concat bins targets sep all": all_concat_error_bins_target_sep_all,
      }
  
file_path: pykale/kale/evaluate/uncertainty_metrics.py
imports: from typing import Dict, List
  import numpy
  import pandas
  from kale.evaluate.similarity_metrics import jaccard_similarity
  from kale.prepdata.string_transform import strip_for_bound
label_desc: Use image node_id 3 for calling a global function with example usage: get_mean_errors(bin_predictions, uncertainty_pairs, num_bins, targets, num_folds, error_scaling_factor, combine_middle_bins) and returns: dict