Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _merge_leafs(self, leafs):
nest_polygons = []
for leaf in leafs:
leafidx = int(leaf.split('polygon-')[1])
nest = dict(self.contours_slice.loc[leafidx, :])
coords = _parse_annot_coords(nest)
nest_polygons.append(Polygon(coords))
return self._merge_polygons(nest_polygons)
# Nests of the same label. The nest IDs are using the index of the
# roi dataframe from the edge_nests dictionary
Nests1 = _get_nests_slice(1)
Nests2 = _get_nests_slice(2)
# to avoid redoing things, keep all polygons in a list
polygons1 = []
nno1 = 0
nno1Max = Nests1.shape[0]
for nid1, nest1 in Nests1.iterrows():
nno1 += 1
self._print2("%s: edge1-nest %d of %d" % (
monitorPrefix, nno1, nno1Max))
try:
coords = np.array(_parse_annot_coords(nest1))
coords[:, 0] = coords[:, 0] + self.roiinfos[
edgepair['roi1-name']]['left']
coords[:, 1] = coords[:, 1] + self.roiinfos[
edgepair['roi1-name']]['top']
polygons1.append((nid1, Polygon(coords)))
except Exception as e:
self._print2(
"%s: edge1-nest %d of %d: Shapely Error (below)" % (
monitorPrefix, nno1, nno1Max))
self._print2(e)
# go through the "other" polygons to get merge list
to_merge = DataFrame(columns=[
'nest1-roiname', 'nest1-nid', 'nest2-roiname', 'nest2-nid'])
nno2 = 0
nno2Max = Nests2.shape[0]
"""Merge polygons using shapely (Internal).
Given a single cluster from _get_merge_clusters_from_df(), This creates
and merges polygons into a single cascaded union. It first dilates the
polygons by buffer_size pixels to make them overlap, merges them,
then erodes back by buffer_size to get the merged polygon.
"""
buffer_size = self.merge_thresh + 3
nest_polygons = []
for nestinfo in cluster:
nest = dict(self.edge_contours[nestinfo['roiname']].loc[
nestinfo['nid'], :])
roitop = self.roiinfos[nestinfo['roiname']]['top']
roileft = self.roiinfos[nestinfo['roiname']]['left']
coords = _parse_annot_coords(
nest, x_offset=roileft, y_offset=roitop)
nest_polygons.append(Polygon(coords).buffer(buffer_size))
merged_polygon = cascaded_union(nest_polygons).buffer(-buffer_size)
return merged_polygon
def _add_roi_offset_to_contours(self, roi_df, roiname):
"""Add roi offset to coordinates of polygons (Internal)."""
for idx, annot in roi_df.iterrows():
coords = np.int32(_parse_annot_coords(dict(annot)))
coords[:, 0] = coords[:, 0] + self.roiinfos[roiname]['left']
coords[:, 1] = coords[:, 1] + self.roiinfos[roiname]['top']
roi_df.loc[idx, 'coords_x'] = ",".join(
[str(j) for j in coords[:, 0]])
roi_df.loc[idx, 'coords_y'] = ",".join(
[str(j) for j in coords[:, 1]])
return roi_df
polygons1.append((nid1, Polygon(coords)))
except Exception as e:
self._print2(
"%s: edge1-nest %d of %d: Shapely Error (below)" % (
monitorPrefix, nno1, nno1Max))
self._print2(e)
# go through the "other" polygons to get merge list
to_merge = DataFrame(columns=[
'nest1-roiname', 'nest1-nid', 'nest2-roiname', 'nest2-nid'])
nno2 = 0
nno2Max = Nests2.shape[0]
for nid2, nest2 in Nests2.iterrows():
nno2 += 1
try:
coords = np.array(_parse_annot_coords(nest2))
coords[:, 0] = coords[:, 0] + self.roiinfos[
edgepair['roi2-name']]['left']
coords[:, 1] = coords[:, 1] + self.roiinfos[
edgepair['roi2-name']]['top']
polygon2 = Polygon(coords)
except Exception as e:
self._print2(
"%s: edge2-nest %d of %d: Shapely Error (below)" % (
monitorPrefix, nno2, nno2Max))
self._print2(e)
continue
nno1Max = len(polygons1)-1
for nno1, poly1 in enumerate(polygons1):
self._print2(
"%s: edge2-nest %d of %d: vs. edge1-nest %d of %d" % (