def test_get_tissue_mask(self): """Test get_tissue_mask().""" thumbnail_rgb = get_slide_thumbnail(gc, SAMPLE_SLIDE_ID) labeled, mask = get_tissue_mask( thumbnail_rgb, deconvolve_first=True, n_thresholding_steps=1, sigma=1.5, min_size=30) # # visualize result # vals = np.random.rand(256,3) # vals[0, ...] = [0.9, 0.9, 0.9] # cMap = ListedColormap(1 - vals) # # f, ax = plt.subplots(1, 3, figsize=(20, 20)) # ax[0].imshow(thumbnail_rgb) # ax[1].imshow(labeled, cmap=cMap) # ax[2].imshow(mask, cmap=cMap) # plt.show() self.assertTupleEqual(labeled.shape, (156, 256)) self.assertEqual(len(np.unique(labeled)), 10) # save for use in the next test imwrite(os.path.join( savepath, 'tissue_binmask.png'), np.uint8(0 + (labeled > 0)))
def set_slide_info_and_get_tissue_mask(self): """Set self.slide_info dict and self.labeled tissue mask.""" # This is a presistent dict to store information about slide self.slide_info = self.gc.get('item/%s/tiles' % self.slide_id) # get tissue mask thumbnail_rgb = get_slide_thumbnail(self.gc, self.slide_id) # color normalization if desired if 'thumbnail' in self.cnorm_params.keys(): thumbnail_rgb = np.uint8( reinhard(im_src=thumbnail_rgb, target_mu=self.cnorm_params['thumbnail']['mu'], target_sigma=self.cnorm_params['thumbnail']['sigma'])) # get labeled tissue mask -- each unique value is one tissue piece labeled, _ = get_tissue_mask(thumbnail_rgb, **self.get_tissue_mask_kwargs) if len(np.unique(labeled)) < 2: raise ValueError("No tissue detected!") if self.visualize_tissue_boundary: annotation_docs = get_tissue_boundary_annotation_documents( self.gc, slide_id=self.slide_id, labeled=labeled) for doc in annotation_docs: _ = self.gc.post("/annotation?itemId=" + self.slide_id, json=doc) # Find size relative to WSI self.slide_info[ 'F_tissue'] = self.slide_info['sizeX'] / labeled.shape[1] return labeled
def test_get_tissue_mask(self): """Test get_tissue_mask().""" thumbnail_rgb = get_slide_thumbnail(cfg.gc, cfg.iteminfo['_id']) cfg.labeled, mask = get_tissue_mask( thumbnail_rgb, deconvolve_first=True, n_thresholding_steps=1, sigma=1.5, min_size=30) assert cfg.labeled.shape == (156, 256) assert len(np.unique(cfg.labeled)) == 11
def set_slide_info_and_get_tissue_mask(self): """Set self.slide_info dict and self.labeled tissue mask.""" # This is a presistent dict to store information about slide self.slide_info = self.gc.get('item/%s/tiles' % self.slide_id) # get tissue mask thumbnail_rgb = get_slide_thumbnail(self.gc, self.slide_id) # get labeled tissue mask -- each unique value is one tissue piece labeled, _ = get_tissue_mask(thumbnail_rgb, **self.get_tissue_mask_kwargs) if len(np.unique(labeled)) < 2: raise ValueError("No tissue detected!") # Find size relative to WSI self.slide_info[ 'F_tissue'] = self.slide_info['sizeX'] / labeled.shape[1] return labeled
def test_get_tissue_mask(self): """Test get_tissue_mask().""" thumbnail_rgb = get_slide_thumbnail(gc, SAMPLE_SLIDE_ID) labeled, mask = get_tissue_mask(thumbnail_rgb, deconvolve_first=True, n_thresholding_steps=2, sigma=0., min_size=30) # # visualize result # vals = np.random.rand(256,3) # vals[0, ...] = [0.9, 0.9, 0.9] # cMap = ListedColormap(1 - vals) # # f, ax = plt.subplots(1, 3, figsize=(20, 20)) # ax[0].imshow(thumbnail_rgb) # ax[1].imshow(labeled, cmap=cMap) # ax[2].imshow(mask, cmap=cMap) # plt.show() self.assertTupleEqual(labeled.shape, (152, 256)) self.assertEqual(len(np.unique(labeled)), 10)
def test_reinhard(self): """Test reinhard.""" # get RGB image at a small magnification slide_info = gc.get('item/%s/tiles' % SAMPLE_SLIDE_ID) getStr = "/item/%s/tiles/region?left=%d&right=%d&top=%d&bottom=%d" % ( SAMPLE_SLIDE_ID, 0, slide_info['sizeX'], 0, slide_info['sizeY'] ) + "&magnification=%.2f" % MAG tissue_rgb = get_image_from_htk_response( gc.get(getStr, jsonResp=False)) # # SANITY CHECK! normalize to LAB mean and std from SAME slide # mean_lab, std_lab = lab_mean_std(tissue_rgb) # tissue_rgb_normalized = reinhard( # tissue_rgb, target_mu=mean_lab, target_sigma=std_lab) # # # we expect the images to be (almost) exactly the same # assert np.mean(tissue_rgb - tissue_rgb_normalized) < 1 # Normalize to pre-set color standard tissue_rgb_normalized = reinhard( tissue_rgb, target_mu=cnorm['mu'], target_sigma=cnorm['sigma']) # check that it matches mean_lab, std_lab = lab_mean_std(tissue_rgb_normalized) self.assertTrue(all( np.abs(mean_lab - cnorm['mu']) < [0.1, 0.1, 0.1])) self.assertTrue(all( np.abs(std_lab - cnorm['sigma']) < [0.1, 0.1, 0.1])) # get tissue mask thumbnail_rgb = get_slide_thumbnail(gc, SAMPLE_SLIDE_ID) labeled, mask = get_tissue_mask( thumbnail_rgb, deconvolve_first=True, n_thresholding_steps=1, sigma=1.5, min_size=30) # # visualize result # vals = np.random.rand(256, 3) # vals[0, ...] = [0.9, 0.9, 0.9] # cMap = ListedColormap(1 - vals) # # f, ax = plt.subplots(1, 3, figsize=(20, 20)) # ax[0].imshow(thumbnail_rgb) # ax[1].imshow(labeled, cmap=cMap) # ax[2].imshow(mask, cmap=cMap) # plt.show() # Do MASKED normalization to preset standard mask_out = resize( labeled == 0, output_shape=tissue_rgb.shape[:2], order=0, preserve_range=True) == 1 tissue_rgb_normalized = reinhard( tissue_rgb, target_mu=cnorm['mu'], target_sigma=cnorm['sigma'], mask_out=mask_out) # check that it matches mean_lab, std_lab = lab_mean_std( tissue_rgb_normalized, mask_out=mask_out) self.assertTrue(all( np.abs(mean_lab - cnorm['mu']) < [0.1, 0.1, 0.1])) self.assertTrue(all( np.abs(std_lab - cnorm['sigma']) < [0.1, 0.1, 0.1]))
[0.71681094, 0.90081588, 0.41999816], [0.38588316, 0.42616716, -0.90380025]]) # %%=========================================================================== print("Getting images to be normalized ...") # get RGB image at a small magnification slide_info = gc.get('item/%s/tiles' % SAMPLE_SLIDE_ID) getStr = "/item/%s/tiles/region?left=%d&right=%d&top=%d&bottom=%d" % ( SAMPLE_SLIDE_ID, 0, slide_info['sizeX'], 0, slide_info['sizeY']) + "&magnification=%.2f" % MAG tissue_rgb = get_image_from_htk_response(gc.get(getStr, jsonResp=False)) # get mask of things to ignore thumbnail_rgb = get_slide_thumbnail(gc, SAMPLE_SLIDE_ID) mask_out, _ = get_tissue_mask(thumbnail_rgb, deconvolve_first=True, n_thresholding_steps=1, sigma=1.5, min_size=30) mask_out = resize(mask_out == 0, output_shape=tissue_rgb.shape[:2], order=0, preserve_range=True) == 1 # since this is a unit test, just work on a small image tissue_rgb = tissue_rgb[1000:1500, 2500:3000, :] mask_out = mask_out[1000:1500, 2500:3000] # for reproducibility