Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Framestack obs env_checker #1574

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

corentinlger
Copy link
Contributor

Fix env_checker function for gymnasium FrameStacked observations (previously raised an error)

Description

FrameStacked obs from Gymnasium only support Box obs and are stored in a LazyFrames class. Because the associated obs_space is a np.ndarray, I only added a condition where I still check if the obs is a np.ndarray class, but also a LazyFrames class (when _is_numpy_array_space is True).

@qgallouedec also proposed to use np.can_cast which worked, but the type hinting for the function became really long (and could lead to errors) because you can also cast str, bytes ... So I don't know if the solution used here is the best but it seems to work. What do you think about it ?

Motivation and Context

Solves the problem mentionned in this issue.

closes #1500

  • [x ] I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • [x ] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • [ x] I've read the CONTRIBUTION guide (required)
  • [x ] I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • [ x] I have updated the tests accordingly (required for a bug fix or a new feature).
  • [ x] I have updated the documentation accordingly.
  • I have opened an associated PR on the SB3-Contrib repository (if necessary)
  • I have opened an associated PR on the RL-Zoo3 repository (if necessary)
  • [x ] I have reformatted the code using make format (required)
  • [ x] I have checked the codestyle using make check-codestyle and make lint (required)
  • I have ensured make pytest and make type both pass. (required) --> I don't have a GPU and had a segmentation error by running 'make pytest'
  • [ x] I have checked that the documentation builds using make doc (required)

Note: You can run most of the checks using make commit-checks.

Note: we are using a maximum length of 127 characters per line

@qgallouedec
Copy link
Collaborator

Thank you @corentinlger!

Are we sure that everything is working with an environment that returns LazyFrame?

@qgallouedec
Copy link
Collaborator

but the type hinting for the function became really long (and could lead to errors) because you can also cast str, bytes

Where do you need to update the type hint?

@corentinlger
Copy link
Contributor Author

Yes I checked with several FrameStacked envs that returned LazyFrames observations and everything worked !

If I am not wrong, when using np.can_cast I needed to update the type hint on the 'check_obs' function. When I ran make pytype I got this error :

'''
stable_baselines3/common/env_checker.py:190: error: Argument 1 to "can_cast" has incompatible type "Union[Tuple[Any, ...], Dict[Any, Any], ndarray[Any, Any], int]"; expected "Union[Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], Union[dtype[Any], None, Type[Any], _SupportsDType[dtype[Any]], str, Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, Any]]]]" [arg-type] Found 1 error in 1 file (checked 82 source files) make: *** [Makefile:11: mypy] Error 1
'''

With the other option I just added LazyFrames to the obs type hint as follows : "obs: Union[tuple, dict, np.ndarray, int, LazyFrames]" and it worked.

Do you think there is a better way to solve the problem ?

@araffin araffin changed the title fix Framestack obs env_checker Fix Framestack obs env_checker Aug 5, 2023
araffin and others added 2 commits August 5, 2023 18:14
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

check_env warning for FrameStacked observation in stable_baselines3.common.env_checker
3 participants