def test_normalize(): # can you pass a Space instance to the Space constructor? space = Space([(0.0, 1.0), (-5, 5), ("a", "b", "c"), (1.0, 5.0, "log-uniform"), ("e", "f")]) space.set_transformer("normalize") X = [[0.0, -5, "a", 1.0, "e"]] Xt = np.zeros((1, 5)) assert_array_equal(space.transform(X), Xt) assert_array_equal(space.inverse_transform(Xt), X) assert_array_equal(space.inverse_transform(space.transform(X)), X)
def test_normalize_types(): # can you pass a Space instance to the Space constructor? space = Space([(0.0, 1.0), Integer(-5, 5, dtype=int), (True, False)]) space.set_transformer("normalize") X = [[0.0, -5, False]] Xt = np.zeros((1, 3)) assert_array_equal(space.transform(X), Xt) assert_array_equal(space.inverse_transform(Xt), X) assert_array_equal(space.inverse_transform(space.transform(X)), X) assert isinstance(space.inverse_transform(Xt)[0][0], float) assert isinstance(space.inverse_transform(Xt)[0][1], int) assert isinstance(space.inverse_transform(Xt)[0][2], (np.bool_, bool))
def test_set_get_transformer(): # can you pass a Space instance to the Space constructor? space = Space([(0.0, 1.0), (-5, 5), ("a", "b", "c"), (1.0, 5.0, "log-uniform"), ("e", "f")]) transformer = space.get_transformer() assert_array_equal( ["identity", "identity", "onehot", "identity", "onehot"], transformer) space.set_transformer("normalize") transformer = space.get_transformer() assert_array_equal(["normalize"] * 5, transformer) space.set_transformer(transformer) assert_array_equal(transformer, space.get_transformer()) space.set_transformer_by_type("label", Categorical) assert space.dimensions[2].transform(["a"]) == [0]