Advertisement
Guest User

bokeh_help

a guest
Jan 23rd, 2016
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. from bokeh import plotting as bkplot
  4. from bokeh import io as bkio
  5. from bokeh import client as bkclient
  6. from bokeh import models as bkmodels
  7. from bokeh.models import widgets
  8. from datetime import datetime
  9.  
  10.  
  11. class Foo(object):
  12.     def __init__(self, num_points, strategy=3):
  13.         np.random.seed(100)
  14.         self.strategy = strategy
  15.         # Generate data
  16.         self.coords = pd.DataFrame(data=np.random.randint(0, 200, (num_points,2)),
  17.                                    index=['p{}'.format(i) for i in range(num_points)],
  18.                                    columns=list('xy'))
  19.         self.coords['color'] = 'steelblue'
  20.         # Generate plots and widgets
  21.         self.graph_fig = None
  22.         self.graph_plot = None
  23.         self.query = None
  24.  
  25.         # Create initial plots
  26.         self.display_graph()
  27.         self.display_query()
  28.  
  29.         #
  30.         self.graph_plot.data_source.on_change('selected', self.graph_update)
  31.         self.query.on_change('value', self.query_update)
  32.         return
  33.  
  34.     def create_page(self):
  35.         bkplot.show(bkplot.hplot(self.graph_fig, self.query, width=800))
  36.         return
  37.  
  38.     def display_graph(self):
  39.         # Generate source
  40.         graph_source = bkmodels.ColumnDataSource(self.coords)
  41.         # Create figure
  42.         self.graph_fig = bkplot.figure(title='Location', tools='pan,box_zoom,box_select,reset')
  43.         self.graph_fig.select(bkmodels.BoxSelectTool).select_every_mousemove = False
  44.         hover_tool = bkmodels.HoverTool(tooltips=[('index', '@index')])
  45.         self.graph_fig.add_tools(hover_tool)
  46.         # Create plot
  47.         self.graph_plot = self.graph_fig.scatter('x', 'y', source=graph_source, color='color', alpha=0.6)
  48.         return
  49.  
  50.     def display_query(self):
  51.         self.query = widgets.TextInput(value='')
  52.         return
  53.  
  54.     def refresh_graph(self, selected_points=None, old_idxs=None, new_idxs=None):
  55.         # Strategy 1: Cherry pick current plot's source.
  56.         # Compute time for 100 points: < 1ms.
  57.         if self.strategy == 1:
  58.             t1 = datetime.now()
  59.             for idx in old_idxs:
  60.                 self.graph_plot.data_source.data['color'][idx] = 'steelblue'
  61.             for idx in new_idxs:
  62.                 self.graph_plot.data_source.data['color'][idx] = 'red'
  63.             print('Strategy #1 completed in {}'.format(datetime.now() - t1))
  64.         # Strategy 2: Create new source.
  65.         # Compute time for 100 points: ~ 70ms.
  66.         elif self.strategy == 2:
  67.             t2 = datetime.now()
  68.             self.coords['color'] = 'steelblue'
  69.             self.coords.loc[selected_points, 'color'] = 'red'
  70.             self.graph_plot.data_source = bkmodels.ColumnDataSource(self.coords)
  71.             print('Strategy #2 completed in {}'.format(datetime.now() - t2))
  72.         # Strategy 3: Create an entirely new plot.
  73.         # Computer time for 100 points: ~ 400ms.
  74.         else:
  75.             t3 = datetime.now()
  76.             self.coords['color'] = 'steelblue'
  77.             self.coords.loc[selected_points, 'color'] = 'red'
  78.             new_source = bkmodels.ColumnDataSource(self.coords)
  79.             self.graph_plot = self.graph_fig.scatter('x', 'y', source=new_source, color='color', alpha=0.6)
  80.             print('Strategy #3 completed in {}'.format(datetime.now() - t3))
  81.         return
  82.  
  83.     def graph_update(self, attr, old, new):
  84.         new_idxs = np.asarray(new['1d']['indices'])
  85.         old_idxs = np.asarray(old['1d']['indices'])
  86.         selected_points = self.coords.index[new_idxs]
  87.  
  88.         # Refresh the graph.
  89.         self.refresh_graph(selected_points, new_idxs, old_idxs)
  90.         return
  91.  
  92.     def query_update(self, attr, old, new):
  93.         if self.strategy == 1:
  94.             print('Strategy #1 requires indices that were not provided. Nothing done.')
  95.             return
  96.  
  97.         # Read in comma separated point names to a list.
  98.         selected_points = [x.strip() for x in self.query.value.split(',')]
  99.  
  100.         # Refresh the graph.
  101.         self.refresh_graph(selected_points)
  102.         return
  103.  
  104. p = Foo(10000, strategy=3)
  105. p.create_page()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement