code: def stack_sequence_fields(
      sequence: Sequence[ElementType],
  ) -> ElementType:
      """Stacks a list of identically nested objects.
  
      This takes a sequence of identically nested objects and returns a single
      nested object whose ith leaf is a stacked numpy array of the corresponding
      ith leaf from each element of the sequence.
  
      For example, if `sequence` is:
  
      ```python
      [{
            'action': np.array([1.0]),
            'observation': (np.array([0.0, 1.0, 2.0]),),
            'reward': 1.0
       }, {
            'action': np.array([0.5]),
            'observation': (np.array([1.0, 2.0, 3.0]),),
            'reward': 0.0
       }, {
            'action': np.array([0.3]),1
            'observation': (np.array([2.0, 3.0, 4.0]),),
            'reward': 0.5
       }]
      ```
  
      Then this function will return:
  
      ```python
      {
          'action': np.array([....])         # array shape = [3 x 1]
          'observation': (np.array([...]),)  # array shape = [3 x 3]
          'reward': np.array([...])          # array shape = [3]
      }
      ```
  
      Note that the 'observation' entry in the above example has two levels of
      nesting, i.e it is a tuple of arrays.
  
      Args:
        sequence: a list of identically nested objects.
  
      Returns:
        A nested object with numpy.
  
      Raises:
        ValueError: If `sequence` is an empty sequence.
      """
      # Handle empty input sequences.
      if not sequence:
          raise ValueError("Input sequence must not be empty")
  
      # Default to asarray when arrays don't have the same shape to be compatible
      # with old behaviour.
      try:
          return fast_map_structure(
              lambda *values: np.stack(values), *sequence
          )
      except ValueError:
          return fast_map_structure(
              lambda *values: np.asarray(values, dtype=object),
              *sequence,
          )
  
file_path: acme/acme/utils/tree_utils.py
imports: from typing import Sequence, List, TypeVar, Any
  import numpy
  import tree
label_desc: Use image node_id 3 for calling a global function with example usage: stack_sequence_fields(sequence) and returns: fast_map_structure, fast_map_structure