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

Adding Gymnasium Compatibility #141

Merged
merged 50 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0d15ab6
gymnasium compatibility
Vittorio-Caggiano Dec 4, 2023
af13c63
fix simplified gym import
Vittorio-Caggiano Dec 12, 2023
e074db6
rewrite step totally oblivious to the gym version
Vittorio-Caggiano Dec 13, 2023
ad4e66a
update doc with gym/gymnasium aspecific import
Vittorio-Caggiano Dec 13, 2023
e899c85
fix test with gym/gymnasium
Vittorio-Caggiano Dec 13, 2023
8948b95
remove temp files
Vittorio-Caggiano Dec 13, 2023
74f2075
remove pin on
Vittorio-Caggiano Dec 13, 2023
8d8b5a5
add packaging dependency
Vittorio-Caggiano Dec 13, 2023
952287c
add missing fields for gym<=0.24
Vittorio-Caggiano Dec 13, 2023
875f907
fix step call for gym/gymnasium
Vittorio-Caggiano Dec 13, 2023
087f0ff
change reference to boading ball base env
Vittorio-Caggiano Dec 13, 2023
15bb47a
exclude mjrl tutorial
Vittorio-Caggiano Dec 13, 2023
af0c33b
add see for gymnasium
Vittorio-Caggiano Dec 14, 2023
c60740b
add explicitely seed
Vittorio-Caggiano Dec 14, 2023
4643149
move to gymnasium as default
Vittorio-Caggiano Dec 14, 2023
9a3aeb5
make the check not cyclical
Vittorio-Caggiano Dec 14, 2023
25300b5
show list packages
Vittorio-Caggiano Dec 14, 2023
67afdb6
update to gym/gymnasium
Vittorio-Caggiano Dec 17, 2023
374aa4f
add ci test for gym/gymnasium/stable-baselines3
Vittorio-Caggiano Dec 17, 2023
b089163
Merge branch 'dev' into add_gymnasium
Vittorio-Caggiano Dec 17, 2023
fe37e19
add ci test for gym/gymnasium/stable-baselines3
Vittorio-Caggiano Dec 17, 2023
0f9f3cb
add deprl package
Vittorio-Caggiano Dec 17, 2023
293b9b3
remove deprl test
Vittorio-Caggiano Dec 17, 2023
ca28e42
add gym/gymnasium/sb3 ci test
Vittorio-Caggiano Dec 17, 2023
8e03840
Reverting unwanted changes - mostly links and keywords
Dec 18, 2023
5c7f286
add gym/gymnasium/sb3 ci test
Vittorio-Caggiano Dec 18, 2023
d7305ea
restoring SAR tutorial
Vittorio-Caggiano Dec 18, 2023
606065c
clean up comments
Vittorio-Caggiano Dec 18, 2023
5e07c28
clean up comments
Vittorio-Caggiano Dec 18, 2023
cf6faf2
restore reference to robohive
Vittorio-Caggiano Dec 18, 2023
b3809b0
restore chasetag to latest version
Vittorio-Caggiano Dec 18, 2023
0451659
fix SAR tutorial
Vittorio-Caggiano Dec 18, 2023
e490086
remove promt as it triggers errors on mac on closing the session... t…
Vittorio-Caggiano Dec 18, 2023
8577a50
add control for checking missing gym/gymnasium
Vittorio-Caggiano Dec 18, 2023
b791669
add gym as startd requirement instead of gymnasium
Vittorio-Caggiano Dec 18, 2023
6d4c478
add test for sb for both gym and gymnasium
Vittorio-Caggiano Dec 18, 2023
a45eb97
add test sb3 with gym/gymnasium only on a smaller subset of tasks
Vittorio-Caggiano Dec 18, 2023
a48a242
replace gym with numpy random generator cc: @Pierre
Vittorio-Caggiano Dec 18, 2023
26107f8
test sb3 only with gymnasium
Vittorio-Caggiano Dec 18, 2023
1cec851
add torchrl script
Vittorio-Caggiano Dec 19, 2023
35a1827
update readme with torchrl info
Vittorio-Caggiano Dec 19, 2023
5efdf93
Update torchrl_job_script.py
vmoens Dec 19, 2023
2525133
Merge pull request #125 from MyoHub/add_torchrl
vmoens Dec 19, 2023
08a72a9
MAJOR: Gym/Gymnasium import. examine_sim upgraded to mujoco. hardcode…
vikashplus Dec 29, 2023
9cbac90
MINOR: Typo and minor update to readme
vikashplus Dec 29, 2023
8ea8f53
MINOR: Updating link
vikashplus Dec 29, 2023
bcf8bc4
FEATURE: Moving gym import to __init__ for a shorter import statement…
vikashplus Dec 29, 2023
d02006c
FEATURE: Moving gym import to __init__ for a shorter import statement
vikashplus Dec 29, 2023
0f89888
Finalizing gymnasium upgrade
vikashplus Jan 1, 2024
c47c536
Merge branch 'dev' into add_gymnasium
Vittorio-Caggiano Jan 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,9 @@ jobs:
- name: Run Test jupyter-notebooks tutorials
if: ${{ runner.os == 'Linux' }}
run: |
conda activate $CONDA_DEFAULT_ENV
source myosuite/tests/test_tutorials.sh
source myosuite/tests/test_tutorials.sh

