From dcc3959888f7eac7124247dea88e9eff59a2e008 Mon Sep 17 00:00:00 2001 From: michele-milesi <74559684+michele-milesi@users.noreply.github.com> Date: Thu, 23 Nov 2023 17:37:32 +0100 Subject: [PATCH] Fix/dependencies (#156) * feat: added custom function taken from movipy github repo * fix: box2d is now an extra dependency + fix readme * feat: update cpu-tests github workflow * feat: update imports.py --- .github/workflows/cpu-tests.yaml | 3 ++- README.md | 5 ++++- pyproject.toml | 6 ++++-- sheeprl/__init__.py | 34 ++++++++++++++++++++++++++++++++ sheeprl/utils/imports.py | 1 + 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/.github/workflows/cpu-tests.yaml b/.github/workflows/cpu-tests.yaml index 4a145332..a91725c3 100644 --- a/.github/workflows/cpu-tests.yaml +++ b/.github/workflows/cpu-tests.yaml @@ -41,7 +41,8 @@ jobs: - name: Install packages run: | python -m pip install -U pip - python -m pip install -e .[atari,test,dev] + python -m pip install swig + python -m pip install -e .[atari,box2d,test,dev] - name: Run tests run: | diff --git a/README.md b/README.md index ae2731c0..ca24de82 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,9 @@ source .venv/bin/activate pip install "sheeprl @ git+https://github.com/Eclectic-Sheep/sheeprl.git" # or, to install with atari and mujuco environment support, do pip install "sheeprl[atari,mujoco,dev] @ git+https://github.com/Eclectic-Sheep/sheeprl.git" +# or, to install with box2d environment support, do +pip install swig +pip install "sheeprl[box2d] @ git+https://github.com/Eclectic-Sheep/sheeprl.git" # or, to install with minedojo environment support, do pip install "sheeprl[minedojo,dev] @ git+https://github.com/Eclectic-Sheep/sheeprl.git" # or, to install with minerl environment support, do @@ -208,7 +211,7 @@ It is recommended to use [homebrew](https://brew.sh/) to install [SWIG](https:// # then, do brew install swig # then attempt to pip install with the preferred method, such as -pip install "sheeprl[atari,mujoco,dev,test] @ git+https://github.com/Eclectic-Sheep/sheeprl.git" +pip install "sheeprl[atari,box2d,mujoco,dev,test] @ git+https://github.com/Eclectic-Sheep/sheeprl.git" ``` diff --git a/pyproject.toml b/pyproject.toml index 70ca3cd8..177875ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ create = true in-project = true [build-system] -requires = ["setuptools >= 61.0.0", "swig==4.*"] +requires = ["setuptools >= 61.0.0"] build-backend = "setuptools.build_meta" [project] @@ -27,7 +27,8 @@ readme = { file = "docs/README.md", content-type = "text/markdown" } requires-python = ">=3.8,<3.11" classifiers = ["Programming Language :: Python", "Topic :: Scientific/Engineering :: Artificial Intelligence"] dependencies = [ - "gymnasium[box2d]==0.29.*", + "gymnasium==0.29.*", + "pygame >=2.1.3", "moviepy>=1.0.3", "tensordict==0.2.*", "tensorboard>=2.10", @@ -72,6 +73,7 @@ dev = [ ] mujoco = ["mujoco>=2.3.3", "gymnasium[mujoco]==0.29.*"] dmc = ["dm_control>=1.0.12"] +box2d = ["gymnasium[box2d]==0.29.*"] atari = [ "gymnasium[atari]==0.29.*", "gymnasium[accept-rom-license]==0.29.*", diff --git a/sheeprl/__init__.py b/sheeprl/__init__.py index d4e03836..5982b5a5 100644 --- a/sheeprl/__init__.py +++ b/sheeprl/__init__.py @@ -2,6 +2,7 @@ ROOT_DIR = os.path.dirname(__file__) +import decorator from dotenv import load_dotenv load_dotenv() @@ -50,3 +51,36 @@ np.bool = bool __version__ = "0.4.6dev1" + + +# Replace `moviepy.decorators.use_clip_fps_by_default` method to work with python 3.8, 3.9, and 3.10 +import moviepy.decorators + + +# Taken from https://github.com/Zulko/moviepy/blob/master/moviepy/decorators.py#L118 +@decorator.decorator +def custom_use_clip_fps_by_default(func, clip, *args, **kwargs): + """Will use ``clip.fps`` if no ``fps=...`` is provided in **kwargs**.""" + import inspect + + def find_fps(fps): + if fps is not None: + return fps + elif getattr(clip, "fps", None): + return clip.fps + raise AttributeError( + "No 'fps' (frames per second) attribute specified" + " for function %s and the clip has no 'fps' attribute. Either" + " provide e.g. fps=24 in the arguments of the function, or define" + " the clip's fps with `clip.fps=24`" % func.__name__ + ) + + names = inspect.getfullargspec(func).args[1:] + + new_args = [find_fps(arg) if (name == "fps") else arg for (arg, name) in zip(args, names)] + new_kwargs = {kwarg: find_fps(value) if kwarg == "fps" else value for (kwarg, value) in kwargs.items()} + + return func(clip, *new_args, **new_kwargs) + + +moviepy.decorators.use_clip_fps_by_default = custom_use_clip_fps_by_default diff --git a/sheeprl/utils/imports.py b/sheeprl/utils/imports.py index 745a2cb4..0e0a9f22 100644 --- a/sheeprl/utils/imports.py +++ b/sheeprl/utils/imports.py @@ -4,6 +4,7 @@ _IS_ATARI_AVAILABLE = RequirementCache("gymnasium[atari]") _IS_ATARI_ROMS_AVAILABLE = RequirementCache("gymnasium[accept-rom-license]") +_IS_BOX2D_AVAILABLE = RequirementCache("gymnasium[box2d]") _IS_CRAFTER_AVAILABLE = RequirementCache("crafter") _IS_DIAMBRA_AVAILABLE = RequirementCache("diambra") _IS_DIAMBRA_ARENA_AVAILABLE = RequirementCache("diambra-arena")