<jupyter_start><jupyter_text>Gym Environment ExampleBasic run of a gym environment to collect and plot traces.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import gym

# Disable scientific printing
np.set_printoptions(threshold=10000, suppress=True, precision=5, linewidth=180)

env = gym.make('CartPole-v1')
print("Observation:")
print(env.observation_space)
print("Action:")
print(env.action_space)

ep_obs = list()

for k in range(2):
  obs = list()
  observation: np.ndarray = env.reset()
  obs.append(observation)
  for t in range(100):
    # env.render()
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)
    obs.append(observation)
    if done:
      print(f"Episode {k} finished after {t+1} timesteps")
      break
  # Curate episode observations
  obs = pd.DataFrame(obs, columns=['Cart Position', 'Cart Velocity', 'Pole Angle', 'Pole Tip Vel'])
  obs['Episode'] = k
  obs['Time Step'] = np.arange(len(obs))
  ep_obs.append(obs)
env.close()

ep_obs = pd.concat(ep_obs)
ep_obs.sample(5)
melted_ep = pd.melt(ep_obs, id_vars=['Episode', 'Time Step'],
                 value_vars=['Cart Position', 'Cart Velocity', 'Pole Angle', 'Pole Tip Vel'],
                 var_name='Observation',
                 value_name='Value')
melted_ep['Type'] = np.where(melted_op['Observation'])
melted_ep.sample(5)
g = sns.relplot(x='Time Step', y='Value', hue='Observation', col='Episode', data=melted_ep)
g.savefig("example_plot.pdf", bbox_inches='tight')
melted_ep['Observation'].map()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1445409429280397, 'fraction_numerical': 0.01054590570719603, 'mean_word_length': 4.760714285714286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '642608', 'license': 'MIT', 'n_tokens_mistral': 578, 'n_tokens_neox': 567, 'n_words': 130, 'path': 'example.ipynb', 'repo_name': 'nuric/pix2rule', 'size': '1612'}