def systemEvolutionBarPlot(df, yLabel, values): with Timer(key='systemEvolutionBarPlot', verbose=True): p = Bar(df, label='snapshot', values=values, agg='sum', stack='software', legend='bottom_left', bar_width=0.5, xlabel="Snapshots", ylabel=yLabel, responsive=True, height=200,tools='hover') glyph_renderers = p.select(GlyphRenderer) bar_source = [glyph_renderers[i].data_source for i in range(len(glyph_renderers))] hover = p.select(HoverTool) hover.tooltips = [ ('software',' @software'), ('value', '@height'), ] p.xaxis.formatter=FuncTickFormatter.from_py_func(getWeekStringTick) p.axis.minor_tick_line_color = None p.background_fill_color = "#fafafa" p.legend.location = "top_left" p.toolbar.logo = None p.toolbar_location = None legend=p.legend[0].legends p.legend[0].legends=[] l = Legend( location=(0, -30)) l.items=legend p.add_layout(l, 'right') return p
def get_user_type(): subscriber_count = 0 customer_count = 0 unknown_count = 0 start_date_range = datetime.datetime.strptime(request.form['start_date'], "%Y-%m-%d").date() end_date_jinja2 = datetime.datetime.strptime(request.form['stop_date'], "%Y-%m-%d").date() end_date_range = datetime.datetime.strptime(request.form['stop_date'], "%Y-%m-%d").date() date_start = str(start_date_range) date_stop = str(end_date_range) date_stop_jinja2 = str(end_date_jinja2) #df = get_dataframe(start_date_range, end_date_range) if 'submitDateFilter' in request.form: df = date_form(df_init, date_start, date_stop) if 'submit_station' in request.form: df = station_form(df_init, date_start, date_stop) if 'submit_gender' in request.form: df = gender_form(df_init, date_start, date_stop) if 'submit_age' in request.form: df = age_form(df_init, date_start, date_stop) if 'submit_time' in request.form: df = time_form(df_init, date_start, date_stop) df = df_init user_type_series = df['usertype'].values for x in user_type_series: if x == 'Subscriber': subscriber_count += 1 elif x == 'Customer': customer_count += 1 else: unknown_count += 1 if unknown_count == 0: #customer_type = [subscriber_count, customer_count] df = pd.DataFrame({'User Type': ['Subscriber', 'Customer'], 'Type': [subscriber_count, customer_count]}) else: #customer_type = [subscriber_count, customer_count, unknown_count] df = pd.DataFrame({'User Type': ['Subscriber', 'Customer', 'Null'], 'Type': [subscriber_count, customer_count, unknown_count]}) #b = Bar(customer_type, title="Bar example", label='categories', ylabel='values', width=400, height=400) b = Bar(df, title="User Types", label='User Type', values='Type') b.select(dict(type=GlyphRenderer)) b.left[0].formatter.use_scientific = False scriptb, divb = components(b) return render_template('userType.html', s=subscriber_count, c=customer_count, u=unknown_count, d1=date_start, d2=date_stop_jinja2, scriptb=scriptb, divb=divb)
def author_frequency_barplot(nb_top_ners=50, tf=True): if tf: output_file('../figures/tf_authors.html') ner_freqs = pickle.load(open('../workspace/tf.m', "rb")) else: output_file('../figures/df_authors.html') ner_freqs = pickle.load(open('../workspace/df.m', "rb")) top_ners = [w for w, _ in ner_freqs.most_common(nb_top_ners)] top_freqs = [c for _, c in ner_freqs.most_common(nb_top_ners)] names = [] for name in top_ners: name = name.replace('*', '') if '(' in name: name = name.split('(')[0].strip() name = ' '.join([n.lower().capitalize() for n in name.split('_')]) name = ''.join([ c for c in unicodedata.normalize('NFKD', name) if not unicodedata.combining(c) ]) names.append(name) data = pd.DataFrame({'values': top_freqs[:25], 'labels': names[:25]}) bar1 = Bar(data, label=CatAttr(columns=['labels'], sort=False), values='values', title='Author Frequency (1-25)', width=800, height=400) xaxis = bar1.select(dict(type=Axis))[1] xaxis.major_label_standoff = 0 xaxis.major_label_orientation = np.pi / 2 xaxis.major_label_standoff = 6 xaxis.major_tick_out = 0 data = pd.DataFrame({'values': top_freqs[25:50], 'labels': names[25:50]}) bar2 = Bar(data, label=CatAttr(columns=['labels'], sort=False), values='values', title='Author Frequency (25-50)', width=800, height=400) xaxis = bar2.select(dict(type=Axis))[1] xaxis.major_label_standoff = 0 xaxis.major_label_orientation = np.pi / 2 xaxis.major_label_standoff = 6 xaxis.major_tick_out = 0 p = vplot(bar1, bar2) save(p)
def plot_scores(self, all_scores): if all_scores is None: return None scores = all_scores[0][:] scores = np.hstack((scores,np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"precision": scores} s1 = Bar(data, cat=class_labels, title="Per Class Precision", xlabel='categories', ylabel='precision', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#b2df8a"]) hover = s1.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('precision', '@precision'), ]) scores = all_scores[1][:] scores = np.hstack((scores,np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"recall": scores} s2 = Bar(data, cat=class_labels, title="Per Class Recall", xlabel='categories', ylabel='recall', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#a6cee3"]) hover = s2.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('recall', '@recall'), ]) data = {"support": all_scores[3]} s3 = Bar(data, cat=self.class_labels, title="Per Class Support", xlabel='categories', ylabel='support', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#6a3d9a"]) hover = s3.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('support', '@support'), ]) p = hplot(s1, s2, s3) script, div = components(p) return (script, div)
def calculate_average_accuracy(self, confusion_matrix): if confusion_matrix is None: return None scores = [] n = np.shape(confusion_matrix)[0] mean_acc = 0 for index, r in enumerate(confusion_matrix): ss = sum(r) if ss != 0: scores.append(float(r[index]) / ss) scores = np.hstack((scores, np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"accuracy": scores} s = Bar(data, cat=class_labels, title="Per Class Accuracy", xlabel='categories', ylabel='accuracy', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#ec5d5e"]) hover = s.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('accuracy', '@accuracy'), ]) p = hplot(s) script, div = components(p) return (script, div)
def make_datasources_for_comparison(idd_dataframe, years, measure_codes): # filter out SHAx measures calculated with METH2012 row_filter = (idd_dataframe.methodo == '0') & (idd_dataframe.measure_code.isin(measure_codes)) bar_charts = [] countries = set() sources = {} for year in years: chart_data = idd_dataframe[row_filter & (idd_dataframe.year == year)] [countries.add(x) for x in chart_data.location_name.unique()] bar = Bar(data=chart_data, stack='measure_name', values='observation', label='location_name', legend='top_right', bar_width=1) bar_charts.append(bar) base_source_df = \ pd.concat([x.to_df() for x in bar.select(dict(type=ColumnDataSource))]) sources['_' + year] = ColumnDataSource(base_source_df) # return the dictionary of ColumnDataSources and 1 bar chart -- # we want to keep a bar chart so we can reconstruct the legend, later return sources, bar_charts[0], countries
def calculate_average_accuracy(self, confusion_matrix): if confusion_matrix is None: return None scores = [] n = np.shape(confusion_matrix)[0] mean_acc = 0 for index, r in enumerate(confusion_matrix): ss = sum(r) if ss != 0: scores.append(float(r[index]) / ss) scores = np.hstack((scores,np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"accuracy": scores} s = Bar(data, cat=class_labels, title="Per Class Accuracy", xlabel='categories', ylabel='accuracy', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#ec5d5e"]) hover = s.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('accuracy', '@accuracy'), ]) p = hplot(s) script, div = components(p) return (script, div)
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[ ("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0])) ]) barchart.add_tools(hover) return barchart
def generate_chart(team): data = generate_data(team) selectable_years = list(map(str, data.index.unique())) # Generate the chart UNFORTUNATELY using the high level plots. Streaming # a bunch of quads resulted in lots of graphics corruption when switching # teams. I would rather have the plots work all the time but really slow # then have the plot show the wrong information. barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Year', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan - " + team barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None # Hacky tooltips for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['Year'] place = data['Place'].loc[data['Year'] == year] score = data['Total Score'].loc[data['Year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("Year", '@Year'), ("Selection", '@event'), ("Event Score", '@height'), ("Total Score", '%.2f' % score.values[0]), ("Overall Place", '%d' % place.values[0])]) barchart.add_tools(hover) return barchart
def output_chart(issues_df, output_mode='static'): import datetime import bokeh from bokeh.models import HoverTool # Add timestamp to title issues_chart = Bar(issues_df, label='value_delivered', values='status', agg='count', stack='status', title=ISSUES_TITLE + " (Updated " + datetime.datetime.now().strftime('%m/%d/%Y') + ")", xlabel="Value Delivered", ylabel="Number of Use Cases", legend='top_right', tools='hover', color=brewer["GnBu"][3]) issues_chart.plot_width = DESTINATION_FRAME_WIDTH - (HTML_BODY_MARGIN * 2) issues_chart.plot_height = DESTINATION_FRAME_HEIGHT - (HTML_BODY_MARGIN * 2) issues_chart.logo = None issues_chart.toolbar_location = None hover = issues_chart.select(dict(type=HoverTool)) hover.tooltips = [("Value Delivered", "$x")] #--- Configure output --- reset_output() if output_mode == 'static': # Static file. CDN is most space efficient output_file(ISSUES_FILE, title=ISSUES_TITLE, autosave=False, mode='cdn', root_dir=None) # Generate file save(issues_chart, filename=ISSUES_FILE) elif output_mode == 'notebook': output_notebook() # Show inline show(issues_chart) else: # Server (using internal server IP, rather than localhost or external) session = bokeh.session.Session(root_url=BOKEH_SERVER_IP, load_from_config=False) output_server("ddod_chart", session=session) show(issues_chart)
def plotG(data, ttl): fig = Bar(data, label=CatAttr(columns=['Group'], sort=False), values='Height', group='Spec', legend=True, plot_width=1000, plot_height=600, title=ttl, ylabel='MiB/s') fig.title.text_font_size = '18pt' # Show value in img fig.add_tools(HoverTool()) hover = fig.select(dict(type=HoverTool)) hover.tooltips = [('Spec', ' $x'), ('MiB/s', ' @height')] return fig
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0]))]) barchart.add_tools(hover) return barchart
def output_chart(issues_df,output_mode='static'): import datetime import bokeh from bokeh.models import HoverTool # Add timestamp to title issues_chart = Bar(issues_df, label='value_delivered', values='status', agg='count', stack='status', title=ISSUES_TITLE+" (Updated "+datetime.datetime.now().strftime('%m/%d/%Y')+")", xlabel="Value Delivered",ylabel="Number of Use Cases", legend='top_right', tools='hover', color=brewer["GnBu"][3] ) issues_chart.plot_width = DESTINATION_FRAME_WIDTH - (HTML_BODY_MARGIN * 2) issues_chart.plot_height = DESTINATION_FRAME_HEIGHT - (HTML_BODY_MARGIN * 2) issues_chart.logo = None issues_chart.toolbar_location = None hover = issues_chart.select(dict(type=HoverTool)) hover.tooltips = [ ("Value Delivered", "$x")] #--- Configure output --- reset_output() if output_mode == 'static': # Static file. CDN is most space efficient output_file(ISSUES_FILE, title=ISSUES_TITLE, autosave=False, mode='cdn', root_dir=None ) # Generate file save(issues_chart,filename=ISSUES_FILE) elif output_mode == 'notebook': output_notebook() # Show inline show(issues_chart) else: # Server (using internal server IP, rather than localhost or external) session = bokeh.session.Session(root_url = BOKEH_SERVER_IP, load_from_config=False) output_server("ddod_chart", session=session) show(issues_chart)
def display_graph(df): df['tweet_ratio'] = 1.0 * df['count'] / df['population'] df['partisan_split'] = [df['partisan_split'].iloc[i] if df['main_party'].iloc[i] == 'Democratic'\ else -df['partisan_split'].iloc[i] for i in range(len(df))] df = df[['tweet_ratio', 'partisan_split', 'sentiment', 'location']] colors = [] for i in range(len(df)): if df['sentiment'].iloc[i] < df['sentiment'].describe()['25%']: colors.append('quartile_1') elif (df['sentiment'].iloc[i] < df['sentiment'].describe()['50%']) & ( df['sentiment'].iloc[i] > df['sentiment'].describe()['25%']): colors.append('quartile_2') elif (df['sentiment'].iloc[i] < df['sentiment'].describe()['75%']) & ( df['sentiment'].iloc[i] > df['sentiment'].describe()['50%']): colors.append('quartile_3') else: colors.append('quartile_4') df['colors'] = colors source = ColumnDataSource(df) #p = figure(tools='pan, hover, box_zoom, reset, wheel_zoom') p = Bar( values='tweet_ratio', label='partisan_split', color='colors', palette=YlOrRd4[::-1], data=df, legend=False, plot_width=1100, plot_height=700, stack='location', title= 'Tweets per Capita vs Partisan Split and average tweet sentiment (redder = higher sentiment)', tools='hover') p.yaxis.axis_label = 'Tweet per Person' p.xaxis.axis_label = 'Partisan Split (positive number = more self identified democrats)' hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([('State', '@location'), ('Tweet per Capita', '@height'), ('Partisanship', '@partisan_split'), ('sentiment', '@colors')]) # output_file("bar.html") return p
def generate_single_bar(df, time_str, color_dict, colors): df = df[df.percent_total >= 3] title = "Failure Rate " + time_str fill = [color_dict[model_key] if model_key in color_dict else 'grey' for model_key in df.model] source = ColumnDataSource(dict(color=[c for c in df['color']], model=[m for m in df['model']], failure_rate=[f for f in df['failure_rate']], count=[co for co in df['count']])) plot = Bar(df, 'model', values='failure_rate', title=title, source=source, tools=['hover'],color='color', legend=None) # outline_line_color="color", border_fill_color='color', hover = plot.select(dict(type=HoverTool)) hover.tooltips = [ ("Model ", "@model"), ("Failure rate ", "@y"), ("Number of drives", "@count") #("Time ", "@timeline"), ] hover.mode = 'mouse' plot.xaxis.axis_label = 'Model Serial Number' plot.yaxis.axis_label = 'Naive Failure Rate' plot.title_text_font_size="18px" plot.grid.grid_line_alpha = 0 plot.ygrid.grid_line_color = None plot.toolbar.logo = None plot.outline_line_width = 0 plot.outline_line_color = "white" plot.plot_height = 600 plot.plot_width = 800 plot.xaxis.major_tick_line_color = None plot.yaxis.major_tick_line_color = None plot.xaxis.axis_line_width = 2 plot.yaxis.axis_line_width = 2 plot.title.text_font_size = '16pt' plot.xaxis.axis_label_text_font_size = "14pt" plot.xaxis.major_label_text_font_size = "14pt" plot.yaxis.axis_label_text_font_size = "14pt" plot.yaxis.major_label_text_font_size = "14pt" return(plot)
def plot_scores(self, all_scores): if all_scores is None: return None scores = all_scores[0][:] scores = np.hstack((scores, np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"precision": scores} s1 = Bar(data, cat=class_labels, title="Per Class Precision", xlabel='categories', ylabel='precision', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#b2df8a"]) hover = s1.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('precision', '@precision'), ]) scores = all_scores[1][:] scores = np.hstack((scores, np.mean(scores))) class_labels = self.class_labels[:] class_labels.append('average') data = {"recall": scores} s2 = Bar(data, cat=class_labels, title="Per Class Recall", xlabel='categories', ylabel='recall', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#a6cee3"]) hover = s2.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('recall', '@recall'), ]) data = {"support": all_scores[3]} s3 = Bar(data, cat=self.class_labels, title="Per Class Support", xlabel='categories', ylabel='support', width=500, height=500, tools="pan,resize,box_zoom,hover,save,reset", stacked=True, palette=["#6a3d9a"]) hover = s3.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('support', '@support'), ]) p = hplot(s1, s2, s3) script, div = components(p) return (script, div)
def show_kvm_exit_types(df, task_re, label): df = df[df['event'] == 'kvm_exit'] df = df[df['task_name'].str.match(task_re)] # the next_comm column contains the exit code exit_codes = Series(KVM_EXIT_REASONS) # add new column congaining the exit reason in clear text df['exit_reason'] = df['next_comm'].map(exit_codes) time_span_msec = get_time_span_msec(df) df.drop( ['cpu', 'duration', 'event', 'next_pid', 'pid', 'next_comm', 'usecs'], inplace=True, axis=1) # Get the list of exit reasons, sorted alphabetically reasons = pandas.unique(df.exit_reason.ravel()).tolist() reasons.sort() # group by task name then exit reasons gb = df.groupby(['task_name', 'exit_reason']) # number of exit types size_series = gb.size() df = size_series.to_frame('count') df.reset_index(inplace=True) p = Bar(df, label='task_name', values='count', stack='exit_reason', title="KVM Exit types per task (%s, %d msec window)" % (label, time_span_msec), legend='top_right', tools="resize,hover,save", width=1000, height=800) p._xaxis.axis_label = "Task Name" p._xaxis.axis_label_text_font_size = "12pt" p._yaxis.axis_label = "Exit Count (sum)" p._yaxis.axis_label_text_font_size = "12pt" # Cannot find a way to display the exit reason in the tooltip # from bokeh.models.renderers import GlyphRenderer # glr = p.select(dict(type=GlyphRenderer)) # bar_source = glr[0].data_source # print bar_source.data # bar_source = glr[1].data_source # bar_source.data['exit_reason'] = ['HOHO'] hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("task", "$x"), # {"reason", "@exit_reason"}, ("count", "@height") ]) # specify how to output the plot(s) # table with counts gb = df.groupby(['exit_reason']) keys = gb.groups.keys() dfr_list = [] for reason in keys: dfr = gb.get_group(reason) # drop the exit reason column dfr = dfr.drop(['exit_reason'], axis=1) # rename the count column with the reason name dfr.rename(columns={'count': reason}, inplace=True) # set the task name as the index dfr.set_index('task_name', inplace=True) dfr_list.append(dfr) # concatenate all task columns into 1 dataframe that has the exit reason as the index # counts for missing exit reasons will be set to NaN dft = pandas.concat(dfr_list, axis=1) dft.fillna(0, inplace=True) # Add a total column dft['TOTAL'] = dft.sum(axis=1) sfmt = StringFormatter(text_align='center', font_style='bold') nfmt = NumberFormatter(format='0,0') col_names = list(dft.columns.values) col_names.sort() # move 'TOTAL' at end of list col_names.remove('TOTAL') col_names.append('TOTAL') # convert index to column name dft.reset_index(level=0, inplace=True) dft.rename(columns={'index': 'Task'}, inplace=True) columns = [ TableColumn(field=name, title=name, formatter=nfmt) for name in col_names ] columns.insert(0, TableColumn(field='Task', title='Task', formatter=sfmt)) table = DataTable(source=ColumnDataSource(dft), columns=columns, width=1000, row_headers=False, height='auto') output_html(vplot(p, table), 'kvm-types', task_re) '''
def dashboard(): # Creating all the queries items = getItems(suffix='items', apiKey=apiKey) users = getItems(suffix='users', apiKey=apiKey) # %% Creating df defining the ratio of right swipes to left swipes df_right_left = pd.DataFrame(items) # Grouping by, and performing different aggregations df_right_left = df_right_left[[ 'category', 'nuSwipesRight', 'nuSwipesLeft' ]].groupby('category', as_index=False).agg({ 'nuSwipesRight': 'sum', 'nuSwipesLeft': 'sum' }) # Finding the ratio df_right_left['RightOverLeft'] = df_right_left['nuSwipesRight'].div( df_right_left['nuSwipesLeft']) # Creating the first bar chart p1 = Bar(data=df_right_left, label='category', values='RightOverLeft', title="Category vs Right-Left Ratio", color='category', tools='hover', ylabel='Right / Left Swipes', agg='mean', legend=False) # Defining the hover tools hover = p1.select(dict(type=HoverTool)) # Defining p1 tooltips hover.tooltips = [('Value', '@y'), ('Category', '@x')] #show(p1) # %% Creating df of time-per-controller variables df_users = pd.io.json.json_normalize(users) df_users = df_users.filter(regex='timePerController') cols = [col.split('.')[1] for col in df_users.columns.values] df_users.columns = cols # Melting df df_users = pd.melt(df_users, value_name='TimeSpent', var_name='Variable') # Creating p2 p2 = Bar(data=df_users, label='Variable', values='TimeSpent', title='Average Time Spent on Controllers', color='Variable', tools='hover', agg='mean', legend=False) # Defining the hover tools hover = p2.select(dict(type=HoverTool)) # Defining p1 tooltips hover.tooltips = [('Value', '@y'), ('Controller Type', '@x')] # show(p2) # Defining dashboard layout p1_script, p1_div = components(p1) p2_script, p2_div = components(p2) js_resources = INLINE.render_js() css_resources = INLINE.render_css() tab1 = Panel(child=p1, title='Right-Left Ratio') tab2 = Panel(child=p2, title='Time on Controllers') tab_plot = Tabs(tabs=[tab1, tab2]) #show(tab_plot) #script, div = components(dict(plot=tabs)) script, div = components(dict(plot=tab_plot)) return (render_template('index.html', script=script, div=div, js_resources=js_resources, css_resources=css_resources))
value_vars=['bronze', 'silver', 'gold', 'total'], value_name='medal_count', var_name='medal') bar1 = Bar(df1, values='medal_count', label='abbr', agg='mean', group=['medal'], title="Olympics Mdeals", color=['brown', 'silver', 'gold', 'green'], plot_width=1000, tools='hover') # set the tooltip for detail information of the bars hover1 = bar1.select(dict(type=HoverTool)) hover1.tooltips = [('medals', '@height'), ('Country', '@abbr')] output_file("bar1.html") # melt the data for second bar chart df2 = pd.melt(df, id_vars=['abbr'], value_vars=['bronze', 'silver', 'gold'], value_name='medal_count', var_name='medal') bar2 = Bar(df2, values='medal_count', label='abbr', agg='mean',
def make_plot(dataframe=pd.DataFrame(), highlight=[], top=100, minvalues=0.01, stacked=True, lgaxis=True, errorbar=True, showS1=True, showST=True): """ Basic method to plot first and total order sensitivity indices. This is the method to generate a Bokeh plot similar to the burtin example template at the Bokeh website. For clarification, parameters refer to an input being measured (Tmax, C, k2, etc.) and stats refer to the 1st or total order sensitivity index. Parameters ----------- dataframe : pandas dataframe Dataframe containing sensitivity analysis results to be plotted. highlight : lst, optional List of strings indicating which parameter wedges will be highlighted. top : int, optional Integer indicating the number of parameters to display (highest sensitivity values) (after minimum cutoff is applied). minvalues : float, optional Cutoff minimum for which parameters should be plotted. Applies to total order only. stacked : bool, optional Boolean indicating in bars should be stacked for each parameter (True) or unstacked (False). lgaxis : bool, optional Boolean indicating if log axis should be used (True) or if a linear axis should be used (False). errorbar : bool, optional Boolean indicating if error bars are shown (True) or are omitted (False). showS1 : bool, optional Boolean indicating whether 1st order sensitivity indices will be plotted (True) or omitted (False). showST : bool, optional Boolean indicating whether total order sensitivity indices will be plotted (True) or omitted (False). **Note if showS1 and showST are both false, the plot will default to showing ST data only instead of a blank plot** Returns -------- p : bokeh figure A Bokeh figure of the data to be plotted """ df = dataframe top = int(top) # Initialize boolean checks and check dataframe structure if (('S1' not in df) or ('ST' not in df) or ('Parameter' not in df) or ('ST_conf' not in df) or ('S1_conf' not in df)): raise Exception('Dataframe not formatted correctly') # Remove rows which have values less than cutoff values df = df[df['ST'] > minvalues] df = df.dropna() # Only keep top values indicated by variable top df = df.sort_values('ST', ascending=False) df = df.head(top) df = df.reset_index(drop=True) # Create arrays of colors and order labels for plotting colors = ["#a1d99b", "#31a354", "#546775", "#225ea8"] s1color = np.array(["#31a354"] * df.S1.size) sTcolor = np.array(["#a1d99b"] * df.ST.size) errs1color = np.array(["#225ea8"] * df.S1.size) errsTcolor = np.array(["#546775"] * df.ST.size) firstorder = np.array(["1st (S1)"] * df.S1.size) totalorder = np.array(["Total (ST)"] * df.S1.size) # Add column indicating which parameters should be highlighted tohighlight = df.Parameter.isin(highlight) df['highlighted'] = tohighlight back_color = { True: "#aeaeb8", False: "#e6e6e6", } # Switch to bar chart if dataframe shrinks below 5 parameters if len(df) <= 5: if stacked is False: data = { 'Sensitivity': pd.Series.append(df.ST, df.S1), 'Parameter': pd.Series.append(df.Parameter, df.Parameter), 'Order': np.append(np.array(['ST'] * len(df)), np.array(['S1'] * len(df))), 'Confidence': pd.Series.append(df.ST_conf, df.S1_conf) } p = Bar(data, values='Sensitivity', label='Parameter', group='Order', legend='top_right', color=["#31a354", "#a1d99b"], ylabel='Sensitivity Indices') else: data = { 'Sensitivity': pd.Series.append(df.S1, (df.ST - df.S1)), 'Parameter': pd.Series.append(df.Parameter, df.Parameter), 'Order': np.append(np.array(['S1'] * len(df)), np.array(['ST'] * len(df))), 'Confidence': pd.Series.append(df.S1_conf, df.ST_conf) } p = Bar(data, values='Sensitivity', label='Parameter', color='Order', legend='top_right', stack='Order', palette=["#31a354", "#a1d99b"], ylabel='Sensitivity Indices') return p # Create Dictionary of colors stat_color = OrderedDict() error_color = OrderedDict() for i in range(0, 2): stat_color[i] = colors[i] # Reset index of dataframe. for i in range(2, 4): error_color[i] = colors[i] # Sizing parameters width = 800 height = 800 inner_radius = 90 outer_radius = 300 - 10 # Determine wedge size based off number of parameters big_angle = 2.0 * np.pi / (len(df) + 1) # Determine division of wedges for plotting bars based on # stats plotted # for stacked or unstacked bars if stacked is False: small_angle = big_angle / 5 else: small_angle = big_angle / 3 # tools enabled for bokeh figure plottools = "hover, wheel_zoom, save, reset, resize" # , tap" # Initialize figure with tools, coloring, etc. p = figure(plot_width=width, plot_height=height, title="", x_axis_type=None, y_axis_type=None, x_range=(-350, 350), y_range=(-350, 350), min_border=0, outline_line_color="#e6e6e6", background_fill_color="#e6e6e6", border_fill_color="#e6e6e6", tools=plottools) # Specify labels for hover tool hover = p.select(dict(type=HoverTool)) hover.tooltips = [("Order", "@Order"), ("Parameter", "@Param"), ("Sensitivity", "@Sens"), ("Confidence", "@Conf")] hover.point_policy = "follow_mouse" p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None # annular wedges divided into smaller sections for bars # Angles for axial line placement num_lines = np.arange(0, len(df) + 1, 1) line_angles = np.pi / 2 - big_angle / 2 - num_lines * big_angle # Angles for data placement angles = np.pi / 2 - big_angle / 2 - df.index.to_series() * big_angle # circular axes and labels minlabel = min(round(np.log10(min(df.ST))), round(np.log10(min(df.S1)))) labels = np.power(10.0, np.arange(0, minlabel - 1, -1)) # Set max radial line to correspond to 1.1 * maximum value + error maxvalST = max(df.ST + df.ST_conf) maxvalS1 = max(df.S1 + df.S1_conf) maxval = max(maxvalST, maxvalS1) labels = np.append(labels, 0.0) labels[0] = round(1.1 * maxval, 1) # Determine if radial axis are log or linearly scaled if lgaxis is True: radii = (((np.log10(labels / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) radii[-1] = inner_radius else: labels = np.delete(labels, -2) radii = (outer_radius - inner_radius) * labels / labels[0] + inner_radius # Convert sensitivity values to the plotted values # Same conversion as for the labels above # Also calculate the angle to which the bars are placed # Add values to the dataframe for future reference cols = np.array(['S1', 'ST']) for statistic in range(0, 2): if lgaxis is True: radius_of_stat = ( ((np.log10(df[cols[statistic]] / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) lower_of_stat = (((np.log10( (df[cols[statistic]] - df[cols[statistic] + '_conf']) / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) higher_of_stat = (((np.log10( (df[cols[statistic]] + df[cols[statistic] + '_conf']) / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) else: radius_of_stat = ((outer_radius - inner_radius) * df[cols[statistic]] / labels[0] + inner_radius) lower_of_stat = ( (outer_radius - inner_radius) * (df[cols[statistic]] - df[cols[statistic] + '_conf']) / labels[0] + inner_radius) higher_of_stat = ((outer_radius - inner_radius) * ( (df[cols[statistic]] + df[cols[statistic] + '_conf']) / labels[0]) + inner_radius) if stacked is False: startA = -big_angle + angles + (2 * statistic + 1) * small_angle stopA = -big_angle + angles + (2 * statistic + 2) * small_angle df[cols[statistic] + '_err_angle'] = pd.Series( (startA + stopA) / 2, index=df.index) else: startA = -big_angle + angles + (1) * small_angle stopA = -big_angle + angles + (2) * small_angle if statistic == 0: df[cols[statistic] + '_err_angle'] = pd.Series( (startA * 2 + stopA) / 3, index=df.index) if statistic == 1: df[cols[statistic] + '_err_angle'] = pd.Series( (startA + stopA * 2) / 3, index=df.index) df[cols[statistic] + 'radial'] = pd.Series(radius_of_stat, index=df.index) df[cols[statistic] + 'upper'] = pd.Series(higher_of_stat, index=df.index) df[cols[statistic] + 'lower'] = pd.Series(lower_of_stat, index=df.index) df[cols[statistic] + '_start_angle'] = pd.Series(startA, index=df.index) df[cols[statistic] + '_stop_angle'] = pd.Series(stopA, index=df.index) # df[cols[statistic]+'_err_angle'] = pd.Series((startA+stopA)/2, # index=df.index) inner_rad = np.ones_like(angles) * inner_radius df[cols[statistic] + 'lower'] = df[cols[statistic] + 'lower'].fillna(90) # Store plotted values into dictionary to be add glyphs pdata = pd.DataFrame({ 'x': np.append(np.zeros_like(inner_rad), np.zeros_like(inner_rad)), 'y': np.append(np.zeros_like(inner_rad), np.zeros_like(inner_rad)), 'ymin': np.append(inner_rad, inner_rad), 'ymax': pd.Series.append(df[cols[1] + 'radial'], df[cols[0] + 'radial']).reset_index(drop=True), 'starts': pd.Series.append(df[cols[1] + '_start_angle'], df[cols[0] + '_start_angle']).reset_index(drop=True), 'stops': pd.Series.append(df[cols[1] + '_stop_angle'], df[cols[0] + '_stop_angle']).reset_index(drop=True), 'Param': pd.Series.append(df.Parameter, df.Parameter).reset_index(drop=True), 'Colors': np.append(sTcolor, s1color), 'Error Colors': np.append(errsTcolor, errs1color), 'Conf': pd.Series.append(df.ST_conf, df.S1_conf).reset_index(drop=True), 'Order': np.append(totalorder, firstorder), 'Sens': pd.Series.append(df.ST, df.S1).reset_index(drop=True), 'Lower': pd.Series.append(df.STlower, df.S1lower).reset_index(drop=True), 'Upper': pd.Series.append( df.STupper, df.S1upper, ).reset_index(drop=True), 'Err_Angle': pd.Series.append( df.ST_err_angle, df.S1_err_angle, ).reset_index(drop=True) }) # removed S1 or ST values if indicated by input if showS1 is False: pdata = pdata.head(len(df)) if showST is False: pdata = pdata.tail(len(df)) # convert dataframe to ColumnDataSource for glyphs pdata_s = ColumnDataSource(pdata) colors = [back_color[highl] for highl in df.highlighted] p.annular_wedge( 0, 0, inner_radius, outer_radius, -big_angle + angles, angles, color=colors, ) # Adding axis lines and labels p.circle(0, 0, radius=radii, fill_color=None, line_color="white") p.text(0, radii[:], [str(r) for r in labels[:]], text_font_size="8pt", text_align="center", text_baseline="middle") # Specify that the plotted bars are the only thing to activate hovertool hoverable = p.annular_wedge(x='x', y='y', inner_radius='ymin', outer_radius='ymax', start_angle='starts', end_angle='stops', color='Colors', source=pdata_s) hover.renderers = [hoverable] # Add error bars if errorbar is True: p.annular_wedge(0, 0, pdata['Lower'], pdata['Upper'], pdata['Err_Angle'], pdata['Err_Angle'], color=pdata['Error Colors'], line_width=1.0) p.annular_wedge(0, 0, pdata['Lower'], pdata['Lower'], pdata['starts'], pdata['stops'], color=pdata['Error Colors'], line_width=2.0) p.annular_wedge(0, 0, pdata['Upper'], pdata['Upper'], pdata['starts'], pdata['stops'], color=pdata['Error Colors'], line_width=2.0) # Placement of parameter labels xr = (radii[0] * 1.1) * np.cos(np.array(-big_angle / 2 + angles)) yr = (radii[0] * 1.1) * np.sin(np.array(-big_angle / 2 + angles)) label_angle = np.array(-big_angle / 2 + angles) label_angle[label_angle < -np.pi / 2] += np.pi # Placing Labels and Legend legend_text = ['ST', 'ST Conf', 'S1', 'S1 Conf'] p.text(xr, yr, df.Parameter, angle=label_angle, text_font_size="9pt", text_align="center", text_baseline="middle") p.rect([-40, -40], [30, -10], width=30, height=13, color=list(stat_color.values())) p.rect([-40, -40], [10, -30], width=30, height=1, color=list(error_color.values())) p.text([-15, -15, -15, -15], [30, 10, -10, -30], text=legend_text, text_font_size="9pt", text_align="left", text_baseline="middle") p.annular_wedge(0, 0, inner_radius - 10, outer_radius + 10, -big_angle + line_angles, -big_angle + line_angles, color="#999999") return p
def make_plot(dataframe=pd.DataFrame(), highlight=[], top=100, minvalues=0.01, stacked=True, lgaxis=True, errorbar=True, showS1=True, showST=True): """ Basic method to plot first and total order sensitivity indices. This is the method to generate a Bokeh plot similar to the burtin example template at the Bokeh website. For clarification, parameters refer to an input being measured (Tmax, C, k2, etc.) and stats refer to the 1st or total order sensitivity index. Parameters ----------- dataframe : pandas dataframe Dataframe containing sensitivity analysis results to be plotted. highlight : lst, optional List of strings indicating which parameter wedges will be highlighted. top : int, optional Integer indicating the number of parameters to display (highest sensitivity values) (after minimum cutoff is applied). minvalues : float, optional Cutoff minimum for which parameters should be plotted. Applies to total order only. stacked : bool, optional Boolean indicating in bars should be stacked for each parameter (True) or unstacked (False). lgaxis : bool, optional Boolean indicating if log axis should be used (True) or if a linear axis should be used (False). errorbar : bool, optional Boolean indicating if error bars are shown (True) or are omitted (False). showS1 : bool, optional Boolean indicating whether 1st order sensitivity indices will be plotted (True) or omitted (False). showST : bool, optional Boolean indicating whether total order sensitivity indices will be plotted (True) or omitted (False). **Note if showS1 and showST are both false, the plot will default to showing ST data only instead of a blank plot** Returns -------- p : bokeh figure A Bokeh figure of the data to be plotted """ df = dataframe top = int(top) # Initialize boolean checks and check dataframe structure if (('S1' not in df) or ('ST' not in df) or ('Parameter' not in df) or ('ST_conf' not in df) or ('S1_conf' not in df)): raise Exception('Dataframe not formatted correctly') # Remove rows which have values less than cutoff values df = df[df['ST'] > minvalues] df = df.dropna() # Only keep top values indicated by variable top df = df.sort_values('ST', ascending=False) df = df.head(top) df = df.reset_index(drop=True) # Create arrays of colors and order labels for plotting colors = ["#a1d99b", "#31a354", "#546775", "#225ea8"] s1color = np.array(["#31a354"]*df.S1.size) sTcolor = np.array(["#a1d99b"]*df.ST.size) errs1color = np.array(["#225ea8"]*df.S1.size) errsTcolor = np.array(["#546775"]*df.ST.size) firstorder = np.array(["1st (S1)"]*df.S1.size) totalorder = np.array(["Total (ST)"]*df.S1.size) # Add column indicating which parameters should be highlighted tohighlight = df.Parameter.isin(highlight) df['highlighted'] = tohighlight back_color = { True: "#aeaeb8", False: "#e6e6e6", } # Switch to bar chart if dataframe shrinks below 5 parameters if len(df) <= 5: if stacked is False: data = { 'Sensitivity': pd.Series.append(df.ST, df.S1), 'Parameter': pd.Series.append(df.Parameter, df.Parameter), 'Order': np.append(np.array(['ST']*len(df)), np.array(['S1']*len(df))), 'Confidence': pd.Series.append(df.ST_conf, df.S1_conf) } p = Bar(data, values='Sensitivity', label='Parameter', group='Order', legend='top_right', color=["#31a354", "#a1d99b"], ylabel='Sensitivity Indices') else: data = { 'Sensitivity': pd.Series.append(df.S1, (df.ST-df.S1)), 'Parameter': pd.Series.append(df.Parameter, df.Parameter), 'Order': np.append(np.array(['S1']*len(df)), np.array(['ST']*len(df))), 'Confidence': pd.Series.append(df.S1_conf, df.ST_conf) } p = Bar(data, values='Sensitivity', label='Parameter', color='Order', legend='top_right', stack='Order', palette=["#31a354", "#a1d99b"], ylabel='Sensitivity Indices') return p # Create Dictionary of colors stat_color = OrderedDict() error_color = OrderedDict() for i in range(0, 2): stat_color[i] = colors[i] # Reset index of dataframe. for i in range(2, 4): error_color[i] = colors[i] # Sizing parameters width = 800 height = 800 inner_radius = 90 outer_radius = 300 - 10 # Determine wedge size based off number of parameters big_angle = 2.0 * np.pi / (len(df)+1) # Determine division of wedges for plotting bars based on # stats plotted # for stacked or unstacked bars if stacked is False: small_angle = big_angle / 5 else: small_angle = big_angle / 3 # tools enabled for bokeh figure plottools = "hover, wheel_zoom, save, reset, resize" # , tap" # Initialize figure with tools, coloring, etc. p = figure(plot_width=width, plot_height=height, title="", x_axis_type=None, y_axis_type=None, x_range=(-350, 350), y_range=(-350, 350), min_border=0, outline_line_color="#e6e6e6", background_fill_color="#e6e6e6", border_fill_color="#e6e6e6", tools=plottools) # Specify labels for hover tool hover = p.select(dict(type=HoverTool)) hover.tooltips = [("Order", "@Order"), ("Parameter", "@Param"), ("Sensitivity", "@Sens"), ("Confidence", "@Conf")] hover.point_policy = "follow_mouse" p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None # annular wedges divided into smaller sections for bars # Angles for axial line placement num_lines = np.arange(0, len(df)+1, 1) line_angles = np.pi/2 - big_angle/2 - num_lines*big_angle # Angles for data placement angles = np.pi/2 - big_angle/2 - df.index.to_series()*big_angle # circular axes and labels minlabel = min(round(np.log10(min(df.ST))), round(np.log10(min(df.S1)))) labels = np.power(10.0, np.arange(0, minlabel-1, -1)) # Set max radial line to correspond to 1.1 * maximum value + error maxvalST = max(df.ST+df.ST_conf) maxvalS1 = max(df.S1+df.S1_conf) maxval = max(maxvalST, maxvalS1) labels = np.append(labels, 0.0) labels[0] = round(1.1*maxval, 1) # Determine if radial axis are log or linearly scaled if lgaxis is True: radii = (((np.log10(labels / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) radii[-1] = inner_radius else: labels = np.delete(labels, -2) radii = (outer_radius - inner_radius)*labels/labels[0] + inner_radius # Convert sensitivity values to the plotted values # Same conversion as for the labels above # Also calculate the angle to which the bars are placed # Add values to the dataframe for future reference cols = np.array(['S1', 'ST']) for statistic in range(0, 2): if lgaxis is True: radius_of_stat = (((np.log10(df[cols[statistic]] / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) lower_of_stat = (((np.log10((df[cols[statistic]] - df[cols[statistic]+'_conf']) / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) higher_of_stat = (((np.log10((df[cols[statistic]] + df[cols[statistic]+'_conf']) / labels[0])) + labels.size) * (outer_radius - inner_radius) / labels.size + inner_radius) else: radius_of_stat = ((outer_radius - inner_radius) * df[cols[statistic]]/labels[0] + inner_radius) lower_of_stat = ((outer_radius - inner_radius) * (df[cols[statistic]] - df[cols[statistic]+'_conf'])/labels[0] + inner_radius) higher_of_stat = ((outer_radius - inner_radius) * ((df[cols[statistic]] + df[cols[statistic]+'_conf'])/labels[0]) + inner_radius) if stacked is False: startA = -big_angle + angles + (2*statistic + 1)*small_angle stopA = -big_angle + angles + (2*statistic + 2)*small_angle df[cols[statistic]+'_err_angle'] = pd.Series((startA+stopA)/2, index=df.index) else: startA = -big_angle + angles + (1)*small_angle stopA = -big_angle + angles + (2)*small_angle if statistic == 0: df[cols[statistic]+'_err_angle'] = pd.Series((startA*2 + stopA)/3, index=df.index) if statistic == 1: df[cols[statistic]+'_err_angle'] = pd.Series((startA + stopA*2)/3, index=df.index) df[cols[statistic]+'radial'] = pd.Series(radius_of_stat, index=df.index) df[cols[statistic]+'upper'] = pd.Series(higher_of_stat, index=df.index) df[cols[statistic]+'lower'] = pd.Series(lower_of_stat, index=df.index) df[cols[statistic]+'_start_angle'] = pd.Series(startA, index=df.index) df[cols[statistic]+'_stop_angle'] = pd.Series(stopA, index=df.index) # df[cols[statistic]+'_err_angle'] = pd.Series((startA+stopA)/2, # index=df.index) inner_rad = np.ones_like(angles)*inner_radius df[cols[statistic]+'lower'] = df[cols[statistic]+'lower'].fillna(90) # Store plotted values into dictionary to be add glyphs pdata = pd.DataFrame({ 'x': np.append(np.zeros_like(inner_rad), np.zeros_like(inner_rad)), 'y': np.append(np.zeros_like(inner_rad), np.zeros_like(inner_rad)), 'ymin': np.append(inner_rad, inner_rad), 'ymax': pd.Series.append(df[cols[1]+'radial'], df[cols[0]+'radial'] ).reset_index(drop=True), 'starts': pd.Series.append(df[cols[1] + '_start_angle'], df[cols[0] + '_start_angle'] ).reset_index(drop=True), 'stops': pd.Series.append(df[cols[1] + '_stop_angle'], df[cols[0] + '_stop_angle'] ).reset_index(drop=True), 'Param': pd.Series.append(df.Parameter, df.Parameter ).reset_index(drop=True), 'Colors': np.append(sTcolor, s1color), 'Error Colors': np.append(errsTcolor, errs1color), 'Conf': pd.Series.append(df.ST_conf, df.S1_conf ).reset_index(drop=True), 'Order': np.append(totalorder, firstorder), 'Sens': pd.Series.append(df.ST, df.S1 ).reset_index(drop=True), 'Lower': pd.Series.append(df.STlower, df.S1lower ).reset_index(drop=True), 'Upper': pd.Series.append(df.STupper, df.S1upper, ).reset_index(drop=True), 'Err_Angle': pd.Series.append(df.ST_err_angle, df.S1_err_angle, ).reset_index(drop=True) }) # removed S1 or ST values if indicated by input if showS1 is False: pdata = pdata.head(len(df)) if showST is False: pdata = pdata.tail(len(df)) # convert dataframe to ColumnDataSource for glyphs pdata_s = ColumnDataSource(pdata) colors = [back_color[highl] for highl in df.highlighted] p.annular_wedge( 0, 0, inner_radius, outer_radius, -big_angle+angles, angles, color=colors, ) # Adding axis lines and labels p.circle(0, 0, radius=radii, fill_color=None, line_color="white") p.text(0, radii[:], [str(r) for r in labels[:]], text_font_size="8pt", text_align="center", text_baseline="middle") # Specify that the plotted bars are the only thing to activate hovertool hoverable = p.annular_wedge(x='x', y='y', inner_radius='ymin', outer_radius='ymax', start_angle='starts', end_angle='stops', color='Colors', source=pdata_s ) hover.renderers = [hoverable] # Add error bars if errorbar is True: p.annular_wedge(0, 0, pdata['Lower'], pdata['Upper'], pdata['Err_Angle'], pdata['Err_Angle'], color=pdata['Error Colors'], line_width=1.0) p.annular_wedge(0, 0, pdata['Lower'], pdata['Lower'], pdata['starts'], pdata['stops'], color=pdata['Error Colors'], line_width=2.0) p.annular_wedge(0, 0, pdata['Upper'], pdata['Upper'], pdata['starts'], pdata['stops'], color=pdata['Error Colors'], line_width=2.0) # Placement of parameter labels xr = (radii[0]*1.1)*np.cos(np.array(-big_angle/2 + angles)) yr = (radii[0]*1.1)*np.sin(np.array(-big_angle/2 + angles)) label_angle = np.array(-big_angle/2+angles) label_angle[label_angle < -np.pi/2] += np.pi # Placing Labels and Legend legend_text = ['ST', 'ST Conf', 'S1', 'S1 Conf'] p.text(xr, yr, df.Parameter, angle=label_angle, text_font_size="9pt", text_align="center", text_baseline="middle") p.rect([-40, -40], [30, -10], width=30, height=13, color=list(stat_color.values())) p.rect([-40, -40], [10, -30], width=30, height=1, color=list(error_color.values())) p.text([-15, -15, -15, -15], [30, 10, -10, -30], text=legend_text, text_font_size="9pt", text_align="left", text_baseline="middle") p.annular_wedge(0, 0, inner_radius-10, outer_radius+10, -big_angle+line_angles, -big_angle+line_angles, color="#999999") return p
def show_kvm_exit_types(df, task_re, label): df = df[df['event'] == 'kvm_exit'] df = df[df['task_name'].str.match(task_re)] # the next_comm column contains the exit code exit_codes = Series(KVM_EXIT_REASONS) # add new column congaining the exit reason in clear text df['exit_reason'] = df['next_comm'].map(exit_codes) time_span_msec = get_time_span_msec(df) df.drop(['cpu', 'duration', 'event', 'next_pid', 'pid', 'next_comm', 'usecs'], inplace=True, axis=1) # Get the list of exit reasons, sorted alphabetically reasons = pandas.unique(df.exit_reason.ravel()).tolist() reasons.sort() # group by task name then exit reasons gb = df.groupby(['task_name', 'exit_reason']) # number of exit types size_series = gb.size() df = size_series.to_frame('count') df.reset_index(inplace=True) p = Bar(df, label='task_name', values='count', stack='exit_reason', title="KVM Exit types per task (%s, %d msec window)" % (label, time_span_msec), legend='top_right', tools="resize,hover,save", width=1000, height=800) p._xaxis.axis_label = "Task Name" p._xaxis.axis_label_text_font_size = "12pt" p._yaxis.axis_label = "Exit Count (sum)" p._yaxis.axis_label_text_font_size = "12pt" # Cannot find a way to display the exit reason in the tooltip # from bokeh.models.renderers import GlyphRenderer # glr = p.select(dict(type=GlyphRenderer)) # bar_source = glr[0].data_source # print bar_source.data # bar_source = glr[1].data_source # bar_source.data['exit_reason'] = ['HOHO'] hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("task", "$x"), # {"reason", "@exit_reason"}, ("count", "@height") ]) # specify how to output the plot(s) # table with counts gb = df.groupby(['exit_reason']) keys = gb.groups.keys() dfr_list = [] for reason in keys: dfr = gb.get_group(reason) # drop the exit reason column dfr = dfr.drop(['exit_reason'], axis=1) # rename the count column with the reason name dfr.rename(columns={'count': reason}, inplace=True) # set the task name as the index dfr.set_index('task_name', inplace=True) dfr_list.append(dfr) # concatenate all task columns into 1 dataframe that has the exit reason as the index # counts for missing exit reasons will be set to NaN dft = pandas.concat(dfr_list, axis=1) dft.fillna(0, inplace=True) # Add a total column dft['TOTAL'] = dft.sum(axis=1) sfmt = StringFormatter(text_align='center', font_style='bold') nfmt = NumberFormatter(format='0,0') col_names = list(dft.columns.values) col_names.sort() # move 'TOTAL' at end of list col_names.remove('TOTAL') col_names.append('TOTAL') # convert index to column name dft.reset_index(level=0, inplace=True) dft.rename(columns={'index': 'Task'}, inplace=True) columns = [TableColumn(field=name, title=name, formatter=nfmt) for name in col_names] columns.insert(0, TableColumn(field='Task', title='Task', formatter=sfmt)) table = DataTable(source=ColumnDataSource(dft), columns=columns, width=1000, row_headers=False, height='auto') output_html(vplot(p, table), 'kvm-types', task_re) '''
def plot_lcoe(top): # all this can probably be done smarter with a Pandas DataFrame?! Any takers? aep = top.fin_a.net_aep fcr = top.fin_a.fixed_charge_rate turbine_lcoe = OrderedDict( Rotor=( fcr / aep * top.tcc_a.tcc.blade_cost * top.tcc_a.tcc.blade_number + fcr / aep * top.tcc_a.tcc.hub_system_cost) * top.turbine_number, Tower=fcr / aep * top.tcc_a.tcc.tower_cost * top.turbine_number, Nacelle=fcr / aep * top.tcc_a.tcc.nacelle_cost * top.turbine_number) infra_lcoe = OrderedDict( Assembly=fcr / aep * top.bos_breakdown.assembly_and_installation_costs, Development=fcr / aep * top.bos_breakdown.development_costs, Electrical=fcr / aep * top.bos_breakdown.electrical_costs, Substructure=fcr / aep * top.bos_breakdown.foundation_and_substructure_costs, Other=fcr / aep * top.bos_breakdown.foundation_and_substructure_costs, Preparation=fcr / aep * top.bos_breakdown.preparation_and_staging_costs, Soft=fcr / aep * top.bos_breakdown.soft_costs, Transportation=fcr / aep * top.bos_breakdown.transportation_costs) opex_lcoe = OrderedDict(Opex=top.opex_a.avg_annual_opex / aep) turbine_sum = np.sum(turbine_lcoe.values()) infra_sum = np.sum(infra_lcoe.values()) opex_sum = np.sum(opex_lcoe.values()) total_lcoe = np.array( [turbine_sum, infra_sum, opex_sum]) # array containing Turbine, BOS, and OPEX lcoe costs lcoe = total_lcoe.sum() total_lcoe = OrderedDict(Total=lcoe) everything_lcoe = turbine_lcoe everything_lcoe.update(infra_lcoe) everything_lcoe.update(opex_lcoe) cumSum = 0 invisibleBlock = OrderedDict() for key in everything_lcoe.keys(): invisibleBlock[key] = cumSum cumSum += everything_lcoe[key] everything_lcoe.update(total_lcoe) invisibleBlock['Total'] = 0 Combined = invisibleBlock.values() colors = ['white' for i in range(len(Combined))] next_color = palette.next() for i in range(3): colors.append(next_color) next_color = palette.next() for i in range(8): colors.append(next_color) colors.append(palette.next()) colors.append('black') myGroup = [] for x in range(2): for i in range(3): myGroup.append('turbine') for i in range(8): myGroup.append('infrastructure') myGroup.append('opex') myGroup.append('total') for stuff in everything_lcoe.values(): Combined.append(stuff) myKeys = OrderedDict(turbine=turbine_lcoe.keys(), infra=infra_lcoe.keys(), myOpex=opex_lcoe.keys()) myNames = myKeys['turbine'] for stuff in list(myKeys['turbine']): myNames.append(stuff) myStack = [] for i in range(13): myStack.append(1) for i in range(13): myStack.append(2) myDict = dict(Amount=Combined, Group=myGroup, Names=myNames, stack=myStack, color=colors) myDF = df(data=myDict) # print myDF myBar = Bar( myDF, values='Amount', label=CatAttr(columns=['Names'], sort=False), stack="stack", toolbar_location="above", color="color", ygrid=False, legend=None, title='LCOE Costs Breakdown', ylabel="LCOE ($/kWh)", xlabel="Component", tools="crosshair,pan,wheel_zoom,box_zoom,reset,hover,previewsave", ) hover = myBar.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Component", "@Names"), # ("Group", "@Group"), # maybe one day bokeh will fix this and it will work. It should show "turbine, infra, or opex" ]) return myBar
'rock': { "words" : rock_len_words} } data = [] for corp in raw_data: for x in raw_data[corp]: entry = { 'val': raw_data[corp][x],'cat': x ,'group' : corp} data.append(entry) #print(data) data_frame = pd.DataFrame(data) p = Bar(data_frame ,values='val', label='cat', xlabel='corpora', ylabel='word count' , group='group', title="Total words for different corpora", legend='top_right' ) yaxis = p.select(dict(type=Axis, layout="left"))[0] yaxis.formatter.use_scientific = False output_file("word statistics.html") show(p) # raw_data = { 'brown': { "vocabulary" : brown_len_vocab}, 'all_lyrics' : { "vocabulary" : total_len_vocab}, 'pop' : { "vocabulary" : pop_len_vocab}, 'rock': { "vocabulary" : rock_len_vocab} } data = [] for corp in raw_data: for x in raw_data[corp]:
ch1TotalSubscribers = channel1_data["total_subscribers"] ch2TotalSubscribers = channel2_data["total_subscribers"] # Data to be used for graphone ordered into groups data = { 'Numbers': ['Dislikes', 'Likes', 'Subscribers', 'Total Views', 'Dislikes', 'Likes', 'Subscribers', 'Total Views'], 'Channels': [ch1, ch1, ch1, ch1, ch2, ch2, ch2, ch2], 'Total': [ch1TotalDislikes, ch1TotalLikes, ch1TotalSubscribers, ch1TotalViews, ch2TotalDislikes, ch2TotalLikes, ch2TotalSubscribers, ch2TotalViews] } # Puts all the data into a format which may be graphed with correct parameters: bar = Bar(data, values='Total', label=['Channels', 'Numbers'],agg = 'sum', title="Comparing two Youtube Channels", width=500, height = 1000, group = 'Numbers', tools=['hover', 'resize', 'box_zoom', 'wheel_zoom', 'pan']) # Allows the hover tool to function: hover = bar.select(dict(type=HoverTool)) hover.tooltips = [('Value of Channel',' $x'),('Value of Total',' @height')] # outputs a file with the data for the graph: output_file("stacked_bar.html") # Shows the graph: show(hplot(bar)) # #End of graphing functions code! #************************************************************************************** print(channel1_data) print(channel2_data)
def compare_libraries_somy(request, experimentId): """ Display somy chart(s) :param request: libcodes, referencegenome/resultids :return: all chart objects """ kwargs = {} #kwargs['user']=user kwargs['listoflinks'] = listoflinks kwargs['title'] = "Comparing Somy" if request.method == 'POST': libcodes = request.POST.getlist('libcodes', '') color_list = [] legendvalues = [] for libcode in libcodes: libobj = Library.objects.get(library_code=libcode) legendvalues.append(libobj.library_code + '(' + libobj.sampleid.samplename + ')') color = request.POST.get(libcode) color_list.append(color) somyobjects = CNV.objects.filter( library__library_code__in=libcodes).filter(cnv_type__cvterm='Somy') contignames = [] for contig in sorted( list( set( somyobjects.values_list('chromosome__chromosome_name', flat=True)))): contignames.append(re.sub(r'\D+', '', re.sub(r'_.+', '', contig))) somy_dict = {} max_somy = 0 for libcode in libcodes: somy_for_lib = [] for somyvalue in somyobjects.filter( library__library_code=libcode).order_by( 'chromosome__chromosome_name').values_list('cnv_value', flat=True): somy_for_lib.append(somyvalue) if somyvalue > max_somy: max_somy = somyvalue somy_dict[libcode] = somy_for_lib plot = Bar(somy_dict, cat=contignames, xlabel="Chromosomes", ylabel="Somy Values", width=1000, height=500, palette=color_list, legend=True, tools='hover') hover = plot.select(dict(type=HoverTool)) hover.tooltips = [("Chromosome", "$x"), ("CNV Value", "@y")] script, div = components(plot) return render_to_response('snpdb/compare_libraries_somy.html', { "script": script, "div": div }, context_instance=RequestContext(request)) else: exp = Experiment.objects.get(id=experimentId) kwargs['exp'] = exp libs = [] for sample in exp.samples.all(): for lib in sample.library_set.all(): libs.append(lib) kwargs['libs'] = libs kwargs['display_form'] = 'yes' kwargs['colors'] = [ 'blue', 'orange', 'red', 'green', 'yellow', 'purple', 'pink', 'black', 'gray', 'cyan', 'white' ] return render_to_response('snpdb/compare_libraries_somy.html', kwargs, context_instance=RequestContext(request))
def robots(): def choose_channel(): channel_name = raw_input('Enter a channel name: ') return channel_name def retrieve_links(name_of_channel): root_url = "https://www.youtube.com" url = urllib2.urlopen("https://www.youtube.com/user/" + name_of_channel + "/videos") content = url.read() soup = BeautifulSoup(content, "html.parser") list_of_links = [] #searches for all 'a' tags that have "/watch" in them. for elem in soup.find_all('a', href=re.compile('/watch')): list_of_links.append(root_url + elem['href']) #This loop gets rid of duplicate links i = 0 while i < len(list_of_links): if list_of_links[i] == list_of_links[i+1]: list_of_links.remove(list_of_links[i]) i += 1 #print("Links for the %d most recent videos:" % len(list_of_links)) #for x in range(len(list_of_links)): #print(list_of_links[x]) return list_of_links #This function will go to each link and scrape data of subscribers, views, likes and dislikes def get_data(list_of_links = []): data = {} data['total_views'] = 0 data['total_likes'] = 0 data['total_dislikes'] = 0 data['total_subscribers'] = 0 for x in range(len(list_of_links)): url = urllib2.urlopen(list_of_links[x]) link_content = url.read() link_soup = BeautifulSoup(link_content, "html.parser") #print("Video number: %d" % (x+1)) views = link_soup.find('div', {'class' : 'watch-view-count'}) views = views.get_text() views = views.replace(",", "") views = views.replace(" views", "") data['total_views'] += int(views) #print("%s views" % views) likes = link_soup.find('button', {'class' : "yt-uix-button yt-uix-button-size-default yt-uix-button-opacity yt-uix-button-has-icon no-icon-markup like-button-renderer-like-button like-button-renderer-like-button-unclicked yt-uix-clickcard-target yt-uix-tooltip"}) likes = likes.get_text() likes = likes.replace(",", "") data['total_likes'] += int(likes) #print("%s likes" % likes) dislikes = link_soup.find('button', {'class' : "yt-uix-button yt-uix-button-size-default yt-uix-button-opacity yt-uix-button-has-icon no-icon-markup like-button-renderer-dislike-button like-button-renderer-dislike-button-unclicked yt-uix-clickcard-target yt-uix-tooltip"}) dislikes = dislikes.get_text() dislikes = dislikes.replace(",", "") data['total_dislikes'] += int(dislikes) #print("%s dislikes" % dislikes) #print(" ") subscribers = link_soup.find('span', {'class' : "yt-subscription-button-subscriber-count-branded-horizontal yt-subscriber-count"}) subscribers = subscribers.get_text() subscribers = subscribers.replace(",", "") data['total_subscribers'] = int(subscribers) return data name1 = request.args['example'] name2 = request.args['example2'] ch1 = name1 ch2 = name2 channel1 = retrieve_links(ch1) channel2 = retrieve_links(ch2) channel1_data = get_data(channel1) channel2_data = get_data(channel2) #************************************************************************************************* #Graphing functions added here: # Puts integer values into variables to be used in bokeh graphing: ch1TotalViews = channel1_data["total_views"] ch2TotalViews = channel2_data["total_views"] ch1TotalLikes = channel1_data["total_likes"] ch2TotalLikes = channel2_data["total_likes"] ch1TotalDislikes = channel1_data["total_dislikes"] ch2TotalDislikes = channel2_data["total_dislikes"] ch1TotalSubscribers = channel1_data["total_subscribers"] ch2TotalSubscribers = channel2_data["total_subscribers"] # Data to be used for graphone ordered into groups data = { 'Numbers': ['Dislikes', 'Likes', 'Subscribers', 'Total Views', 'Dislikes', 'Likes', 'Subscribers', 'Total Views'], 'Channels': [ch1, ch1, ch1, ch1, ch2, ch2, ch2, ch2], 'Total': [ch1TotalDislikes, ch1TotalLikes, ch1TotalSubscribers, ch1TotalViews, ch2TotalDislikes, ch2TotalLikes, ch2TotalSubscribers, ch2TotalViews] } # Puts all the data into a format which may be graphed with correct parameters: bar = Bar(data, values='Total', label=['Channels', 'Numbers'],agg = 'sum', title="Comparing two Youtube Channels", width=500, height = 1000, group = 'Numbers', tools=['hover', 'resize', 'box_zoom', 'wheel_zoom', 'pan']) # Allows the hover tool to function: hover = bar.select(dict(type=HoverTool)) hover.tooltips = [('Value of Channel',' $x'),('Value of Total',' @height')] # outputs a file with the data for the graph: output_file("stacked_bar.html") # Shows the graph: show(hplot(bar)) # #End of graphing functions code! #************************************************************************************** return render_template("resultsPage.html")
def plot_lcoe(top): # all this can probably be done smarter with a Pandas DataFrame?! Any takers? aep = top.fin_a.net_aep fcr = top.fin_a.fixed_charge_rate turbine_lcoe = OrderedDict(Rotor=(fcr / aep * top.tcc_a.tcc.blade_cost * top.tcc_a.tcc.blade_number + fcr / aep * top.tcc_a.tcc.hub_system_cost) * top.turbine_number, Tower=fcr / aep * top.tcc_a.tcc.tower_cost * top.turbine_number, Nacelle=fcr / aep * top.tcc_a.tcc.nacelle_cost * top.turbine_number) infra_lcoe = OrderedDict(Assembly=fcr / aep * top.bos_breakdown.assembly_and_installation_costs, Development=fcr / aep * top.bos_breakdown.development_costs, Electrical=fcr / aep * top.bos_breakdown.electrical_costs, Substructure=fcr / aep * top.bos_breakdown.foundation_and_substructure_costs, Other=fcr / aep * top.bos_breakdown.foundation_and_substructure_costs, Preparation=fcr / aep * top.bos_breakdown.preparation_and_staging_costs, Soft=fcr / aep * top.bos_breakdown.soft_costs, Transportation=fcr / aep * top.bos_breakdown.transportation_costs) opex_lcoe = OrderedDict(Opex=top.opex_a.avg_annual_opex / aep) turbine_sum = np.sum(turbine_lcoe.values()) infra_sum = np.sum(infra_lcoe.values()) opex_sum = np.sum(opex_lcoe.values()) total_lcoe = np.array([turbine_sum, infra_sum, opex_sum]) # array containing Turbine, BOS, and OPEX lcoe costs lcoe = total_lcoe.sum() total_lcoe = OrderedDict(Total=lcoe) everything_lcoe = turbine_lcoe everything_lcoe.update(infra_lcoe) everything_lcoe.update(opex_lcoe) cumSum = 0 invisibleBlock = OrderedDict() for key in everything_lcoe.keys(): invisibleBlock[key] = cumSum cumSum += everything_lcoe[key] everything_lcoe.update(total_lcoe) invisibleBlock['Total'] = 0 Combined = invisibleBlock.values() colors = ['white' for i in range(len(Combined))] next_color = palette.next() for i in range(3): colors.append(next_color) next_color = palette.next() for i in range(8): colors.append(next_color) colors.append(palette.next()) colors.append('black') myGroup=[] for x in range(2): for i in range(3): myGroup.append('turbine') for i in range(8): myGroup.append('infrastructure') myGroup.append('opex') myGroup.append('total') for stuff in everything_lcoe.values(): Combined.append(stuff) myKeys = OrderedDict(turbine=turbine_lcoe.keys(), infra=infra_lcoe.keys(), myOpex=opex_lcoe.keys()) myNames = myKeys['turbine'] for stuff in list(myKeys['turbine']): myNames.append(stuff) myStack = [] for i in range(13): myStack.append(1) for i in range(13): myStack.append(2) myDict = dict(Amount=Combined, Group=myGroup, Names=myNames, stack=myStack, color=colors) myDF = df(data=myDict) # print myDF myBar = Bar(myDF, values='Amount', label=CatAttr(columns=['Names'], sort=False), stack="stack", toolbar_location="above", color="color", ygrid=False, legend=None, title='LCOE Costs Breakdown', ylabel="LCOE ($/kWh)", xlabel="Component", tools="crosshair,pan,wheel_zoom,box_zoom,reset,hover,previewsave", ) hover = myBar.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Component","@Names"), # ("Group", "@Group"), # maybe one day bokeh will fix this and it will work. It should show "turbine, infra, or opex" ]) return myBar
''' import pandas as pd from bokeh.charts import Bar, output_file, show, defaults import bokeh.plotting as bp from bokeh.models import FixedTicker, LinearAxis from bokeh.io import output_file, show, vplot crapsArray(100, 5, 300) csv=pd.read_csv('test.csv') output_file("craps.html") #Plot sizes for charts defaults.width = 1200 defaults.height = 300 action = Bar(csv, 'roll', values = 'score', title = "Craps Outcomes", bar_width = 1, color = 'score') money = Bar(csv, 'roll', values = 'bank', title = "Craps Bankroll", bar_width = 1, color = 'score') winLose = Bar(csv, 'roll', values = 'value', title = "Craps Roll Win/Loss", bar_width = 1, color = 'score') xaxis=action.select({'type' : LinearAxis}) xaxis.ticker=FixedTicker(ticks=[0,25,50,75,100]) yaxis=action.select({'type' : LinearAxis}) yaxis.ticker=FixedTicker(ticks=[2,3,4,5,6,7,8,9,10,11,12]) p = vplot(action, money,winLose) show(p) print("Cashing Out")
cursor = db.cursor() cursor.execute( "select cust_id,quantity from new_schema.cust2 where prod_id=21975 ;") data = cursor.fetchall() print(data) df = pd.DataFrame([[ij for ij in i] for i in data]) df.rename(columns={ 0: 'Customer ID', 1: 'Quantity' }, inplace=True) df = df.sort_values(['Customer ID'], ascending=[1]) #df['new'] = 0 df = df.groupby("Customer ID").sum() print(df.head(10)) from bokeh.charts import Bar, output_file, save from bokeh.models import HoverTool bar = Bar(df, 'Customer ID', values='Quantity', title="test chart", responsive=True, tools='hover', legend=False) hover = bar.select(dict(type=HoverTool)) hover.tooltips = [("Customer ID", "$x"), ("Quantity", "$y")] output_file('listofcust.html') save(bar)