code: def _calibration(
      name: str = CALIBRATION_NAME,
      eval_config: Optional[config_pb2.EvalConfig] = None,
      model_name: str = "",
      output_name: str = "",
      sub_key: Optional[metric_types.SubKey] = None,
      aggregation_type: Optional[metric_types.AggregationType] = None,
      class_weights: Optional[Dict[int, float]] = None,
      example_weighted: bool = False,
  ) -> metric_types.MetricComputations:
      """Returns metric computations for calibration."""
      key = metric_types.MetricKey(
          name=name,
          model_name=model_name,
          output_name=output_name,
          sub_key=sub_key,
          example_weighted=example_weighted,
      )
  
      # Make sure weighted_labels_predictions_examples are calculated.
      computations = _weighted_labels_predictions_examples(
          eval_config=eval_config,
          model_name=model_name,
          output_name=output_name,
          sub_key=sub_key,
          aggregation_type=aggregation_type,
          class_weights=class_weights,
          example_weighted=example_weighted,
      )
      weighted_labels_predictions_key = computations[-1].keys[-1]
  
      def result(
          metrics: Dict[metric_types.MetricKey, Any]
      ) -> Dict[metric_types.MetricKey, Any]:
          """Returns calibration."""
          metric = metrics[weighted_labels_predictions_key]
          if np.isclose(metric.total_weighted_labels, 0.0):
              value = float("nan")
          else:
              value = (
                  metric.total_weighted_predictions
                  / metric.total_weighted_labels
              )
  
          return {key: value}
  
      derived_computation = metric_types.DerivedMetricComputation(
          keys=[key], result=result
      )
      computations.append(derived_computation)
      return computations
  
imports: from typing import Any, Dict, Iterable, Optional
  import apache_beam
  import numpy
  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 3 for calling a global function with example usage: _calibration(name, eval_config, model_name, output_name, sub_key, aggregation_type, class_weights, example_weighted) and returns: computations, dict