code: def convert_metric_value_to_proto(
      value: types.MetricValueType,
  ) -> metrics_for_slice_pb2.MetricValue:
      """Converts a MetricValueType into its proto format."""
      if isinstance(value, types.StructuredMetricValue):
          return value.to_proto()
  
      result = metrics_for_slice_pb2.MetricValue()
      if isinstance(value, bytes):
          # Convert textual types to string metrics.
          result.bytes_value = value
      elif isinstance(value, str):
          # Convert textual types to string metrics.
          result.bytes_value = value.encode("utf8")
      elif isinstance(value, np.ndarray):
          # Convert NumPy arrays to ArrayValue.
          result.array_value.CopyFrom(_convert_to_array_value(value))
      else:
          # We try to convert to float values.
          try:
              result.double_value.value = float(value)
          except (TypeError, ValueError) as e:
              result.unknown_type.value = str(value).encode("utf8")
              result.unknown_type.error = str(e)
      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 9 for calling a global function with example usage: convert_metric_value_to_proto(value) and returns: result, value