def _setup_map(self): """""" pan = PanTool() save = SaveTool() tap = TapTool() lasso = LassoSelectTool() reset = ResetTool() wheel = WheelZoomTool() tooltips = HoverTool(tooltips=[("Station", "@STATN"), ("Lat", "@LATIT_DD"), ("Lon", "@LONGI_DD")]) # range bounds supplied in web mercator coordinates self.map = figure( x_range=(0, 4000000), y_range=(7100000, 9850000), x_axis_type="mercator", y_axis_type="mercator", sizing_mode='stretch_both', tools=[pan, wheel, tap, lasso, tooltips, reset, save], height=300, width=500, ) # self.map.yaxis.axis_label = ' ' # in order to aline y-axis with figure window below self.map.toolbar.active_scroll = self.map.select_one(WheelZoomTool) self.map.add_tile(self.tile_provider) tap.callback = station_callback( plot_source=self.plot_source, data_source=self.data_source, text_source=self.text, station_source=self.position_source, text_input_list=self.text_inputs, )
def generate_plot(title, cds, x, y, tooltip): plot = figure(x_axis_type="datetime", title=title, tooltips=tooltip) plot.circle(x=x, y=y, source=cds, size=8) taptool = TapTool() # taptool.callback = OpenURL(url='http://www.dilbert.com/strip/@{AAPL_p}') # craft URL by hand, as flask escapes all the symbols - so bokeh doesnt interpolate taptool.callback = OpenURL(url="/display/@{AAPL_p}") plot.add_tools(taptool) script, div = components(plot) return {'script': script, 'div': div}
def draw_plot(G, query, expr): plot = Plot( x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1), sizing_mode="stretch_both") plot.title.text = "Relationship Graph for: " + expr tooltips = """ <div style="max-width : 300px"> <div><span style="">@type</span></div> <div><span style="font-weight: bold;">@title</span></div> <div><span style="">@authors</span></div> <div><span style="font-weight: bold;">@journal</span></div> <div><span style="font-weight: bold;">@year</span></div> <div><span style=""><a href="https://doi.org/@DOI">@DOI</a></span></div> </div> """ node_hover_tool = HoverTool(tooltips=tooltips) zoom_tool = WheelZoomTool() tap_tool_open = TapTool() #tap_tool_open.callback = OpenURL(url='https://doi.org/@DOI') #tap_tool_open.callback = OpenURL( # same_tab=True, # url="javascript:document.getElementById('showpaper').innerHTML='" + tooltips + "';") #tap_tool_open.callback = CustomJS( # args=dict(source=0, selected=0), # code = """document.getElementById('showpaper').innerHTML='""" + ''.join(tooltips.splitlines()) + "';") showpaper = """ <div style="max-width : 80%"> <div><span style="">@type</span></div> <div><span style="font-weight: bold;">@title</span></div> <div><span style="">@authors</span></div> <div><span style="font-weight: bold;">@journal</span></div> <div><span style="font-weight: bold;">@year</span></div> <div><span style=""><a target="_blank" href="https://doi.org/@DOI">@DOI</a></span></div> </div> """ code = ''' if (cb_data.source.selected.indices.length > 0){ var selected_index = cb_data.source.selected.indices[0]; var tooltip = document.getElementById("showpaper"); cb_data.source.data.color[selected_index] = 'grey' tp = tp.replace('@type', cb_data.source.data.type[selected_index]); tp = tp.replace('@title', cb_data.source.data.title[selected_index]); tp = tp.replace('@authors', cb_data.source.data.authors[selected_index]); tp = tp.replace('@journal', cb_data.source.data.journal[selected_index]); tp = tp.replace('@year', cb_data.source.data.year[selected_index]); tp = tp.replace('@DOI', cb_data.source.data.DOI[selected_index]); tp = tp.replace('@DOI', cb_data.source.data.DOI[selected_index]); tooltip.innerHTML = tp; } ''' tap_tool_open.callback = CustomJS( args = {'tp': showpaper}, code = code) tap_tool = TapTool() plot.add_tools( node_hover_tool, zoom_tool, # BoxZoomTool(), ResetTool(), tap_tool_open, tap_tool ) plot.toolbar.active_scroll = zoom_tool graph_renderer = from_networkx( G, nx.spring_layout, scale=1, center=(0, 0), seed=12345) # normal graph_renderer.node_renderer.glyph = Circle( size="size", fill_color="color") graph_renderer.edge_renderer.glyph = MultiLine( line_alpha=0.2) # selection graph_renderer.node_renderer.selection_glyph = Circle( fill_color="color", fill_alpha=1, line_alpha=1) graph_renderer.edge_renderer.selection_glyph = MultiLine( line_width=3, line_alpha=1) graph_renderer.node_renderer.nonselection_glyph = Circle( fill_color="color", fill_alpha=0.5, line_alpha=0.5) graph_renderer.edge_renderer.nonselection_glyph = MultiLine( line_alpha=0.2) # hover graph_renderer.node_renderer.hover_glyph = Circle( fill_color='#abdda4') graph_renderer.edge_renderer.hover_glyph = MultiLine( line_color='#abdda4', line_width=3) graph_renderer.inspection_policy = NodesAndLinkedEdges() graph_renderer.selection_policy = NodesAndLinkedEdges() plot.renderers.append(graph_renderer) script, div = components(plot) return script, div
def url_tool(url_column1, url_column2): url = f"http://*****:*****@{url_column1}&other=@{url_column2}" taptool = TapTool() taptool.callback = OpenURL(url=url) return taptool
controls = [min_year, max_year, search_player] for control in controls: control.on_change('value', lambda attr, old, new: update()) country_select.on_click(update_wrapped) clear_button.on_click(update_click_wrapped) controls += [country_select] controls.insert(3, clear_button) sizing_mode = 'fixed' # 'scale_width' also looks nice with this example # Add Circle tooltips on hover hover = HoverTool(tooltips=[("Team", "@team"), ("Total", "@war")], renderers=[circles]) click = TapTool(renderers=[circles]) click.callback = CustomJS(args={ 'source': source, 'circle': circles.data_source, 'table_source': table_source }, code=click_circle_callback) table_source.on_change('selected', lambda attr, old, new: update('table_select')) p.add_tools(hover, click) inputs = widgetbox(*controls, sizing_mode=sizing_mode) lay_out = layout([[desc], [inputs, p, data_table]], sizing_mode=sizing_mode) update() # initial load of the data curdoc().title = "La Vida WAR Visualizer"
def process_file(self): potiron_path = potiron.potiron_path lentwo = False ck = self.red.sismember('CK', 'YES') for v in self.fieldvalues: # if any field value is in format 'value-protocol' (or 'value-all'), it means we want to display each protocol separatly if len(v.split('-')) >= 2: lentwo = True # Using the format 'value-protocol' is not possible if combined keys are not deployed in the current redis database if lentwo and not ck: sys.stderr.write('Combined keys are not used in this redis dataset') sys.exit(1) if not self.outputdir.endswith('/'): self.outputdir = "{}/".format(self.outputdir) if not os.path.exists(self.outputdir): os.makedirs(self.outputdir) # Definition of the protocols currently present in our dataset protocols = self.red.smembers('PROTOCOLS') # Define the strings used for legends, titles, etc. concerning fields field_string, field_in_file_name = field2string(self.field, potiron_path) field_data = create_dict(self.field, potiron_path) all_proto = False for fv in self.fieldvalues: v = fv.split('-') # If we want to display the values for all the procotols, we display the sum of all of them as well if len(v) >= 2 and (v[1] == '*' or v[1] == 'all'): all_proto = True self.fieldvalues.append(v[0]) # Creation of the figure and the tools used on it namefile=self.output_name(field_in_file_name,lentwo, all_proto) # As displaying values for all the protocols may generate a lot of lines in the plot, # We help users showing them the protocol when they have there cursor in the line if all_proto: hover = HoverTool(tooltips = [('count','@y'),('protocol','@prot')]) else: hover = HoverTool(tooltips = [('count','@y')]) taptool = TapTool() TOOLS = [hover,PanTool(),BoxZoomTool(),WheelZoomTool(), taptool, SaveTool(), ResetTool()] p = figure(width=self.plot_width,height=self.plot_height,tools=TOOLS) # Definition of some variables which will be used and modified with the iterations at_least_one = False days = calendar.monthrange(int(self.date[0:4]),int(self.date[4:6]))[1] maxVal = 0 minVal = sys.maxsize maxDay = 0 vlength = len(self.fieldvalues) actual_values = [] nbLine = 0 # day_string = "@x" for v in range(vlength): # For each selected field or occurrence value = self.fieldvalues[v].split('-') actual_field = value[0] if len(value) >= 2: # If we specified a or all protocol(s) protocol = value[1] if protocol == "*" or protocol == "all": for prot in protocols: score=[] dayValue=[] proto = prot.decode() exists = False keys = self.red.keys("{}:{}:{}*:{}".format(self.source,proto,self.date,self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore(redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: at_least_one = True # We define the color of the line, draw it color = palette[nbLine%10] protos = [] for x in dayValue: protos.append("{}_{}".format(x, proto)) prots = [proto] * len(score) sourceplot = ColumnDataSource(data=dict( x = dayValue, y = score, protocol = protos, prot = prots )) leg = def_legend(actual_field, proto, self.field, field_string, field_data) p.line(x='x',y='y',legend=leg,line_color=color,line_width=2,source=sourceplot) c = p.scatter(x='x',y='y',legend=leg,size=10,color=color,alpha=0.1,source=sourceplot) taptool.renderers.append(c) # In order to have the interaction on click nbLine += 1 maxScore = max(score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}-{}".format(actual_field, protocol) actual_values.append(actual_value) else: score=[] dayValue=[] exists = False keys = self.red.keys("{}:{}:{}*:{}".format(self.source,protocol,self.date,self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore(redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: # If at least one occurrence for the current value of field has been found at_least_one = True # We define the color of the line, draw it color = palette[nbLine%10] leg = def_legend(actual_field, protocol, self.field, field_string, field_data) p.line(x=dayValue,y=score,legend=leg,line_color=color,line_width=2) c = p.scatter(x=dayValue,y=score,legend=leg,size=10,color=color,alpha=0.1) taptool.renderers.append(c) # In order to have the interaction on click nbLine += 1 maxScore = max(score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}-{}".format(actual_field, protocol) actual_values.append(actual_value) else: # on the other case, we don't split informations for each protocol score=[] dayValue=[] exists = False # If combined keys are used, we must by the way take data from all the keys (i.e for each protocol) if ck: for d in range(1,days+1): # For each day with data stored in redis exists_day = False day = format(d, '02d') countValue = 0 keys = self.red.keys("{}:*:{}{}:{}".format(self.source,self.date,day,self.field)) for k in keys: redisKey = k.decode() tmpscore = self.red.zscore(redisKey, actual_field) countValue += tmpscore if tmpscore is not None else 0 exists_day = True if exists_day: if countValue > 0: exists = True score.append(countValue) dayValue.append(day) else: # When combined keys are not used, we only need to read the scores for each day keys = self.red.keys("{}:{}*:{}".format(self.source,self.date,self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore(redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: # If at least one occurrence for the current value of field has been found at_least_one = True # We define the color of the line, draw it color = palette[nbLine%10] leg = def_legend(actual_field, None, self.field, field_string, field_data) if all_proto: protos = [] for x in dayValue: protos.append(x) prots = ['all protocols'] * len(score) sourceplot = ColumnDataSource(data=dict( x = dayValue, y = score, protocol = protos, prot = prots )) p.line(x='x',y='y',legend=leg,line_color=color,line_width=2,source=sourceplot) c = p.scatter(x='x',y='y',legend=leg,size=10,color=color,alpha=0.1,source=sourceplot) else: p.line(x=dayValue,y=score,legend=leg,line_color=color,line_width=2) c = p.scatter(x=dayValue,y=score,legend=leg,size=10,color=color,alpha=0.1) taptool.renderers.append(c) # In order to have the interaction on click nbLine += 1 maxScore = max(score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}".format(actual_field) actual_values.append(actual_value) if at_least_one: # If at least one value has been found in redis with our selection if lentwo: # Defines the name of the files to call with a click on a point in the plot taptool.callback = OpenURL(url="{}_{}_with-protocols_{}-{}[email protected]".format(self.source, field_in_file_name,self.date[0:4],self.date[4:6])) else: taptool.callback = OpenURL(url="{}_{}_{}-{}[email protected]".format(self.source, field_in_file_name,self.date[0:4],self.date[4:6])) output_file("{}.html".format(namefile), title=namefile.split("/")[-1]) # Definition of some parameters of the graph fieldvalues_string = plot_annotation(self.field, potiron_path, actual_values, field_string, field_data) p.title.text = "Number of {} {}seen each day in {} {}".format(field_string, fieldvalues_string, potiron.year[self.date[4:6]], self.date[0:4]) p.yaxis[0].formatter = BasicTickFormatter(use_scientific=False) p.xaxis.axis_label = "Days" p.yaxis.axis_label = "Count" p.legend.location = "top_left" p.legend.click_policy = "hide" # Definition of some parameters for the logo with Image.open(self.logofile) as im : im_width, im_height = im.size xdr = maxDay + 1 upper_space = 10 if nbLine > 2: upper_space *= (nbLine / 2) ydrmax = maxVal + maxVal * upper_space / 100 ydrmin = minVal - maxVal * 5 / 100 p.x_range = Range1d(0,xdr) p.y_range = Range1d(ydrmin,ydrmax) height = (ydrmax - ydrmin) / self.logo_y_scale width = xdr / ((self.logo_y_scale * im_height * self.plot_width) / (im_width * self.plot_height)) p.image_url(url=[self.logofile],x=[xdr],y=[ydrmax-ydrmax*2/100],w=[width],h=[height],anchor="top_right") # Process the graph save(p) if self.links: export_csv = export_csv_all_days_per_month.Export_Csv(self.red, self.source, self.date, self.field, 10, ['-1'], self.outputdir, True, True, self.logofile, ck, lentwo) ck = True if red.sismember('CK', 'YES') else False export_csv.process_all_files() else: print ("There is no such value for a {} you specified: {}".format(field_string,self.fieldvalues))
<div> <img src="@imgs" height="@imgs_ht" alt="@imgs" width="@imgs_wd" style="float: left; margin: 0px 15px 15px 0px;" border="1" ></img> </div> <div> <img src="@flag" height="@flag_ht" alt="@imgs" width="@flag_wd" ></img> <span style="font-size: 14px; font-weight: bold;">@name</span> </div> <div> <span style="font-size: 10px: bold;">@addr</span> </div> </div> """) wheel_zoom = WheelZoomTool() tap = TapTool() tap.renderers = [] tap.callback = OpenURL(url='@embassy_url') plot.add_tools(PanTool(), wheel_zoom, hover, tap, ResetTool(), SaveTool()) plot.toolbar.active_scroll = wheel_zoom ### Export show(plot)
tooltips=tooltips) else: tooltips = [('Label', "@Term"), ('Score model', '@Score1'), ('Namespace', '@Namespace'), ('Description', '@Description')] if args.importance: tooltips = [('Label', "@Term"), ('Score model', '@Score1'), ('Importance', '@Importance'), ('Namespace', '@Namespace'), ('Description', '@Description')] hoverNodes = HoverTool(renderers=[nodes1], tooltips=tooltips) plot.add_tools(hoverNodes) hoverEdges = HoverTool(renderers=[edges_r], tooltips=[('Parent', "@Term1"), ('Child', "@Term2"), ('Type', '@Type')]) plot.add_tools(hoverEdges) #Create a taptool that opens the gene ontology website with the clicked GO term url = "http://amigo2.berkeleybop.org/amigo/term/@Term/" taptool.callback = OpenURL(url=url) plot.add_tools(taptool) #Show parents and children on select in the visualiser and in the text fields showlinks = ColumnDataSource({'x0': [], 'y0': [], 'x1': [], 'y1': []}) showSelected = ColumnDataSource({'x': [], 'top': [], 'bottom': []}) sr = plot.segment(x0='x0', y0='y0', x1='x1', y1='y1', color='olive', alpha=0.6, line_width=2, source=showlinks) sn = plot.vbar(x ="x", top='top', bottom='bottom', width=1, color='red', alpha=0.5, source=showSelected) parentlist = TextInput(value="", title="Parents:") childrenlist = TextInput(value="", title="Children:") selectedTerm = TextInput(value="", title="Selected term:") ancestryTool = TapTool() plot.add_tools(ancestryTool) #Table if args.models == 2: columns = [ TableColumn(field="Term", title="Term"),
color = palette[nbLine%10] protos = [proto] * days sourceplot = ColumnDataSource(data=dict( x = dayValue, y = score, protocol = protos )) leg = def_legend(actual_field, proto, field, field_string, field_data) p.line(x='x',y='y',legend=leg,line_color=color,line_width=2,source=sourceplot) c = p.scatter(x='x',y='y',legend=leg,size=10,color=color,alpha=0.1,source=sourceplot) # p.line(x=dayValue,y=score,legend=leg,line_color=color,line_width=2) # c = p.scatter(x=dayValue,y=score,legend=leg,size=10,color=color,alpha=0.1) day_string = "@x" taptool.renderers.append(c) # In order to have the interaction on click # proto = "@protocol" taptool.callback = OpenURL(url="{}_{}_{}-{}-{}.html".format(source,field_in_file_name,date[0:4],date[4:6],day_string)) nbLine += 1 maxScore = max(score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}-{}".format(actual_field, protocol) actual_values.append(actual_value) else: score=[] dayValue=[]
def _setup_map(self): """Initiate the map object including all the connected interactivity.""" pan = PanTool() save = SaveTool() tap = TapTool() lasso = LassoSelectTool() reset = ResetTool() wheel = WheelZoomTool() tooltips = HoverTool(tooltips=[("Station", "@STATION"), ("Serie", "@KEY")]) # range bounds supplied in web mercator coordinates self.map = figure( x_range=(0, 4000000), y_range=(7100000, 9850000), x_axis_type="mercator", y_axis_type="mercator", plot_height=420, plot_width=1000, tools=[pan, wheel, tap, lasso, tooltips, reset, save]) self.map.yaxis.axis_label = ' ' # in order to aline y-axis with figure window below self.map.xgrid.grid_line_color = None self.map.ygrid.grid_line_color = None self.map.toolbar.active_scroll = self.map.select_one(WheelZoomTool) self.map.add_tile(self.tile_provider) station_data_callback = cbs.station_callback( position_source=self.position_plot_source, data_source=self.data_source, figures=self.figures, seconds=self.seconds, pmap=self.plot_parameters_mapping) tap.callback = station_data_callback # When we mark stations on the map using lasso selection, we activate the TS-diagram. lasso_callback = cbs.lasso_callback(monthly_keys=self.monthly_keys, in_data=self.ts_source, plot_data=self.ts_plot_source, x_range=self.ts.x_range, y_range=self.ts.y_range) station_data_callback_2 = cbs.station_callback( position_source=self.position_plot_source, data_source=self.data_source, figures=self.figures, seconds=self.seconds, pmap=self.plot_parameters_mapping) comnt_callback = cbs.comnt_callback( position_source=self.position_plot_source, comnt_obj=self.comnt_visit, single_select=1) comnt_samp_callback = cbs.comnt_samp_callback( position_source=self.position_plot_source, data_source=self.data_source['main_source'], comnt_obj=self.comnt_samp, comnt_selector=self.comnt_samp_selector, single_select=1, ) update_slider_callback = cbs.range_slider_update_callback( slider=self.pressure_slider, data_source=self.data_source['main_source'], ) select_button_type_callback = cbs.change_button_type_callback( button=self.select_all_button, btype='default') lasso_callback.args["month"] = self.month_selector self.position_plot_source.selected.js_on_change( 'indices', lasso_callback, station_data_callback_2, comnt_callback, update_slider_callback, select_button_type_callback, comnt_samp_callback, )
def create_figures(source, source_columns, fig_len=fig_len, fig_height=fig_height, data_colors=data_colors, hover_data=hover_data, alpha=0.6, figure_collector=figure_collector, fig_title=fig_title, sub_title=sub_title): # title = create_text(fig_title, text_color='#2b2d2f', text_font_size='40px', fig_height=60) # sub_title = create_text(sub_title, text_color='grey', text_font_size='20px', fig_height=40) # figure_collector.append(title) # figure_collector.append(sub_title) num_figures = len(source_columns) num = 0 while num < num_figures: # create figure fig = figure(plot_width=fig_len, plot_height=fig_height, x_axis_type='datetime', name='data_series', output_backend="webgl") fig.sizing_mode = 'scale_width' # add tools tools = [ PanTool(), BoxZoomTool(), WheelZoomTool(), SaveTool(), ResetTool() ] fig.add_tools(*tools) # create the scatter plot scatter = Circle(x='publishedAt', y=source_columns[num], line_color=data_colors[num], fill_color=data_colors[num], size=6, fill_alpha=alpha, line_alpha=alpha) scatter_render = fig.add_glyph(source_or_glyph=source, glyph=scatter) # hover only over scatter plot hover = HoverTool(renderers=[scatter_render], tooltips=hover_data, formatters={'@publishedAt': 'datetime'}) fig.add_tools(hover) # open video url on tap scatter points taptool = TapTool(renderers=[scatter_render]) taptool.callback = OpenURL(url=url) fig.add_tools(taptool) # create line plot without hover or top # line = Line(x='publishedAt', y=source_columns[num], line_color=data_colors[num], line_width=2, line_alpha=1) # line_render = fig.add_glyph(source_or_glyph=source, glyph=line) # add series title title = Text(x=data['publishedAt'].min(), y=0.2, text=[figure_titles[num]], text_color=data_colors[num], text_font_size='35px') fig.add_glyph(title) # decrease clutter fig.outline_line_color = None fig.xgrid.grid_line_color = None fig.ygrid.grid_line_color = None fig.yaxis.visible = False fig.xaxis.visible = False # format x-axis ticks fig.xaxis.major_tick_line_color = "grey" fig.xaxis.major_label_text_font_size = "15pt" fig.xaxis.major_label_text_color = "grey" # collect figures in a list figure_collector.append(fig) num += 1 return figure_collector
def draw_plot(G, query, expr, type='publications'): # plot plot = figure( x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1), sizing_mode="stretch_both", tools="") plot.axis.visible = False plot.xgrid.grid_line_color = None plot.ygrid.grid_line_color = None # legend plot.circle( x=[-200000, ], y=[-200000, ], fill_color='white', size=0, line_width=0, legend_label='Visualization for "' + query + '"') plot.circle( x=[-200000, ], y=[-200000, ], fill_color='white', size=0, line_width=0, legend_label='created using Microsoft Academic Graph and') plot.circle( x=[-200000, ], y=[-200000, ], fill_color='white', size=0, line_width=0, legend_label='Sciencegraph by Marcus Ossiander, 2020') if type == 'publications': plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm2[3], size=20, legend_label='Publication, Color measures Citation Count') plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm1[3], size=10, legend_label='Reference, Color measures Citation Count') if type == 'authors': plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm2[3], size=15, legend_label='Publication, Color measures Citation Count') plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm1[3], size=10, legend_label='Co-Author, Color measures Collaboration') plot.legend.background_fill_alpha = 0 plot.legend.border_line_alpha = 0 plot.legend.location = 'top_left' # tools node_hover_tool = HoverTool(tooltips=tooltips) zoom_tool = WheelZoomTool() div = Div( text='<div id="showpaper" style="position: absolute; display: none; width=500px"></div>', name='showpaper', sizing_mode="stretch_width") tap_tool_open = TapTool() tap_tool_open.callback = CustomJS( args={'tp': showpaper_content}, code=code) help_tool = HelpTool( help_tooltip='Created using Microsoft Academic Graph and Sciencegraph by Marcus Ossiander, 2020', redirect='https://github.com/marcus-o/sciencegraph/') plot.add_tools( node_hover_tool, zoom_tool, BoxZoomTool(), ResetTool(), tap_tool_open, help_tool) plot.toolbar.active_scroll = zoom_tool # graph graph_renderer = from_networkx( G, nx.spring_layout, scale=1, center=(0, 0), seed=12345) # normal graph_renderer.node_renderer.glyph = Circle( size="size", fill_color="color") graph_renderer.edge_renderer.glyph = MultiLine( line_alpha=0.2) # selection graph_renderer.node_renderer.selection_glyph = Circle( fill_color="color", fill_alpha=1, line_alpha=1) graph_renderer.edge_renderer.selection_glyph = MultiLine( line_width=3, line_alpha=1) graph_renderer.node_renderer.nonselection_glyph = Circle( fill_color="color", fill_alpha=0.5, line_alpha=0.5) graph_renderer.edge_renderer.nonselection_glyph = MultiLine( line_alpha=0.2) # hover graph_renderer.node_renderer.hover_glyph = Circle( fill_color='#abdda4') graph_renderer.edge_renderer.hover_glyph = MultiLine( line_color='#abdda4', line_width=3) graph_renderer.inspection_policy = NodesAndLinkedEdges() graph_renderer.selection_policy = NodesAndLinkedEdges() # add everything plot.renderers.append(graph_renderer) script, div = components(Column(children=[plot, div], sizing_mode="stretch_both")) return script, div
def process_file(self): potiron_path = potiron.potiron_path lentwo = False ck = self.red.sismember('CK', 'YES') for v in self.fieldvalues: # if any field value is in format 'value-protocol' (or 'value-all'), it means we want to display each protocol separatly if len(v.split('-')) >= 2: lentwo = True # Using the format 'value-protocol' is not possible if combined keys are not deployed in the current redis database if lentwo and not ck: sys.stderr.write( 'Combined keys are not used in this redis dataset') sys.exit(1) if not self.outputdir.endswith('/'): self.outputdir = "{}/".format(self.outputdir) if not os.path.exists(self.outputdir): os.makedirs(self.outputdir) # Definition of the protocols currently present in our dataset protocols = self.red.smembers('PROTOCOLS') # Define the strings used for legends, titles, etc. concerning fields field_string, field_in_file_name = field2string( self.field, potiron_path) field_data = create_dict(self.field, potiron_path) all_proto = False for fv in self.fieldvalues: v = fv.split('-') # If we want to display the values for all the procotols, we display the sum of all of them as well if len(v) >= 2 and (v[1] == '*' or v[1] == 'all'): all_proto = True self.fieldvalues.append(v[0]) # Creation of the figure and the tools used on it namefile = self.output_name(field_in_file_name, lentwo, all_proto) # As displaying values for all the protocols may generate a lot of lines in the plot, # We help users showing them the protocol when they have there cursor in the line if all_proto: hover = HoverTool(tooltips=[('count', '@y'), ('protocol', '@prot')]) else: hover = HoverTool(tooltips=[('count', '@y')]) taptool = TapTool() TOOLS = [ hover, PanTool(), BoxZoomTool(), WheelZoomTool(), taptool, SaveTool(), ResetTool() ] p = figure(width=self.plot_width, height=self.plot_height, tools=TOOLS) # Definition of some variables which will be used and modified with the iterations at_least_one = False days = calendar.monthrange(int(self.date[0:4]), int(self.date[4:6]))[1] maxVal = 0 minVal = sys.maxsize maxDay = 0 vlength = len(self.fieldvalues) actual_values = [] nbLine = 0 # day_string = "@x" for v in range(vlength): # For each selected field or occurrence value = self.fieldvalues[v].split('-') actual_field = value[0] if len(value) >= 2: # If we specified a or all protocol(s) protocol = value[1] if protocol == "*" or protocol == "all": for prot in protocols: score = [] dayValue = [] proto = prot.decode() exists = False keys = self.red.keys("{}:{}:{}*:{}".format( self.source, proto, self.date, self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore( redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: at_least_one = True # We define the color of the line, draw it color = palette[nbLine % 10] protos = [] for x in dayValue: protos.append("{}_{}".format(x, proto)) prots = [proto] * len(score) sourceplot = ColumnDataSource( data=dict(x=dayValue, y=score, protocol=protos, prot=prots)) leg = def_legend(actual_field, proto, self.field, field_string, field_data) p.line(x='x', y='y', legend=leg, line_color=color, line_width=2, source=sourceplot) c = p.scatter(x='x', y='y', legend=leg, size=10, color=color, alpha=0.1, source=sourceplot) taptool.renderers.append( c) # In order to have the interaction on click nbLine += 1 maxScore = max( score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}-{}".format( actual_field, protocol) actual_values.append(actual_value) else: score = [] dayValue = [] exists = False keys = self.red.keys("{}:{}:{}*:{}".format( self.source, protocol, self.date, self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore(redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: # If at least one occurrence for the current value of field has been found at_least_one = True # We define the color of the line, draw it color = palette[nbLine % 10] leg = def_legend(actual_field, protocol, self.field, field_string, field_data) p.line(x=dayValue, y=score, legend=leg, line_color=color, line_width=2) c = p.scatter(x=dayValue, y=score, legend=leg, size=10, color=color, alpha=0.1) taptool.renderers.append( c) # In order to have the interaction on click nbLine += 1 maxScore = max( score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}-{}".format(actual_field, protocol) actual_values.append(actual_value) else: # on the other case, we don't split informations for each protocol score = [] dayValue = [] exists = False # If combined keys are used, we must by the way take data from all the keys (i.e for each protocol) if ck: for d in range( 1, days + 1): # For each day with data stored in redis exists_day = False day = format(d, '02d') countValue = 0 keys = self.red.keys("{}:*:{}{}:{}".format( self.source, self.date, day, self.field)) for k in keys: redisKey = k.decode() tmpscore = self.red.zscore(redisKey, actual_field) countValue += tmpscore if tmpscore is not None else 0 exists_day = True if exists_day: if countValue > 0: exists = True score.append(countValue) dayValue.append(day) else: # When combined keys are not used, we only need to read the scores for each day keys = self.red.keys("{}:{}*:{}".format( self.source, self.date, self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore(redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: # If at least one occurrence for the current value of field has been found at_least_one = True # We define the color of the line, draw it color = palette[nbLine % 10] leg = def_legend(actual_field, None, self.field, field_string, field_data) if all_proto: protos = [] for x in dayValue: protos.append(x) prots = ['all protocols'] * len(score) sourceplot = ColumnDataSource(data=dict( x=dayValue, y=score, protocol=protos, prot=prots)) p.line(x='x', y='y', legend=leg, line_color=color, line_width=2, source=sourceplot) c = p.scatter(x='x', y='y', legend=leg, size=10, color=color, alpha=0.1, source=sourceplot) else: p.line(x=dayValue, y=score, legend=leg, line_color=color, line_width=2) c = p.scatter(x=dayValue, y=score, legend=leg, size=10, color=color, alpha=0.1) taptool.renderers.append( c) # In order to have the interaction on click nbLine += 1 maxScore = max( score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}".format(actual_field) actual_values.append(actual_value) if at_least_one: # If at least one value has been found in redis with our selection if lentwo: # Defines the name of the files to call with a click on a point in the plot taptool.callback = OpenURL( url="{}_{}_with-protocols_{}-{}[email protected]".format( self.source, field_in_file_name, self.date[0:4], self.date[4:6])) else: taptool.callback = OpenURL(url="{}_{}_{}-{}[email protected]".format( self.source, field_in_file_name, self.date[0:4], self.date[4:6])) output_file("{}.html".format(namefile), title=namefile.split("/")[-1]) # Definition of some parameters of the graph fieldvalues_string = plot_annotation(self.field, potiron_path, actual_values, field_string, field_data) p.title.text = "Number of {} {}seen each day in {} {}".format( field_string, fieldvalues_string, potiron.year[self.date[4:6]], self.date[0:4]) p.yaxis[0].formatter = BasicTickFormatter(use_scientific=False) p.xaxis.axis_label = "Days" p.yaxis.axis_label = "Count" p.legend.location = "top_left" p.legend.click_policy = "hide" # Definition of some parameters for the logo with Image.open(self.logofile) as im: im_width, im_height = im.size xdr = maxDay + 1 upper_space = 10 if nbLine > 2: upper_space *= (nbLine / 2) ydrmax = maxVal + maxVal * upper_space / 100 ydrmin = minVal - maxVal * 5 / 100 p.x_range = Range1d(0, xdr) p.y_range = Range1d(ydrmin, ydrmax) height = (ydrmax - ydrmin) / self.logo_y_scale width = xdr / ((self.logo_y_scale * im_height * self.plot_width) / (im_width * self.plot_height)) p.image_url(url=[self.logofile], x=[xdr], y=[ydrmax - ydrmax * 2 / 100], w=[width], h=[height], anchor="top_right") # Process the graph save(p) if self.links: export_csv = export_csv_all_days_per_month.Export_Csv( self.red, self.source, self.date, self.field, 10, ['-1'], self.outputdir, True, True, self.logofile, ck, lentwo) ck = True if red.sismember('CK', 'YES') else False export_csv.process_all_files() else: print("There is no such value for a {} you specified: {}".format( field_string, self.fieldvalues))
taptool.callback = CustomJS(args=dict(source=tap_source, lrp=lrp_source, high=highlight_source, div=text_banner, div_orig=text_banner2), code=""" cell = cb_obj.selected['1d']['indices'][0] var d = high.data; d['scores'] = [] for(var i=0; i<source.data['wc_words'].length; i++){ d['scores'].push(lrp.data['lrp'][cell]) } high.change.emit(); ws = div_orig.text.split(" "); ws_out = []; for(var j=0; j<ws.length; j++){ w_idx = source.data['wc_words'].indexOf(ws[j]) if (w_idx>=0){ if (d['scores'][w_idx]>0){ ws_out.push("<span style='background-color: rgba(255,0,0,"+d['scores'][w_idx]+")'>"+ws[j]+"</span>") } else if (d['scores'][w_idx]<0){ ws_out.push("<span style='background-color: rgba(0,255,0,"+Math.abs(d['scores'][w_idx])+")'>"+ws[j]+"</span>") } } else { ws_out.push(ws[j]) } } div.text = ws_out.join(" ") console.log(ws_out) """)