class_docstr: A tuple representing a batch of whole episodes.
  
  Data type for on-policy algorithms.
  
  A :class:`~EpisodeBatch` represents a batch of whole episodes, produced
  when one or more agents interacts with one or more environments.
  
  +-----------------------+-------------------------------------------------+
  | Symbol                | Description                                     |
  +=======================+=================================================+
  | :math:`N`             | Episode batch dimension                         |
  +-----------------------+-------------------------------------------------+
  | :math:`[T]`           | Variable-length time dimension of each          |
  |                       | episode                                         |
  +-----------------------+-------------------------------------------------+
  | :math:`S^*`           | Single-step shape of a time-series tensor       |
  +-----------------------+-------------------------------------------------+
  | :math:`N \bullet [T]` | A dimension computed by flattening a            |
  |                       | variable-length time dimension :math:`[T]` into |
  |                       | a single batch dimension with length            |
  |                       | :math:`sum_{i \in N} [T]_i`                     |
  +-----------------------+-------------------------------------------------+
  
  Attributes:
      env_spec (EnvSpec): Specification for the environment from
          which this data was sampled.
      episode_infos (dict[str, np.ndarray]): A dict of numpy arrays
          containing the episode-level information of each episode. Each
          value of this dict should be a numpy array of shape :math:`(N,
          S^*)`. For example, in goal-conditioned reinforcement learning this
          could contain the goal state for each episode.
      observations (numpy.ndarray): A numpy array of shape
          :math:`(N \bullet [T], O^*)` containing the (possibly
          multi-dimensional) observations for all time steps in this batch.
          These must conform to :obj:`EnvStep.observation_space`.
      last_observations (numpy.ndarray): A numpy array of shape
          :math:`(N, O^*)` containing the last observation of each episode.
          This is necessary since there are one more observations than
          actions every episode.
      actions (numpy.ndarray): A  numpy array of shape
          :math:`(N \bullet [T], A^*)` containing the (possibly
          multi-dimensional) actions for all time steps in this batch. These
          must conform to :obj:`EnvStep.action_space`.
      rewards (numpy.ndarray): A numpy array of shape
          :math:`(N \bullet [T])` containing the rewards for all time steps
          in this batch.
      env_infos (dict[str, np.ndarray]): A dict of numpy arrays arbitrary
          environment state information. Each value of this dict should be
          a numpy array of shape :math:`(N \bullet [T])` or :math:`(N \bullet
          [T], S^*)`.
      agent_infos (dict[str, np.ndarray]): A dict of numpy arrays arbitrary
          agent state information. Each value of this dict should be a numpy
          array of shape :math:`(N \bullet [T])` or :math:`(N \bullet [T],
          S^*)`.  For example, this may contain the hidden states from an RNN
          policy.
      step_types (numpy.ndarray): A numpy array of `StepType with shape
          :math:`(N \bullet [T])` containing the time step types for all
          transitions in this batch.
      lengths (numpy.ndarray): An integer numpy array of shape :math:`(N,)`
          containing the length of each episode in this batch. This may be
          used to reconstruct the individual episodes.
  
  Raises:
      ValueError: If any of the above attributes do not conform to their
          prescribed types and shapes.
class_docstr_tok: ["A","tuple","representing","a","batch","of","whole","episodes",".","Data","type","for","on-policy","algorithms",".","A",":","class",":","`","~EpisodeBatch","`","represents","a","batch","of","whole","episodes",",","produced","when","one","or","more","agents","interacts","with","one","or","more","environments",".","+","--","--","--","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","-+","|","Symbol","|","Description","|","+=======================+=================================================+","|",":","math",":","`","N","`","|","Episode","batch","dimension","|","+","--","--","--","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","-+","|",":","math",":","`","[","T","]","`","|","Variable-length","time","dimension","of","each","|","|","|","episode","|","+","--","--","--","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","-+","|",":","math",":","`","S^","*","`","|","Single-step","shape","of","a","time-series","tensor","|","+","--","--","--","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","-+","|",":","math",":","`","N","\\bullet","[","T","]","`","|","A","dimension","computed","by","flattening","a","|","|","|","variable-length","time","dimension",":","math",":","`","[","T","]","`","into","|","|","|","a","single","batch","dimension","with","length","|","|","|",":","math",":","`","sum_","{","i","\\in","N","}","[","T","]","_i","`","|","+","--","--","--","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","-+","Attributes",":","env_spec","(","EnvSpec",")",":","Specification","for","the","environment","from","which","this","data","was","sampled",".","episode_infos","(","dict","[","str",",","np.ndarray","]",")",":","A","dict","of","numpy","arrays","containing","the","episode-level","information","of","each","episode",".","Each","value","of","this","dict","should","be","a","numpy","array","of","shape",":","math",":","`","(","N",",","S^","*",")","`",".","For","example",",","in","goal-conditioned","reinforcement","learning","this","could","contain","the","goal","state","for","each","episode",".","observations","(","numpy.ndarray",")",":","A","numpy","array","of","shape",":","math",":","`","(","N","\\bullet","[","T","]",",","O^","*",")","`","containing","the","(","possibly","multi-dimensional",")","observations","for","all","time","steps","in","this","batch",".","These","must","conform","to",":","obj",":","`","EnvStep.observation_space","`",".","last_observations","(","numpy.ndarray",")",":","A","numpy","array","of","shape",":","math",":","`","(","N",",","O^","*",")","`","containing","the","last","observation","of","each","episode",".","This","is","necessary","since","there","are","one","more","observations","than","actions","every","episode",".","actions","(","numpy.ndarray",")",":","A","numpy","array","of","shape",":","math",":","`","(","N","\\bullet","[","T","]",",","A^","*",")","`","containing","the","(","possibly","multi-dimensional",")","actions","for","all","time","steps","in","this","batch",".","These","must","conform","to",":","obj",":","`","EnvStep.action_space","`",".","rewards","(","numpy.ndarray",")",":","A","numpy","array","of","shape",":","math",":","`","(","N","\\bullet","[","T","]",")","`","containing","the","rewards","for","all","time","steps","in","this","batch",".","env_infos","(","dict","[","str",",","np.ndarray","]",")",":","A","dict","of","numpy","arrays","arbitrary","environment","state","information",".","Each","value","of","this","dict","should","be","a","numpy","array","of","shape",":","math",":","`","(","N","\\bullet","[","T","]",")","`","or",":","math",":","`","(","N","\\bullet","[","T","]",",","S^","*",")","`",".","agent_infos","(","dict","[","str",",","np.ndarray","]",")",":","A","dict","of","numpy","arrays","arbitrary","agent","state","information",".","Each","value","of","this","dict","should","be","a","numpy","array","of","shape",":","math",":","`","(","N","\\bullet","[","T","]",")","`","or",":","math",":","`","(","N","\\bullet","[","T","]",",","S^","*",")","`",".","For","example",",","this","may","contain","the","hidden","states","from","an","RNN","policy",".","step_types","(","numpy.ndarray",")",":","A","numpy","array","of","`","StepType","with","shape",":","math",":","`","(","N","\\bullet","[","T","]",")","`","containing","the","time","step","types","for","all","transitions","in","this","batch",".","lengths","(","numpy.ndarray",")",":","An","integer","numpy","array","of","shape",":","math",":","`","(","N",",",")","`","containing","the","length","of","each","episode","in","this","batch",".","This","may","be","used","to","reconstruct","the","individual","episodes",".","Raises",":","ValueError",":","If","any","of","the","above","attributes","do","not","conform","to","their","prescribed","types","and","shapes","."]
docstr: Create a EpisodeBatch from a list of episodes.
  
  Args:
      env_spec (EnvSpec): Specification for the environment from which
          this data was sampled.
      paths (list[dict[str, np.ndarray or dict[str, np.ndarray]]]): Keys:
          * episode_infos (dict[str, np.ndarray]): Dictionary of stacked,
              non-flattened `episode_info` arrays, each of shape (S^*).
          * observations (np.ndarray): Non-flattened array of
              observations. Typically has shape (T, S^*) (the unflattened
              state space of the current environment). observations[i]
              was used by the agent to choose actions[i]. observations
              may instead have shape (T + 1, S^*).
          * next_observations (np.ndarray): Non-flattened array of
              observations. Has shape (T, S^*). next_observations[i] was
              observed by the agent after taking actions[i]. Optional.
              Note that to ensure all information from the environment
              was preserved, observations[i] must have shape (T + 1,
              S^*), or this key must be set. However, this method is
              lenient and will "duplicate" the last observation if the
              original last observation has been lost.
          * actions (np.ndarray): Non-flattened array of actions. Must
              have shape (T, S^*) (the unflattened action space of the
              current environment).
          * rewards (np.ndarray): Array of rewards of shape (T,) (1D
              array of length timesteps).
          * agent_infos (dict[str, np.ndarray]): Dictionary of stacked,
              non-flattened `agent_info` arrays.
          * env_infos (dict[str, np.ndarray]): Dictionary of stacked,
              non-flattened `env_info` arrays.
          * step_types (numpy.ndarray): A numpy array of `StepType with
              shape (T,) containing the time step types for all
              transitions in this batch.
docstr_tok: ["Create","a","EpisodeBatch","from","a","list","of","episodes",".","Args",":","env_spec","(","EnvSpec",")",":","Specification","for","the","environment","from","which","this","data","was","sampled",".","paths","(","list","[","dict","[","str",",","np.ndarray","or","dict","[","str",",","np.ndarray","]","]","]",")",":","Keys",":","*","episode_infos","(","dict","[","str",",","np.ndarray","]",")",":","Dictionary","of","stacked",",","non-flattened","`","episode_info","`","arrays",",","each","of","shape","(","S^","*",")",".","*","observations","(","np.ndarray",")",":","Non-flattened","array","of","observations",".","Typically","has","shape","(","T",",","S^","*",")","(","the","unflattened","state","space","of","the","current","environment",")",".","observations","[","i","]","was","used","by","the","agent","to","choose","actions","[","i","]",".","observations","may","instead","have","shape","(","T","+","1",",","S^","*",")",".","*","next_observations","(","np.ndarray",")",":","Non-flattened","array","of","observations",".","Has","shape","(","T",",","S^","*",")",".","next_observations","[","i","]","was","observed","by","the","agent","after","taking","actions","[","i","]",".","Optional",".","Note","that","to","ensure","all","information","from","the","environment","was","preserved",",","observations","[","i","]","must","have","shape","(","T","+","1",",","S^","*",")",",","or","this","key","must","be","set",".","However",",","this","method","is","lenient","and","will","``","duplicate","''","the","last","observation","if","the","original","last","observation","has","been","lost",".","*","actions","(","np.ndarray",")",":","Non-flattened","array","of","actions",".","Must","have","shape","(","T",",","S^","*",")","(","the","unflattened","action","space","of","the","current","environment",")",".","*","rewards","(","np.ndarray",")",":","Array","of","rewards","of","shape","(","T",",",")","(","1D","array","of","length","timesteps",")",".","*","agent_infos","(","dict","[","str",",","np.ndarray","]",")",":","Dictionary","of","stacked",",","non-flattened","`","agent_info","`","arrays",".","*","env_infos","(","dict","[","str",",","np.ndarray","]",")",":","Dictionary","of","stacked",",","non-flattened","`","env_info","`","arrays",".","*","step_types","(","numpy.ndarray",")",":","A","numpy","array","of","`","StepType","with","shape","(","T",",",")","containing","the","time","step","types","for","all","transitions","in","this","batch","."]
code: def from_list(cls, env_spec, paths):
      """Create a EpisodeBatch from a list of episodes.
  
      Args:
          env_spec (EnvSpec): Specification for the environment from which
              this data was sampled.
          paths (list[dict[str, np.ndarray or dict[str, np.ndarray]]]): Keys:
              * episode_infos (dict[str, np.ndarray]): Dictionary of stacked,
                  non-flattened `episode_info` arrays, each of shape (S^*).
              * observations (np.ndarray): Non-flattened array of
                  observations. Typically has shape (T, S^*) (the unflattened
                  state space of the current environment). observations[i]
                  was used by the agent to choose actions[i]. observations
                  may instead have shape (T + 1, S^*).
              * next_observations (np.ndarray): Non-flattened array of
                  observations. Has shape (T, S^*). next_observations[i] was
                  observed by the agent after taking actions[i]. Optional.
                  Note that to ensure all information from the environment
                  was preserved, observations[i] must have shape (T + 1,
                  S^*), or this key must be set. However, this method is
                  lenient and will "duplicate" the last observation if the
                  original last observation has been lost.
              * actions (np.ndarray): Non-flattened array of actions. Must
                  have shape (T, S^*) (the unflattened action space of the
                  current environment).
              * rewards (np.ndarray): Array of rewards of shape (T,) (1D
                  array of length timesteps).
              * agent_infos (dict[str, np.ndarray]): Dictionary of stacked,
                  non-flattened `agent_info` arrays.
              * env_infos (dict[str, np.ndarray]): Dictionary of stacked,
                  non-flattened `env_info` arrays.
              * step_types (numpy.ndarray): A numpy array of `StepType with
                  shape (T,) containing the time step types for all
                  transitions in this batch.
      """
      lengths = np.asarray([len(p["rewards"]) for p in paths])
      if all(
          len(path["observations"]) == length + 1
          for (path, length) in zip(paths, lengths)
      ):
          last_observations = np.asarray(
              [p["observations"][-1] for p in paths]
          )
          observations = np.concatenate(
              [p["observations"][:-1] for p in paths]
          )
      else:
          # The number of observations and timesteps must match.
          observations = np.concatenate(
              [p["observations"] for p in paths]
          )
          if paths[0].get("next_observations") is not None:
              last_observations = np.asarray(
                  [p["next_observations"][-1] for p in paths]
              )
          else:
              last_observations = np.asarray(
                  [p["observations"][-1] for p in paths]
              )
  
      stacked_paths = concat_tensor_dict_list(paths)
      episode_infos = stack_tensor_dict_list(
          [path["episode_infos"] for path in paths]
      )
  
      # Temporary solution. This logic is not needed if algorithms process
      # step_types instead of dones directly.
      if "dones" in stacked_paths and "step_types" not in stacked_paths:
          step_types = np.array(
              [
                  StepType.TERMINAL if done else StepType.MID
                  for done in stacked_paths["dones"]
              ],
              dtype=StepType,
          )
          stacked_paths["step_types"] = step_types
          del stacked_paths["dones"]
  
      return cls(
          env_spec=env_spec,
          episode_infos=episode_infos,
          observations=observations,
          last_observations=last_observations,
          actions=stacked_paths["actions"],
          rewards=stacked_paths["rewards"],
          env_infos=stacked_paths["env_infos"],
          agent_infos=stacked_paths["agent_infos"],
          step_types=stacked_paths["step_types"],
          lengths=lengths,
      )
  
imports: from dataclasses import dataclass
  import enum
  from typing import Dict, List
  import warnings
  import numpy
  from garage.np import concat_tensor_dict_list, pad_batch_array, slice_nested_dict, stack_tensor_dict_list
label_desc: Use image node_id 6 for calling the EpisodeBatch obj's underlying member method code with example usage: obj.from_list(cls, env_spec, paths) and returns: cls