code: def ece(
      y_true: Union[List[int], np.ndarray],
      y_score: Union[List[float], np.ndarray],
      n_bins: int = 100,
      return_curve: bool = False,
  ) -> Union[float, Tuple[float, Tuple[List[int], List[float]]]]:
      """Compute Expected Calibration Error (ECE) from prediction scores.
  
      ECE measures how close the correct prediction scores are to one and the incorrect prediction scores are to zero.
      ECE ranges from zero to one with the best value zero (the lower the value, the better).
      """
      y_true = np.array(y_true)
      y_score = np.array(y_score)
      assert len(y_true) == len(y_score)
      assert np.all(y_true >= 0) and np.all(y_true <= 1)
      py = np.array([1 - y_score, y_score]).T
      acc, conf = np.zeros(n_bins), np.zeros(n_bins)
      Bm = np.zeros(n_bins)
      ece_curve = []
      thresholds = []
      for m in range(n_bins):
          a, b = m / n_bins, (m + 1) / n_bins
          threshold = (a + b) / 2
          thresholds.append(threshold)
          py_index = (py.T[1] >= threshold).astype(int)
          py_value = py[np.arange(len(py_index)), py_index]
          bin_range = ((py_value > a) & (py_value <= b)).nonzero()[0]
          Bm[m] = len(bin_range)
          if Bm[m] > 0:
              acc[m] = (
                  py_index[bin_range] == y_true[bin_range]
              ).sum() / Bm[m]
              conf[m] = py_value[bin_range].sum() / Bm[m]
          ece_curve.append(Bm[m] * np.abs(acc[m] - conf[m]))
      ece = sum(ece_curve) / sum(Bm)
      if return_curve:
          return ece, (thresholds, ece_curve)
      else:
          return ece
  
file_path: NeMo/nemo/collections/asr/parts/utils/confidence_metrics.py
imports: import math
  import os
  from pathlib import Path
  from typing import List, Optional, Tuple, Union
  import matplotlib.pyplot
  import numpy
  from sklearn.metrics import PrecisionRecallDisplay, RocCurveDisplay, average_precision_score, log_loss, precision_recall_curve, roc_auc_score, roc_curve
label_desc: Use image node_id 5 for calling a global function with example usage: ece(y_true, y_score, n_bins, return_curve) and returns: ece,  thresholds,  ece_curve, ece