code: def _flatten_proto(
      root_field: message.Message,
      field_name: str,
      index: int,
      include_empty_columns: bool = False,
  ) -> Iterator[Tuple[str, Any, int]]:
      """Generates the leaf primitive fields by traversing the proto recursively.
  
      Traverses a proto and emits a tuple of the name, value, index at which the
      value should be inserted. If include_empty_columns is True, unset fields are
      also emitted with value of None. The index is the order of which this
      primitive should be inserted to the DataFrame.
      Note: that nested or misaligned repeated fields are not supported and will
      lead to undefined behavior.
  
      Args:
        root_field: The root message field.
        field_name: The child field name under the root field where the traversal
          begins.
        index: The starting row index where the DataFrame was at.
        include_empty_columns: If True, the unset fields are also emitted.
  
      Returns:
        An iterator of the field name, field value, and index of the underlying
        proto primitives.
      """
  
      def _is_repeated_field(parent, field_name):
          return (
              parent.DESCRIPTOR.fields_by_name[field_name].label
              == descriptor.FieldDescriptor.LABEL_REPEATED
          )
  
      def _flatten_proto_in(
          parent: message.Message,
          field_name: str,
          field_value: Any,
          index: int,
          is_repeated_field: bool = False,
      ) -> Iterator[Tuple[str, Any, int]]:
          if isinstance(field_value, message.Message):
              # Test the message field is unset.
              if not is_repeated_field and not parent.HasField(
                  field_name
              ):
                  if include_empty_columns:
                      yield (field_name, None, index)
                  return
              elif isinstance(field_value, _WRAPPED_PRIMITIVES):
                  # Preserve the field_name of the wrapped primitives.
                  yield (field_name, field_value.value, index)
              else:
                  for field in field_value.DESCRIPTOR.fields:
                      yield from _flatten_proto_in(
                          field_value,
                          field.name,
                          getattr(field_value, field.name),
                          index,
                      )
          # Handling repeated field.
          elif _is_repeated_field(parent, field_name):
              for i, single_field_value in enumerate(field_value):
                  yield from _flatten_proto_in(
                      parent,
                      field_name,
                      single_field_value,
                      index + i,
                      is_repeated_field=True,
                  )
          # Python primitives.
          else:
              yield (field_name, field_value, index)
  
      field_value = getattr(root_field, field_name)
      return _flatten_proto_in(
          root_field, field_name, field_value, index
      )
  
imports: import collections
  import dataclasses
  import itertools
  from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union
  import numpy
  import pandas
  from tensorflow_model_analysis.proto import metrics_for_slice_pb2
  from google.protobuf import wrappers_pb2
  from google.protobuf import descriptor
  from google.protobuf import message
label_desc: Use image node_id 1 for calling a global function with example usage: _flatten_proto(root_field, field_name, index, include_empty_columns) and returns: _flatten_proto_in, unknown