mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-15 11:18:47 +00:00
Pydocstyle utils vector docstring (#2788)
* Added pydocstyle to pre-commit * Added docstrings for tests and updated the tests for autoreset * Add pydocstyle exclude folder to allow slowly adding new docstrings * Add docstrings for setup.py and gym/__init__.py, core.py, error.py and logger.py * Check that all unwrapped environment are of a particular wrapper type * Reverted back to import gym.spaces.Space to gym.spaces * Fixed the __init__.py docstring * Fixed autoreset autoreset test * Updated gym __init__.py top docstring * Fix examples in docstrings * Add docstrings and type hints where known to all functions and classes in gym/utils and gym/vector * Remove unnecessary import * Removed "unused error" and make APIerror deprecated at gym 1.0 * Add pydocstyle description to CONTRIBUTING.md * Added docstrings section to CONTRIBUTING.md * Added :meth: and :attr: keywords to docstrings * Added :meth: and :attr: keywords to docstrings * Imported annotations from __future__ to fix python 3.7 * Add __future__ import annotations for python 3.7 * isort * Remove utils and vectors for this PR and spaces for previous PR * Update gym/envs/classic_control/acrobot.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/envs/classic_control/acrobot.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/envs/classic_control/acrobot.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/spaces/dict.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/ezpickle.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/ezpickle.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/play.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Pre-commit * Updated docstrings with :meth: * Updated docstrings with :meth: * Update gym/utils/play.py * Update gym/utils/play.py * Update gym/utils/play.py * Apply suggestions from code review Co-authored-by: Markus Krimmel <montcyril@gmail.com> * pre-commit * Update gym/utils/play.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Updated fps and zoom parameter docstring * Update play docstring * Apply suggestions from code review Added suggested corrections from @markus28 Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Pre-commit magic * Update the `gym.make` docstring with a warning for `env_checker` * Updated and fixed vector docstrings * Update test names for reflect the project filename style Co-authored-by: Markus Krimmel <montcyril@gmail.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"""Numpy utility functions: concatenate space samples and create empty array."""
|
||||
from collections import OrderedDict
|
||||
from functools import singledispatch
|
||||
from typing import Iterable, Union
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -9,36 +11,29 @@ __all__ = ["concatenate", "create_empty_array"]
|
||||
|
||||
|
||||
@singledispatch
|
||||
def concatenate(space, items, out):
|
||||
def concatenate(
|
||||
space: Space, items: Iterable, out: Union[tuple, dict, np.ndarray]
|
||||
) -> Union[tuple, dict, np.ndarray]:
|
||||
"""Concatenate multiple samples from space into a single object.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
items : iterable of samples of `space`
|
||||
Samples to be concatenated.
|
||||
Example::
|
||||
|
||||
out : tuple, dict, or `np.ndarray`
|
||||
>>> from gym.spaces import Box
|
||||
>>> space = Box(low=0, high=1, shape=(3,), dtype=np.float32)
|
||||
>>> out = np.zeros((2, 3), dtype=np.float32)
|
||||
>>> items = [space.sample() for _ in range(2)]
|
||||
>>> concatenate(space, items, out)
|
||||
array([[0.6348213 , 0.28607962, 0.60760117],
|
||||
[0.87383074, 0.192658 , 0.2148103 ]], dtype=float32)
|
||||
|
||||
Args:
|
||||
space: Observation space of a single environment in the vectorized environment.
|
||||
items: Samples to be concatenated.
|
||||
out: The output object. This object is a (possibly nested) numpy array.
|
||||
|
||||
Returns:
|
||||
The output object. This object is a (possibly nested) numpy array.
|
||||
|
||||
space : `gym.spaces.Space` instance
|
||||
Observation space of a single environment in the vectorized environment.
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : tuple, dict, or `np.ndarray`
|
||||
The output object. This object is a (possibly nested) numpy array.
|
||||
|
||||
Example
|
||||
-------
|
||||
>>> from gym.spaces import Box
|
||||
>>> space = Box(low=0, high=1, shape=(3,), dtype=np.float32)
|
||||
>>> out = np.zeros((2, 3), dtype=np.float32)
|
||||
>>> items = [space.sample() for _ in range(2)]
|
||||
>>> concatenate(items, out, space)
|
||||
array([[0.6348213 , 0.28607962, 0.60760117],
|
||||
[0.87383074, 0.192658 , 0.2148103 ]], dtype=float32)
|
||||
"""
|
||||
assert isinstance(items, (list, tuple))
|
||||
raise ValueError(
|
||||
f"Space of type `{type(space)}` is not a valid `gym.Space` instance."
|
||||
)
|
||||
@@ -76,38 +71,30 @@ def _concatenate_custom(space, items, out):
|
||||
|
||||
|
||||
@singledispatch
|
||||
def create_empty_array(space, n=1, fn=np.zeros):
|
||||
def create_empty_array(
|
||||
space: Space, n: int = 1, fn: callable = np.zeros
|
||||
) -> Union[tuple, dict, np.ndarray]:
|
||||
"""Create an empty (possibly nested) numpy array.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
space : `gym.spaces.Space` instance
|
||||
Observation space of a single environment in the vectorized environment.
|
||||
Example::
|
||||
|
||||
n : int
|
||||
Number of environments in the vectorized environment. If `None`, creates
|
||||
an empty sample from `space`.
|
||||
>>> from gym.spaces import Box, Dict
|
||||
>>> space = Dict({
|
||||
... 'position': Box(low=0, high=1, shape=(3,), dtype=np.float32),
|
||||
... 'velocity': Box(low=0, high=1, shape=(2,), dtype=np.float32)})
|
||||
>>> create_empty_array(space, n=2, fn=np.zeros)
|
||||
OrderedDict([('position', array([[0., 0., 0.],
|
||||
[0., 0., 0.]], dtype=float32)),
|
||||
('velocity', array([[0., 0.],
|
||||
[0., 0.]], dtype=float32))])
|
||||
|
||||
fn : callable
|
||||
Function to apply when creating the empty numpy array. Examples of such
|
||||
functions are `np.empty` or `np.zeros`.
|
||||
Args:
|
||||
space: Observation space of a single environment in the vectorized environment.
|
||||
n: Number of environments in the vectorized environment. If `None`, creates an empty sample from `space`.
|
||||
fn: Function to apply when creating the empty numpy array. Examples of such functions are `np.empty` or `np.zeros`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : tuple, dict, or `np.ndarray`
|
||||
Returns:
|
||||
The output object. This object is a (possibly nested) numpy array.
|
||||
|
||||
Example
|
||||
-------
|
||||
>>> from gym.spaces import Box, Dict
|
||||
>>> space = Dict({
|
||||
... 'position': Box(low=0, high=1, shape=(3,), dtype=np.float32),
|
||||
... 'velocity': Box(low=0, high=1, shape=(2,), dtype=np.float32)})
|
||||
>>> create_empty_array(space, n=2, fn=np.zeros)
|
||||
OrderedDict([('position', array([[0., 0., 0.],
|
||||
[0., 0., 0.]], dtype=float32)),
|
||||
('velocity', array([[0., 0.],
|
||||
[0., 0.]], dtype=float32))])
|
||||
"""
|
||||
raise ValueError(
|
||||
f"Space of type `{type(space)}` is not a valid `gym.Space` instance."
|
||||
|
Reference in New Issue
Block a user