code: def tf_metric_computations(
      metrics: Union[
          List[_TFMetricOrLoss], Dict[str, List[_TFMetricOrLoss]]
      ],
      eval_config: Optional[config_pb2.EvalConfig] = None,
      model_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,
      desired_batch_size: Optional[int] = None,
  ) -> metric_types.MetricComputations:
      """Returns metric computations for the given TF metrics.
  
      Note that there is no requirement that a one to one mapping exist between the
      input metrics and the output metric computations. The implementation may
      combine multiple metrics into a single computation for efficency.
  
      Args:
        metrics: Dict from metric name to tf.keras.metrics.Metric or
          tf.keras.metrics.Loss. For multi-output models a dict of dicts may be
          passed where the first dict is indexed by the output_name.
        eval_config: Eval config.
        model_name: Optional model name (if multi-model evaluation).
        sub_key: Optional sub key.
        aggregation_type: Optional aggregation type.
        class_weights: Optional class weights to apply to multi-class / multi-label
          labels and predictions. This should only be used when the aggregation_type
          is set.
        example_weighted: True if example weights should be applied.
        desired_batch_size: Batch size to use when calling TF metrics (testing
          only).
  
      Returns:
        Metric computations.
      """
      if not isinstance(metrics, dict):
          metrics = {"": metrics}
  
      if aggregation_type is not None:
          sparse_metrics = _sparse_metrics(metrics)
          if sparse_metrics:
              raise ValueError(
                  "sparse metrics cannot be used with aggregation options. Either "
                  "disable aggregation settings or replace the sparse metrics with"
                  "non-sparse versions: {}".format(sparse_metrics)
              )
  
      metrics = _filter_duplicate_metrics(metrics, model_name, sub_key)
  
      computations = []
  
      # For efficency, metrics are separated into confusion matrix based vs
      # non-confusion matrix based metrics. Since the confusion matrix based metrics
      # can all be calculated from the calibration histogram, these metrics are
      # computed separately as derived metrics. The remaining non-confusion matrix
      # metrics are calculated using batches of predictions/labels in eager mode
      # (possibly with additional pre-processing of the values to perform
      # binarization, etc).
      #
      # Note that in theory if a model was provided, all the metrics could be
      # calculated by calling model.evaluate(). However, this call is inefficient
      # for confusion matrix based metrics given the large number of weights that
      # need to be calculated and the overlapping computations between the metrics.
      # In addition, some metrics and plots are only defined in TFMA so a separate
      # evaluation step would still be required. Lastly, if the metrics have any
      # binarization, etc applied the inputs and outputs will not match those
      # expected by the model. For these reasons, a separate implementation is used
      # for each specific use case. It also allows evaluations that are not
      # associated with a model (i.e. raw predictions are passed as input) to share
      # the same code path as model based evaluations where possible.
      (
          confusion_matrix_metrics,
          non_confusion_matrix_metrics,
      ) = _separate_confusion_matrix_metrics(metrics)
  
      for output_name, metrics in confusion_matrix_metrics.items():
          for metric in metrics:
              computations.extend(
                  _wrap_confusion_matrix_metric(
                      metric,
                      eval_config,
                      model_name,
                      output_name,
                      sub_key,
                      aggregation_type,
                      class_weights,
                      example_weighted,
                  )
              )
  
      if non_confusion_matrix_metrics:
          custom_objects = _custom_objects(non_confusion_matrix_metrics)
          (
              metric_keys,
              metric_configs,
              loss_configs,
          ) = _metric_keys_and_configs(
              non_confusion_matrix_metrics,
              model_name,
              sub_key,
              aggregation_type,
              example_weighted,
          )
          for sub_key, keys in metric_keys.items():
              computations.append(
                  metric_types.MetricComputation(
                      keys=keys,
                      preprocessors=None,
                      combiner=_CompilableMetricsCombiner(
                          metric_configs[sub_key],
                          loss_configs[sub_key],
                          custom_objects,
                          eval_config,
                          model_name,
                          sub_key,
                          aggregation_type,
                          class_weights,
                          example_weighted,
                          desired_batch_size,
                      ),
                  )
              )
  
      return computations
  
imports: import collections
  import importlib
  import itertools
  from typing import Any, Dict, Iterable, List, Optional, Type, Tuple, Union
  import apache_beam
  import numpy
  import tensorflow
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.metrics import binary_confusion_matrices
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.metrics import tf_metric_accumulators
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.utils import model_util
label_desc: Use image node_id 1 for calling a global function with example usage: tf_metric_computations(metrics, eval_config, model_name, sub_key, aggregation_type, class_weights, example_weighted, desired_batch_size) and returns: computations