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 generate_plot(): 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='', 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.selected['1d'].indices, data = cb_obj.data, selected_names = ''; Bokeh.$.each(indices, function(i, index) { selected_names += data['name'][index]; }); alert(selected_names); """) return plot
def test_editable_changes_data(output_file_url, selenium): # Make plot and add a taptool callback that generates an alert source = ColumnDataSource({'values': [1, 2]}) source.callback = CustomJS(code='alert(cb_obj.data.values)') column = TableColumn(field='values', title='values', editor=IntEditor()) data_table = DataTable(source=source, columns=[column], editable=True, width=600) # Save the plot and start the test save(data_table) selenium.get(output_file_url) assert has_no_console_errors(selenium) # Resize the page so that the table displays correctly selenium.set_window_size(width=800, height=800) # Click row_1 (which triggers first alert) row_1_cell = selenium.find_element_by_css_selector('.grid-canvas .slick-row:first-child .r1') row_1_cell.click() alert = selenium.switch_to_alert() assert alert.text == '1,2' alert.dismiss() # Now double click, enter the text 33 actions = ActionChains(selenium) row_1_cell = selenium.find_element_by_css_selector('.grid-canvas .slick-row:first-child .r1') actions.move_to_element(row_1_cell) actions.double_click() actions.send_keys(u"33\ue007") # After the backslash is ENTER key actions.perform() # Click row_2 (which triggers alert again so we can inspect the data) row_2_cell = selenium.find_element_by_css_selector('.grid-canvas .slick-row:nth-child(2) .r1') row_2_cell.click() alert = selenium.switch_to_alert() assert alert.text == '33,2'
def BuildHtmlMap(): freq = [] for i in model.wv.vocab: freq.append(model.wv.vocab[i].count) scale = max(freq) / 50 s1 = ColumnDataSource(data=dict(x=X[:, 0], y=X[:, 1], words=words)) # s1 = ColumnDataSource(data=dict(x=list(X_tsne[:, 0]), y=list(X_tsne[:, 1]), words=sentence,color=['#000000' for i in range(len(sentence))])) p1 = figure(tools="pan,lasso_select,wheel_zoom,undo,reset,save,tap", title="Select Here", plot_width=1000, plot_height=600) p1.scatter(x='x', y='y', size=10, source=s1, alpha=0) def lbset(i, j, size, tcol): x = [] x.append(j[0]) y = [] y.append(j[1]) z = [] z.append(i) col = [] col.append(tcol) s = ColumnDataSource(data=dict(x=x, y=y, words=z, color=col)) return LabelSet(x=j[0], y=j[1], text='words', source=s, text_font_size=size, render_mode='canvas', text_color='color') lbsets = [] for i, j in zip(words, X): lbsets.append( lbset(i, j, str(12 + model.wv.vocab[i].count / scale) + 'pt', word_centroid_map[i])) s1.callback = CustomJS(args=dict(s1=s1), code=""" var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; for (i = 0; i < inds.length; i++) { d1['color'][inds[i]]='#DC143C' } s1.change.emit(); """) tap = p1.select(type=TapTool) tap.callback = CustomJS(args=dict(s1=s1), code=""" var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; for (i = 0; i < inds.length; i++) { d1['words'][inds[i]]='' d1['x'][inds[i]]=100 } s1.change.emit(); """) for i in lbsets: p1.add_layout(i) script, div = components(p1) def ChangeName(filename): ext = filename.split('.')[-1] # get filename filename = '{}.{}'.format(uuid4().hex, ext) # return the whole path to the file return filename picture_name = ChangeName('.png') export_png(p1, os.path.join(STATIC_ROOT, picture_name)) # продакшн STATIC_ROOT #Создаем новое изображение imgopt=ImageOptions.objects.create(id=Img_id) imgopt.img = picture_name imgopt.num_clusters=num_cl imgopt.num_neighbors = num_neigh imgopt.opt=Options.objects.get(id=Opt_id) imgopt.script = script imgopt.div = div imgopt.save() args['username'] = auth.get_user(request).username args['script'] = script args['div'] = div args['Img_id'] = imgopt.id args['form'] = CentroidForm() args['form2'] = SimilarWordForm() args['form3'] = MinFrequencyWordForm() args['Data_id'] = Data_id args['Opt_id'] = Opt_id args['num_clusters'] =num_cl args['num_neighbors'] = num_neigh
def index(): """ Very simple embedding of a lightcurve chart """ # FLASK # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args _from = str(args.get('_from', str(DEFAULT_TR.start))) _to = str(args.get('_to', str(DEFAULT_TR.end))) tr = TimeRange(parse_time(_from), parse_time(_to)) if 'next' in args: tr = tr.next() if 'prev' in args: tr = tr.previous() if 'next_hour' in args: tr = TimeRange(tr.start + ONE_HOUR, tr.end + ONE_HOUR) if 'next_day' in args: tr = TimeRange(tr.start + ONE_DAY, tr.end + ONE_DAY) if 'prev_hour' in args: tr = TimeRange(tr.start - ONE_HOUR, tr.end - ONE_HOUR) if 'prev_day' in args: tr = TimeRange(tr.start - ONE_DAY, tr.end - ONE_DAY) _from = str(tr.start) _to = str(tr.end) # get the data goes = lc.GOESLightCurve.create(tr) # resample to reduce the number of points for debugging goes.data = goes.data.resample("1T").mean() # add time string for display of hover tool goes.data['time_str'] = goes.data.index.strftime('%F %H:%M:%S') source = ColumnDataSource(data=goes.data) source_static = ColumnDataSource(data=goes.data) # now create the bokeh plots # XRS-B Plot fig1 = figure(title="GOES", tools=TOOLS, plot_height=PLOT_HEIGHT, width=PLOT_WIDTH, x_axis_type='datetime', y_axis_type="log", y_range=(10**-9, 10**-2), toolbar_location="right") fig1.xaxis.formatter = formatter fig1.line('index', 'xrsb', source=source_static, color='red', line_width=2, legend="xrsa 1-8 Angstrom") fig2 = figure(title="GOES", tools=TOOLS, plot_height=PLOT_HEIGHT, width=PLOT_WIDTH, x_axis_type='datetime', y_axis_type="log", y_range=(10**-9, 10**-2)) fig2.xaxis.formatter = formatter fig2.line('index', 'xrsa', source=source_static, color='blue', line_width=2, legend="xrsa 0.5-4.0 Angstrom") # link the x-range for common panning fig2.x_range = fig1.x_range fig = Column(fig1, fig2) source_static.callback = CustomJS(code=""" var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; var m = 0; if (inds.length == 0) { return; } for (i = 0; i < inds.length; i++) { d1['color'][inds[i]] = "red" if (d1['y'][inds[i]] > m) { m = d1['y'][inds[i]] } } console.log(m); cb_obj.trigger('change'); """) hover = HoverTool() hover.tooltips = [("time", "@time_str"), ("xrsb", "@xrsb"), ("xrsa", "@xrsa")] fig1.add_tools(hover) hover2 = HoverTool() hover2.tooltips = [("time", "@time_str"), ("xrsb", "@xrsb"), ("xrsa", "@xrsa")] fig2.add_tools(hover2) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(fig, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, _from=_from, _to=_to, ) return encode_utf8(html)
def mann_only_interactive(data, cut1, cut2, chrs_plot=None, ms=6, color_sequence=['#7fc97f', "#beaed4", '#fdc086']): ''' Generate interactive dots. :param data: :param cut1: :param cut2: :param ms: Marker size. Default 6. :return: ''' # Defining DataFrame for bokeh ts = DataFrame({ 'snp': data['snp'], 'pos': data['pos'], 'chr': data['chr'], 'color': np.zeros(len(data), dtype='S20'), 'abspos': data['pos'], 'pval1': -np.log10(data['pval1']), 'pval1_q': -np.log10(data['pval1_q']), 'pval2': -np.log10(data['pval2']), 'pval2_q': -np.log10(data['pval2_q']) }) # Calculating proper positions if chrs_plot is None: chrs = np.unique(ts['chr']) if type(chrs[0]) == str: chrs = sorted_nicely(chrs) else: chrs.sort() else: chrs = chrs_plot print(chrs) temp_pos = 0 xtixks_pos = np.zeros(len(chrs) + 1) print(chrs) for i in range(len(chrs)): # Can be optimized here temp = ts['abspos'][ts['chr'] == chrs[i]] if len(temp) > 0: temp = np.max(temp) else: temp = 1000 print(temp) xtixks_pos[i + 1] = temp # temp_pos += temp # xtixks_pos[i+1] = temp_pos # ts['abspos'][ts['chr'] == chrs[i+1]] += temp_pos print(xtixks_pos) xtixks_pos = np.cumsum(xtixks_pos) print(xtixks_pos) for i in range(len(chrs)): ts['abspos'][ts['chr'] == chrs[i]] += xtixks_pos[i] print(xtixks_pos) xtixks_pos = (xtixks_pos[1:] + xtixks_pos[:-1]) / 2.0 print(xtixks_pos) print(chrs) # Old color selection # for i in range(len(chrs)): # if i % 2 == 0: # ts['color'][ts['chr'] == chrs[i]] = '#FA8072' # else: # ts['color'][ts['chr'] == chrs[i]] = '#00BFFF' for i in range(len(chrs)): ts['color'][ts['chr'] == chrs[i]] = color_sequence[i % len(color_sequence)] # Defining hover tools hover1 = HoverTool(tooltips=[ ("chr", "@chr"), ("snp", "@snp"), ("pos", "@pos"), ("-log10(pval1,pval2)", "(@pval1, @pval2)"), ]) hover2 = HoverTool(tooltips=[ ("chr", "@chr"), ("snp", "@snp"), ("pos", "@pos"), ("-log10(pval1,pval2)", "(@pval1, @pval2)"), ]) hoverq = HoverTool(tooltips=[ ("chr", "@chr"), ("snp", "@snp"), ("pos", "@pos"), ("-log10(pval1,pval2)", "(@pval1, @pval2)"), ]) tools1 = ['reset', 'xwheel_zoom', 'xpan', 'box_select', hover1] tools2 = ['reset', 'xwheel_zoom', 'xpan', 'box_select', hover2] toolsq = ['reset', 'wheel_zoom', 'pan', 'box_select', hoverq] source = ColumnDataSource(data=ts) # original_source = ColumnDataSource(data=ts) source_filt = ColumnDataSource( data=dict(snp=[], pos=[], pval1=[], pval2=[])) source.callback = CustomJS(args=dict(source_filt=source_filt), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = source_filt.get('data'); d2['snp'] = [] d2['pos'] = [] d2['chr'] = [] d2['pval1'] = [] d2['pval2'] = [] for (i = 0; i < inds.length; i++) { d2['snp'].push(d1['snp'][inds[i]]) d2['pos'].push(d1['pos'][inds[i]]) d2['chr'].push(d1['chr'][inds[i]]) d2['pval1'].push(d1['pval1'][inds[i]]) d2['pval2'].push(d1['pval2'][inds[i]]) } source_filt.trigger('change'); // data_table_filt.trigger('change'); """) selection_glyph = Circle(fill_color='firebrick', line_color=None, size=ms) nonselection_glyph = Circle(fill_color='gray', fill_alpha=0.1, line_color=None, size=ms) selection_glyph_2 = Square(fill_color='firebrick', line_color=None, size=ms) nonselection_glyph_2 = Square(fill_color='gray', fill_alpha=0.1, line_color=None, size=ms) upper_bound = np.ceil( np.max([np.max(ts['pval1']), np.max(ts['pval2'])]) + .51) p1 = figure(responsive=True, plot_width=900, plot_height=300, tools=tools1, x_range=[0, np.max(ts['abspos'])], y_range=[-0.12 * upper_bound, upper_bound], webgl=True) r1 = p1.circle('abspos', 'pval1', source=source, line_color=None, color='color', size=ms) r1.selection_glyph = selection_glyph r1.nonselection_glyph = nonselection_glyph p1.patch([0, np.max(ts['abspos']), np.max(ts['abspos']), 0], [0, 0, -np.log10(cut1), -np.log10(cut1)], alpha=0.5, line_color=None, fill_color='gray', line_width=2) p2 = figure(responsive=True, plot_width=900, plot_height=300, tools=tools2, x_range=p1.x_range, y_range=p1.y_range, webgl=True) r2 = p2.square('abspos', 'pval2', source=source, line_color=None, color='color', size=ms) r2.selection_glyph = selection_glyph_2 r2.nonselection_glyph = nonselection_glyph_2 p2.patch([0, np.max(ts['abspos']), np.max(ts['abspos']), 0], [0, 0, -np.log10(cut1), -np.log10(cut1)], alpha=0.5, line_color=None, fill_color='gray', line_width=2) pq1 = figure(responsive=True, plot_width=400, plot_height=400, tools=toolsq, webgl=True) pq1.line([0, 7], [0, 7], line_width=3, color="black", alpha=0.5, line_dash=[4, 4]) rq1 = pq1.circle('pval1_q', 'pval1', source=source, line_color=None, size=ms) # err_x = -np.log10(np.concatenate([data['pval1_q'][:100], data['pval1_q'][100::-1]])) # err_y = -np.log10(np.concatenate([data['pval1_q_top'][:100], data['pval1_q_bot'][100::-1]])) # er1 = pq1.patch(err_x, err_y, alpha=0.2, color='blue') rq2 = pq1.square('pval2_q', 'pval2', source=source, line_color=None, size=ms, color="red") # err_x = -np.log10(np.concatenate([data['pval2_q'][:100], data['pval2_q'][100::-1]])) # err_y = -np.log10(np.concatenate([data['pval2_q_top'][:100], data['pval2_q_bot'][100::-1]])) # er2 = pq1.patch(err_x, err_y, alpha=0.2, color='olive') rq1.selection_glyph = selection_glyph rq1.nonselection_glyph = nonselection_glyph rq2.selection_glyph = selection_glyph_2 rq2.nonselection_glyph = nonselection_glyph_2 # Labels for axes pq1.yaxis.axis_label = "Experimental quantiles, -log10(p)" pq1.xaxis.axis_label = "Theoretical quantiles, -log10(p)" p1.yaxis.axis_label = "-log10(p)" p1.xaxis.axis_label = "Chromosomes" p2.yaxis.axis_label = "-log10(p)" p2.xaxis.axis_label = "Chromosomes" p1.xgrid.grid_line_color = None p2.xgrid.grid_line_color = None # print(xtixks_pos) p1.xaxis[0].ticker = FixedTicker(ticks=[]) p2.xaxis[0].ticker = FixedTicker(ticks=[]) p1.text(xtixks_pos, xtixks_pos * 0 - 0.12 * upper_bound, [str(chrs[i]) for i in range(len(chrs))], text_align='center') p2.text(xtixks_pos, xtixks_pos * 0 - 0.12 * upper_bound, [str(chrs[i]) for i in range(len(chrs))], text_align='center') # p1.xaxis[0].ti columns = [ TableColumn(field="chr", title="chr"), TableColumn(field="snp", title="snp"), TableColumn(field="pos", title="pos"), TableColumn(field="pval1", title="pval1"), TableColumn(field="pval2", title="pval2"), ] data_table = DataTable(source=source, columns=columns, width=300, height=280) p3 = vform(data_table) data_table_filt = DataTable(source=source_filt, columns=columns, width=500, height=500) p4 = vform(data_table_filt) return p1, p2, p3, p4, pq1
def main(): output_file("testbokeh.html") # # x = [random() for x in range(500)] # # y = [random() for y in range(500)] # # # s1 = ColumnDataSource(data=dict(x=[], y=[])) # # p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here") # # p1.circle('x', 'y', source=s1, alpha=0.6) # source = ColumnDataSource(data=dict(x=[], y=[])) # p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1), # tools="", title="move your pointer") # # p2.circle('x', 'y', source=source, size = 8, alpha=0.6) # # # s2.callback = CustomJS(args=dict(s2=s2), code=""" # # var inds = cb_obj.get('selected')['1d'].indices; # # var d1 = cb_obj.get('data'); # # var d2 = s2.get('data'); # # d2['x'] = [] # # d2['y'] = [] # # for (i = 0; i < inds.length; i++) { # # d2['x'].push(d1['x'][inds[i]]) # # d2['y'].push(d1['y'][inds[i]]) # # } # # s2.trigger('change'); # # """) source.callback = CustomJS( args=dict(source=source), code=""" var data = source.get('data'); var index = cb_data['index']; var x = index['x'] var y = index['y'] data['x'].push(x) data['y'].push(y) source.trigger('change'); """, ) # layout = hplot(p1, p2) # p2.circle('x', 'y', source=source, size = 8, alpha=0.6) # show(p2) # x = xrange(0, 600) y = xrange(0, 300) # Basic plot setup p = figure(width=600, height=300, tools="hover", toolbar_location=None, title="Hover") # p. # p.line(x, y, line_dash="4 4", line_width=1, color='gray') # Add a circle, that is visible only when selected # source = ColumnDataSource({'x': [], 'y': []}) invisible_circle = Circle(x="x", y="y", fill_color="gray", fill_alpha=0.05, line_color=None, size=20) visible_circle = Circle(x="x", y="y", fill_color="firebrick", fill_alpha=0.5, line_color=None, size=20) # cr = p.add_glyph(source, invisible_circle, selection_glyph=visible_circle, nonselection_glyph=invisible_circle) cr = p.add_glyph(source, invisible_circle) # cr = p.circle('x','y',source = source,size = 8,alpha = 0.6) # Add a hover tool, that selects the circle code = "source.set('selected', cb_data['index']);" callback = CustomJS(args={"source": source}, code=code) p.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[cr], mode="mouse")) show(p)
def plot(): # Read data as if uploaded file is now used, Data set 1 data1 = read_csv('app/static/uploads/Data1.csv', sep=',', skipinitialspace=1) xdata1 = data1.values[:,1] # Extract Data ydata1 = data1.values[:,2] colour1 = ['black']*len(xdata1) # Read data as if uploaded file is now used, Data set 2 data2 = read_csv('app/static/uploads/Data2.csv', sep=',', skipinitialspace=1) xdata2 = data2.values[:,1] ydata2 = data2.values[:,2] colour2 = ['green']*len(xdata2) # Read data as if uploaded file is now used, Data set 3 data3 = read_csv('app/static/uploads/Data3.csv', sep=',', skipinitialspace=1) xdata3 = data3.values[:,1] ydata3 = data3.values[:,2] colour3 = ['red']*len(xdata3) # Prepare Data3 # xdata3 = linspace(0, 100, 10) # ydata3 = sin(10*xdata3) # Data_pandas = DataFrame({'Time': xdata3, # 'Value': ydata3}) # Data_pandas.to_csv('app/static/uploads/Data3.csv') # Assign read data to ColumnDataSource objects sourceData1 = ColumnDataSource(data=dict(x=xdata1, y=ydata1, color=colour1)) sourceData2 = ColumnDataSource(data=dict(x=xdata2, y=ydata2, color=colour2)) sourceData3 = ColumnDataSource(data=dict(x=xdata3, y=ydata3, color=colour3)) my_plot = figure(tools=[BoxSelectTool(dimensions=['width'], select_every_mousemove=True), PanTool(), ResetTool(), WheelZoomTool(), BoxZoomTool()], title='Time series data', x_range=(xdata1.min(), xdata1.max()), y_range=(ydata1.min(), ydata1.max()), width=1200) # Create figure object; DEBUG: select_every_mousemove=False my_plot.extra_y_ranges = {# 'Data1': Range1d(start=ydata1.min(), end=ydata1.max()), 'Data2': Range1d(start=ydata2.min(), end=ydata2.max()), 'Data3': Range1d(start=ydata3.min(), end=ydata3.max())} my_plot.circle(x='x', y='y', color='color', source=sourceData1, size=8, alpha=0.8, legend='Data 1') # Add circle elements (glyphs) to the figure my_plot.circle(x='x', y='y', color='color', source=sourceData2, size=5, alpha=0.8, y_range_name='Data2', legend='Data 2') my_plot.circle(x='x', y='y', color='color', source=sourceData3, size=8, alpha=0.5, y_range_name='Data3', legend='Data 3') my_plot.line(x='x', y='y', color='red', source= sourceData3, alpha=0.5, y_range_name='Data3', legend='Data 3') sourceFit = ColumnDataSource(data=dict(xfit=[], yfit=[])) my_plot.circle(x='xfit', y='yfit', source=sourceFit, color='orange', alpha=0.3) # my_plot.add_layout(LinearAxis(y_range_name='Data1', axis_line_color=colour1[0]), 'left') my_plot.add_layout(LinearAxis(y_range_name='Data2', axis_line_color=colour2[0], axis_line_width=3), 'left') my_plot.add_layout(LinearAxis(y_range_name='Data3', axis_line_color=colour3[0], axis_line_width=3), 'left') # sourceAnnotate = ColumnDataSource(data=dict(text=['Foo', 'Bah'], x=[50, 50], y=[0.5, 0], x_offset=[0,0], y_offset=[0,0], text_font_size=['15pt', '15pt'], # text_color=['orange', 'orange'])) # my_plot.text(source=sourceAnnotate, text='text', x='x', y='y', x_offset='x_offset', y_offset='y_offset', text_font_size='text_font_size', text_color='text_color') sourceData1.callback = CustomJS(args=dict(sourceFit=sourceFit), code=("""FirstOrderEyeball(cb_obj, sourceFit)""")) # sourceData.callback = CustomJS(args=dict(sourceFit=sourceFit, sourceAnnotate=sourceAnnotate), code=("""FirstOrderEyeball(cb_obj, sourceFit, sourceAnnotate)""")) script, div = components(my_plot) # Break figure up into component HTML to be added to template return render_template("int_scatter.html", myScript=script, myDiv=div)
def intplotter(data, isodata, nodata, y): #,hetclassintdf): linewidth = 1.5 source = ColumnDataSource(data) s2 = ColumnDataSource(data=dict(mz=data["mz"], Error=data["Error"], RA=data["RA"], Formula=data["Formula"], HeteroClass=data["HeteroClass"])) isosource = ColumnDataSource(isodata) nosource = ColumnDataSource(nodata) url = "http://www.chemspider.com/Search.aspx?q=@Formula" TOOLS = "crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select,hover" figdims = (900, 500) #pixel dimensions for the normal figures #msxlim = [200,700] #x limits in m/z for the mass spectra msxlim = [myround(min(data["mz"]), 100), myround(max(data["mz"]), 100)] vkxlim = [-0.02, round(max(data["OC"]), 1)] #vkxlim = [0,1] #vkylim = [0,2] vkylim = [round(min(data["HC"]), 1) - 0.1, round(max(data["HC"]), 1) + 0.1] p1 = figure(tools=TOOLS, title=y[:-5] + " - Van Krevelen", width=figdims[0], height=figdims[1], x_axis_label='O/C', y_axis_label='H/C', x_range=vkxlim, y_range=vkylim) color_mapper = LinearColorMapper(palette=glocmap, low=msxlim[0], high=msxlim[1]) p1.scatter(x='OC', y='HC', source=source, size='VKsize', fill_color={ 'field': 'mz', 'transform': color_mapper }, fill_alpha=0.75, line_color=None) #use size not radius. hover = p1.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('Formula', "@Formula"), ('Mass', "@mz{1.11111}"), ('Error (ppm)', "@Error{1.11}")]) taptool = p1.select(type=TapTool) taptool.callback = OpenURL(url=url) color_bar = ColorBar(color_mapper=color_mapper, title="m/z", border_line_color=None, location=(0, 0), scale_alpha=0.7) #orientation='horizontal',location='top_left', scale_alpha=0.7)#,ticker=FixedTicker(ticks=[2,6,10,14,18])) p1.add_layout(color_bar, "right") #dbexlim = [0,45] #dbeylim = [0,40] dbexlim = [0, max(data["C"])] dbeylim = [0, myround(max(data["DBE"]), 5)] cmax = myround(max(data["O"]), 5) #cmax = int(5 * round(float(cmax)/5)) p2 = figure(tools=TOOLS, title=y[:-5] + " - DBE vs C# Plot", width=figdims[0], height=figdims[1], x_axis_label='C#', y_axis_label='DBE', x_range=dbexlim, y_range=dbeylim) color_mapper2 = LinearColorMapper(palette=glocmap2, low=0, high=cmax) p2.scatter(x='C', y='DBE', source=source, size='VKsize', fill_color={ 'field': 'O', 'transform': color_mapper2 }, fill_alpha=0.75, line_color=None) hover = p2.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('Formula', "@Formula"), ('Mass', "@mz{1.11111}"), ('Error (ppm)', "@Error{1.11}")]) taptool = p2.select(type=TapTool) taptool.callback = OpenURL(url=url) color_bar2 = ColorBar( color_mapper=color_mapper2, title="O#", border_line_color=None, location=(0, 0), scale_alpha=0.7, ticker=FixedTicker( ticks=[0, int(cmax / 4), int(cmax / 2), int(3 * cmax / 4), cmax])) p2.add_layout(color_bar2, "right") #aixlim=[0,45] aiylim = [0, 1] aixlim = [0, max(data["C"])] #aiylim = [0,myround(max(data["AImod"]),5)] p3 = figure(tools=TOOLS, title=y[:-5] + " - AI(mod) vs C# Plot", width=figdims[0], height=figdims[1], x_axis_label='C#', y_axis_label='AI(mod)', x_range=aixlim, y_range=aiylim) color_mapper3 = LinearColorMapper(palette=glocmap2, low=0, high=cmax) p3.scatter(x='C', y='AImod', source=source, size='VKsize', fill_color={ 'field': 'O', 'transform': color_mapper3 }, fill_alpha=0.75, line_color=None) hover = p3.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('Formula', "@Formula"), ('Mass', "@mz{1.11111}"), ('Error (ppm)', "@Error{1.11}")]) taptool = p3.select(type=TapTool) taptool.callback = OpenURL(url=url) color_bar3 = ColorBar( color_mapper=color_mapper3, title="O#", border_line_color=None, location=(0, 0), scale_alpha=0.7, ticker=FixedTicker( ticks=[0, int(cmax / 4), int(cmax / 2), int(3 * cmax / 4), cmax])) #orientation='horizontal',location='top_left', scale_alpha=0.7)#,ticker=FixedTicker(ticks=[2,6,10,14,18])) p3.add_layout(color_bar3, "right") p4 = figure(tools=TOOLS, title=y[:-5] + " - Assigned Centroid MS", width=figdims[0], height=figdims[1], x_axis_label='m/z', y_axis_label='Abundance', y_range=[min(data["RA"]), max(data["RA"])], x_range=msxlim) p4.segment(x0=0, x1=800, y0=0, y1=0, line_width=1, line_color="black") p4.segment(x0='mz', y0=0, x1='mz', y1='RA', source=source, line_width=linewidth, line_color="black") p4.scatter(x='mz', y='RA', source=source, fill_color='black', line_color=None) hover = p4.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('Formula', "@Formula"), ('Mass', "@mz{1.11111}"), ('Error (ppm)', "@Error{1.11}")]) taptool = p4.select(type=TapTool) taptool.callback = OpenURL(url=url) p4.yaxis[0].formatter = PrintfTickFormatter(format="%4.1e") """ #this is me trying to plot a barplot of heteroatomic class distributions... p7 = figure(tools=TOOLS, title=y[:-9]+"",width=800, height=600, x_axis_label='HeteroClass',y_axis_label='Count',webgl=True) p7.quad(left="HetClassInts",y=hetclassdf[0],source=source,width=5,height=) t7 = layouts.Column(hist) tab7 = Panel(child=t7,title="test") """ stretch = msxlim[0] * 0.1 p5 = figure(tools=TOOLS, title=y[:-5] + " - Assigned Centroid MS", width=1400, height=600, x_axis_label='m/z', y_axis_label='Abundance', y_range=[min(data["RA"]), max(data["RA"])], x_range=(msxlim[0] - stretch, msxlim[1] + stretch)) p5.segment(x0=0, x1=800, y0=0, y1=0, line_width=1, line_color="black") no1 = p5.segment(x0='mz', y0=0, x1='mz', y1='RA', source=nosource, line_width=linewidth, line_color="red") no2 = p5.scatter(x='mz', y='RA', source=nosource, fill_color='red', line_color=None, legend="Unassigned Peaks") p5.scatter(x='mz', y='RA', source=source, fill_color='black', line_color=None, legend="Assigned Peaks") p5.segment(x0='mz', y0=0, x1='mz', y1='RA', source=source, line_width=linewidth, line_color="black") iso1 = p5.segment(x0='mz', y0=0, x1='mz', y1='RA', source=isosource, line_width=linewidth, line_color="green") iso2 = p5.scatter(x='mz', y='RA', source=isosource, fill_color='green', line_color=None, legend="Isotologue Peaks") hover = p5.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('Formula', "@Formula"), ('Mass', "@mz{1.11111}"), ('Error (ppm)', "@Error{1.11}")]) taptool = p5.select(type=TapTool) taptool.callback = OpenURL(url=url) p5.yaxis[0].formatter = PrintfTickFormatter(format="%4.1e") js_code1 = "iso1.glyph.visible = false; iso2.glyph.visible = false; no1.glyph.visible = false; no2.glyph.visible = false;" cb1 = CustomJS(code=js_code1, args=dict(iso1=iso1, iso2=iso2, no1=no1, no2=no2)) js_code2 = "iso1.glyph.visible = true; iso2.glyph.visible = true; no1.glyph.visible = true; no2.glyph.visible = true;" cb2 = CustomJS(code=js_code2, args=dict(iso1=iso1, iso2=iso2, no1=no1, no2=no2)) toggleOn = Button(label="Hide", button_type="success", callback=cb1) toggleOff = Button(label="Show", button_type="success", callback=cb2) top = layouts.Row(toggleOn, toggleOff) t3 = layouts.Column(top, p5) tab3 = Panel(child=t3, title="Centroid MS with Isotopomers and No Hits") downloadbutton = Button(label="Download", button_type="success") downloadbutton.callback = CustomJS(args=dict(s2=s2), code=""" var data = s2.get('data'); var filetext = 'mz,Error,RA,Formula,HeteroClass\\n'; for (i=0; i < data['mz'].length; i++) { var currRow = [data['mz'][i].toString(), data['Error'][i].toString(), data['RA'][i].toString(), data['Formula'][i].toString(), data['HeteroClass'][i].toString().concat('\\n')]; var joined = currRow.join(); filetext = filetext.concat(joined); } var filename = 'data_result.csv'; var blob = new Blob([filetext], { type: 'text/csv;charset=utf-8;' }); //addresses IE if (navigator.msSaveBlob) { navigator.msSaveBlob(blob, filename); } else { var link = document.createElement("a"); link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = filename; link.target = "_blank"; link.style.visibility = 'hidden'; link.dispatchEvent(new MouseEvent('click')) } """) columns = [ TableColumn(field="mz", title="m/z", formatter=NumberFormatter(format="0.00000")), TableColumn(field="Error", title="Error (ppm)", formatter=NumberFormatter(format="0.00")), TableColumn(field="RA", title="Abundance"), TableColumn(field="Formula", title="Formula"), TableColumn(field="HeteroClass", title="Heteroatomic Class") ] data_table = DataTable(source=s2, columns=columns, width=1400, row_headers=False, fit_columns=True) t4 = layouts.Column(data_table, downloadbutton) tab4 = Panel(child=t4, title="Selected Data Table") source.callback = CustomJS(args=dict(s2=s2, dt=data_table), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = s2.get('data'); if (inds.length == 0) { d2['mz'] = d1['mz'] d2['Error'] = d1['Error'] d2['RA'] = d1['RA'] d2['Formula'] = d1['Formula'] d2['HeteroClass'] = d1['HeteroClass'] } else if (inds.length != 0) { d2['mz'] = [] d2['Error'] = [] d2['RA'] = [] d2['Formula'] = [] d2['HeteroClass'] = [] for (i = 0; i < inds.length; i++) { d2['mz'].push(d1['mz'][inds[i]]) d2['Error'].push(d1['Error'][inds[i]]) d2['RA'].push(d1['RA'][inds[i]]) d2['Formula'].push(d1['Formula'][inds[i]]) d2['HeteroClass'].push(d1['HeteroClass'][inds[i]]) } } s2.trigger('change'); dt.trigger('change'); """) """ hetclasslist = hetclassintdf["HetClass"].tolist() hetclasslistnew = [] for x in hetclasslist: hetclasslistnew.append([x,x]) dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=hetclasslistnew) """ t1 = layouts.Row(p1, p4) t2 = layouts.Row(p2, p3) t12 = layouts.Column(t1, t2) tab1 = Panel(child=t12, title="Main") tabs = Tabs(tabs=[tab1, tab3, tab4]) for figs in [p1, p2, p3, p4, p5]: figs.xaxis.axis_label_text_font_size = "14pt" figs.xaxis.major_label_text_font_size = "14pt" figs.yaxis.axis_label_text_font_size = "14pt" figs.yaxis.major_label_text_font_size = "14pt" figs.title.text_font_size = "14pt" figs.toolbar_location = "above" with open(outputpath + 'templates/index.html', 'r') as f: template = Template(f.read()) #js_resources = JSResources(mode='inline') html = file_html(tabs, (CDN, CDN), "Interactive Van Krevelen Diagrams", template=template) output_file2 = outputpath + y[:-5] + '-plot.html' with open(output_file2, 'w') as f: f.write(html) view(output_file2)
s1.callback = CustomJS(args=dict(s2=s2), code=""" console.log(cb_obj); var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; var d2 = s2.data; d2['full_artists'] = []; d2['full_counts'] = []; var max_freq = 0; for (i=0; i<inds.length; i++){ var current = d1['arts'][inds[i]]; if (d2['full_artists'].indexOf(current) == -1){ d2['full_artists'].push(d1['arts'][inds[i]]); d2['full_counts'].push(1); } else{ d2['full_counts'][d2['full_artists'].indexOf(current)] += 1; if (d2['full_counts'][d2['full_artists'].indexOf(current)] > max_freq){ max_freq = d2['full_counts'][d2['full_artists'].indexOf(current)]; } } } console.log(max_freq); d2['artists'] = []; d2['counts'] = []; var thres = max_freq * 0.05; //filter arrays to only include freqs >= 5pcnt of max_freq for (i=0; i<d2['full_artists'].length; i++){ if (d2['full_counts'][i] >= thres){ d2['artists'].push(d2['full_artists'][i]); d2['counts'].push(d2['full_counts'][i]); } } s2.change.emit(); if (inds.length > 5){ if (document.getElementById("right_side_style")==null){ var css = ".right_side div {\\n\\tfont: 12px sans-serif;\\n"; css = css.concat("\\tbackground-color: white;\\n\\tpadding: 3px;\\n\\tcolor: white;}"); var style = document.createElement('style'); style.type = 'text/css'; style.id = "right_side_style"; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); } if (document.getElementById("chart_style")==null){ var css = ".chart div {\\n\\tfont: 12px sans-serif;\\n"; css = css.concat("\\tbackground-color: steelblue;\\n\\topacity: 0.8;\\n\\theight: 14px;\\n\\tmargin: 1px;\\n\\twidth: 470px;\\n\\ttext-align: right;\\n\\tcolor: white;}"); var style = document.createElement('style'); style.type = 'text/css'; style.id = "chart_style"; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); } if (document.getElementById("artist_style")==null){ var css = ".artists div {\\n\\tfont: 12px sans-serif;\\n"; css = css.concat("\\tbackground-color: white;\\n\\theight: 14px;\\n\\tmargin: 1px;\\n\\twidth: 470px;\\n\\ttext-align: right;\\n\\tcolor: black;}"); var style = document.createElement('style'); style.type = 'text/css'; style.id = "artist_style"; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); } if (document.getElementById("right_side_div")==null){ var rightdiv = document.createElement('div'); rightdiv.className = "right_side"; rightdiv.style = "float: left; tdisplay: inline-flex; width: 970px;"; rightdiv.id = "right_side_div"; document.getElementsByClassName("bk-spacer-box bk-layout-fixed")[0].innerHTML = ""; document.getElementsByClassName("bk-spacer-box bk-layout-fixed")[0].style = "width: 970px"; document.getElementsByClassName("bk-spacer-box bk-layout-fixed")[0].appendChild(rightdiv); } else{ document.getElementsByClassName("bk-spacer-box bk-layout-fixed")[0].style = "width: 970px"; document.getElementById("right_side_div").innerHTML = ""; } if (document.getElementById("title_p")==null){ var para = document.createElement("p"); var node = document.createTextNode("Artist Frequencies"); para.className = "title"; para.style = "text-align: center; font-weight: bold; font-size: 15px;"; para.id = "title_p"; para.appendChild(node); document.getElementsByClassName("right_side")[0].appendChild(para); } else{ document.getElementById("artists_div").innerHTML = ""; } if (document.getElementById("artists_div")==null){ var artdiv = document.createElement('div'); artdiv.className = "artists"; artdiv.style = "float: left; tdisplay: inline-flex;"; artdiv.id = "artists_div"; document.getElementsByClassName("right_side")[0].appendChild(artdiv); } else{ document.getElementById("artists_div").innerHTML = ""; } if (document.getElementById("chart_div")==null){ var chartdiv = document.createElement('div'); chartdiv.className = "chart"; chartdiv.style = "float: right; tdisplay: inline-flex;"; chartdiv.id = "chart_div"; document.getElementsByClassName("right_side")[0].appendChild(chartdiv); } else{ document.getElementById("chart_div").innerHTML = ""; } if (document.getElementById("source_d3js")==null){ var d3js = document.createElement('script'); d3js.src = "https://d3js.org/d3.v3.min.js"; d3js.id = "source_d3js"; document.body.appendChild(d3js); } if (document.getElementById("mycode")==null){ var code_div = document.createElement('script'); code_div.id = "mycode"; document.body.appendChild(code_div); } //populate var data with d2["counts"] and var art_names with d2["artists"] var string = "["; for (j=0; j<d2['counts'].length-1; j++){ var tmp = d2['counts'][j]; string = string.concat(tmp); string += ", "; } var tmp = d2['counts'][d2['counts'].length-1]; string = string.concat(tmp); string += "]"; var string1 = "[\\""; for (j=0; j<d2['artists'].length-1; j++){ var tmp = d2['artists'][j]; string1 = string1.concat(tmp); string1 = string1.concat("\\"") string1 += ", \\""; } var tmp = d2['artists'][d2['artists'].length-1]; string1 = string1.concat(tmp); string1 += "\\"]"; var d3js_code = "var data = "; d3js_code = d3js_code.concat(string); d3js_code = d3js_code + ";\\n\\n"; d3js_code = d3js_code+ "var art_names = "; d3js_code = d3js_code.concat(string1); d3js_code = d3js_code + ";\\n\\n"; d3js_code = d3js_code.concat("var x = d3.scale.linear()\\n .domain([0, d3.max(data)])\\n .range([0, 470]);\\n\\n"); // var x = d3.scale.linear() // .domain([0, d3.max(data)]) // .range([0, 420]); d3js_code = d3js_code.concat("d3.select(\\".chart\\")\\n .selectAll(\\"div\\")\\n "+ ".data(data)\\n .enter().append(\\"div\\")\\n "+ ".style(\\"width\\", function(d) { return x(d) + \\"px\\"; })\\n .text(function(d) { return d; });"); // d3.select(".chart") // .selectAll("div") // .data(data) // .enter().append("div") // .style("width", function(d) { return x(d) + "px"; }) // .style("margin", "auto 5px") // .text(function(d) { return d; });"; // .class("chartel"); d3js_code = d3js_code.concat("\\n\\nd3.select(\\".artists\\")\\n .selectAll(\\"div\\")\\n "+ ".data(art_names)\\n .enter().append(\\"div\\")\\n "+ ".style(\\"width\\", \\"300\\")\\n .text(function(d) { return d; });"); // d3.select(".artists") // .selectAll("div") // .data(art_names) // .enter().append("div") // .style("width", "300") // .style("margin", "auto 5px") // .text(function(d) { return d; });"; // .class("artel"); document.getElementById("mycode").innerHTML = ""; document.getElementById("mycode").appendChild(document.createTextNode(d3js_code)); var script = document.getElementById("mycode"); eval(script.innerHTML); // Check if chart and script exist // if not, create them // if they are, change innerhtml for script // delete nodes from char and repopulate with new data } """)
def bokeh_plot(self): chosen_bic = literal_eval(self.get_argument('subject')) # prep n_cols = len(chosen_bic['feats']) n_lines = len(chosen_bic['objs']) bic_mtrx = np.zeros((n_lines,n_cols), dtype = object) objs = sorted(chosen_bic['objs']) feats = sorted(chosen_bic['feats']) # bokeh objlist = [] featlist = [] # colormap = ["#444444", "#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a"] color = [] i = 0 for obj in objs: j = 0 for feat in feats: objlist.append(obj) featlist.append(feat) color.append("#669999") #color.append(np.random.choice(colormap)) bic_mtrx[i, j] = (obj, feat) j+=1 i+=1 # ColumnDataSource Object source = ColumnDataSource(data=dict(xname=objlist, yname=featlist, colors=color, count=bic_mtrx.flatten())) source.callback = Callback(args=dict(source=source), code=""" var data = source.get('data'); var f = source.get('value'); object_p = data['xname']; attribute_p = data['yname']; document.write(f); """) p = figure(title="Matriz do Co-Grupo",x_axis_location="above", tools="resize, previewsave, reset, hover", x_range=list(objs), y_range=list(reversed(feats)), plot_width=n_lines*100, plot_height=n_cols*40, toolbar_location="left") p.rect('xname', 'yname', 1, 1, source=source, color='colors', line_color="#000000") p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = "#000000" p.axis.major_label_text_font_size = "10pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = np.pi/2.5 p.border_fill = "#FFFFF0" tap = TapTool(plot=p) # tap = TapTool(plot=p, action=OpenURL(url="http://www.ufabc.edu.br")) p.tools.append(tap) hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('Tupla', '@yname, @xname')]) # hover = HoverTool(plot=p, tooltips=[('Tupla', '@yname, @xname')]) # p.tools.append(hover) # script & div tags script, div = components(p, CDN) # return to html return (script, div)
def Make_Plot(ddata,odata,imgo,imgd,oplayer,dplayer): ### function the does the actual plotting sd1 = ColumnDataSource(ddata) sd2 = ColumnDataSource(ddata) so1 = ColumnDataSource(odata) so2 = ColumnDataSource(odata) o_sum = ColumnDataSource(get_start_values(odata)) d_sum = ColumnDataSource(get_start_values(ddata)) columns = [ TableColumn(field="attributes", title="attributes"), TableColumn(field="values", title="values") ] odata_table = DataTable(source=o_sum, columns=columns, width=400, height=280) ddata_table = DataTable(source=d_sum, columns=columns, width=400, height=280) #output_file("wiggins_shot_charts.html", title="Gettin' Wiggy Wit it") defense = dplayer + " - Defensive Value Added Chart" offense = oplayer + " - Offensive Value Added Chart" op = figure(tools="box_select", title=offense) dp = figure(tools="box_select", title=defense) #p1.image_rgba(image=[img1],x=[-31], y=[-17.5], dw=[62], dh=[117.5])# use with tWolves court op.image_rgba(image=[imgo],x=[-25], y=[-41], dw=[50], dh=[47]) dp.image_rgba(image=[imgd],x=[-25], y=[-6], dw=[50], dh=[47]) op.scatter(x='x',y= 'y',size="dense", fill_color='colors', source=so1, alpha=1,marker="square", line_color=None,width=1000,height=1000) dp.scatter(x='x',y= 'y',size="dense", fill_color='colors', source=sd1, alpha=1,marker="square", line_color=None,width=1000,height=1000) o_sum.callback = Callback(args=dict(so2=so2,so1=so1,o_sum=o_sum), code=""" var attempts = 0; var makes = 0; var value = 0; var fg_per = 0; var prob = 0; var value = 0; var pps = 0; var worth = 0; var close = 0; var shot_clock = 0; var distance = 0; var touch_time = 0; var dribbles = 0; var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = so2.get('data'); var o_sum = o_sum.get('data'); for (i = 0; i < inds.length; i++) { attempts += d1['attempts'][inds[i]] makes += d1['makes'][inds[i]] value += d1['value'][inds[i]] prob += d1['attempts'][inds[i]]*d2['prob'][inds[i]] value += d1['attempts'][inds[i]]*d2['value'][inds[i]] pps += d1['attempts'][inds[i]]*d2['pts'][inds[i]] worth += d1['attempts'][inds[i]]*d2['worth'][inds[i]] close += d1['attempts'][inds[i]]*d2['close'][inds[i]] shot_clock += d1['attempts'][inds[i]]*d2['shot_clock'][inds[i]] distance += d1['attempts'][inds[i]]*d2['distance'][inds[i]] touch_time += d1['attempts'][inds[i]]*d2['touch_time'][inds[i]] dribbles += d1['attempts'][inds[i]]*d2['dribbles'][inds[i]] } o_sum['values'] = [attempts,makes,makes/attempts,prob/attempts,pps/attempts,worth/attempts,value/attempts,close/attempts,shot_clock/attempts,distance/attempts,touch_time/attempts,dribbles/attempts]; o_sum.trigger('change') """) so1.callback = Callback(args=dict(so2=so2,so1=so1,o_sum=o_sum), code=""" var attempts = 0; var makes = 0; var value = 0; var fg_per = 0; var prob = 0; var value = 0; var pps = 0; var worth = 0; var close = 0; var shot_clock = 0; var distance = 0; var touch_time = 0; var dribbles = 0; var palette = ["#053061", "#2166ac", "#4393c3", "#92c5de", "#d1e5f0", "#f7f7f7", "#fddbc7", "#f4a582", "#d6604d", "#b2182b", "#67001f"] var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = so2.get('data'); var d3 = o_sum.get('data'); for (i = 0; i < inds.length; i++) { attempts += d1['attempts'][inds[i]] makes += d1['makes'][inds[i]] value += d1['value'][inds[i]] prob += d1['attempts'][inds[i]]*d2['prob'][inds[i]] value += d1['attempts'][inds[i]]*d2['value'][inds[i]] pps += d1['attempts'][inds[i]]*d2['pts'][inds[i]] worth += d1['attempts'][inds[i]]*d2['worth'][inds[i]] close += d1['attempts'][inds[i]]*d2['close'][inds[i]] shot_clock += d1['attempts'][inds[i]]*d2['shot_clock'][inds[i]] distance += d1['attempts'][inds[i]]*d2['distance'][inds[i]] touch_time += d1['attempts'][inds[i]]*d2['touch_time'][inds[i]] dribbles += d1['attempts'][inds[i]]*d2['dribbles'][inds[i]] } for (i = 0; i < d1['x'].length; i++) { d1['attempts'][i] = d2['attempts'][i] d1['makes'][i] = d2['makes'][i] d1['colors'][i] = d2['colors'][i] d1['value'][i] = d2['value'][i] d1['fg_percent'][i] = d2['fg_percent'][i] d1['prob'][i] = d2['prob'][i] d1['dense'][i] = d2['dense'][i] d1['value'][i] = d2['value'][i] d1['pts'][i] = d2['pts'][i] d1['worth'][i] = d2['worth'][i] d1['close'][i] = d2['close'][i] d1['shot_clock'][i] = d2['shot_clock'][i] d1['distance'][i] = d2['distance'][i] d1['touch_time'][i] = d2['touch_time'][i] d1['dribbles'][i] = d2['dribbles'][i] } value = value/attempts var color_index = Math.round(value*8)+5; color_index = Math.max(color_index,0) color_index = Math.min(color_index,10) var color = palette[color_index]; for (i = 0; i < inds.length; i++) { d1['attempts'][inds[i]] = attempts d1['makes'][inds[i]] = makes d1['colors'][inds[i]] = color d1['value'][inds[i]] = value d1['fg_percent'][inds[i]] = makes/attempts d1['prob'][inds[i]] = prob/attempts d1['dense'][inds[i]] = 11 d1['value'][inds[i]] = value/attempts d1['pts'][inds[i]] = pps/attempts d1['worth'][inds[i]] = worth/attempts d1['close'][inds[i]] = close/attempts d1['shot_clock'][inds[i]] = shot_clock/attempts d1['distance'][inds[i]] = distance/attempts d1['touch_time'][inds[i]] = touch_time/attempts d1['dribbles'][inds[i]] = dribbles/attempts } d3['values'] = [attempts,makes,makes/attempts,prob/attempts,pps/attempts,worth/attempts,value/attempts,close/attempts,shot_clock/attempts,distance/attempts,touch_time/attempts,dribbles/attempts] o_sum.trigger('change') """) sd1.callback = Callback(args=dict(sd2=sd2,sd1=sd1), code=""" var attempts = 0; var value = 0; var palette = ["#053061", "#2166ac", "#4393c3", "#92c5de", "#d1e5f0", "#f7f7f7", "#fddbc7", "#f4a582", "#d6604d", "#b2182b", "#67001f"] var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = sd2.get('data'); for (i = 0; i < inds.length; i++) { attempts += d2['attempts'][inds[i]] value += d2['attempts'][inds[i]]*d2['value'][inds[i]] } for (i = 0; i < d1['x'].length; i++) { d1['attempts'][i] = d2['attempts'][i] d1['colors'][i] = d2['colors'][i] d1['value'][i] = d2['value'][i] } value = value/attempts var color_index = Math.round(value*8)+5; color_index = Math.max(color_index,0) color_index = Math.min(color_index,10) var color = palette[color_index]; for (i = 0; i < inds.length; i++) { d1['attempts'][inds[i]] = attempts d1['colors'][inds[i]] = color d1['value'][inds[i]] = value/attempts } """) op.x_range = Range1d(-25, 25) dp.x_range = Range1d(-25, 25) op.y_range = Range1d(-41, 6) dp.y_range = Range1d(-6, 41) #layout = hplot(vform(op,odata_table), vform(dp, ddata_table)) #show(layout) return op, odata_table, dp, ddata_table
tools="lasso_select", title="Select Here") p.circle('x', 'y', color='color', size=8, source=s, alpha=0.4) s2 = ColumnDataSource(data=dict(ym=[0.5, 0.5])) p.line(x=[0, 1], y='ym', color="orange", line_width=5, alpha=0.6, source=s2) s.callback = Callback(args=dict(s2=s2), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d = cb_obj.get('data'); var ym = 0 if (inds.length == 0) { return; } for (i = 0; i < d['color'].length; i++) { d['color'][i] = "navy" } for (i = 0; i < inds.length; i++) { d['color'][inds[i]] = "firebrick" ym += d['y'][inds[i]] } ym /= inds.length s2.get('data')['ym'] = [ym, ym] cb_obj.trigger('change'); s2.trigger('change'); """) show(p)
source_code = """ var inds = cb_obj.selected['1d'].indices; checkbox.active = inds; checkbox.change.emit() """ checkbox_code = """ source.selected['1d'].indices = cb_obj.active; """ button_code = """ console.log('checkbox',checkbox.active); console.log('source',source.selected['1d'].indices); """ source.callback = CustomJS(args=dict(checkbox=checkbox), code=source_code) checkbox.callback = CustomJS(args=dict(table=data_table, source=source), code=checkbox_code) button.callback = CustomJS(args=dict(table=data_table, checkbox=checkbox, source=source), code=button_code) show(widgetbox(data_table, checkbox, button))
def mann_only_interactive(data, cut1, cut2, chrs_plot=None): ''' Generate interactive dots. :param data: :param cut1: :param cut2: :return: ''' # Defining DataFrame for bokeh ts = DataFrame({'snp': data['snp'], 'pos': data['pos'], 'chr': data['chr'], 'color': np.zeros(len(data), dtype='S20'), 'abspos': data['pos'], 'pval1': -np.log10(data['pval1']), 'pval1_q': -np.log10(data['pval1_q']), 'pval2': -np.log10(data['pval2']), 'pval2_q': -np.log10(data['pval2_q'])}) # Calculating proper positions if chrs_plot is None: chrs = np.unique(ts['chr']) if type(chrs[0]) == str: chrs = sorted_nicely(chrs) else: chrs.sort() else: chrs = chrs_plot print(chrs) temp_pos = 0 xtixks_pos = np.zeros(len(chrs)+1) print(chrs) for i in range(len(chrs)): # Can be optimized here temp = ts['abspos'][ts['chr'] == chrs[i]] if len(temp) > 0: temp = np.max(temp) else: temp = 1000 print(temp) xtixks_pos[i+1] = temp # temp_pos += temp # xtixks_pos[i+1] = temp_pos # ts['abspos'][ts['chr'] == chrs[i+1]] += temp_pos print(xtixks_pos) xtixks_pos = np.cumsum(xtixks_pos) print(xtixks_pos) for i in range(len(chrs)): ts['abspos'][ts['chr'] == chrs[i]] += xtixks_pos[i] print(xtixks_pos) xtixks_pos = (xtixks_pos[1:] + xtixks_pos[:-1])/2.0 print(xtixks_pos) print(chrs) for i in range(len(chrs)): if i % 2 == 0: ts['color'][ts['chr'] == chrs[i]] = '#FA8072' else: ts['color'][ts['chr'] == chrs[i]] = '#00BFFF' # Defining hover tools hover1 = HoverTool( tooltips=[ ("chr", "@chr"), ("snp", "@snp"), ("pos", "@pos"), ("-log10(pval1,pval2)", "(@pval1, @pval2)"), ] ) hover2 = HoverTool( tooltips=[ ("chr", "@chr"), ("snp", "@snp"), ("pos", "@pos"), ("-log10(pval1,pval2)", "(@pval1, @pval2)"), ] ) hoverq = HoverTool( tooltips=[ ("chr", "@chr"), ("snp", "@snp"), ("pos", "@pos"), ("-log10(pval1,pval2)", "(@pval1, @pval2)"), ] ) tools1 = ['reset', 'xwheel_zoom', 'xpan', 'box_select', hover1] tools2 = ['reset', 'xwheel_zoom', 'xpan', 'box_select', hover2] toolsq = ['reset', 'wheel_zoom', 'pan', 'box_select', hoverq] source = ColumnDataSource(data=ts) # original_source = ColumnDataSource(data=ts) source_filt = ColumnDataSource(data=dict(snp=[], pos=[], pval1=[], pval2=[])) source.callback = CustomJS(args=dict(source_filt=source_filt), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = source_filt.get('data'); d2['snp'] = [] d2['pos'] = [] d2['chr'] = [] d2['pval1'] = [] d2['pval2'] = [] for (i = 0; i < inds.length; i++) { d2['snp'].push(d1['snp'][inds[i]]) d2['pos'].push(d1['pos'][inds[i]]) d2['chr'].push(d1['chr'][inds[i]]) d2['pval1'].push(d1['pval1'][inds[i]]) d2['pval2'].push(d1['pval2'][inds[i]]) } source_filt.trigger('change'); // data_table_filt.trigger('change'); """) selection_glyph = Circle(fill_color='firebrick', line_color=None, size=6) nonselection_glyph = Circle(fill_color='gray', fill_alpha=0.1, line_color=None, size=6) selection_glyph_2 = Square(fill_color='firebrick', line_color=None, size=6) nonselection_glyph_2 = Square(fill_color='gray', fill_alpha=0.1, line_color=None, size=6) upper_bound = np.ceil(np.max([np.max(ts['pval1']), np.max(ts['pval2'])]) + .51) p1 = figure(responsive=True, plot_width=900, plot_height=300, tools=tools1, x_range=[0, np.max(ts['abspos'])], y_range=[-0.12*upper_bound, upper_bound], webgl=True) r1 = p1.circle('abspos', 'pval1', source=source, line_color=None, color='color', size=6) r1.selection_glyph = selection_glyph r1.nonselection_glyph = nonselection_glyph p1.patch([0, np.max(ts['abspos']), np.max(ts['abspos']), 0], [0, 0, -np.log10(cut1), -np.log10(cut1)], alpha=0.5, line_color=None, fill_color='gray', line_width=2) p2 = figure(responsive=True, plot_width=900, plot_height=300, tools=tools2, x_range=p1.x_range, y_range=p1.y_range, webgl=True) r2 = p2.square('abspos', 'pval2', source=source, line_color=None, color='color', size=6) r2.selection_glyph = selection_glyph_2 r2.nonselection_glyph = nonselection_glyph_2 p2.patch([0, np.max(ts['abspos']), np.max(ts['abspos']), 0], [0, 0, -np.log10(cut1), -np.log10(cut1)], alpha=0.5, line_color=None, fill_color='gray', line_width=2) pq1 = figure(responsive=True, plot_width=400, plot_height=400, tools=toolsq, webgl=True) pq1.line([0, 7], [0, 7], line_width=3, color="black", alpha=0.5, line_dash=[4, 4]) rq1 = pq1.circle('pval1_q', 'pval1', source=source, line_color=None, size=10) # err_x = -np.log10(np.concatenate([data['pval1_q'][:100], data['pval1_q'][100::-1]])) # err_y = -np.log10(np.concatenate([data['pval1_q_top'][:100], data['pval1_q_bot'][100::-1]])) # er1 = pq1.patch(err_x, err_y, alpha=0.2, color='blue') rq2 = pq1.square('pval2_q', 'pval2', source=source, line_color=None, size=10, color="olive") # err_x = -np.log10(np.concatenate([data['pval2_q'][:100], data['pval2_q'][100::-1]])) # err_y = -np.log10(np.concatenate([data['pval2_q_top'][:100], data['pval2_q_bot'][100::-1]])) # er2 = pq1.patch(err_x, err_y, alpha=0.2, color='olive') rq1.selection_glyph = selection_glyph rq1.nonselection_glyph = nonselection_glyph rq2.selection_glyph = selection_glyph_2 rq2.nonselection_glyph = nonselection_glyph_2 # Labels for axes pq1.yaxis.axis_label = "Experimental quantiles, -log10(p)" pq1.xaxis.axis_label = "Theoretical quantiles, -log10(p)" p1.yaxis.axis_label = "-log10(p)" p1.xaxis.axis_label = "Chromosomes" p2.yaxis.axis_label = "-log10(p)" p2.xaxis.axis_label = "Chromosomes" p1.xgrid.grid_line_color = None p2.xgrid.grid_line_color = None # print(xtixks_pos) p1.xaxis[0].ticker = FixedTicker(ticks=[]) p2.xaxis[0].ticker = FixedTicker(ticks=[]) p1.text(xtixks_pos,xtixks_pos*0-0.12*upper_bound, [str(chrs[i]) for i in range(len(chrs))], text_align='center') p2.text(xtixks_pos,xtixks_pos*0-0.12*upper_bound, [str(chrs[i]) for i in range(len(chrs))], text_align='center') # p1.xaxis[0].ti columns = [ TableColumn(field="chr", title="chr"), TableColumn(field="snp", title="snp"), TableColumn(field="pos", title="pos"), TableColumn(field="pval1", title="pval1"), TableColumn(field="pval2", title="pval2"), ] data_table = DataTable(source=source, columns=columns, width=300, height=280) p3 = vform(data_table) data_table_filt = DataTable(source=source_filt, columns=columns, width=500, height=500) p4 = vform(data_table_filt) return p1,p2,p3,p4,pq1
background_fill_color='white', background_fill_alpha=1.0, )) # Linking plot2 with plot1 selection source1.callback = CustomJS(args=dict(s2=source2, p2=plot2), code=""" var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; var d2 = s2.data; var d1_x0; d2['f'] = []; d2['t'] = []; inds.sort(function(a, b){return a-b}); d1_x0 = d1['t'][inds[0]]; for (i = 0; i < inds.length; i++) { d2['f'].push(d1['f'][inds[i]]); d2['t'].push(d1['t'][inds[i]] - d1_x0); }; p2.toolbar.tools[""" + util.reset_tool_index(g.TOOLS2) + """].trigger('do'); s2.trigger('change'); """) ############################################################################## # Widgets device_slc = Select(title="Device ID:",
line_color=None) plotMapBase.add_glyph(source, mapMarker) pltHist = figure(tools=TOOLS, plot_width=800, plot_height=200) pltHist.vbar(x='month', top='n', width=0.5, source=sourceHist, color='navy', selection_color='orange') pltHist.xaxis.ticker = dfMonth['month'] source.callback = CustomJS(args=dict(source=source, sourceHist=sourceHist), code=""" var selIds = source.selected['1d'].indices; var selData = source.data; var sourceOutData = sourceHist.data; console.log(selData['x0'][0]); """) sourceHist.callback = CustomJS(args=dict(source=source, sourceHist=sourceHist), code=""" var selIds = sourceHist.selected['1d'].indices[0]; var selData = sourceHist.data; var sourceData = source.data; sourceData['x0'] = source.data['dateReset']; sourceData['x1'] = source.data['xReset']; sourceData['y0'] = source.data['barriReset']; sourceData['y1'] = source.data['yReset'];
output_file("callback.html") x = [random() for x in range(500)] y = [random() for y in range(500)] s1 = ColumnDataSource(data=dict(x=x, y=y)) p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here") p1.circle('x', 'y', source=s1, alpha=0.6) s2 = ColumnDataSource(data=dict(x=[], y=[])) p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1), tools="", title="Watch Here") p2.circle('x', 'y', source=s2, alpha=0.6) s1.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.selected.indices; var d1 = cb_obj.data; var d2 = s2.data; d2['x'] = [] d2['y'] = [] for (i = 0; i < inds.length; i++) { d2['x'].push(d1['x'][inds[i]]) d2['y'].push(d1['y'][inds[i]]) } s2.change.emit(); """) layout = row(p1, p2) show(layout)
cu1_s.callback = Callback(args=dict(cu1_avg=cu1_avg, cu1_lower=cu1_lower, cu1_upper=cu1_upper), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d = cb_obj.get('data'); var items = []; if (inds.length == 0) { return; } for (i = 0; i < inds.length; i++) { items.push(d['y'][inds[i]]); } var ym = average(items); var std = standardDeviation(items); cu1_avg.get('data')['y'] = [ym, ym] cu1_lower.get('data')['y'] = [ym - (2 * std), ym - (2 * std)] cu1_upper.get('data')['y'] = [(2 * std) + ym, (2 * std) + ym] cb_obj.trigger('change'); cu1_avg.trigger('change'); cu1_lower.trigger('change'); cu1_upper.trigger('change'); //define the functions needed for standard dev and average function standardDeviation(values){ var avg = average(values); var squareDiffs = values.map(function(value){ var diff = value - avg; var sqrDiff = diff * diff; return sqrDiff; }); var avgSquareDiff = average(squareDiffs); var stdDev = Math.sqrt(avgSquareDiff); return stdDev; } function average(data){ var sum = data.reduce(function(sum, value){ return sum + value; }, 0); var avg = sum / data.length; return avg; } """)
def show_projection(alg, selected_ids, dim_red, directionality): filename = "static/data/anchs_" title = "2D Projection - Key Features" samples = 7468 if (not alg): samples = 3114 filename = "static/data/changes_" title = "2D Projection - Changes" filename += dim_red if (not directionality): filename += "_dir_False" filename += ".csv" fp = open(filename, 'r', encoding='utf-8') X = np.zeros((samples, 2)) ids = [] category = [] ft_selected_ids = [] for i in range(samples): roww = fp.readline().split(',') # print(roww) ids.append(int(roww[0])) X[i][0] = float(roww[3]) X[i][1] = float(roww[4]) if int(roww[0]) in selected_ids: ft_selected_ids.append(1) else: ft_selected_ids.append(0) if roww[2] == "TP": category.append(0) elif roww[2] == "TN": category.append(1) elif roww[2] == "FP": category.append(2) elif roww[2] == "FN": category.append(3) # percentage.append(float(roww[4])) fp.close() for p in range(1, 2): x = [] y = [] for i in range(samples): x.append(X[i][0]) y.append(X[i][1]) ################# ################# ##### bokeh ##### ################# ################# color_opt = [ "rgb(27, 158, 119)", "rgb(217, 95, 2)", "rgb(27, 158, 119)", "rgb(217, 95, 2)", "gray" ] alpha_opt = [0.6, 0.05] line_opt = [ "rgb(27, 158, 119)", "rgb(217, 95, 2)", "black", "black", "gray" ] if (len(ft_selected_ids) > 0): alpha_opt = [0.7, 0.02] colors = [] line_color = [] fill_alpha = [] line_alpha = [] for k in range(samples): if ft_selected_ids[k]: colors.append(color_opt[category[k]]) line_color.append(line_opt[category[k]]) fill_alpha.append(alpha_opt[0]) line_alpha.append(alpha_opt[0]) else: colors.append(color_opt[-1]) line_color.append(line_opt[-1]) fill_alpha.append(alpha_opt[-1]) line_alpha.append(alpha_opt[-1]) output_file('2d_changes_map.html') s1 = ColumnDataSource(data=dict(x=x, y=y, ids=ids, category=category, colors=colors, fill_alpha=fill_alpha, line_alpha=line_alpha, line_color=line_color, ft_selected_ids=ft_selected_ids)) hover = HoverTool(tooltips=""" """) # help_b = HelpTool(help_tooltip = """ """) wheel_zoom = WheelZoomTool() lasso_select = LassoSelectTool() p1 = figure( tools=[hover, lasso_select, "reset", "tap", wheel_zoom, "pan"], toolbar_location="right", toolbar_sticky=False, title=title, width=390, height=390) # p1.circle('x', 'y', source=s1, size=7.3, fill_alpha = 'fill_alpha', line_alpha = 'line_alpha', fill_color = 'colors', line_color = 'line_color', # nonselection_fill_alpha=alpha_opt[-1], # nonselection_fill_color=color_opt[-1], # nonselection_line_color=color_opt[-1], # nonselection_line_alpha=alpha_opt[-1] # ) p1.title.text_font_size = '10pt' p1.title.align = 'center' p1.toolbar.active_scroll = wheel_zoom p1.toolbar.active_drag = lasso_select p1.axis.visible = False lasso_select.select_every_mousemove = False # CheckboxGroup to select categories category_selection = CheckboxGroup(labels=["TP", "TN", "FP", "FN"], active=[0, 1, 2, 3]) selection_callback = CustomJS(args=dict(source=s1), code=""" source.change.emit(); """) category_selection.js_on_change('active', selection_callback) # Define the custom filter to return the indices, compare against values in source.data js_filter = CustomJSFilter(args=dict( category_selection=category_selection, source=s1), code=""" var indices = []; for (var i = 0; i <= source.data['category'].length; i++){ if (category_selection.active.includes(source.data['category'][i])) { indices.push(i) } } return indices; """) s1.callback = CustomJS(code=""" var lasso_ids = cb_obj.selected['1d'].indices; //console.log(lasso_ids); var ft_selected_ids = cb_obj.data['ft_selected_ids']; var ids = cb_obj.data['ids']; //console.log(ft_selected_ids); var aggregation_ids = []; for (i=0; i<ft_selected_ids.length; i++){ if (ft_selected_ids[i] == 1 && lasso_ids.includes(i)){ //console.log(ids[i]); aggregation_ids.push(ids[i]); } } if (!(aggregation_ids && aggregation_ids.length)) { aggregation_ids = [-1]; } console.log(aggregation_ids); //parent.makeBokehRequest(aggregation_ids); parent.makeBokehRequest2(aggregation_ids); """) # Use the filter in a view view = CDSView(source=s1, filters=[js_filter]) p1.circle('x', 'y', source=s1, view=view, size=7.3, fill_alpha='fill_alpha', line_alpha='line_alpha', fill_color='colors', line_color='line_color', nonselection_fill_alpha=alpha_opt[-1], nonselection_fill_color=color_opt[-1], nonselection_line_color=color_opt[-1], nonselection_line_alpha=alpha_opt[-1]) layout = column(p1, category_selection) # show(layout) # grid = gridplot([[p1, None]], merge_tools=False) # # show(grid) html = file_html(layout, CDN, "my_plot") html = html.replace("auto;", "0px;") fp = open("static/html/projection_file_raw.html", 'w') fp.write(html) fp.close() # show_projection(False, list(range(7468)), "pca", True )
def plot_hist_gantt(sparkdf, start_column, end_column, hist_var, df=pd.DataFrame(), hist_grouped=False, gantt_pooled=False, unit=None): """ Plots histogram and linked Gantt chart of objects in Spark dataframe. :sparkdf: Input Spark dataframe. :param start_column: Start time column name. :param end_column: End time column name. :param hist_var: Histogram variable column name. :df: Pandas dataframe which can be optionally inputed to reduce redundant operations. :param hist_grouped: If True, each bin in the histogram will be a range of values; if False, each bin will be an individual value. :param gantt_pooled: if True, resulting Gantt chart will pool objects; if False, each object will reside on its own horizontal line. :param unit: Unit of timestamps in sparkdf. :return: None. """ if df.empty: df = sparkdf.sort(start_column).toPandas() if unit: df[start_column] = pd.to_datetime(df[start_column], unit=unit) df[end_column] = pd.to_datetime(df[end_column], unit=unit) # Histogram if hist_grouped: bins, counts, df_bins = bokeh_utils.group_hist(df, hist_var) else: bins, counts, df_bins = bokeh_utils.indiv_hist(sparkdf, hist_var) bin_column = 'bin' df[bin_column] = df_bins c0 = ColumnDataSource(data=df) c1 = ColumnDataSource(data=dict(bins=bins, counts=counts)) f1 = figure(title="Distribution of " + hist_var.capitalize() + "s", tools='box_select', height=500, x_range=bins) f1.vbar(source=c1, x='bins', top='counts', width=0.9) bar_width = 37 if hist_grouped else 75 calc_width = lambda h, w: 500 if len(h) < 5 else len(h) * w f1.width = calc_width(counts, bar_width) f1.xaxis.axis_label = hist_var.capitalize() f1.xgrid.grid_line_color = None f1.yaxis.axis_label = 'Count' f1.y_range.start = 0 # Gantt Chart c2 = ColumnDataSource(data={ start_column: [], end_column: [], hist_var: [], 'bottom': [], 'top': [] }) f2 = figure(title='Gantt Chart', tools='box_zoom,reset', width=1000, height=500, x_axis_type='datetime') f2.quad(source=c2, left=start_column, right=end_column, bottom='bottom', top='top') f2.xaxis.axis_label = 'Time' f2.xaxis.formatter = bokeh_utils.gantt_dtf() f2.add_tools( bokeh_utils.gantt_hover_tool(start_column, end_column, hist_var)) quote = bokeh_utils.quote jscode = bokeh_utils.hist_gantt_callback().format( start_column=quote(start_column), end_column=quote(end_column), bin_column=quote(bin_column), hist_var=quote(hist_var), gantt_pooled=str(gantt_pooled).lower()) c1.callback = CustomJS(code=jscode, args=dict(c0=c0, c2=c2, f2=f2)) layout = column(f1, f2) show(layout)
def monsoon_data_plot_power(self, mon_info, monsoon_results, tag=''): """Plot the monsoon power data using bokeh interactive plotting tool. Args: mon_info: Dictionary with the monsoon packet config. monsoon_results: a MonsoonResult or list of MonsoonResult objects to to plot. tag: an extra tag to append to the resulting filename. """ if not isinstance(monsoon_results, list): monsoon_results = [monsoon_results] logging.info('Plotting the power measurement data.') voltage = monsoon_results[0].voltage total_current = 0 total_samples = 0 for result in monsoon_results: total_current += result.average_current * result.num_samples total_samples += result.num_samples avg_current = total_current / total_samples time_relative = [ data_point.time for monsoon_result in monsoon_results for data_point in monsoon_result.get_data_points() ] power_data = [ data_point.current * voltage for monsoon_result in monsoon_results for data_point in monsoon_result.get_data_points() ] total_data_points = sum( result.num_samples for result in monsoon_results) color = ['navy'] * total_data_points # Preparing the data and source link for bokehn java callback source = ColumnDataSource( data=dict(x0=time_relative, y0=power_data, color=color)) s2 = ColumnDataSource( data=dict( z0=[mon_info.duration], y0=[round(avg_current, 2)], x0=[round(avg_current * voltage, 2)], z1=[round(avg_current * voltage * mon_info.duration, 2)], z2=[round(avg_current * mon_info.duration, 2)])) # Setting up data table for the output columns = [ TableColumn(field='z0', title='Total Duration (s)'), TableColumn(field='y0', title='Average Current (mA)'), TableColumn(field='x0', title='Average Power (4.2v) (mW)'), TableColumn(field='z1', title='Average Energy (mW*s)'), TableColumn(field='z2', title='Normalized Average Energy (mA*s)') ] dt = DataTable( source=s2, columns=columns, width=1300, height=60, editable=True) plot_title = (os.path.basename( os.path.splitext(monsoon_results[0].tag)[0]) + tag) output_file(os.path.join(mon_info.data_path, plot_title + '.html')) tools = 'box_zoom,box_select,pan,crosshair,redo,undo,reset,hover,save' # Create a new plot with the datatable above plot = figure( plot_width=1300, plot_height=700, title=plot_title, tools=tools, output_backend='webgl') plot.add_tools(bokeh_tools.WheelZoomTool(dimensions='width')) plot.add_tools(bokeh_tools.WheelZoomTool(dimensions='height')) plot.line('x0', 'y0', source=source, line_width=2) plot.circle('x0', 'y0', source=source, size=0.5, fill_color='color') plot.xaxis.axis_label = 'Time (s)' plot.yaxis.axis_label = 'Power (mW)' plot.title.text_font_size = {'value': '15pt'} jsscript = open(self.customjsfile, 'r') customjsscript = jsscript.read() # Callback Java scripting source.callback = CustomJS( args=dict(mytable=dt), code=customjsscript) # Layout the plot and the datatable bar save(layout([[dt], [plot]]))
#] g=[i for i in range(14401)] s2=ColumnDataSource(data=dict(z=array_vals[0].tolist(),g=g,**d1)) #Creating the Line graph n = figure(title="Line graph for selected station",x_axis_label='Time in seconds', y_axis_label='Frequency', plot_width=450, plot_height=250) n.line('g','z',source=s2) s1.callback = CustomJS(args=dict(s2=s2),code=""" var d2 = s2.data; s2.data['z']=d2[cb_obj.selected['1d'].indices] console.log(d2['z']) s2.trigger('change'); console.log('Tap event occured at x-position: ' + cb_obj.selected['1d'].indices) """) #Creating a slider slider = Slider(start=0, end=14400, value=0, step=1, title="Time slider in seconds") def update(s1=s1,slider=slider, window=None): data = s1.data v = cb_obj.value data['rate'] = data[v] s1.trigger('change') slider.js_on_change('value', CustomJS.from_py_func(update)) g1=(column(p,widgetbox(slider),)) g2= column(sp,n) layout = row(g1, g2)
def ligand_receptor_interaction(request, sel_thresh): mdsrv_url = obtain_domain_url(request) sel_thresh = float(sel_thresh) cra_path = "/protwis/sites/files/Precomputed/crossreceptor_analysis_files" resli_file_path = path.join(cra_path, "ligres_int.csv") #resli_file_pathobj = Path(resli_file_path) #try: #resli_abs_path = resli_file_pathobj.resolve() df = pd.read_csv(resli_file_path, index_col=[0, 1]) #except FileNotFoundError: # df=pd.DataFrame({}) all_thresh = set(df.index.get_level_values(0)) other_thresh_set = all_thresh - {sel_thresh} other_thresh = sorted(list(other_thresh_set)) compl_file_path = path.join(cra_path, "compl_info.json") compl_data = json_dict(compl_file_path) #Prepare data df_t = df.loc[sel_thresh] df_t.columns.names = ["Position"] df_t = df_t.fillna(value=0) # [!] Only in prod """ indexes = df_t.index.values index_f = sorted([idx for idx in indexes if int(idx[3:]) > 29],key=lambda x: int(x[3:])) df_t = df_t.loc[index_f] """ #Compute cluster and order accordingly dist_mat_md = squareform(pdist(df_t)) method = "ward" res_order, res_linkage = compute_serial_matrix(dist_mat_md, method) df_order = [] for e in res_order: df_order.append(df_t.iloc[e].name) df_t = df_t.loc[df_order] #Rename dyn identifiers (recept_info, recept_info_order, df_t, dyn_gpcr_pdb) = improve_receptor_names(df_t, compl_data) #df_t.sort_index(inplace=True,ascending=False) df_ts = df_t.stack().rename("value").reset_index() #DataSource with extra information df_ri = pd.DataFrame(recept_info) ri_source = ColumnDataSource(df_ri) df_rio = pd.DataFrame(recept_info_order, index=[0]) rio_source = ColumnDataSource(df_rio) df_gnum = pd.DataFrame(dyn_gpcr_pdb) gnum_source = ColumnDataSource(df_gnum) extra_source = ColumnDataSource({ "thresh": [sel_thresh], "mdsrv_url": [mdsrv_url] }) #Map colors colors = colors = [ "#FFFFFF", '#f7fcfc', '#f6fbfc', '#f5fafc', '#f4fafb', '#f2f9fb', '#f1f8fa', '#f0f8fa', '#eff7fa', '#edf6f9', '#ecf6f9', '#ebf5f8', '#e9f4f8', '#e8f4f7', '#e7f3f7', '#e6f2f7', '#e4f1f6', '#e3f0f6', '#e2f0f5', '#e1eff5', '#dfeef4', '#deedf4', '#ddecf4', '#dbebf3', '#daeaf3', '#d9eaf2', '#d8e9f2', '#d6e8f1', '#d5e7f1', '#d4e6f1', '#d3e5f0', '#d1e4f0', '#d0e3ef', '#cfe2ef', '#cde1ee', '#cce0ee', '#cbdfee', '#cadeed', '#c8dded', '#c7dcec', '#c6daec', '#c5d9ec', '#c3d8eb', '#c2d7eb', '#c1d6ea', '#bfd5ea', '#bed4e9', '#bdd2e9', '#bcd1e9', '#bad0e8', '#b9cfe8', '#b8cee7', '#b7cce7', '#b5cbe6', '#b4cae6', '#b3c9e6', '#b1c7e5', '#b0c6e5', '#afc5e4', '#aec3e4', '#acc2e3', '#abc1e3', '#aabfe3', '#a9bee2', '#a7bce2', '#a6bbe1', '#a5bae1', '#a3b8e0', '#a2b7e0', '#a1b5e0', '#a0b4df', '#9eb2df', '#9db1de', '#9cafde', '#9aaedd', '#99acdd', '#98abdd', '#97a9dc', '#95a8dc', '#94a6db', '#93a4db', '#92a3db', '#90a1da', '#8fa0da', '#8e9ed9', '#8c9cd9', '#8b9bd8', '#8a99d8', '#8997d8', '#8795d7', '#8694d7', '#8592d6', '#8490d6', '#828ed5', '#818dd5', '#808bd5', '#7e89d4', '#7d87d4', '#7c85d3', '#7b84d3', '#7982d2', '#7880d2', '#777ed2', '#767cd1', '#747ad1', '#7378d0', '#7276d0', '#7074cf', '#6f72cf', '#6e70cf', '#6d6fce', '#6b6dce', '#6a6bcd', '#6969cd', '#6968cd', '#6866cc', '#6865cc', '#6764cb', '#6762cb', '#6661ca', '#6660ca', '#655fca', '#655dc9', '#645cc9', '#645bc8', '#645ac8', '#6358c7', '#6357c7', '#6356c7', '#6254c6', '#6253c6', '#6252c5', '#6151c5', '#614fc4', '#614ec4', '#614dc4', '#604bc3', '#604ac3', '#6049c2', '#6048c2', '#6046c1', '#5f45c1', '#5f44c1', '#5f43c0', '#5f41c0', '#5f40bf', '#5f3fbe', '#5f3fbd', '#603fbc', '#603eba', '#603eb9', '#603db8', '#613db7', '#613cb5', '#613cb4', '#613cb3', '#623bb2', '#623bb0', '#623aaf', '#623aae', '#6239ac', '#6239ab', '#6239aa', '#6238a9', '#6338a7', '#6337a6', '#6337a5', '#6336a3', '#6336a2', '#6336a1', '#6335a0', '#63359e', '#63349d', '#63349c', '#63349b', '#633399', '#633398', '#633297', '#623295', '#623194', '#623193', '#623192', '#623090', '#62308f', '#622f8e', '#622f8d', '#612e8b', '#612e8a', '#612e89', '#612d87', '#602d86', '#602c85', '#602c84', '#602b82', '#5f2b81', '#5f2b80', '#5f2a7f', '#5e2a7d', '#5e297c', '#5e297b', '#5d2879', '#5d2878', '#5d2877', '#5c2776', '#5c2774', '#5b2673', '#5b2672', '#5a2671', '#5a256f', '#59256e', '#59246d', '#58246b', '#58236a', '#572369', '#572368', '#562266', '#562265', '#552164', '#552163', '#542061', '#532060', '#53205f', '#521f5d', '#511f5c', '#511e5b', '#501e5a', '#4f1d58', '#4f1d57', '#4e1d56', '#4d1c55', '#4c1c53', '#4c1b52', '#4b1b51', '#4a1a4f', '#491a4e', '#481a4d', '#48194c', '#47194a', '#461849', '#451848', '#441746', '#431745', '#421744', '#411643', '#411641', '#401540', '#3f153f', '#3e153d', '#3c143c', '#3b143a', '#3a1339' ] mapper = LinearColorMapper(palette=colors, low=0, high=100) # Define a figure #pan=PanTool(dimensions="width") #mytools = ["hover","tap","save","reset","wheel_zoom",pan] mytools = ["hover", "tap", "save", "reset", "wheel_zoom", "pan"] #w=int(len(df_t.columns)*40,) cw = 275 ch = 30 #h=int(((w-cw)*len(df_t.index)/len(df_t.columns))+ch) w = 760 h = 132 p = figure( plot_width=w, #len(df_t.columns)*40, plot_height=h, #int(len(df_t.index)*40*0.8), #title="Example freq", y_range=list(df_ts.Id.drop_duplicates()), x_range=list(df_ts.Position.drop_duplicates()), tools=mytools, x_axis_location="above", active_drag=None, toolbar_location="right", toolbar_sticky=False) # Create rectangle for heatmap mysource = ColumnDataSource(df_ts) p.rect( y="Id", x="Position", width=1, height=1, source=mysource, line_color=None, fill_color=transform('value', mapper), # set visual properties for selected glyphs selection_line_color="crimson", selection_fill_color=transform('value', mapper), # set visual properties for non-selected glyphs nonselection_fill_alpha=1, nonselection_fill_color=transform('value', mapper), nonselection_line_color=None) # Add legend color_bar = ColorBar(color_mapper=mapper, location=(0, 0), ticker=BasicTicker(desired_num_ticks=2), formatter=PrintfTickFormatter(format="%d%%")) p.add_layout(color_bar, 'right') p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.xaxis.major_label_text_font_size = "9pt" p.yaxis.major_label_text_font_size = "8pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = 1 #"vertical" #Hover tool: p.select_one(HoverTool).tooltips = [ ('Receptor', '@Id'), ('Position', '@Position'), ('Frequency', '@value{(0.0)}%'), ] #Select tool and callback: mysource.callback = CustomJS(args={ "r_info": ri_source, "ro_info": rio_source, "extra_info": extra_source, "gnum_info": gnum_source }, code=""" var sel_ind = cb_obj.selected["1d"].indices; var plot_bclass=$("#plot_col").attr("class"); if (sel_ind.length != 0){ var data = cb_obj.data; var ri_data=r_info.data; var rio_data=ro_info.data; var gnum_data=gnum_info.data; var recept_id=data["Id"][sel_ind]; var pos=data["Position"][sel_ind]; var freq=data["value"][sel_ind]; var pos_ind=gnum_data['index'].indexOf(pos) var pdb_pos=gnum_data[recept_id][pos_ind] var lig=ri_data[recept_id][rio_data['resname']]; var lig_lname=ri_data[recept_id][rio_data['lig_lname']]; var recept=ri_data[recept_id][rio_data['upname']]; var dyn_id_pre=ri_data[recept_id][rio_data['dyn_id']]; var dyn_id=dyn_id_pre.match(/\d*$/)[0]; var prot_id=ri_data[recept_id][rio_data['prot_id']]; var prot_lname=ri_data[recept_id][rio_data['prot_lname']]; var comp_id=ri_data[recept_id][rio_data['comp_id']]; var sel_thresh=extra_info.data["thresh"][0]; var struc_fname=ri_data[recept_id][rio_data['struc_fname']]; var traj_fnames=ri_data[recept_id][rio_data['traj_fnames']]; var delta=ri_data[recept_id][rio_data['delta']]; $('#ngl_iframe')[0].contentWindow.$('body').trigger('createNewRef', [struc_fname , traj_fnames ,lig, delta ,pos, pdb_pos]); if (plot_bclass != "col-xs-9"){ $("#plot_col").attr("class","col-xs-9"); $("#info").css({"visibility":"visible","position":"relative","z-index":"auto"}); } $("#freq_val").html(freq); $("#recept_val").html(prot_lname + " ("+recept+")"); $("#pos_val").html(pos); $("#lig_val").html(lig_lname + " ("+lig+")"); $("#viewer_link").attr("href","../../../view/"+dyn_id+"/"+sel_thresh+"/"+pos); $("#recept_link").attr("href","../../../dynadb/protein/id/"+prot_id); $("#lig_link").attr("href","../../../dynadb/compound/id/"+comp_id); } else { if (plot_bclass != "col-xs-12"){ $("#info").css({"visibility":"hidden","position":"absolute","z-index":"-1"}); $("#plot_col").attr("class","col-xs-12"); } } """) plotdiv_w = w + cw script, div = components(p) context = { 'script': script, 'div': div, 'other_thresh': other_thresh, 'sel_thresh': sel_thresh, 'plotdiv_w': plotdiv_w, 'mdsrv_url': mdsrv_url } return render(request, 'crossreceptor_analysis/index_h.html', context)
def make_map(poly_src, links, rs_data, transit_lines): TOOLS = "pan,wheel_zoom,reset,save" TOOLTIPS = [ ("Sector", "@name"), ] p = figure(tools=TOOLS, width=column_width, height=500, x_axis_location=None, y_axis_location=None, x_range=(-9990000, -9619944), y_range=(5011119, 5310000)) p.grid.grid_line_color = None poly = p.patches('x', 'y', source=poly_src, fill_alpha=None, line_color='#66676A', line_width=1) line_source = make_line(transit_lines) p.multi_line('x', 'y', source=line_source, color='color', line_width=1) seg_src = ColumnDataSource({ 'x0': [], 'y0': [], 'x1': [], 'y1': [], 'width': [] }) seg = p.segment(x0='x0', y0='y0', x1='x1', y1='y1', color="#FFD66E", alpha=0.5, line_width='width', source=seg_src) column_names = [ 'Origin', '1', '2N', '2NW', '2S', '2SW', '2W', '2WNW', '2WSW', '3IN', '3N', '3NW', '3S', '3SW', '3W', '3WNW', '3WSW', '4N', '4NW', '4SW', '4W', '4WNW', '4WSW', 'XIN', 'XWI', 'XIL', 'Total' ] cr_src = ColumnDataSource(rs_data) cr = p.circle(x='POINT_X', y='POINT_Y', color="#EDBD42", size=15, alpha=0.2, hover_color="#DAA316", hover_alpha=1.0, source=cr_src) columns = [TableColumn(field=column_names[0], title=column_names[0])]+\ [TableColumn(field=col, title=col, formatter=NumberFormatter(format="0.0%")) for col in column_names[1:]] tbl_src_m = ColumnDataSource({col: [] for col in column_names}) tbl_src_s = ColumnDataSource({col: [] for col in column_names}) tbl_src_d = ColumnDataSource({col: [] for col in column_names}) mtbl = DataTable(columns=columns, source=tbl_src_m, height=100, selectable=True, width=column_width, fit_columns=True) stbl = DataTable(columns=columns, source=tbl_src_s, height=100, selectable=True, width=column_width, fit_columns=True) dtbl = DataTable(columns=columns, source=tbl_src_d, height=100, selectable=True, width=column_width, fit_columns=True) # Add a hover tool, that sets the link data for a hovered circle code = """ var links = %s; var data = {'x0': [], 'y0': [], 'x1': [], 'y1': [], 'width': []}; var cdata = cb_obj.data; var indices = cb_obj.selected.indices; function pushTable(source, src_indices, suffix){ var target = {'Origin' : [], '1' : [], '2N' : [], '2NW' : [], '2S' : [], '2SW' : [], '2W' : [], '2WNW' : [], '2WSW': [], '3IN' : [],'3N' : [], '3NW' : [], '3S' : [], '3SW' : [],'3W' : [],'3WNW' : [],'3WSW' : [], '4N' : [],'4NW' : [], '4SW' : [],'4W' : [],'4WNW' : [],'4WSW' : [], 'XIN' : [], 'XWI' : [], 'XIL' : [],'Total' : []}; for (var i = 0; i < src_indices.length; i++) { var ind0 = src_indices[i] target['Origin'].push(source['origin_geo'+ suffix][ind0]); target['1'].push(source['1'+ suffix][ind0]); target['2N'].push(source['2N'+ suffix][ind0]); target['2NW'].push(source['2NW'+ suffix][ind0]); target['2S'].push(source['2S'+ suffix][ind0]); target['2SW'].push(source['2SW'+ suffix][ind0]); target['2W'].push(source['2W'+ suffix][ind0]); target['2WNW'].push(source['2WNW'+ suffix][ind0]); target['2WSW'].push(source['2WSW'+ suffix][ind0]); target['3IN'].push(source['3IN'+ suffix][ind0]); target['3N'].push(source['3N'+ suffix][ind0]); target['3NW'].push(source['3NW'+ suffix][ind0]); target['3S'].push(source['3S'+ suffix][ind0]); target['3SW'].push(source['3SW'+ suffix][ind0]); target['3W'].push(source['3W'+ suffix][ind0]); target['3WNW'].push(source['3WNW'+ suffix][ind0]); target['3WSW'].push(source['3WSW'+ suffix][ind0]); target['4N'].push(source['4N'+ suffix][ind0]); target['4NW'].push(source['4NW'+ suffix][ind0]); target['4SW'].push(source['4SW'+ suffix][ind0]); target['4W'].push(source['4W'+ suffix][ind0]); target['4WNW'].push(source['4WNW'+ suffix][ind0]); target['4WSW'].push(source['4WSW'+ suffix][ind0]); target['XIL'].push(source['XIL'+ suffix][ind0]); target['XIN'].push(source['XIN'+ suffix][ind0]); target['XWI'].push(source['XWI'+ suffix][ind0]); target['Total'].push(source['Total'+ suffix][ind0]); } return target; } for (var i = 0; i < indices.length; i++){ var ind0 = indices[i] for (var j = 0; j < links['segments'][ind0].length; j++) { var ind1 = links['segments'][ind0][j]; data['x0'].push(cdata.POINT_X[ind0]); data['y0'].push(cdata.POINT_Y[ind0]); data['x1'].push(cdata.POINT_X[ind1]); data['y1'].push(cdata.POINT_Y[ind1]); data['width'].push(links['width'][ind0][ind1]); } } segment.data = data; model_source.data = pushTable(cdata, indices, '_m'); survey_source.data = pushTable(cdata, indices, '_s'); diff_source.data = pushTable(cdata, indices, '_diff'); model_source.trigger('change'); survey_source.trigger('change'); diff_source.trigger('change'); model_tbl.trigger('change'); survey_tbl.trigger('change'); diff_tbl.trigger('change'); """ % (links) TOOLTIPS = [("Sector", '@rs2'), ("Total Transit Trips From Sector", "@alightings_s{0,0}"), ("Total Transit Trips To Sector", "@boardings_s{0,0}")] cr_src.callback = CustomJS(args=dict(segment=seg_src, model_source=tbl_src_m, survey_source=tbl_src_s, diff_source=tbl_src_d, model_tbl=mtbl, survey_tbl=stbl, diff_tbl=dtbl), code=code) p.add_tools(HoverTool(tooltips=TOOLTIPS, renderers=[cr])) p.add_tools(TapTool(renderers=[cr])) #p.add_tools(BoxSelectTool(renderers=[cr])) p.add_tile(CARTODBPOSITRON_RETINA) return p, mtbl, stbl, dtbl
title="Select Here") p.circle('x', 'y', color='color', size=8, source=s, alpha=0.4) s2 = ColumnDataSource(data=dict(x=[0, 1], ym=[0.5, 0.5])) p.line(x='x', y='ym', color="orange", line_width=5, alpha=0.6, source=s2) s.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.selected['1d'].indices; var d = cb_obj.data; var ym = 0 if (inds.length == 0) { return; } for (i = 0; i < d['color'].length; i++) { d['color'][i] = "navy" } for (i = 0; i < inds.length; i++) { d['color'][inds[i]] = "firebrick" ym += d['y'][inds[i]] } ym /= inds.length s2.data['ym'] = [ym, ym] cb_obj.change.emit(); s2.change.emit(); """) show(p)
def bokeh_2d(ids_dict, save_to, load_from_data, load_from_clusters, load_from_metadata, audio_path, **curr_params_data): df_data = pd.read_csv(load_from_data, names=['UniqueID', 'f1', 'f2']) df_clusters = pd.read_csv(load_from_clusters, names=['UniqueID', 'label']) df_metadata = pd.read_csv(load_from_metadata, names=['UniqueID', 'file_path', 'artist']) df_plot = df_data.join(df_clusters.set_index('UniqueID'), on='UniqueID') df_plot = df_plot.join(df_metadata.set_index('UniqueID'), on='UniqueID') uid = df_plot['UniqueID'].values x = df_plot['f1'].values y = df_plot['f2'].values label = df_plot['label'].values artist = df_plot['artist'].values display_name = np.array([get_key(x, ids_dict) for x in uid]) color_vals = [ "olive", "darkred", "goldenrod", "skyblue", "red", "darkblue", "gray", "indigo", "black" ] color = np.array([color_vals[l] for l in label]) audio = np.array( [os.path.join('..', audio_path, iD + '.wav') for iD in uid]) images = np.array([]) output_file(save_to) s1 = ColumnDataSource(data=dict( x=x, y=y, uid=uid, desc=audio, dsp=display_name, arts=artist, #imgs=images, dsp=display_names, colors=color)) hovertoolcallback = CustomJS(args=dict(source=s1), code=""" var uids = source.data['uid']; const indices = cb_data.index.indices; if (indices.length > 0){ for (i=1; i<indices.length; i++){ //console.log(uids[indices[i]]); var element = document.getElementById(uids[indices[i]]); element.parentNode.removeChild(element); } } """) hover = HoverTool(callback=hovertoolcallback, attachment='below', tooltips=""" <div id="@uid"> <img src="@imgs" height="120" alt="@imgs" width="120" style="display: block; margin-left: auto; margin-right: auto;" border="2" ></img> <audio src="@desc" height="20" width="20" autoplay border="2" ></audio> </div> <p align="center" style="display: block; width: 320px;">@dsp</p> """) tap_box_toolcallback = CustomJS(args=dict(source=s1), code=""" var sources = source.data['desc']; var names = source.data['dsp']; var inds = source['selected']['1d'].indices; for (i=0; i<inds.length; i++){ var title = names[inds[i]]; var source = sources[inds[i]]; var para = document.createElement("p"); var node = document.createTextNode(title); para.appendChild(node); document.body.appendChild(para); var x = document.createElement("AUDIO"); var song = String(title); x.setAttribute("src",source); x.setAttribute("controls", "controls"); document.body.appendChild(x); var para2 = document.createElement("br"); document.body.appendChild(para2); } """) tap = TapTool(callback=tap_box_toolcallback) box = BoxSelectTool(callback=tap_box_toolcallback) help_b = HelpTool(help_tooltip=""" Button fuctions: Pan: Move around plot Lasso Select: View plot of artists in selection Box Select: Listen to all songs in selection Wheel Zoom: Resize plot Tap (Click): Listen to all overlaping songs Hover: Listen, view album cover and title Reset """) wheel_zoom = WheelZoomTool() lasso_select = LassoSelectTool() p2 = figure(width=700, height=700) p1 = figure(tools=[ hover, tap, box, lasso_select, help_b, wheel_zoom, "pan", "reset" ], toolbar_location="right", toolbar_sticky=False, title="Music Collections", width=700, height=700) p1.circle('x', 'y', source=s1, size=7.3, fill_alpha=0.5, fill_color='colors', line_color='colors') p1.title.text_font_size = '12pt' p1.title.align = 'center' p1.toolbar.active_scroll = wheel_zoom p1.toolbar.active_drag = lasso_select s2 = ColumnDataSource( data=dict(artists=[], counts=[], full_artists=[], full_counts=[])) s1.callback = CustomJS(args=dict(s2=s2), code=""" console.log(cb_obj); if (document.getElementById("source_d3js")==null){ var d3js = document.createElement('script'); d3js.src = "https://d3js.org/d3.v3.min.js"; d3js.charset = "utf-8"; d3js.id = "source_d3js"; document.head.appendChild(d3js); } var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; var d2 = s2.data; d2['full_artists'] = []; d2['full_counts'] = []; var max_freq = 0; for (i=0; i<inds.length; i++){ var current = d1['arts'][inds[i]]; if (d2['full_artists'].indexOf(current) == -1){ d2['full_artists'].push(d1['arts'][inds[i]]); d2['full_counts'].push(1); } else{ d2['full_counts'][d2['full_artists'].indexOf(current)] += 1; if (d2['full_counts'][d2['full_artists'].indexOf(current)] > max_freq){ max_freq = d2['full_counts'][d2['full_artists'].indexOf(current)]; } } } console.log(max_freq); d2['artists'] = []; d2['counts'] = []; var thres = max_freq * 0.05; //filter arrays to only include freqs >= 5pcnt of max_freq for (i=0; i<d2['full_artists'].length; i++){ if (d2['full_counts'][i] >= thres){ d2['artists'].push(d2['full_artists'][i]); d2['counts'].push(d2['full_counts'][i]); } } s2.change.emit(); if (inds.length > 5){ if (document.getElementById("right_side_style")==null){ var css = ".right_side div {\\n\\tfont: 12px sans-serif;\\n"; css = css.concat("\\tbackground-color: white;\\n\\tpadding: 3px;\\n\\tcolor: white;}"); var style = document.createElement('style'); style.type = 'text/css'; style.id = "right_side_style"; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); } if (document.getElementById("chart_style")==null){ var css = ".chart div {\\n\\tfont: 12px sans-serif;\\n"; css = css.concat("\\tbackground-color: steelblue;\\n\\topacity: 0.8;\\n\\theight: 14px;\\n\\tmargin: 1px;\\n\\twidth: 470px;\\n\\ttext-align: right;\\n\\tcolor: white;}"); var style = document.createElement('style'); style.type = 'text/css'; style.id = "chart_style"; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); } if (document.getElementById("artist_style")==null){ var css = ".artists div {\\n\\tfont: 12px sans-serif;\\n"; css = css.concat("\\tbackground-color: white;\\n\\theight: 14px;\\n\\tmargin: 1px;\\n\\twidth: 470px;\\n\\ttext-align: right;\\n\\tcolor: black;}"); var style = document.createElement('style'); style.type = 'text/css'; style.id = "artist_style"; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); } if (document.getElementById("right_side_div")==null){ var rightdiv = document.createElement('div'); rightdiv.className = "right_side"; rightdiv.style = "float: left; position: absolute; top: 10px; left: 720px; width: 970px;"; rightdiv.id = "right_side_div"; //document.getElementsByClassName("bk-root")[0].innerHTML = ""; //document.getElementsByClassName("bk-root")[0].style = "width: 970px"; document.getElementsByClassName("bk-root")[0].appendChild(rightdiv); } else{ //document.getElementsByClassName("bk-spacer-box bk-layout-fixed")[0].style = "width: 970px"; document.getElementById("right_side_div").innerHTML = ""; } if (document.getElementById("title_p")==null){ var para = document.createElement("p"); var node = document.createTextNode("Artist Frequencies"); para.className = "title"; para.style = "text-align: center; font-weight: bold; font-size: 15px;"; para.id = "title_p"; para.appendChild(node); document.getElementsByClassName("right_side")[0].appendChild(para); } else{ document.getElementById("artists_div").innerHTML = ""; } if (document.getElementById("artists_div")==null){ var artdiv = document.createElement('div'); artdiv.className = "artists"; artdiv.style = "float: left; tdisplay: inline-flex;"; artdiv.id = "artists_div"; document.getElementsByClassName("right_side")[0].appendChild(artdiv); } else{ document.getElementById("artists_div").innerHTML = ""; } if (document.getElementById("chart_div")==null){ var chartdiv = document.createElement('div'); chartdiv.className = "chart"; chartdiv.style = "float: right; tdisplay: inline-flex;"; chartdiv.id = "chart_div"; document.getElementsByClassName("right_side")[0].appendChild(chartdiv); } else{ document.getElementById("chart_div").innerHTML = ""; } if (document.getElementById("source_d3js")==null){ var d3js = document.createElement('script'); d3js.src = "https://d3js.org/d3.v3.min.js"; d3js.id = "source_d3js"; document.head.appendChild(d3js); } if (document.getElementById("mycode")==null){ var code_div = document.createElement('script'); code_div.id = "mycode"; document.body.appendChild(code_div); } //populate var data with d2["counts"] and var art_names with d2["artists"] var string = "["; for (j=0; j<d2['counts'].length-1; j++){ var tmp = d2['counts'][j]; string = string.concat(tmp); string += ", "; } var tmp = d2['counts'][d2['counts'].length-1]; string = string.concat(tmp); string += "]"; var string1 = "[\\""; for (j=0; j<d2['artists'].length-1; j++){ var tmp = d2['artists'][j]; string1 = string1.concat(tmp); string1 = string1.concat("\\"") string1 += ", \\""; } var tmp = d2['artists'][d2['artists'].length-1]; string1 = string1.concat(tmp); string1 += "\\"]"; var d3js_code = "var data = "; d3js_code = d3js_code.concat(string); d3js_code = d3js_code + ";\\n\\n"; d3js_code = d3js_code+ "var art_names = "; d3js_code = d3js_code.concat(string1); d3js_code = d3js_code + ";\\n\\n"; d3js_code = d3js_code.concat("var x = d3.scale.linear()\\n .domain([0, d3.max(data)])\\n .range([0, 470]);\\n\\n"); // var x = d3.scale.linear() // .domain([0, d3.max(data)]) // .range([0, 420]); d3js_code = d3js_code.concat("d3.select(\\".chart\\")\\n .selectAll(\\"div\\")\\n "+ ".data(data)\\n .enter().append(\\"div\\")\\n "+ ".style(\\"width\\", function(d) { return x(d) + \\"px\\"; })\\n .text(function(d) { return d; });"); // d3.select(".chart") // .selectAll("div") // .data(data) // .enter().append("div") // .style("width", function(d) { return x(d) + "px"; }) // .style("margin", "auto 5px") // .text(function(d) { return d; });"; // .class("chartel"); d3js_code = d3js_code.concat("\\n\\nd3.select(\\".artists\\")\\n .selectAll(\\"div\\")\\n "+ ".data(art_names)\\n .enter().append(\\"div\\")\\n "+ ".style(\\"width\\", \\"300\\")\\n .text(function(d) { return d; });"); // d3.select(".artists") // .selectAll("div") // .data(art_names) // .enter().append("div") // .style("width", "300") // .style("margin", "auto 5px") // .text(function(d) { return d; });"; // .class("artel"); document.getElementById("mycode").innerHTML = ""; document.getElementById("mycode").appendChild(document.createTextNode(d3js_code)); var script = document.getElementById("mycode"); eval(script.innerHTML); // Check if chart and script exist // if not, create them // if they are, change innerhtml for script // delete nodes from char and repopulate with new data } """) grid = gridplot([[p1, None]], merge_tools=False) show(grid)
d1['color'][inds[i]] = colors[clusters]; } cb_obj.change.emit(); """) source.selected.indices = [] cluster_button.on_click(callback) run_button.on_click(run_task) source.callback = CustomJS(args=dict(colors=colors, clusters=i), code=""" var inds = cb_obj.getv('selected')['1d'].indices; var d1 = cb_obj.data; if (inds.length == 0) { return; } for (i = 0; i < inds.length; i++) { d1['cluster'][inds[i]] = clusters; d1['color'][inds[i]] = colors[clusters]; } cb_obj.change.emit(); """) xdr = Range1d(start=0, end=300) ydr = Range1d(start=0, end=500) plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=300, plot_height=500, h_symmetry=False,
def monsoon_data_plot(mon_info, file_path, tag=""): """Plot the monsoon current data using bokeh interactive plotting tool. Plotting power measurement data with bokeh to generate interactive plots. You can do interactive data analysis on the plot after generating with the provided widgets, which make the debugging much easier. To realize that, bokeh callback java scripting is used. View a sample html output file: https://drive.google.com/open?id=0Bwp8Cq841VnpT2dGUUxLYWZvVjA Args: mon_info: obj with information of monsoon measurement, including monsoon device object, measurement frequency, duration and offset etc. file_path: the path to the monsoon log file with current data Returns: plot: the plotting object of bokeh, optional, will be needed if multiple plots will be combined to one html file. dt: the datatable object of bokeh, optional, will be needed if multiple datatables will be combined to one html file. """ log = logging.getLogger() log.info("Plot the power measurement data") #Get results as monsoon data object from the input file results = monsoon.MonsoonData.from_text_file(file_path) #Decouple current and timestamp data from the monsoon object current_data = [] timestamps = [] voltage = results[0].voltage [current_data.extend(x.data_points) for x in results] [timestamps.extend(x.timestamps) for x in results] period = 1 / float(mon_info.freq) time_relative = [x * period for x in range(len(current_data))] #Calculate the average current for the test current_data = [x * 1000 for x in current_data] avg_current = sum(current_data) / len(current_data) color = ['navy'] * len(current_data) #Preparing the data and source link for bokehn java callback source = ColumnDataSource( data=dict(x0=time_relative, y0=current_data, color=color)) s2 = ColumnDataSource( data=dict(z0=[mon_info.duration], y0=[round(avg_current, 2)], x0=[round(avg_current * voltage, 2)], z1=[round(avg_current * voltage * mon_info.duration, 2)], z2=[round(avg_current * mon_info.duration, 2)])) #Setting up data table for the output columns = [ TableColumn(field='z0', title='Total Duration (s)'), TableColumn(field='y0', title='Average Current (mA)'), TableColumn(field='x0', title='Average Power (4.2v) (mW)'), TableColumn(field='z1', title='Average Energy (mW*s)'), TableColumn(field='z2', title='Normalized Average Energy (mA*s)') ] dt = DataTable(source=s2, columns=columns, width=1300, height=60, editable=True) plot_title = file_path[file_path.rfind('/') + 1:-4] + tag output_file("%s/%s.html" % (mon_info.data_path, plot_title)) TOOLS = ('box_zoom,box_select,pan,crosshair,redo,undo,reset,hover,save') # Create a new plot with the datatable above plot = figure(plot_width=1300, plot_height=700, title=plot_title, tools=TOOLS, output_backend="webgl") plot.add_tools(bokeh_tools.WheelZoomTool(dimensions="width")) plot.add_tools(bokeh_tools.WheelZoomTool(dimensions="height")) plot.line('x0', 'y0', source=source, line_width=2) plot.circle('x0', 'y0', source=source, size=0.5, fill_color='color') plot.xaxis.axis_label = 'Time (s)' plot.yaxis.axis_label = 'Current (mA)' plot.title.text_font_size = {'value': '15pt'} #Callback Java scripting source.callback = CustomJS(args=dict(mytable=dt), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = mytable.get('source').get('data'); ym = 0 ts = 0 d2['x0'] = [] d2['y0'] = [] d2['z1'] = [] d2['z2'] = [] d2['z0'] = [] min=max=d1['x0'][inds[0]] if (inds.length==0) {return;} for (i = 0; i < inds.length; i++) { ym += d1['y0'][inds[i]] d1['color'][inds[i]] = "red" if (d1['x0'][inds[i]] < min) { min = d1['x0'][inds[i]]} if (d1['x0'][inds[i]] > max) { max = d1['x0'][inds[i]]} } ym /= inds.length ts = max - min dx0 = Math.round(ym*4.2*100.0)/100.0 dy0 = Math.round(ym*100.0)/100.0 dz1 = Math.round(ym*4.2*ts*100.0)/100.0 dz2 = Math.round(ym*ts*100.0)/100.0 dz0 = Math.round(ts*1000.0)/1000.0 d2['z0'].push(dz0) d2['x0'].push(dx0) d2['y0'].push(dy0) d2['z1'].push(dz1) d2['z2'].push(dz2) mytable.trigger('change'); """) #Layout the plot and the datatable bar l = layout([[dt], [plot]]) save(l) return [plot, dt]
def monsoon_data_plot_power(self, samples, voltage, dest_path, plot_title): """Plot the monsoon power data using bokeh interactive plotting tool. Args: samples: a list of tuples in which the first element is a timestamp and the second element is the sampled current at that time voltage: the voltage that was used during the measurement dest_path: destination path plot_title: a filename and title for the plot. """ logging.info('Plotting the power measurement data.') time_relative = [sample[0] for sample in samples] duration = time_relative[-1] - time_relative[0] current_data = [sample[1] * 1000 for sample in samples] avg_current = sum(current_data) / len(current_data) power_data = [current * voltage for current in current_data] color = ['navy'] * len(samples) # Preparing the data and source link for bokehn java callback source = ColumnDataSource( data=dict(x0=time_relative, y0=power_data, color=color)) s2 = ColumnDataSource( data=dict(z0=[duration], y0=[round(avg_current, 2)], x0=[round(avg_current * voltage, 2)], z1=[round(avg_current * voltage * duration, 2)], z2=[round(avg_current * duration, 2)])) # Setting up data table for the output columns = [ TableColumn(field='z0', title='Total Duration (s)'), TableColumn(field='y0', title='Average Current (mA)'), TableColumn(field='x0', title='Average Power (4.2v) (mW)'), TableColumn(field='z1', title='Average Energy (mW*s)'), TableColumn(field='z2', title='Normalized Average Energy (mA*s)') ] dt = DataTable(source=s2, columns=columns, width=1300, height=60, editable=True) output_file(os.path.join(dest_path, plot_title + '.html')) tools = 'box_zoom,box_select,pan,crosshair,redo,undo,reset,hover,save' # Create a new plot with the datatable above plot = figure(plot_width=1300, plot_height=700, title=plot_title, tools=tools, output_backend='webgl') plot.add_tools(bokeh_tools.WheelZoomTool(dimensions='width')) plot.add_tools(bokeh_tools.WheelZoomTool(dimensions='height')) plot.line('x0', 'y0', source=source, line_width=2) plot.circle('x0', 'y0', source=source, size=0.5, fill_color='color') plot.xaxis.axis_label = 'Time (s)' plot.yaxis.axis_label = 'Power (mW)' plot.title.text_font_size = {'value': '15pt'} jsscript = open(self.customjsfile, 'r') customjsscript = jsscript.read() # Callback Java scripting source.callback = CustomJS(args=dict(mytable=dt), code=customjsscript) # Layout the plot and the datatable bar save(layout([[dt], [plot]]))
def polynomial(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args # Get all the form arguments in the url with defaults _source=getitem(args,'_source',url_default) _size=int(getitem(args,'size','5')) favcolor=getitem(args,'favcolor','#1F77B4') bcolor=getitem(args,'bcolor','#F5F5DC') _shape=getitem(args,'_shape','circle') lcolor=getitem(args,'lcolor','#FCB938') toggle=bool(getitem(args,'toggle','')) #Parsing json DEFAULT_TICKERS = ['TOAs','RawProfiles', 'Period', 'PeriodDerivative', 'DM', 'RMS', 'Binary'] TOOLS = "tap,resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select,previewsave,hover" Pulsar,TOAs,RawProfiles, Period, PeriodDerivative, DM, RMS, Binary=[],[],[],[],[],[],[],[] url_location=urllib.urlopen(_source) for item in ijson.items(url_location,"item"): Pulsar.append(str(item["Pulsar"])) TOAs.append(float(item["TOAs"])) RawProfiles.append(int(item["Raw Profiles"])) Period.append(float(item["Period"])) PeriodDerivative.append(float(item["Period Derivative"])) DM.append(str(item["DM"])) RMS.append(str(item["RMS"])) if (item["Binary"]=="Y"): Binary.append("Yes") else: Binary.append("No") #Create a plot periodvs period derivative s1 = ColumnDataSource( data=dict( Pulsar=Pulsar, TOAs=TOAs, RawProfiles=RawProfiles, Period=Period, PeriodDerivative=PeriodDerivative, DM=DM, RMS=RMS, Binary=Binary, ) ) p1 = figure(plot_width=600, plot_height=600, title="Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/5, max(PeriodDerivative)+max(PeriodDerivative)/5],x_range=[min(Period)-min(Period)/5, max(Period)+max(Period)/5],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS) p1.background_fill_color = bcolor p1.background_fill_alpha = "0.5" getattr(p1,_shape)('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1) #p1.circle('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1) p1.xaxis.axis_label_text_font_size = "15pt" p1.yaxis.axis_label_text_font_size = "15pt" #p1.xaxis[0].formatter = PrintfTickFormatter(format="s") #p1.yaxis[0].formatter = PrintfTickFormatter(format=" s/s") #Toggle Line #if getattr(p1,line,None): # getattr(p1,line)('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1) p1.line('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1,visible=toggle) # Custom data source for selected points s2 = ColumnDataSource( data=dict( Pulsar=[], TOAs=[], RawProfiles=[], Period=[], PeriodDerivative=[], DM=[], RMS=[], Binary=[], ) ) p2= figure(plot_width=600, plot_height=600, title=" Selected points from Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/10, max(PeriodDerivative)+max(PeriodDerivative)/10],x_range=[min(Period)-min(Period)/10, max(Period)+max(Period)/10],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS) p2.xaxis.axis_label_text_font_size = "15pt" p2.yaxis.axis_label_text_font_size = "15pt" p2.circle('Period', 'PeriodDerivative', legend="period deri",alpha=1.2, source=s2) s1.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = s2.get('data'); d2['Pulsar'] = [] d2['TOAs'] = [] d2['RawProfiles'] = [] d2['Period'] = [] d2['PeriodDerivative'] = [] d2['DM'] = [] d2['RMS'] = [] d2['Binary'] = [] for (i = 0; i < inds.length; i++) { d2['Pulsar'].push(d1['Pulsar'][inds[i]]) d2['TOAs'].push(d1['TOAs'][inds[i]]) d2['RawProfiles'].push(d1['RawProfiles'][inds[i]]) d2['Period'].push(d1['Period'][inds[i]]) d2['PeriodDerivative'].push(d1['PeriodDerivative'][inds[i]]) d2['DM'].push(d1['DM'][inds[i]]) d2['RMS'].push(d1['RMS'][inds[i]]) d2['Binary'].push(d1['Binary'][inds[i]]) } s2.trigger('change'); """ ) hover = p1.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Pulsar's Name", '@Pulsar'), ('TOAs', '@TOAs'), ('RawProfiles', '@RawProfiles'), ('Period[s]', '@Period'), ('PeriodDerivative[s/s]', '@PeriodDerivative'), ('DM[pc/cc]', '@DM'), ('RMS[us]', '@RMS'), ('Binary', '@Binary'), ]) js_resources = INLINE.render_js() css_resources = INLINE.render_css() #Setting up layout layout = hplot(p1, p2) script, div = components(layout,INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, _source=_source, Pulsar=Pulsar, _shape=_shape, favcolor=favcolor, bcolor=bcolor, _size=_size, lcolor=lcolor, toggle=toggle ) return encode_utf8(html)
def bokeh_plot(self): chosen_bic = literal_eval(self.get_argument('subject')) # prep n_cols = len(chosen_bic['feats']) n_lines = len(chosen_bic['objs']) bic_mtrx = np.zeros((n_lines, n_cols), dtype=object) objs = sorted(chosen_bic['objs']) feats = sorted(chosen_bic['feats']) # bokeh objlist = [] featlist = [] # colormap = ["#444444", "#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a"] color = [] i = 0 for obj in objs: j = 0 for feat in feats: objlist.append(obj) featlist.append(feat) color.append("#669999") #color.append(np.random.choice(colormap)) bic_mtrx[i, j] = (obj, feat) j += 1 i += 1 # ColumnDataSource Object source = ColumnDataSource(data=dict(xname=objlist, yname=featlist, colors=color, count=bic_mtrx.flatten())) source.callback = Callback(args=dict(source=source), code=""" var data = source.get('data'); var f = source.get('value'); object_p = data['xname']; attribute_p = data['yname']; document.write(f); """) p = figure(title="Matriz do Co-Grupo", x_axis_location="above", tools="resize, previewsave, reset, hover", x_range=list(objs), y_range=list(reversed(feats)), plot_width=n_lines * 100, plot_height=n_cols * 40, toolbar_location="left") p.rect('xname', 'yname', 1, 1, source=source, color='colors', line_color="#000000") p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = "#000000" p.axis.major_label_text_font_size = "10pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = np.pi / 2.5 p.border_fill = "#FFFFF0" tap = TapTool(plot=p) # tap = TapTool(plot=p, action=OpenURL(url="http://www.ufabc.edu.br")) p.tools.append(tap) hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('Tupla', '@yname, @xname')]) # hover = HoverTool(plot=p, tooltips=[('Tupla', '@yname, @xname')]) # p.tools.append(hover) # script & div tags script, div = components(p, CDN) # return to html return (script, div)
def plot_timeseries_return(mongo_connection, optimization_name, top=10, param_filter_fn=None, flat_staking=True, config_ids=None): pipeline = [{ '$match': { 'name': optimization_name } }, { '$sort': { 'updated_dt': -1 } }, { '$limit': 1 }, { '$unwind': '$optimization' }, { '$project': { 'config_id': '$optimization.config_id', 'start_date': '$optimization.start_date', 'end_date': '$optimization.end_date', 'score': '$optimization.score', 'metrics': '$optimization.metrics' } }] if config_ids is not None: pipeline.append({ '$match': { 'config_id': { '$in': [ObjectId(cfg) for cfg in config_ids] } } }) top = len(config_ids) pipeline.append({'$sort': {'score': -1}}) optimization_runs = mongo_connection['backtest_optimization'].aggregate( pipeline) seen_configs = [] count = 0 plot_data = [] all_metrics = [] for run in optimization_runs: start_date = run['start_date'] end_date = run['end_date'] config_id = run['config_id'] if config_id in seen_configs: continue seen_configs.append(config_id) params = mongo_connection['backtest_configurations'].find_one( {'_id': run['config_id']}) if param_filter_fn is not None: if not param_filter_fn(params): continue results = mongo_connection['backtest_results'].find({ 'config_id': config_id, 'date': { '$lte': end_date, '$gte': start_date } }) if results.count() > 0 and params is not None: dict_result = [] for i in results: for o in i['orders']: dict_result.append({ 'date': i['date'], 'pnl': o.get('pnl', np.nan), 'sticker': o['sticker'], 'is_back': o['is_back'], 'stake': o['size'], 'odds': o['odds'] }) tbl = pd.DataFrame(dict_result) tbl['dt'] = tbl['date'].map(lambda x: datetime.combine( datetime.strptime(x, '%Y%m%d'), datetime.min.time())) starting_capital = 10000 if flat_staking: tbl.loc[:, 'capital'] = starting_capital timeseries = flat_capital_traces(tbl) data = pd.DataFrame({ 'config': str(config_id), 'return': timeseries['cum_return'], 'date': list(timeseries.index) }) else: # Accumulate the capital cap_df = tbl.groupby('dt').pnl.sum().to_frame().rename( columns={'pnl': 'capital'}) cap_df = cap_df.reset_index().sort('dt') cap_df.loc[:, 'capital'] = starting_capital + np.concatenate( [[0.], cap_df.capital.cumsum().values[:-1]]) tbl = tbl.merge(cap_df) timeseries = cumulative_capital_traces(tbl, starting_capital) data = pd.DataFrame({ 'config': str(config_id), 'return': timeseries['cum_ret'], 'date': list(timeseries.index) }) plot_data.append(data) met_df = flat_capital_metrics(tbl.dropna(subset=['pnl'])) met_df = met_df.rename( columns={ 'unitary_stake_return': 'unit_return', 'average_trade_win': 'avg_trade_win', 'average_trade_loss': 'avg_trade_loss', 'cum_return': 'cum_ret', 'volatility (not annualised)': 'vol_not_ann', 'sharpe_ratio': 'sharpe_ratio', 'maximum_drawdown': 'max_drawdown', 'drawdown_duration (days)': 'drawdown_days', 'maximum_runup': 'max_runup', 'runup_duration (days)': 'runup_days' }) met_df.loc[:, 'config'] = str(run['config_id']) all_metrics.append(met_df) count += 1 if count >= top: break if count == 0: raise Exception('Mongo cursor empty, no data were found') plot_data = pd.concat(plot_data) all_metrics = pd.concat(all_metrics) plot_data = plot_data.merge(all_metrics, how='left', on='config') # Create data sources for plotting configs = list(set(plot_data.config)) plot_data.loc[:, 'colour'] = 'grey' plot_data.loc[:, 'alpha'] = 0.5 plot_data.loc[:, 'size'] = 5 s = ColumnDataSource(data=plot_data) s2 = ColumnDataSource({ 'config': configs, 'date': [ plot_data.date.loc[plot_data.config == cfg].values for cfg in configs ], 'return': [ plot_data['return'].loc[plot_data.config == cfg].values for cfg in configs ], 'colour': ['grey'] * len(configs), 'alpha': [0.0] * len(configs) }) # Add plots p1 = figure(plot_width=900, plot_height=400, title="Cumulative returns", tools=[TapTool(), SaveTool()], x_axis_type='datetime') p1.circle('date', 'return', source=s, color='colour', alpha='alpha', size='size') p1.multi_line(xs='date', ys='return', source=s2, color='colour', alpha='alpha') p1.yaxis.axis_label = 'Return' # Metrics metric_formats = OrderedDict([('n_trades', '0'), ('n_win', '0'), ('n_loss', '0'), ('hit_ratio', '0.000'), ('avg_trade_win', '0.0000'), ('avg_trade_loss', '0.0000'), ('unit_return', '0.000'), ('total_pnl', '0.00'), ('cr_trade', '0.000'), ('cr_day', '0.000'), ('cum_ret', '0.000'), ('vol_not_ann', '0.000'), ('cum_ret/vol', '0.000'), ('max_drawdown', '0.000'), ('drawdown_days', '0'), ('max_runup', '0.000'), ('runup_days', '0')]) cols = ['config'] + metric_formats.keys() # Add table of metrics s3 = ColumnDataSource(all_metrics.iloc[[]]) tbl_columns = [TableColumn(field='config', title='config', width=100)] tbl_columns += [ TableColumn(field=mm, title=mm, width=100, formatter=NumberFormatter(format=fmt)) for mm, fmt in metric_formats.iteritems() ] data_table = DataTable(source=s3, columns=tbl_columns, width=900, height=200, fit_columns=False) cb_code = ''' var cfgs = []; var ind = cb_obj.get('selected')['1d'].indices; var d = cb_obj.get('data'); var d2 = s2.get('data'); var d3 = s3.get('data'); var cols = __COLS__; for (i = 0; i < d['config'].length; i++){ if ((ind.indexOf(i) != -1) && (cfgs.indexOf(d['config'][i]) == -1)){ cfgs.push(d['config'][i]); } } for (i = 0; i < d['config'].length; i++){ if (cfgs.indexOf(d['config'][i]) == -1){ d['size'][i] = 5; d['colour'][i] = "grey"; d['alpha'][i] = 0.1; } else { d['size'][i] = 10; d['colour'][i] = "blue"; d['alpha'][i] = 0.9; } } for (i = 0; i < d2['config'].length; i++){ if (cfgs.indexOf(d2['config'][i]) == -1){ d2['colour'][i] = "grey"; d2['alpha'][i] = 0.0; } else { d2['colour'][i] = "blue"; d2['alpha'][i] = 0.9; } } for (j = 0; j < cols.length; j++){ d3[cols[j]] = []; } var cfgs_tbl = []; for (i = 0; i < d['config'].length; i++){ if ((cfgs.indexOf(d['config'][i]) != -1) && (cfgs_tbl.indexOf(d['config'][i]) == -1)){ cfgs_tbl.push(d['config'][i]); for (j = 0; j < cols.length; j++){ d3[cols[j]].push(d[cols[j]][i]) } } } s2.trigger('change'); s3.trigger('change'); dt.trigger('change'); ''' cb_code = cb_code.replace('__COLS__', '["' + '","'.join(cols) + '"]') s.callback = CustomJS(args={ 's2': s2, 's3': s3, 'dt': data_table }, code=cb_code) return Column(p1, data_table)
line_color="navy", source=source4, alpha=0.5) p4.y_range = Range1d(1200, 0) source.callback = CustomJS(args=dict(source4=source4), code=""" var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; var d2 = source4.data; d2['MappedFixationPointX'] = [[]] d2['MappedFixationPointY'] = [[]] for (var i = 0; i < inds.length; i++) { d2['MappedFixationPointX'][[i]] = d1['MappedFixationPointX'][inds[i]] d2['MappedFixationPointY'][[i]] = d1['MappedFixationPointY'][inds[i]] } d2['MappedFixationPointX'] = d2['MappedFixationPointX'].map(JSON.stringify).reverse().filter(function (e, i, a) { return a.indexOf(e, i+1) === -1; }).reverse().map(JSON.parse) // d2['MappedFixationPointY'] = d2['MappedFixationPointY'].map(JSON.stringify).reverse().filter(function (e, i, a) { return a.indexOf(e, i+1) === -1; }).reverse().map(JSON.parse) // d2['MappedFixationPointX'] = [].concat.apply([], d2['MappedFixationPointX']); d2['MappedFixationPointY'] = [].concat.apply([], d2['MappedFixationPointY']); source4.change.emit(); """) ################# ###Update data### #################
def plot_defects(render, stack, out_html_dir, args): tspecs = args[0] matches = args[1] dis_tiles = args[2] gap_tiles = args[3] seam_centroids = np.array(args[4]) stats = args[5] z = args[6] # Tile residual mean tile_residual_mean = cr.compute_mean_tile_residuals( stats['tile_residuals']) tile_positions = [] tile_ids = [] residual = [] for ts in tspecs: tile_ids.append(ts.tileId) pts = [] pts.append([ts.minX, ts.minY]) pts.append([ts.maxX, ts.minY]) pts.append([ts.maxX, ts.maxY]) pts.append([ts.minX, ts.maxY]) pts.append([ts.minX, ts.minY]) tile_positions.append(pts) try: residual.append(tile_residual_mean[ts.tileId]) except KeyError: residual.append(50) # a high value for residual for that tile out_html = os.path.join( out_html_dir, "%s_%d_%s.html" % (stack, z, datetime.datetime.now().strftime('%Y%m%d%H%S%M%f'))) output_file(out_html) xs = [] ys = [] alphas = [] for tp in tile_positions: sp = np.array(tp) x = list(sp[:, 0]) y = list(sp[:, 1]) xs.append(x) ys.append(y) alphas.append(0.5) fill_color = [] label = [] for t in tile_ids: if t in gap_tiles: label.append("Gap tiles") fill_color.append("red") elif t in dis_tiles: label.append("Disconnected tiles") fill_color.append("yellow") else: label.append("Stitched tiles") fill_color.append("blue") color_mapper = CategoricalColorMapper( factors=['Gap tiles', 'Disconnected tiles', 'Stitched tiles'], palette=["red", "yellow", "blue"]) source = ColumnDataSource(data=dict(x=xs, y=ys, alpha=alphas, names=tile_ids, fill_color=fill_color, labels=label)) seam_source = ColumnDataSource( data=dict(x=(seam_centroids[:, 0] if len(seam_centroids) else []), y=(seam_centroids[:, 1] if len(seam_centroids) else []), lbl=["Seam Centroids" for s in xrange(len(seam_centroids))])) TOOLS = "pan,box_zoom,reset,hover,tap,save" p = figure(title=str(z), width=1000, height=1000, tools=TOOLS, match_aspect=True) pp = p.patches('x', 'y', source=source, alpha='alpha', line_width=2, color={ 'field': 'labels', 'transform': color_mapper }, legend='labels') cp = p.circle('x', 'y', source=seam_source, legend='lbl', size=11) jscode = """ var inds = cb_obj.selected['1d'].indices; var d = cb_obj.data; var line = "<span style='float:left;clear:left;font_size=0.5pt'><br>" + d['%s'][inds[0]] + "</b></span>\\n"; var text = div.text.concat(line); var lines = text.split("\\n") if ( lines.length > 35 ) { lines.shift(); } div.text = lines.join("\\n"); """ div = Div(width=1000) layout = row(p, div) urls = "%s:%d/render-ws/v1/owner/%s/project/%s/stack/%s/tile/@names/withNeighbors/jpeg-image?scale=0.1" % ( render.DEFAULT_HOST, render.DEFAULT_PORT, render.DEFAULT_OWNER, render.DEFAULT_PROJECT, stack) taptool = p.select(type=TapTool) taptool.renderers = [pp] taptool.callback = OpenURL(url=urls) hover = p.select(dict(type=HoverTool)) hover.renderers = [pp] hover.point_policy = "follow_mouse" hover.tooltips = [("tileId", "@names"), ("x", "$x{int}"), ("y", "$y{int}")] source.callback = CustomJS(args=dict(div=div), code=jscode % ('names')) # add point match plot in another tab plot = point_match_plot(tspecs, matches) # montage statistics plots in other tabs stat_layout = plot_residual(xs, ys, residual) tabs = [] tabs.append(Panel(child=layout, title="Defects")) tabs.append(Panel(child=plot, title="Point match plot")) tabs.append(Panel(child=stat_layout, title="Mean tile residual")) plot_tabs = Tabs(tabs=tabs) save(plot_tabs) return out_html
s = ColumnDataSource(data=dict(x=x, y=y, color=color)) p = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here") p.circle('x', 'y', color='color', size=8, source=s, alpha=0.4) s2 = ColumnDataSource(data=dict(x=[0, 1], ym=[0.5, 0.5])) p.line(x='x', y='ym', color="orange", line_width=5, alpha=0.6, source=s2) s.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.selected['1d'].indices; var d = cb_obj.data; var ym = 0 if (inds.length == 0) { return; } for (i = 0; i < d['color'].length; i++) { d['color'][i] = "navy" } for (i = 0; i < inds.length; i++) { d['color'][inds[i]] = "firebrick" ym += d['y'][inds[i]] } ym /= inds.length s2.data['ym'] = [ym, ym] cb_obj.trigger('change'); s2.trigger('change'); """) show(p)
tools="lasso_select", title="Select Here") p1.circle('x', 'y', source=s1, alpha=0.6) s2 = ColumnDataSource(data=dict(x=[], y=[])) p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1), tools="", title="Watch Here") p2.circle('x', 'y', source=s2, alpha=0.6) s1.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = s2.get('data'); d2['x'] = [] d2['y'] = [] for (i = 0; i < inds.length; i++) { d2['x'].push(d1['x'][inds[i]]) d2['y'].push(d1['y'][inds[i]]) } s2.trigger('change'); """) layout = row(p1, p2) show(layout)
x = [random() for x in range(500)] y = [random() for y in range(500)] s1 = ColumnDataSource(data=dict(x=x, y=y)) p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here") p1.circle("x", "y", source=s1, alpha=0.6) s2 = ColumnDataSource(data=dict(x=[], y=[])) p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1), tools="", title="Watch Here") p2.circle("x", "y", source=s2, alpha=0.6) s1.callback = Callback( args=dict(s2=s2), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = s2.get('data'); d2['x'] = [] d2['y'] = [] for (i = 0; i < inds.length; i++) { d2['x'].push(d1['x'][inds[i]]) d2['y'].push(d1['y'][inds[i]]) } s2.trigger('change'); """, ) layout = hplot(p1, p2) show(layout)
output_file("callback.html") x = [random() for x in range(500)] y = [random() for y in range(500)] s1 = ColumnDataSource(data=dict(x=x, y=y)) p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here") p1.circle('x', 'y', source=s1, alpha=0.6) s2 = ColumnDataSource(data=dict(x=[], y=[])) p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1), tools="", title="Watch Here") p2.circle('x', 'y', source=s2, alpha=0.6) s1.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.selected.indices; var d1 = cb_obj.data; var d2 = s2.data; d2['x'] = [] d2['y'] = [] for (var i = 0; i < inds.length; i++) { d2['x'].push(d1['x'][inds[i]]) d2['y'].push(d1['y'][inds[i]]) } s2.change.emit(); """) layout = row(p1, p2) show(layout)
def scatter2D( x, y, z, vmin=None, vmax=None, cmax='red', cmin='blue', cundef='grey', cpalete_size=250, ctransform='log', radii=1., lcolor='black', title='Scatterplot', TOOLS='resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select', xlabel='X', ylabel='Y', alpha=0.8, outvar='__inds'): """Plot a 2D scaterplot, e.g. data location with bokeh, and allows manual selection of data. Parameters ---------- x, y : array of floats. Coordinates z : array of floats. Variable for color vmin, vmax : floats (Optional, default None, that is data max and min). Value minimum and maximum for colour scale. cmax : string with valid bokeh colour (Optional, default 'red'). Color corresponding to vmax or above cmin : string with valid bokeh colour (Optional, default 'blue'). Color corresponding to vmin cundef : string with valid bokeh colour (Optional, default 'grey'). Color corresponding to values < vmin and undefined cpalete_size : integer (Optional default 250). Size of the colour palete/scale ctransform : string ( Optional, default 'log'). If == 'log' will use log scale to asign colors, otherwise will use linear scale radii : float (Optional, default 1.). Size of the circles plotted lcolor: string (ptional, default 'black'). Color of the circle outline. title: string (Optional, default 'Scatterplot'). Title of the plot TOOLS: string (Optional, default 'resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select'). Tools shown in the plot. xlabel: string (Optional, default 'X'). X axis label ylabel: string (Optional, default 'Y'). Y axis label alpha: float (Optional, default 0.8). Transparency of circles background outvar: string (Optional, default '__inds'). Name of the callback variable. Each time you select points in the scatterplot this variable will be updated. Note ------- This function will create a new variable defined by outvar, e.g. '__inds', the firts time you select points. This variable will be overwritten if it already exist. This variable may be overwritten by selecting points in a different plot with same value in the parameter outvar. The selection variable is updated in your python kernel from a javascript (web brouser) using a calling back function. Example ------- >>> pygslib.plothtml.scatter2D( # data x= mydata['Xlocation'], y= mydata['Ylocation'], z= mydata['Primary'], # vmin and max for formating vmin=0.1, vmax=30, # parameters for color cmax = 'red', cmin = 'blue', cundef='grey', cpalete_size=300, ctransform = 'linear', #parameter for circle size radii=0.5, # line color lcolor= 'black', # parameters for plot title='Scatterplot', TOOLS='resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select', xlabel='X', ylabel='Y', alpha=0.9, # call back variable outvar='__inds') >>> print " Select some datapoints, e.g. using the tool lasso " >>> print mydata.iloc[__inds,:] """ #prepare data df = pd.DataFrame({'x': x, 'y': y, 'z': z}) df['radii'] = radii df['alpha'] = alpha df['lcolor'] = lcolor if vmin is None: vmin = z.min() if vmax is None: vmax = z.max() assert vmin < vmax source = ColumnDataSource(df) # create a palete. colourscale = pygslib.charttable.Leyend_num(vmin=vmin, vmax=vmax, cmax=cmax, cmin=cmin, undef=cundef, nval=cpalete_size, convert='HEX') palete = colourscale.c.tolist() # create a mapper if ctransform == 'log': assert vmin > 0 and vmax > 0, "vmin/vmax <=0 and using log transform" mapper = LogColorMapper(palette=palete, low=vmin, high=vmax) else: mapper = LinearColorMapper(palette=palete, low=vmin, high=vmax) mapper.low_color = cundef mapper.high_color = cmax # create figure p = bkplt.figure(title=title, tools=TOOLS, toolbar_location='above') p.xaxis.axis_label = xlabel p.yaxis.axis_label = ylabel # colorbar color_bar = ColorBar(color_mapper=mapper, location=(0, 0)) # plot p.scatter(source=source, x="x", y="y", radius='radii', fill_color={ 'field': 'z', 'transform': mapper }, fill_alpha='alpha', line_color='lcolor') p.add_layout(color_bar, 'right') source.callback = CustomJS(args=dict(p=p), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); console.log(d1) var kernel = IPython.notebook.kernel; IPython.notebook.kernel.execute("{} = " + inds); """.format(outvar)) bkplt.show(p)
freq_sources[site][site].callback = CustomJS(args = dict(s2=freq_sources[site][SELECT],dt=data_table,scor=freq_cor_sources[site]), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = s2.get('data'); var tab = dt.get('source').get('data'); var dcor = scor.get('data'); var difm = 0; var difm2 = 0; var scat = 0; var ym1 = 0; var ym2 = 0; var T1 = 0; var T2 = 0; var T3 = 0; dcor['x'] = []; dcor['y'] = []; tab['N']["""+str(count)+"""] = inds.length; if (inds.length == 0) { tab['RMS']["""+str(count)+"""] = 0; tab['Bias']["""+str(count)+"""] = 0; tab['Scatter']["""+str(count)+"""] = 0; tab['R']["""+str(count)+"""] = 0; dt.change.emit(); return; } for (i=0; i < inds.length; i++){ difm += d1['y'][inds[i]] - d2['y'][inds[i]]; difm2 += Math.pow(d1['y'][inds[i]] - d2['y'][inds[i]],2); ym1 += d1['y'][inds[i]]; ym2 += d2['y'][inds[i]]; dcor['x'].push(d2['y'][inds[i]]); dcor['y'].push(d1['y'][inds[i]]); } difm /= inds.length; difm2 /= inds.length; ym1 /= inds.length; ym2 /= inds.length; for (i=0; i < inds.length; i++){ scat += Math.pow(d1['y'][inds[i]] - d2['y'][inds[i]] - difm,2); } for (i=0; i < inds.length; i++){ T1 += (d1['y'][inds[i]] - ym1)*(d2['y'][inds[i]] - ym2); T2 += Math.pow(d1['y'][inds[i]] - ym1,2); T3 += Math.pow(d2['y'][inds[i]] - ym2,2); } tab['RMS']["""+str(count)+"""] = Math.sqrt(difm2).toFixed("""+prec+"""); tab['Bias']["""+str(count)+"""] = difm.toFixed("""+prec+"""); tab['Scatter']["""+str(count)+"""] = Math.sqrt(scat/(inds.length -1)).toFixed("""+prec+"""); tab['R']["""+str(count)+"""] = (T1/Math.sqrt(T2*T3)).toFixed("""+prec+"""); dt.change.emit(); scor.change.emit(); """)