def it_locates_the_biggest_bbox(self, tmpdir, slide_fixture, tissue_mask, save_scaled_image, expectation): slide = Slide(slide_fixture, os.path.join(tmpdir, "processed")) if save_scaled_image: slide.save_scaled_image(3) expected_img = load_expectation( os.path.join("bbox-location-images", expectation), type_="png", ) bbox_location_img = slide.locate_biggest_tissue_box( tissue_mask=tissue_mask, scale_factor=3) np.testing.assert_array_almost_equal(np.asarray(bbox_location_img), expected_img)
def it_can_save_scaled_image(self, tmpdir, resampled_dims_): tmp_path_ = tmpdir.mkdir("myslide") image = PILIMG.RGBA_COLOR_500X500_155_249_240 image.save(os.path.join(tmp_path_, "mywsi.png"), "PNG") slide_path = os.path.join(tmp_path_, "mywsi.png") slide = Slide(slide_path, os.path.join(tmp_path_, "processed")) resampled_dims_.return_value = (100, 200, 300, 400) slide.save_scaled_image(32) assert slide.scaled_image_path(32) == os.path.join( tmp_path_, "processed", "mywsi-32x-100x200-300x400.png") assert os.path.exists( os.path.join(tmp_path_, slide.scaled_image_path(32)))
def it_locates_tiles_on_the_slide(self, request, fixture_slide, expectation, tmpdir): slide = Slide(fixture_slide, os.path.join(tmpdir, "processed")) slide.save_scaled_image(10) random_tiles_extractor = RandomTiler(tile_size=(512, 512), n_tiles=2, level=0, seed=42, check_tissue=False) expected_img = load_expectation( expectation, type_="png", ) tiles_location_img = random_tiles_extractor.locate_tiles( slide, scale_factor=10) # --- Expanding test report with actual and expected images --- expand_tests_report(request, actual=tiles_location_img, expected=expected_img) np.testing.assert_array_almost_equal(np.asarray(tiles_location_img), expected_img)
# plt.imshow(pil_img); plt.axis('off'); pil_img.save(tmp_outpath / 'pil_img.png', format='png') # Apply mask to image pil_img_boxed = histolab.util.apply_mask_image( pil_img, pdx_slide.biggest_tissue_box_mask) pil_img_boxed.save(tmp_outpath / 'pil_img_boxed.png', format='png') # Scale and save image scale_factor = 16 np_img_scaled = pdx_slide.resampled_array( scale_factor=scale_factor) # scale and return ndarray print(type(np_img_scaled)) print(np_img_scaled.shape) # out image file path: {name}-{scale_factor}x-{large_w}x{large_h}-{new_w}x{new_h}.{IMG_EXT} pdx_slide.save_scaled_image( scale_factor=scale_factor) # scale and save into file # Show img from array # plt.imshow(img_scaled); plt.axis('off'); fig, ax = plt.subplots(figsize=(5, 5)) ax.imshow(np_img_scaled) ax.axis('off') # PIL.Image.fromarray(np_img_scaled) # ----------------------------------- # Metadata # ----------------------------------- # Load meta if (metapath / 'meta_from_wsi_images.csv').exists(): meta_df = pd.read_csv(metapath / 'meta_from_wsi_images.csv')