def set_superpixel_features(self): """Get superpixel features.""" assert (self.cd.use_intensity or self.cd.use_texture) # Possibly deconvolvve to get hematoxylin channel (cellular areas) # hematoxylin channel return shows MINIMA so we invert if self.cd.deconvolve: Stains, channel = _deconv_color(self.tissue_rgb) tissue_htx = 255 - Stains[..., channel] else: tissue_htx = rgb2gray(self.tissue_rgb) # calculate features from superpixels rprops = regionprops(self.spixel_mask) fdata_list = [] if self.cd.use_intensity: fdata_list.append(compute_intensity_features( im_label=self.spixel_mask, im_intensity=tissue_htx, rprops=rprops)) if self.cd.use_texture: fdata_list.append(compute_haralick_features( im_label=self.spixel_mask, im_intensity=tissue_htx, rprops=rprops)) self.fdata = concat(fdata_list, axis=1) if self.cd.keep_feats is not None: self.fdata = self.fdata.loc[:, self.cd.keep_feats] # Index is corresponding pixel value in the superpixel mask # IMPORTANT: this assumes that regionprops output is sorted by unique # pixel values in label mask, which it is by default self.fdata.index = set(np.unique(self.spixel_mask)) - {0, }
def find_top_cellular_regions(self): """Keep largest and most cellular regions.""" # keep only largest n regions regions top_cellular_mask = _get_largest_regions( self.maybe_cellular, top_n=self.cdt.cellular_largest_n) top_cellular = self.maybe_cellular.copy() top_cellular[top_cellular_mask == 0] = 0 # get intensity features of hematoxylin channel for each region intensity_feats = compute_intensity_features( im_label=top_cellular, im_intensity=self.tissue_htx, feature_list=['Intensity.Mean']) unique = np.unique(top_cellular[top_cellular > 0]) intensity_feats.index = unique # get top n brightest regions from the largest areas intensity_feats.sort_values("Intensity.Mean", axis=0, inplace=True) discard = np.array(intensity_feats.index[:-self.cdt.cellular_top_n]) discard = np.in1d(top_cellular, discard).reshape(top_cellular.shape) top_cellular[discard] = 0 # integrate into labeled mask self.labeled[top_cellular > 0] = self.cdt.GTcodes.loc['top_cellular', 'GT_code']