2019-10-11 23:58:04 +02:00
from gym import ObservationWrapper
2021-07-28 22:21:47 -04:00
import warnings
2019-10-11 23:58:04 +02:00
class TransformObservation ( ObservationWrapper ) :
2021-07-29 02:26:34 +02:00
r """ Transform the observation via an arbitrary function.
2019-10-11 23:58:04 +02:00
Example : :
>> > import gym
>> > env = gym . make ( ' CartPole-v1 ' )
>> > env = TransformObservation ( env , lambda obs : obs + 0.1 * np . random . randn ( * obs . shape ) )
>> > env . reset ( )
array ( [ - 0.08319338 , 0.04635121 , - 0.07394746 , 0.20877492 ] )
Args :
env ( Env ) : environment
f ( callable ) : a function that transforms the observation
"""
2021-07-29 02:26:34 +02:00
2019-10-11 23:58:04 +02:00
def __init__ ( self , env , f ) :
super ( TransformObservation , self ) . __init__ ( env )
assert callable ( f )
2021-07-28 22:21:47 -04:00
warnings . warn ( " Gym \' s internal preprocessing wrappers are now deprecated. While they will continue to work for the foreseeable future, we strongly recommend using SuperSuit instead: https://github.com/PettingZoo-Team/SuperSuit " )
2019-10-11 23:58:04 +02:00
self . f = f
def observation ( self , observation ) :
return self . f ( observation )