code: def testCalibrationHistogramWithK(self):
      histogram = calibration_histogram.calibration_histogram(
          sub_key=metric_types.SubKey(k=2), example_weighted=True
      )[0]
  
      example1 = {
          "labels": np.array([2]),
          "predictions": np.array([0.2, 0.05, 0.1, 0.05]),
          "example_weights": np.array([1.0]),
      }
      example2 = {
          "labels": np.array([2]),
          "predictions": np.array([0.7, 0.1, 0.8, 0.5]),
          "example_weights": np.array([2.0]),
      }
      example3 = {
          "labels": np.array([3]),
          "predictions": np.array([0.1, 0.5, 0.3, 0.4]),
          "example_weights": np.array([3.0]),
      }
      example4 = {
          "labels": np.array([0]),
          "predictions": np.array([-0.1, -0.2, -0.7, -0.4]),
          "example_weights": np.array([4.0]),
      }
      example5 = {
          "labels": np.array([1]),
          "predictions": np.array([0.3, 0.5, 0.0, 0.4]),
          "example_weights": np.array([5.0]),
      }
      example6 = {
          "labels": np.array([2]),
          "predictions": np.array([0.1, 0.1, 0.8, 0.7]),
          "example_weights": np.array([6.0]),
      }
      example7 = {
          "labels": np.array([2]),
          "predictions": np.array([0.0, 0.2, 0.1, 0.0]),
          "example_weights": np.array([7.0]),
      }
      example8 = {
          "labels": np.array([0]),
          "predictions": np.array([1.1, 0.3, 1.05, 0.2]),
          "example_weights": np.array([8.0]),
      }
  
      with beam.Pipeline() as pipeline:
          # pylint: disable=no-value-for-parameter
          result = (
              pipeline
              | "Create"
              >> beam.Create(
                  [
                      example1,
                      example2,
                      example3,
                      example4,
                      example5,
                      example6,
                      example7,
                      example8,
                  ]
              )
              | "Process"
              >> beam.Map(metric_util.to_standard_metric_inputs)
              | "AddSlice" >> beam.Map(lambda x: ((), x))
              | "ComputeHistogram"
              >> beam.CombinePerKey(histogram.combiner)
          )
  
          # pylint: enable=no-value-for-parameter
  
          def check_result(got):
              try:
                  self.assertLen(got, 1)
                  got_slice_key, got_plots = got[0]
                  self.assertEqual(got_slice_key, ())
                  self.assertLen(got_plots, 1)
                  key = metric_types.PlotKey(
                      name=(
                          "_calibration_histogram:fractional_labels=True,left=0.0,"
                          "num_buckets=10000,prediction_based_bucketing=True,right=1.0"
                      ),
                      sub_key=metric_types.SubKey(k=2),
                      example_weighted=True,
                  )
                  self.assertIn(key, got_plots)
                  got_histogram = got_plots[key]
                  self.assertLen(got_histogram, 5)
                  self.assertEqual(
                      got_histogram[0],
                      calibration_histogram.Bucket(
                          bucket_id=0,
                          weighted_labels=0.0 * 4.0,
                          weighted_predictions=-0.2 * 4.0,
                          weighted_examples=4.0,
                      ),
                  )
                  self.assertEqual(
                      got_histogram[1],
                      calibration_histogram.Bucket(
                          bucket_id=1001,
                          weighted_labels=1.0 + 7 * 1.0,
                          weighted_predictions=0.1 + 7 * 0.1,
                          weighted_examples=1.0 + 7.0,
                      ),
                  )
                  self.assertEqual(
                      got_histogram[2],
                      calibration_histogram.Bucket(
                          bucket_id=4001,
                          weighted_labels=1.0 * 3.0 + 0.0 * 5.0,
                          weighted_predictions=0.4 * 3.0 + 0.4 * 5.0,
                          weighted_examples=3.0 + 5.0,
                      ),
                  )
                  self.assertEqual(
                      got_histogram[3],
                      calibration_histogram.Bucket(
                          bucket_id=7001,
                          weighted_labels=0.0 * 2.0 + 0.0 * 6.0,
                          weighted_predictions=0.7 * 2.0 + 0.7 * 6.0,
                          weighted_examples=2.0 + 6.0,
                      ),
                  )
                  self.assertEqual(
                      got_histogram[4],
                      calibration_histogram.Bucket(
                          bucket_id=10001,
                          weighted_labels=0.0 * 8.0,
                          weighted_predictions=1.05 * 8.0,
                          weighted_examples=8.0,
                      ),
                  )
  
              except AssertionError as err:
                  raise util.BeamAssertException(err)
  
          util.assert_that(result, check_result, label="result")
  
file_path: model-analysis/tensorflow_model_analysis/metrics/calibration_histogram_test.py
imports: import dataclasses
  import apache_beam
  from apache_beam.testing import util
  import numpy
  import tensorflow
  from tensorflow_model_analysis.eval_saved_model import testutil
  from tensorflow_model_analysis.metrics import calibration_histogram
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
label_desc: Use image node_id 2 for calling the CalibrationHistogramTest obj's underlying member method code with example usage: obj.testCalibrationHistogramWithK() without return types