diff --git a/docs/scripts/gen_mds.py b/docs/scripts/gen_mds.py index 3b3abfb3d..c8b0c11fd 100644 --- a/docs/scripts/gen_mds.py +++ b/docs/scripts/gen_mds.py @@ -1,177 +1,94 @@ -__author__ = "Sander Schulhoff" -__email__ = "sanderschulhoff@gmail.com" - import os import re -from functools import reduce +from collections import defaultdict -import numpy as np -from tqdm import tqdm +from docs.scripts.utils import trim import gymnasium as gym -from utils import kill_strs, trim +from gymnasium.envs.registration import find_highest_version, get_env_id -LAYOUT = "env" +filtered_envs = defaultdict(list) +exclude_env_names = [ + "GymV21Environment", + "GymV26Environment", + "FrozenLake8x8", + "LunarLanderContinuous", + "BipedalWalkerHardcore", + "CartPoleJax", + "PendulumJax", + "Jax-Blackjack", +] +for env_spec in gym.registry.values(): + if env_spec.name not in exclude_env_names: + highest_version = find_highest_version(env_spec.namespace, env_spec.name) + env_id = get_env_id(env_spec.namespace, env_spec.name, highest_version) -pattern = re.compile(r"(? filtered_envs_by_type[env_type][env_name].version + env_spec = gym.spec(env_id) + if ( + isinstance(env_spec.entry_point, str) + and "gymnasium" in env_spec.entry_point ): - filtered_envs_by_type[env_type][env_name] = env_spec + env_module = env_spec.entry_point.split(".")[2] - except Exception as e: - print(e) + if env_spec not in filtered_envs[env_module]: + filtered_envs[env_module].append(env_spec) -# Sort -filtered_envs = list( - reduce( - lambda s, x: s + x, - map( - lambda arr: sorted(arr, key=lambda x: x.name), - map(lambda dic: list(dic.values()), list(filtered_envs_by_type.values())), - ), - [], - ) -) +# print(filtered_envs.keys()) +for env_module, env_specs in filtered_envs.items(): + env_module_name = env_module.replace("_", " ").title() + print(env_module_name) + env_specs = sorted(env_specs, key=lambda spec: spec.name) + for i, env_spec in enumerate(env_specs): + print(f"\t{i=}, {env_spec.name}") + env = gym.make(env_spec) + env_docstring = trim(env.unwrapped.__doc__) + assert env_docstring -# Update Docs -for i, env_spec in tqdm(enumerate(filtered_envs)): - print("ID:", env_spec.id) - env_type = env_spec.entry_point.split(".")[2] - try: - env = gym.make(env_spec.id) + snake_env_name = re.sub(r"(?{env_type_title} environments." - + "Please read that page first for general information." - ) - env_table = "| | |\n|---|---|\n" - env_table += f"| Action Space | {env.action_space} |\n" - - if env.observation_space.shape: - env_table += f"| Observation Shape | {env.observation_space.shape} |\n" - - if hasattr(env.observation_space, "high"): - high = env.observation_space.high - - if hasattr(high, "shape"): - if len(high.shape) == 3: - high = high[0][0][0] - if env_type == "mujoco": - high = high[0] - high = np.round(high, 2) - high = str(high).replace("\n", " ") - env_table += f"| Observation High | {high} |\n" - - if hasattr(env.observation_space, "low"): - low = env.observation_space.low - if hasattr(low, "shape"): - if len(low.shape) == 3: - low = low[0][0][0] - if env_type == "mujoco": - low = low[0] - low = np.round(low, 2) - low = str(low).replace("\n", " ") - env_table += f"| Observation Low | {low} |\n" - else: - env_table += f"| Observation Space | {env.observation_space} |\n" - - env_table += f'| Import | `gymnasium.make("{env_spec.id}")` | \n' - - if docstring is None: - docstring = "No information provided" - all_text = f"""{front_matter} -{title} - -{gif} - -{info} - -{env_table} - -{docstring} -""" - file = open(v_path, "w", encoding="utf-8") - file.write(all_text) + file = open(env_md_path, "w", encoding="utf-8") + file.write(env_page) file.close() - except Exception as e: - print(e)