def generate_km_plot(kms, models, color_dict): surv_plt1 = figure(title="Survival Analysis", tools=['hover,box_zoom,wheel_zoom,save,reset']) hover = surv_plt1.select(dict(type=HoverTool)) hover.tooltips = [("Model ", "@model"), ("Time ", "@timeline"), ("survival fraction ", "@km_surv"), ("upper 95% bound ", "@surv_upper"), ("lower 95% bound ", "@surv_lower")] if len(kms) > 1: hover.mode = 'mouse' else: hover.mode = 'vline' km_colors = [ '#1a1334', '#03c383', '#fbbf45', '#ed0345', '#26294a', '#aad962', '#01545a', '#ef6a32', '#017351', '#a12a5e', '#710162', '#110141' ] #km_colors = [str(color_dict[model_key]) if model_key in color_dict else 'grey' for model_key in models] n = 0 surv_plt_objs1 = [] surv_plt_objs2 = [] for km in kms: model = models[n] time = km['time'] surv = km['surv'] surv_upper = km['surv_lower'] surv_lower = km['surv_upper'] band_x = np.append(time, time[::-1]) band_y = np.append(surv_upper, surv_lower[::-1]) source = ColumnDataSource(data=dict(timeline=[i for i in time], km_surv=[i for i in surv], model=[models[n] for i in time], surv_lower=[i for i in surv_lower], surv_upper=[i for i in surv_upper])) tmp = surv_plt1.patch(band_x, band_y, color=km_colors[n], fill_alpha=0.2) surv_plt_objs1.append(tmp) tmp = surv_plt1.line('timeline', 'km_surv', line_width=2, alpha=.8, source=source, legend=models[n], color=km_colors[n]) surv_plt_objs2.append(tmp) n += 1 surv_plt1.xaxis.axis_label = 'Time (Years)' surv_plt1.yaxis.axis_label = 'Kaplan-Meier Estimation (survival fraction)' # grid styles surv_plt1.toolbar.logo = None surv_plt1.grid.grid_line_alpha = 0 surv_plt1.ygrid.band_fill_color = None #"grey" surv_plt1.ygrid.band_fill_alpha = 0.1 surv_plt1.x_range.range_padding = 0 surv_plt1.legend.location = "bottom_left" surv_plt1.plot_height = 700 surv_plt1.plot_width = 790 #surv_plt1.border_fill_color = "black" #surv_plt1.background_fill_color = "white" surv_plt1.min_border_left = 80 surv_plt1.outline_line_width = 1 surv_plt1.outline_line_alpha = 0.3 surv_plt1.xaxis.minor_tick_line_color = None #'N' surv_plt1.yaxis.minor_tick_line_color = None #'#C1C1C1' #surv_plt1.xaxis.major_label_text_color = 'white' surv_plt1.y_range = Range1d(0.0, 1.02) code1 = '''\ object1.visible = toggle.active object2.visible = toggle.active ''' #Toggle possible attributes are: # active, button_type, callback, css_classes, disabled, height, icon, js_callbacks, label, name, sizing_mode, tags or width callbacks = [] toggles = [] for i in range(len(models)): callbacks.append(CustomJS.from_coffeescript(code=code1, args={})) km_color = km_colors[i].lstrip("#") style_class = ('bk-' + km_color, 'bk-' + km_color) toggles.append( Toggle(label=models[i], button_type="success", callback=callbacks[i], css_classes=style_class)) callbacks[i].args = { 'toggle': toggles[i], 'object1': surv_plt_objs1[i], 'object2': surv_plt_objs2[i] } togs = [t for t in list(toggles)] controls = widgetbox(togs, width=190) layout = row(controls, surv_plt1) return layout
xdr = DataRange1d() ydr = DataRange1d() plot = Plot(x_range=xdr, y_range=ydr) circle = Circle(x="x", y="y", radius=0.2, fill_color="color", line_color="black") circle_renderer = plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') customjs = CustomJS.from_coffeescript(args=dict(source=source), code=""" Util = require "util/util" data = source.data for i in Util.get_indices(source) color = data['color'][i] window.alert("Selected color: #{color}") """) tap = TapTool(renderers=[circle_renderer], callback=customjs) plot.add_tools(PanTool(), WheelZoomTool(), tap) doc = Document() doc.add_root(plot) if __name__ == "__main__": doc.validate() filename = "customjs.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Demonstration of custom callback written in CoffeeScript"))
circle = Circle(x="x", y="y", radius=0.2, fill_color="color", line_color="black") circle_renderer = plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') customjs = CustomJS.from_coffeescript(args=dict(source=source), code=""" Util = require "util/util" data = source.data for i in Util.get_indices(source) color = data['color'][i] window.alert("Selected color: #{color}") """) tap = TapTool(renderers=[circle_renderer], callback=customjs) plot.add_tools(PanTool(), WheelZoomTool(), tap) doc = Document() doc.add_root(plot) if __name__ == "__main__": doc.validate() filename = "customjs.html" with open(filename, "w") as f:
plot = Plot() circle = Circle(x="x", y="y", radius=0.2, fill_color="color", line_color="black") circle_renderer = plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') customjs = CustomJS.from_coffeescript(args=dict(source=source), code=""" import {get_indices} from "core/util/selection" for i in get_indices(source) color = source.data['color'][i] window.alert("Selected color: #{color}") """) tap = TapTool(renderers=[circle_renderer], callback=customjs) plot.add_tools(PanTool(), WheelZoomTool(), tap) doc = Document() doc.add_root(plot) if __name__ == "__main__": doc.validate() filename = "customjs.html" with open(filename, "w") as f: f.write(
def generate_summary_plot(): #x = [x*0.005 for x in range(0, 200)] #y = x x = np.arange(0, 10) #x = [x*0.005 for x in range(0, 200)] y = [i*i for i in x] z = [i*3.0 for i in x] source = ColumnDataSource(data=dict(x=x, y=y)) source2 = ColumnDataSource(data=dict(x=x, z=z)) print(source) print(dict(source=source)) summary_fig = figure(x_axis_location='above', plot_width=400, plot_height=400) line_one = summary_fig.line('x', 'y', source=source, line_width=3, line_alpha=0.6)#, xaxis.axis_label = 'initalized')#,axis.axis_label='title') line_two = summary_fig.line('x', 'z', source=source2, line_width=3, line_alpha=0.6)#, xaxis.axis_label = 'initalized')#,axis.axis_label='title') #selector_options=['alpha','beta','gamma'] #selector1=Select(title='X-Axis',value=selector_options[0],options=selector_options, callback = callback) #summary_fig.xaxis.visible = None xaxis = LinearAxis(axis_label="Initial x-axis label") summary_fig.add_layout(xaxis, 'below') #summary_fig.xaxis.axis_label = 'Time (Years)' #selector1.on_change('value',callback) #plot.yaxis.axis_label = y_title #controls = widgetbox([selector1], width=200) code1 = '''\ object.visible = toggle.active ''' code2 = '''\ object.visible = radio_button.active ''' #var f = cb_obj.value callback2 = CustomJS(args=dict(source=source2), code=""" var data = source.data; var f = cb_obj.value x = data['x'] z = data['z'] if (f == "alpha") { for (i = 0; i < x.length; i++) { z[i] = Math.pow(x[i], 3.)} } else if (f = "beta") { for (i = 0; i < x.length; i++) { z[i] = 20} } source.trigger('change'); """) callback3 = CustomJS(args=dict(xaxis=xaxis), code=""" var f = cb_obj.value xaxis.attributes.axis_label = f xaxis.trigger('change'); """) callback4 = CustomJS(args=dict(source=source2), code=""" var data = source.data; var f = cb_obj.value x = data['x'] z = data['z'] if (f == "A alpha") { for (i = 0; i < x.length; i++) { z[i] = Math.pow(x[i], 3.)} } else if (f = "B alpha") { for (i = 0; i < x.length; i++) { z[i] = 20} } source.trigger('change'); """) callback1 = CustomJS.from_coffeescript(code=code1, args={}) toggle1 = Toggle(label="Green Box", button_type="success", callback=callback1) callback1.args = {'toggle': toggle1, 'object': line_one} #callback2 = CustomJS.from_coffeescript(code=code2, args={}) radio_button_group = RadioButtonGroup(labels=['alpha','beta','gamma'], active=0)# callback=callback2) radio_button_group.js_on_change('value', callback2) #print([method for method in dir(radio_button_group)]) #callback2.args = {'radio_button': radio_button_group, 'object': line_two} #output_file("styling_visible_annotation_with_interaction.html") selector_options = ['A alpha', 'B beta', 'G gamma'] selector1 = Select(title='X-Axis', value=selector_options[0], options=selector_options)#, callback=callback4) selector1.js_on_change('value', callback4) selector2_options = ['Aa alpha', 'Ba beta', 'Ga gamma'] selector2 = Select(title='X-Axis', value=selector_options[0], options=selector2_options)#, callback=callback4) selector2.js_on_change('value', callback3) #slider = Slider(start=0.1, end=4, value=1, step=.1, title="power") #slider.js_on_change('value', callback) controls = widgetbox([toggle1,radio_button_group, selector1, selector2], width=200) layout = row(controls, summary_fig) return layout
xdr = DataRange1d() ydr = DataRange1d() plot = Plot(x_range=xdr, y_range=ydr) circle = Circle(x="x", y="y", radius=0.2, fill_color="color", line_color="black") circle_renderer = plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), "below") plot.add_layout(LinearAxis(), "left") customjs = CustomJS.from_coffeescript( args=dict(source=source), code=""" import {get_indices} from "core/util/selection" for i in get_indices(source) color = source.data['color'][i] window.alert("Selected color: #{color}") """, ) tap = TapTool(renderers=[circle_renderer], callback=customjs) plot.add_tools(PanTool(), WheelZoomTool(), tap) doc = Document() doc.add_root(plot) if __name__ == "__main__": doc.validate() filename = "customjs.html" with open(filename, "w") as f: