Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- from bokeh import plotting as bkplot
- from bokeh import io as bkio
- from bokeh import client as bkclient
- from bokeh import models as bkmodels
- from bokeh.models import widgets
- from datetime import datetime
- class Foo(object):
- def __init__(self, num_points, strategy=3):
- np.random.seed(100)
- self.strategy = strategy
- # Generate data
- self.coords = pd.DataFrame(data=np.random.randint(0, 200, (num_points,2)),
- index=['p{}'.format(i) for i in range(num_points)],
- columns=list('xy'))
- self.coords['color'] = 'steelblue'
- # Generate plots and widgets
- self.graph_fig = None
- self.graph_plot = None
- self.query = None
- # Create initial plots
- self.display_graph()
- self.display_query()
- #
- self.graph_plot.data_source.on_change('selected', self.graph_update)
- self.query.on_change('value', self.query_update)
- return
- def create_page(self):
- bkplot.show(bkplot.hplot(self.graph_fig, self.query, width=800))
- return
- def display_graph(self):
- # Generate source
- graph_source = bkmodels.ColumnDataSource(self.coords)
- # Create figure
- self.graph_fig = bkplot.figure(title='Location', tools='pan,box_zoom,box_select,reset')
- self.graph_fig.select(bkmodels.BoxSelectTool).select_every_mousemove = False
- hover_tool = bkmodels.HoverTool(tooltips=[('index', '@index')])
- self.graph_fig.add_tools(hover_tool)
- # Create plot
- self.graph_plot = self.graph_fig.scatter('x', 'y', source=graph_source, color='color', alpha=0.6)
- return
- def display_query(self):
- self.query = widgets.TextInput(value='')
- return
- def refresh_graph(self, selected_points=None, old_idxs=None, new_idxs=None):
- # Strategy 1: Cherry pick current plot's source.
- # Compute time for 100 points: < 1ms.
- if self.strategy == 1:
- t1 = datetime.now()
- for idx in old_idxs:
- self.graph_plot.data_source.data['color'][idx] = 'steelblue'
- for idx in new_idxs:
- self.graph_plot.data_source.data['color'][idx] = 'red'
- print('Strategy #1 completed in {}'.format(datetime.now() - t1))
- # Strategy 2: Create new source.
- # Compute time for 100 points: ~ 70ms.
- elif self.strategy == 2:
- t2 = datetime.now()
- self.coords['color'] = 'steelblue'
- self.coords.loc[selected_points, 'color'] = 'red'
- self.graph_plot.data_source = bkmodels.ColumnDataSource(self.coords)
- print('Strategy #2 completed in {}'.format(datetime.now() - t2))
- # Strategy 3: Create an entirely new plot.
- # Computer time for 100 points: ~ 400ms.
- else:
- t3 = datetime.now()
- self.coords['color'] = 'steelblue'
- self.coords.loc[selected_points, 'color'] = 'red'
- new_source = bkmodels.ColumnDataSource(self.coords)
- self.graph_plot = self.graph_fig.scatter('x', 'y', source=new_source, color='color', alpha=0.6)
- print('Strategy #3 completed in {}'.format(datetime.now() - t3))
- return
- def graph_update(self, attr, old, new):
- new_idxs = np.asarray(new['1d']['indices'])
- old_idxs = np.asarray(old['1d']['indices'])
- selected_points = self.coords.index[new_idxs]
- # Refresh the graph.
- self.refresh_graph(selected_points, new_idxs, old_idxs)
- return
- def query_update(self, attr, old, new):
- if self.strategy == 1:
- print('Strategy #1 requires indices that were not provided. Nothing done.')
- return
- # Read in comma separated point names to a list.
- selected_points = [x.strip() for x in self.query.value.split(',')]
- # Refresh the graph.
- self.refresh_graph(selected_points)
- return
- p = Foo(10000, strategy=3)
- p.create_page()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement