socialfoundations / whynot

A Python sandbox for decision making in dynamics
MIT License
418 stars 43 forks source link

Gym registry issue prevents import of whynot #26

Open milagorecki opened 2 years ago

milagorecki commented 2 years ago

Hi,

I just started looking into that repo and went into troubles when trying to import the package in Python. I set up a new virtual environment with only whynot and dependencies installed (Python 3.8.14). Then, when running import whynot as wn, I get the following error:

ImportError Traceback (most recent call last) Cell In [2], line 4 1 import matplotlib.pyplot as plt 2 import numpy as np ----> 4 import whynot as wn 5 import whynot.gym as gym 7 import scripts.utils as utils

File ~/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/whynot/init.py:5 3 version = "0.12.0" 4 from whynot.algorithms import ----> 5 from whynot.simulators import 6 from whynot import causal_graphs, dynamics, framework 7 from whynot.dynamics import ( 8 DynamicsExperiment, 9 Run, 10 )

File ~/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/whynot/simulators/init.py:4 1 """Simulator initialization.""" 3 from whynot.simulators import civil_violence ----> 4 from whynot.simulators import credit 5 from whynot.simulators import delayed_impact 6 from whynot.simulators import dice

File ~/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/whynot/simulators/credit/init.py:15 3 from whynot.simulators.credit.simulator import ( 4 agent_model, 5 Config, (...) 10 State, 11 ) 13 from whynot.simulators.credit.dataloader import CreditData ---> 15 from whynot.simulators.credit.environments import * 17 SUPPORTS_CAUSAL_GRAPHS = True

File ~/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/whynot/simulators/credit/environments.py:4 1 """Interactive environment for the credit simulator.""" 2 import numpy as np ----> 4 from whynot.gym import spaces 5 from whynot.gym.envs import ODEEnvBuilder, register 6 from whynot.simulators.credit import ( 7 agent_model, 8 CreditData, (...) 13 State, 14 )

File ~/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/whynot/gym/init.py:12 9 from gym.core import Env 10 from gym import logger ---> 12 from whynot.gym.envs import make, spec, register 14 all = ["Env", "make", "spec", "register"]

File ~/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/whynot/gym/envs/init.py:3 1 """Environments based on WhyNot simulators.""" ----> 3 from whynot.gym.envs.registration import registry, register, make, spec 4 from whynot.gym.envs.ode_env import ODEEnvBuilder

File ~/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/whynot/gym/envs/registration.py:4 1 """Global registry of environments, for consistency with openai gym.""" 2 import importlib ----> 4 from gym.envs.registration import EnvRegistry 6 # Keep for consistency with original API 7 # pylint:disable-msg=invalid-name 8 # Have a global registry 9 registry = EnvRegistry()

ImportError: cannot import name 'EnvRegistry' from 'gym.envs.registration' (/Users/.../opt/miniconda3/envs/testenv/lib/python3.8/site-packages/gym/envs/registration.py)

Because this seems to be related to gym (by default version 0.26.2 got installed), I tried out different gym versions and indeed with gym version 0.23.0 or lower the import works. So maybe, a quick-fix could be to restrict the gym version. It seems that from version 0.24.0 onwards, the gym registry module/API got changed causing an issue when importing whynot.

Having gym 0.23.0 installed, allows me to run the performative prediction example, which is what I'm currently interested in. However, in the walkthrough example there still occurs an error when running the simulator of world3:

dataset = experiment.run(num_samples=200, seed=1234, show_progress=True)

RuntimeError: Native library not available at /Users/mgorecki/opt/miniconda3/envs/testenv/lib/python3.8/site-packages/py_mini_racer/libmini_racer.dylib

I couldn't resolve that so far.

mhrnz commented 1 year ago

Hello,

I also had this issue while importing whynot and got the same import error for EnvRegistry in Gym. Thanks to what @milagorecki suggested, installing the gym version 0.23.0 fixed the issue for me, however, it would be nice if you could make changes in the code accordingly to resolve this issue so others won't get stuck.

Thanks!

mrtzh commented 1 year ago

As @milagorecki said, to fix this issue adequately, we'd have to make whynot consistent with the current gym versions that have changed. We'd love to have help with that and I'd greatly appreciate any contributions from the community.

marimeireles commented 1 year ago

hey! awesome package! It's exactly what I was aiming to develop so I'm happy I found it. I'll spend some time trying to update it, so I can take this issue. But first I'd like to set-up a few things so it's usable again by the broader community. :) Cheers!

marimeireles commented 1 year ago

The PR fixes this issue in the sense that's now using gym 0.23 by default. However, instead of working on further versions of gym, my preferred course of action would be working on compatibility for gymnasium. gym was discontinued by OpenAI and gymanasium (its continuation) was picked up by Farama.

mrtzh commented 1 year ago

Thanks for your contribution!