def bar(cds, metric): """ Create a bar graph comparing a single metric across forecasts. Parameters ---------- cds : bokeh.models.ColumnDataSource Metric cds created by :py:func:`solarforecastarbiter.reports.figures.construct_metrics_cds` metric: str The metric to plot. This value should be found in cds['metric']. Returns ------- data_table : bokeh.widgets.DataTable """ # NOQA x_range = np.unique(cds.data['abbrev']) palette = cycle(PALETTE) palette = [next(palette) for _ in x_range] metric_name = datamodel.ALLOWED_METRICS[metric] view = CDSView(source=cds, filters=[ GroupFilter(column_name='metric', group=metric), GroupFilter(column_name='category', group='total') ]) # TODO: add units to title fig = figure(x_range=x_range, width=800, height=200, title=metric_name, name=f'{metric}_total_bar', toolbar_location='above', tools='pan,xwheel_zoom,box_zoom,reset,save') fig.vbar(x='abbrev', top='value', width=0.8, source=cds, view=view, line_color='white', fill_color=factor_cmap('abbrev', palette, factors=x_range)) fig.xgrid.grid_line_color = None tooltips = [ ('Forecast', '@name'), (metric_name, '@value'), ] hover = HoverTool(tooltips=tooltips, mode='vline') # more accurate would be if any single name is longer than each # name's allotted space. For example, never need to rotate labels # if forecasts are named A, B, C, D... but quickly need to rotate # if they have long names. if len(x_range) > 6: # pi/4 looks a lot better, but first tick label flows off chart # and I can't figure out how to add padding in bokeh fig.xaxis.major_label_orientation = np.pi / 2 fig.width = 800 # add more height to figure so that the names can go somewhere. fig.height = 400 fig.add_tools(hover) return fig
def __init__(self, scheduler, **kwargs): self.scheduler = scheduler self.layout = GraphLayout(scheduler) self.invisible_count = 0 # number of invisible nodes self.node_source = ColumnDataSource({ 'x': [], 'y': [], 'name': [], 'state': [], 'visible': [], 'key': [] }) self.edge_source = ColumnDataSource({'x': [], 'y': [], 'visible': []}) node_view = CDSView( source=self.node_source, filters=[GroupFilter(column_name='visible', group='True')]) edge_view = CDSView( source=self.edge_source, filters=[GroupFilter(column_name='visible', group='True')]) node_colors = factor_cmap( 'state', factors=['waiting', 'processing', 'memory', 'released', 'erred'], palette=['gray', 'green', 'red', 'blue', 'black']) self.root = figure(title='Task Graph', **kwargs) self.root.multi_line(xs='x', ys='y', source=self.edge_source, line_width=1, view=edge_view, color='black', alpha=0.3) rect = self.root.square(x='x', y='y', size=10, color=node_colors, source=self.node_source, view=node_view, legend='state') self.root.xgrid.grid_line_color = None self.root.ygrid.grid_line_color = None hover = HoverTool(point_policy="follow_mouse", tooltips="<b>@name</b>: @state", renderers=[rect]) tap = TapTool(callback=OpenURL(url='info/task/@key.html'), renderers=[rect]) rect.nonselection_glyph = None self.root.add_tools(hover, tap)
def __populate_scrubber(cls, scrubber, scrubberdf, scrubbersource, columnmap): categorycol = columnmap['scrubber_category'] resultcol = columnmap['result'] colorcol = columnmap['scrubber_color'] timecol = columnmap['timeline_start_ts'] results = scrubberdf[resultcol].unique() scrubber.y_range.update(factors=results) categories = scrubberdf[categorycol].unique() scrubber_legends_items = [] for category in categories: view = CDSView( source=scrubbersource, filters=[GroupFilter(column_name=categorycol, group=category)]) renderer = scrubber.circle(x=timecol, y=resultcol, size=12, source=scrubbersource, view=view, color=colorcol) scrubber_legends_items.append( LegendItem(label=category, renderers=[renderer])) legend = Legend(items=scrubber_legends_items) legend.click_policy = 'hide' scrubber.add_layout(legend, 'left') return scrubber
def total_kms_day(source, X_AXIS): source = source week_1_view = CDSView(source=source, filters=[GroupFilter(column_name='week', group='week 1.0')]) p = figure( plot_height=300, plot_width=800, title="Total runs for each day of the week", x_axis_label="Day of the week", y_axis_label="KMs", toolbar_location=None, x_range=X_AXIS, x_minor_ticks=2, y_range=(0, 350),) p.vbar( x='day_of_week', bottom=0, top='kms', color='#084594', width=0.75, legend='Actual', source=source, ) tooltips = [ ('Kilometers','@kms'), ] # Add the HoverTool to the figure p.add_tools(HoverTool(tooltips=tooltips)) return p
def __create_timeline(cls, timelinedf, callback, columnmap, figurekwargs): leftcol = columnmap['timeline_start_ts'] rightcol = columnmap['timeline_end_ts'] categorycol = columnmap['timeline_category'] resultcol = columnmap['result'] colorcol = columnmap['timeline_color'] timelinesource = ColumnDataSource(timelinedf) timeline = figure(y_range=timelinedf[categorycol], x_axis_type='datetime', **figurekwargs) timeline.xaxis.formatter = DatetimeTickFormatter(hourmin=['%H:%M'], minsec=['%H:%M:%S']) timeline.yaxis.axis_label_text_font = 'Consolas' results = timelinedf[resultcol].unique() for result in results: view = CDSView( source=timelinesource, filters=[GroupFilter(column_name=resultcol, group=result)]) p = timeline.hbar(left=leftcol, right=rightcol, y=categorycol, source=timelinesource, view=view, color=colorcol, legend=result, height=0.6) timelinesource.callback = callback timeline.legend.click_policy = 'hide' return (timelinesource, timeline)
def make_scatter(df, genre_list, title): #DataFrameを渡す source = ColumnDataSource(df) #データをFilterでジャンルごとに抽出 views = [] for genre in genre_list: view = CDSView(source=source, filters=[GroupFilter(column_name="genre", group=genre)]) views.append(view) # グラフサイズおよびタイトルの設定 fig = figure(plot_height=660, plot_width=880, title=title, x_axis_type="datetime") fig.xaxis.axis_label = "購入日" fig.yaxis.axis_label = "書籍総数" # HoverToolの設定 hover = HoverTool(tooltips=[("タイトル", "@title")]) fig.add_tools(hover) # 散布図 for count, view in enumerate(views): #ジャンル毎に色を設定 for value in config.genre_conf.GENRES: if genre_list[count] == value: color = config.color_conf.GENRE(value) fig.circle(x="purchase_date", y="total", source=source, legend_label=genre_list[count], view=view, color=color) #htmlに埋め込むデータを作成 script, div = components(fig) data = [script, div] return data
def _make_data_table(self): """ Builds the datatable portion of the final plot. """ columns = [ TableColumn(field="props", title="Property"), TableColumn(field="values", title="Value"), ] prop_source = ColumnDataSource(self._prop_df) model_id = self._node_source.data["index"][0] groupfilter = GroupFilter(column_name="id", group=model_id) data_table2_view = CDSView(source=prop_source, filters=[groupfilter]) data_table2 = DataTable( source=prop_source, view=data_table2_view, columns=columns, visible=False, index_position=None, fit_columns=True, editable=False, ) self._groupfilter = groupfilter self._prop_source = prop_source return data_table2
def add_renderers(): color_attribute, label_mapping = { 'Categories': ('cat_str', categories), 'cats_ae_pred': ('cats_ae_pred_str', binary), 'original_pred': ('original_pred_str', binary), }[color_from_dropdown.value] plot = figure(x_range=(0, 1), y_range=(0, 1), width=800, height=400, tools=[hover_tool]) plot.add_tools(TapTool(behavior='select')) if toggle_background_button.active: plot.image_url(url=['bokeh_stream/static/images/bokeh_plot.png'], x=0, y=0, w=1, h=1, anchor='bottom_left') legend_list = [] for i in range(len(pd.unique(df.loc[:, color_attribute]))): cat = label_mapping[i] view = CDSView( source=source, filters=[GroupFilter(column_name=color_attribute, group=str(i))]) rend = plot.scatter('x_cats_ae', 'y_cats_ae', color=colors[i], size=15, line_color='black', source=source, view=view) rend.selection_glyph = Circle(fill_alpha=1, fill_color=colors[i], line_color='black') rend.nonselection_glyph = CircleCross(fill_alpha=0.1, fill_color=colors[i], line_color=colors[i]) legend_list.append((cat, [rend])) legend = Legend(items=legend_list, location=(20, 0)) legend.click_policy = 'hide' plot.add_layout(legend, 'left') plot.circle('x', 'y', radius='rad', source=radius_source, line_color='black', line_width=2, color=None, line_dash='dashed', line_alpha=0.7) return plot
def viz(self): if not self.viz_params['to_viz']: return None views = {} for phase in self.phases: views[phase] = CDSView( source=self.source, filters=[GroupFilter(column_name='phase', group=phase)]) plots = [] # bokeh.plotting.markers() markers = [ 'circle', 'triangle', 'square', 'diamond', 'hex', 'cross', 'asterisk', 'inverted_triangle' ] colors = palettes.all_palettes['Colorblind'][8] # generate a plot for each metric for metric in self.source.data.keys(): if metric not in ['epoch', 'phase']: hover = HoverTool() hover.tooltips = [('epoch', '@{epoch}{0i}'), ('value', '@{}{{0.0000f}}'.format(metric))] p = figure(tools='pan,box_zoom,tap,lasso_select,save,reset', plot_width=290, plot_height=290, title=metric) p.tools.append(hover) for i, phase in enumerate(self.phases): p.scatter(x='epoch', y=metric, source=self.source, view=views[phase], legend=phase, marker=markers[i], color=colors[i], size=7, alpha=0.85) p.legend.location = 'bottom_left' # p.legend.click_policy = 'hide' p.legend.visible = False plots.append(p) plots[-1].legend.visible = True grid = [] cur_row = [] for plot in plots: if len(cur_row) >= 3: grid.append(cur_row) cur_row = [] cur_row.append(plot) if len(cur_row) > 0: grid.append(cur_row) return grid
def create_figure(): active = {} for w in sort_widgets: active[w.name] = converters[w.name][w.active] key = '{} {} {}'.format(active['Model'], active['Dataset'], active['Filter']) xrange = sorts[key] alpha_dict = {'Support vector machine': 1.0, 'Random forest': 0.1} color_dict = {'Split': Dark2[5][0], 'Mixed': Dark2[5][1], 'Reverse split': Dark2[5][2], 'US': Dark2[5][3], 'UK': Dark2[5][4]} shape_dict = {'Complete': 'circle', 'Complete filtered': 'diamond', 'Filtered': 'square'} visible_models = [modelboxes.labels[x] for x in modelboxes.active] visible_datasets = [datasetboxes.labels[x] for x in datasetboxes.active] visible_filters = [filterboxes.labels[x] for x in filterboxes.active] height = 600 + (22*(len(visible_models) * len(visible_datasets) * len(visible_filters))) p = figure(x_range=FactorRange(factors=xrange), x_axis_label='Kmer', y_axis_label='Score', plot_width=900, plot_height=height, output_backend='webgl') p.xaxis.major_label_orientation = 3.1415/4 legend = [] for model in visible_models: for dataset in visible_datasets: for f in visible_filters: curr_filter = [GroupFilter(column_name='Model', group=model), GroupFilter(column_name='Dataset', group=dataset), GroupFilter(column_name='Filter', group=f), bools = [True if k in xrange else False for k in source.data['Kmer']] BooleanFilter(bools)] view = CDSView(source=source, filters=curr_filter) alpha = alpha_dict[model] color = color_dict[dataset] shape = shape_dict[f] size=10 glyph = p.scatter(x='Kmer', y='Score', source=source, view=view, color=color, fill_alpha=alpha, size=size, marker=shape) legend.append(("{} {} {}".format(model, dataset, f), [glyph])) p.add_layout(Legend(items=legend), 'below') return p
def weekly_actual_goal(source, X_AXIS): source = source week_1_view = CDSView( source=source, filters=[GroupFilter(column_name='week', group='week 1.0')]) p = figure( plot_height=300, plot_width=800, title="Weekly running", tools='', x_axis_label="Day of the week", y_axis_label="KMs", toolbar_location="above", x_range=X_AXIS, x_minor_ticks=2, y_range=(0, 20), ) p.vbar( x='day_of_week', bottom=0, top='kms', color='#084594', width=0.75, legend='Actual', source=source, ) p.line( x='day_of_week', y='daily_goal', color='#9ecae1', line_width=5, legend='Goal', source=source, ) tooltips = [ ('Kilometers', '@kms'), ('Week number', '@week'), ] # Add the HoverTool to the figure p.add_tools(HoverTool(tooltips=tooltips)) return p
def function_geosource(attr, old, new): try: selected_index = geosource.selected.indices[0] subdist = cg.iloc[selected_index]['area_numbe'] community = cg.loc[cg['area_numbe'] == subdist]['community'].values[0] view1 = CDSView( source=source, filters=[GroupFilter(column_name='community_area', group=subdist)]) columns = [ TableColumn(field='primary_type', title='<b>Crime_Description<b>'), TableColumn(field='case_number', title='<b>#Cases<b>', formatter=NumberFormatter(format='0,0')) ] data_table = DataTable(source=source, view=view1, columns=columns, width=280, height=500, editable=False, index_position=None) p2 = figure( y_range=df2.loc[df2['community_area'] == subdist]['primary_type'].unique()[::-1], x_axis_location="above", title="Offence count by neighbourhood: {}".format(community), plot_height=800, plot_width=1000) p2.hbar(y='primary_type', right='case_number', source=source, view=view1, height=0.5) #p2.xaxis.major_label_orientation = 1.2 p2.toolbar.active_drag = None p2.x_range.start = 0 style(p2) p2.outline_line_width = 0 # Replace the updated datatable in the layout layout.children[1] = data_table layout.children[2] = p2 except IndexError: pass
def create_figure(): p = figure(plot_height=600, plot_width=700, title="", toolbar_location=None, tools=[hover, TOOLS]) # df = pd.DataFrame(np.tile(np.array([1, 2, 3, 4, 5]), (3, 1)), columns=list('ABCDE')) # df = df.transpose() # df.loc['F'] = [3.5, 3.5, 3.5] # print(df) # train = df.loc['C'] cds_data = ColumnDataSource(df_result) print(cds_data) target_view = CDSView( source=cds_data, filters=[GroupFilter(column_name='index', group=target)]) p.circle(x="x", y="y", source=cds_data, size=10, color="navy", alpha=0.5, hover_color="red") p.circle(x="x", y="y", source=cds_data, size=10, color="red", alpha=0.5, hover_color="red") #p.add_layout(LabelSet(x='x', y='x', text='index', source=cds_data, x_offset=0.01, y_offset=0.01)) for loc, dim in zip([0, 0], ['width', 'height']): p.add_layout( Span( location=loc, dimension=dim, #source=target_view, line_color='green', line_dash='dashed', line_width=3)) return p
def make_data(country_list, country_rank_df): p = figure(plot_width=600, plot_height=600) for i in range(len(country_list)): data = bp.ColumnDataSource( data={ 'year': country_rank_df.loc[country_rank_df.country_name == country_list[i]].year, 'group': country_rank_df.loc[country_rank_df.country_name == country_list[i]].country_name, 'score': country_rank_df.loc[country_rank_df.country_name == country_list[i]].score }) view = CDSView(source=data, filters=[ GroupFilter(column_name='country_name', group=country_list[i]) ]) p.line('year', 'score', source=data, view=view, line_width=2, color=(Category10[3])[i], legend=country_list[i]) hover = HoverTool(tooltips=[('Score', '@score')]) checkbox_group = CheckboxGroup(labels=list( country_rank_df.country_name.unique()), active=[0, 1]) p.add_tools(hover) layout = row(p, checkbox_group) return layout
def embedding(source, settings): """Display a 2-dimensional embedding of the images. Parameters ---------- source : ColumnDataSource settings : dictionary Returns ------- embed : bokeh figure Scatterplot of precomputed x/y coordinates result """ glyph_size = settings['plots']['glyph_size'] tools_scatter = ['pan, box_select, poly_select, wheel_zoom, reset, tap'] embed = figure(title='Embedding', sizing_mode='scale_both', tools=tools_scatter, active_drag="box_select", active_scroll='wheel_zoom', tooltips=get_tooltips(settings), output_backend='webgl') embed = _dynamic_range(embed) color_column = settings['color-columns']['categorical'][0] if color_column in source.data: group_names = pd.Series(source.data[color_column]).unique() my_colors = _palette(len(group_names)) for i, group in enumerate(group_names): group_filter = GroupFilter(column_name=color_column, group=group) view = CDSView(source=source, filters=[group_filter]) glyphs = embed.circle(x="x", y="y", source=source, view=view, size=glyph_size, color=my_colors[i], legend=group) embed.legend.location = "top_right" embed.legend.click_policy = "hide" embed.legend.background_fill_alpha = 0.5 else: embed.circle(source=source, x='x', y='y', size=glyph_size) return embed
def add_select(self): """Build select control """ # select control self.controls['select'] = Select(value='a', options=self.meta['options'], max_width=self.plot.plot_width-35) # map views _filter = GroupFilter(column_name='state_id', group='12') _counties_on = CDSView(source=self.srcs['counties'], filters=[_filter]) _counties_off = CDSView(source=self.srcs['counties'], filters=[]) _states_on = CDSView(source=self.srcs['states'], filters=[_filter]) _states_off = CDSView(source=self.srcs['states'], filters=[]) _args = dict(counties_src=self.srcs['counties'], states_src=self.srcs['states'], counties_glyph=self.plot.select('counties')[0], states_glyph=self.plot.select( 'states')[0], filter=_filter, counties_view_on=_counties_on, states_view_on=_states_on, counties_view_off=_counties_off, states_view_off=_states_off) _callback = CustomJS(args=_args, code=""" if (cb_obj.value != '00'){ console.log(cb_obj.value); filter.group = cb_obj.value; counties_glyph.view = counties_view_on; states_glyph.view = states_view_on; } else{ console.log(cb_obj.value); counties_glyph.view = counties_view_off; states_glyph.view = states_view_off; } counties_src.change.emit(); states_src.change.emit(); """) self.controls['select'].js_on_change('value', _callback) log.debug('select control added')
def _make_views(self): get_flt = lambda ind, val: ([GroupFilter(column_name=ind, group=val)] if ind else []) def get_view(source, valx, valy): filters = (get_flt(self.ind_pltx, valx) + get_flt(self.ind_plty, valy)) return CDSView(source=source, filters=filters) list_pn = ['pos', 'neg'] list_cds = [(cds, pn) for cds, pn in zip([self.cds_pos, self.cds_neg], list_pn) if cds] self.views = dict.fromkeys(self.xy_combs) for valx, valy in self.views.keys(): self.views[(valx, valy)] = dict.fromkeys(list_pn) for source, pn in list_cds: self.views[(valx, valy)][pn] = get_view(source, valx, valy)
import pandas as pd player_stats = pd.read_csv('2017-18_playerBoxScore.csv', parse_dates=['gmDate']) team_stats = pd.read_csv('2017-18_teamBoxScore.csv', parse_dates=['gmDate']) standings = pd.read_csv('2017-18_standings.csv', parse_dates=['stDate']) # Output inline in the notebook output_file('lebron-vs-durant.html', title='LeBron James vs. Kevin Durant') # Store the data in a ColumnDataSource player_gm_stats = ColumnDataSource(player_stats) # Create a view for each player lebron_filters = [ GroupFilter(column_name='playFNm', group='LeBron'), GroupFilter(column_name='playLNm', group='James') ] lebron_view = CDSView(source=player_gm_stats, filters=lebron_filters) durant_filters = [ GroupFilter(column_name='playFNm', group='Kevin'), GroupFilter(column_name='playLNm', group='Durant') ] durant_view = CDSView(source=player_gm_stats, filters=durant_filters) # Consolidate the common keyword arguments in dicts common_figure_kwargs = { 'plot_width': 400, 'x_axis_label': 'Points', 'toolbar_location': None,
from bokeh.models import ColumnDataSource, CDSView, GroupFilter # Import the data from read_nba_data import * # Output to static HTML file output_file('west_top_2_standings_race.html', title='Western Conference Top 2 Teams Wins Race') # Create a ColumnDataSource west_cds = ColumnDataSource(west_top_2) # Create view for each team rockets_view = CDSView( source=west_cds, filters=[GroupFilter(column_name='teamAbbr', group='HOU')]) warriors_view = CDSView( source=west_cds, filters=[GroupFilter(column_name='teamAbbr', group='GS')]) # Create and configure the figure fig = figure(x_axis_type='datetime', plot_height=300, plot_width=600, title='Western Conference Top 2 Teams Wins Race, 2017-18', x_axis_label='Date', y_axis_label='Wins', toolbar_location=None) # Render the race as step lines fig.step('stDate',
from bokeh.layouts import gridplot from bokeh.models import CDSView, ColumnDataSource, GroupFilter from bokeh.plotting import figure, show from bokeh.sampledata.iris import flowers source = ColumnDataSource(flowers) view1 = CDSView( source=source, filters=[GroupFilter(column_name='species', group='versicolor')]) plot_size_and_tools = { 'plot_height': 300, 'plot_width': 300, 'tools': ['box_select', 'reset', 'help'] } p1 = figure(title="Full data set", **plot_size_and_tools) p1.circle(x='petal_length', y='petal_width', source=source, color='black') p2 = figure(title="Setosa only", x_range=p1.x_range, y_range=p1.y_range, **plot_size_and_tools) p2.circle(x='petal_length', y='petal_width', source=source, view=view1, color='red') show(gridplot([[p1, p2]]))
output_file("COVID19_1.html", title="Total Number of Confirmed COVID-19 Cases") show(p) # %% #Plotting log-scale number of cases for the selected countries. Hover-tool and series-hiding on. from bokeh.plotting import figure, show, output_file from bokeh.models import ColumnDataSource, CDSView, GroupFilter, HoverTool source = ColumnDataSource(covid) source.add(covid['date'].apply(lambda d: d.strftime('%Y-%m-%d')), 'date_formatted') USA = CDSView(source=source, filters=[GroupFilter(column_name='country', group='USA')]) DNK = CDSView(source=source, filters=[GroupFilter(column_name='country', group='DNK')]) SWE = CDSView(source=source, filters=[GroupFilter(column_name='country', group='SWE')]) ITA = CDSView(source=source, filters=[GroupFilter(column_name='country', group='ITA')]) ESP = CDSView(source=source, filters=[GroupFilter(column_name='country', group='ESP')]) p = figure( title= 'Total Confirmed COVID-19 Cases (Click on legend entries to hide the corresponding datapoints)', x_axis_type='datetime', x_axis_label='Date', y_axis_label='Log Cases',
colors = [colormap[x] for x in data['handedness']] #source = ColumnDataSource(data=dict(avg=data['avg'], HR=data['HR'], handedness=data['handedness'])) source = ColumnDataSource(data) tools = ["box_select", "lasso_select", "hover", "pan", "box_zoom", "reset"] p_all = figure(tools=tools, title='Batting Average vs Home Runs of all players', height=200, width=1200) p_all.xaxis.axis_label = 'Average' p_all.yaxis.axis_label = 'Home Runs' p_all.circle(data["avg"], data["HR"], color=colors, hover_color="red") view1 = CDSView(source=source, filters=[GroupFilter(column_name='handedness', group='R')]) p_R = figure(tools=tools, title='Batting Average vs Home Runs of right handed players', height=200, width=1200) p_R.xaxis.axis_label = 'Average' p_R.yaxis.axis_label = 'Home Runs' p_R.circle(x="avg", y="HR", color='red', hover_color="yellow", view=view1, source=source) view2 = CDSView(source=source, filters=[GroupFilter(column_name='handedness', group='L')])
genes = assign_and_color(genes, groupby=['condition', 'uniprot_bio_process']) #%% # Instantiate the output file. bokeh.io.output_file('../../figs/biological_process_dashboard.html') # Define the data sources growth_source = ColumnDataSource(processes) treemap_source = ColumnDataSource(genes) # Define a view filter based on a selection selection = Select(title="Biological Process (UniProt):", value='', options=list(processes['uniprot_bio_process'].unique())) # Define a view and group filter for the selected process process_filter = GroupFilter(column_name='uniprot_bio_process', group='') condition_filter = GroupFilter(column_name='condition', group='') growth_view = CDSView(source=growth_source, filters=[process_filter]) treemap_view = CDSView(source=treemap_source, filters=[process_filter, condition_filter]) growth_tooltips = [('growth condition', '@condition'), ('growth rate [hr^-1]', '@growth_rate_hr')] treemap_tooltips = [('gene', '@group'), ('growth condition', '@condition'), ('COG class', '@cog_class'), ('Description', '@desc')] # Set up the canvases treemap = bokeh.plotting.figure(width=450, height=450, x_range=[0, 500], y_range=[0, 500], title='OCCUPANCY OF PROCESS BY PROTEIN',
def figures_slopes(df_slopes, df_pop): nbStart = 7 nbEnd = 0 rolling = 7 df_countrySlopes = determineSlope(df_slopes, df_pop, nbStart, nbEnd, rolling) df_countrySlopes = df_countrySlopes.replace([np.inf, -np.inf], np.nan) df_countrySlopes = df_countrySlopes.dropna() #df_countrySlopes=df_countrySlopes[df_countrySlopes.casesSlopePval<0.05] #df_countrySlopes=df_countrySlopes[df_countrySlopes.testsSlopePval<0.05] df_countrySlopes["temp"] = "0" df_countrySlopes.loc[ df_countrySlopes.testsWeeklyPerc >= df_countrySlopes.casesWeeklyPerc, ['temp']] = "1" #df_countrySlopes[["CountryProv","casesWeeklyPerc","testsWeeklyPerc"]].to_csv("df_countrySlopes.csv", index=False) df_countrySlopes = ColumnDataSource(df_countrySlopes) gf = GroupFilter(column_name='temp', group="1") view1 = CDSView(source=df_countrySlopes, filters=[gf]) gf = GroupFilter(column_name='temp', group="0") view2 = CDSView(source=df_countrySlopes, filters=[gf]) TOOLTIPS = [ ("Country/Region", "@CountryProv"), ("Cases Rate (%)", "@casesWeeklyPerc"), ("Tests Rate (%)", "@testsWeeklyPerc"), ] p1 = figure(tooltips=TOOLTIPS, tools=",pan,tap,box_zoom,reset", title="Generated on the basis of " + str(rolling) + " day moving average") r1 = p1.scatter('casesWeeklyPerc', 'testsWeeklyPerc', source=df_countrySlopes, size=12, color='#73b2ff', legend_label='Tests Rate > Cases Rate', view=view1) r2 = p1.scatter('casesWeeklyPerc', 'testsWeeklyPerc', source=df_countrySlopes, size=12, color='#ff7f7f', legend_label='Tests Rate < Cases Rate', view=view2) p1.xaxis.axis_label = 'Weekly Rate of Change for Positive Cases(%)' p1.yaxis.axis_label = 'Weekly Rate of Change for Nb. Tests(%)' p1.ray([0], [0], length=0, angle=np.pi, color='white') p1.ray([0], [0], length=0, angle=0, color='white') p1.ray([0], [0], length=0, angle=np.pi / 2, color='white') p1.ray([0], [0], length=0, angle=3 * np.pi / 2, color='white') editplotcolors(p1) slope = Slope(gradient=1, y_intercept=0, line_color='white', line_dash='dashed', line_width=2) p1.add_layout(slope) p1.legend.background_fill_alpha = 0.8 p1.legend.background_fill_color = "#262626" p1.legend.border_line_alpha = 0 p1.legend.label_text_color = "whitesmoke" p1.legend.location = 'top_right' p1.toolbar_location = "right" from bokeh.layouts import row, column, widgetbox return df_countrySlopes, p1
def plot_pair( ax, plotters, numvars, figsize, textsize, kind, scatter_kwargs, # pylint: disable=unused-argument kde_kwargs, hexbin_kwargs, gridsize, # pylint: disable=unused-argument colorbar, # pylint: disable=unused-argument divergences, diverging_mask, divergences_kwargs, flat_var_names, backend_kwargs, marginal_kwargs, show, marginals, point_estimate, point_estimate_kwargs, point_estimate_marker_kwargs, reference_values, reference_values_kwargs, ): """Bokeh pair plot.""" if backend_kwargs is None: backend_kwargs = {} backend_kwargs = { **backend_kwarg_defaults(("dpi", "plot.bokeh.figure.dpi"), ), **backend_kwargs, } if hexbin_kwargs is None: hexbin_kwargs = {} hexbin_kwargs.setdefault("size", 0.5) if marginal_kwargs is None: marginal_kwargs = {} if point_estimate_kwargs is None: point_estimate_kwargs = {} if kde_kwargs is None: kde_kwargs = {} if kind != "kde": kde_kwargs.setdefault("contourf_kwargs", {}) kde_kwargs["contourf_kwargs"].setdefault("fill_alpha", 0) kde_kwargs.setdefault("contour_kwargs", {}) kde_kwargs["contour_kwargs"].setdefault("line_color", "black") kde_kwargs["contour_kwargs"].setdefault("line_alpha", 1) if reference_values: reference_values_copy = {} label = [] for variable in list(reference_values.keys()): if " " in variable: variable_copy = variable.replace(" ", "\n", 1) else: variable_copy = variable label.append(variable_copy) reference_values_copy[variable_copy] = reference_values[variable] difference = set(flat_var_names).difference(set(label)) if difference: warn = [diff.replace("\n", " ", 1) for diff in difference] warnings.warn( "Argument reference_values does not include reference value for: {}" .format(", ".join(warn)), UserWarning, ) if reference_values: reference_values_copy = {} label = [] for variable in list(reference_values.keys()): if " " in variable: variable_copy = variable.replace(" ", "\n", 1) else: variable_copy = variable label.append(variable_copy) reference_values_copy[variable_copy] = reference_values[variable] difference = set(flat_var_names).difference(set(label)) for dif in difference: reference_values_copy[dif] = None if difference: warn = [dif.replace("\n", " ", 1) for dif in difference] warnings.warn( "Argument reference_values does not include reference value for: {}" .format(", ".join(warn)), UserWarning, ) if reference_values_kwargs is None: reference_values_kwargs = {} reference_values_kwargs.setdefault("line_color", "black") reference_values_kwargs.setdefault("fill_color", vectorized_to_hex("C2")) reference_values_kwargs.setdefault("line_width", 1) reference_values_kwargs.setdefault("size", 10) if divergences_kwargs is None: divergences_kwargs = {} divergences_kwargs.setdefault("line_color", "black") divergences_kwargs.setdefault("fill_color", vectorized_to_hex("C1")) divergences_kwargs.setdefault("line_width", 1) divergences_kwargs.setdefault("size", 10) dpi = backend_kwargs.pop("dpi") max_plots = (numvars**2 if rcParams["plot.max_subplots"] is None else rcParams["plot.max_subplots"]) vars_to_plot = np.sum(np.arange(numvars).cumsum() < max_plots) if vars_to_plot < numvars: warnings.warn( "rcParams['plot.max_subplots'] ({max_plots}) is smaller than the number " "of resulting pair plots with these variables, generating only a " "{side}x{side} grid".format(max_plots=max_plots, side=vars_to_plot), UserWarning, ) numvars = vars_to_plot if numvars == 2: offset = 1 else: offset = 2 (figsize, _, _, _, _, markersize) = _scale_fig_size(figsize, textsize, numvars - offset, numvars - offset) if point_estimate_marker_kwargs is None: point_estimate_marker_kwargs = {} point_estimate_marker_kwargs.setdefault("size", markersize) point_estimate_marker_kwargs.setdefault("color", "black") point_estimate_kwargs.setdefault("line_color", "black") point_estimate_kwargs.setdefault("line_width", 2) point_estimate_kwargs.setdefault("line_dash", "solid") tmp_flat_var_names = None if len(flat_var_names) == len(list(set(flat_var_names))): source_dict = dict( zip(flat_var_names, [list(post[-1].flatten()) for post in plotters])) else: tmp_flat_var_names = [ f"{name}__{str(uuid4())}" for name in flat_var_names ] source_dict = dict( zip(tmp_flat_var_names, [list(post[-1].flatten()) for post in plotters])) if divergences: divergenve_name = f"divergences_{str(uuid4())}" source_dict[divergenve_name] = np.array(diverging_mask).astype( bool).astype(int).astype(str) source = ColumnDataSource(data=source_dict) if divergences: source_nondiv = CDSView( source=source, filters=[GroupFilter(column_name=divergenve_name, group="0")]) source_div = CDSView( source=source, filters=[GroupFilter(column_name=divergenve_name, group="1")]) def get_width_and_height(jointplot, rotate): """Compute subplots dimensions for two or more variables.""" if jointplot: if rotate: width = int(figsize[0] / (numvars - 1) + 2 * dpi) height = int(figsize[1] / (numvars - 1) * dpi) else: width = int(figsize[0] / (numvars - 1) * dpi) height = int(figsize[1] / (numvars - 1) + 2 * dpi) else: width = int(figsize[0] / (numvars - 1) * dpi) height = int(figsize[1] / (numvars - 1) * dpi) return width, height if marginals: marginals_offset = 0 else: marginals_offset = 1 if ax is None: ax = [] backend_kwargs.setdefault("width", int(figsize[0] / (numvars - 1) * dpi)) backend_kwargs.setdefault("height", int(figsize[1] / (numvars - 1) * dpi)) for row in range(numvars - marginals_offset): row_ax = [] var1 = (flat_var_names[row + marginals_offset] if tmp_flat_var_names is None else tmp_flat_var_names[row + marginals_offset]) for col in range(numvars - marginals_offset): var2 = (flat_var_names[col] if tmp_flat_var_names is None else tmp_flat_var_names[col]) backend_kwargs_copy = backend_kwargs.copy() if "scatter" in kind: tooltips = [ (var2, f"@{{{var2}}}"), (var1, f"@{{{var1}}}"), ] backend_kwargs_copy.setdefault("tooltips", tooltips) else: tooltips = None if row < col: row_ax.append(None) else: jointplot = row == col and numvars == 2 and marginals rotate = col == 1 width, height = get_width_and_height(jointplot, rotate) if jointplot: ax_ = bkp.figure(width=width, height=height, tooltips=tooltips) else: ax_ = bkp.figure(**backend_kwargs_copy) row_ax.append(ax_) ax.append(row_ax) ax = np.array(ax) else: assert ax.shape == (numvars - marginals_offset, numvars - marginals_offset) # pylint: disable=too-many-nested-blocks for i in range(0, numvars - marginals_offset): var1 = flat_var_names[ i] if tmp_flat_var_names is None else tmp_flat_var_names[i] for j in range(0, numvars - marginals_offset): var2 = (flat_var_names[j + marginals_offset] if tmp_flat_var_names is None else tmp_flat_var_names[j + marginals_offset]) if j == i and marginals: rotate = numvars == 2 and j == 1 var1_dist = plotters[i][-1].flatten() plot_dist( var1_dist, ax=ax[j, i], show=False, backend="bokeh", rotated=rotate, **marginal_kwargs, ) ax[j, i].xaxis.axis_label = flat_var_names[i] ax[j, i].yaxis.axis_label = flat_var_names[j + marginals_offset] elif j + marginals_offset > i: if "scatter" in kind: if divergences: ax[j, i].circle(var1, var2, source=source, view=source_nondiv) else: ax[j, i].circle(var1, var2, source=source) if "kde" in kind: var1_kde = plotters[i][-1].flatten() var2_kde = plotters[j + marginals_offset][-1].flatten() plot_kde( var1_kde, var2_kde, ax=ax[j, i], backend="bokeh", backend_kwargs={}, show=False, **deepcopy(kde_kwargs), ) if "hexbin" in kind: var1_hexbin = plotters[i][-1].flatten() var2_hexbin = plotters[j + marginals_offset][-1].flatten() ax[j, i].grid.visible = False ax[j, i].hexbin( var1_hexbin, var2_hexbin, **hexbin_kwargs, ) if divergences: ax[j, i].circle( var1, var2, source=source, view=source_div, **divergences_kwargs, ) if point_estimate: var1_pe = plotters[i][-1].flatten() var2_pe = plotters[j][-1].flatten() pe_x = calculate_point_estimate(point_estimate, var1_pe) pe_y = calculate_point_estimate(point_estimate, var2_pe) ax[j, i].square(pe_x, pe_y, **point_estimate_marker_kwargs) ax_hline = Span( location=pe_y, dimension="width", **point_estimate_kwargs, ) ax_vline = Span( location=pe_x, dimension="height", **point_estimate_kwargs, ) ax[j, i].add_layout(ax_hline) ax[j, i].add_layout(ax_vline) if marginals: ax[j - 1, i].add_layout(ax_vline) pe_last = calculate_point_estimate( point_estimate, plotters[-1][-1]) ax_pe_vline = Span( location=pe_last, dimension="height", **point_estimate_kwargs, ) ax[-1, -1].add_layout(ax_pe_vline) if numvars == 2: ax_pe_hline = Span( location=pe_last, dimension="width", **point_estimate_kwargs, ) ax[-1, -1].add_layout(ax_pe_hline) if reference_values: x = reference_values_copy[flat_var_names[j + marginals_offset]] y = reference_values_copy[flat_var_names[i]] if x and y: ax[j, i].circle(y, x, **reference_values_kwargs) ax[j, i].xaxis.axis_label = flat_var_names[i] ax[j, i].yaxis.axis_label = flat_var_names[j + marginals_offset] show_layout(ax, show) return ax
'PFD_type': [], 'BANDS': [] }) # Define the tools tools = 'box_zoom,wheel_zoom,pan,reset,save' rel_hover = HoverTool( tooltips=[('Pos. Rank', '@Pos-@RankPos'), ('Rel. Val.', '@REL{0.000 a}')]) rb_hover = HoverTool( tooltips=[('Pos. Rank', '@Pos-@RankPos'), ('Rel. Val. (RB baseline)', '@RBB{0.000 a}')]) hover = HoverTool(tooltips=[('Pos. Rank', '@Pos-@RankPos'), ('Avg Pts.', '@AVG{0.0 a}')]) # Create filters for using views qb = GroupFilter(column_name='Pos', group='QB') rb = GroupFilter(column_name='Pos', group='RB') te = GroupFilter(column_name='Pos', group='TE') wr = GroupFilter(column_name='Pos', group='WR') ppr = GroupFilter(column_name='PPR_type', group='PPR') hppr = GroupFilter(column_name='PPR_type', group='HPPR') sppr = GroupFilter(column_name='PPR_type', group='STD') pfd = GroupFilter(column_name='PFD_type', group='PFD') hpfd = GroupFilter(column_name='PFD_type', group='HPFD') spfd = GroupFilter(column_name='PFD_type', group='STD') ppr_filters = [sppr, hppr, ppr] pfd_filters = [spfd, hpfd, pfd] pos_filters = [qb, rb, te, wr] def callback(l_df_flex=l_df_flex,
def plot_interactive_footprint(self): """Use bokeh to create an interactive algorithm footprint with zoom and hover tooltips. Should avoid problems with overplotting (since we can zoom) and provide better information about instances.""" features = np.array(self.features_2d) instances = self.insts runhistory = self.rh algo = {v: k for k, v in self.algo_name.items()} incumbent = algo['incumbent'] default = algo['default'] source = ColumnDataSource(data=dict(x=features[:, 0], y=features[:, 1])) # Add all necessary information for incumbent and default source.add(instances, 'instance_name') instance_set = [ 'train' if i in self.train_feats.keys() else 'test' for i in instances ] source.add(instance_set, 'instance_set') # train or test for config, name in [(incumbent, 'incumbent'), (default, 'default')]: cost = get_cost_dict_for_config(runhistory, config) source.add([cost[i] for i in instances], '{}_cost'.format(name)) # TODO should be in function good, bad = self._get_good_bad(config) color = [ 1 if idx in good else 0 for idx, i in enumerate(instances) ] # TODO end color = ['blue' if c else 'red' for c in color] self.logger.debug("%s colors: %s", name, str(color)) source.add(color, '{}_color'.format(name)) source.add(source.data['default_color'], 'color') # Define what appears in tooltips hover = HoverTool(tooltips=[ ('instance name', '@instance_name'), ('def cost', '@default_cost'), ('inc_cost', '@incumbent_cost'), ('set', '@instance_set'), ]) # Add radio-button def_inc_callback = CustomJS(args=dict(source=source), code=""" var data = source.data; if (cb_obj.active == 0) { data['color'] = data['default_color']; } else { data['color'] = data['incumbent_color']; } source.change.emit(); """) def_inc_radio_button = RadioButtonGroup( labels=["default", "incumbent"], active=0, callback=def_inc_callback) # Plot x_range = DataRange1d(bounds='auto', start=min(features[:, 0]) - 1, end=max(features[:, 0]) + 1) y_range = DataRange1d(bounds='auto', start=min(features[:, 1]) - 1, end=max(features[:, 1]) + 1) p = figure( plot_height=500, plot_width=600, tools=[hover, 'save', 'wheel_zoom', 'box_zoom', 'pan', 'reset'], active_drag='box_zoom', x_range=x_range, y_range=y_range) # Scatter train and test individually to toggle them train_view = CDSView( source=source, filters=[GroupFilter(column_name='instance_set', group='train')]) test_view = CDSView( source=source, filters=[GroupFilter(column_name='instance_set', group='test')]) train = p.scatter(x='x', y='y', source=source, view=train_view, color='color') test = p.scatter(x='x', y='y', source=source, view=test_view, color='color') p.xaxis.axis_label, p.yaxis.axis_label = 'principal component 1', 'principal component 2' p.xaxis.axis_label_text_font_size = p.yaxis.axis_label_text_font_size = "15pt" train_test_callback = CustomJS(args=dict(source=source, train_view=train, test_view=test), code=""" var data = source.data; if (cb_obj.active == 0) { train_view.visible = true; test_view.visible = true; } else if (cb_obj.active == 1) { train_view.visible = true; test_view.visible = false; } else { train_view.visible = false; test_view.visible = true; } """) train_test_radio_button = RadioButtonGroup( labels=["all", "train", "test"], active=0, callback=train_test_callback) # Export and return if self.output_dir: path = os.path.join(self.output_dir, "content/images/algorithm_footprint.png") export_bokeh(p, path, self.logger) layout = column( p, row(widgetbox(def_inc_radio_button), widgetbox(train_test_radio_button))) return layout
# Bokeh libraries from bokeh.plotting import figure, show from bokeh.io import output_file from bokeh.models import ColumnDataSource, CDSView, GroupFilter # Output to file output_file('west-top-2-standings-race.html', title='Western Conference Top 2 Teams Wins Race') # Create a ColumnDataSource west_cds = ColumnDataSource(west_top_2) # Create views for each team rockets_view = CDSView( source=west_cds, filters=[GroupFilter(column_name='teamAbbr', group='HOU')]) warriors_view = CDSView( source=west_cds, filters=[GroupFilter(column_name='teamAbbr', group='GS')]) # Create and configure the figure west_fig = figure(x_axis_type="datetime", plot_height=300, plot_width=600, title="Western Conference Top 2 Teams Wins Race, 2017-18", x_axis_label='Date', y_axis_label='Wins', toolbar_location=None) # Render the race as step lines west_fig.step('stDate', 'gameWon',
team_stats = pd.read_csv('2017-18_teamBoxScore.csv', parse_dates=['gmDate']) standings = pd.read_csv('2017-18_standings.csv', parse_dates=['stDate']) # Output to file # output_file('east-top-2-standings-race.html', # title='Eastern Conference Top 2 Teams Wins Race') output_file('east-west-top-2-standings-race.html', title='Conference Top 2 Teams Wins Race') # Create a ColumnDataSource standings_cds = ColumnDataSource(standings) # Create views for each team celtics_view = CDSView( source=standings_cds, filters=[GroupFilter(column_name='teamAbbr', group='BOS')]) raptors_view = CDSView( source=standings_cds, filters=[GroupFilter(column_name='teamAbbr', group='TOR')]) # Create and configure the figure east_fig = figure(x_axis_type='datetime', plot_height=300, plot_width=600, title='Eastern Conference Top 2 Teams Wins Race, 2017-18', x_axis_label='Date', y_axis_label='Wins', toolbar_location=None) # Render the race as step lines east_fig.step('stDate',
def figures_chisq_simple(init_group, df_chisq): df_chisq["case_ma07"] = df_chisq["case_ma07"] / 1000 df_chisq["threshold_min_eps"] = df_chisq["threshold_min_eps"] / 1000 df_chisq["threshold_max_eps"] = df_chisq["threshold_max_eps"] / 1000 source = ColumnDataSource(df_chisq) gf = GroupFilter(column_name='CountryProv', group=init_group) view1 = CDSView(source=source, filters=[gf]) booleans = [ True if float(case_detrended) >= 0 else False for case_detrended in source.data['case_detrended'] ] t1_view = CDSView(source=source, filters=[gf, BooleanFilter(booleans)]) booleans = [ True if float(case_detrended) < 0 else False for case_detrended in source.data['case_detrended'] ] t2_view = CDSView(source=source, filters=[gf, BooleanFilter(booleans)]) p_b1_tooltip = [ ("Date", "@Date{%F}"), ("Thresholds Min", "@threshold_min_eps"), ("Thresholds Max", "@threshold_max_eps"), ("Positive Cases", "@case_ma07"), ] p_b2_tooltip = [ ("Date", "@Date{%F}"), ("Detrended Cases", "@case_detrended"), ] p_formatters = {'@Date': 'datetime'} plot_size_and_tools = { 'tools': ['box_select', 'reset', 'help', 'box_zoom'], 'x_axis_type': 'datetime' } # FIXME couldnt do p_a1.line below, so using hack of varea p_b1 = figure(title="Positive cases", **plot_size_and_tools) c_b1b = p_b1.varea(x='Date', y1='threshold_min_eps', y2='threshold_max_eps', source=source, color='grey', view=view1, legend_label="Range of 14-day expectation") c_b1a = p_b1.circle(x='Date', y='case_ma07', source=source, color='red', view=view1, legend_label="7-day average") p_b2 = figure( title="7-day moving average cases minus thresholds -> Excess cases", **plot_size_and_tools) c_b2a = p_b2.scatter(x='Date', y='case_detrended', source=source, color='#73b2ff', legend_label="Above Threshold", view=t2_view) c_b2a = p_b2.scatter(x='Date', y='case_detrended', source=source, color='#ff7f7f', legend_label="Below Threshold", view=t1_view) editplotcolors(p_b1) editplotcolors(p_b2) p_b1.xaxis.axis_label = 'Date' p_b1.yaxis.axis_label = 'Positive Cases (in thousands)' p_b2.xaxis.axis_label = 'Date' p_b2.yaxis.axis_label = 'Detrended Number of Cases' p_b2.x_range = p_b1.x_range p_b1.add_tools(HoverTool(tooltips=p_b1_tooltip, formatters=p_formatters)) p_b2.add_tools(HoverTool(tooltips=p_b2_tooltip, formatters=p_formatters)) p_b1.legend.location = 'top_left' p_b1.legend.background_fill_alpha = 0.8 p_b1.legend.background_fill_color = "#262626" p_b1.legend.border_line_alpha = 0 p_b1.legend.label_text_color = "whitesmoke" p_b2.legend.background_fill_alpha = 0.8 p_b2.legend.background_fill_color = "#262626" p_b2.legend.border_line_alpha = 0 p_b2.legend.label_text_color = "whitesmoke" p_b2.legend.location = 'bottom_left' return source, c_b1b, p_b1, p_b2