mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-01 22:11:25 +00:00
* Remove additional ignores from flake8 * Remove all unused imports * Remove all unused imports * Update flake8 and pyupgrade * F841, removed unused variables * E731, removed lambda assignment to variables * Remove E731, F403, F405, F524 * Remove E722, bare exceptions * Remove E712, compare variable == True or == False to is True or is False * Remove E402, module level import not at top of file * Added --pre-file-ignores * Add --per-file-ignores removing E741, E302 and E704 * Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control * Fixed issues for pytest==6.2 * Remove unnecessary # noqa * Edit comment with the removal of E302 * Added warnings and declared module, attr for pyright type hinting * Remove unused import * Removed flake8 E302 * Updated flake8 from 3.9.2 to 4.0.1 * Remove unused variable
101 lines
3.1 KiB
Python
101 lines
3.1 KiB
Python
import os
|
|
import shutil
|
|
|
|
import gym
|
|
from gym.wrappers import capped_cubic_video_schedule
|
|
|
|
|
|
def test_record_video_using_default_trigger():
|
|
|
|
env = gym.make("CartPole-v1")
|
|
env = gym.wrappers.RecordVideo(env, "videos")
|
|
env.reset()
|
|
for _ in range(199):
|
|
action = env.action_space.sample()
|
|
_, _, done, _ = env.step(action)
|
|
if done:
|
|
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) == sum(
|
|
capped_cubic_video_schedule(i) for i in range(env.episode_id + 1)
|
|
)
|
|
shutil.rmtree("videos")
|
|
|
|
|
|
def test_record_video_reset_return_info():
|
|
env = gym.make("CartPole-v1")
|
|
env = gym.wrappers.RecordVideo(env, "videos", step_trigger=lambda x: x % 100 == 0)
|
|
ob_space = env.observation_space
|
|
obs, info = env.reset(return_info=True)
|
|
env.close()
|
|
assert os.path.isdir("videos")
|
|
shutil.rmtree("videos")
|
|
assert ob_space.contains(obs)
|
|
assert isinstance(info, dict)
|
|
|
|
env = gym.make("CartPole-v1")
|
|
env = gym.wrappers.RecordVideo(env, "videos", step_trigger=lambda x: x % 100 == 0)
|
|
ob_space = env.observation_space
|
|
obs = env.reset(return_info=False)
|
|
env.close()
|
|
assert os.path.isdir("videos")
|
|
shutil.rmtree("videos")
|
|
assert ob_space.contains(obs)
|
|
|
|
env = gym.make("CartPole-v1")
|
|
env = gym.wrappers.RecordVideo(env, "videos", step_trigger=lambda x: x % 100 == 0)
|
|
ob_space = env.observation_space
|
|
obs = env.reset()
|
|
env.close()
|
|
assert os.path.isdir("videos")
|
|
shutil.rmtree("videos")
|
|
assert ob_space.contains(obs)
|
|
|
|
|
|
def test_record_video_step_trigger():
|
|
env = gym.make("CartPole-v1")
|
|
env._max_episode_steps = 20
|
|
env = gym.wrappers.RecordVideo(env, "videos", step_trigger=lambda x: x % 100 == 0)
|
|
env.reset()
|
|
for _ in range(199):
|
|
action = env.action_space.sample()
|
|
_, _, done, _ = env.step(action)
|
|
if done:
|
|
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")
|
|
|
|
|
|
def make_env(gym_id, seed):
|
|
def thunk():
|
|
env = gym.make(gym_id)
|
|
env._max_episode_steps = 20
|
|
if seed == 1:
|
|
env = gym.wrappers.RecordVideo(
|
|
env, "videos", step_trigger=lambda x: x % 100 == 0
|
|
)
|
|
return env
|
|
|
|
return thunk
|
|
|
|
|
|
def test_record_video_within_vector():
|
|
envs = gym.vector.SyncVectorEnv([make_env("CartPole-v1", 1 + i) for i in range(2)])
|
|
envs = gym.wrappers.RecordEpisodeStatistics(envs)
|
|
envs.reset()
|
|
for i in range(199):
|
|
_, _, _, infos = envs.step(envs.action_space.sample())
|
|
for info in infos:
|
|
if "episode" in info.keys():
|
|
print(f"episode_reward={info['episode']['r']}")
|
|
break
|
|
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")
|