Monitor Wrapper

class stable_baselines3.common.monitor.Monitor(env, filename=None, allow_early_resets=True, reset_keywords=(), info_keywords=(), override_existing=True)[source]

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

Parameters:
  • env – The environment

  • filename – the location to save a log file, can be None for no log

  • allow_early_resets – allows the reset of the environment before it is done

  • reset_keywords – extra keywords for the reset call, if extra parameters are needed at reset

  • info_keywords – extra information to log, from the information return of env.step()

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

close()[source]

Closes the environment

Return type:

None

get_episode_lengths()[source]

Returns the number of timesteps of all the episodes

Returns:

Return type:

List[int]

get_episode_rewards()[source]

Returns the rewards of all the episodes

Returns:

Return type:

List[float]

get_episode_times()[source]

Returns the runtime in seconds of all the episodes

Returns:

Return type:

List[float]

get_total_steps()[source]

Returns the total number of timesteps

Returns:

Return type:

int

reset(**kwargs)[source]

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:

the first observation of the environment

Return type:

Tuple[ObsType, Dict[str, Any]]

step(action)[source]

Step the environment with the given action

Parameters:

action (ActType) – the action

Returns:

observation, reward, terminated, truncated, information

Return type:

Tuple[ObsType, SupportsFloat, bool, bool, Dict[str, Any]]

class stable_baselines3.common.monitor.ResultsWriter(filename='', header=None, extra_keys=(), override_existing=True)[source]

A result writer that saves the data from the Monitor class

Parameters:
  • filename (str) – 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)

close()[source]

Close the file handler

Return type:

None

write_row(epinfo)[source]

Write row of monitor data to csv log file.

Parameters:

epinfo (Dict[str, float]) – the information on episodic return, length, and time

Return type:

None

stable_baselines3.common.monitor.get_monitor_files(path)[source]

get all the monitor files in the given path

Parameters:

path (str) – the logging folder

Returns:

the log files

Return type:

List[str]

stable_baselines3.common.monitor.load_results(path)[source]

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

Parameters:

path (str) – the directory path containing the log file(s)

Returns:

the logged data

Return type:

DataFrame