code: def convert_metrics_proto_to_dict(
      metrics_for_slice: metrics_for_slice_pb2.MetricsForSlice,
      model_name: Optional[str] = None,
  ) -> Optional[
      Tuple[
          slicer.SliceKeyOrCrossSliceKeyType,
          Optional[view_types.MetricsByOutputName],
      ]
  ]:
      """Converts metrics proto to dict."""
      model_metrics_map = {}
      if metrics_for_slice.metrics:
          model_metrics_map[""] = {
              "": {
                  "": _convert_proto_map_to_dict(
                      metrics_for_slice.metrics
                  )
              }
          }
  
      default_model_name = None
      if metrics_for_slice.metric_keys_and_values:
          for kv in metrics_for_slice.metric_keys_and_values:
              current_model_name = kv.key.model_name
              if current_model_name not in model_metrics_map:
                  model_metrics_map[current_model_name] = {}
              output_name = kv.key.output_name
              if (
                  output_name
                  not in model_metrics_map[current_model_name]
              ):
                  model_metrics_map[current_model_name][
                      output_name
                  ] = {}
  
              sub_key_metrics_map = model_metrics_map[
                  current_model_name
              ][output_name]
              sub_key_id = (
                  str(metric_types.SubKey.from_proto(kv.key.sub_key))
                  if kv.key.HasField("sub_key")
                  else ""
              )
              if sub_key_id not in sub_key_metrics_map:
                  sub_key_metrics_map[sub_key_id] = {}
              if kv.key.is_diff:
                  if default_model_name is None:
                      default_model_name = current_model_name
                  elif default_model_name != current_model_name:
                      # Setting '' to trigger no match found ValueError below.
                      default_model_name = ""
                  metric_name = "{}_diff".format(kv.key.name)
              elif kv.key.HasField("aggregation_type"):
                  metric_name = "{}_{}".format(
                      kv.key.aggregation_type, kv.key.name
                  )
              else:
                  metric_name = kv.key.name
              sub_key_metrics_map[sub_key_id][
                  metric_name
              ] = json_format.MessageToDict(
                  _get_metric_value_with_ci(
                      kv.value, kv.confidence_interval
                  )
              )
  
      metrics_map = None
      keys = list(model_metrics_map)
      tmp_model_name = model_name or default_model_name
      if tmp_model_name in model_metrics_map:
          # Use the provided model name if there is a match.
          metrics_map = model_metrics_map[tmp_model_name]
          # Add model-independent (e.g. example_count) metrics to all models.
          if tmp_model_name and "" in model_metrics_map:
              for output_name, output_dict in model_metrics_map[
                  ""
              ].items():
                  for sub_key_id, sub_key_dict in output_dict.items():
                      for name, value in sub_key_dict.items():
                          metrics_map.setdefault(
                              output_name, {}
                          ).setdefault(sub_key_id, {})[name] = value
      elif not tmp_model_name and len(keys) == 1:
          # Show result of the only model if no model name is specified.
          metrics_map = model_metrics_map[keys[0]]
      elif keys:
          # No match found.
          logging.warning(
              "Fail to find metrics for model name: %s . "
              "Available model names are [%s]",
              model_name,
              ", ".join(keys),
          )
          return None
  
      slice_key = None
      if metrics_for_slice.HasField("cross_slice_key"):
          slice_key = slicer.deserialize_cross_slice_key(
              metrics_for_slice.cross_slice_key
          )
      else:
          slice_key = slicer.deserialize_slice_key(
              metrics_for_slice.slice_key
          )
      return (slice_key, metrics_map)
  
imports: import json
  import os
  from typing import Any, Dict, List, Optional, Tuple, Union
  from absl import logging
  from tensorflow_model_analysis.metrics import example_count
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import weighted_example_count
  from tensorflow_model_analysis.post_export_metrics import metric_keys
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.proto import metrics_for_slice_pb2
  from tensorflow_model_analysis.slicer import slicer_lib
  from tensorflow_model_analysis.view import view_types
  from google.protobuf import json_format
label_desc: Use image node_id 10 for calling a global function with example usage: convert_metrics_proto_to_dict(metrics_for_slice, model_name) and returns: slice_key,  metrics_map, None