Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
- self.ordinary_contours: dict: indexed by maskname, each entry
is a contours dataframe
- self.edge_contours: dict: indexed by maskname, each entry is
a contours dataframe
- self.merged_contours: pandas DataFrame: single dataframe to
save all merged contours
"""
ordinary_contours = dict()
edge_contours = dict()
for midx, maskpath in enumerate(self.maskpaths):
# extract contours
MASK = imread(maskpath)
contours_df = get_contours_from_mask(
MASK=MASK,
monitorPrefix="%s: mask %d of %d" % (
monitorPrefix, midx, len(self.maskpaths)),
**self.contkwargs)
# separate edge from non-edge contours
edgeids = []
for edge in ['top', 'left', 'bottom', 'right']:
edgeids.extend(list(contours_df.loc[contours_df.loc[
:, 'touches_edge-%s' % edge] == 1, :].index))
edgeids = list(set(edgeids))
roiname = os.path.split(maskpath)[1]
edge_contours[roiname] = contours_df.loc[edgeids, :].copy()
ordinary_contours[roiname] = contours_df.drop(edgeids, axis=0)
self.ordinary_contours = ordinary_contours
def visualize_individual_superpixels(self):
"""Visualize individual spixels, color-coded by cellularity."""
# Define GTCodes dataframe
GTCodes_df = DataFrame(columns=['group', 'GT_code', 'color'])
for spval, sp in self.fdata.iterrows():
spstr = 'spixel-%d_cellularity-%d' % (
spval, self.cluster_props[sp['cluster']]['cellularity'])
GTCodes_df.loc[spstr, 'group'] = spstr
GTCodes_df.loc[spstr, 'GT_code'] = spval
GTCodes_df.loc[spstr, 'color'] = \
self.cluster_props[sp['cluster']]['color']
# get contours df
contours_df = get_contours_from_mask(
MASK=self.spixel_mask, GTCodes_df=GTCodes_df,
get_roi_contour=False, MIN_SIZE=0, MAX_SIZE=None,
verbose=self.cd.verbose == 3, monitorPrefix=self.monitorPrefix)
contours_df.loc[:, "group"] = [
j.split('_')[-1] for j in contours_df.loc[:, "group"]]
# get annotation docs
annprops = {
'F': (self.ymax - self.ymin) / self.tissue_rgb.shape[0],
'X_OFFSET': self.xmin,
'Y_OFFSET': self.ymin,
'opacity': self.cd.opacity,
'lineWidth': self.cd.lineWidth,
}
annotation_docs = get_annotation_documents_from_contours(
contours_df.copy(), docnamePrefix='spixel', annprops=annprops,
def visualize_results(self):
"""Visualize results in DSA."""
# get contours
contours_df = get_contours_from_mask(
MASK=self.labeled, GTCodes_df=self.cdt.GTcodes.copy(),
groups_to_get=self.cdt.groups_to_visualize,
get_roi_contour=self.cdt.get_roi_contour, roi_group='roi',
background_group='not_specified',
discard_nonenclosed_background=True,
MIN_SIZE=15, MAX_SIZE=None,
verbose=self.cdt.verbose == 3,
monitorPrefix=self.monitorPrefix + ": -- contours")
# get annotation docs
annprops = {
'F': self.cdt.slide_info['magnification'] / self.cdt.MAG,
'X_OFFSET': self.xmin,
'Y_OFFSET': self.ymin,
'opacity': self.cdt.opacity,
'lineWidth': self.cdt.lineWidth,
"""Visualize contiguous spixels, color-coded by cellularity."""
# get cellularity cluster membership mask
cellularity_mask = np.zeros(self.spixel_mask.shape)
for spval, sp in self.fdata.iterrows():
cellularity_mask[self.spixel_mask == spval] = sp['cluster']
# Define GTCodes dataframe
GTCodes_df = DataFrame(columns=['group', 'GT_code', 'color'])
for spval, cp in self.cluster_props.items():
spstr = 'cellularity-%d' % (cp['cellularity'])
GTCodes_df.loc[spstr, 'group'] = spstr
GTCodes_df.loc[spstr, 'GT_code'] = spval
GTCodes_df.loc[spstr, 'color'] = cp['color']
# get contours df
contours_df = get_contours_from_mask(
MASK=cellularity_mask, GTCodes_df=GTCodes_df,
get_roi_contour=False, MIN_SIZE=0, MAX_SIZE=None,
verbose=self.cd.verbose == 3, monitorPrefix=self.monitorPrefix)
# get annotation docs
annprops = {
'F': (self.ymax - self.ymin) / self.tissue_rgb.shape[0],
'X_OFFSET': self.xmin,
'Y_OFFSET': self.ymin,
'opacity': self.cd.opacity_contig,
'lineWidth': self.cd.lineWidth,
}
annotation_docs = get_annotation_documents_from_contours(
contours_df.copy(), docnamePrefix='contig', annprops=annprops,
annots_per_doc=1000, separate_docs_by_group=True,
verbose=self.cd.verbose == 3, monitorPrefix=self.monitorPrefix)
annprops = {
'F': slide_info['sizeX'] / labeled.shape[1], # relative to base
'X_OFFSET': 0,
'Y_OFFSET': 0,
'opacity': 0,
'lineWidth': 4.0,
}
# Define GTCodes dataframe
GTCodes_df = DataFrame(columns=['group', 'GT_code', 'color'])
GTCodes_df.loc['tissue', 'group'] = group
GTCodes_df.loc['tissue', 'GT_code'] = 1
GTCodes_df.loc['tissue', 'color'] = color
# get annotation docs
contours_tissue = get_contours_from_mask(
MASK=0 + (labeled > 0), GTCodes_df=GTCodes_df,
get_roi_contour=False, MIN_SIZE=0, MAX_SIZE=None, verbose=False,
monitorPrefix="tissue: getting contours")
annotation_docs = get_annotation_documents_from_contours(
contours_tissue.copy(), docnamePrefix='test', annprops=annprops,
verbose=False, monitorPrefix="tissue : annotation docs")
return annotation_docs