code: def rebin(
      thresholds: List[float],
      histogram: Histogram,
      num_buckets: int = DEFAULT_NUM_BUCKETS,
      left: float = 0.0,
      right: float = 1.0,
  ) -> Histogram:
      """Applies new thresholds to an existing calibration histogram.
  
      Args:
        thresholds: New thresholds to apply to the histogram. Must be in sorted
          order, but need not be evenly spaced.
        histogram: Existing calibration histogram.
        num_buckets: Number of buckets in existing histogram.
        left: Left boundary for existing histogram.
        right: Right boundary for existing histogram.
  
      Returns:
        A histogram of len(thresholds) where the buckets with IDs (0, 1, 2, ...)
        correspond to the intervals:
          [thresholds[0], thresholds[1]), ... [thresholds[i], thresholds[i+1])
        Any values in buckets -inf or +inf will be added to the start and end
        thresholds respectively. Unlike the input histogram empty buckets will be
        returned.
      """
      buckets = []
      offset = 0
      total_weighted_labels = 0.0
      total_weighted_predictions = 0.0
      total_weighted_examples = 0.0
      for bucket in histogram:
          if bucket.bucket_id == 0:
              pred = float("-inf")
          elif bucket.bucket_id >= num_buckets + 1:
              pred = float("inf")
          else:
              pred = (bucket.bucket_id - 1) / num_buckets * (
                  right - left
              ) + left
          if (
              offset + 1 < len(thresholds)
              and pred >= thresholds[offset + 1]
          ):
              buckets.append(
                  Bucket(
                      offset,
                      total_weighted_labels,
                      total_weighted_predictions,
                      total_weighted_examples,
                  )
              )
              offset += 1
              total_weighted_labels = 0.0
              total_weighted_predictions = 0.0
              total_weighted_examples = 0.0
              while (
                  offset + 1 < len(thresholds)
                  and pred >= thresholds[offset + 1]
              ):
                  buckets.append(Bucket(offset, 0.0, 0.0, 0.0))
                  offset += 1
          total_weighted_labels += bucket.weighted_labels
          total_weighted_predictions += bucket.weighted_predictions
          total_weighted_examples += bucket.weighted_examples
      buckets.append(
          Bucket(
              offset,
              total_weighted_labels,
              total_weighted_predictions,
              total_weighted_examples,
          )
      )
      offset += 1
      while offset < len(thresholds):
          buckets.append(Bucket(offset, 0.0, 0.0, 0.0))
          offset += 1
      return buckets
  
file_path: model-analysis/tensorflow_model_analysis/metrics/calibration_histogram.py
imports: import dataclasses
  import operator
  from typing import Dict, Iterable, List, Optional
  import apache_beam
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.proto import config_pb2
label_desc: Use image node_id 2 for calling a global function with example usage: rebin(thresholds, histogram, num_buckets, left, right) and returns: buckets