code: def _filter_duplicate_metrics(
      metrics: Dict[str, List[tf.keras.metrics.Metric]],
      model_name: str,
      sub_key: Optional[metric_types.SubKey] = None,
  ) -> Dict[str, List[tf.keras.metrics.Metric]]:
      """Filters duplicate metrics from the metrics."""
      for output_name, metrics_list in metrics.items():
          unique_metrics = {}
          for metric in metrics_list:
              key = metric_types.MetricKey(
                  name=metric.name,
                  model_name=model_name,
                  output_name=output_name,
                  sub_key=_verify_and_update_sub_key(
                      model_name, output_name, sub_key, metric
                  ),
              )
              # Replace any previous metric (i.e. last added metric wins).
              unique_metrics[key] = metric
          metrics[output_name] = list(unique_metrics.values())
      return metrics
  
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 2 for calling a global function with example usage: _filter_duplicate_metrics(metrics, model_name, sub_key) and returns: metrics