def testQuery1DTensorHistoryWithImagePngMapping(self): store = tensor_store.TensorStore() watch_key = "A:0:DebugIdentity" store.add(watch_key, np.array([0, 2, 4, 6, 8])) store.add(watch_key, np.array([1, 3, 5, 7, 9])) output = store.query(watch_key, time_indices=":", mapping="image/png") decoded = im_util.decode_png(base64.b64decode(output)) self.assertEqual((2, 5, 3), decoded.shape)
def testConvertHealthy2DNestedList(self): x = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] encoded_x = tensor_helper.array_to_base64_png(x) decoded_x = im_util.decode_png(base64.b64decode(encoded_x)) self.assertEqual((4, 4, 3), decoded_x.shape) decoded_flat = decoded_x.flatten() self.assertEqual(0, np.min(decoded_flat)) self.assertEqual(255, np.max(decoded_flat))
def testConvertHealthy2DArray(self): x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) encoded_x = tensor_helper.array_to_base64_png(x) decoded_x = im_util.decode_png(base64.b64decode(encoded_x)) self.assertEqual((3, 3, 3), decoded_x.shape) decoded_flat = decoded_x.flatten() self.assertEqual(0, np.min(decoded_flat)) self.assertEqual(255, np.max(decoded_flat))
def testImagePngMappingWorksForArrayWithOnlyOneElement(self): x = np.array([[-42]], dtype=np.int16) dtype, shape, data = tensor_helper.array_view(x, mapping="image/png") self.assertEqual("int16", dtype) self.assertEqual((1, 1), shape) decoded_x = im_util.decode_png(base64.b64decode(data)) self.assertEqual((1, 1, 3), decoded_x.shape) self.assertEqual(np.uint8, decoded_x.dtype) self.assertAllClose(np.zeros([1, 1, 3]), decoded_x)
def testArrayView2DWithSlicingAndImagePngMapping(self): x = np.ones([15, 16], dtype=np.int32) dtype, shape, data = tensor_helper.array_view(x, slicing="[:15:3, :16:2]", mapping="image/png") self.assertEqual("int32", dtype) self.assertEqual((5, 8), shape) decoded_x = im_util.decode_png(base64.b64decode(data)) self.assertEqual((5, 8, 3), decoded_x.shape) self.assertEqual(np.uint8, decoded_x.dtype) self.assertAllClose(np.zeros([5, 8, 3]), decoded_x)
def testImagePngMappingWorksForArrayWithOnlyInfAndNaN(self): x = np.array([[np.nan, -np.inf], [np.inf, np.nan]], dtype=np.float32) dtype, shape, data = tensor_helper.array_view(x, mapping="image/png") self.assertEqual("float32", dtype) self.assertEqual((2, 2), shape) decoded_x = im_util.decode_png(base64.b64decode(data)) self.assertEqual((2, 2, 3), decoded_x.shape) self.assertEqual(np.uint8, decoded_x.dtype) self.assertAllClose(tensor_helper.NAN_RGB, decoded_x[0, 0, :]) # nan. self.assertAllClose(tensor_helper.NEGATIVE_INFINITY_RGB, decoded_x[0, 1, :]) # -infinity. self.assertAllClose(tensor_helper.POSITIVE_INFINITY_RGB, decoded_x[1, 0, :]) # +infinity. self.assertAllClose(tensor_helper.NAN_RGB, decoded_x[1, 1, :]) # nan.