Files
Gymnasium/tests/wrappers/test_record_video.py

95 lines
2.9 KiB
Python
Raw Normal View History

import os
import shutil
2022-09-08 10:10:07 +01:00
import gymnasium
from gymnasium.wrappers import capped_cubic_video_schedule
2021-08-31 16:59:21 -04:00
def test_record_video_using_default_trigger():
2022-09-08 10:10:07 +01:00
env = gymnasium.make(
"CartPole-v1", render_mode="rgb_array_list", disable_env_checker=True
)
2022-09-08 10:10:07 +01:00
env = gymnasium.wrappers.RecordVideo(env, "videos")
2021-08-31 16:59:21 -04:00
env.reset()
for _ in range(199):
action = env.action_space.sample()
_, _, terminated, truncated, _ = env.step(action)
if terminated or truncated:
2021-08-31 16:59:21 -04:00
env.reset()
env.close()
assert os.path.isdir("videos")
mp4_files = [file for file in os.listdir("videos") if file.endswith(".mp4")]
2021-09-03 20:26:41 -04:00
assert len(mp4_files) == sum(
2022-01-11 18:12:05 +01:00
capped_cubic_video_schedule(i) for i in range(env.episode_id + 1)
2021-09-03 20:26:41 -04:00
)
2021-08-31 16:59:21 -04:00
shutil.rmtree("videos")
def test_record_video_reset():
2022-09-08 10:11:31 +01:00
env = gymnasium.make(
"CartPole-v1", render_mode="rgb_array", disable_env_checker=True
)
env = gymnasium.wrappers.RecordVideo(
env, "videos", step_trigger=lambda x: x % 100 == 0
)
ob_space = env.observation_space
obs, info = env.reset()
env.close()
assert os.path.isdir("videos")
shutil.rmtree("videos")
assert ob_space.contains(obs)
assert isinstance(info, dict)
2021-08-31 16:59:21 -04:00
def test_record_video_step_trigger():
2022-09-08 10:11:31 +01:00
env = gymnasium.make(
"CartPole-v1", render_mode="rgb_array", disable_env_checker=True
)
2021-09-06 17:24:55 -04:00
env._max_episode_steps = 20
2022-09-08 10:11:31 +01:00
env = gymnasium.wrappers.RecordVideo(
env, "videos", step_trigger=lambda x: x % 100 == 0
)
env.reset()
for _ in range(199):
action = env.action_space.sample()
_, _, terminated, truncated, _ = env.step(action)
if terminated or truncated:
env.reset()
env.close()
assert os.path.isdir("videos")
mp4_files = [file for file in os.listdir("videos") if file.endswith(".mp4")]
assert len(mp4_files) == 2
shutil.rmtree("videos")
Render API (#2671) * add pygame GUI for frozen_lake.py env * add new line at EOF * pre-commit reformat * improve graphics * new images and dynamic window size * darker tile borders and fix ICC profile * pre-commit hook * adjust elf and stool size * Update frozen_lake.py * reformat * fix #2600 * #2600 * add rgb_array support * reformat * test render api change on FrozenLake * add render support for reset on frozenlake * add clock on pygame render * new render api for blackjack * new render api for cliffwalking * new render api for Env class * update reset method, lunar and Env * fix wrapper * fix reset lunar * new render api for box2d envs * new render api for mujoco envs * fix bug * new render api for classic control envs * fix tests * add render_mode None for CartPole * new render api for test fake envs * pre-commit hook * fix FrozenLake * fix FrozenLake * more render_mode to super - frozenlake * remove kwargs from frozen_lake new * pre-commit hook * add deprecated render method * add backwards compatibility * fix test * add _render * move pygame.init() (avoid pygame dependency on init) * fix pygame dependencies * remove collect_render() maintain multi-behaviours .render() * add type hints * fix renderer * don't call .render() with None * improve docstring * add single_rgb_array to all envs * remove None from metadata["render_modes"] * add type hints to test_env_checkers * fix lint * add comments to renderer * add comments to single_depth_array and single_state_pixels * reformat * add deprecation warnings and env.render_mode declaration * fix lint * reformat * fix tests * add docs * fix car racing determinism * remove warning test envs, customizable modes on renderer * remove commments and add todo for env_checker * fix car racing * replace render mode check with assert * update new mujoco * reformat * reformat * change metaclass definition * fix tests * implement mark suggestions (test, docs, sets) * check_render Co-authored-by: J K Terry <jkterry0@gmail.com>
2022-06-08 00:20:56 +02:00
def make_env(gym_id, seed, **kwargs):
def thunk():
2022-09-08 10:10:07 +01:00
env = gymnasium.make(gym_id, disable_env_checker=True, **kwargs)
2021-09-06 17:24:55 -04:00
env._max_episode_steps = 20
if seed == 1:
2022-09-08 10:10:07 +01:00
env = gymnasium.wrappers.RecordVideo(
2021-08-31 16:59:21 -04:00
env, "videos", step_trigger=lambda x: x % 100 == 0
)
return env
return thunk
def test_record_video_within_vector():
2022-09-08 10:10:07 +01:00
envs = gymnasium.vector.SyncVectorEnv(
Render API (#2671) * add pygame GUI for frozen_lake.py env * add new line at EOF * pre-commit reformat * improve graphics * new images and dynamic window size * darker tile borders and fix ICC profile * pre-commit hook * adjust elf and stool size * Update frozen_lake.py * reformat * fix #2600 * #2600 * add rgb_array support * reformat * test render api change on FrozenLake * add render support for reset on frozenlake * add clock on pygame render * new render api for blackjack * new render api for cliffwalking * new render api for Env class * update reset method, lunar and Env * fix wrapper * fix reset lunar * new render api for box2d envs * new render api for mujoco envs * fix bug * new render api for classic control envs * fix tests * add render_mode None for CartPole * new render api for test fake envs * pre-commit hook * fix FrozenLake * fix FrozenLake * more render_mode to super - frozenlake * remove kwargs from frozen_lake new * pre-commit hook * add deprecated render method * add backwards compatibility * fix test * add _render * move pygame.init() (avoid pygame dependency on init) * fix pygame dependencies * remove collect_render() maintain multi-behaviours .render() * add type hints * fix renderer * don't call .render() with None * improve docstring * add single_rgb_array to all envs * remove None from metadata["render_modes"] * add type hints to test_env_checkers * fix lint * add comments to renderer * add comments to single_depth_array and single_state_pixels * reformat * add deprecation warnings and env.render_mode declaration * fix lint * reformat * fix tests * add docs * fix car racing determinism * remove warning test envs, customizable modes on renderer * remove commments and add todo for env_checker * fix car racing * replace render mode check with assert * update new mujoco * reformat * reformat * change metaclass definition * fix tests * implement mark suggestions (test, docs, sets) * check_render Co-authored-by: J K Terry <jkterry0@gmail.com>
2022-06-08 00:20:56 +02:00
[make_env("CartPole-v1", 1 + i, render_mode="rgb_array") for i in range(2)]
)
2022-09-08 10:10:07 +01:00
envs = gymnasium.wrappers.RecordEpisodeStatistics(envs)
envs.reset()
for i in range(199):
_, _, _, _, infos = envs.step(envs.action_space.sample())
New `info` API for vectorized environments #2657 (#2773) * WIP refactor info API sync vector. * Add missing untracked file. * Add info strategy to reset_wait. * Add interface and docstring. * info with strategy pattern on async vector env. * Add default to async vecenv. * episode statistics for asyncvecnev. * Add tests info strategy format. * Add info strategy to reset_wait. * refactor and cleanup. * Code cleanup. Add tests. * Add tests for video recording with new info format. * fix test case. * fix camelcase. * rename enum. * update tests, docstrings, cleanup. * Changes brax strategy to numpy. add_strategy method in StrategyFactory. Add tests. * fix docstring and logging format. * Set Brax info format as default. Remove classic info format. Update tests. * breaking the wrong loop. * WIP: wrapper. * Add wrapper for brax to classic info. * WIP: wrapper with nested RecordEpisodeStatistic. * Add tests. Refactor docstrings. Cleanup. * cleanup. * patch conflicts. * rebase and conflicts. * new pre-commit conventions. * docstring. * renaming. * incorporate info_processor in vecEnv. * renaming. Create info dict only if needed. * remove all brax references. update docstring. Update duplicate test. * reviews. * pre-commit. * reviews. * docstring. * cleanup blank lines. * add support for numpy dtypes. * docstring fix. * formatting. * naming. * assert correct info from wrappers chaining. Test correct wrappers chaining. naming. * simplify episode_statistics. * change args orer. * update tests. * wip: refactor episode_statistics. * Add test for add_vecore_episode_statistics.
2022-05-24 16:36:35 +02:00
# break when every env is done
if "episode" in infos and all(infos["_episode"]):
print(f"episode_reward={infos['episode']['r']}")
assert os.path.isdir("videos")
mp4_files = [file for file in os.listdir("videos") if file.endswith(".mp4")]
assert len(mp4_files) == 2
shutil.rmtree("videos")