Пример #1
0
def test_type_based_converter():
    converter = py_converters.TypeBasedConverter(
        conversion_map={FloatTensor: py_converters.convert_tensor_message_to_numpy}
    )
    tensor_message = FloatTensor(shape=[1], value=[1])
    numpy_array = converter(tensor_message)
    assert isinstance(numpy_array, np.ndarray)
Пример #2
0
def test_event_message_default_converter():
    message_converter = py_converters.TypeBasedConverter(
        conversion_map={FloatTensor: py_converters.convert_tensor_message_to_numpy}
    )
    event_converter = py_converters.EventMessageDefaultConverter(message_converter)
    tensor_message = FloatTensor(shape=[1], value=[1])
    event_message = Event(float_tensor=tensor_message)
    numpy_array = event_converter(event_message)
    assert isinstance(numpy_array, np.ndarray)
Пример #3
0
def test_to_dict_event_message_converter():
    converter = py_converters.TypeBasedConverter(
        conversion_map={int: lambda x: Event(int64_value=x)}
    )
    dict_converter = py_converters.ToDictEventMessageConverter(converter)
    original_dict = {"a": 1}
    converted_dict = dict_converter(original_dict)
    assert isinstance(converted_dict, DictEvent)
    assert len(converted_dict.event) == len(original_dict)
    assert converted_dict.event["a"].int64_value == original_dict["a"]
Пример #4
0
def test_to_list_event_message_converter():
    converter = py_converters.TypeBasedConverter(
        conversion_map={int: lambda x: Event(int64_value=x)}
    )
    list_converter = py_converters.ToListEventMessageConverter(converter)
    original_list = [1, 2]
    converted_list = list_converter(original_list)
    assert isinstance(converted_list, ListEvent)
    assert len(converted_list.event) == len(original_list)
    assert converted_list.event[0].int64_value == original_list[0]
    assert converted_list.event[1].int64_value == original_list[1]
Пример #5
0
def test_space_message_default_converter():
    message_converter = py_converters.TypeBasedConverter(
        conversion_map={StringSpace: py_converters.convert_sequence_space}
    )
    space_converter = py_converters.SpaceMessageDefaultConverter(message_converter)
    val = StringSpace(length_range=Int64Range(min=1, max=2))
    space_message = Space(string_value=val)
    converted_space = space_converter(space_message)
    assert isinstance(converted_space, Sequence)
    assert converted_space.dtype == str
    assert converted_space.size_range[0] == 1
    assert converted_space.size_range[1] == 2
Пример #6
0
def test_to_list_space_message_converter():
    to_message_converter = py_converters.TypeBasedConverter(
        conversion_map={Discrete: py_converters.convert_to_discrete_space_message}
    )
    to_space_converter = py_converters.ToSpaceMessageConverter(to_message_converter)
    to_list_converter = py_converters.ToListSpaceMessageConverter(to_space_converter)
    space = Tuple(name=None, spaces=[Discrete(name=None, n=5)])
    converted_space = to_list_converter(space)
    assert isinstance(converted_space, ListSpace)
    assert len(converted_space.space) == 1
    assert isinstance(converted_space.space[0], Space)
    assert hasattr(converted_space.space[0], "discrete")
    assert converted_space.space[0].discrete.n == 5
Пример #7
0
def test_list_event_message_converter():
    message_converter = py_converters.TypeBasedConverter(
        conversion_map={FloatTensor: py_converters.convert_tensor_message_to_numpy}
    )
    event_converter = py_converters.EventMessageDefaultConverter(message_converter)
    list_converter = py_converters.ListEventMessageConverter(event_converter)
    tensor_message = FloatTensor(shape=[1], value=[1])
    event_message = Event(float_tensor=tensor_message)
    list_message = ListEvent(event=[event_message])
    converted_list = list_converter(list_message)
    assert isinstance(converted_list, Collection)
    assert len(converted_list) == 1
    assert isinstance(converted_list[0], np.ndarray)
