code: def get_example_encoder(spec, compress_image=False, image_quality=95):
      """Get example encoder function for the given spec.
  
      Given a spec, returns an example encoder function. The example encoder
      function takes a nest of np.array feature values as input and returns a
      TF Example proto.
  
      Example:
        spec = {
            'lidar': array_spec.ArraySpec((900,), np.float32),
            'joint_positions': {
                'arm': array_spec.ArraySpec((7,), np.float32),
                'hand': array_spec.BoundedArraySpec((3, 3), np.int32, -1, 1)
            },
        }
  
        example_encoder = get_example_encoder(spec)
        serialized = example_encoder({
            'lidar': np.zeros((900,), np.float32),
            'joint_positions': {
                'arm': np.array([0.0, 1.57, 0.707, 0.2, 0.0, -1.57, 0.0],
                                np.float32),
                'hand': np.ones((3, 3), np.int32)
            },
        })
  
      The returned example encoder function requires that the feature nest passed
      has the shape and exact dtype specified in the spec. For example, it is
      an error to pass an array with np.float64 dtype where np.float32 is expected.
  
      Args:
        spec: list/tuple/nest of ArraySpecs describing a single example.
        compress_image: Whether to compress image. It is assumed that any uint8
          tensor of rank 3 with shape (w,h,c) is an image.
        image_quality: An optional int. Defaults to 95. Quality of the compression
          from 0 to 100 (higher is better and slower).
  
      Returns:
        Function
  
        ```python
        encoder(features_nest of np.arrays) -> tf.train.Example
        ```
      """
      # pylint: disable=g-complex-comprehension
      feature_encoders = [
          (
              path,
              _get_feature_encoder(
                  spec.shape, spec.dtype, compress_image, image_quality
              ),
          )
          for (path, spec) in nest_utils.flatten_with_joined_paths(spec)
      ]
  
      # pylint: enable=g-complex-comprehension
  
      def _example_encoder(features_nest):
          flat_features = tf.nest.flatten(features_nest)
          if len(flat_features) != len(feature_encoders):
              raise ValueError(
                  "Encoding failed: The number of items to encode does not match the "
                  "number of encoders generated. Num features %d, num_encoders:%d"
                  % (len(flat_features), len(feature_encoders))
              )
  
          feature_dict = {
              path: feature_encoder(feature)
              for feature, (path, feature_encoder) in zip(
                  flat_features, feature_encoders
              )
          }
          return tf.train.Example(
              features=tf.train.Features(feature=feature_dict)
          )
  
      return _example_encoder
  
file_path: agents/tf_agents/utils/example_encoding.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from collections import abc
  import io
  import gin
  import numpy
  from PIL import Image
  import tensorflow
  from tf_agents.utils import nest_utils
label_desc: Use image node_id 1 for calling a global function with example usage: get_example_encoder(spec, compress_image, image_quality) and returns: _example_encoder, tf