Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
**it_kwargs)
# get global x and y positions, tile height and width
left = tile_info['gx']
top = tile_info['gy']
# get scale
scale = tile_info['magnification'] / args.analysis_mag
# get ratio between max(height, width) and pixel points
ratio_pixel = tile_info['gwidth'] / (tile_info['width'] / scale)
im_tile = tile_info['tile'][:, :, :3]
# perform color normalization
im_nmzd = htk_cnorm.reinhard(im_tile,
args.reference_mu_lab, args.reference_std_lab,
wsi_mean, wsi_stddev)
# resize to requested size
im_nmzd = (resize(
im_nmzd, (int(im_nmzd.shape[0] / scale), int(im_nmzd.shape[1] / scale)),
mode='reflect') * 255).astype(np.uint8)
# get red and green channels
im_red = im_nmzd[:, :, 0]
im_green = im_nmzd[:, :, 1]
# get foreground mask simply using numpy.spacing, a generalization of EPS
im_ratio = im_red / (im_green + np.spacing(1))
im_fgnd_mask = im_ratio > args.rg_ratio_superpixel
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(
def set_tissue_rgb(self):
"""Load RGB from server for single tissue piece."""
# load RGB for this tissue piece at saliency magnification
getStr = "/item/%s/tiles/region?left=%d&right=%d&top=%d&bottom=%d" % (
self.cd.slide_id, self.xmin, self.xmax, self.ymin, self.ymax
) + "&magnification=%d" % self.cd.MAG
resp = self.cd.gc.get(getStr, jsonResp=False)
self.tissue_rgb = get_image_from_htk_response(resp)
# color normalization if desired
if 'main' in self.cd.cnorm_params.keys():
self.tissue_rgb = np.uint8(reinhard(
im_src=self.tissue_rgb,
target_mu=self.cd.cnorm_params['main']['mu'],
target_sigma=self.cd.cnorm_params['main']['sigma']))
src_mu_lab=None, src_sigma_lab=None):
# get slide tile source
ts = large_image.getTileSource(slide_path)
# get requested tile
tile_info = ts.getSingleTile(
tile_position=tile_position,
format=large_image.tilesource.TILE_FORMAT_NUMPY,
**it_kwargs)
# get tile image
im_tile = tile_info['tile'][:, :, :3]
# perform color normalization
im_nmzd = htk_cnorm.reinhard(im_tile,
args.reference_mu_lab,
args.reference_std_lab,
src_mu=src_mu_lab,
src_sigma=src_sigma_lab)
# perform color decovolution
w = cli_utils.get_stain_matrix(args)
im_stains = htk_cdeconv.color_deconvolution(im_nmzd, w).Stains
im_nuclei_stain = im_stains[:, :, 0].astype(np.float)
# segment nuclear foreground
im_nuclei_fgnd_mask = im_nuclei_stain < args.foreground_threshold
# segment nuclei
def color_normalize_unspecified_components(self):
"""Color normalize "true" tissue components."""
if self.cdt.color_normalization_method == 'reinhard':
self.cdt._print2(
"%s: -- reinhard normalization ..." % self.monitorPrefix)
self.tissue_rgb = reinhard(
self.tissue_rgb,
target_mu=self.cdt.target_stats_reinhard['mu'],
target_sigma=self.cdt.target_stats_reinhard['sigma'],
mask_out=self.labeled != self.cdt.GTcodes
.loc["not_specified", "GT_code"])
elif self.cdt.color_normalization_method == 'macenko_pca':
self.cdt._print2(
"%s: -- macenko normalization ..." % self.monitorPrefix)
self.tissue_rgb = deconvolution_based_normalization(
self.tissue_rgb, W_target=self.cdt.target_W_macenko,
mask_out=self.labeled != self.cdt.GTcodes
.loc["not_specified", "GT_code"],
stain_unmixing_routine_params=self.
cdt.stain_unmixing_routine_params)
else:
# get slide tile source
ts = large_image.getTileSource(img_path)
# get requested tile
tile_info = ts.getSingleTile(
tile_position=tile_position,
format=large_image.tilesource.TILE_FORMAT_NUMPY,
**it_kwargs)
# get scale
scale = tile_info['magnification'] / args.analysis_mag
im_tile = tile_info['tile'][:, :, :3]
# perform color normalization
im_nmzd = htk_cnorm.reinhard(im_tile,
args.reference_mu_lab, args.reference_std_lab,
wsi_mean, wsi_stddev)
# resize to requested size
im_nmzd = (resize(
im_nmzd, (int(im_nmzd.shape[0] / scale), int(im_nmzd.shape[1] / scale)),
mode='reflect') * 255).astype(np.uint8)
# get red and green channels
im_red = im_nmzd[:, :, 0]
im_green = im_nmzd[:, :, 1]
# get foreground mask simply using numpy.spacing, a generalization of EPS
im_ratio = im_red / (im_green + np.spacing(1))
im_fgnd_mask = im_ratio > args.rg_ratio_superpixel