mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-22 07:02:19 +00:00
* initial draft of optional info dict in reset function, implemented for cartpole, tests seem to be passing * merged core.py * updated return type annotation for reset function in core.py * optional metadata with return_info from reset added for all first party environments, with corresponding tests. Incomplete implementation for wrappers and vector wrappers * removed Optional type for return_info arguments * added tests for return_info to normalize wrapper and sync_vector_env * autoformatted using black * added optional reset metadata tests to several wrappers * added return_info capability to async_vector_env.py and test to verify functionality * added optional return_info test for record_video.py * removed tests for mujoco environments * autoformatted * improved test coverage for optional reset return_info * re-removed unit test envs accidentally reintroduced in merge * removed unnecessary import * changes based on code-review * small fix to core wrapper typing and autoformatted record_epsisode_stats * small change to pass flake8 style
108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
import pytest
|
|
import os
|
|
import shutil
|
|
|
|
import numpy as np
|
|
|
|
import gym
|
|
from gym.wrappers import (
|
|
RecordEpisodeStatistics,
|
|
RecordVideo,
|
|
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")
|