def _process(self, element, key=None): try: import cv2 as cv except ImportError: raise ImportError('GrabCut algorithm requires openCV') if isinstance(self.p.foreground, hv.Polygons): rasterize_op = rasterize_polygon else: rasterize_op = rasterize.instance(aggregator=ds.any()) kwargs = {'dynamic': False, 'target': element} fg_mask = rasterize_op(self.p.foreground, **kwargs) bg_mask = rasterize_op(self.p.background, **kwargs) fg_mask = fg_mask.dimension_values(2, flat=False) bg_mask = bg_mask.dimension_values(2, flat=False) if fg_mask[np.isfinite(fg_mask)].sum() == 0 or bg_mask[np.isfinite( bg_mask)].sum() == 0: return element.clone([], vdims=['Foreground'], new_type=gv.Image, crs=element.crs) mask = np.where(fg_mask, fg_mask, 2) mask = np.where(bg_mask, 0, mask).copy() bgdModel = np.zeros((1, 65), np.float64) fgdModel = np.zeros((1, 65), np.float64) if isinstance(element, hv.RGB): img = np.dstack([ element.dimension_values(d, flat=False) for d in element.vdims ]) else: img = element.dimension_values(2, flat=False) mask, _, _ = cv.grabCut(img, mask.astype('uint8'), None, bgdModel, fgdModel, self.p.iterations, cv.GC_INIT_WITH_MASK) fg_mask = np.where((mask == 2) | (mask == 0), 0, 1).astype('bool') xs, ys = (element.dimension_values(d, expanded=False) for d in element.kdims) return element.clone((xs, ys, fg_mask), vdims=['Foreground'], new_type=gv.Image, crs=element.crs)
def test_segments_aggregate_sum(self, instance=False): segments = Segments([(0, 1, 4, 1, 2), (1, 0, 1, 4, 4)], vdims=['value']) if instance: agg = rasterize.instance( width=10, height=10, dynamic=False, aggregator='sum' )(segments, width=4, height=4) else: agg = rasterize( segments, width=4, height=4, dynamic=False, aggregator='sum' ) xs = [0.5, 1.5, 2.5, 3.5] ys = [0.5, 1.5, 2.5, 3.5] na = np.nan arr = np.array([ [na, 4, na, na], [2 , 6, 2 , 2 ], [na, 4, na, na], [na, 4, na, na] ]) expected = Image((xs, ys, arr), vdims='value') self.assertEqual(agg, expected)
def __call__(self, dset, **params): self.p = ParamOverrides(self, params) if self.p.vdim is None: vdim = dset.vdims[0].name else: vdim = self.p.vdim ra_range = (ra0, ra1) = dset.range("ra") if self.p.ra_sampling: xsampling = (ra1 - ra0) / self.p.ra_sampling else: xsampling = None dec_range = (dec0, dec1) = dset.range("dec") if self.p.dec_sampling: ysampling = (dec1 - dec0) / self.p.dec_sampling else: ysampling = None if self.p.aggregator == "mean": aggregator = ds.mean(vdim) elif self.p.aggregator == "std": aggregator = ds.std(vdim) elif self.p.aggregator == "count": aggregator = ds.count() sky_range = RangeXY() if self.p.range_stream: def redim(dset, x_range, y_range): ranges = {} if x_range and all(isfinite(v) for v in x_range): ranges["ra"] = x_range if y_range and all(isfinite(v) for v in x_range): ranges["dec"] = y_range return dset.redim.range(**ranges) if ranges else dset dset = dset.apply(redim, streams=[self.p.range_stream]) link_streams(self.p.range_stream, sky_range) streams = [sky_range, PlotSize()] pts = dset.apply(skypoints, streams=[self.p.filter_stream]) reset = PlotReset(source=pts) reset.add_subscriber(partial(reset_stream, None, [self.p.range_stream])) rasterize_inst = rasterize.instance(aggregator=aggregator, streams=streams, x_sampling=xsampling, y_sampling=ysampling) raster_pts = apply_when( pts, operation=rasterize_inst, predicate=lambda pts: len(pts) > self.p.max_points) return raster_pts.opts( opts.Image( bgcolor="black", colorbar=True, cmap=self.p.cmap, min_height=100, responsive=True, tools=["hover"], symmetric=True, ), opts.Points( color=vdim, cmap=self.p.cmap, framewise=True, size=self.p.decimate_size, tools=["hover"], symmetric=True, ), opts.Overlay(hooks=[ partial(reset_hook, x_range=ra_range, y_range=dec_range) ]), )
def __call__(self, dset, **params): self.p = ParamOverrides(self, params) if self.p.xdim not in dset.dimensions(): raise ValueError("{} not in Dataset.".format(self.p.xdim)) if self.p.ydim not in dset.dimensions(): raise ValueError("{} not in Dataset.".format(self.p.ydim)) if ("ra" not in dset.dimensions()) or ("dec" not in dset.dimensions()): raise ValueError("ra and/or dec not in Dataset.") # Compute sampling ra_range = (ra0, ra1) = dset.range("ra") if self.p.ra_sampling: ra_sampling = (ra1 - ra0) / self.p.x_sampling else: ra_sampling = None dec_range = (dec0, dec1) = dset.range("dec") if self.p.dec_sampling: dec_sampling = (dec1 - dec0) / self.p.y_sampling else: dec_sampling = None x_range = (x0, x1) = dset.range(self.p.xdim) if self.p.x_sampling: x_sampling = (x1 - x0) / self.p.x_sampling else: x_sampling = None y_range = (y0, y1) = dset.range(self.p.ydim) if self.p.y_sampling: y_sampling = (y1 - y0) / self.p.y_sampling else: y_sampling = None # Set up scatter plot scatter_range = RangeXY() if self.p.scatter_range_stream: def redim_scatter(dset, x_range, y_range): ranges = {} if x_range and all(isfinite(v) for v in x_range): ranges[self.p.xdim] = x_range if y_range and all(isfinite(v) for v in x_range): ranges[self.p.ydim] = y_range return dset.redim.range(**ranges) if ranges else dset dset_scatter = dset.apply(redim_scatter, streams=[self.p.scatter_range_stream]) link_streams(self.p.scatter_range_stream, scatter_range) else: dset_scatter = dset scatter_pts = dset_scatter.apply(filterpoints, streams=[self.p.filter_stream], xdim=self.p.xdim, ydim=self.p.ydim) scatter_streams = [scatter_range, PlotSize()] scatter_rasterize = rasterize.instance(streams=scatter_streams, x_sampling=x_sampling, y_sampling=y_sampling) cmap = (process_cmap(self.p.scatter_cmap)[:250] if self.p.scatter_cmap == "fire" else self.p.scatter_cmap) scatter_rasterized = apply_when( scatter_pts, operation=scatter_rasterize, predicate=lambda pts: len(pts) > self.p.max_points ).opts( opts.Image(clim=(1, np.nan), clipping_colors={"min": "transparent"}, cmap=cmap), opts.Points(clim=(1, np.nan), clipping_colors={"min": "transparent"}, cmap=cmap), opts.Overlay( hooks=[partial(reset_hook, x_range=x_range, y_range=y_range)]), ) # Set up sky plot sky_range = RangeXY() if self.p.sky_range_stream: def redim_sky(dset, x_range, y_range): ranges = {} if x_range and all(isfinite(v) for v in x_range): ranges["ra"] = x_range if y_range and all(isfinite(v) for v in x_range): ranges["dec"] = y_range return dset.redim.range(**ranges) if ranges else dset dset_sky = dset.apply(redim_sky, streams=[self.p.sky_range_stream]) link_streams(self.p.sky_range_stream, sky_range) else: dset_sky = dset sky_pts = dset_sky.apply(filterpoints, xdim="ra", ydim="dec", set_title=False, streams=[self.p.filter_stream]) skyplot_streams = [sky_range, PlotSize()] sky_rasterize = rasterize.instance( aggregator=ds.mean(self.p.ydim), streams=skyplot_streams, x_sampling=ra_sampling, y_sampling=dec_sampling, ) sky_rasterized = apply_when( sky_pts, operation=sky_rasterize, predicate=lambda pts: len(pts) > self.p.max_points).opts( opts.Image(bgcolor="black", cmap=self.p.sky_cmap, symmetric=True), opts.Points(bgcolor="black", cmap=self.p.sky_cmap, symmetric=True), opts.Overlay(hooks=[ partial(reset_hook, x_range=ra_range, y_range=dec_range) ]), ) # Set up BoundsXY streams to listen to box_select events and notify FilterStream scatter_select = BoundsXY(source=scatter_pts) scatter_notifier = partial(notify_stream, filter_stream=self.p.filter_stream, xdim=self.p.xdim, ydim=self.p.ydim) scatter_select.add_subscriber(scatter_notifier) sky_select = BoundsXY(source=sky_pts) sky_notifier = partial(notify_stream, filter_stream=self.p.filter_stream, xdim="ra", ydim="dec") sky_select.add_subscriber(sky_notifier) # Reset reset = PlotReset(source=sky_pts) reset.add_subscriber( partial(reset_stream, self.p.filter_stream, [self.p.sky_range_stream, self.p.scatter_range_stream])) raw_scatterpts = filterpoints(dset, xdim=self.p.xdim, ydim=self.p.ydim) raw_scatter = datashade( raw_scatterpts, cmap=list(Greys9[::-1][:5]), streams=scatter_streams, x_sampling=x_sampling, y_sampling=y_sampling, ) scatter_p = raw_scatter * scatter_rasterized if self.p.show_rawsky: raw_skypts = filterpoints(dset, xdim=self.p.xdim, ydim=self.p.ydim) raw_sky = datashade( rawskypts, cmap=list(Greys9[::-1][:5]), streams=skyplot_streams, x_sampling=ra_sampling, y_sampling=dec_sampling, ) sky_p = raw_sky * sky_rasterized else: sky_p = sky_rasterized if self.p.show_table: table = dset.apply(summary_table, ydim=self.p.ydim, streams=[self.p.filter_stream]) table = table.opts() layout = table + scatter_p + sky_p else: layout = (scatter_p + sky_p).opts(sizing_mode="stretch_width") return layout.opts( opts.Image(colorbar=True, responsive=True, tools=["box_select", "hover"]), opts.Layout(sizing_mode="stretch_width"), opts.Points(color=self.p.ydim, tools=["hover"]), opts.RGB(alpha=0.5), opts.Table(width=200), )