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 specs_from_gym_space(
      space,
      allow_infinite_box_bounds=False,
      min_value=None,
      max_value=None,
  ):
      import gym
  
      if isinstance(space, gym.spaces.Discrete):
          return dict(type="int", shape=(), num_values=space.n)
  
      elif isinstance(space, gym.spaces.MultiBinary):
          return dict(type="bool", shape=space.n)
  
      elif isinstance(space, gym.spaces.MultiDiscrete):
          if (space.nvec == space.nvec.item(0)).all():
              return dict(
                  type="int",
                  shape=space.nvec.shape,
                  num_values=space.nvec.item(0),
              )
          else:
              specs = dict()
              nvec = space.nvec.flatten()
              shape = "_".join(str(x) for x in space.nvec.shape)
              for n in range(nvec.shape[0]):
                  specs["gymmdc{}_{}".format(n, shape)] = dict(
                      type="int", shape=(), num_values=nvec[n]
                  )
              return specs
  
      elif isinstance(space, gym.spaces.Box):
          spec = dict(type="float", shape=space.shape)
  
          if (space.low == space.low.item(0)).all():
              _min_value = float(space.low.item(0))
              if _min_value > -1e6:
                  spec["min_value"] = _min_value
              else:
                  spec["min_value"] = min_value
          elif allow_infinite_box_bounds:
              _min_value = np.where(
                  space.low <= -1e6, -np.inf, space.low
              )
              spec["min_value"] = _min_value.astype(
                  util.np_dtype(dtype="float")
              )
          elif (space.low > -1e6).all():
              spec["min_value"] = space.low.astype(
                  util.np_dtype(dtype="float")
              )
          elif min_value is None:
              raise TensorforceError("Invalid infinite box bounds")
          else:
              _min_value = np.where(
                  space.low <= -1e6, min_value, space.low
              )
              spec["min_value"] = _min_value.astype(
                  util.np_dtype(dtype="float")
              )
  
          if spec is None:
              pass
          elif (space.high == space.high.item(0)).all():
              _max_value = float(space.high.item(0))
              if _max_value < 1e6:
                  spec["max_value"] = _max_value
              else:
                  spec["max_value"] = max_value
          elif allow_infinite_box_bounds:
              _max_value = np.where(
                  space.high >= 1e6, np.inf, space.high
              )
              spec["max_value"] = _max_value.astype(
                  util.np_dtype(dtype="float")
              )
          elif (space.high < 1e6).all():
              spec["max_value"] = space.high.astype(
                  util.np_dtype(dtype="float")
              )
          elif max_value is None:
              raise TensorforceError(
                  "OpenAIGym: Invalid infinite box bounds"
              )
          else:
              _max_value = np.where(
                  space.high >= 1e6, max_value, space.high
              )
              spec["max_value"] = _max_value.astype(
                  util.np_dtype(dtype="float")
              )
  
          if spec is None:
              specs = dict()
              low = space.low.flatten()
              high = space.high.flatten()
              shape = "_".join(str(x) for x in space.low.shape)
              for n in range(low.shape[0]):
                  spec = dict(type="float", shape=())
                  if low[n] > -1e6:
                      spec["min_value"] = float(low[n])
                  if high[n] < 1e6:
                      spec["max_value"] = float(high[n])
                  specs["gymbox{}_{}".format(n, shape)] = spec
              return specs
          else:
              return spec
  
      elif isinstance(space, gym.spaces.Tuple):
          specs = dict()
          n = 0
          for n, space in enumerate(space.spaces):
              spec = OpenAIGym.specs_from_gym_space(
                  space=space,
                  allow_infinite_box_bounds=allow_infinite_box_bounds,
              )
              if "type" in spec:
                  specs["gymtpl{}".format(n)] = spec
              else:
                  for name, spec in spec.items():
                      specs["gymtpl{}_{}".format(n, name)] = spec
          return specs
  
      elif isinstance(space, gym.spaces.Dict):
          specs = dict()
          for space_name, space in space.spaces.items():
              spec = OpenAIGym.specs_from_gym_space(
                  space=space,
                  allow_infinite_box_bounds=allow_infinite_box_bounds,
              )
              if "type" in spec:
                  specs[space_name] = spec
              else:
                  for name, spec in spec.items():
                      specs["{}_{}".format(space_name, name)] = spec
          return specs
  
      else:
          raise TensorforceError("Unknown Gym space.")
  
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 11 for calling the OpenAIGym obj's underlying member method code with example usage: obj.specs_from_gym_space(space, allow_infinite_box_bounds, min_value, max_value) and returns: dict, dict, dict, specs, specs, spec, specs, specs