def modify_doc(doc): plot = Plot(height=400, width=400, x_range=Range1d(0, 3), y_range=Range1d(0, 3), min_border=0) renderer = plot.add_glyph(source, MultiLine(xs='xs', ys='ys')) tool = PolyEditTool(renderers=[renderer]) psource = ColumnDataSource(dict(x=[], y=[])) prenderer = plot.add_glyph(psource, Circle(x='x', y='y', size=10)) tool.vertex_renderer = prenderer plot.add_tools(tool) plot.toolbar.active_multi = tool plot.toolbar_sticky = False div = Div(text='False') def cb(attr, old, new): try: if cds_data_almost_equal(new, expected): div.text = 'True' except ValueError: return source.on_change('data', cb) code = RECORD("matches", "div.text") plot.add_tools( CustomAction(callback=CustomJS(args=dict(div=div), code=code))) doc.add_root(column(plot, div))
def _make_plot(): data = {"xs": [[1, 2], [1.6, 2.45]], "ys": [[1, 1], [1.5, 0.75]]} source = ColumnDataSource(data) plot = Plot(plot_height=400, plot_width=400, x_range=Range1d(0, 3), y_range=Range1d(0, 3), min_border=0) renderer = plot.add_glyph(source, MultiLine(xs='xs', ys='ys', line_width=10)) tool = PolyEditTool(renderers=[renderer]) psource = ColumnDataSource(dict(x=[], y=[])) prenderer = plot.add_glyph(psource, Circle(x='x', y='y', size=10)) tool.vertex_renderer = prenderer plot.add_tools(tool) plot.toolbar.active_multi = tool code = RECORD("xs", "source.data.xs") + RECORD("ys", "source.data.ys") plot.add_tools(CustomAction(callback=CustomJS(args=dict(source=source), code=code))) plot.toolbar_sticky = False return plot
def __init__( self, dataf, labels, x, y, size=5, alpha=0.5, width=400, height=400, color=None, legend=True, ): self.uuid = str(uuid.uuid4())[:10] self.x = x self.y = y self.plot = figure(width=width, height=height, title=f"{x} vs. {y}") self.color_column = labels if isinstance(labels, str) else color self._colors = ["red", "blue", "green", "purple", "cyan"] self.legend = legend if isinstance(labels, str): self.labels = list(dataf[labels].unique()) d = {k: col for k, col in zip(self.labels, self._colors)} dataf = dataf.assign(color=[d[lab] for lab in dataf[labels]]) self.source = ColumnDataSource(data=dataf) else: if not self.color_column: dataf = dataf.assign(color=["gray" for _ in range(dataf.shape[0])]) else: color_labels = list(dataf[self.color_column].unique()) d = {k: col for k, col in zip(color_labels, self._colors)} dataf = dataf.assign(color=[d[lab] for lab in dataf[self.color_column]]) self.source = ColumnDataSource(data=dataf) self.labels = labels if len(self.labels) > 5: raise ValueError("We currently only allow for 5 classes max.") self.plot.circle( x=x, y=y, color="color", source=self.source, size=size, alpha=alpha ) # Create all the tools for drawing self.poly_patches = {} self.poly_draw = {} for k, col in zip(self.labels, self._colors): self.poly_patches[k] = self.plot.patches( [], [], fill_color=col, fill_alpha=0.4, line_alpha=0.0 ) icon_path = pathlib.Path(resource_filename("hulearn", f"images/{col}.png")) self.poly_draw[k] = PolyDrawTool( renderers=[self.poly_patches[k]], custom_icon=icon_path ) c = self.plot.circle([], [], size=5, color="black") edit_tool = PolyEditTool( renderers=list(self.poly_patches.values()), vertex_renderer=c ) self.plot.add_tools(*self.poly_draw.values(), edit_tool) self.plot.toolbar.active_tap = self.poly_draw[self.labels[0]]
def _addLesionForm(self, form, color, plot): ''' Method adds lesion form, either true or pred Parameters --------- plot: instance of bokeh figure form: true or predicted form of the lesion color: color of the lesion (red=true, blue=pred) Output ------ Returns instance of a polygon ''' #to uint form = np.uint8(form) #Find contours cnts = cv2.findContours(form, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[1] #Scale to correct xy coords xy_coords = cnts[0][:, 0, :] xy_coords[:, 1] = (form.shape[0] - 1) - xy_coords[:, 1] #first is number of rows, so y #Lists of x and y coords xs_m = list(xy_coords[:, 0]) ys_m = list(xy_coords[:, 1]) #Scale xs_m = [i * self.scale for i in xs_m] ys_m = [i * self.scale for i in ys_m] ############## #Add pplygon ############## p1 = plot.patches([], [], fill_alpha=0.4) c1 = plot.circle([], [], size=10, color=color) poly = plot.patches(xs=[xs_m], ys=[ys_m], fill_color=color, line_color=color, line_alpha=0.5, fill_alpha=0.5) #Add edit tool (only for pred) if color == 'blue': edit_tool = PolyEditTool(renderers=[p1, poly], vertex_renderer=c1) plot.add_tools(edit_tool) plot.toolbar.active_drag = edit_tool return poly, c1
def initialize(self, plot_id=None): plot = self.plot vertex_tool = None if all(s.shared for s in self.streams): tools = [tool for tool in plot.state.tools if isinstance(tool, PolyEditTool)] vertex_tool = tools[0] if tools else None if vertex_tool is None: vertex_style = dict({'size': 10}, **self.streams[0].vertex_style) r1 = plot.state.scatter([], [], **vertex_style) vertex_tool = PolyEditTool(vertex_renderer=r1) plot.state.tools.append(vertex_tool) vertex_tool.renderers.append(plot.handles['glyph_renderer']) self._update_cds_vdims() CDSCallback.initialize(self, plot_id)
def initialize(self, plot_id=None): try: from bokeh.models import PolyEditTool except: param.main.warning('PolyEdit requires bokeh >= 0.12.14') return plot = self.plot vertex_tool = None if all(s.shared for s in self.streams): tools = [tool for tool in plot.state.tools if isinstance(tool, PolyEditTool)] vertex_tool = tools[0] if tools else None if vertex_tool is None: vertex_style = dict(size=10, **self.streams[0].vertex_style) r1 = plot.state.scatter([], [], **vertex_style) vertex_tool = PolyEditTool(vertex_renderer=r1) plot.state.tools.append(vertex_tool) vertex_tool.renderers.append(plot.handles['glyph_renderer']) super(PolyEditCallback, self).initialize(plot_id)
def initialize(self, plot_id=None): plot = self.plot cds = plot.handles['cds'] vertex_tool = None if all(s.shared for s in self.streams): tools = [ tool for tool in plot.state.tools if isinstance(tool, PolyEditTool) ] vertex_tool = tools[0] if tools else None stream = self.streams[0] kwargs = {} if stream.tooltip: kwargs[CUSTOM_TOOLTIP] = stream.tooltip if vertex_tool is None: vertex_style = dict({'size': 10}, **stream.vertex_style) r1 = plot.state.scatter([], [], **vertex_style) vertex_tool = PolyEditTool(vertex_renderer=r1, **kwargs) plot.state.tools.append(vertex_tool) vertex_tool.renderers.append(plot.handles['glyph_renderer']) self._update_cds_vdims(cds.data) CDSCallback.initialize(self, plot_id)
def create_poly_edit_tool(plot: figure): p1 = plot.patches([], [], fill_alpha=0.4) c1 = plot.circle([], [], size=10, color='red') return PolyEditTool(renderers = [p1], vertex_renderer = c1)
from bokeh.models import PolyDrawTool, PolyEditTool from bokeh.plotting import figure, output_file, show output_file("tools_poly_edit.html") p = figure(x_range=(0, 10), y_range=(0, 10), width=400, height=400, title='Poly Edit Tool') p1 = p.patches([], [], fill_alpha=0.4) p2 = p.patches([[1, 2, 3]], [[3, 5, 2]], fill_color='green', fill_alpha=0.4) c1 = p.circle([], [], size=10, color='red') draw_tool = PolyDrawTool(renderers=[p1, p2]) edit_tool = PolyEditTool(renderers=[p1, p2], vertex_renderer=c1) p.add_tools(draw_tool, edit_tool) p.toolbar.active_drag = edit_tool show(p)
def create_image_figure( image_source: ColumnDataSource, roi_source: ColumnDataSource, vector_source: ColumnDataSource, ) -> Figure: try: image = image_source.data['image'][0] width = image.shape[1] height = image.shape[0] except IndexError: width = 800 height = 800 plot = figure( plot_width=min(width, 800), plot_height=min(height, 800), x_range=[0, width], y_range=[0, height], title='Selected Image', name='image_figure', ) plot.image( image='image', x=0, y=0, dw='dw', dh='dh', source=image_source, palette='Spectral11', name='image_plot', ) hover = HoverTool(tooltips=[('x', '$x'), ('y', '$y'), ('value', '@image')]) r1 = plot.rect( x='x', y='y', width='width', height='height', source=roi_source, fill_alpha=0.5, fill_color='#DAF7A6', name='rois', ) lines = plot.multi_line( xs='xs', ys='ys', source=vector_source, line_color='red', line_width=2, name='vectors', ) circles = plot.circle( x=[], y=[], size=10, color='yellow', ) plot.tools = [ hover, BoxEditTool(renderers=[r1]), PolyDrawTool(renderers=[lines]), PolyEditTool(renderers=[lines], vertex_renderer=circles), ] plot.toolbar.active_inspect = [] return plot