def test_show_images_dict(self): htmls = [] with mock.patch('IPython.display.display', htmls.append): media.show_images({ 'title1': media.color_ramp(), 'title2': media.color_ramp() }) self.assertLen(htmls, 1) self.assertIsInstance(htmls[0], IPython.display.HTML) self.assertLen(re.findall('(?s)<table', htmls[0].data), 1) self.assertRegex(htmls[0].data, '(?s)title1.*<img .*title2.*<img ') self.assertLen(re.findall('(?s)<img', htmls[0].data), 2)
def test_show_images_over_multiple_rows(self): htmls = [] with mock.patch('IPython.display.display', htmls.append): media.show_images([media.color_ramp()] * 5, columns=2) self.assertLen(htmls, 1) self.assertIsInstance(htmls[0], IPython.display.HTML) self.assertLen(re.findall('(?s)<table', htmls[0].data), 3) self.assertLen(re.findall('(?s)<img', htmls[0].data), 5)
def test_show_save_image(self): with tempfile.TemporaryDirectory() as directory_name: with media.show_save.to_dir(directory_name): with mock.patch('IPython.display.display'): media.show_images({'ramp': media.color_ramp((128, 128))}) filename = os.path.join(directory_name, 'ramp.png') self.assertTrue(os.path.isfile(filename)) self.assertBetween(os.path.getsize(filename), 200, 1000)
def test_show_images_list(self): htmls = [] with mock.patch('IPython.display.display', htmls.append): media.show_images([media.color_ramp()] * 2) self.assertLen(htmls, 1) self.assertIsInstance(htmls[0], IPython.display.HTML) self.assertLen(re.findall('(?s)<table', htmls[0].data), 1) self.assertLen(re.findall('(?s)<img', htmls[0].data), 2)
def test_show_image(self): htmls = [] with mock.patch('IPython.display.display', htmls.append): media.show_image(media.color_ramp()) self.assertLen(htmls, 1) self.assertIsInstance(htmls[0], IPython.display.HTML) self.assertLen(re.findall('(?s)<table', htmls[0].data), 1) self.assertRegex(htmls[0].data, '(?s)<img width=[^<>]*/>') self.assertLen(re.findall('(?s)<img', htmls[0].data), 1)
def test_compress_decompress_image_roundtrip(self, dtype): image = media.color_ramp((27, 63), dtype=dtype) if dtype == 'uint16': # Unfortunately PIL supports only single-channel 16-bit images for now. image = image[..., 0] data = media.compress_image(image) new_image = media.decompress_image(data, dtype=dtype) self.assertEqual(image.shape, new_image.shape) self.assertEqual(image.dtype, new_image.dtype) self.assert_all_equal(image, new_image)
def test_image_write_read_roundtrip(self, dtype): image = media.color_ramp((27, 63), dtype=dtype) if dtype == 'uint16': # Unfortunately PIL supports only single-channel 16-bit images for now. image = image[..., 0] with tempfile.TemporaryDirectory() as directory_name: path = pathlib.Path(directory_name) / 'test.png' media.write_image(path, image) new_image = media.read_image(path, dtype=dtype) self.assert_all_equal(image.shape, new_image.shape) self.assertEqual(image.dtype, new_image.dtype) self.assert_all_equal(image, new_image)
def test_write_image(self): image = media.color_ramp(shape=(500, 500), dtype=np.uint8) np.random.seed(1) image += np.random.randint(0, 10, size=image.shape, dtype=np.uint8) def get_num_bytes(**kwargs): with tempfile.TemporaryDirectory() as directory_name: filename = os.path.join(directory_name, 'test.png') media.write_image(filename, image, **kwargs) return os.path.getsize(filename) self.assertAlmostEqual(get_num_bytes(), 383588, delta=300) self.assertAlmostEqual(get_num_bytes(optimize=True), 382909, delta=300)
def test_color_ramp_float(self): shape = (2, 3) image = media.color_ramp(shape=shape) self.assert_all_equal(image.shape[:2], shape) self.assert_all_close(image, [ [ [0.5 / shape[0], 0.5 / shape[1], 0.0], [0.5 / shape[0], 1.5 / shape[1], 0.0], [0.5 / shape[0], 2.5 / shape[1], 0.0], ], [ [1.5 / shape[0], 0.5 / shape[1], 0.0], [1.5 / shape[0], 1.5 / shape[1], 0.0], [1.5 / shape[0], 2.5 / shape[1], 0.0], ], ])
def test_color_ramp_uint8(self): shape = (1, 3) image = media.color_ramp(shape=shape, dtype=np.uint8) self.assert_all_equal(image.shape[:2], shape) expected = [[ [ int(0.5 / shape[0] * 255 + 0.5), int(0.5 / shape[1] * 255 + 0.5), 0 ], [ int(0.5 / shape[0] * 255 + 0.5), int(1.5 / shape[1] * 255 + 0.5), 0 ], [ int(0.5 / shape[0] * 255 + 0.5), int(2.5 / shape[1] * 255 + 0.5), 0 ], ]] self.assert_all_equal(image, expected)
def create_image(shape): image = media.color_ramp(shape[:2], dtype=dtype) return image.mean(axis=-1).astype(dtype) if len( shape) == 2 else image[..., :shape[2]]