Skip to content

lit_sb3.wrappers.monitor

source module lit_sb3.wrappers.monitor

Classes

  • Monitor — A monitor wrapper for Gym environments, it is used to know the episode reward, length, time and other data.

  • ResultsWriter — A result writer that saves the data from the Monitor class.

  • LoadMonitorResultsError — Raised when loading the monitor log fails.

Functions

  • get_monitor_files — Get all the monitor files in the given path.

  • load_results — Load all Monitor logs from a given directory path matching *monitor.csv.

source class Monitor(env: gym.Env, filename: str | Path | None = None, allow_early_resets: bool = True, reset_keywords: tuple[str, ...] = (), info_keywords: tuple[str, ...] = (), override_existing: bool = True)

Bases : gym.Wrapper[ObsType, ActType, ObsType, ActType]

A monitor wrapper for Gym environments, it is used to know the episode reward, length, time and other data.

Parameters

  • env : gym.Env — The environment

  • filename : str | Path | None — the location to save a log file, can be None for no log

  • allow_early_resets : bool — allows the reset of the environment before it is done

  • reset_keywords : tuple[str, ...] — extra keywords for the reset call, if extra parameters are needed at reset

  • info_keywords : tuple[str, ...] — extra information to log, from the information return of env.step()

  • override_existing : bool — appends to file if filename exists, otherwise override existing files (default)

Attributes

  • metadata : dict[str, Any] — attr:Env :attr:metadata.

  • render_mode : str | None — attr:Env :attr:render_mode.

  • spec : EnvSpec | None — attr:Env :attr:spec attribute with the WrapperSpec if the wrapper inherits from EzPickle.

  • action_space : spaces.Space[ActType] | spaces.Space[WrapperActType] — attr:Env :attr:action_space unless overwritten then the wrapper :attr:action_space is used.

  • observation_space : spaces.Space[ObsType] | spaces.Space[WrapperObsType] — attr:Env :attr:observation_space unless overwritten then the wrapper :attr:observation_space is used.

  • unwrapped : Env[ObsType, ActType] — Returns the base environment of the wrapper.

  • np_random_seed : int | None — attr:np_random_seed.

  • np_random : np.random.Generator — attr:Env :attr:np_random attribute.

Methods

  • reset — Calls the Gym environment reset. Can only be called if the environment is over, or if allow_early_resets is.

  • step — Step the environment with the given action.

  • close — Closes the environment.

  • get_total_steps — Returns the total number of timesteps.

  • get_episode_rewards — Returns the rewards of all the episodes.

  • get_episode_lengths — Returns the number of timesteps of all the episodes.

  • get_episode_times — Returns the runtime in seconds of all the episodes.

source method Monitor.reset(**kwargs) → tuple[ObsType, dict[str, Any]]

Calls the Gym environment reset. Can only be called if the environment is over, or if allow_early_resets is.

True.

Parameters

  • kwargs — Extra keywords saved for the next episode. only if defined by reset_keywords

Returns

  • tuple[ObsType, dict[str, Any]] — the first observation of the environment

Raises

  • RuntimeError

  • ValueError

source method Monitor.step(action: ActType) → tuple[ObsType, SupportsFloat, bool, bool, dict[str, Any]]

Step the environment with the given action.

Parameters

  • action : ActType — the action

Returns

  • tuple[ObsType, SupportsFloat, bool, bool, dict[str, Any]] — observation, reward, terminated, truncated, information

Raises

  • RuntimeError

source method Monitor.close() → None

Closes the environment.

source method Monitor.get_total_steps() → int

Returns the total number of timesteps.

source method Monitor.get_episode_rewards() → list[float]

Returns the rewards of all the episodes.

source method Monitor.get_episode_lengths() → list[int]

Returns the number of timesteps of all the episodes.

source method Monitor.get_episode_times() → list[float]

Returns the runtime in seconds of all the episodes.

source class ResultsWriter(filename: str | Path = '', header: dict[str, float | str] | None = None, extra_keys: tuple[str, ...] = (), override_existing: bool = True)

A result writer that saves the data from the Monitor class.

Parameters

  • filename : str | Path — the location to save a log file. When it does not end in the string "monitor.csv", this suffix will be appended to it

  • header : dict[str, float | str] | None — the header dictionary object of the saved csv

  • extra_keys : tuple[str, ...] — the extra information to log, typically is composed of reset_keywords and info_keywords

  • override_existing : bool — appends to file if filename exists, otherwise override existing files (default)

Methods

  • write_row — Write row of monitor data to csv log file.

  • close — Close the file handler.

source method ResultsWriter.write_row(epinfo: dict[str, float]) → None

Write row of monitor data to csv log file.

Parameters

  • epinfo : dict[str, float] — the information on episodic return, length, and time

source method ResultsWriter.close() → None

Close the file handler.

source get_monitor_files(path: str | Path) → list[str]

Get all the monitor files in the given path.

Parameters

  • path : str | Path — the logging folder

Returns

  • list[str] — the log files

source load_results(path: str | Path) → pandas.DataFrame

Load all Monitor logs from a given directory path matching *monitor.csv.

Parameters

  • path : str | Path — the directory path containing the log file(s)

Returns

  • pandas.DataFrame — the logged data

Raises

source class LoadMonitorResultsError()

Bases : Exception

Raised when loading the monitor log fails.