def test_read_block_2d(self): """ Read 2d uncompressed block. Reads thew whole uncompressed image and compares received raster against reference images. """ # Image to test image_path = get_test_image_path("czi", "pJP31mCherry.czi") # Reference channel images channel_image_paths = [ get_test_image_path( "czi", "pJP31mCherry.grey/pJP31mCherry_b0t0z0c0x0-512y0-512.bmp"), get_test_image_path( "czi", "pJP31mCherry.grey/pJP31mCherry_b0t0z0c1x0-512y0-512.bmp"), get_test_image_path( "czi", "pJP31mCherry.grey/pJP31mCherry_b0t0z0c2x0-512y0-512.bmp") ] slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) for channel_index in range(scene.num_channels): image_raster = scene.read_block(channel_indices=[channel_index]) reference_raster = cv.imread(channel_image_paths[channel_index], cv.IMREAD_GRAYSCALE) self.assertTrue(np.array_equal(image_raster, reference_raster))
def test_read_block_4d(self): """ Read 4d uncompressed block. Reads thew whole uncompressed image and compares received raster against reference images. """ # Image to test image_path = get_test_image_path("czi", "pJP31mCherry.czi") slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) slices = (2, 5) frames = (0, 1) for channel_index in range(scene.num_channels): image_raster = scene.read_block(channel_indices=[channel_index], slices=slices, frames=frames) image_shape = image_raster.shape self.assertEqual(image_shape, (3, 512, 512)) for slice_index in range(slices[0], slices[1]): slice_raster = image_raster[slice_index - slices[0], :, :] slice_shape = slice_raster.shape self.assertEqual(slice_shape, (512, 512)) ref_image_name = "pJP31mCherry.grey/pJP31mCherry_b0t0z" + \ str(slice_index) + "c" + str(channel_index) + \ "x0-512y0-512.bmp" ref_image_path = get_test_image_path("czi", ref_image_name) reference_raster = cv.imread(ref_image_path, cv.IMREAD_GRAYSCALE) self.assertTrue(np.array_equal(slice_raster, reference_raster))
def test_3chnl_png16b_metadata(self): """Opens 3 channel 16 bit png file and checks metadata.""" image_path = get_test_image_path( "gdal", "img_2448x2448_3x16bit_SRC_RGB_ducks.png" ) slide = slideio.open_slide(image_path, "GDAL") self.assertTrue(slide is not None) num_scenes = slide.num_scenes self.assertEqual(num_scenes, 1) self.assertEqual(image_path, slide.file_path) scene = slide.get_scene(0) self.assertTrue(scene is not None) self.assertEqual(image_path, scene.file_path) self.assertEqual(3, scene.num_channels) scene_rect = scene.rect self.assertEqual(0, scene_rect[0]) self.assertEqual(0, scene_rect[1]) self.assertEqual(2448, scene_rect[2]) self.assertEqual(2448, scene_rect[3]) for channel_index in range(scene.num_channels): channel_type = scene.get_channel_data_type(channel_index) self.assertEqual(channel_type, np.uint16) compression = scene.compression self.assertEqual(compression, slideio.Compression.Png) res = scene.resolution self.assertEqual(0, res[0]) self.assertEqual(0, res[1])
def test_readblock_png8bit_with(self): """ 8 bit png image. Reads 8b png images and checks the raster. by calculation of raster statistics for specific rectangles """ image_path = get_test_image_path( "gdal", "img_1024x600_3x8bit_RGB_color_bars_CMYKWRGB.png" ) with slideio.open_slide(image_path, "GDAL") as slide: self.assertTrue(slide is not None) with slide.get_scene(0) as scene: block_rect = (260, 500, 100, 100) # read 3 channel block raster = scene.read_block(block_rect) mean, stddev = cv.meanStdDev(raster) self.assertEqual(mean[0], 255) self.assertEqual(stddev[0], 0) self.assertEqual(mean[1], 255) self.assertEqual(stddev[1], 0) self.assertEqual(mean[2], 0) self.assertEqual(stddev[2], 0) # read one channel block raster = scene.read_block(block_rect, channel_indices=[1]) mean, stddev = cv.meanStdDev(raster) self.assertEqual(mean[0], 255) self.assertEqual(stddev[0], 0)
def test_3chnl_jpeg_metadata(self): """Opens 3 channel jpeg file and checks metadata.""" image_path = get_test_image_path( "gdal", "Airbus_Pleiades_50cm_8bit_RGB_Yogyakarta.jpg" ) slide = slideio.open_slide(image_path, "GDAL") self.assertTrue(slide is not None) num_scenes = slide.num_scenes self.assertEqual(num_scenes, 1) self.assertEqual(image_path, slide.file_path) scene = slide.get_scene(0) self.assertTrue(scene is not None) self.assertEqual(image_path, scene.file_path) self.assertEqual(3, scene.num_channels) scene_rect = scene.rect self.assertEqual(0, scene_rect[0]) self.assertEqual(0, scene_rect[1]) self.assertEqual(5494, scene_rect[2]) self.assertEqual(5839, scene_rect[3]) for channel_index in range(scene.num_channels): channel_type = scene.get_channel_data_type(channel_index) self.assertEqual(channel_type, np.uint8) compression = scene.compression self.assertEqual(compression, slideio.Compression.Jpeg) res = scene.resolution self.assertEqual(0, res[0]) self.assertEqual(0, res[1])
def test_file_metadata2(self): """ Checks image metadata. Opens 6 channel uncompressed czi file and checks metadata. """ image_path = get_test_image_path("czi", "08_18_2018_enc_1001_633.czi") channel_names = ["646", "655", "664", "673", "682", "691"] slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) num_scenes = slide.num_scenes self.assertEqual(num_scenes, 1) self.assertEqual(image_path, slide.file_path) scene = slide.get_scene(0) self.assertTrue(scene is not None) self.assertEqual(image_path, scene.file_path) self.assertEqual(6, scene.num_channels) scene_rect = scene.rect self.assertEqual(0, scene_rect[0]) self.assertEqual(0, scene_rect[1]) self.assertEqual(1000, scene_rect[2]) self.assertEqual(1000, scene_rect[3]) self.assertEqual(63, scene.magnification) for channel_index in range(scene.num_channels): channel_type = scene.get_channel_data_type(channel_index) self.assertEqual(channel_type, np.uint16) compression = scene.compression self.assertEqual(compression, slideio.Compression.Uncompressed) channel_name = scene.get_channel_name(channel_index) self.assertEqual(channel_name, channel_names[channel_index]) res = scene.resolution correct_res = 6.7475572821478794e-8 self.assertEqual(correct_res, pytest.approx(res[0])) self.assertEqual(correct_res, pytest.approx(res[1]))
def test_file_metadata(self): """ Checks image metadata. Opens 3 channel uncompressed czi file and checks metadata. """ image_path = get_test_image_path("czi", "pJP31mCherry.czi") slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) num_scenes = slide.num_scenes self.assertEqual(num_scenes, 1) self.assertEqual(image_path, slide.file_path) scene = slide.get_scene(0) self.assertTrue(scene is not None) self.assertEqual(image_path, scene.file_path) self.assertEqual(3, scene.num_channels) scene_rect = scene.rect self.assertEqual(0, scene_rect[0]) self.assertEqual(0, scene_rect[1]) self.assertEqual(512, scene_rect[2]) self.assertEqual(512, scene_rect[3]) self.assertEqual(100, scene.magnification) raw_metadata = slide.raw_metadata self.assertTrue(raw_metadata.startswith("<ImageDocument>")) for channel_index in range(scene.num_channels): channel_type = scene.get_channel_data_type(channel_index) self.assertEqual(channel_type, np.uint8) compression = scene.compression self.assertEqual(compression, slideio.Compression.Uncompressed) res = scene.resolution correct_res = 9.76783e-8 self.assertEqual(correct_res, pytest.approx(res[0])) self.assertEqual(correct_res, pytest.approx(res[1]))
def test_core_not_existing_driver(self): """ Test for calling of not-existing driver. Checks if slideio throws RuntimeError exception during opening of not existing file. """ image_path = get_test_image_path( "GDAL", "img_2448x2448_3x8bit_SRC_RGB_ducks.png") with pytest.raises(RuntimeError): slideio.open_slide(image_path, "AAAA")
def test_corrupted_image(self): """ Raise an error for corrupted images. Tries to open and read a corrupted image. slideio shall raise an exception. """ # Image to test image_path = get_test_image_path("czi", "corrupted.czi") with pytest.raises(RuntimeError): slideio.open_slide(image_path, "CZI")
def test_image_similarity_equal(self): """Similarity score of equal images shall be 1.""" image_path = get_test_image_path( "gdal", "img_2448x2448_3x8bit_SRC_RGB_ducks.png") with slideio.open_slide(image_path, "GDAL") as slide: with slide.get_scene(0) as scene: img1 = scene.read_block() img2 = scene.read_block() score = slideio.compare_images(img1, img2) self.assertEqual(score, 1) blank_image = np.zeros(img1.shape, np.uint8) score = slideio.compare_images(img1, blank_image) self.assertLess(score, 0.5) blur = cv.blur(img2, (20, 20)) score = slideio.compare_images(img1, blur) self.assertLess(score, 0.98) self.assertGreater(score, 0.8)