- name: Run Test gym/gymnasium + stable-baselines3
if: ${{ runner.os == 'Linux' }}
run: |
source myosuite/tests/test_versions.sh
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ It is possible to create and interface with MyoSuite environments just like any


```python
import myosuite
import gym
from myosuite.utils import gym
env = gym.make('myoElbowPose1D6MRandom-v0')
env.reset()
for _ in range(1000):
Expand Down
3 changes: 1 addition & 2 deletions docs/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ For example, to use the ``myoElbowPose1D6MRandom-v0`` environment it is possible

.. code-block:: python

import myosuite
import gym
from myosuite.utils import gym
env = gym.make('myoElbowPose1D6MRandom-v0')
env.reset()
for _ in range(1000):
Expand Down
21 changes: 7 additions & 14 deletions docs/source/tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Example on how to use an environment e.g. send random movements

.. code-block:: python

import myosuite
import gym
from myosuite.utils import gym
env = gym.make('myoElbowPose1D6MRandom-v0')
env.reset()
for _ in range(1000):
Expand All @@ -43,8 +42,7 @@ Example on how to generate and visualize a movement e.g. index flexion, and visu

.. code-block:: python

import myosuite
import gym
from myosuite.utils import gym
env = gym.make('myoHandPoseRandom-v0')
env.reset()
for _ in range(1000):
Expand All @@ -60,8 +58,7 @@ Example on using a policy e.g. elbow flexion, and change non-stationaries

.. code-block:: python

import myosuite
import gym
from myosuite.utils import gym
policy = "iterations/best_policy.pickle"

import pickle
Expand All @@ -83,8 +80,7 @@ This example shows how to add fatigue to a model. It tests random actions on a m

.. code-block:: python

import myosuite
import gym
from myosuite.utils import gym
env = gym.make('myoElbowPose1D6MRandom-v0')
env.reset()
for _ in range(1000):
Expand All @@ -108,8 +104,7 @@ This example shows how to add sarcopenia or muscle weakness to a model. It tests

.. code-block:: python

import myosuite
import gym
from myosuite.utils import gym
env = gym.make('myoElbowPose1D6MRandom-v0')
env.reset()
for _ in range(1000):
Expand All @@ -134,8 +129,7 @@ This example shows how load a model with physical tendon transfer.

.. code-block:: python

import myosuite
import gym
from myosuite.utils import gym
env = gym.make('myoHandKeyTurnFixed-v0')
env.reset()
for _ in range(1000):
Expand Down Expand Up @@ -171,8 +165,7 @@ If you want to load and execute the pre-trained DEP-RL baseline. Make sure that

.. code-block:: python

import gym
import myosuite
from myosuite.utils import gym
import deprl

# we can pass arguments to the environments here
Expand Down
3 changes: 1 addition & 2 deletions docs/source/tutorials/1_Get_Started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
}
],
"source": [
"import myosuite\n",
"import gym\n",
"from myosuite.utils import gym\n",
"import skvideo.io\n",
"import numpy as np\n",
"import os"
Expand Down
5 changes: 2 additions & 3 deletions docs/source/tutorials/2_Load_policy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"import myosuite\n",
"import gym\n",
"from myosuite.utils import gym\n",
"import skvideo.io\n",
"import numpy as np\n",
"import os"
Expand Down Expand Up @@ -79,7 +78,7 @@
" frames.append(frame)\n",
" o = env.get_obs()\n",
" a = pi.get_action(o)[0]\n",
" next_o, r, done, ifo = env.step(a) # take an action based on the current observation\n",
" next_o, r, done, *_, ifo = env.step(a) # take an action based on the current observation\n",
"env.close()\n",
"\n",
"os.makedirs('videos', exist_ok=True)\n",
Expand Down
5 changes: 2 additions & 3 deletions docs/source/tutorials/3_Analyse_movements.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"import myosuite\n",
"import gym\n",
"from myosuite.utils import gym\n",
"import skvideo.io\n",
"import numpy as np\n",
"import os"
Expand Down Expand Up @@ -45,7 +44,7 @@
" for _ in range(100): # 100 samples for each episode\n",
" o = env.get_obs()\n",
" a = pi.predict(o)[0]\n",
" next_o, r, done, ifo = env.step(a) # take a random action\n",
" next_o, r, done, *_, ifo = env.step(a) # take a random action\n",
" \n",
" data_store.append({\"action\":a.copy(), \n",
" \"jpos\":env.sim.data.qpos.copy(), \n",
Expand Down
5 changes: 2 additions & 3 deletions docs/source/tutorials/4_Train_policy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"import myosuite\n",
"import gym\n",
"from myosuite.utils import gym\n",
"import skvideo.io\n",
"import numpy as np\n",
"import os"
Expand Down Expand Up @@ -122,7 +121,7 @@
" frames.append(frame[::-1,:,:])\n",
" o = env.get_obs()\n",
" a = pi.get_action(o)[0]\n",
" next_o, r, done, ifo = env.step(a) # take an action based on the current observation\n",
" next_o, r, done, *_, ifo = env.step(a) # take an action based on the current observation\n",
"env.close()\n",
"\n",
"os.makedirs('videos', exist_ok=True)\n",
Expand Down
17 changes: 4 additions & 13 deletions docs/source/tutorials/4a_deprl.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
}
],
"source": [
"\n",
"import gym, myosuite\n",
"import deprl\n",
"import gym"
"from myosuite.utils import gym\n",
"import deprl"
]
},
{
Expand Down Expand Up @@ -75,10 +73,10 @@
"source": [
"policy = deprl.load_baseline(env)\n",
"\n",
"obs = env.reset()\n",
"obs = env.reset()[0]\n",
"for _ in range(T):\n",
" action = policy(obs)\n",
" obs, rew, done, info = env.step(action)\n",
" obs, rew, done, *_, info = env.step(action)\n",
" if done:\n",
" break\n",
"env.close()\n",
Expand All @@ -95,13 +93,6 @@
"source": [
"If you want to load your own policy, replace `deprl.load_baseline(env)` with `deprl.load(path, env)`. This will automatically load it for you. We also provide more advanced commands like `python -m deprl.play --path /folder/` to setup a visualization script."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
5 changes: 2 additions & 3 deletions docs/source/tutorials/4c_Train_SB_policy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"import myosuite\n",
"import gym\n",
"from myosuite.utils import gym\n",
"import skvideo.io\n",
"import numpy as np\n",
"import os"
Expand Down Expand Up @@ -92,7 +91,7 @@
" frames.append(frame[::-1,:,:])\n",
" o = env.get_obs()\n",
" a = pi.predict(o)[0]\n",
" next_o, r, done, ifo = env.step(a) # take an action based on the current observation\n",
" next_o, r, done, *_, ifo = env.step(a) # take an action based on the current observation\n",
"env.close()\n",
"\n",
"os.makedirs('videos', exist_ok=True)\n",
Expand Down
3 changes: 1 addition & 2 deletions docs/source/tutorials/5_Move_Hand_Fingers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"import myosuite\n",
"import gym\n",
"from myosuite.utils import gym\n",
"import skvideo.io\n",
"import numpy as np\n",
"import os"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@
"cell_type": "code",
"execution_count": null,
"id": "8526f610",
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"# !pip install stable-baselines3==1.7.0\n",
"# !pip install joblib\n",
"# !pip install scikit-learn\n",
"# !pip install tqdm\n",
"# !pip install matplotlib"
"# !pip install matplotlib\n",
"# !pip install gymnasium"
]
},
{
Expand Down Expand Up @@ -220,7 +219,7 @@
" o = env.get_obs()\n",
" o = vec.normalize_obs(o)\n",
" a, __ = model.predict(o, deterministic=False)\n",
" next_o, r, done, info = env.step(a)\n",
" next_o, r, done, *_, info = env.step(a)\n",
" rewards += r\n",
" preview_rewards.append(rewards)\n",
" reward_threshold = np.percentile(preview_rewards, percentile)\n",
Expand All @@ -236,14 +235,14 @@
" o = env.get_obs()\n",
" o = vec.normalize_obs(o)\n",
" a, __ = model.predict(o, deterministic=False)\n",
" next_o, r, done, info = env.step(a)\n",
" next_o, r, done, *_, info = env.step(a)\n",
" acts.append(env.sim.data.act.copy())\n",
" rewards += r\n",
"\n",
" if rewards > reward_threshold:\n",
" solved_acts.extend(acts)\n",
"\n",
" return solved_acts"
" return np.array(solved_acts)"
]
},
{
Expand Down Expand Up @@ -562,6 +561,16 @@
"muscle_data = get_activations(name='play_period', env_name='myoLegWalk-v0', seed='0', episodes=1000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39ee90ed-73c9-4b02-ba56-488d539c4993",
"metadata": {},
"outputs": [],
"source": [
"muscle_data.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -650,6 +659,16 @@
"show_video(f\"{video_name}.mp4\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88c68a0a-eede-4505-b800-5c0ccdd831bc",
"metadata": {},
"outputs": [],
"source": [
"gym.make(f'myoLeg{new_terrain}TerrainWalk-v0')"
]
},
{
"cell_type": "markdown",
"id": "9d10cb1e",
Expand Down Expand Up @@ -976,7 +995,8 @@
" if syn_nosyn:\n",
" env = SynNoSynWrapper(gym.make(test_env_name), ica, pca, normalizer, phi)\n",
" else:\n",
" env = SynergyWrapper(gym.make(test_env_name), ica, pca, normalizer, phi)\n",
" # env = SynergyWrapper(gym.make(test_env_name), ica, pca, normalizer, phi)\n",
" env = SynergyWrapper(gym.make(test_env_name), ica, pca, normalizer)\n",
" else:\n",
" env = gym.make(test_env_name)\n",
" env.reset()\n",
Expand All @@ -992,7 +1012,7 @@
" o = env.get_obs()\n",
" o = vec.normalize_obs(o)\n",
" a, __ = model.predict(o, deterministic=determ)\n",
" next_o, r, done, info = env.step(a)\n",
" next_o, r, done, *_, info = env.step(a)\n",
" is_solved.append(info['solved'])\n",
" \n",
" if sum(is_solved) > 0:\n",
Expand Down Expand Up @@ -1085,7 +1105,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.18"
}
},
"nbformat": 4,
Expand Down
8 changes: 4 additions & 4 deletions docs/source/tutorials/SAR/SAR_tutorial_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import myosuite
import gym
from myosuite.utils import gym

from stable_baselines3 import SAC, PPO
from stable_baselines3.common.vec_env import DummyVecEnv, VecNormalize
Expand Down Expand Up @@ -347,7 +346,8 @@ def get_vid(name, env_name, seed, episodes, video_name, determ=False,
if syn_nosyn:
env = SynNoSynWrapper(gym.make(env_name), ica, pca, normalizer, phi)
else:
env = SynergyWrapper(gym.make(env_name), ica, pca, normalizer, phi)
# env = SynergyWrapper(gym.make(env_name), ica, pca, normalizer, phi)
env = SynergyWrapper(gym.make(env_name), ica, pca, normalizer)
else:
env = gym.make(env_name)

Expand All @@ -372,7 +372,7 @@ def get_vid(name, env_name, seed, episodes, video_name, determ=False,
frame = env.sim.renderer.render_offscreen(width=640, height=480,camera_id=camera)
frames.append(frame)

next_o, r, done, info = env.step(a)
next_o, r, done, *_, info = env.step(a)
is_solved.append(info['solved'])

rs+=r
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
tonic:
after_training: ''
agent: deprl.custom_agents.dep_factory(3, deprl.custom_mpo_torch.TunedMPO())(replay=deprl.replays.buffers.Buffer(return_steps=3, batch_size=256, steps_between_batches=1000, batch_iterations=30, steps_before_batches=2e5))
before_training: ''
checkpoint: last
environment: deprl.environments.Gym('myoLegWalk-v0', scaled_actions=False)
environment_name: relocate_test
header: import deprl, gym
sequential: 10
name: myoLeg
parallel: 20
seed: 0
test_environment: null
trainer: deprl.custom_trainer.Trainer(steps=int(1e8), epoch_steps=int(2e5), save_steps=int(1e6))
env_args: {}
working_dir: 'baselines_DEPRL/myoLegWalk_20230514/myoLeg/'
mpo_args:
hidden_size: 1024
lr_actor: 3.53e-05
lr_critic: 6.081e-05
lr_dual: 0.00213

Loading