code: def bin_wise_errors(
      fold_errors,
      fold_bins,
      num_bins,
      targets,
      uncertainty_key,
      error_scaling_factor,
  ):
      """
      Helper function for get_mean_errors. Calculates the mean error for each bin and for each target.
  
      Args:
          fold_errors (Pandas Dataframe): Pandas Dataframe of errors for this fold.
          fold_bins (Pandas Dataframe): Pandas Dataframe of predicted quantile bins for this fold.
          num_bins (int): Number of quantile bins,
          targets (list) list of targets to measure uncertainty estimation,
          uncertainty_key (string): Name of uncertainty type to calculate accuracy for,
  
  
      Returns:
          [Dict]: Dict with mean error statistics.
      """
  
      all_target_error = []
      all_qs_error = [[] for x in range(num_bins)]
      all_qs_error_concat_targets_sep = [
          [[] for y in range(num_bins)] for x in range(len(targets))
      ]
  
      for i, target_idx in enumerate(targets):
          true_errors_ti = fold_errors[
              (fold_errors["target_idx"] == target_idx)
          ][["uid", uncertainty_key + " Error"]]
          pred_bins_ti = fold_bins[
              (fold_errors["target_idx"] == target_idx)
          ][["uid", uncertainty_key + " Uncertainty bins"]]
  
          # Zip to dictionary
          true_errors_ti = dict(
              zip(
                  true_errors_ti.uid,
                  true_errors_ti[uncertainty_key + " Error"]
                  * error_scaling_factor,
              )
          )
          pred_bins_ti = dict(
              zip(
                  pred_bins_ti.uid,
                  pred_bins_ti[uncertainty_key + " Uncertainty bins"],
              )
          )
  
          pred_bins_keys = []
          pred_bins_errors = []
  
          # This is saving them from best quantile of predictions to worst quantile of predictions in terms of uncertainty
          for j in range(num_bins):
              inner_list = list(
                  [
                      key
                      for key, val in pred_bins_ti.items()
                      if str(j) == str(val)
                  ]
              )
              inner_list_errors = []
  
              for id_ in inner_list:
                  inner_list_errors.append(
                      list(
                          [
                              val
                              for key, val in true_errors_ti.items()
                              if str(key) == str(id_)
                          ]
                      )[0]
                  )
  
              pred_bins_errors.append(inner_list_errors)
              pred_bins_keys.append(inner_list)
  
          # Now for each bin, get the mean error
          inner_errors = []
          for bin in range(num_bins):
              # pred_b_keys = pred_bins_keys[bin]
              pred_b_errors = pred_bins_errors[bin]
  
              # test for empty bin, it would've created a mean_error==nan , so don't add it!
              if pred_b_errors == []:
                  continue
  
              mean_error = np.mean(pred_b_errors)
              all_qs_error[bin].append(mean_error)
              all_qs_error_concat_targets_sep[i][bin].append(
                  pred_b_errors
              )
              inner_errors.append(mean_error)
  
          all_target_error.append(np.mean(inner_errors))
  
      mean_all_targets = np.mean(all_target_error)
      mean_all_bins = []
      for x in all_qs_error:
          if x == []:
              mean_all_bins.append(None)
          else:
              mean_all_bins.append(np.mean(x))
  
      return {
          "mean all targets": mean_all_targets,
          "mean all bins": mean_all_bins,
          "all bins": all_qs_error,
          "all bins concatenated targets seperated": all_qs_error_concat_targets_sep,
      }
  
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 5 for calling a global function with example usage: bin_wise_errors(fold_errors, fold_bins, num_bins, targets, uncertainty_key, error_scaling_factor) and returns: dict