code: def get_plot_data_and_config(
      results: List[Tuple[slicer.SliceKeyType, Dict[str, Any]]],
      slicing_spec: slicer.SingleSliceSpec,
      output_name: Optional[str] = None,
      class_id: Optional[int] = None,
      top_k: Optional[int] = None,
      k: Optional[int] = None,
      label: Optional[str] = None,
  ) -> Tuple[
      Union[Dict[str, Any], str],
      Dict[str, Union[Dict[str, Dict[str, str]], str]],
  ]:
      """Util function that extracts plot for a particular slice from the results.
  
      Args:
        results: A list of records. Each record is a tuple of (slice_name,
          {metric_name, metric_value}).
        slicing_spec: The slicer.SingleSliceSpec to identify the slice to fetch plot
          for.
        output_name: The name of the output.
        class_id: An int representing the class id if model is multi-class.
        top_k: The k used to compute prediction in the top k position.
        k: The k used to compute prediciton at the kth position.
        label: A partial label used to match a set of plots in the results. This is
          kept for backward compatibility.
  
      Returns:
        (plot_data, plot_config) for the specified slice.
  
        Note that plot_data should be of type Dict[Text, Any]. However, PyType
        can't figure it out. As a result, the annotation has to be
        Union[Dict[Text, Any], Text].
  
      Raises:
        ValueError: The provided slicing_column does not exist in results or more
        than one result is found; or there is not plot data available; or there are
        multiple sets of plot while label is not provided; or label matches to more
        than one set of plots; or label does not match any set of plots.
      """
      if label is not None and (
          output_name is not None
          or class_id is not None
          or top_k is not None
          or k is not None
      ):
          # Plot key (specified by output_name and class_id / top k / k) and label (
          # for backward compatibiility only) should not be provided together.
          raise ValueError(
              "Do not specify both label and output_name / class_id"
          )
  
      sub_key_oneof_check = 0
      sub_key_id = None
  
      if class_id is not None:
          sub_key_oneof_check = sub_key_oneof_check + 1
          sub_key_id = "classId:" + str(class_id)
      if top_k is not None:
          sub_key_oneof_check = sub_key_oneof_check + 1
          sub_key_id = "topK:" + str(top_k)
      if k is not None:
          sub_key_oneof_check = sub_key_oneof_check + 1
          sub_key_id = "k:" + str(k)
      if sub_key_oneof_check > 1:
          raise ValueError(
              "Up to one of class_id, top_k and k can be provided."
          )
  
      output_name = "" if output_name is None else output_name
  
      matching_slices = find_all_slices(results, slicing_spec)
      count = len(matching_slices)
  
      if count == 0:
          raise ValueError("No slice matching slicing spec is found.")
      elif count > 1:
          raise ValueError(
              "More than one slice matching slicing spec is found."
          )
  
      target_slice = matching_slices[0]
  
      plot_config = {
          "sliceName": target_slice["slice"],
          "metricKeys": _SUPPORTED_PLOT_KEYS,
      }
  
      if output_name not in target_slice["metrics"]:
          if output_name:
              raise ValueError(
                  "No plot data found for output name %s." % output_name
              )
          else:
              raise ValueError(
                  "No plot data found without output name."
              )
  
      output = target_slice["metrics"][output_name]
      class_id_to_use = sub_key_id if sub_key_id is not None else ""
  
      if class_id_to_use not in output:
          if class_id is None:
              raise ValueError(
                  "No plot data found for output name %s with no class id."
                  % output_name
              )
          else:
              raise ValueError(
                  "No plot data found for output name %s with class id %d."
                  % (output_name, class_id)
              )
  
      plot_data = target_slice["metrics"][output_name][class_id_to_use]
  
      # Backward compatibility support for plot data stored in a map before the plot
      # key was introduced.
      if label is not None:
          plot_set = None
          for key in plot_data:
              if label in key:
                  if not plot_set:
                      plot_set = plot_data[key]
                  else:
                      raise ValueError(
                          "Label %s matches more than one key. Keys are %s. Please make it more specific."
                          % (
                              label,
                              ", ".join([key for key, _ in plot_data]),
                          )
                      )
  
          if not plot_set:
              raise ValueError(
                  "Label %s does not match any key. Keys are %s. Please provide a new one."
                  % (label, ", ".join([key for key, _ in plot_data]))
              )
          plot_data = plot_set
      else:
          # Make sure that we are not looking at actual plot data instead of a map of
          # plot.
          contains_supported_plot_data = False
          for key in plot_data:
              for _, plot in _SUPPORTED_PLOT_KEYS.items():
                  contains_supported_plot_data = (
                      contains_supported_plot_data
                      or plot["metricName"] == key
                  )
  
          if not contains_supported_plot_data:
              raise ValueError(
                  "No plot data found. Maybe provide a label? %s"
                  % json.dumps(plot_data)
              )
  
      plot_data = _replace_nan_with_none(
          plot_data, _SUPPORTED_PLOT_KEYS
      )
  
      return plot_data, plot_config
  
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 6 for calling a global function with example usage: get_plot_data_and_config(results, slicing_spec, output_name, class_id, top_k, k, label) and returns: plot_data,  plot_config