code: def calibration_plot(
      predictions: types.TensorType,
      labels: types.TensorType,
      left: float,
      right: float,
      num_buckets: int,
      weights: Optional[types.TensorType] = None,
  ) -> Tuple[types.TensorType, types.TensorType]:
      # pyformat: disable
      """Calibration plot for predictions in [left, right].
  
      A calibration plot contains multiple buckets, based on the prediction.
      Each bucket contains:
        (i) the weighted sum of predictions that fall within that bucket
       (ii) the weighted sum of labels associated with those predictions
      (iii) the sum of weights of the associated examples
  
      Note that the calibration plot also contains enough information to build
      to prediction histogram (which doesn't need the information about the labels).
  
      Args:
        predictions: Predictions to compute calibration plot for.
        labels: Labels associated with the corresponding predictions.
        left: Left-most bucket boundary.
        right: Right-most bucket boundary.
        num_buckets: Number of buckets to divide [left, right] into.
        weights: Optional weights for each of the predictions/labels. If None,
          each of the predictions/labels will be assumed to have a weight of 1.0.
  
      left=1.0, right=2.0, num_buckets=2 yields buckets:
        bucket 0: (-inf, 1.0)
        bucket 1: [1.0, 1.5)
        bucket 2: [1.5, 2.0)
        bucket 3: [2.0, inf)
  
      The value_op will return a matrix with num_buckets + 2 rows and 3 columns:
      [ bucket 0 weighted prediction sum, weighted label sum, sum of weights ]
      [ bucket 1 weighted prediction sum, weighted label sum, sum of weights ]
      [               :                            :               :         ]
      [               :                            :               :         ]
      [ bucket k weighted prediction sum, weighted label sum, sum of weights ]
      where k = num_buckets + 1
  
      Returns:
        (value_op, update_op) for the calibration plot.
      """
      # pyformat: enable
  
      with tf.compat.v1.variable_scope(
          "calibration_plot", [predictions, labels]
      ):
          predictions_f64 = tf.cast(predictions, tf.float64)
          labels_f64 = tf.cast(labels, tf.float64)
          # Ensure that we don't mistakenly use the non-casted versions.
          del predictions, labels
  
          prediction_bucket_counts = tf.compat.v1.Variable(
              initial_value=[0.0] * (num_buckets + 2),
              dtype=tf.float64,
              trainable=False,
              collections=[
                  tf.compat.v1.GraphKeys.METRIC_VARIABLES,
                  tf.compat.v1.GraphKeys.LOCAL_VARIABLES,
              ],
              validate_shape=True,
              name="prediction_bucket_counts",
          )
          label_bucket_counts = tf.compat.v1.Variable(
              initial_value=[0.0] * (num_buckets + 2),
              dtype=tf.float64,
              trainable=False,
              collections=[
                  tf.compat.v1.GraphKeys.METRIC_VARIABLES,
                  tf.compat.v1.GraphKeys.LOCAL_VARIABLES,
              ],
              validate_shape=True,
              name="label_bucket_counts",
          )
          weight_bucket_counts = tf.compat.v1.Variable(
              initial_value=[0.0] * (num_buckets + 2),
              dtype=tf.float64,
              trainable=False,
              collections=[
                  tf.compat.v1.GraphKeys.METRIC_VARIABLES,
                  tf.compat.v1.GraphKeys.LOCAL_VARIABLES,
              ],
              validate_shape=True,
              name="weight_bucket_counts",
          )
  
          bucket_width = (right - left) / num_buckets
          indices = tf.cast(
              tf.clip_by_value(
                  tf.floor((predictions_f64 - left) / bucket_width),
                  clip_value_min=-1,
                  clip_value_max=num_buckets,
              )
              + 1,
              tf.int32,
          )
  
          if weights is not None:
              weights_f64 = tf.cast(weights, tf.float64)
          else:
              weights_f64 = tf.ones_like(indices, dtype=tf.float64)
  
          update_prediction_buckets_op = tf.compat.v1.scatter_add(
              prediction_bucket_counts,
              indices,
              predictions_f64 * weights_f64,
          )
          update_label_buckets_op = tf.compat.v1.scatter_add(
              label_bucket_counts, indices, labels_f64 * weights_f64
          )
          update_weight_buckets_op = tf.compat.v1.scatter_add(
              weight_bucket_counts, indices, weights_f64
          )
          update_op = tf.group(
              update_prediction_buckets_op,
              update_label_buckets_op,
              update_weight_buckets_op,
          )
          value_op = tf.transpose(
              a=tf.stack(
                  [
                      prediction_bucket_counts,
                      label_bucket_counts,
                      weight_bucket_counts,
                  ]
              )
          )
  
      return value_op, update_op
  
file_path: model-analysis/tensorflow_model_analysis/post_export_metrics/metrics.py
imports: from typing import List, Optional, Tuple
  import numpy
  import tensorflow
  from tensorflow_model_analysis.api import types
label_desc: Use image node_id 4 for calling a global function with example usage: calibration_plot(predictions, labels, left, right, num_buckets, weights) and returns: value_op,  update_op