def get_grid(data_by_node, attributes): """Create the ColumnDataSource instance as used by Bokeh for plotting the grid. It transforms the pandas dataframe into the appropriate structure. Todo: link the node id to a list of articles IDs for further bindings to URIs in the database Parameters: ----------- data_by_node: pandas dataframe Contains the node data with the list of nodes linking to the cluster ID and the main topics, and article hits attributes: dictionary Contains the main attributes of the data modelling session. """ n_clusters = attributes["n_clusters"] k_shape = attributes["kshape"] colors = ["#%02x%02x%02x" % (np.floor(250.0 * (float(n) / n_clusters)), 100, 100) for n in xrange(n_clusters)] colormap = {i: colors[i] for i in xrange(n_clusters)} source = ColumnDataSource(data_by_node) source.add([colormap[cl] for cl in source.data["cluster"]], "color") x_range = [str(x) for x in xrange(k_shape[0])] y_range = [str(x) for x in xrange(k_shape[1])] return source, x_range, y_range
def get_bokeh_fig(): from bokeh.plotting import Figure # , gridplot from bokeh.models import ColumnDataSource, HoverTool results, varied_keys, varied_vals = read() include_keys = varied_keys + [ 'nfev', 'njev', 'nprec_setup', 'nprec_solve', 'njacvec_dot', 'nprec_solve_ilu', 'nprec_solve_lu', "n_steps", "n_rhs_evals", "n_lin_solv_setups", "n_err_test_fails", "n_nonlin_solv_iters", "n_nonlin_solv_conv_fails", "krylov_n_lin_iters", "krylov_n_prec_evals", "krylov_n_prec_solves", "krylov_n_conv_fails", "krylov_n_jac_times_evals", "krylov_n_iter_rhs" ] cols = [xkey, ykey, 'color'] + include_keys sources = {} varied3 = varied_vals[2] keys = list(results.keys()) vals = list(results.values()) for val in varied3: sources[val] = ColumnDataSource(data={k: [] for k in cols}) for k in cols: sources[val].data[k] = [vals[idx].get(k, None) for idx in range(len(vals)) if keys[idx][2] == val] hover = HoverTool(tooltips=[(k, '@'+k) for k in include_keys]) top = Figure( plot_height=600, plot_width=800, title="%s vs. %s" % (ykey, xkey), x_axis_type="linear", y_axis_type="log", tools=[ hover, 'pan', 'reset', 'box_zoom', 'wheel_zoom', 'save']) top.xaxis.axis_label = xkey top.yaxis.axis_label = ykey for source, marker in zip(sources.values(), ['circle', 'diamond']): top.scatter(x=xkey, y=ykey, source=source, size=9, color="color", line_color=None, marker=marker) return top
class CommunicatingTimeSeries(DashboardComponent): def __init__(self, worker, **kwargs): self.worker = worker self.source = ColumnDataSource({'x': [], 'in': [], 'out': []}) x_range = DataRange1d(follow='end', follow_interval=20000, range_padding=0) fig = figure(title="Communication History", x_axis_type='datetime', y_range=[-0.1, worker.total_out_connections + 0.5], height=150, tools='', x_range=x_range, **kwargs) fig.line(source=self.source, x='x', y='in', color='red') fig.line(source=self.source, x='x', y='out', color='blue') fig.add_tools( ResetTool(), PanTool(dimensions="width"), WheelZoomTool(dimensions="width") ) self.root = fig @without_property_validation def update(self): with log_errors(): self.source.stream({'x': [time() * 1000], 'out': [len(self.worker._comms)], 'in': [len(self.worker.in_flight_workers)]}, 10000)
def trail_map(data): lon = (min(data.lon) + max(data.lon))/2 lat = (min(data.lat) + max(data.lat))/2 map_options = GMapOptions(lng=lon, lat=lat, zoom=13) plot = GMapPlot(title="%s - Trail Map" % title, map_options=map_options, plot_width=800, plot_height=800) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') xgrid = Grid(plot=plot, dimension=0, ticker=xaxis.ticker, grid_line_dash="dashed", grid_line_color="gray") ygrid = Grid(plot=plot, dimension=1, ticker=yaxis.ticker, grid_line_dash="dashed", grid_line_color="gray") plot.renderers.extend([xgrid, ygrid]) plot.add_tools(PanTool(), WheelZoomTool(), ResetTool(), BoxSelectTool()) line_source = ColumnDataSource(dict(x=data.lon, y=data.lat, dist=data.dist)) line = Line(x="x", y="y", line_color="blue", line_width=2) plot.add_glyph(line_source, line) plot.x_range = DataRange1d(sources=[line_source.columns("x")]) plot.y_range = DataRange1d(sources=[line_source.columns("y")]) return plot
def plot_interactive_timeseries(x, y, data, title, hover_cols=None): hover_cols = hover_cols or list(data.columns) df = data[hover_cols].dropna(axis=0, how='any') source = ColumnDataSource(df) tooltips = [] for col in df.columns: df[col].dtype if df[col].dtype == pd.np.dtype('datetime64[ns]'): # TODO: fix air_date is off by a day # tz_localize('US/Eastern') source.add(df[col].map(lambda x: x.strftime('%x')), col + "_str") tooltips.append([col, "@" + col + "_str"]) elif df[col].dtype == pd.np.dtype('float64'): tooltips.append([col, "@" + col + "{1.11}"]) else: tooltips.append([col, "@" + col]) p = figure( plot_width=bokeh.charts.defaults.width, plot_height=bokeh.charts.defaults.height, x_axis_type="datetime", title=title) p.axis[1].formatter.use_scientific = False hover = HoverTool(tooltips=tooltips) p.circle(x=x, y=y, line_width=2, source=source, size=5) p.add_tools(hover) p.toolbar.logo = None return p
class ExecutingTimeSeries(DashboardComponent): def __init__(self, worker, **kwargs): self.worker = worker self.source = ColumnDataSource({'x': [], 'y': []}) x_range = DataRange1d(follow='end', follow_interval=20000, range_padding=0) fig = figure(title="Executing History", x_axis_type='datetime', y_range=[-0.1, worker.ncores + 0.1], height=150, tools='', x_range=x_range, **kwargs) fig.line(source=self.source, x='x', y='y') fig.add_tools( ResetTool(), PanTool(dimensions="width"), WheelZoomTool(dimensions="width") ) self.root = fig @without_property_validation def update(self): with log_errors(): self.source.stream({'x': [time() * 1000], 'y': [len(self.worker.executing)]}, 1000)
def test_patch_notification(self): d = document.Document() assert not d.roots m = ColumnDataSource(data=dict(a=[10,11], b=[20,21])) d.add_root(m) assert len(d.roots) == 1 assert curdoc() is not d events = [] curdoc_from_listener = [] def listener(event): curdoc_from_listener.append(curdoc()) events.append(event) d.on_change(listener) m.patch(dict(a=[(0, 1)], b=[(0,0), (1,1)])) assert events event = events[0] assert isinstance(event, ModelChangedEvent) assert isinstance(event.hint, ColumnsPatchedEvent) assert event.document == d assert event.model == m assert event.hint.column_source == m assert event.hint.patches == dict(a=[(0, 1)], b=[(0,0), (1,1)]) assert event.attr == 'data' # old == new because stream events update in-place assert event.old == dict(a=[1, 11], b=[0, 1]) assert event.new == dict(a=[1, 11], b=[0, 1]) assert len(curdoc_from_listener) == 1 assert curdoc_from_listener[0] is d
def test_stream_notification(self): d = document.Document() assert not d.roots m = ColumnDataSource(data=dict(a=[10], b=[20])) d.add_root(m) assert len(d.roots) == 1 assert curdoc() is not d events = [] curdoc_from_listener = [] def listener(event): curdoc_from_listener.append(curdoc()) events.append(event) d.on_change(listener) m.stream(dict(a=[11, 12], b=[21, 22]), 200) assert events event = events[0] assert isinstance(event, ModelChangedEvent) assert isinstance(event.hint, ColumnsStreamedEvent) assert event.document == d assert event.model == m assert event.hint.column_source == m assert event.hint.data == dict(a=[11, 12], b=[21, 22]) assert event.hint.rollover == 200 assert event.attr == 'data' # old == new because stream events update in-place assert event.old == dict(a=[10, 11, 12], b=[20, 21, 22]) assert event.new == dict(a=[10, 11, 12], b=[20, 21, 22]) assert len(curdoc_from_listener) == 1 assert curdoc_from_listener[0] is d
def stock(ticker1, ticker2): pretext = PreText(text="", width=500) df = get_data(ticker1, ticker2) source = ColumnDataSource(data=df) source.tags = ['main_source'] p = figure( title="%s vs %s" % (ticker1, ticker2), plot_width=400, plot_height=400, tools="pan,wheel_zoom,box_select,reset", title_text_font_size="10pt", ) p.circle(ticker1 + "_returns", ticker2 + "_returns", size=2, nonselection_alpha=0.02, source=source ) stats = df.describe() pretext.text = str(stats) row1 = HBox(children=[p, pretext]) hist1 = hist_plot(df, ticker1) hist2 = hist_plot(df, ticker2) row2 = HBox(children=[hist1, hist2]) line1 = line_plot(ticker1, source) line2 = line_plot(ticker2, source, line1.x_range) output = VBox(children=[row1, row2, line1, line2]) return output
def stock2(ticker1, ticker2): pretext = PreText(text="", width=500) df = get_data(ticker1, ticker2) source = ColumnDataSource(data=df) source.tags = ['main_source'] p = figure( title="%s vs %s" % (ticker1, ticker2), plot_width=400, plot_height=400, tools="pan,wheel_zoom,box_select,reset", title_text_font_size="10pt", ) p.circle(ticker1 + "_returns", ticker2 + "_returns", size=2, nonselection_alpha=0.02, source=source ) stats = df.describe() pretext.text = str(stats) hist1 = hist_plot(df, ticker1) hist2 = hist_plot(df, ticker2) line1 = line_plot(ticker1, source) line2 = line_plot(ticker2, source, line1.x_range) return dict(scatterplot=p, statstext=pretext, hist1=hist1, hist2=hist2, line1=line1, line2=line2)
def make_washmap_all(): data = get_data_with_countries(wat_stats, san_stats) source = ColumnDataSource(data) source.selected = [30] line_data = get_line_data("Morocco") line_source = ColumnDataSource(line_data) wat_map = construct_water_map_tools(source) wat_line = construct_water_line(line_source) wat_text = construct_water_text(source) wat_key = construct_key(WATER_COLOR_RANGE) san_map = construct_san_map_tools(source) san_line = construct_san_line(line_source) san_text = construct_san_text(source) san_key = construct_key(SANITATION_COLOR_RANGE) tabs = Tabs( tabs=[ Panel(title="Water", child=layout_components(wat_map, wat_line, wat_text, wat_key)), Panel(title="Sanitation", child=layout_components(san_map, san_line, san_text, san_key)), ] ) year = Slider(title="Year", name="year", value=1990, start=1990, end=2012, step=1) return vplot(tabs, year)
def __plot_control_chart(self, index): plot_args = self.__conf.plot_args[index] annotations = self.__conf.annotations[index] if not annotations: annotations = PlotAnnotationContainer() plot = Figure(plot_height=500, plot_width=600, x_range=FactorRange(factors=self.__factors, name='x_factors')) plot.toolbar.logo = None plot.title.text = 'Control chart' hover_tool = self.__create_tooltips() plot.add_tools(hover_tool) plot.xaxis.major_label_orientation = pi / 4 plot.xaxis.major_label_standoff = 10 if not self.__values['_calc_value'].empty: if 'color' not in plot_args: plot_args['color'] = 'navy' if 'alpha' not in plot_args: plot_args['alpha'] = 0.5 self.__values['s_fac'] = self.__factors col_ds = ColumnDataSource(self.__values, name='control_data') col_ds.js_on_change('data', CustomJS(code="refresh_maps();")) plot.circle('s_fac', '_calc_value', source=col_ds, name='circle', **plot_args) plot.line('s_fac', '_calc_value', source=col_ds, name='line', **plot_args) min_anno, max_anno = annotations.calc_min_max_annotation( self.__values['_calc_value']) annotations.plot(plot, self.__values['_calc_value']) anno_range = max_anno - min_anno if anno_range and not isnan(anno_range): plot.y_range.start = min_anno - anno_range plot.y_range.end = max_anno + anno_range return plot
def test_patches_hover_still_works_when_a_seleciton_is_preselcted(output_file_url, selenium): # This tests an edge case interaction when Patches (specifically) is used # with a tool that requires hit testing e.g. HitTool AND a selection is # pre-made on the data source driving it. plot = Plot( x_range=Range1d(0, 100), y_range=Range1d(0, 100), min_border=0 ) source = ColumnDataSource(dict( xs=[[0, 50, 50, 0], [50, 100, 100, 50]], ys=[[0, 0, 100, 100], [0, 0, 100, 100]], color=['pink', 'blue'] )) source.selected = { '0d': {'glyph': None, 'indices': []}, '1d': {'indices': [1]}, '2d': {} } plot.add_glyph(source, Patches(xs='xs', ys='ys', fill_color='color')) plot.add_tools(HoverTool()) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') save(plot) selenium.get(output_file_url) assert has_no_console_errors(selenium) # Hover plot and test no error canvas = selenium.find_element_by_tag_name('canvas') actions = ActionChains(selenium) actions.move_to_element_with_offset(canvas, 100, 100) actions.perform() # If this assertion fails then there were likely errors on hover assert has_no_console_errors(selenium)
def source_from_dataframe(dataframe, settings, current_selection): """""" source = ColumnDataSource(dataframe) embeddings_names = list(settings['embeddings'].keys()) selected_name = embeddings_names[current_selection] selected_column_x = settings['embeddings'][selected_name][0] selected_column_y = settings['embeddings'][selected_name][1] # Create empty columns to put selected coordinate data into source.add(dataframe[selected_column_x], name='x') source.add(dataframe[selected_column_y], name='y') return source
def modify_doc(doc): source = ColumnDataSource(dict(x=[1, 2], y=[1, 1])) 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, Circle(x='x', y='y')) tool = PointDrawTool(renderers=[renderer]) plot.add_tools(tool) plot.toolbar.active_multi = tool div = Div(text='False') def cb(attr, old, new): if cds_data_almost_equal(new, expected): div.text = 'True' 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 scatter(): colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'} source = ColumnDataSource(flowers) source.data['colors'] = [colormap[x] for x in flowers['species']] s = figure(title = "Iris Morphology") s.xaxis.axis_label = 'Petal Length' s.yaxis.axis_label = 'Petal Width' s.circle("petal_length", "petal_width", color="colors", source=source, fill_alpha=0.2, size=10, legend="species") # Lets move the legend off-canvas! legend = s.legend[0] legend.border_line_color = None legend.orientation = 'horizontal' legend.location = (0, 0) s.above.append(legend) return s
def test_PropertyValueColumnData_update(mock_notify): from bokeh.document.events import ColumnDataChangedEvent source = ColumnDataSource(data=dict(foo=[10], bar=[20], baz=[30])) pvcd = bcpw.PropertyValueColumnData(source.data) pvcd._register_owner(source, source.lookup('data')) mock_notify.reset_mock() pvcd.update(foo=[11], bar=[21]) assert pvcd == dict(foo=[11], bar=[21], baz=[30]) assert mock_notify.call_count == 1 assert mock_notify.call_args[0] == (source, dict(foo=[10], bar=[20], baz=[30])) assert 'hint' in mock_notify.call_args[1] assert isinstance(mock_notify.call_args[1]['hint'], ColumnDataChangedEvent) assert mock_notify.call_args[1]['hint'].column_source == source assert sorted(mock_notify.call_args[1]['hint'].cols) == ['bar', 'foo']
def make_source(self): self.source = ColumnDataSource(data=self.df) self.source.callback = Callback(args=dict(), code=""" var inds = cb_obj.get('selected')['1d'].indices; var theidx = inds[0]; var d1 = cb_obj.get("data"); var brand = d1["brand"][theidx]; console.log("yep"); console.log(theidx); $.get( "shoes", {id: brand}, function( response ) { console.log("logging rep"); console.log(response); console.log("done logging rep"); $( "#child" ).html( response ); }, "html"); console.log("done"); """) self.make_brand_source()
def create(cls): obj = cls() obj.year = Slider( title="Year", name='year', value=1990, start=1990, end=2012, step=1 ) wat_all_df = get_wat_stats_all_years() san_all_df = get_san_stats_all_years() data = get_data_with_countries(wat_all_df, san_all_df) source = ColumnDataSource(data) source.selected = [30] line_data = get_line_data('Morocco') line_source = ColumnDataSource(line_data) wat_map = construct_water_map_tools(source) wat_line = construct_water_line(line_source) wat_text = construct_water_text(source) wat_key = construct_key(WATER_COLOR_RANGE) san_map = construct_san_map_tools(source) san_line = construct_san_line(line_source) san_text = construct_san_text(source) san_key = construct_key(SANITATION_COLOR_RANGE) obj.source = source obj.line_source = line_source wat_all_df.year = wat_all_df.year.astype(str) san_all_df.year = san_all_df.year.astype(str) obj.wat_all = ColumnDataSource(wat_all_df) obj.san_all = ColumnDataSource(san_all_df) tabs = Tabs( tabs=[ Panel( title="Water", child=layout_components(wat_map, wat_line, wat_text, wat_key) ), Panel( title="Sanitation", child=layout_components(san_map, san_line, san_text, san_key) ) ] ) obj.children = [tabs, obj.year] return obj
def __init__(self, n_rectangles=1000, clear_interval=20000, **kwargs): """ kwargs are applied to the bokeh.models.plots.Plot constructor """ self.n_rectangles = n_rectangles self.clear_interval = clear_interval self.source = ColumnDataSource(data=dict( start=[], duration=[], key=[], name=[], color=[], worker=[], y=[], worker_thread=[], alpha=[]) ) x_range = DataRange1d() y_range = DataRange1d(range_padding=0) self.root = Plot( title=Title(text="Task Stream"), id='bk-task-stream-plot', x_range=x_range, y_range=y_range, toolbar_location="above", min_border_right=35, **kwargs ) self.root.add_glyph( self.source, Rect(x="start", y="y", width="duration", height=0.8, fill_color="color", line_color="color", line_alpha=0.6, fill_alpha="alpha", line_width=3) ) self.root.add_layout(DatetimeAxis(axis_label="Time"), "below") ticker = BasicTicker(num_minor_ticks=0) self.root.add_layout(LinearAxis(axis_label="Worker Core", ticker=ticker), "left") self.root.add_layout(Grid(dimension=1, grid_line_alpha=0.4, ticker=ticker)) hover = HoverTool( point_policy="follow_mouse", tooltips=""" <div> <span style="font-size: 12px; font-weight: bold;">@name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@duration</span> <span style="font-size: 10px;">ms</span> </div> """ ) # export = ExportTool() # export.register_plot(self.root) self.root.add_tools( hover, # export, BoxZoomTool(), ResetTool(reset_size=False), PanTool(dimensions="width"), WheelZoomTool(dimensions="width") ) # Required for update callback self.task_stream_index = [0]
def __init__(self, worker, height=150, **kwargs): self.worker = worker self.names = ['cpu', 'memory', 'read_bytes', 'write_bytes', 'time'] if not WINDOWS: self.names.append('num_fds') self.source = ColumnDataSource({name: [] for name in self.names}) x_range = DataRange1d(follow='end', follow_interval=20000, range_padding=0) tools = 'reset,pan,wheel_zoom' self.cpu = figure(title="CPU", x_axis_type='datetime', height=height, tools=tools, x_range=x_range, **kwargs) self.cpu.line(source=self.source, x='time', y='cpu') self.cpu.yaxis.axis_label = 'Percentage' self.mem = figure(title="Memory", x_axis_type='datetime', height=height, tools=tools, x_range=x_range, **kwargs) self.mem.line(source=self.source, x='time', y='memory') self.mem.yaxis.axis_label = 'Bytes' self.bandwidth = figure(title='Bandwidth', x_axis_type='datetime', height=height, x_range=x_range, tools=tools, **kwargs) self.bandwidth.line(source=self.source, x='time', y='read_bytes', color='red') self.bandwidth.line(source=self.source, x='time', y='write_bytes', color='blue') self.bandwidth.yaxis.axis_label = 'Bytes / second' # self.cpu.yaxis[0].formatter = NumeralTickFormatter(format='0%') self.bandwidth.yaxis[0].formatter = NumeralTickFormatter(format='0.0b') self.mem.yaxis[0].formatter = NumeralTickFormatter(format='0.0b') plots = [self.cpu, self.mem, self.bandwidth] if not WINDOWS: self.num_fds = figure(title='Number of File Descriptors', x_axis_type='datetime', height=height, x_range=x_range, tools=tools, **kwargs) self.num_fds.line(source=self.source, x='time', y='num_fds') plots.append(self.num_fds) if 'sizing_mode' in kwargs: kw = {'sizing_mode': kwargs['sizing_mode']} else: kw = {} if not WINDOWS: self.num_fds.y_range.start = 0 self.mem.y_range.start = 0 self.cpu.y_range.start = 0 self.bandwidth.y_range.start = 0 self.last = 0 self.root = column(*plots, **kw) self.worker.monitor.update()
def make_washmap_map_tools_linked_tabbed(): data = get_data_with_countries(wat_stats, san_stats) source = ColumnDataSource(data) source.selected = [30] wat_map = construct_water_map_tools(source) wat_text = construct_water_text(source) wat_key = construct_key(WATER_COLOR_RANGE) san_map = construct_san_map_tools(source) san_text = construct_san_text(source) san_key = construct_key(SANITATION_COLOR_RANGE) tabs = Tabs( tabs=[ Panel(title="Water", child=hplot(vplot(wat_map), vplot(wat_text, wat_key))), Panel(title="Sanitation", child=hplot(vplot(san_map), vplot(san_text, san_key))), ] ) return vplot(tabs)
def update(): age = df[(df.Location == location.value) & (df.Year == int(year.value))] ages.data = ColumnDataSource.from_df(age) pop = df[df.Location == location.value].groupby(df.Year).Value.sum() new_known = pop[pop.index <= 2010] new_predicted = pop[pop.index >= 2010] known.data = dict(x=new_known.index.map(str), y=new_known.values) predicted.data = dict(x=new_predicted.index.map(str), y=new_predicted.values)
class Visual(Process): """ Visualize the results in a queue """ def __init__(self, vis_q): self.vis = vis_q self.mes = ColumnDataSource(dict(x=[], y=[])) self.alert = ColumnDataSource(dict(x=[], y=[])) self.p = figure(width=1200, height=800, x_axis_type="datetime", title='Streaming demo', tools="xpan,xwheel_zoom,xbox_zoom,reset") self.p.border_fill_color = "whitesmoke" self.p.min_border_left = 80 self.p.line(x='x', y='y', source=self.mes) self.p.circle(x='x', y='y', source=self.mes, fill_color="white", size=8) self.p.square(x='x', y='y', source=self.alert, line_color="orange", fill_color='red', size=10) self.p.xaxis.axis_label = 'Time' self.p.yaxis.axis_label = 'RTT (ms)' self.doc = curdoc() self.session = push_session(self.doc) self.doc.add_root(self.p) self.doc.add_periodic_callback(self.update, 500) self.doc.title = "Streaming demo" self.session.show() self.session.loop_until_closed() # if not add, nothing will show on the screen, yet if added, process can not be joined... super(Visual, self).__init__() def update(self): try: task = self.vis.get(False) except Queue.Empty: pass else: if task != 'STOP': print 'BOKEH received new data.' mes = PingResult(task['rec']) new_data = dict(x=[mes.created], y=[mes.rtt_min]) if task['type'] == 'mes': self.mes.stream(new_data) elif task['type'] == 'alert': self.alert.stream(new_data) else: print 'BOKEH received STOP signal.' self.session.close()
def __init__(self, worker, **kwargs): with log_errors(): self.worker = worker names = ['nbytes', 'duration', 'bandwidth', 'count', 'type', 'inout-color', 'type-color', 'key', 'key-color', 'start', 'stop'] quantities = ['nbytes', 'duration', 'bandwidth', 'count', 'start', 'stop'] colors = ['inout-color', 'type-color', 'key-color'] # self.source = ColumnDataSource({name: [] for name in names}) self.source = ColumnDataSource({ 'nbytes': [1, 2], 'duration': [0.01, 0.02], 'bandwidth': [0.01, 0.02], 'count': [1, 2], 'type': ['int', 'str'], 'inout-color': ['blue', 'red'], 'type-color': ['blue', 'red'], 'key': ['add', 'inc'], 'start': [1, 2], 'stop': [1, 2] }) self.x = Select(title='X-Axis', value='nbytes', options=quantities) self.x.on_change('value', self.update_figure) self.y = Select(title='Y-Axis', value='bandwidth', options=quantities) self.y.on_change('value', self.update_figure) self.size = Select(title='Size', value='None', options=['None'] + quantities) self.size.on_change('value', self.update_figure) self.color = Select(title='Color', value='inout-color', options=['black'] + colors) self.color.on_change('value', self.update_figure) if 'sizing_mode' in kwargs: kw = {'sizing_mode': kwargs['sizing_mode']} else: kw = {} self.control = widgetbox([self.x, self.y, self.size, self.color], width=200, **kw) self.last_outgoing = 0 self.last_incoming = 0 self.kwargs = kwargs self.layout = row(self.control, self.create_figure(**self.kwargs), **kw) self.root = self.layout
def test_box_select(output_file_url, selenium): PLOT_DIM = 600 source = ColumnDataSource(dict( x=[1, 2, 3], y=[3, 2, 3], name=['top_left', 'middle', 'top_right'], )) # Make plot and add a taptool callback that generates an alert plot = figure(tools='box_select', height=PLOT_DIM, width=PLOT_DIM, x_range=[1, 3], y_range=[1, 3]) plot.circle(x='x', y='y', radius=0.2, source=source) source.callback = CustomJS(code=""" var indices = cb_obj.get('selected')['1d'].indices, data = cb_obj.get('data'), selected_names = ''; Bokeh.$.each(indices, function(i, index) { selected_names += data['name'][index]; }); alert(selected_names); """) # Save the plot and start the test save(plot) selenium.get(output_file_url) assert has_no_console_errors(selenium) # Drag a box zoom around middle point canvas = selenium.find_element_by_tag_name('canvas') actions = ActionChains(selenium) actions.move_to_element_with_offset(canvas, PLOT_DIM * 0.25, PLOT_DIM * 0.25) actions.click_and_hold() actions.move_by_offset(PLOT_DIM * 0.5, PLOT_DIM * 0.5) actions.release() actions.perform() # Get the alert from box select and assert that the middle item is selected alert = selenium.switch_to_alert() assert alert.text == 'middle'
def make_source(self): if self.ticker2 == 'Daily Prices': self.source = ColumnDataSource(data=self.df) elif self.ticker2 == 'Daily Returns': self.source = ColumnDataSource(data=daily_returns_df(self.df)) elif self.ticker2 == 'Daily Cum Returns': self.source = ColumnDataSource(data=daily_cum_returns_df(daily_returns_df(self.df),int(self.ticker3))) elif self.ticker2 == 'Rolling Ann. Volatility': self.source = ColumnDataSource(data=annualized_volatility_df(self.df,int(self.ticker3))) elif self.ticker2 == 'Rolling Worst Dly. Loss': self.source = ColumnDataSource(data=worst_daily_loss_df(self.df,int(self.ticker3))) elif self.ticker2 == 'Rolling 95% VaR': self.source = ColumnDataSource(data=VaR_df(self.df,int(self.ticker3),0.95)) elif self.ticker2 == 'Ann. Sharpe Ratio': self.source = ColumnDataSource(data=sharpe_ratio_df(self.df,int(self.ticker3))) elif self.ticker2 == 'Max DD Percentage': self.source = ColumnDataSource(data=draw_down_df(self.df,int(self.ticker3))) elif self.ticker2 == 'Percentage Up Days': self.source = ColumnDataSource(data=per_up_days_df(self.df,int(self.ticker3))) else: self.source = ColumnDataSource(data=self.df)
def create_plot(N, func1, func2, color): N = 300 x = np.linspace(0, 4*np.pi, N) y1 = np.sin(x) y2 = np.cos(x) source = ColumnDataSource() source.add(data=x, name='x') source.add(data=y1, name='y1') source.add(data=y2, name='y2') TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select" s1 = plt.figure(tools=TOOLS, plot_width=350, plot_height=350) s1.scatter('x', 'y1', source=source, fill_color=color) s2 = plt.figure(tools=TOOLS, plot_width=350, plot_height=350) s2.scatter('x', 'y2', source=source, fill_color=color) p = plt.gridplot([[s1,s2]]) return p
def __init__(self, vis_q): self.vis = vis_q self.mes = ColumnDataSource(dict(x=[], y=[])) self.alert = ColumnDataSource(dict(x=[], y=[])) self.p = figure(width=1200, height=800, x_axis_type="datetime", title='Streaming demo', tools="xpan,xwheel_zoom,xbox_zoom,reset") self.p.border_fill_color = "whitesmoke" self.p.min_border_left = 80 self.p.line(x='x', y='y', source=self.mes) self.p.circle(x='x', y='y', source=self.mes, fill_color="white", size=8) self.p.square(x='x', y='y', source=self.alert, line_color="orange", fill_color='red', size=10) self.p.xaxis.axis_label = 'Time' self.p.yaxis.axis_label = 'RTT (ms)' self.doc = curdoc() self.session = push_session(self.doc) self.doc.add_root(self.p) self.doc.add_periodic_callback(self.update, 500) self.doc.title = "Streaming demo" self.session.show() self.session.loop_until_closed() # if not add, nothing will show on the screen, yet if added, process can not be joined... super(Visual, self).__init__()
def plot(self, epochs): global s p = figure(background_fill_color='#F0E8E2', title="Learning curve") s = ColumnDataSource(data=dict(x=self.res, y=self.res)) # s1 = ColumnDataSource(data=dict(x=[]], y=[]]) # p.line('x','y', source=s) # list data # p1.line('x','y', source=s1) # empty r = p.line(x=[],y=[], line_width=2) ds = r.data_source def callback(attr,old,new): global plotiter ds.data['x'].append(self.res[plotiter]) ds.data['y'].append(self.res[plotiter]) ds.trigger('data', ds.data, ds.data) plotiter += 1 print "whats upppppp" s.on_change('data',callback) curdoc().add_root(p)
from os.path import dirname, join import pandas as pd import collections from bokeh.io import curdoc from bokeh.layouts import column, row, layout from bokeh.models import (Button, ColumnDataSource, CustomJS, DataTable, NumberFormatter, TableColumn, Div, Panel) df = pd.read_csv(join(dirname(__file__), '../data/Netflix.csv')) #to keep dict order source = ColumnDataSource(data=collections.OrderedDict()) source.data = { 'Region': df['Area'], 'QTR-YR': df['Years'], 'Revenue': df[' Revenue '].str.replace(',', '').astype(float), 'Subscribers': df[' Subscribers '].str.replace(',', '').astype(float) } columns = [ TableColumn(field='Region', title='Region'), TableColumn(field='QTR-YR', title='QTR-YR'), TableColumn(field='Subscribers', title='Subscribers', formatter=NumberFormatter(format='0,0')), TableColumn(field='Revenue', title='Revenue', formatter=NumberFormatter(format='$0,0')), ] columnNames = [x.field for x in columns]
class DataModel(): """ Processing of data for a Dashboard. """ def __init__(self, doc): # Save reference self.doc = doc # Dataset self._df = DATASET # Entire dataset self._dfs = INITIAL # Pre-processed KPI dataset self.ads_list = PROBES # All probes in the dashboard self.p_range = np.arange(0.5, 20.5, 0.5) # Adsorbate definitions self.g1 = SETTINGS['g1'] self.g2 = SETTINGS['g2'] # Temperature definitions self.t_abs = SETTINGS['t_abs'] self.t_tol = SETTINGS['t_tol'] # Isotherm type definitions self.iso_type = None # Pressure definitions self.lp = '1' # 0.5 bar self.p1 = '1' # 0.5 bar self.p2 = '10' # 5.0 bar # Bokeh-specific data source generation self.data = ColumnDataSource( data=self.gen_data(self.lp, self.p1, self.p2)) self.errors = ColumnDataSource(data=self.gen_error()) self.g1_iso_sel = ColumnDataSource(data=self.gen_iso_dict()) self.g2_iso_sel = ColumnDataSource(data=self.gen_iso_dict()) # Data selection callback self.data.selected.on_change('indices', self.selection_callback) self.data.js_on_change('data', CustomJS(code="toggleLoading()")) def callback_link_sep(self, sep_dash): """Link the separation dashboard to the model.""" # Store reference self.sep_dash = sep_dash # Data type selections def dtype_callback(attr, old, new): if new == 0: self.iso_type = None elif new == 1: self.iso_type = 'exp' elif new == 2: self.iso_type = 'sim' self.sep_dash.data_type.on_change('active', dtype_callback) # Adsorbate drop-down selections def g1_sel_callback(attr, old, new): self.g1 = new def g2_sel_callback(attr, old, new): self.g2 = new self.sep_dash.g1_sel.on_change("value", g1_sel_callback) self.sep_dash.g2_sel.on_change("value", g2_sel_callback) # Temperature selection callback def t_abs_callback(attr, old, new): self.t_abs = new def t_tol_callback(attr, old, new): self.t_tol = new self.sep_dash.t_absolute.on_change("value", t_abs_callback) self.sep_dash.t_tolerance.on_change("value", t_tol_callback) # Update callback self.sep_dash.process.on_click(self.update_data) # Pressure slider self.sep_dash.p_slider.on_change('value_throttled', self.uptake_callback) # Working capacity slider self.sep_dash.wc_slider.on_change('value_throttled', self.wc_callback) # Slider limits if len(self.data.data['labels']) > 0: limit = find_nearest( self.p_range, np.nanmin([ np.nanmax(self.data.data['L_x']), np.nanmax(self.data.data['L_y']) ])) self.sep_dash.p_slider.end = limit self.sep_dash.wc_slider.end = limit # ######################################################################### # Selection update def update_data(self): """What to do when new data is needed.""" # Request calculation in separate thread Thread(target=self.calculate_data).start() # Reset any selected materials if self.data.selected.indices: self.data.selected.update(indices=[]) # Update labels self.sep_dash.top_graph_labels() # Update detail plots self.g1_iso_sel.data = self.gen_iso_dict() self.g2_iso_sel.data = self.gen_iso_dict() self.sep_dash.p_g1iso.title.text = 'Isotherms {0}'.format(self.g1) self.sep_dash.p_g2iso.title.text = 'Isotherms {0}'.format(self.g2) def calculate_data(self): self._dfs = select_data(self._df, self.iso_type, self.t_abs, self.t_tol, self.g1, self.g2) self.doc.add_next_tick_callback(self.push_data) @gen.coroutine def push_data(self): """Assign data""" self.data.data = self.gen_data(self.lp, self.p1, self.p2) # Recalculate slider limits if len(self.data.data['labels']) > 0: limit = find_nearest( self.p_range, np.nanmin([ np.nanmax(self.data.data['L_x']), np.nanmax(self.data.data['L_y']) ])) self.sep_dash.p_slider.end = limit self.sep_dash.wc_slider.end = limit # ######################################################################### # Set up pressure slider and callback def uptake_callback(self, attr, old, new): """Callback on each pressure selected for uptake.""" self.lp = str(int(2 * new)) # regenerate graph data self.data.patch(self.patch_data_l(self.lp)) if self.data.selected.indices: self.errors.patch(self.patch_error_l(self.data.selected.indices)) # ######################################################################### # Set up working capacity slider and callback def wc_callback(self, attr, old, new): """Callback on pressure range for working capacity.""" self.p1, self.p2 = str(int(2 * new[0])), str(int(2 * new[1])) # regenerate graph data self.data.patch(self.patch_data_w(self.p1, self.p2)) if self.data.selected.indices: self.errors.patch(self.patch_error_wc(self.data.selected.indices)) # ######################################################################### # Data generator def gen_data(self, lp, p1, p2): """Select or generate all KPI data for a pair of ads_list.""" if self._dfs is None: return { 'labels': [], 'sel': [], 'psa_W': [], 'K_x': [], 'K_y': [], 'K_nx': [], 'K_ny': [], 'K_n': [], 'L_x': [], 'L_y': [], 'L_nx': [], 'L_ny': [], 'L_n': [], 'W_x': [], 'W_y': [], 'W_nx': [], 'W_ny': [], 'W_n': [], } # Henry coefficient K_x = self._dfs[('kH_x', 'med')] K_y = self._dfs[('kH_y', 'med')] K_nx = self._dfs[('kH_x', 'size')] K_ny = self._dfs[('kH_y', 'size')] K_n = K_nx + K_ny # Loading L_x, L_y, L_nx, L_ny, L_n = 0, 0, 0, 0, 0 if self.lp != '0': L_x = self._dfs[(f'{lp}_x', 'med')] L_y = self._dfs[(f'{lp}_y', 'med')] L_nx = self._dfs[(f'{lp}_x', 'size')] L_ny = self._dfs[(f'{lp}_y', 'size')] L_n = L_nx + L_ny # Working capacity if self.p1 == '0': W_xp1 = W_yp1 = 0 else: W_xp1 = self._dfs[(f'{p1}_x', 'med')] W_yp1 = self._dfs[(f'{p1}_y', 'med')] if self.p2 == '0': W_xp2 = W_yp2 = 0 else: W_xp2 = self._dfs[(f'{p2}_x', 'med')] W_yp2 = self._dfs[(f'{p2}_y', 'med')] W_x = W_xp2 - W_xp1 W_y = W_yp2 - W_yp1 W_nx = np.maximum(self._dfs[(f'{p1}_x', 'size')] if p1 != '0' else 0, self._dfs[(f'{p2}_x', 'size')] if p2 != '0' else 0) W_ny = np.maximum(self._dfs[(f'{p1}_y', 'size')] if p1 != '0' else 0, self._dfs[(f'{p2}_y', 'size')] if p2 != '0' else 0) W_n = W_nx + W_ny sel = np.exp(K_y - K_x) psa_W = (W_y / W_x) * sel return { 'labels': self._dfs.index, # parameters 'sel': sel, 'psa_W': psa_W, # Henry data 'K_x': K_x, 'K_y': K_y, 'K_nx': K_nx, 'K_ny': K_ny, 'K_n': K_n, # Loading data 'L_x': L_x, 'L_y': L_y, 'L_nx': L_nx, 'L_ny': L_ny, 'L_n': L_n, # Working capacity data 'W_x': W_x, 'W_y': W_y, 'W_nx': W_nx, 'W_ny': W_ny, 'W_n': W_n, } def patch_data_l(self, p): """Patch KPI data when uptake changes.""" if self._dfs is None: return {} if p == '0': L_x = L_y = L_nx = L_ny = L_n = [0 for a in self._dfs.index] else: L_x = self._dfs.loc[:, (f'{p}_x', 'med')] L_y = self._dfs.loc[:, (f'{p}_y', 'med')] L_nx = self._dfs.loc[:, (f'{p}_x', 'size')] L_ny = self._dfs.loc[:, (f'{p}_y', 'size')] L_n = L_nx + L_ny return { # Loading data 'L_x': [(slice(None), L_x)], 'L_y': [(slice(None), L_y)], 'L_nx': [(slice(None), L_nx)], 'L_ny': [(slice(None), L_ny)], 'L_n': [(slice(None), L_n)] } def patch_data_w(self, p1, p2): """Patch KPI data when working capacity changes.""" if self._dfs is None: return {} if self.p1 == '0': W_xp1 = W_yp1 = 0 else: W_xp1 = self._dfs[(f'{p1}_x', 'med')] W_yp1 = self._dfs[(f'{p1}_y', 'med')] if self.p2 == '0': W_xp2 = W_yp2 = 0 else: W_xp2 = self._dfs[(f'{self.p2}_x', 'med')] W_yp2 = self._dfs[(f'{self.p2}_y', 'med')] W_x = W_xp2 - W_xp1 W_y = W_yp2 - W_yp1 W_nx = np.maximum(self._dfs[(f'{p1}_x', 'size')] if p1 != '0' else 0, self._dfs[(f'{p2}_x', 'size')] if p2 != '0' else 0) W_ny = np.maximum(self._dfs[(f'{p1}_y', 'size')] if p1 != '0' else 0, self._dfs[(f'{p2}_y', 'size')] if p2 != '0' else 0) W_n = W_nx + W_ny psa_W = (W_y / W_x) * self.data.data['sel'] return { # parameters 'psa_W': [(slice(None), psa_W)], # Working capacity data 'W_x': [(slice(None), W_x)], 'W_y': [(slice(None), W_y)], 'W_nx': [(slice(None), W_nx)], 'W_ny': [(slice(None), W_ny)], 'W_n': [(slice(None), W_n)] } # ######################################################################### # Error generator def gen_error(self, indices=None): """Select or generate all error data for selected points.""" if indices is None: return { 'labels': [], 'K_x': [], 'K_y': [], 'L_x': [], 'L_y': [], 'W_x': [], 'W_y': [], 'K_x0': [], 'K_y0': [], 'K_x1': [], 'K_y1': [], 'L_x0': [], 'L_y0': [], 'L_x1': [], 'L_y1': [], 'W_x0': [], 'W_y0': [], 'W_x1': [], 'W_y1': [], } else: mats = [] K_X, K_Y, L_X, L_Y, W_X, W_Y = [], [], [], [], [], [] K_X1, K_Y1, K_X2, K_Y2 = [], [], [], [] L_X1, L_Y1, L_X2, L_Y2 = [], [], [], [] W_X1, W_Y1, W_X2, W_Y2 = [], [], [], [] for index in indices: mat = self.data.data['labels'][index] K_x = self.data.data['K_x'][index] K_y = self.data.data['K_y'][index] L_x = self.data.data['L_x'][index] L_y = self.data.data['L_y'][index] W_x = self.data.data['W_x'][index] W_y = self.data.data['W_y'][index] # NaN values have to be avoided if np.isnan(K_x) or np.isnan(K_y): K_x, K_y = 0, 0 K_ex, K_ey = 0, 0 else: K_ex = self._dfs.loc[mat, ('kH_x', 'err')] K_ey = self._dfs.loc[mat, ('kH_y', 'err')] if np.isnan(L_x) or np.isnan(L_y) or self.lp == '0': L_x, L_y = 0, 0 L_ex, L_ey = 0, 0 else: L_ex = self._dfs.loc[mat, (f'{self.lp}_x', 'err')] L_ey = self._dfs.loc[mat, (f'{self.lp}_y', 'err')] if np.isnan(W_x) or np.isnan(W_y): W_x, W_y = 0, 0 W_ex, W_ey = 0, 0 else: W_ex = self._dfs.loc[mat, (f'{self.p1}_x', 'err')] if self.p1 != 0 else 0 + \ self._dfs.loc[mat, ('{}_x'.format( self.p2), 'err')] if self.p2 != 0 else 0 W_ey = self._dfs.loc[mat, (f'{self.p1}_y', 'err')] if self.p1 != 0 else 0 + \ self._dfs.loc[mat, ('{}_y'.format( self.p2), 'err')] if self.p2 != 0 else 0 mats.extend([mat, mat]) K_X.extend([K_x, K_x]) K_Y.extend([K_y, K_y]) L_X.extend([L_x, L_x]) L_Y.extend([L_y, L_y]) W_X.extend([W_x, W_x]) W_Y.extend([W_y, W_y]) # henry data K_X1.extend([K_x - K_ex, K_x]) K_Y1.extend([K_y, K_y - K_ey]) K_X2.extend([K_x + K_ex, K_x]) K_Y2.extend([K_y, K_y + K_ey]) # loading data L_X1.extend([L_x - L_ex, L_x]) L_Y1.extend([L_y, L_y - L_ey]) L_X2.extend([L_x + L_ex, L_x]) L_Y2.extend([L_y, L_y + L_ey]) # working capacity data W_X1.extend([W_x - W_ex, W_x]) W_Y1.extend([W_y, W_y - W_ey]) W_X2.extend([W_x + W_ex, W_x]) W_Y2.extend([W_y, W_y + W_ey]) return { # labels 'labels': mats, 'K_x': K_X, 'K_y': K_Y, 'L_x': L_X, 'L_y': L_Y, 'W_x': W_X, 'W_y': W_Y, # henry data 'K_x0': K_X1, 'K_y0': K_Y1, 'K_x1': K_X2, 'K_y1': K_Y2, # loading data 'L_x0': L_X1, 'L_y0': L_Y1, 'L_x1': L_X2, 'L_y1': L_Y2, # working capacity data 'W_x0': W_X1, 'W_y0': W_Y1, 'W_x1': W_X2, 'W_y1': W_Y2, } def patch_error_l(self, indices=None): """Patch error data when uptake changes.""" if indices is None: return { # loading data 'L_x': [(slice(None), [])], 'L_y': [(slice(None), [])], 'L_x0': [(slice(None), [])], 'L_y0': [(slice(None), [])], 'L_x1': [(slice(None), [])], 'L_y1': [(slice(None), [])], } else: L_X, L_Y = [], [] L_X1, L_Y1, L_X2, L_Y2 = [], [], [], [] for index in indices: L_x = self.data.data['L_x'][index] L_y = self.data.data['L_y'][index] if np.isnan(L_x) or np.isnan(L_y) or self.lp == '0': L_x, L_y = 0, 0 L_ex, L_ey = 0, 0 else: mat = self.data.data['labels'][index] L_ex = self._dfs.loc[mat, (f'{self.lp}_x', 'err')] L_ey = self._dfs.loc[mat, (f'{self.lp}_y', 'err')] L_X.extend([L_x, L_x]) L_Y.extend([L_y, L_y]) L_X1.extend([L_x - L_ex, L_x]) L_Y1.extend([L_y, L_y - L_ey]) L_X2.extend([L_x + L_ex, L_x]) L_Y2.extend([L_y, L_y + L_ey]) return { # loading data 'L_x': [(slice(None), L_X)], 'L_y': [(slice(None), L_Y)], 'L_x0': [(slice(None), L_X1)], 'L_y0': [(slice(None), L_Y1)], 'L_x1': [(slice(None), L_X2)], 'L_y1': [(slice(None), L_Y2)], } def patch_error_wc(self, indices=None): """Patch error data when working capacity changes.""" if indices is None: return { # loading data 'W_x': [(slice(None), [])], 'W_y': [(slice(None), [])], 'W_x0': [(slice(None), [])], 'W_y0': [(slice(None), [])], 'W_x1': [(slice(None), [])], 'W_y1': [(slice(None), [])], } else: W_X, W_Y = [], [] W_X1, W_Y1, W_X2, W_Y2 = [], [], [], [] for index in indices: W_x = self.data.data['W_x'][index] W_y = self.data.data['W_y'][index] if np.isnan(W_x) or np.isnan(W_y): W_x, W_y = 0, 0 W_ex, W_ey = 0, 0 else: mat = self.data.data['labels'][index] W_ex = self._dfs.loc[mat, (f'{self.p1}_x', 'err')] if self.p1 != 0 else 0 + \ self._dfs.loc[mat, ('{}_x'.format( self.p2), 'err')] if self.p2 != 0 else 0 W_ey = self._dfs.loc[mat, (f'{self.p1}_y', 'err')] if self.p1 != 0 else 0 + \ self._dfs.loc[mat, ('{}_y'.format( self.p2), 'err')] if self.p2 != 0 else 0 W_X.extend([W_x, W_x]) W_Y.extend([W_y, W_y]) W_X1.extend([W_x - W_ex, W_x]) W_Y1.extend([W_y, W_y - W_ey]) W_X2.extend([W_x + W_ex, W_x]) W_Y2.extend([W_y, W_y + W_ey]) return { # loading data 'W_x': [(slice(None), W_X)], 'W_y': [(slice(None), W_Y)], 'W_x0': [(slice(None), W_X1)], 'W_y0': [(slice(None), W_Y1)], 'W_x1': [(slice(None), W_X2)], 'W_y1': [(slice(None), W_Y2)], } # ######################################################################### # Iso generator def gen_iso_dict(self): """Empty dictionary for isotherm display.""" return { 'labels': [], 'doi': [], 'x': [], 'y': [], 'temp': [], 'color': [], } # ######################################################################### # Callback for selection def selection_callback(self, attr, old, new): """Display selected points on graph and the isotherms.""" # If the user has not selected anything if len(new) == 0: # Remove error points: self.errors.data = self.gen_error() # Reset bottom graphs self.g1_iso_sel.data = self.gen_iso_dict() self.g2_iso_sel.data = self.gen_iso_dict() self.g1_iso_sel.selected.update(indices=[]) self.g2_iso_sel.selected.update(indices=[]) self.sep_dash.p_g1iso.x_range.end = 0.01 self.sep_dash.p_g1iso.y_range.end = 0.01 self.sep_dash.p_g2iso.x_range.end = 0.01 self.sep_dash.p_g2iso.y_range.end = 0.01 # done here return # If the user has selected more than one point # Display error points: self.errors.data = self.gen_error(new) # Reset bottom graphs self.g1_iso_sel.data = self.gen_iso_dict() self.g2_iso_sel.data = self.gen_iso_dict() self.g1_iso_sel.selected.update(indices=[]) self.g2_iso_sel.selected.update(indices=[]) self.sep_dash.p_g1iso.x_range.end = 0.01 self.sep_dash.p_g1iso.y_range.end = 0.01 self.sep_dash.p_g2iso.x_range.end = 0.01 self.sep_dash.p_g2iso.y_range.end = 0.01 # If we have only one point then we display isotherms if len(new) == 1: # Generate bottom graphs self.sel_mat = self.data.data['labels'][new[0]] self.g1_hashes = get_isohash(self._df, self.iso_type, self.t_abs, self.t_tol, self.g1, self.sel_mat) self.g2_hashes = get_isohash(self._df, self.iso_type, self.t_abs, self.t_tol, self.g2, self.sel_mat) Thread(target=self.populate_isos, args=['g1']).start() Thread(target=self.populate_isos, args=['g2']).start() # ######################################################################### # Isotherm interactions def populate_isos(self, ads): """Threaded code to add isotherms to bottom graphs.""" if ads == 'g1': # "average" isotherm loading = self._dfs.loc[self.sel_mat, (slice(None), 'med')].values[1:41] self.doc.add_next_tick_callback( partial(self.iso_update_g1, iso={ 'labels': ['median'], 'x': [self.p_range[~np.isnan(loading)]], 'y': [loading[~np.isnan(loading)]], 'temp': [self.t_abs], 'doi': [''] }, color='k')) # rest of the isotherms for iso in get_isohash(self._df, self.iso_type, self.t_abs, self.t_tol, self.g1, self.sel_mat): parsed = load_isotherm(iso) if parsed: self.doc.add_next_tick_callback( partial(self.iso_update_g1, iso=parsed)) elif ads == 'g2': # "average" isotherm loading = self._dfs.loc[self.sel_mat, (slice(None), 'med')].values[42:] self.doc.add_next_tick_callback( partial(self.iso_update_g2, iso={ 'labels': ['median'], 'x': [self.p_range[~np.isnan(loading)]], 'y': [loading[~np.isnan(loading)]], 'temp': [self.t_abs], 'doi': [''] }, color='k', resize=False)) # rest of the isotherms for iso in get_isohash(self._df, self.iso_type, self.t_abs, self.t_tol, self.g2, self.sel_mat): parsed = load_isotherm(iso) if parsed: self.doc.add_next_tick_callback( partial(self.iso_update_g2, iso=parsed)) @gen.coroutine def iso_update_g1(self, iso, color=None): iso['color'] = [next(self.sep_dash.c_cyc) if color is None else color] self.g1_iso_sel.stream(iso) if float(iso['x'][0][-1]) > self.sep_dash.p_g1iso.x_range.end: self.sep_dash.p_g1iso.x_range.end = 1.1 * float(iso['x'][0][-1]) if float(iso['y'][0][-1]) > self.sep_dash.p_g1iso.y_range.end: self.sep_dash.p_g1iso.y_range.end = 1.1 * float(iso['y'][0][-1]) @gen.coroutine def iso_update_g2(self, iso, color=None, resize=True): iso['color'] = [next(self.sep_dash.c_cyc) if color is None else color] self.g2_iso_sel.stream(iso) if float(iso['x'][0][-1]) > self.sep_dash.p_g2iso.x_range.end: self.sep_dash.p_g2iso.x_range.end = 1.1 * float(iso['x'][0][-1]) if float(iso['y'][0][-1]) > self.sep_dash.p_g2iso.y_range.end: self.sep_dash.p_g2iso.y_range.end = 1.1 * float(iso['y'][0][-1])
#x_values = [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, # 2, 3,4,5 6, 7, 8, 9, 10, 13, 14, 15, 16, 17] output_file = 'spain_vs_mexico_line.html' #_______________________________________________________________________________________________ x_values = list(range(34)) #________________________________________________________________________________________________ source = ColumnDataSource(data=dict( x_values=list(range(34)), spain_y_values=[ 323, 181, 219, 355, 585, 307, 363, 334, 232, 248, 334, 400, 419, 564, 301, 200, 301, 388, 444, 442, 709, 976, 1244, 341, 383, 543, 852, 1249, 1646, 2045, 666, 875, 1361, 1400 ], mexico_y_values=[ 4147, 3427, 4599, 4930, 5662, 5030, 4717, 5343, 4577, 6288, 5437, 6104, 5441, 4410, 4050, 3805, 5432, 5681, 6741, 6740, 6914, 4683, 4902, 6258, 6995, 7280, 6891, 6094, 4482, 4685, 7051, 6149, 6406, 7257 ])) p = figure() #p.vline_stack(['spain_y_values','mexico_y_values'], x = 'x_values', source = source, line_width = 2, color = 'firebrick') #p.varea_stack(['spain_y_values','mexico_y_values'], x = 'x_values',color = ["aqua" , "blueviolet"],source = source) p.hbar_stack(['spain_y_values', 'mexico_y_values'], y='x_values', height=0.8, color=["aqua", "blueviolet"], source=source) show(p)
# define some points and a little graph between them x = [2, 3, 5, 6, 8, 7] y = [6, 4, 3, 8, 7, 5] links = { 0: [1, 2], 1: [0, 3, 4], 2: [0, 5], 3: [1, 4], 4: [1, 3], 5: [2, 3, 4] } p = figure(width=400, height=400, tools="", toolbar_location=None, title='Hover over points') source = ColumnDataSource({'x0': [], 'y0': [], 'x1': [], 'y1': []}) sr = p.segment(x0='x0', y0='y0', x1='x1', y1='y1', color='olive', alpha=0.6, line_width=3, source=source, ) cr = p.circle(x, y, color='olive', size=30, alpha=0.4, hover_color='olive', hover_alpha=1.0) # Add a hover tool, that sets the link data for a hovered circle code = """ var links = %s; var data = {'x0': [], 'y0': [], 'x1': [], 'y1': []}; var cdata = circle.get('data'); var indices = cb_data.index['1d'].indices; for (i=0; i < indices.length; i++) { ind0 = indices[i] for (j=0; j < links[ind0].length; j++) { ind1 = links[ind0][j]; data['x0'].push(cdata.x[ind0]); data['y0'].push(cdata.y[ind0]);
w_temperature_data = w_temperature_data[w_temperature_data.Country.isin(countries)] w_temperature_data = w_temperature_data[w_temperature_data.month == 1] # I select just January #w_temperature_data = w_temperature_data[w_temperature_data.year.isin(['1850', '1875', '1900', '1925', '1950', '1975', '2000'])] # STEP 2: map the colors to our data pal = sns.color_palette("hls", 3) # 3 is the number of colors to extract from the palette, that's because we have 3 countries colors = pal.as_hex() # get the values of those colors. We could also have written the name/numbers of some colors #print(colors) # if you uncomment this line you can observe that this is just a string of color values colormap = CategoricalColorMapper(palette=colors, factors=w_temperature_data['Country'].unique()) # note that we are working with categorical data here, so we need one color per category #factors=list(set(w_temperature_data['Country']))) # set() is similar to unique(), but makes the data of type 'set' # STEP 3: create the data source for the interactive graph source = ColumnDataSource(data=dict( x = w_temperature_data.year, y = w_temperature_data.AverageTemperature, country = w_temperature_data.Country, )) # STEP 4: create the base figure p = figure(title = "Country Land Temperture", plot_width=900, plot_height=400, tools="hover, save, pan, box_zoom, reset, wheel_zoom", # here we add the interactive tools that we want our plot to have (these are the simple ones) tooltips = [('Year', '@x'), ('Temperature', '@y'), ('Country', '@country')]) # here we can assign which values to show on the hover tool p.xaxis.axis_label = 'Time' p.yaxis.axis_label = 'Temperature' # STEP 5: fill it with circles p.circle(x = 'x', y = 'y', source = source, # here we assign the data color={'field': 'country', 'transform': colormap}, # assign the colors: this is a dictionary with the keys field and transform, transform has to be a mapper object
def make_plot(): # Load the dictionary to see the patterns btc_dict = load_dictionary('./bitcoin2016.dict') # Remove the singletons btc_dict = {k: v for k, v in btc_dict.items() if v[1] > 1} # Turn it into a dataframe df = pd.DataFrame.from_dict(btc_dict, orient='Index', columns=['Support', 'Length', 'Time']) # Sort them by support df = df.sort_values(by='Support')[::-1] st, d = import_dat('./bitcoin2016.dat') ct = st.copy() for key in list(df.index[:10]): ct = add(key, ct, d) #patterns[key]=value patterns = {} for key, value in ct.items(): if value[1] > 1: patterns[key] = value ordered_p = cov_order(patterns) val_d = {} sign = -100 for x in ordered_p: # 'Paint' the dataset with 0's where covered and return p amount d = painter(x, d, sign) val_d[sign] = x sign *= 2 d2 = [[val_d[x] if x in val_d else 'None' for x in row] for row in d] df2 = pd.read_excel('./bitcoin2016.xlsx') df2['Date'] = pd.to_datetime(df2['<DATE>']) df2['ToolTipDates'] = df2.Date.map(lambda x: x.strftime("%d %b %y")) colors = [ '#e6194B', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#42d4f4', '#f032e6', '#e6beff', '#9A6324', '#800000', '#000075' ] P_TO_COLOR = {x: colors[i] for i, x in enumerate(patterns)} P_TO_COLOR['None'] = '#f1f1f1' for i, x in enumerate(('<OPEN>', '<HIGH>', '<LOW>', '<CLOSE>', '<VOL>')): df2[f'pattern{x}'] = [str(x) for x in d2[i]] df2[f'color{x}'] = [P_TO_COLOR[x] for x in d2[i]] #for every column we generate the line we need open_ = xyc(df2, 'Date', 'og<OPEN>', 'color<OPEN>') high_ = xyc(df2, 'Date', 'og<HIGH>', 'color<HIGH>') low_ = xyc(df2, 'Date', 'og<LOW>', 'color<LOW>') close_ = xyc(df2, 'Date', 'og<CLOSE>', 'color<CLOSE>') vol_ = xyc(df2, 'Date', 'og<VOL>', 'color<VOL>') df2['labOPEN'] = df2['pattern<OPEN>'] df2['labHIGH'] = df2['pattern<HIGH>'] df2['labLOW'] = df2['pattern<LOW>'] df2['labCLOSE'] = df2['pattern<CLOSE>'] df2['labVOL'] = df2['pattern<VOL>'] source2 = ColumnDataSource(df2) output_file('bitcoindaily.html') p = figure(x_axis_type='datetime', plot_width=1440, plot_height=600, title="Bitcoin Stock Price") p.circle(x='Date', y='og<OPEN>', name='open', alpha=0, source=source2, size=3) p.circle(x='Date', y='og<CLOSE>', name='close', alpha=0, source=source2, size=3) p.circle(x='Date', y='og<HIGH>', name='high', alpha=0, source=source2, size=3) p.circle(x='Date', y='og<LOW>', name='low', alpha=0, source=source2, size=3) p.multi_line(name='q', xs=open_[0], ys=open_[1], color=open_[2], line_width=3) p.multi_line(name='e', xs=high_[0], ys=high_[1], color=high_[2], line_width=3) p.multi_line(name='ee', xs=low_[0], ys=low_[1], color=low_[2], line_width=3) p.multi_line(name='w', xs=close_[0], ys=close_[1], color=close_[2], line_width=3) q = figure(x_range=p.x_range, x_axis_type='datetime', plot_width=1440, plot_height=200, title="Stock Volume", y_axis_type='linear') q.circle(x='Date', y='og<VOL>', name='VOL', alpha=0, source=source2, size=3) p.circle(x='Date', y='og<LOW>', name='low', alpha=0, source=source2, size=3) q.multi_line(name='qw', xs=vol_[0], ys=vol_[1], color=vol_[2], line_width=3) p.add_tools( HoverTool(names=['low'], mode="vline", line_policy='nearest', point_policy='snap_to_data', tooltips=[ ('Date : ', '@ToolTipDates'), ('Low Price : ', '@{og<LOW>}{0.2f}'), ('Low Pattern : ', '@labLOW'), ('High Price : ', '@{og<HIGH>}{0.2f}'), ('High Pattern : ', '@labHIGH'), ('Open Price : ', '@{og<OPEN>}{0.2f}'), ('Open Pattern : ', '@labOPEN'), ('Close Price : ', '@{og<CLOSE>}{0.2f}'), ('Close Pattern : ', '@labCLOSE'), ])) p.add_tools( HoverTool(names=['open'], mode="vline", line_policy='nearest', point_policy='snap_to_data', tooltips=[ ('Name', 'Open'), ])) p.add_tools( HoverTool(names=['close'], mode="vline", line_policy='nearest', point_policy='snap_to_data', tooltips=[ ('Name', 'Close'), ])) p.add_tools( HoverTool(names=['high'], mode="vline", line_policy='nearest', point_policy='snap_to_data', tooltips=[ ('Name', 'High'), ])) p.add_tools( HoverTool(names=['low'], mode="vline", line_policy='nearest', point_policy='snap_to_data', tooltips=[ ('Name', 'Low'), ])) q.add_tools( HoverTool(names=['VOL'], mode="vline", line_policy='nearest', point_policy='snap_to_data', tooltips=[ ('Date : ', '@ToolTipDates'), ('High Price : ', '@{og<VOL>}{0.2f}'), ('High Pattern : ', '@labVOL'), ])) show(column(p, q))
import numpy as np from numpy import pi from bokeh.client import push_session from bokeh.driving import cosine from bokeh.plotting import figure, curdoc from bokeh.models import ColumnDataSource from bokeh.models.widgets import TableColumn, DataTable from bokeh.io import show, vform x = np.linspace(0, 4 * pi, 80) y = np.sin(x) source = ColumnDataSource(data=dict(x=x, y=y)) p = figure() r1 = p.line([0, 4 * pi], [-1, 1], color="firebrick") r2 = p.line(x='x', y='y', color="navy", line_width=4, source=source) columns = [ TableColumn(field="x", title="X"), TableColumn(field="y", title="Y") ] data_table = DataTable(source=source, columns=columns, width=400, height=280) session = push_session(curdoc()) @cosine(w=0.03) def update(step):
def corr_plot(corr_array, title, plot_name): from bokeh.plotting import figure, show, output_file from bokeh.models import HoverTool, ColumnDataSource from bokeh.palettes import Spectral10 from bokeh.embed import components import read_keywords nodes = np.arange(0, np.shape(corr_array)[0], 1) names = list(read_keywords.get_keys()) colormap = Spectral10 xname = [] yname = [] color = [] corr_value = [] value_bins = np.linspace(-1, 1, 10) def get_color(value): ind = np.digitize([value], value_bins) return ind[0]-1 for i in range(len(nodes)): for j in range(len(nodes)): xname.append(names[i]) yname.append(names[j]) corr_value.append(corr_array[i, j]) color.append(colormap[get_color(corr_array[i, j])]) source = ColumnDataSource(data=dict( xname=xname, yname=yname, colors=color, corr=corr_value, )) p = figure(title=title, x_axis_location="above", tools="resize,hover,pan,box_zoom,wheel_zoom,reset", x_range=list(reversed(names)), y_range=names, responsive=True) p.plot_width = 800 p.plot_height = 800 p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "5pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = np.pi/3 p.rect('xname', 'yname', 0.9, 0.9, source=source, color='colors', line_color=None) p.select_one(HoverTool).tooltips = [ ('keywords', '@yname, @xname'), ('value', '@corr'), ] # save output file output_file(plot_name + '.html', title=title) # for website script, div = components(p) # save js file for website js_file = open(plot_name + '.js', 'w') js_file.write(script) js_file.close() print div show(p)
def scatter_matrix(dataset, target_dataset=None): # pylint:disable=too-many-locals # https://stackoverflow.com/a/49634658 dataset_source = ColumnDataSource(data=dataset) xdr = DataRange1d(bounds=None) ydr = DataRange1d(bounds=None) columns = dataset.columns if target_dataset is not None: assert len(target_dataset.columns) == len(columns) target_source = ColumnDataSource(data=target_dataset) y_max = len(columns) scatter_plots = [] for yidx, y_col in enumerate(columns): scatter_plots += [[]] scatter_plots[yidx] += [None] * yidx for xidx in range(yidx, y_max): x_col = columns[xidx] pic = figure( x_range=xdr, y_range=ydr, plot_width=200, plot_height=200, min_border_left=2, min_border_right=2, min_border_top=2, min_border_bottom=2, tools="", ) rend = pic.circle( source=dataset_source, x=x_col, y=y_col, fill_alpha=0.3, line_alpha=0.3, size=3, ) # pylint:disable=no-member xdr.renderers.append(rend) ydr.renderers.append(rend) # pylint:enable=no-member if target_dataset is not None: rend = pic.circle( source=target_source, x=target_dataset.columns[xidx], y=target_dataset.columns[yidx], fill_alpha=0.3, line_alpha=0.3, size=10, color="red", ) # pylint:disable=no-member xdr.renderers.append(rend) ydr.renderers.append(rend) # pylint:enable=no-member pic.yaxis.axis_label = "" pic.yaxis.visible = False pic.xaxis.axis_label = "" pic.xaxis.visible = False scatter_plots[yidx] += [pic] return gridplot(scatter_plots, sizing_mode="scale_both")
def plot_ess( ax, plotters, xdata, ess_tail_dataset, mean_ess, sd_ess, idata, data, text_x, text_va, kind, extra_methods, rows, cols, figsize, kwargs, extra_kwargs, text_kwargs, _linewidth, _markersize, n_samples, relative, min_ess, xt_labelsize, titlesize, ax_labelsize, ylabel, rug, rug_kind, rug_kwargs, hline_kwargs, backend_kwargs, show, ): """Bokeh essplot.""" if backend_kwargs is None: backend_kwargs = {} backend_kwargs = { **backend_kwarg_defaults(), **backend_kwargs, } if ax is None: _, ax = _create_axes_grid( len(plotters), rows, cols, figsize=figsize, squeeze=False, constrained_layout=True, backend="bokeh", backend_kwargs=backend_kwargs, ) else: ax = np.atleast_2d(ax) for (var_name, selection, x), ax_ in zip(plotters, (item for item in ax.flatten() if item is not None)): bulk_points = ax_.circle(np.asarray(xdata), np.asarray(x), size=6) if kind == "evolution": bulk_line = ax_.line(np.asarray(xdata), np.asarray(x)) ess_tail = ess_tail_dataset[var_name].sel(**selection) tail_points = ax_.line(np.asarray(xdata), np.asarray(ess_tail), color="orange") tail_line = ax_.circle(np.asarray(xdata), np.asarray(ess_tail), size=6, color="orange") elif rug: if rug_kwargs is None: rug_kwargs = {} if not hasattr(idata, "sample_stats"): raise ValueError( "InferenceData object must contain sample_stats for rug plot" ) if not hasattr(idata.sample_stats, rug_kind): raise ValueError( "InferenceData does not contain {} data".format(rug_kind)) rug_kwargs.setdefault("space", 0.1) _rug_kwargs = {} _rug_kwargs.setdefault("size", 8) _rug_kwargs.setdefault("line_color", rug_kwargs.get("line_color", "black")) _rug_kwargs.setdefault("line_width", 1) _rug_kwargs.setdefault("line_alpha", 0.35) _rug_kwargs.setdefault("angle", np.pi / 2) values = data[var_name].sel(**selection).values.flatten() mask = idata.sample_stats[rug_kind].values.flatten() values = rankdata(values)[mask] rug_space = np.max(x) * rug_kwargs.pop("space") rug_x, rug_y = values / (len(mask) - 1), np.zeros_like(values) - rug_space glyph = Dash(x="rug_x", y="rug_y", **_rug_kwargs) cds_rug = ColumnDataSource({ "rug_x": np.asarray(rug_x), "rug_y": np.asarray(rug_y) }) ax_.add_glyph(cds_rug, glyph) hline = Span( location=0, dimension="width", line_color="black", line_width=_linewidth, line_alpha=0.7, ) ax_.renderers.append(hline) if extra_methods: mean_ess_i = mean_ess[var_name].sel(**selection).values.item() sd_ess_i = sd_ess[var_name].sel(**selection).values.item() hline = Span( location=mean_ess_i, dimension="width", line_color="black", line_width=2, line_dash="dashed", line_alpha=1.0, ) ax_.renderers.append(hline) hline = Span( location=sd_ess_i, dimension="width", line_color="black", line_width=1, line_dash="dashed", line_alpha=1.0, ) ax_.renderers.append(hline) hline = Span( location=400 / n_samples if relative else min_ess, dimension="width", line_color="red", line_width=3, line_dash="dashed", line_alpha=1.0, ) ax_.renderers.append(hline) if kind == "evolution": legend = Legend( items=[("bulk", [bulk_points, bulk_line]), ("tail", [tail_line, tail_points])], location="center_right", orientation="horizontal", ) ax_.add_layout(legend, "above") ax_.legend.click_policy = "hide" title = Title() title.text = make_label(var_name, selection) ax_.title = title ax_.xaxis.axis_label = "Total number of draws" if kind == "evolution" else "Quantile" ax_.yaxis.axis_label = ylabel.format( "Relative ESS" if relative else "ESS") if backend_show(show): grid = gridplot(ax.tolist(), toolbar_location="above") bkp.show(grid) return ax
req = requests.get(url) if g.BOKEH_DEV: print "req:", req, "\n" MEASURES = req.json() MEASURES['m_data'] = [{k:[float(v) for v in _list] for k,_list in _dict.items()} for _dict in MEASURES['m_data']] except: exit() ############################################################################## # Sources source = ColumnDataSource(data=dict( f = MEASURES['m_data'][0]['force'], t = MEASURES['m_data'][0]['time'] )) source_table = ColumnDataSource(data=dict( m_datetime = MEASURES['m_date'], d_code = MEASURES['d_code'], c_ang = [_d['angular'] for _d in MEASURES['c_data']], c_lin = [_d['linear'] for _d in MEASURES['c_data']], index = range(len(MEASURES['id'])) )) source_table.selected['1d']['indices'] = [0] ############################################################################## # Plot
def slider_app_bokeh(start_date=datetime(2020, 3, 1), lock_date=datetime(2020, 3, 26), post_lock_date=datetime(2020, 4, 26), init_rho=3.7, lock_rho=0.7, post_lock_rho=1.6, init_inf=61, conf_case_delay=7, init_exp=0, init_rec=0, mort_rate=0.013, symp_2_death=14, MaxDays=500, tau=8, k=0.5, nu=1.7, mu=5.1, model='NBD_SEIR', county='El Paso'): models = {'SIR_nu': 0, 'SEIR_nu': 1, 'NBD_SEIR': 2} model_text = { 'SIR_nu': 'Simple S-I-R Infection Model with \n' + 'Heterogeneous Mixing Simulated using \n' + 'a Power-Law Scaling Term (' 'nu' ')', 'SEIR_nu': 'S-E-I-R Infection Model with \n' + 'Heterogeneous Mixing Simulated using \n' + 'a Power-Law Scaling Term (' 'nu' ')', 'NBD_SEIR': 'S-E-I-R Infection Model with \n' + 'Heterogeneous Mixing Simulated using \n' + 'Negative Binomially-Distributed \n' + 'Infection Events (PDF shape ' 'k' ')' } t = 7 / 24 today = datetime.now() pct_tst = 0.15 global zm_toggle, legend_toggle zm_toggle = -1 legend_toggle = 1 rho_sched = {lock_date: lock_rho, post_lock_date: post_lock_rho} lock_time = (lock_date-start_date).days + \ (lock_date-start_date).seconds/86400 post_lock_time = (post_lock_date-start_date).days + \ (post_lock_date-start_date).seconds/86400 if model == 'SIR_nu': I_ind = 2 R_ind = 3 D_ind = 4 out = SIR_nu(start_date, P=720403, I=init_inf, R=0, rho=init_rho, tau=tau, nu=1.7, MaxDays=MaxDays, suppress_output=1, rho_sched=rho_sched, mort_rate=mort_rate, symp_2_death=symp_2_death, t=t) elif model == 'SEIR_nu': I_ind = 3 R_ind = 4 D_ind = 5 out = SEIR_nu(start_date, P=720403, E=0, I=init_inf, R=0, mu=mu, rho=init_rho, tau=tau, nu=1.7, MaxDays=MaxDays, suppress_output=1, rho_sched=rho_sched, mort_rate=mort_rate, symp_2_death=symp_2_death, t=t) elif model == 'NBD_SEIR': I_ind = 3 R_ind = 4 D_ind = 6 out = NBD_SEIR(start_date, 720403, 0, init_inf, 0, init_rho, tau, k, mu, county, MaxDays, suppress_output=1, rho_sched=rho_sched, t=t, mort_rate=mort_rate, symp_2_death=symp_2_death) C = int(tau / t) max_ind = int((today - start_date).days / t) T = out[0] I = out[I_ind] R = out[R_ind] D = out[D_ind] T1 = T T2 = T[0:max_ind] T3 = T[0:-C] CC1 = array(R[C:max_ind + C]) CC1p = pct_tst * CC1 CC2 = array(R[C:]) CC2p = pct_tst * CC2 [death_times, case_times, deaths, cases] = dataHandler(start_date, conf_case_delay) # def days_2_ms(T): # Dt = [] # for i in T: # Dt.append(i*24*3600*1000) # return Dt # Dt1 = days_2_ms(T1) # Dt2 = days_2_ms(T2) # Dt3 = days_2_ms(T3) # DTd = days_2_ms(death_times) # DTc = days_2_ms(case_times) # Set up data: --- # source1 = ColumnDataSource(data=dict(T1=Dt1, I=I, D=D)) # source2 = ColumnDataSource(data=dict(T2=Dt2, CC1=CC1,CC1p=CC1p)) # source3 = ColumnDataSource(data=dict(T3=Dt3, CC2=CC2, CC2p=CC2p)) # scatter_source1 = ColumnDataSource(data=dict(Td=DTd, d=deaths)) # scatter_source2 = ColumnDataSource(data=dict(Tc=DTc, c=cases)) source1 = ColumnDataSource(data=dict(T1=T1, I=I, D=D)) source2 = ColumnDataSource(data=dict(T2=T2, CC1=CC1, CC1p=CC1p)) source3 = ColumnDataSource(data=dict(T3=T3, CC2=CC2, CC2p=CC2p)) scatter_source1 = ColumnDataSource(data=dict(Td=death_times, d=deaths)) scatter_source2 = ColumnDataSource(data=dict(Tc=case_times, c=cases)) # Set up plots: --- # Top-left plot plot1 = figure( plot_height=400, plot_width=400, title="Predicted vs. Reported Deaths in " + county + " County", tools="crosshair,pan,reset,save,wheel_zoom", # x_axis_type='datetime', x_range=[0, T[max_ind]], y_range=[0, max(deaths)]) plot1.line('T1'[0:max_ind], 'D'[0:max_ind], source=source1, line_color='red', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Cumulative Deaths') plot1.circle('Td', 'd', source=scatter_source1, line_color='red', muted_alpha=0.2, legend_label='Reported Deaths in ' + county + ' County') plot1.legend.location = 'top_left' plot1.legend.click_policy = 'mute' plot1.xaxis.ticker = [0, 31, 61] plot1.xaxis.major_label_overrides = {0: 'Mar', 31: 'Apr', 61: 'May'} # Top-right plot plot2 = figure( plot_height=400, plot_width=400, title="Predicted Deaths over the Long Term", tools="crosshair,pan,reset,save,wheel_zoom", #x_axis_type='datetime', x_range=[0, MaxDays], y_range=[0, max(D)]) plot2.line('T1', 'D', source=source1, line_color='red', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Cumulative Deaths') plot2.circle('Td', 'd', source=scatter_source1, line_color='red', muted_alpha=0.2, legend_label='Reported Deaths in' + county + ' County') plot2.legend.location = 'top_left' plot2.legend.click_policy = 'mute' plot2.xaxis.ticker = [0, 31, 61, 92, 122, 153, 183, 214, 245, 275] plot2.xaxis.major_label_overrides = { 0: 'Mar', 31: 'Apr', 61: 'May', 92: 'Jun', 122: 'Jul', 153: 'Aug', 183: 'Sep', 214: 'Oct', 245: 'Nov', 275: 'Dec' } # Bottom-left plot plot3 = figure( plot_height=400, plot_width=400, title="Predicted vs. Reported Infectious in El Paso County", tools="crosshair,pan,reset,save,wheel_zoom", #x_axis_type='datetime', x_range=[0, T[max_ind]], y_range=[0, R[max_ind]]) plot3.line('T1'[0:max_ind], 'I'[0:max_ind], source=source1, line_color='blue', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Infectious Population') plot3.line('T2', 'CC1', source=source2, line_color='green', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Cumulative Cases') plot3.line( 'T2', 'CC1p', source=source2, line_color='green', line_dash='dotted', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Confirmed Cases based on Tested Percentage') plot3.circle('Tc', 'c', source=scatter_source2, line_color='blue', muted_alpha=0.2, legend_label='Reported Confirmed Cases in ' + county + ' County') plot3.legend.location = 'top_left' plot3.legend.click_policy = 'mute' plot3.xaxis.ticker = [0, 31, 61] plot3.xaxis.major_label_overrides = {0: 'Mar', 31: 'Apr', 61: 'May'} # Bottom-right plot plot4 = figure( plot_height=400, plot_width=400, title="Predicted Infectious over the Long Term", tools="crosshair,pan,reset,save,wheel_zoom", #x_axis_type='datetime', x_range=[0, MaxDays], y_range=[0, max(R)]) plot4.line('T1', 'I', source=source1, line_color='blue', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Infectious Population') plot4.line('T3', 'CC2', source=source3, line_color='green', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Cumulative Cases') plot4.line( 'T3', 'CC2p', source=source3, line_color='green', line_dash='dotted', line_width=3, line_alpha=0.6, muted_alpha=0.2, legend_label='Predicted Confirmed Cases based on Tested Percentage') plot4.circle('Tc', 'c', source=scatter_source2, line_color='blue', muted_alpha=0.2, legend_label='Reported Confirmed Cases in ' + county + ' County') plot4.legend.location = 'top_left' plot4.legend.click_policy = 'mute' plot4.xaxis.ticker = [0, 31, 61, 92, 122, 153, 183, 214, 245, 275] plot4.xaxis.major_label_overrides = { 0: 'Mar', 31: 'Apr', 61: 'May', 92: 'Jun', 122: 'Jul', 153: 'Aug', 183: 'Sep', 214: 'Oct', 245: 'Nov', 275: 'Dec' } # Set up widgets delta = 0.1 sliders = [] text = TextInput(title="title", value='my sine wave') s_init_rho = Slider(title="Initial R_0", value=init_rho, start=0.1, end=6.0, step=delta / 10) sliders.append(s_init_rho) s_lock_rho = Slider(title="Stay-at-home R_0", value=lock_rho, start=0.1, end=6.0, step=delta / 10) sliders.append(s_lock_rho) s_post_lock_rho = Slider(title="R_0 after End of Stay-at-home order", value=post_lock_rho, start=0.1, end=6.0, step=delta / 10) sliders.append(s_post_lock_rho) s_post_lock_time = Slider( title="End of Stay-at-home posture (Days after 1 Mar)", value=post_lock_time, start=50, end=200, step=1) sliders.append(s_post_lock_time) s_init_inf = Slider(title="Initial Infectious", value=init_inf, start=1, end=100, step=1) sliders.append(s_init_inf) s_init_exp = Slider(title="Initial Exposed", value=init_exp, start=0, end=100, step=1) sliders.append(s_init_exp) s_init_rec = Slider(title="Initial Recovered", value=init_rec, start=0, end=100, step=1) sliders.append(s_init_rec) s_pct_tst = Slider( title="Percent of Infected Population Tested & Positive", value=pct_tst * 100, start=1, end=100, step=delta) sliders.append(s_pct_tst) s_tau = Slider(title="Infectious Period (days)", value=tau, start=2, end=14, step=delta) sliders.append(s_tau) s_mu = Slider(title="Incubation Period (days)", value=mu, start=0, end=7, step=delta) sliders.append(s_mu) if model == 'SIR_nu' or model == 'SEIR_nu': s_het = Slider(title="Power-Law Heterogeneity", value=nu, start=1.0, end=3.3, step=delta) elif model == 'NBD_SEIR': s_het = Slider(title="NBD Homogeneity", value=k, start=0.001, end=5, step=0.01) sliders.append(s_het) s_mort_rate = Slider(title="Mortality Rate (%)", value=mort_rate * 100, start=0.1, end=10, step=delta) sliders.append(s_mort_rate) s_symp_2_death = Slider(title="Avg Delay from Symptoms until Death (days)", value=symp_2_death, start=3, end=20, step=delta) sliders.append(s_symp_2_death) s_conf_case_delay = Slider(title="Avg Delay from Test Processing (days)", value=conf_case_delay, start=0, end=14, step=delta) sliders.append(s_conf_case_delay) s_t = Slider(title="Timestep (minutes)", value=t * (60 * 24), start=15, end=60 * 24, step=5) sliders.append(s_t) # Set up callbacks def update_title(attrname, old, new): plot1.title.text = text.value #text.on_change('value', update_title) def update_data(attrname, old, new): # Get the current slider values init_rho = s_init_rho.value lock_rho = s_lock_rho.value post_lock_rho = s_post_lock_rho.value post_lock_time = s_post_lock_time.value post_lock_date = start_date + timedelta(days=post_lock_time) init_inf = s_init_inf.value init_exp = s_init_exp.value init_red = s_init_rec.value pct_tst = s_pct_tst.value / 100 tau = s_tau.value mu = s_mu.value if model_sel.active in [0, 1]: # Power law models (nu) nu = s_het.value elif model_sel.active == 2: # NBD model (k) k = s_het.value mort_rate = s_mort_rate.value / 100 symp_2_death = s_symp_2_death.value conf_case_delay = s_conf_case_delay.value t = s_t.value / (60 * 24) max_ind = int((today - start_date).days / t) [death_times, case_times, deaths, cases] = dataHandler(start_date, conf_case_delay) rho_sched = {lock_date: lock_rho, post_lock_date: post_lock_rho} if model_sel.active == 0: # SIR_nu I_ind = 2 R_ind = 3 D_ind = 4 out2 = SIR_nu(start_date, P=720403, I=init_inf, R=init_rec, rho=init_rho, tau=tau, nu=nu, MaxDays=MaxDays, suppress_output=1, rho_sched=rho_sched, mort_rate=mort_rate, symp_2_death=symp_2_death, t=t) elif model_sel.active == 1: # SEIR_nu I_ind = 3 R_ind = 4 D_ind = 5 out2 = SEIR_nu(start_date, P=720403, E=init_exp, I=init_inf, R=init_rec, mu=mu, rho=init_rho, tau=tau, nu=nu, MaxDays=MaxDays, suppress_output=1, rho_sched=rho_sched, mort_rate=mort_rate, symp_2_death=symp_2_death, t=t) elif model_sel.active == 2: # NBD_SEIR I_ind = 3 R_ind = 4 D_ind = 6 out2 = NBD_SEIR(start_date, 720403, init_exp, init_inf, init_rec, init_rho, tau, k, mu, county, MaxDays, suppress_output=1, rho_sched=rho_sched, mort_rate=mort_rate, symp_2_death=symp_2_death, t=t) T = out2[0] I = out2[I_ind] R = out2[R_ind] D = out2[D_ind] T1 = T T2 = T[0:max_ind] T3 = T[0:-C] CC1 = array(R[C:max_ind + C]) CC1p = pct_tst * CC1 CC2 = array(R[C:]) CC2p = pct_tst * CC2 [death_times, case_times, deaths, cases] = dataHandler(start_date, conf_case_delay) plot1.y_range.start = 0 plot1.y_range.end = max(deaths) plot2.y_range.start = 0 plot2.y_range.end = max(D) if zm_toggle == -1: plot3.y_range.start = 0 plot3.y_range.end = R[max_ind] plot4.y_range.start = 0 plot4.y_range.end = max(R) # Generate the new curve source1.data = dict(T1=T1, I=I, D=D) source2.data = dict(T2=T2, CC1=CC1, CC1p=CC1p) source3.data = dict(T3=T3, CC2=CC2, CC2p=CC2p) scatter_source1.data = dict(Td=death_times, d=deaths) scatter_source2.data = dict(Tc=case_times, c=cases) def update_radio_buttons(attrname, old, new): if model_sel.active in [0, 1]: s_het.title = "Power-Law Heterogeneity" s_het.value = nu s_het.start = 1.0 s_het.end = 3.3 s_het.step = delta elif model_sel.active == 2: s_het.title = "NBD Heterogeneity" s_het.value = k s_het.start = 0.0001 s_het.end = 5 s_het.step = delta / 10 update_data(attrname, old, new) def zm_button_event(event): global zm_toggle zm_toggle = -zm_toggle if zm_toggle == 1: plot3.y_range.start = 0 plot3.y_range.end = max(cases) elif zm_toggle == -1: plot3.y_range.start = 0 plot3.y_range.end = R[max_ind] def legend_button_event(event): global legend_toggle legend_toggle = -legend_toggle if legend_toggle == 1: plot1.legend.visible = True plot2.legend.visible = True plot3.legend.visible = True plot4.legend.visible = True elif legend_toggle == -1: plot1.legend.visible = False plot2.legend.visible = False plot3.legend.visible = False plot4.legend.visible = False for s in sliders: s.on_change('value', update_data) model_sel = RadioButtonGroup(labels=['SIR-nu', 'SEIR-nu', 'NBD_SEIR'], active=2) model_sel.on_change('active', update_radio_buttons) zm_button = Button(label='Zoom to Confirmed Cases', button_type='success') zm_button.on_event(ButtonClick, zm_button_event) legend_button = Button(label='Hide Plot Legends', button_type='success') legend_button.on_event(ButtonClick, legend_button_event) # Set up layouts and add to document inputs = column( model_sel, s_init_rho, s_lock_rho, s_post_lock_rho, s_post_lock_time, s_init_inf, s_init_exp, s_init_rec, s_pct_tst, s_tau, s_mu, s_het, #s_nu, s_k, s_mort_rate, s_symp_2_death, s_conf_case_delay, s_t) grid = gridplot([[plot1, plot2], [plot3, plot4]]) middle = column(grid, zm_button) curdoc().add_root(row(inputs, middle, legend_button, width=800)) curdoc().title = "El Paso County COVID-19 App"
def callback(attr, old, new): if new == 0: data = df else: data = df.rolling('{0}D'.format(new)).mean() source.data = ColumnDataSource(data=data).data
def bokeh_reduce_scatter( df, file_name, file_title, colors_column=None, radii_column=None, colors_scale_method = None, radii_scale_method = None, max_radii = 0.5, fill_alpha = 0.5, nonselection_alpha=0.5, hover_info=None, select_tools=['wheel_zoom', 'crosshair', 'undo', 'redo', 'box_select', 'lasso_select', 'poly_select', 'tap', 'reset', 'box_zoom'], x_axis_label='X', y_axis_label='y', plot_height=800, plot_width=1200, mpl_color_map=['viridis'], plot_title='plot_title', toolbar_location='below', line_color=None, ): # create 2Dcolormap if not color array provided if not colors_column: df['filling_colors'] = bidim_colormap(df[[x_axis_label, y_axis_label]].values, cmp=mpl_color_map) else: df['filling_colors'] = df[colors_column] if colors_scale_method: df = scale_df(df,['filling_colors'],method = 'RobustScaler') if radii_scale_method: df = scale_df(df, [radii_column], method='RobustScaler') radiiarg = {} if radii_column: radiiarg = {'radius': radii_column} data_cds = ColumnDataSource(df) fig = figure( plot_height=plot_height, plot_width=plot_width, x_axis_label=x_axis_label, y_axis_label=y_axis_label, title=plot_title, toolbar_location=toolbar_location, tools=select_tools, ) fig.scatter( x=x_axis_label, y=y_axis_label, source=data_cds, # color=colors, selection_color='deepskyblue', nonselection_color='lightgray', nonselection_alpha=nonselection_alpha, fill_alpha = fill_alpha, fill_color='filling_colors', line_color=line_color, **radiiarg ) # Add the HoverTool to the figure if hover_info: if hover_info.__class__ not in [list, tuple, set]: hover_info = [hover_info] tooltips = [(i, '@{}'.format(i)) for i in hover_info] fig.add_tools(HoverTool(tooltips=tooltips)) # Visualize output_file(file_name + '.html', title=file_title, mode="cdn" ) show(fig) return fig
def _init_datasource(self, data): """ Initializes a data source to be passed into the bokeh glyph. """ data = {k: decode_bytes(vs) for k, vs in data.items()} return ColumnDataSource(data=data)
def get_dataset(src, name, car): df = src[(src.year == name) & (src.brand == car)].copy() return ColumnDataSource(data=df)
ProofType = Div(text=div_style + '<div class="sans-font"><h3>Proof Type</h3></div>') ProofTypev = Div(text=div_style + '<div class="sans-font"><p></p></div>') TotalCoinsMined = Div( text=div_style + '<div class="sans-font"><h3>Total Coins Mined</h3></div>') TotalCoinsMinedv = Div(text=div_style + '<div class="sans-font"><p></p></div>') NumberofExchanges = Div( text=div_style + '<div class="sans-font"><h3>Number of Exchanges</h3></div>') NumberofExchangesv = Div(text=div_style + '<div class="sans-font"><p></p></div>') AdditionalSocialData = Div( text=div_style + '<div class="sans-font"><h3>Additional Social Data</h3></div>') asosource = ColumnDataSource( dict(twitter_statuses=[], reddit_comments_per_day=[], facebook_likes=[])) asocolumns = [ TableColumn(field="twitter_statuses", title="Twitter Statuses"), TableColumn(field="reddit_comments_per_day", title="Reddit Comments per Day"), TableColumn(field="facebook_likes", title="Facebook Likes") ] asostats = DataTable(source=asosource, columns=asocolumns, fit_columns=True, row_headers=False, width=540, height=100) tmetric = Select(value='Total Volume', options=['Total Volume', 'Market Cap', 'Average Daily Price'])
y_axis_one = df.at[2, 'Config'] y_axis_two = df.at[3, 'Config'] y_axis_three = df.at[4, 'Config'] y_axis_four = df.at[5, 'Config'] ucl = df.at[6, 'Config'] lcl = df.at[7, 'Config'] x_label = df.at[8, 'Config'] y_label = df.at[9, 'Config'] histogram_column = df.at[10, 'Config'] if not pd.isnull(ucl): df[ucl] = df[ucl].apply(lambda x: df.at[0, ucl]) if not pd.isnull(lcl): df[lcl] = df[lcl].apply(lambda x: df.at[0, lcl]) source = ColumnDataSource(df) output_file(df.at[0, 'Config'] + ".html") # Plot control chart figure p_control = figure(title=title + " - Control Chart", toolbar_location="above", plot_width=1000, plot_height=400, tools="hover") tooltips = [] columns = [] if not pd.isnull(y_axis_one): p_control.line(x=x_axis, y=y_axis_one,
def create_plots(Estimators): print("Starting update") if not isinstance(Estimators, (type(np.array), list)): Estimators = np.array(Estimators) estimator_names = np.array(list(estimator_select.value)) ix = np.isin(Estimator_Names, estimator_names) estimator_indices = [int(i) for i in np.where(ix)[0].flatten()] estimators = np.array(Estimators)[estimator_indices] variable1 = drop1.value variable2 = drop2.value y = drop3.value #Things to update: # image background i.e. image source √ # observation source √ #Color mapper values√ #hover tool values √ #Figure ranges √ #Model score text things √ #Lets calculate all the image and observation data first plots = [None for i in range(len(estimators))] image_sources = [None for i in range(len(estimators))] observation_sources = [None for i in range(len(estimators))] hover_tools = [None for i in range(len(estimators))] model_score_sources = [None for i in range(len(estimators))] glyphs0 = [None for i in range(len(estimators))] color_bars = [None for i in range(len(estimators))] p_circles = [None for i in range(len(estimators))] p_images = [None for i in range(len(estimators))] #Iterate over the estimators for idx, estimator in enumerate(estimators): #Find the title for each plot estimator_name = str(estimator).split('(')[0] #Extract the needed data full_mat = X[[variable1, variable2, y]].dropna(how="any", axis=0) #Define a class bijection for class colour mapping unique_classes, y_bijection = np.unique(full_mat[y], return_inverse=True) full_mat['y_bijection'] = y_bijection #Rescale the X Data so that the data fits nicely on the axis/predictions are reliable full_mat[variable1 + "_s"] = StandardScaler().fit_transform( full_mat[variable1].values.reshape((-1, 1))) full_mat[variable2 + "_s"] = StandardScaler().fit_transform( full_mat[variable2].values.reshape((-1, 1))) #Define the Step size in the mesh delta = Delta #Separate the data into arrays so it is easy to work with X1 = full_mat[variable1 + "_s"].values X2 = full_mat[variable2 + "_s"].values Y = full_mat["y_bijection"].values #Define the mesh-grid co-ordiantes over which to colour in x1_min, x1_max = X1.min() - 0.5, X1.max() + 0.5 x2_min, x2_max = X2.min() - 0.5, X2.max() + 0.5 #Create the meshgrid itself x1, x2 = np.arange(x1_min, x1_max, delta), np.arange(x2_min, x2_max, delta) x1x1, x2x2 = np.meshgrid(x1, x2) #Create the train test split X_train, X_test, y_train, y_test = train_test_split( full_mat[[variable1 + "_s", variable2 + "_s"]], Y, test_size=Test_Size, random_state=Random_State) #Fit and predict/score the model model = estimator.fit(X=X_train, y=y_train) # train_preds = model.predict(X_train) # test_preds = model.predict(X_test) model_score = model.score(X_test, y_test) model_score_text = "Model score: %.2f" % model_score if hasattr(model, "decision_function"): Z = model.decision_function(np.c_[x1x1.ravel(), x2x2.ravel()]) elif hasattr(model, "predict_proba"): Z = model.predict_proba(np.c_[x1x1.ravel(), x2x2.ravel()]) else: print( "This Estimator doesn't have a decision_function attribute and can't predict probabilities" ) Z = np.argmax(Z, axis=1) Z_uniques = np.unique(Z) unique_predictions = unique_classes[Z_uniques] Z = Z.reshape(x1x1.shape) #Add in the probabilities and predicitions for the tooltips full_mat["probability"] = np.amax(model.predict_proba( full_mat[[variable1 + "_s", variable2 + "_s"]]), axis=1) bijected_predictions = model.predict( full_mat[[variable1 + "_s", variable2 + "_s"]]) full_mat["prediction"] = unique_classes[bijected_predictions] #Add an associated color to the predictions number_of_colors = len(np.unique(y_bijection)) #Create the hover tool to be updated hover = HoverTool( tooltips=[(variable1, "@" + variable1), ( variable2, "@" + variable2), ( "Probability", "@probability"), ("Prediction", "@prediction"), ("Actual", "@" + y)]) #Create the axes for all the plots plots[idx] = figure(x_axis_label=variable1, y_axis_label=variable2, title=estimator_name, x_range=(x1x1.min(), x1x1.max()), y_range=(x2x2.min(), x2x2.max()), plot_height=600, plot_width=600, toolbar_sticky=False, toolbar_location="above") #Create all the image sources image_data = dict() image_data['x'] = np.array([x1x1.min()]) image_data["y"] = np.array([x2x2.min()]) image_data['dw'] = np.array([x1x1.max() - x1x1.min()]) image_data['dh'] = np.array([x2x2.max() - x2x2.min()]) image_data['boundaries'] = [Z] image_sources[idx] = ColumnDataSource(image_data) if number_of_colors > 11: col_palette = inferno(number_of_colors) elif number_of_colors > 2: col_palette = RdBu[number_of_colors] else: col_palette = ["#1D61F2 ", "#F73B28"] low = full_mat["y_bijection"].min() high = full_mat["y_bijection"].max() cbar_mapper = LinearColorMapper(palette=col_palette, high=high, low=low) p_images[idx] = plots[idx].image(image='boundaries', x='x', y='y', dw='dw', dh='dh', color_mapper=cbar_mapper, source=image_sources[idx]) #Create the sources to update the observation points observation_sources[idx] = ColumnDataSource(data=full_mat) p_circles[idx] = plots[idx].circle(x=variable1 + "_s", y=variable2 + "_s", color=dict(field='y_bijection', transform=cbar_mapper), source=observation_sources[idx], line_color="black") #Create the hovertool for each plot hover_tools[idx] = hover #Add the hover tools to each plot plots[idx].add_tools(hover_tools[idx]) #Create all the text sources (model scores) for the plots model_score_sources[idx] = ColumnDataSource( data=dict(x=[x1x1.min() + 0.3], y=[x2x2.min() + 0.3], text=[model_score_text])) #Add the model scores to all the plots score_as_text = Text(x="x", y="y", text="text") glyphs0[idx] = plots[idx].add_glyph(model_score_sources[idx], score_as_text) #Add a colorbar color_bars[idx] = ColorBar( color_mapper=cbar_mapper, ticker=BasicTicker(desired_num_ticks=number_of_colors), label_standoff=12, location=(0, 0), bar_line_color="black") plots[idx].add_layout(color_bars[idx], "right") plots[idx].add_tools(LassoSelectTool(), WheelZoomTool()) # configure so that no drag tools are active plots[idx].toolbar.tools = plots[idx].toolbar.tools[1:] plots[idx].toolbar.tools[0], plots[idx].toolbar.tools[-2] = plots[ idx].toolbar.tools[-2], plots[idx].toolbar.tools[0] layout_col = [row(plot) for plot in plots] print("Ending Update") return layout_col
import numpy as np from bokeh.models import ColumnDataSource, Plot, LinearAxis, Grid from bokeh.models.markers import Hex from bokeh.io import curdoc, show N = 9 x = np.linspace(-2, 2, N) y = x**2 sizes = np.linspace(10, 20, N) source = ColumnDataSource(dict(x=x, y=y, sizes=sizes)) plot = Plot( title=None, plot_width=300, plot_height=300, h_symmetry=False, v_symmetry=False, min_border=0, toolbar_location=None) glyph = Hex(x="x", y="y", size="sizes", fill_color="#f0027f") plot.add_glyph(source, glyph) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) curdoc().add_root(plot)
Remember, as with the figures you generated in previous chapters, you can interact with your figures here with a variety of tools. Instructions Import output_file and show from bokeh.io, figure from bokeh.plotting, and HoverTool and ColumnDataSource from bokeh.models. Make a ColumnDataSource called source with x set to the fertility column, y set to the life column and country set to the Country column. For all columns, select the rows with index value 1970. This can be done using data.loc[1970].column_name. ''' # Perform necessary imports from bokeh.io import output_file, show from bokeh.plotting import figure from bokeh.models import HoverTool, ColumnDataSource # Make the ColumnDataSource: source source = ColumnDataSource(data={ 'x' : data.loc[1970].fertility, 'y' : data.loc[1970].life, 'country' : data.loc[1970].Country, }) # Create the figure: p p = figure(title='1970', x_axis_label='Fertility (children per woman)', y_axis_label='Life Expectancy (years)', plot_height=400, plot_width=700, tools=[HoverTool(tooltips='@country')]) # Add a circle glyph to the figure p p.circle(x='x', y='y', source=source) # Output the file and show the figure output_file('gapminder.html') show(p)
# adding hover tools to plot plot.add_tools(hover_tool) # line graph data filtration df_short = df2[['year', 'price']] # group by mean and reset index df_line = df_short.groupby(df['year'])['price'].agg(['mean']) df_line = df_line.reset_index(drop=False) # rename columns df_line.columns = ['year', 'avg_price'] # declare source of line graph source2 = ColumnDataSource(df_line) # create line graph line_plot = create_linegraph(source2) # hover tool (shows values on graph) hover_tool2 = HoverTool( tooltips=[('Average Price', '@avg_price{($0,000.00)}'), ('Year', '@year{(0000)}')]) # adding hover tools to plot line_plot.add_tools(hover_tool2) # format widget grid for layout widgets = column(year_select, brand_select, sizing_mode='scale_width')
parse_dates=['gmDate']) standings = pd.read_csv(WORK_DIR + '/data/2017-18_standings.csv', parse_dates=['stDate']) west_top_2 = (standings[(standings['teamAbbr'] == 'HOU') | (standings['teamAbbr'] == 'GS')]. loc[:, ['stDate', 'teamAbbr', 'gameWon']].sort_values( ['teamAbbr', 'stDate'])) print(west_top_2.head()) # Output to file output_file('west-top-2-standings-race.html', title='Western Conference Top 2 Teams Wins Race') # Create a ColumnDataSource west_cds = ColumnDataSource(west_top_2) # Create views for each team rockets_view = CDSView( source=west_cds, filters=[GroupFilter(column_name='teamAbbr', group='HOU')]) warriors_view = CDSView( source=west_cds, filters=[GroupFilter(column_name='teamAbbr', group='GS')]) # Create and configure the figure west_fig = figure(x_axis_type='datetime', plot_height=300, plot_width=600, title='Western Conference Top 2 Teams Wins Race, 2017-18', x_axis_label='Date', y_axis_label='Wins',
def index(request): if request.user.is_anonymous: return redirect("/login") labels = [] data = [] now = datetime.now() queryset = Project_add.objects.all() lstProj = queryset[len(queryset) - 1] for proj in queryset: labels.append(proj.name) dateStr = proj.date.split(' ')[0].split('-')[1] curYear = proj.date.split(' ')[0].split('-')[0] if curYear == str(now.year): if dateStr[0] == '0': data.append(dateStr[1]) else: data.append(dateStr) df = DataFrame({ 'month': data, }) df2 = DataFrame({'count': df.groupby(["month"]).size()}).reset_index() df2['class-date'] = df2['month'].map(str) # x (months) and y(count of projects) axes class_date = df2['class-date'].tolist() count = df2['count'].tolist() for i in range(1, 13): if not (str(i) in class_date): class_date.insert(i - 1, str(i)) count.insert(i - 1, 0) replacements = { '1': 'Jan', '2': 'Feb', '3': 'Mar', '4': 'Apr', '5': 'May', '6': 'Jun', '7': 'Jul', '8': 'Aug', '9': 'Sep', '10': 'Oct', '11': 'Nov', '12': 'Dec' } class_date = [replacements.get(x, x) for x in class_date] monDict = dict() counts = [] for idx, c in enumerate(count): if c != 0: monDict[class_date[idx]] = [] counts.append(c) startIdx = 0 for idx, item in enumerate(monDict.keys()): print(item, monDict[item], labels[startIdx:startIdx + counts[idx]]) monDict[item] = labels[startIdx:startIdx + counts[idx]] startIdx += counts[idx] # Bokeh's mapping of column names and data lists source = ColumnDataSource(data=dict( class_date=class_date, count=count, color=Viridis3 + Viridis9)) # Bokeh's convenience function for creating a Figure object p = figure(x_range=class_date, plot_height=350, title="# Projects created per month in " + str(now.year), toolbar_location="below") # Render and show the vbar plot p.vbar(x='class_date', top='count', width=0.9, color='color', source=source) script, div = components(p) return render(request, 'index.html', { 'script': script, 'div': div, 'lstProj': lstProj, 'monDict': monDict })
def show(self, connected=False): node_indices = list(self.graph.vertices.keys()) plot = figure(title='Graph Layout Demonstration', x_range=(-1, 5), y_range=(-1, 5), tools='', toolbar_location=None) plot.axis.visible = False plot.grid.visible = False graph = GraphRenderer() graph.node_renderer.data_source.add(node_indices, 'index') # Random color generator if connected: graph.node_renderer.data_source.add(node_indices, 'index') color = [] for vertex in self.graph.vertices: color.append(self.graph.vertices[vertex].color) else: graph.node_renderer.data_source.add(node_indices, 'index') number_of_colors = len(node_indices) color = [ "#" + ''.join([random.choice('0123456789ABCDEF') for _ in range(6)]) for _ in range(number_of_colors) ] graph.node_renderer.data_source.add(color, 'color') graph.node_renderer.glyph = Circle(size=20, fill_color='color') edge_start = [] edge_end = [] for vertex in self.graph.vertices: for edge in self.graph.vertices[vertex].edges: edge_start.append(vertex) edge_end.append(edge.name) graph.edge_renderer.data_source.data = dict(start=edge_start, end=edge_end) # TODO: Need to implement a way that nodes don't overlap, or are too close position = {} for vertex in self.graph.vertices: position[vertex] = (random.random() * 4, random.random() * 4) graph.layout_provider = StaticLayoutProvider(graph_layout=position) source = ColumnDataSource( dict(x=[x for x, y in position.values()], y=[y for x, y in position.values()], name=node_indices)) labels = LabelSet(x='x', y='y', text='name', level='glyph', x_offset=7, y_offset=7, source=source, render_mode='canvas') plot.add_layout(labels) plot.renderers.append(graph) output_file('graph.html') show(plot)
actual_size.append(deathsByState[i][6]) for i in testsByState: # size color_list.append((float(testsByState[i][6]) / final_population[i]) * 250) p = figure(title="COVID-19 Number of Deaths vs Testing: September", plot_width=1000, plot_height=650) p.patches(state_xs, state_ys, line_color="#000000", line_width=2, fill_color="#ffffff") source = ColumnDataSource( dict(x=x, y=y, size_list=size_list, color_list=color_list, actual_size=actual_size)) mapper = linear_cmap(field_name='size_list', palette=Spectral11, low=min(size_list), high=max(size_list)) renderer = p.circle(source=source, x='x', y='y', size='color_list', fill_color=mapper) hover_tool = HoverTool(tooltips=[('tests', '@color_list')], renderers=[renderer]) p.add_tools(hover_tool) color_bar = ColorBar(title='Deaths',
class Page: def __init__(self, sqlsession): self.doc = bokeh.plotting.curdoc() self.sqlsession = sqlsession self.graph = MyGraph() self.infoPanel = InfoPanel() self.newSamplePanel = NewSamplePanel() self.imagePanel = ImagePanel() self.uploadfileSource = ColumnDataSource({ 'fileContents': [], 'fileName': [] }) self.infoPanel.loadImageButton.js_on_click( CustomJS(args=dict(file_source=self.uploadfileSource), code=""" function read_file(filename) { var reader = new FileReader(); reader.onload = load_handler; reader.onerror = error_handler; // readAsDataURL represents the file's data as a base64 encoded string reader.readAsDataURL(filename); } function load_handler(event) { var b64string = event.target.result; file_source.data = {'file_contents' : [b64string], 'file_name':[input.files[0].name]}; file_source.change.emit(); } function error_handler(evt) { console.log('error') if(evt.target.error.name == "NotReadableError") { console.log("Can't read file!") } } function timed_check(){ console.log('timer'); console.log(input.files); if (input.files.length != 0){ readfile(); } else{ setTimeout(timed_check,500); } } function readfile(){ if (window.FileReader) { read_file(input.files[0]); } else { console.log('FileReader is not supported in this browser'); } } var input = document.createElement('input'); input.setAttribute('type', 'file'); input.setAttribute("accept","image/*","capture=camera"); input.onclick = function(){ input.setAttribute('value','""'); //clear the filelist setTimeout(timed_check,500); } input.click(); """)) self.uploadfileSource.on_change('data', self.uploadCallback) self.plotInfoRow = Row(self.graph.widget, self.infoPanel.widget) self.dialog = Dialog(self.graph.plot) self.loadData() '''Callbacks!!!!!!''' self.newSamplePanel.button.on_click(self.newSampleCallback) self.graph.renderer.node_renderer.data_source.on_change( 'selected', self.nodeSelectCallback) self.graph.xSelectSwitch.on_change('active', self.xSelectCallback) self.infoPanel.addNoteButton.on_click(self.addNoteCallback) self.imagePanel.imgSelectDropDown.on_click(self.selectImageCallback) self.infoPanel.deleteButton.on_click(self.deleteSampleCallback) '''''' self.tab1 = Panel(child=self.plotInfoRow, title='Plot') self.tab2 = Panel(child=self.dTable, title='Data') self.tabs = Tabs(tabs=[ self.tab1, self.tab2, self.newSamplePanel.widget, self.imagePanel.widget ]) def loadData(self): self.graph.getFromDB(self.sqlsession) # reregister select callback self.graph.renderer.node_renderer.data_source.on_change( 'selected', self.nodeSelectCallback) self.graph.renderer.node_renderer.data_source.trigger( 'selected', [], self.graph.renderer.node_renderer.data_source.selected) '''Data Table''' col = self.graph.renderer.node_renderer.data_source colnames = [TableColumn(field=k, title=k) for k in col.data.keys()] self.dTable = DataTable(source=col, columns=colnames) '''Callbacks!!!!!!!!!!!!''' def nodeSelectCallback(self, attr: str, old, new: bokeh.models.Selection): print('node call') try: index = new.indices[0] datasource = self.graph.renderer.node_renderer.data_source ID = datasource.data['id'][index] species = datasource.data['species'][index] Type = datasource.data['type'][index] birthDate = datasource.data['birthDate'][index] notes = datasource.data['notes'][index] images = datasource.data['images'][index] objectRef = self.sqlsession.query(Sample).filter_by( id=int(ID)).first() except IndexError: # nothing is selected ID, species, Type, birthDate, notes, images, objectRef = (None, None, None, None, [], [], None) self.infoPanel.updateText(ID, species, Type, birthDate, notes, images, objectRef) self.newSamplePanel.parentText.value = str(ID) if objectRef: self.imagePanel.imgSelectDropDown.menu = [ (str(i + 1), v.text) for i, v in enumerate(objectRef.images) ] self.imagePanel.reset() def newSampleCallback(self): print('new sample') panel = self.newSamplePanel try: ID = list(map(int, panel.parentText.value.split(','))) parent = self.sqlsession.query(Sample).filter( Sample.id.in_(ID)).all() except ValueError: parent = panel.parentText.value # for if its a new sample and the species was entered instead samples = [] for i in range(int(self.newSamplePanel.copiesSelector.value)): sample = Sample(parent, panel.typeButtons.labels[panel.typeButtons.active], birthdate=panel.dateText.value) if panel.noteText.value: sample.addNote(panel.noteText.value) self.sqlsession.add(sample) samples.append(sample) self.sqlsession.commit() self.loadData() ids = [sample.id for sample in samples ] # this only works if it comes after the commit idstring = ','.join(map(str, ids)) self.dialog.open('alert', "Successfully added new sample: {}".format(idstring)) def addNoteCallback(self, note=None): if note is None: print('note added') self.dialog.open('prompt', 'Note', self.addNoteCallback) elif type(note) == str: self.infoPanel.object.addNote(note) self.sqlsession.commit() self.loadData() else: print('type not valid', type(note)) def uploadCallback(self, attr, old, new): print('filename:', self.uploadfileSource.data['file_name']) raw_contents = self.uploadfileSource.data['file_contents'][0] # remove the prefix that JS adds prefix, b64_contents = raw_contents.split(",", 1) file_contents = base64.b64decode(b64_contents) file_io = io.BytesIO(file_contents) try: image = Image.open(file_io) for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == 'Orientation': break if orientation in image._getexif().keys(): print('rotate') exif = dict(image._getexif().items()) if exif[orientation] == 3: image = image.rotate(180, expand=True) elif exif[orientation] == 6: image = image.rotate(270, expand=True) elif exif[orientation] == 8: image = image.rotate(90, expand=True) elif exif[orientation] == 1: print('no rotation needed') else: print('no valid rotation found') print('orientation value = ', exif[orientation]) except: raise TypeError("Failed to upload file") return imgs = glob.glob(os.path.join('static', '*.png')) if len(imgs) == 0: newfname = '1' else: newfname = str( max([int(i.split(os.sep)[-1][:-4]) for i in imgs]) + 1) self.infoPanel.object.addImage(newfname) image.save(os.path.join('static', newfname + '.png')) self.sqlsession.commit() self.loadData() self.dialog.open('alert', 'Image successfully added.') def selectImageCallback(self, event: bokeh.events.ButtonClick): new = event.item if new is not None: self.imagePanel.reset() imgurl = os.path.join("myco_app", 'static', new + ".png") img_source = ColumnDataSource(dict(url=[imgurl])) if len(self.imagePanel.plot.renderers) > 0: self.imagePanel.plot.renderers.pop(-1) width, height = Image.open(os.path.join('static', new + '.png')).size newWidth = width / max((width, height)) newHeight = height / max((width, height)) self.imagePanel.plot.image_url(url='url', x=0, y=1, w=newWidth, h=newHeight, source=img_source) def xSelectCallback(self, attr, old, new): print(new) if new == 1: self.graph._graph_layout = date_pos self.graph.plot.xaxis[0].axis_label = 'Days' elif new == 0: self.graph._graph_layout = side_pos self.graph.plot.xaxis[0].axis_label = 'Generations' else: print("xSelect buttton choice not valid") self.loadData() def deleteSampleCallback(self, choice: bool = None): if choice is None: print('del') choice = self.dialog.open('confirm', "Are you sure you want to delete?", self.deleteSampleCallback) elif choice and self.infoPanel.object: if len(self.infoPanel.object.children) == 0: imFiles = [i.text for i in self.infoPanel.object.images] for i in imFiles: os.remove(os.path.join('static', i + '.png')) self.sqlsession.delete(self.infoPanel.object) self.sqlsession.commit() self.loadData() else: self.dialog.open( 'alert', 'Cannot delete a sample that has child samples') else: print("Cancelled deletion")
("Gainsboro", "#DCDCDC", "Gray/Black"), ("LightGray", "#D3D3D3", "Gray/Black"), ("Silver", "#C0C0C0", "Gray/Black"), ("DarkGray", "#A9A9A9", "Gray/Black"), ("Gray", "#808080", "Gray/Black"), ("DimGray", "#696969", "Gray/Black"), ("LightSlateGray", "#778899", "Gray/Black"), ("SlateGray", "#708090", "Gray/Black"), ("DarkSlateGray", "#2F4F4F", "Gray/Black"), ("Black", "#000000", "Gray/Black"), ], columns=["Name", "Color", "Group"]) source = ColumnDataSource( dict( names=list(css3_colors.Name), groups=list(css3_colors.Group), colors=list(css3_colors.Color), )) xdr = FactorRange(factors=list(css3_colors.Group.unique())) ydr = FactorRange(factors=list(reversed(css3_colors.Name))) x_scale, y_scale = CategoricalScale(), CategoricalScale() plot = Plot(x_range=xdr, y_range=ydr, x_scale=x_scale, y_scale=y_scale, plot_width=600, plot_height=2000) plot.title.text = "CSS3 Color Names"
def plot_graph_by_transaction(scenario_metrics_df: pd.DataFrame, overall_percentile_df: pd.DataFrame, scenario: str, right_y_axis_filter: str, percentile: int) -> figure(): # Remove $ from the names of column names of scenario_metrics_df and # Rename the Transactions of overall_percentile_df col_name_dict = remove_dollar_sign_and_get_column_names_dict( scenario_metrics_df, overall_percentile_df) # Define Y-Axis Range of the Graph (left_y_range, right_y_range) = get_y_range_of_graph(scenario_metrics_df, right_y_axis_filter) # Get the colors for the Lines of the Graph color_palette = get_color_palette(scenario_metrics_df, scenario) # Tools to be available in graph tools_to_show = 'box_zoom,reset,save' # create a new plot with a title and axis labels scenario_graph = plot_new_graph('Time', 'datetime', 'Response Time (ms)', 1900, 400, left_y_range, 'below', tools_to_show) # Disabling Hover Tool scenario_graph.toolbar.active_inspect = None # Index to go through Color Palette color_index = 0 # get all the legends in one list legend_list = [] # Sort the Transactions names in Alphabetical order transaction_col_list = sort_transaction_names_and_remove_localtime_col( right_y_axis_filter, list(scenario_metrics_df.columns)) # Source of Graphs source = ColumnDataSource(scenario_metrics_df) # Plot graph transaction-wise for col_name in transaction_col_list: if col_name in right_y_axis_filter: # Get the legend name legend_name = col_name # Setting the second y axis range name and range scenario_graph.extra_y_ranges = { col_name: Range1d(0, right_y_range) } # Adding the second axis to the plot. scenario_graph.add_layout( LinearAxis(y_range_name=col_name, axis_label=col_name), 'right') # PlotGraph if col_name in "Errors": axis_color = "#d62728" else: axis_color = "yellow" plot_graph = scenario_graph.line('LocalTime', col_name, source=source, line_width=2, color=axis_color, y_range_name=col_name, name=col_name) else: # Transaction Percentile col_percentile = int(overall_percentile_df.loc[ overall_percentile_df['Transaction'] == col_name, 'Percentile'].item()) # Get the legend name along with Transaction's Percentile legend_name = col_name_dict[col_name] + " ({}th: {} ms)".format( percentile, col_percentile) # PlotGraph plot_graph = scenario_graph.line('LocalTime', col_name, source=source, line_width=2, color=color_palette[color_index], name=col_name) # increment through color palette color_index = color_index + 1 # Append the legend legend_list.append((legend_name, [plot_graph])) # Append the graph in list which will be passed to "Column" scenario_graph_final = set_graph_and_legend_properties( scenario_graph, legend_list, scenario) return scenario_graph_final
def __init__(self, sqlsession): self.doc = bokeh.plotting.curdoc() self.sqlsession = sqlsession self.graph = MyGraph() self.infoPanel = InfoPanel() self.newSamplePanel = NewSamplePanel() self.imagePanel = ImagePanel() self.uploadfileSource = ColumnDataSource({ 'fileContents': [], 'fileName': [] }) self.infoPanel.loadImageButton.js_on_click( CustomJS(args=dict(file_source=self.uploadfileSource), code=""" function read_file(filename) { var reader = new FileReader(); reader.onload = load_handler; reader.onerror = error_handler; // readAsDataURL represents the file's data as a base64 encoded string reader.readAsDataURL(filename); } function load_handler(event) { var b64string = event.target.result; file_source.data = {'file_contents' : [b64string], 'file_name':[input.files[0].name]}; file_source.change.emit(); } function error_handler(evt) { console.log('error') if(evt.target.error.name == "NotReadableError") { console.log("Can't read file!") } } function timed_check(){ console.log('timer'); console.log(input.files); if (input.files.length != 0){ readfile(); } else{ setTimeout(timed_check,500); } } function readfile(){ if (window.FileReader) { read_file(input.files[0]); } else { console.log('FileReader is not supported in this browser'); } } var input = document.createElement('input'); input.setAttribute('type', 'file'); input.setAttribute("accept","image/*","capture=camera"); input.onclick = function(){ input.setAttribute('value','""'); //clear the filelist setTimeout(timed_check,500); } input.click(); """)) self.uploadfileSource.on_change('data', self.uploadCallback) self.plotInfoRow = Row(self.graph.widget, self.infoPanel.widget) self.dialog = Dialog(self.graph.plot) self.loadData() '''Callbacks!!!!!!''' self.newSamplePanel.button.on_click(self.newSampleCallback) self.graph.renderer.node_renderer.data_source.on_change( 'selected', self.nodeSelectCallback) self.graph.xSelectSwitch.on_change('active', self.xSelectCallback) self.infoPanel.addNoteButton.on_click(self.addNoteCallback) self.imagePanel.imgSelectDropDown.on_click(self.selectImageCallback) self.infoPanel.deleteButton.on_click(self.deleteSampleCallback) '''''' self.tab1 = Panel(child=self.plotInfoRow, title='Plot') self.tab2 = Panel(child=self.dTable, title='Data') self.tabs = Tabs(tabs=[ self.tab1, self.tab2, self.newSamplePanel.widget, self.imagePanel.widget ])