code: def to_computations(
      metrics_specs: List[config_pb2.MetricsSpec],
      eval_config: Optional[config_pb2.EvalConfig] = None,
      schema: Optional[schema_pb2.Schema] = None,
  ) -> metric_types.MetricComputations:
      """Returns computations associated with given metrics specs."""
      computations = []
  
      #
      # Split into TF metrics and TFMA metrics
      #
  
      # Dict[Text, Type[tf.keras.metrics.Metric]]
      tf_metric_classes = {}  # class_name -> class
      # Dict[Text, Type[tf.keras.losses.Loss]]
      tf_loss_classes = {}  # class_name -> class
      # List[metric_types.MetricsSpec]
      tf_metrics_specs = []
      # Dict[Text, Type[metric_types.Metric]]
      tfma_metric_classes = (
          metric_types.registered_metrics()
      )  # class_name -> class
      # List[metric_types.MetricsSpec]
      tfma_metrics_specs = []
      #
      # Note: Lists are used instead of Dicts for the following items because
      # protos are are no hashable.
      #
      # List[List[_TFOrTFMAMetricOrLoss]] (offsets align with metrics_specs).
      per_spec_metric_instances = []
      # List[List[MetricConfig]] (offsets align with metrics_specs).
      per_spec_metric_configs = []
      # List[List[_TFMetricOrLoss]] (offsets align with tf_metrics_specs).
      per_tf_spec_metric_instances = []
      # List[List[metric_types.Metric]]] (offsets align with tfma_metrics_specs).
      per_tfma_spec_metric_instances = []
      for spec in metrics_specs:
          tf_spec = config_pb2.MetricsSpec()
          tf_spec.CopyFrom(spec)
          del tf_spec.metrics[:]
          tfma_spec = config_pb2.MetricsSpec()
          tfma_spec.CopyFrom(spec)
          del tfma_spec.metrics[:]
          for metric in spec.metrics:
              if metric.class_name in tfma_metric_classes:
                  tfma_spec.metrics.append(metric)
              elif not metric.module:
                  tf_spec.metrics.append(metric)
              else:
                  cls = getattr(
                      importlib.import_module(metric.module),
                      metric.class_name,
                  )
                  if issubclass(cls, tf.keras.metrics.Metric):
                      tf_metric_classes[metric.class_name] = cls
                      tf_spec.metrics.append(metric)
                  elif issubclass(cls, tf.keras.losses.Loss):
                      tf_loss_classes[metric.class_name] = cls
                      tf_spec.metrics.append(metric)
                  else:
                      tfma_metric_classes[metric.class_name] = cls
                      tfma_spec.metrics.append(metric)
  
          metric_instances = []
          metric_configs = []
          if tf_spec.metrics:
              tf_metrics_specs.append(tf_spec)
              tf_metric_instances = []
              for m in tf_spec.metrics:
                  # To distinguish losses from metrics, losses are required to set the
                  # module name.
                  if m.module == _TF_LOSSES_MODULE:
                      tf_metric_instances.append(
                          _deserialize_tf_loss(m, tf_loss_classes)
                      )
                  else:
                      tf_metric_instances.append(
                          _deserialize_tf_metric(m, tf_metric_classes)
                      )
              per_tf_spec_metric_instances.append(tf_metric_instances)
              metric_instances.extend(tf_metric_instances)
              metric_configs.extend(tf_spec.metrics)
          if tfma_spec.metrics:
              tfma_metrics_specs.append(tfma_spec)
              tfma_metric_instances = [
                  _deserialize_tfma_metric(m, tfma_metric_classes)
                  for m in tfma_spec.metrics
              ]
              per_tfma_spec_metric_instances.append(
                  tfma_metric_instances
              )
              metric_instances.extend(tfma_metric_instances)
              metric_configs.extend(tfma_spec.metrics)
          per_spec_metric_instances.append(metric_instances)
          per_spec_metric_configs.append(metric_configs)
  
      # Process TF specs
      computations.extend(
          _process_tf_metrics_specs(
              tf_metrics_specs,
              per_tf_spec_metric_instances,
              eval_config,
          )
      )
  
      # Process TFMA specs
      computations.extend(
          _process_tfma_metrics_specs(
              tfma_metrics_specs,
              per_tfma_spec_metric_instances,
              eval_config,
              schema,
          )
      )
  
      # Process aggregation based metrics (output aggregation and macro averaging).
      # Note that processing of TF and TFMA specs were setup to create the binarized
      # metrics that macro averaging depends on.
      for i, spec in enumerate(metrics_specs):
          for example_weighted in _example_weight_options(
              eval_config, spec
          ):
              for aggregation_type, sub_keys in _create_sub_keys(
                  spec
              ).items():
                  output_names = spec.output_names or [""]
                  output_weights = dict(spec.output_weights)
                  if not set(output_weights).issubset(output_names):
                      raise ValueError(
                          "one or more output_names used in output_weights does not exist: "
                          "output_names={}, output_weights={}".format(
                              output_names, output_weights
                          )
                      )
                  for model_name in spec.model_names or [""]:
                      for sub_key in sub_keys:
                          for metric, _ in zip(
                              per_spec_metric_instances[i],
                              per_spec_metric_configs[i],
                          ):
                              if aggregation_type and (
                                  aggregation_type.macro_average
                                  or aggregation_type.weighted_macro_average
                              ):
                                  class_weights = (
                                      _class_weights(spec) or {}
                                  )
                                  for output_name in output_names:
                                      macro_average_sub_keys = (
                                          _macro_average_sub_keys(
                                              sub_key, class_weights
                                          )
                                      )
                                      if aggregation_type.macro_average:
                                          computations.extend(
                                              aggregation.macro_average(
                                                  metric.get_config()[
                                                      "name"
                                                  ],
                                                  sub_keys=macro_average_sub_keys,
                                                  eval_config=eval_config,
                                                  model_name=model_name,
                                                  output_name=output_name,
                                                  sub_key=sub_key,
                                                  class_weights=class_weights,
                                                  example_weighted=example_weighted,
                                              )
                                          )
                                      elif (
                                          aggregation_type.weighted_macro_average
                                      ):
                                          computations.extend(
                                              aggregation.weighted_macro_average(
                                                  metric.get_config()[
                                                      "name"
                                                  ],
                                                  sub_keys=macro_average_sub_keys,
                                                  eval_config=eval_config,
                                                  model_name=model_name,
                                                  output_name=output_name,
                                                  sub_key=sub_key,
                                                  class_weights=class_weights,
                                                  example_weighted=example_weighted,
                                              )
                                          )
                              if output_weights:
                                  computations.extend(
                                      aggregation.output_average(
                                          metric.get_config()["name"],
                                          output_weights=output_weights,
                                          eval_config=eval_config,
                                          model_name=model_name,
                                          sub_key=sub_key,
                                          example_weighted=example_weighted,
                                      )
                                  )
  
      return computations
  
imports: import collections
  import importlib
  import json
  import re
  from typing import Any, Dict, FrozenSet, Iterable, Iterator, List, NamedTuple, Optional, Tuple, Type, Union
  import tensorflow
  from tensorflow_model_analysis.metrics import aggregation
  from tensorflow_model_analysis.metrics import binary_confusion_matrices
  from tensorflow_model_analysis.metrics import calibration
  from tensorflow_model_analysis.metrics import calibration_plot
  from tensorflow_model_analysis.metrics import confusion_matrix_metrics
  from tensorflow_model_analysis.metrics import confusion_matrix_plot
  from tensorflow_model_analysis.metrics import example_count
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.metrics import multi_class_confusion_matrix_plot
  from tensorflow_model_analysis.metrics import tf_metric_wrapper
  from tensorflow_model_analysis.metrics import weighted_example_count
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.slicer import slicer_lib
  from tensorflow_model_analysis.utils import model_util
  from tensorflow_metadata.proto.v0 import schema_pb2
label_desc: Use image node_id 14 for calling a global function with example usage: to_computations(metrics_specs, eval_config, schema) and returns: computations