class_docstr: [OpenAI Gym](https://gym.openai.com/) environment adapter (specification key: `gym`,
  `openai_gym`).
  
  May require:
  ```bash
  pip3 install gym
  pip3 install gym[all]
  ```
  
  Args:
      level (string | gym.Env): Gym id or instance
          (<span style="color:#C00000"><b>required</b></span>).
      visualize (bool): Whether to visualize interaction
          (<span style="color:#00C000"><b>default</b></span>: false).
      min_value (float): Lower bound clipping for otherwise unbounded state values
          (<span style="color:#00C000"><b>default</b></span>: no clipping).
      max_value (float): Upper bound clipping for otherwise unbounded state values
          (<span style="color:#00C000"><b>default</b></span>: no clipping).
      terminal_reward (float): Additional reward for early termination, if otherwise
          indistinguishable from termination due to maximum number of timesteps
          (<span style="color:#00C000"><b>default</b></span>: Gym default).
      reward_threshold (float): Gym environment argument, the reward threshold before the task is
          considered solved
          (<span style="color:#00C000"><b>default</b></span>: Gym default).
      drop_states_indices (list[int]): Drop states indices
          (<span style="color:#00C000"><b>default</b></span>: none).
      visualize_directory (string): Visualization output directory
          (<span style="color:#00C000"><b>default</b></span>: none).
      kwargs: Additional Gym environment arguments.
class_docstr_tok: ["[","OpenAI","Gym","]","(","https",":","\/\/gym.openai.com\/",")","environment","adapter","(","specification","key",":","`","gym","`",",","`","openai_gym","`",")",".","May","require",":","``","`","bash","pip3","install","gym","pip3","install","gym","[","all","]","``","`","Args",":","level","(","string","|","gym.Env",")",":","Gym","id","or","instance","(","<","span","style=","''","color",":","#","C00000","''",">","<","b",">","required","<","\/b",">","<","\/span",">",")",".","visualize","(","bool",")",":","Whether","to","visualize","interaction","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","false",")",".","min_value","(","float",")",":","Lower","bound","clipping","for","otherwise","unbounded","state","values","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","no","clipping",")",".","max_value","(","float",")",":","Upper","bound","clipping","for","otherwise","unbounded","state","values","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","no","clipping",")",".","terminal_reward","(","float",")",":","Additional","reward","for","early","termination",",","if","otherwise","indistinguishable","from","termination","due","to","maximum","number","of","timesteps","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","Gym","default",")",".","reward_threshold","(","float",")",":","Gym","environment","argument",",","the","reward","threshold","before","the","task","is","considered","solved","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","Gym","default",")",".","drop_states_indices","(","list","[","int","]",")",":","Drop","states","indices","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","none",")",".","visualize_directory","(","string",")",":","Visualization","output","directory","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","none",")",".","kwargs",":","Additional","Gym","environment","arguments","."]
code: def flatten_state(state, states_spec, actions_spec=None):
      if isinstance(state, tuple):
          states = dict()
          for n, state in enumerate(state):
              if "gymtpl{}".format(n) in states_spec:
                  spec = states_spec["gymtpl{}".format(n)]
              else:
                  spec = None
                  for name in states_spec:
                      if name.startswith("gymtpl{}_".format(n)):
                          assert spec is None
                          spec = states_spec[name]
                  assert spec is not None
              state = OpenAIGym.flatten_state(
                  state=state, states_spec=spec
              )
              if isinstance(state, dict):
                  for name, state in state.items():
                      states["gymtpl{}_{}".format(n, name)] = state
              else:
                  states["gymtpl{}".format(n)] = state
          return states
  
      elif isinstance(state, dict):
          states = dict()
          if actions_spec is not None:
              if "type" in actions_spec:
                  if "action_mask" in state:
                      states["action_mask"] = state.pop("action_mask")
              else:
                  for action_name in actions_spec:
                      action_name = "{}_mask".format(action_name)
                      if action_name in state:
                          states[action_name] = state.pop(action_name)
          for state_name, state in state.items():
              if state_name in states_spec:
                  spec = states_spec[state_name]
              else:
                  spec = None
                  for name in states_spec:
                      if name.startswith("{}_".format(state_name)):
                          assert spec is None
                          spec = states_spec[name]
                  assert spec is not None
              state = OpenAIGym.flatten_state(
                  state=state, states_spec=spec
              )
              if isinstance(state, dict):
                  for name, state in state.items():
                      states["{}_{}".format(state_name, name)] = state
              else:
                  states[state_name] = state
          return states
  
      elif np.isinf(state).any() or np.isnan(state).any():
          raise TensorforceError("State contains inf or nan.")
  
      elif "gymbox0" in states_spec:
          states = dict()
          state = state.flatten()
          shape = "_".join(str(x) for x in state.shape)
          for n in range(state.shape[0]):
              states["gymbox{}_{}".format(n, shape)] = state[n]
          return states
  
      elif "gymmdc0" in states_spec:
          states = dict()
          state = state.flatten()
          shape = "_".join(str(x) for x in state.shape)
          for n in range(state.shape[0]):
              states["gymmdc{}_{}".format(n, shape)] = state[n]
          return states
  
      else:
          return state
  
file_path: tensorforce/tensorforce/environments/openai_gym.py
imports: import importlib
  import numpy
  from tensorforce import TensorforceError, util
  from tensorforce.environments import Environment
label_desc: Use image node_id 12 for calling the OpenAIGym obj's underlying member method code with example usage: obj.flatten_state(state, states_spec, actions_spec) and returns: states, states, states, states, state