-
Notifications
You must be signed in to change notification settings - Fork 615
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(integrations): update gym integration to match last version #4571
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d230507
update gym integration to match last gym version
younik 5097359
change __del__ to del_
younik 06d5a7c
Merge branch 'main' of https://github.com/wandb/wandb
dmitryduev e699cd6
handle gym versions < 0.26
dmitryduev 8410b60
fix typo
dmitryduev 7f2b4d8
fix typo
dmitryduev 9ac6e24
lint
dmitryduev File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,53 @@ | ||
import re | ||
from typing import Optional | ||
|
||
import wandb | ||
|
||
_gym_version_lt_0_26: Optional[bool] = None | ||
|
||
|
||
def monitor(): | ||
vcr = wandb.util.get_module( | ||
"gym.wrappers.monitoring.video_recorder", | ||
required="Couldn't import the gym python package, install with pip install gym", | ||
required="Couldn't import the gym python package, install with `pip install gym`", | ||
) | ||
vcr.ImageEncoder.orig_close = vcr.ImageEncoder.close | ||
|
||
global _gym_version_lt_0_26 | ||
|
||
if _gym_version_lt_0_26 is None: | ||
import gym # type: ignore | ||
from pkg_resources import parse_version | ||
|
||
if parse_version(gym.__version__) < parse_version("0.26.0"): | ||
_gym_version_lt_0_26 = True | ||
else: | ||
_gym_version_lt_0_26 = False | ||
|
||
# breaking change in gym 0.26.0 | ||
vcr_recorder_attribute = "ImageEncoder" if _gym_version_lt_0_26 else "VideoRecorder" | ||
recorder = getattr(vcr, vcr_recorder_attribute) | ||
path = "output_path" if _gym_version_lt_0_26 else "path" | ||
|
||
recorder.orig_close = recorder.close | ||
|
||
def close(self): | ||
vcr.ImageEncoder.orig_close(self) | ||
m = re.match(r".+(video\.\d+).+", self.output_path) | ||
recorder.orig_close(self) | ||
m = re.match(r".+(video\.\d+).+", getattr(self, path)) | ||
if m: | ||
key = m.group(1) | ||
else: | ||
key = "videos" | ||
wandb.log({key: wandb.Video(self.output_path)}) | ||
wandb.log({key: wandb.Video(getattr(self, path))}) | ||
|
||
def del_(self): | ||
self.orig_close() | ||
|
||
vcr.ImageEncoder.close = close | ||
if not _gym_version_lt_0_26: | ||
recorder.__del__ = del_ | ||
recorder.close = close | ||
wandb.patched["gym"].append( | ||
["gym.wrappers.monitoring.video_recorder.ImageEncoder", "close"] | ||
[ | ||
f"gym.wrappers.monitoring.video_recorder.{vcr_recorder_attribute}", | ||
"close", | ||
] | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's verify this del_ behavior. other than that looks great!