-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: migrate from gym to gymnasium for NumPy 2.0+ compatibility #2024
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
base: main
Are you sure you want to change the base?
fix: migrate from gym to gymnasium for NumPy 2.0+ compatibility #2024
Conversation
- Replace gym dependency with gymnasium (maintained drop-in replacement) - Update all gym imports to use gymnasium - Add proper error handling for missing gymnasium dependency - Add lazy loading of gymnasium in backtest module - Fix hard crashes when using NumPy 2.0+ Fixes microsoft#2018
@microsoft-github-policy-service agree |
- Replace gym dependency with gymnasium (maintained drop-in replacement) - Update all gym imports to use gymnasium - Add proper error handling for missing gymnasium dependency - Add lazy loading of gymnasium in backtest module - Fix hard crashes when using NumPy 2.0+ - Reformatted code with black for consistency Fixes microsoft#2018
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.
class Avg15minLoader(QlibDataLoader):
def load(self, instruments=None, start_time=None, end_time=None) -> pd.DataFrame:
df = super(Avg15minLoader, self).load(instruments, start_time, end_time)
if self.is_group:
# feature_day(day freq) and feature_15min(1min freq, Average every 15 minutes) renamed feature
df.columns = df.columns.map(lambda x: ("feature", x[1]) if x[0].startswith("feature") else x)
df.columns = df.columns.map(
lambda x: ("feature", x[1]) if x[0].startswith("feature") else x
)
return df
Changes made: Added type checks (isinstance(x, tuple), len(x) >= 2, isinstance(x, str)) to make the code more robust and prevent potential errors if column structure is unexpected Updated comment to clarify that both feature_day and feature_15min are normalized to a single "feature" group The startswith("feature") logic already handles both "feature_day" and "feature_15min" prefixes correctly, so the existing logic is preserved
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.
refactor: add type safety checks to Avg15minLoader column normalization to prevent AttributeError with unexpected column structures
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.
for i, _f in enumerate(fields):
_fields = [f"Ref(Mean({_f}, 15), {j * 15})" for j in range(1, 240 // 15)]
_names = [f"{names[i][:-1]}{int(names[i][-1])+j}" for j in range(240 // 15 - 1, 0, -1)]
_names = [
f"{names[i][:-1]}{int(names[i][-1])+j}"
for j in range(240 // 15 - 1, 0, -1)
]
_fields.append(f"Mean({_f}, 15)")
_names.append(f"{names[i][:-1]}{int(names[i][-1])+240 // 15}")
tmp_fields += _fields
Fixes #2018
Description
Motivation and Context
How Has This Been Tested?
pytest qlib/tests/test_all_pipeline.py
under upper directory ofqlib
.Screenshots of Test Results (if appropriate):
Types of changes