Пример #8
0
def test_to_dict_space_message_converter():
    to_message_converter = py_converters.TypeBasedConverter(
        conversion_map={Discrete: py_converters.convert_to_discrete_space_message}
    )
    to_space_converter = py_converters.ToSpaceMessageConverter(to_message_converter)
    to_dict_converter = py_converters.ToDictSpaceMessageConverter(to_space_converter)
    space = Dict(name=None, spaces={"key": Discrete(name=None, n=5)})
    converted_space = to_dict_converter(space)
    assert isinstance(converted_space, DictSpace)
    assert len(converted_space.space) == 1
    assert "key" in converted_space.space
    assert isinstance(converted_space.space["key"], Space)
    assert hasattr(converted_space.space["key"], "discrete")
    assert converted_space.space["key"].discrete.n == 5
Пример #9
0
def test_dict_event_message_converter():
    message_converter = py_converters.TypeBasedConverter(
        conversion_map={FloatTensor: py_converters.convert_tensor_message_to_numpy}
    )
    event_converter = py_converters.EventMessageDefaultConverter(message_converter)
    dict_converter = py_converters.DictEventMessageConverter(event_converter)
    tensor_message = FloatTensor(shape=[1], value=[1])
    event_message = Event(float_tensor=tensor_message)
    dict_message = DictEvent(event={"event_message_key": event_message})
    converted_list = dict_converter(dict_message)
    assert isinstance(converted_list, Mapping)
    assert len(converted_list) == 1
    assert "event_message_key" in converted_list
    assert isinstance(converted_list["event_message_key"], np.ndarray)
Пример #10
0
def test_protobuf_any_converter():
    unpacker = py_converters.ProtobufAnyUnpacker(
        {"compiler_gym.FloatTensor": FloatTensor}
    )
    type_based_converter = py_converters.TypeBasedConverter(
        conversion_map={FloatTensor: py_converters.convert_tensor_message_to_numpy}
    )
    converter = py_converters.ProtobufAnyConverter(
        unpacker=unpacker, message_converter=type_based_converter
    )
    any_msg = any_pb2.Any()
    tensor_message = FloatTensor(shape=[1], value=[1])
    any_msg.Pack(tensor_message)
    tensor = converter(any_msg)
    assert isinstance(tensor, np.ndarray)
Пример #11
0
def test_list_space_message_converter():
    message_converter = py_converters.TypeBasedConverter(
        conversion_map={StringSpace: py_converters.convert_sequence_space}
    )
    space_converter = py_converters.SpaceMessageDefaultConverter(message_converter)
    list_converter = py_converters.ListSpaceMessageConverter(space_converter)
    space_message = ListSpace(
        space=[
            Space(
                string_value=StringSpace(length_range=Int64Range(min=1, max=2)),
            )
        ]
    )
    converted_space = list_converter(space_message)
    assert isinstance(converted_space, Tuple)
    assert len(converted_space.spaces) == 1
    assert converted_space.spaces[0].dtype == str
    assert converted_space.spaces[0].size_range[0] == 1
    assert converted_space.spaces[0].size_range[1] == 2
Пример #12
0
def test_dict_space_message_converter():
    message_converter = py_converters.TypeBasedConverter(
        conversion_map={StringSpace: py_converters.convert_sequence_space}
    )
    space_converter = py_converters.SpaceMessageDefaultConverter(message_converter)
    dict_converter = py_converters.DictSpaceMessageConverter(space_converter)
    space_message = DictSpace(
        space={
            "key": Space(
                string_value=StringSpace(length_range=Int64Range(min=1, max=2)),
            )
        }
    )
    converted_space = dict_converter(space_message)
    assert isinstance(converted_space, Dict)
    assert len(converted_space.spaces) == 1
    assert "key" in converted_space.spaces
    assert converted_space.spaces["key"].dtype == str
    assert converted_space.spaces["key"].size_range[0] == 1
    assert converted_space.spaces["key"].size_range[1] == 2