def test_multi(self): writer = prt.CustomInputLayer(multi_output=True) output = prt.OutputLayer(writer) out_a = prt.OutputLayer(transform_func(writer.get_port('a'))) out_b = prt.OutputLayer(transform_func(writer.get_port('b'))) prt.LayerManager.session().start() data = {'a': 1, 'b': 2} writer.supply_input(data) self.assertEqual(output.get_output(), data) self.assertEqual(out_a.get_output(), data['a'] + 1) self.assertEqual(out_b.get_output(), data['b'] + 1) writer.supply_input(prt.LayerSignal.STOP)
def test_passthrough(self): writer = prt.CustomInputLayer() output = prt.OutputLayer(writer) prt.LayerManager.session().start() for i in range(5): writer.supply_input(i) for i in range(5): self.assertEqual(output.get_output(), i) writer.supply_input(prt.LayerSignal.STOP)
def test_binary_read2(self): """Test ByteSerialReadLayer""" reader = prt.ByteSerialReadLayer.from_port(self.s, num_bytes=2) output = prt.OutputLayer(reader) prt.LayerManager.session().start() self.s.write(b'1234') time.sleep(.1) prt.LayerManager.session().stop() self.assertEqual(output.get_output(), b'12') self.assertEqual(output.get_output(), b'34')
def test_read(self): """Test SerialReadLayer""" reader = prt.SerialReadLayer.from_port(self.s, decoder='utf-8') output = prt.OutputLayer(reader) prt.LayerManager.session().start() self.s.write("test1\n".encode("UTF-8")) self.s.write("test2\n".encode("UTF-8")) time.sleep(.1) prt.LayerManager.session().stop() self.assertEqual(output.get_output(), 'test1\n') self.assertEqual(output.get_output(), 'test2\n')
def test_dec_fun(self): """Test transformer""" writer = prt.CustomInputLayer() output = prt.OutputLayer(transform_func(writer)) prt.LayerManager.session().start() writer.supply_input(1) writer.supply_input(5) time.sleep(.1) writer.supply_input(prt.LayerSignal.STOP) self.assertEqual(output.get_output(), 2) self.assertEqual(output.get_output(), 6)
def test_fixedbuffer2d(self): writer = prt.CustomInputLayer(buffer=prt.FixedBuffer(buffer_size=2, use_np=True, shape=(3,))) output = prt.OutputLayer(writer) prt.LayerManager.session().start() for i in range(4): writer.supply_input([i, i + .01, i + .02]) writer.supply_input([i + .1, i + .11, i + .12]) for i in range(4): o = output.get_output() np.testing.assert_equal(o, [[i, i + .01, i + .02], [i + .1, i + .11, i + .12]]) writer.supply_input(prt.LayerSignal.STOP)
def test_fixedbuffer1d(self): writer = prt.CustomInputLayer(buffer=prt.FixedBuffer(buffer_size=3, use_np=True)) output = prt.OutputLayer(writer) prt.LayerManager.session().start() for i in range(4): writer.supply_input(np.array([i])) writer.supply_input(np.array([i + .1])) writer.supply_input(np.array([i + .2])) for i in range(4): np.testing.assert_equal(output.get_output(), np.atleast_2d([i, i + .1, i + .2]).T) writer.supply_input(prt.LayerSignal.STOP)
def test_fixedbuffer(self): writer = prt.CustomInputLayer(buffer=prt.FixedBuffer(buffer_size=3)) output = prt.OutputLayer(writer) prt.LayerManager.session().start() for i in range(4): writer.supply_input(i) writer.supply_input(i + .1) writer.supply_input(i + .2) for i in range(4): self.assertEqual(output.get_output(), [i, i + .1, i + .2]) writer.supply_input(prt.LayerSignal.STOP)
def test_fixedbuffer3d(self): writer = prt.CustomInputLayer(buffer=prt.FixedBuffer(buffer_size=20, use_np=True, shape=(3, 5))) output = prt.OutputLayer(writer) prt.LayerManager.session().start() data = [] for i in range(3): data.append(np.zeros((20, 3, 5))) for i in range(60): x = np.random.rand(3, 5) data[i // 20][i % 20] = x writer.supply_input(x) for x in data: np.testing.assert_equal(output.get_output(), x) writer.supply_input(prt.LayerSignal.STOP)