class_docstr: RL^2.
  
  Reference: https://arxiv.org/pdf/1611.02779.pdf.
  
  When sampling for RL^2, there are more than one environments to be
  sampled from. In the original implementation, within each task/environment,
  all episodes sampled will be concatenated into one single episode, and fed
  to the inner algorithm. Thus, returns and advantages are calculated across
  the episode.
  
  RL2Worker is required in sampling for RL2.
  See example/tf/rl2_ppo_halfcheetah.py for reference.
  
  User should not instantiate RL2 directly.
  Currently garage supports PPO and TRPO as inner algorithm. Refer to
  garage/tf/algos/rl2ppo.py and garage/tf/algos/rl2trpo.py.
  
  Args:
      env_spec (EnvSpec): Environment specification.
      episodes_per_trial (int): Used to calculate the max episode length for
          the inner algorithm.
      meta_batch_size (int): Meta batch size.
      task_sampler (TaskSampler): Task sampler.
      meta_evaluator (MetaEvaluator): Evaluator for meta-RL algorithms.
      n_epochs_per_eval (int): If meta_evaluator is passed, meta-evaluation
          will be performed every `n_epochs_per_eval` epochs.
      inner_algo_args (dict): Arguments for inner algorithm.
class_docstr_tok: ["RL^2",".","Reference",":","https",":","\/\/arxiv.org\/pdf\/1611.02779.pdf",".","When","sampling","for","RL^2",",","there","are","more","than","one","environments","to","be","sampled","from",".","In","the","original","implementation",",","within","each","task\/environment",",","all","episodes","sampled","will","be","concatenated","into","one","single","episode",",","and","fed","to","the","inner","algorithm",".","Thus",",","returns","and","advantages","are","calculated","across","the","episode",".","RL2Worker","is","required","in","sampling","for","RL2",".","See","example\/tf\/rl2_ppo_halfcheetah.py","for","reference",".","User","should","not","instantiate","RL2","directly",".","Currently","garage","supports","PPO","and","TRPO","as","inner","algorithm",".","Refer","to","garage\/tf\/algos\/rl2ppo.py","and","garage\/tf\/algos\/rl2trpo.py",".","Args",":","env_spec","(","EnvSpec",")",":","Environment","specification",".","episodes_per_trial","(","int",")",":","Used","to","calculate","the","max","episode","length","for","the","inner","algorithm",".","meta_batch_size","(","int",")",":","Meta","batch","size",".","task_sampler","(","TaskSampler",")",":","Task","sampler",".","meta_evaluator","(","MetaEvaluator",")",":","Evaluator","for","meta-RL","algorithms",".","n_epochs_per_eval","(","int",")",":","If","meta_evaluator","is","passed",",","meta-evaluation","will","be","performed","every","`","n_epochs_per_eval","`","epochs",".","inner_algo_args","(","dict",")",":","Arguments","for","inner","algorithm","."]
docstr: Return processed sample data based on the collected paths.
  
  Args:
      itr (int): Iteration number.
      episodes (EpisodeBatch): Original collected episode batch for each
          task. For each episode, episode.agent_infos['batch_idx']
          indicates which task this episode belongs to. In RL^2, there
          are n environments/tasks and paths in each of them will be
          concatenated at some point and fed to the policy.
  
  Returns:
      EpisodeBatch: Processed batch of episodes for feeding the inner
          algorithm.
      numpy.float64: The average return.
  
  Raises:
      ValueError: If 'batch_idx' is not found.
docstr_tok: ["Return","processed","sample","data","based","on","the","collected","paths",".","Args",":","itr","(","int",")",":","Iteration","number",".","episodes","(","EpisodeBatch",")",":","Original","collected","episode","batch","for","each","task",".","For","each","episode",",","episode.agent_infos","[","'batch_idx","'","]","indicates","which","task","this","episode","belongs","to",".","In","RL^2",",","there","are","n","environments\/tasks","and","paths","in","each","of","them","will","be","concatenated","at","some","point","and","fed","to","the","policy",".","Returns",":","EpisodeBatch",":","Processed","batch","of","episodes","for","feeding","the","inner","algorithm",".","numpy.float64",":","The","average","return",".","Raises",":","ValueError",":","If","'batch_idx","'","is","not","found","."]
code: def _process_samples(self, itr, episodes):
      # pylint: disable=too-many-statements
      """Return processed sample data based on the collected paths.
  
      Args:
          itr (int): Iteration number.
          episodes (EpisodeBatch): Original collected episode batch for each
              task. For each episode, episode.agent_infos['batch_idx']
              indicates which task this episode belongs to. In RL^2, there
              are n environments/tasks and paths in each of them will be
              concatenated at some point and fed to the policy.
  
      Returns:
          EpisodeBatch: Processed batch of episodes for feeding the inner
              algorithm.
          numpy.float64: The average return.
  
      Raises:
          ValueError: If 'batch_idx' is not found.
  
      """
      concatenated_paths = []
  
      paths_by_task = collections.defaultdict(list)
      for episode in episodes.split():
          if hasattr(episode, "batch_idx"):
              paths_by_task[episode.batch_idx[0]].append(episode)
          elif "batch_idx" in episode.agent_infos:
              paths_by_task[episode.agent_infos["batch_idx"][0]].append(
                  episode
              )
          else:
              raise ValueError(
                  "Batch idx is required for RL2 but not found, "
                  "Make sure to use garage.tf.algos.rl2.RL2Worker "
                  "for sampling"
              )
  
      # all path in paths_by_task[i] are sampled from task[i]
      for episode_list in paths_by_task.values():
          concatenated_path = self._concatenate_episodes(episode_list)
          concatenated_paths.append(concatenated_path)
  
      concatenated_episodes = EpisodeBatch.concatenate(
          *concatenated_paths
      )
  
      name_map = None
      if hasattr(self._task_sampler, "_envs") and hasattr(
          self._task_sampler._envs[0]._env, "all_task_names"
      ):
          names = [
              env._env.all_task_names[0]
              for env in self._task_sampler._envs
          ]
          name_map = dict(enumerate(names))
  
      undiscounted_returns = log_multitask_performance(
          itr, episodes, self._inner_algo._discount, name_map=name_map
      )
  
      average_return = np.mean(undiscounted_returns)
  
      return concatenated_episodes, average_return
  
imports: import abc
  import collections
  import akro
  from dowel import logger
  import numpy
  from garage import EnvSpec, EnvStep, EpisodeBatch, log_multitask_performance, StepType, Wrapper
  from garage.np.algos import MetaRLAlgorithm
  from garage.sampler import DefaultWorker
  from garage.tf.algos._rl2npo import RL2NPO
label_desc: Use image node_id 6 for calling the RL2 obj's underlying member method code with example usage: obj._process_samples(itr, episodes) and returns: concatenated_episodes,  average_return