code: def convert_slice_plots_to_proto(
      plots: Tuple[slicer.SliceKeyOrCrossSliceKeyType, Dict[Any, Any]],
      add_metrics_callbacks: Optional[
          List[types.AddMetricsCallbackType]
      ],
  ) -> metrics_for_slice_pb2.PlotsForSlice:
      """Converts the given slice plots into PlotsForSlice proto.
  
      Args:
        plots: The slice plots.
        add_metrics_callbacks: A list of metric callbacks. This should be the same
          list as the one passed to tfma.Evaluate().
  
      Returns:
        The PlotsForSlice proto.
      """
      result = metrics_for_slice_pb2.PlotsForSlice()
      slice_key, slice_plots = plots
  
      if slicer.is_cross_slice_key(slice_key):
          result.cross_slice_key.CopyFrom(
              slicer.serialize_cross_slice_key(slice_key)
          )
      else:
          result.slice_key.CopyFrom(
              slicer.serialize_slice_key(slice_key)
          )
  
      slice_plots = slice_plots.copy()
  
      if metric_keys.ERROR_METRIC in slice_plots:
          logging.warning(
              "Error for slice: %s with error message: %s ",
              slice_key,
              slice_plots[metric_keys.ERROR_METRIC],
          )
          error_metric = slice_plots.pop(metric_keys.ERROR_METRIC)
          result.plots[
              metric_keys.ERROR_METRIC
          ].debug_message = error_metric
          return result
  
      if add_metrics_callbacks and (
          not any(
              isinstance(k, metric_types.MetricKey) for k in slice_plots
          )
      ):
          for add_metrics_callback in add_metrics_callbacks:
              if hasattr(
                  add_metrics_callback, "populate_plots_and_pop"
              ):
                  add_metrics_callback.populate_plots_and_pop(
                      slice_plots, result.plots
                  )
      plots_by_key = {}
      for key in sorted(slice_plots):
          value = slice_plots[key]
          # Remove plot name from key, if the name is one of the default plots.
          # This is for the backward compatibility where there was no name for plots.
          # (multiple plots are combined into a single proto).
          if isinstance(key, metric_types.MetricKey) and key.name in [
              confusion_matrix_plot.CONFUSION_MATRIX_PLOT_NAME,
              calibration_plot.CALIBRATION_PLOT_NAME,
          ]:
              parent_key = key._replace(name=None)
          # If the user specifies the name, plots are not going to merge.
          else:
              parent_key = key
          if parent_key not in plots_by_key:
              key_and_value = result.plot_keys_and_values.add()
              key_and_value.key.CopyFrom(parent_key.to_proto())
              plots_by_key[parent_key] = key_and_value.value
  
          if isinstance(
              value, metrics_for_slice_pb2.CalibrationHistogramBuckets
          ):
              plots_by_key[
                  parent_key
              ].calibration_histogram_buckets.CopyFrom(value)
              slice_plots.pop(key)
          elif isinstance(
              value, metrics_for_slice_pb2.ConfusionMatrixAtThresholds
          ):
              plots_by_key[
                  parent_key
              ].confusion_matrix_at_thresholds.CopyFrom(value)
              slice_plots.pop(key)
          elif isinstance(
              value,
              metrics_for_slice_pb2.MultiClassConfusionMatrixAtThresholds,
          ):
              plots_by_key[
                  parent_key
              ].multi_class_confusion_matrix_at_thresholds.CopyFrom(
                  value
              )
              slice_plots.pop(key)
          elif isinstance(
              value,
              metrics_for_slice_pb2.MultiLabelConfusionMatrixAtThresholds,
          ):
              plots_by_key[
                  parent_key
              ].multi_label_confusion_matrix_at_thresholds.CopyFrom(
                  value
              )
              slice_plots.pop(key)
  
      if slice_plots:
          if add_metrics_callbacks is None:
              add_metrics_callbacks = []
          raise NotImplementedError(
              "some plots were not converted or popped. keys: %s. "
              "add_metrics_callbacks were: %s"
              % (
                  slice_plots.keys(),
                  [
                      x.name
                      for x in add_metrics_callbacks  # pytype: disable=attribute-error
                  ],
              )
          )
  
      return result
  
imports: import itertools
  import os
  from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union
  from absl import logging
  import apache_beam
  import numpy
  import pyarrow
  import tensorflow
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.api import types
  from tensorflow_model_analysis.evaluators import evaluator
  from tensorflow_model_analysis.evaluators import metrics_validator
  from tensorflow_model_analysis.metrics import calibration_plot
  from tensorflow_model_analysis.metrics import confusion_matrix_plot
  from tensorflow_model_analysis.metrics import metric_specs
  from tensorflow_model_analysis.metrics import metric_types
  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.proto import validation_result_pb2
  from tensorflow_model_analysis.slicer import slicer_lib
  from tensorflow_model_analysis.utils import math_util
  from tensorflow_model_analysis.writers import writer
  from google.protobuf import wrappers_pb2
label_desc: Use image node_id 11 for calling a global function with example usage: convert_slice_plots_to_proto(plots, add_metrics_callbacks) and returns: result, result