code: def _metric_keys(
      metrics: Iterable[tf.keras.metrics.Metric],
      model_name: str,
      output_names: Iterable[str],
  ) -> List[metric_types.MetricKey]:
      """Returns metric keys for given metrics."""
      # We need to use the metric name to determine the associated output because
      # keras does not provide an API (see b/149780822). Keras names its metrics
      # using the following format:
      #   <output_name>_[weighted]_<metric_name>
      result = []
      for metric in metrics:
          sub_key = None
          if (
              hasattr(metric, "class_id")
              and metric.class_id is not None
          ):
              sub_key = metric_types.SubKey(class_id=metric.class_id)
          elif hasattr(metric, "top_k") and metric.top_k is not None:
              sub_key = metric_types.SubKey(top_k=metric.top_k)
          for output_name in output_names or []:
              if metric.name.startswith(output_name + "_"):
                  # TODO(b/171559113): Output prefixes used to be added multiple times.
                  # Remove this while loop after the last TF version with the issue is
                  # no longer supported.
                  name = metric.name
                  while name.startswith(output_name + "_"):
                      name = name[len(output_name) + 1 :]
                  result.append(
                      metric_types.MetricKey(
                          name=name,
                          model_name=model_name,
                          output_name=output_name,
                          sub_key=sub_key,
                          example_weighted=None,
                      )
                  )
                  break
          else:
              result.append(
                  metric_types.MetricKey(
                      name=metric.name,
                      model_name=model_name,
                      sub_key=sub_key,
                      example_weighted=None,
                  )
              )
      return result
  
imports: import collections
  from itertools import chain
  from typing import Dict, Iterable, List, Optional
  import apache_beam
  import numpy
  import tensorflow
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.api import types
  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
  from tensorflow_model_analysis.utils import util
label_desc: Use image node_id 2 for calling a global function with example usage: _metric_keys(metrics, model_name, output_names) and returns: result