class_docstr: A preprocessor used for the soft actor critic from demonstrations.
  
  This preprocessor reads trajectories and adds the next observation to the current transition, as they are needed
  for the state-action critic of the sac.
class_docstr_tok: ["A","preprocessor","used","for","the","soft","actor","critic","from","demonstrations",".","This","preprocessor","reads","trajectories","and","adds","the","next","observation","to","the","current","transition",",","as","they","are","needed","for","the","state-action","critic","of","the","sac","."]
docstr: Convert an episode trajectory record into an array of observations and actions using the given env.
  
  :param trajectory: Episode record to load
  :param conversion_env: Env to use for conversion of MazeStates and MazeActions into observations and actions.
                         Required only if state records are being loaded (i.e. conversion to raw actions and
                         observations is needed).
  :return: Loaded observations and actions. I.e., a tuple (observation_list, action_list). Each of the
           lists contains observation/action dictionaries, with keys corresponding to IDs of structured
           sub-steps. (I.e., the dictionary will have just one entry for non-structured scenarios.)
docstr_tok: ["Convert","an","episode","trajectory","record","into","an","array","of","observations","and","actions","using","the","given","env",".",":","param","trajectory",":","Episode","record","to","load",":","param","conversion_env",":","Env","to","use","for","conversion","of","MazeStates","and","MazeActions","into","observations","and","actions",".","Required","only","if","state","records","are","being","loaded","(","i.e",".","conversion","to","raw","actions","and","observations","is","needed",")",".",":","return",":","Loaded","observations","and","actions",".","I.e.",",","a","tuple","(","observation_list",",","action_list",")",".","Each","of","the","lists","contains","observation\/action","dictionaries",",","with","keys","corresponding","to","IDs","of","structured","sub-steps",".","(","I.e.",",","the","dictionary","will","have","just","one","entry","for","non-structured","scenarios",".",")"]
code: def convert_trajectory_with_env(
      trajectory: TrajectoryRecord, conversion_env: Optional[MazeEnv]
  ) -> List[StructuredSpacesRecord]:
      """Convert an episode trajectory record into an array of observations and actions using the given env.
  
      :param trajectory: Episode record to load
      :param conversion_env: Env to use for conversion of MazeStates and MazeActions into observations and actions.
                             Required only if state records are being loaded (i.e. conversion to raw actions and
                             observations is needed).
      :return: Loaded observations and actions. I.e., a tuple (observation_list, action_list). Each of the
               lists contains observation/action dictionaries, with keys corresponding to IDs of structured
               sub-steps. (I.e., the dictionary will have just one entry for non-structured scenarios.)
      """
      spaces_records = []
  
      for step_id, step_record in enumerate(
          trajectory.step_records[::-1]
      ):
          # Process and convert in case we are dealing with state records (otherwise no conversion needed)
          if isinstance(step_record, StateRecord):
              assert conversion_env is not None, (
                  "when conversion from Maze states is needed, conversion env "
                  "needs to be present."
              )
  
              # Drop incomplete records (e.g. at the end of episode)
              if (
                  step_record.maze_state is None
                  or step_record.maze_action is None
              ):
                  if step_id == 0:
                      step_record.maze_action = trajectory.step_records[
                          1
                      ].maze_action
                  else:
                      continue
  
              # Convert to spaces
              spaces_record: StructuredSpacesRecord = (
                  StructuredSpacesRecord.converted_from(
                      step_record,
                      conversion_env=conversion_env,
                      first_step_in_episode=step_id
                      == len(trajectory.step_records),
                  )
              )
              assert len(spaces_record.substep_records) == 1, (
                  f"Only spaces with one substep are supported at this "
                  f"point"
              )
              spaces_record.substep_records[
                  -1
              ].reward = step_record.reward
          else:
              spaces_record = step_record
  
          if step_id > 0:
              for substep, next_substep in zip(
                  spaces_record.substep_records,
                  spaces_records[-1].substep_records,
              ):
                  substep.next_observation = next_substep.observation
  
          spaces_records.append(spaces_record)
  
      spaces_records = spaces_records[1:][::-1]
  
      return spaces_records
  
imports: import dataclasses
  from abc import abstractmethod
  from typing import List, Optional, Union, Dict, Tuple
  from maze.core.annotations import override
  from maze.core.env.maze_env import MazeEnv
  from maze.core.trajectory_recording.records.state_record import StateRecord
  from maze.core.trajectory_recording.records.structured_spaces_record import StructuredSpacesRecord
  from maze.core.trajectory_recording.records.trajectory_record import TrajectoryRecord
label_desc: Use image node_id 1 for calling the IdentityWithNextObservationTrajectoryProcessor obj's underlying member method code with example usage: obj.convert_trajectory_with_env(trajectory, conversion_env) and returns: spaces_records