code: def metric_thresholds_from_metrics_specs(
      metrics_specs: Iterable[config_pb2.MetricsSpec],
      eval_config: Optional[config_pb2.EvalConfig] = None,
  ) -> Dict[metric_types.MetricKey, Iterable[_SliceAndThreshold]]:
      """Returns thresholds associated with given metrics specs."""
      if eval_config is None:
          eval_config = config_pb2.EvalConfig()
      result = collections.defaultdict(list)
      existing = collections.defaultdict(dict)
  
      def add_if_not_exists(
          key: metric_types.MetricKey,
          slice_spec: Optional[
              Union[config_pb2.SlicingSpec, config_pb2.CrossSlicingSpec]
          ],
          threshold: Union[
              config_pb2.GenericChangeThreshold,
              config_pb2.GenericValueThreshold,
          ],
      ):
          """Adds value to results if it doesn't already exist."""
          hashable_slice_spec = None
          if slice_spec:
              hashable_slice_spec = slicer.deserialize_slice_spec(
                  slice_spec
              )
          # Note that hashing by SerializeToString() is only safe if used within the
          # same process.
          threshold_hash = threshold.SerializeToString()
          if not (
              key in existing
              and hashable_slice_spec in existing[key]
              and threshold_hash in existing[key][hashable_slice_spec]
          ):
              if hashable_slice_spec not in existing[key]:
                  existing[key][hashable_slice_spec] = {}
              existing[key][hashable_slice_spec][threshold_hash] = True
              result[key].append((slice_spec, threshold))
  
      def add_threshold(
          key: metric_types.MetricKey,
          slice_spec: Union[
              Optional[config_pb2.SlicingSpec],
              Optional[config_pb2.CrossSlicingSpec],
          ],
          threshold: config_pb2.MetricThreshold,
      ):
          """Adds thresholds to results."""
          if threshold.HasField("value_threshold"):
              add_if_not_exists(
                  key, slice_spec, threshold.value_threshold
              )
          if threshold.HasField("change_threshold"):
              key = key.make_diff_key()
              add_if_not_exists(
                  key, slice_spec, threshold.change_threshold
              )
  
      for spec in metrics_specs:
          for aggregation_type, sub_keys in _create_sub_keys(
              spec
          ).items():
              # Add thresholds for metrics computed in-graph.
              for metric_name, threshold in spec.thresholds.items():
                  for key in _keys_for_metric(
                      metric_name,
                      spec,
                      aggregation_type,
                      sub_keys,
                      [None],
                  ):
                      add_threshold(key, None, threshold)
              for (
                  metric_name,
                  per_slice_thresholds,
              ) in spec.per_slice_thresholds.items():
                  for key in _keys_for_metric(
                      metric_name,
                      spec,
                      aggregation_type,
                      sub_keys,
                      [None],
                  ):
                      for (
                          per_slice_threshold
                      ) in per_slice_thresholds.thresholds:
                          for (
                              slice_spec
                          ) in per_slice_threshold.slicing_specs:
                              add_threshold(
                                  key,
                                  slice_spec,
                                  per_slice_threshold.threshold,
                              )
              for (
                  metric_name,
                  cross_slice_thresholds,
              ) in spec.cross_slice_thresholds.items():
                  for key in _keys_for_metric(
                      metric_name,
                      spec,
                      aggregation_type,
                      sub_keys,
                      [None],
                  ):
                      for (
                          cross_slice_threshold
                      ) in cross_slice_thresholds.thresholds:
                          for (
                              cross_slice_spec
                          ) in (
                              cross_slice_threshold.cross_slicing_specs
                          ):
                              add_threshold(
                                  key,
                                  cross_slice_spec,
                                  cross_slice_threshold.threshold,
                              )
  
      # Add thresholds for post export metrics defined in MetricConfigs.
      for key, metric_config, _ in keys_and_metrics_from_specs(
          eval_config, metrics_specs
      ):
          if metric_config.HasField("threshold"):
              add_threshold(key, None, metric_config.threshold)
          for per_slice_threshold in metric_config.per_slice_thresholds:
              for slice_spec in per_slice_threshold.slicing_specs:
                  add_threshold(
                      key, slice_spec, per_slice_threshold.threshold
                  )
          for (
              cross_slice_threshold
          ) in metric_config.cross_slice_thresholds:
              for (
                  cross_slice_spec
              ) in cross_slice_threshold.cross_slicing_specs:
                  add_threshold(
                      key,
                      cross_slice_spec,
                      cross_slice_threshold.threshold,
                  )
  
      return result
  
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 13 for calling a global function with example usage: metric_thresholds_from_metrics_specs(metrics_specs, eval_config) and returns: result