def bball(): os.chdir('C:\\Users\\weinfz18\\Documents\\NBA_current') ## switch to correct directory imgo = np.loadtxt('imgo.csv', delimiter=',') imgd = np.loadtxt('imgd.csv', delimiter=',') ## court image players = pd.read_csv('C:\\Users\\weinfz18\\Documents\\NBA_current\\all_players.csv', names=['players'],delimiter='$') oplayer = "Whiteside, Hassan" dplayer = "Whiteside, Hassan" dplayername = ''.join([x for x in dplayer.lower() if x in 'abcdefghijklmnopqrstuvwxyz']) oplayername = ''.join([x for x in oplayer.lower() if x in 'abcdefghijklmnopqrstuvwxyz']) odata = pd.read_csv('C:\\Users\\weinfz18\\Documents\\NBA_current\\player_shots\\{}\\odata.csv'.format(oplayername)) ddata = pd.read_csv('C:\\Users\\weinfz18\\Documents\\NBA_current\\player_shots\\{}\\ddata.csv'.format(dplayername)) odata = odata[odata['dense']!=0] ddata = ddata[ddata['dense']!=0] ddata = ddata.reset_index(drop=True) odata = odata.reset_index(drop=True) odata = odata.to_dict("list") ddata = ddata.to_dict("list") oplayer_select = Select(value="Rubio, Ricky", title='Offensive player', options=sorted(players['players'].tolist())) dplayer_select = Select(value="Rubio, Ricky", title='Defensive player', options=sorted(players['players'].tolist())) oplayer_select.on_change('value', update_plot) dplayer_select.on_change('value', update_plot) op, odata_table, dp, ddata_table = Make_Plot(ddata,odata,imgo,imgd,oplayer,dplayer) oscript, odiv = components(op) otscript, otdiv = components(odata_table) dscript, ddiv = components(dp) dtscript, dtdiv = components(ddata_table) script = [oscript, otscript, dscript, dtscript] div = [odiv, otdiv, ddiv, dtdiv] return render_template('index.html', script=script, div=div)
def modify_doc(doc): source = ColumnDataSource(dict(x=[1, 2], y=[1, 1], val=["a", "b"])) plot = Plot(plot_height=400, plot_width=400, x_range=Range1d(0, 1), y_range=Range1d(0, 1), min_border=0) plot.add_glyph(source, Circle(x='x', y='y', size=20)) plot.add_tools(CustomAction(callback=CustomJS(args=dict(s=source), code=RECORD("data", "s.data")))) select = Select(options=["Option 1", "Option 2", "Option 3"], css_classes=["foo"]) def cb(attr, old, new): source.data['val'] = [old, new] select.on_change('value', cb) doc.add_root(column(select, plot))
def create_layout(): year_select = Select(title="Year:", value="2010", options=years) location_select = Select(title="Location:", value="World", options=locations) year_select.on_change('value', on_year_change) location_select.on_change('value', on_location_change) controls = row(children=[year_select, location_select]) layout = column(children=[controls, pyramid(), population()]) return layout
def create_layout(self): years = list(map(str, sorted(self.df.Year.unique()))) locations = sorted(self.df.Location.unique()) year_select = Select(title="Year:", value="2010", options=years) location_select = Select(title="Location:", value="World", options=locations) year_select.on_change('value', self.on_year_change) location_select.on_change('value', self.on_location_change) controls = HBox(year_select, location_select) self.layout = VBox(controls, self.plot)
def create_layout(): size_select = Select( value="Days Active", title="Marker Scaling:", options=["Days Active", "Days Remaining", "Power Output"] ) size_select.on_change("value", on_size_change) location_select = Select(title="Power Plant Name:", value=location, options=locations) location_select.on_change("value", on_location_change) controls = HBox(children=[size_select, location_select]) layout = VBox(children=[controls, map(), performance()]) return layout
# Add a circle glyph to the figure p plot.circle(x='x', y='y', source=source, color=dict(field='region', transform=color_mapper), legend='region') plot.legend.location = 'bottom_left' slider = Slider(start=1970, end=2010, step=1, value=1970, title='Year') # Attach the callback to the 'value' property of slider slider.on_change('value', update_plot) # Create a dropdown Select widget for the x data: x_select x_select = Select(options=['fertility', 'life', 'child_mortality', 'gdp'], value='fertility', title='x-axis data') # Attach the update_plot callback to the 'value' property of x_select x_select.on_change('value', update_plot) # Create a dropdown Select widget for the y data: y_select y_select = Select(options=['fertility', 'life', 'child_mortality', 'gdp'], value='life', title='y-axis data') y_select.on_change('value', update_plot) layout = row(widgetbox(slider, x_select, y_select), plot) curdoc().add_root(layout) curdoc().title = 'Gapminder'
print ('ADD DATA', self.add_data) layout.children[4] = self.create_figure(self.add_data, datplot='Add', plot_type='plot_pade') def update(): pass #source_data = CF.plot_data # initialize the crossfilter instance CF = CrossFiltDFs() # define the selection widgets for code, exchange, # TODO: enable widgets that support multi-selection # Elements selection widget from a periodic table code = Select(title='Code', value=codes[0], options=codes) code.on_change('value', lambda attr, old, new: CF.update_code()) exchange = Select(title='ExchangeCorrelation', value=exchanges[0], options=exchanges) exchange.on_change('value', lambda attr, old, new: CF.update_exchange()) struct = Select(title='Structure', value=structures[0], options=structures) struct.on_change('value', lambda attr, old, new: CF.update_struct()) element = CheckboxButtonGroup(labels=_elements, active=[1]) element.on_click(CF.update_element) prop = Select(title='Property', value=properties[0], options=properties) prop.on_change('value', lambda attr, old, new: CF.update_prop()) apply_crossfilter = Button(label='CrossFilter and Plot') apply_crossfilter.on_click(CF.update_crossfilter)
class TimeSeries: def __init__(self, sources, range_categories, custom_title, data_tables): self.sources = sources self.range_categories = range_categories self.current_dvh_group = {n: [] for n in GROUP_LABELS} # Control Chart layout (Time-Series) tools = "pan,wheel_zoom,box_zoom,lasso_select,poly_select,reset,crosshair,save" self.plot = figure(plot_width=1050, plot_height=400, tools=tools, logo=None, active_drag="box_zoom", x_axis_type='datetime') self.plot.xaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE self.plot.yaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE self.plot.xaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE self.plot.yaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE # plot.min_border_left = min_border self.plot.min_border_bottom = options.MIN_BORDER self.plot_data_1 = self.plot.circle('x', 'y', size=options.TIME_SERIES_1_CIRCLE_SIZE, color=options.GROUP_1_COLOR, alpha=options.TIME_SERIES_1_CIRCLE_ALPHA, source=sources.time_1) self.plot_data_2 = self.plot.circle('x', 'y', size=options.TIME_SERIES_2_CIRCLE_SIZE, color=options.GROUP_2_COLOR, alpha=options.TIME_SERIES_2_CIRCLE_ALPHA, source=sources.time_2) self.plot_trend_1 = self.plot.line('x', 'y', color=options.GROUP_1_COLOR, source=sources.time_trend_1, line_width=options.TIME_SERIES_1_TREND_LINE_WIDTH, line_dash=options.TIME_SERIES_1_TREND_LINE_DASH) self.plot_trend_2 = self.plot.line('x', 'y', color=options.GROUP_2_COLOR, source=sources.time_trend_2, line_width=options.TIME_SERIES_2_TREND_LINE_WIDTH, line_dash=options.TIME_SERIES_2_TREND_LINE_DASH) self.plot_avg_1 = self.plot.line('x', 'avg', color=options.GROUP_1_COLOR, source=sources.time_bound_1, line_width=options.TIME_SERIES_1_AVG_LINE_WIDTH, line_dash=options.TIME_SERIES_1_AVG_LINE_DASH) self.plot_avg_2 = self.plot.line('x', 'avg', color=options.GROUP_2_COLOR, source=sources.time_bound_2, line_width=options.TIME_SERIES_2_AVG_LINE_WIDTH, line_dash=options.TIME_SERIES_2_AVG_LINE_DASH) self.plot_patch_1 = self.plot.patch('x', 'y', color=options.GROUP_1_COLOR, source=sources.time_patch_1, alpha=options.TIME_SERIES_1_PATCH_ALPHA) self.plot_patch_2 = self.plot.patch('x', 'y', color=options.GROUP_2_COLOR, source=sources.time_patch_2, alpha=options.TIME_SERIES_1_PATCH_ALPHA) self.plot.add_tools(HoverTool(show_arrow=True, tooltips=[('ID', '@mrn'), ('Date', '@x{%F}'), ('Value', '@y{0.2f}')], formatters={'x': 'datetime'})) self.plot.xaxis.axis_label = "Simulation Date" self.plot.yaxis.axis_label = "" # Set the legend legend_plot = Legend(items=[("Group 1", [self.plot_data_1]), ("Series Average", [self.plot_avg_1]), ("Rolling Average", [self.plot_trend_1]), ("Percentile Region", [self.plot_patch_1]), ("Group 2", [self.plot_data_2]), ("Series Average", [self.plot_avg_2]), ("Rolling Average", [self.plot_trend_2]), ("Percentile Region", [self.plot_patch_2])], location=(25, 0)) # Add the layout outside the plot, clicking legend item hides the line self.plot.add_layout(legend_plot, 'right') self.plot.legend.click_policy = "hide" plot_options = list(range_categories) plot_options.sort() plot_options.insert(0, '') self.y_axis = Select(value=plot_options[0], options=plot_options, width=300) self.y_axis.title = "Select a Range Variable" self.y_axis.on_change('value', self.update_y_axis_ticker) self.look_back_distance = TextInput(value='1', title="Lookback Distance", width=200) self.look_back_distance.on_change('value', self.update_plot_trend_ticker) self.plot_percentile = TextInput(value='90', title="Percentile", width=200) self.plot_percentile.on_change('value', self.update_plot_trend_ticker) look_back_units_options = ['Dates with a Sim', 'Days'] self.look_back_units = Select(value=look_back_units_options[0], options=look_back_units_options, width=200) self.look_back_units.title = 'Lookback Units' self.look_back_units.on_change('value', self.update_plot_ticker) # source_time.on_change('selected', plot_update_trend) self.trend_update_button = Button(label="Update Trend", button_type="primary", width=150) self.trend_update_button.on_click(self.plot_update_trend) self.download_time_plot = Button(label="Download Plot Data", button_type="default", width=150) self.download_time_plot.callback = CustomJS(args=dict(source_1=sources.time_1, source_2=sources.time_2), code=open(join(dirname(__file__), "download_time_plot.js")).read()) # histograms tools = "pan,wheel_zoom,box_zoom,reset,crosshair,save" self.histograms = figure(plot_width=1050, plot_height=400, tools=tools, logo=None, active_drag="box_zoom") self.histograms.xaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE self.histograms.yaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE self.histograms.xaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE self.histograms.yaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE self.histograms.min_border_left = options.MIN_BORDER self.histograms.min_border_bottom = options.MIN_BORDER self.hist_1 = self.histograms.vbar(x='x', width='width', bottom=0, top='top', source=sources.histogram_1, color=options.GROUP_1_COLOR, alpha=options.HISTOGRAM_1_ALPHA) self.hist_2 = self.histograms.vbar(x='x', width='width', bottom=0, top='top', source=sources.histogram_2, color=options.GROUP_2_COLOR, alpha=options.HISTOGRAM_2_ALPHA) self.histograms.xaxis.axis_label = "" self.histograms.yaxis.axis_label = "Frequency" self.histogram_bin_slider = Slider(start=1, end=100, value=10, step=1, title="Number of Bins") self.histogram_bin_slider.on_change('value', self.histograms_ticker) self.histogram_radio_group = RadioGroup(labels=["Absolute Y-Axis", "Relative Y-Axis (to Group Max)"], active=0) self.histogram_radio_group.on_change('active', self.histograms_ticker) self.histogram_normaltest_1_text = Div(text="Group 1 Normal Test p-value = ", width=400) self.histogram_normaltest_2_text = Div(text="Group 2 Normal Test p-value = ", width=400) self.histogram_ttest_text = Div(text="Two Sample t-Test (Group 1 vs 2) p-value = ", width=400) self.histogram_ranksums_text = Div(text="Wilcoxon rank-sum (Group 1 vs 2) p-value = ", width=400) self.histograms.add_tools(HoverTool(show_arrow=True, line_policy='next', tooltips=[('x', '@x{0.2f}'), ('Counts', '@top')])) # Set the legend legend_hist = Legend(items=[("Group 1", [self.hist_1]), ("Group 2", [self.hist_2])], location=(25, 0)) # Add the layout outside the plot, clicking legend item hides the line self.histograms.add_layout(legend_hist, 'right') self.histograms.legend.click_policy = "hide" self.layout = column(Div(text="<b>DVH Analytics v%s</b>" % options.VERSION), row(custom_title['1']['time_series'], Spacer(width=50), custom_title['2']['time_series']), row(self.y_axis, self.look_back_units, self.look_back_distance, Spacer(width=10), self.plot_percentile, Spacer(width=10), self.trend_update_button), self.plot, self.download_time_plot, Div(text="<hr>", width=1050), row(self.histogram_bin_slider, self.histogram_radio_group), row(self.histogram_normaltest_1_text, self.histogram_ttest_text), row(self.histogram_normaltest_2_text, self.histogram_ranksums_text), self.histograms, Spacer(width=1000, height=10)) def update_current_dvh_group(self, data): self.current_dvh_group = data def update_plot_ticker(self, attr, old, new): self.update_plot() def update_y_axis_ticker(self, attr, old, new): self.update_plot() def update_plot_trend_ticker(self, attr, old, new): self.plot_update_trend() def histograms_ticker(self, attr, old, new): self.update_histograms() def update_plot(self): new = str(self.y_axis.value) if new: clear_source_selection(self.sources, 'time_1') clear_source_selection(self.sources, 'time_2') if new.startswith('DVH Endpoint: '): y_var_name = new.split(': ')[1] y_source_values = self.sources.endpoint_calcs.data[y_var_name] y_source_uids = self.sources.endpoint_calcs.data['uid'] y_source_mrns = self.sources.endpoint_calcs.data['mrn'] elif new == 'EUD': y_source_values = self.sources.rad_bio.data['eud'] y_source_uids = self.sources.rad_bio.data['uid'] y_source_mrns = self.sources.rad_bio.data['mrn'] elif new == 'NTCP/TCP': y_source_values = self.sources.rad_bio.data['ntcp_tcp'] y_source_uids = self.sources.rad_bio.data['uid'] y_source_mrns = self.sources.rad_bio.data['mrn'] else: y_source = self.range_categories[new]['source'] y_var_name = self.range_categories[new]['var_name'] y_source_values = y_source.data[y_var_name] y_source_uids = y_source.data['uid'] y_source_mrns = y_source.data['mrn'] self.update_y_axis_label() sim_study_dates = self.sources.plans.data['sim_study_date'] sim_study_dates_uids = self.sources.plans.data['uid'] x_values = [] skipped = [] colors = [] for v in range(len(y_source_values)): uid = y_source_uids[v] try: sim_study_dates_index = sim_study_dates_uids.index(uid) current_date_str = sim_study_dates[sim_study_dates_index] if current_date_str == 'None': current_date = datetime.now() else: current_date = datetime(int(current_date_str[0:4]), int(current_date_str[5:7]), int(current_date_str[8:10])) x_values.append(current_date) skipped.append(False) except: skipped.append(True) # Get group color if not skipped[-1]: if new.startswith('DVH Endpoint') or new in {'EUD', 'NTCP/TCP'} \ or self.range_categories[new]['source'] == self.sources.dvhs: if new in {'EUD', 'NTCP/TCP'}: roi = self.sources.rad_bio.data['roi_name'][v] else: roi = self.sources.dvhs.data['roi_name'][v] found = {'Group 1': False, 'Group 2': False} color = None if self.current_dvh_group['1']: r1, r1_max = 0, len(self.current_dvh_group['1'].study_instance_uid) while r1 < r1_max and not found['Group 1']: if self.current_dvh_group['1'].study_instance_uid[r1] == uid and \ self.current_dvh_group['1'].roi_name[r1] == roi: found['Group 1'] = True color = options.GROUP_1_COLOR r1 += 1 if self.current_dvh_group['2']: r2, r2_max = 0, len(self.current_dvh_group['2'].study_instance_uid) while r2 < r2_max and not found['Group 2']: if self.current_dvh_group['2'].study_instance_uid[r2] == uid and \ self.current_dvh_group['2'].roi_name[r2] == roi: found['Group 2'] = True if found['Group 1']: color = options.GROUP_1_and_2_COLOR else: color = options.GROUP_2_COLOR r2 += 1 colors.append(color) else: if self.current_dvh_group['1'] and self.current_dvh_group['2']: if uid in self.current_dvh_group['1'].study_instance_uid and \ uid in self.current_dvh_group['2'].study_instance_uid: colors.append(options.GROUP_1_and_2_COLOR) elif uid in self.current_dvh_group['1'].study_instance_uid: colors.append(options.GROUP_1_COLOR) else: colors.append(options.GROUP_2_COLOR) elif self.current_dvh_group['1']: colors.append(options.GROUP_1_COLOR) else: colors.append(options.GROUP_2_COLOR) y_values = [] y_mrns = [] for v in range(len(y_source_values)): if not skipped[v]: y_values.append(y_source_values[v]) y_mrns.append(y_source_mrns[v]) if not isinstance(y_values[-1], (int, long, float)): y_values[-1] = 0 sort_index = sorted(range(len(x_values)), key=lambda k: x_values[k]) x_values_sorted, y_values_sorted, y_mrns_sorted, colors_sorted = [], [], [], [] for s in range(len(x_values)): x_values_sorted.append(x_values[sort_index[s]]) y_values_sorted.append(y_values[sort_index[s]]) y_mrns_sorted.append(y_mrns[sort_index[s]]) colors_sorted.append(colors[sort_index[s]]) source_time_1_data = {'x': [], 'y': [], 'mrn': [], 'date_str': []} source_time_2_data = {'x': [], 'y': [], 'mrn': [], 'date_str': []} for i in range(len(x_values_sorted)): if colors_sorted[i] in {options.GROUP_1_COLOR, options.GROUP_1_and_2_COLOR}: source_time_1_data['x'].append(x_values_sorted[i]) source_time_1_data['y'].append(y_values_sorted[i]) source_time_1_data['mrn'].append(y_mrns_sorted[i]) source_time_1_data['date_str'].append(x_values_sorted[i].strftime("%Y-%m-%d")) if colors_sorted[i] in {options.GROUP_2_COLOR, options.GROUP_1_and_2_COLOR}: source_time_2_data['x'].append(x_values_sorted[i]) source_time_2_data['y'].append(y_values_sorted[i]) source_time_2_data['mrn'].append(y_mrns_sorted[i]) source_time_2_data['date_str'].append(x_values_sorted[i].strftime("%Y-%m-%d")) self.sources.time_1.data = source_time_1_data self.sources.time_2.data = source_time_2_data else: clear_source_data(self.sources, 'time_1') clear_source_data(self.sources, 'time_2') self.plot_update_trend() def plot_update_trend(self): if self.y_axis.value: selected_indices = {n: getattr(self.sources, 'time_%s' % n).selected.indices for n in GROUP_LABELS} for n in GROUP_LABELS: if not selected_indices[n]: selected_indices[n] = range(len(getattr(self.sources, 'time_%s' % n).data['x'])) group = {n: {'x': [], 'y': []} for n in GROUP_LABELS} for n in GROUP_LABELS: for i in range(len(getattr(self.sources, 'time_%s' % n).data['x'])): if i in selected_indices[n]: for v in ['x', 'y']: group[n][v].append(getattr(self.sources, 'time_%s' % n).data[v][i]) try: avg_len = int(self.look_back_distance.value) except: avg_len = 1 try: percentile = float(self.plot_percentile.value) except: percentile = 90. # average daily data and keep track of points per day, calculate moving average group_collapsed = {n: [] for n in GROUP_LABELS} for n in GROUP_LABELS: if group[n]['x']: group_collapsed[n] = collapse_into_single_dates(group[n]['x'], group[n]['y']) if self.look_back_units.value == "Dates with a Sim": x_trend, moving_avgs = moving_avg(group_collapsed[n], avg_len) else: x_trend, moving_avgs = moving_avg_by_calendar_day(group_collapsed[n], avg_len) y_np = np.array(group[n]['y']) upper_bound = float(np.percentile(y_np, 50. + percentile / 2.)) average = float(np.percentile(y_np, 50)) lower_bound = float(np.percentile(y_np, 50. - percentile / 2.)) getattr(self.sources, 'time_trend_%s' % n).data = {'x': x_trend, 'y': moving_avgs, 'mrn': ['Avg'] * len(x_trend)} getattr(self.sources, 'time_bound_%s' % n).data = {'x': group[n]['x'], 'mrn': ['Bound'] * len(group[n]['x']), 'upper': [upper_bound] * len(group[n]['x']), 'avg': [average] * len(group[n]['x']), 'lower': [lower_bound] * len(group[n]['x'])} getattr(self.sources, 'time_patch_%s' % n).data = {'x': [group[n]['x'][0], group[n]['x'][-1], group[n]['x'][-1], group[n]['x'][0]], 'y': [upper_bound, upper_bound, lower_bound, lower_bound]} else: for v in ['trend', 'bound', 'patch']: clear_source_data(self.sources, 'time_%s_%s' % (v, n)) x_var = str(self.y_axis.value) if x_var.startswith('DVH Endpoint'): self.histograms.xaxis.axis_label = x_var.split("DVH Endpoint: ")[1] elif x_var == 'EUD': self.histograms.xaxis.axis_label = "%s (Gy)" % x_var elif x_var == 'NTCP/TCP': self.histograms.xaxis.axis_label = "NTCP or TCP" else: if self.range_categories[x_var]['units']: self.histograms.xaxis.axis_label = "%s (%s)" % (x_var, self.range_categories[x_var]['units']) else: self.histograms.xaxis.axis_label = x_var # Normal Test s, p = {n: '' for n in GROUP_LABELS}, {n: '' for n in GROUP_LABELS} for n in GROUP_LABELS: if group[n]['y']: s[n], p[n] = normaltest(group[n]['y']) p[n] = "%0.3f" % p[n] # t-Test and Rank Sums pt, pr = '', '' if group['1']['y'] and group['2']['y']: st, pt = ttest_ind(group['1']['y'], group['2']['y']) sr, pr = ranksums(group['1']['y'], group['2']['y']) pt = "%0.3f" % pt pr = "%0.3f" % pr self.histogram_normaltest_1_text.text = "Group 1 Normal Test p-value = %s" % p['1'] self.histogram_normaltest_2_text.text = "Group 2 Normal Test p-value = %s" % p['2'] self.histogram_ttest_text.text = "Two Sample t-Test (Group 1 vs 2) p-value = %s" % pt self.histogram_ranksums_text.text = "Wilcoxon rank-sum (Group 1 vs 2) p-value = %s" % pr else: for n in GROUP_LABELS: for k in ['trend', 'bound', 'patch']: clear_source_data(self.sources, "time_%s_%s" % (k, n)) self.histogram_normaltest_1_text.text = "Group 1 Normal Test p-value = " self.histogram_normaltest_2_text.text = "Group 2 Normal Test p-value = " self.histogram_ttest_text.text = "Two Sample t-Test (Group 1 vs 2) p-value = " self.histogram_ranksums_text.text = "Wilcoxon rank-sum (Group 1 vs 2) p-value = " self.update_histograms() def update_histograms(self): if self.y_axis.value != '': # Update Histograms bin_size = int(self.histogram_bin_slider.value) width_fraction = 0.9 for n in GROUP_LABELS: hist, bins = np.histogram(getattr(self.sources, 'time_%s' % n).data['y'], bins=bin_size) if self.histogram_radio_group.active == 1: hist = np.divide(hist, np.float(np.max(hist))) self.histograms.yaxis.axis_label = "Relative Frequency" else: self.histograms.yaxis.axis_label = "Frequency" width = [width_fraction * (bins[1] - bins[0])] * bin_size center = (bins[:-1] + bins[1:]) / 2. getattr(self.sources, 'histogram_%s' % n).data = {'x': center, 'top': hist, 'width': width} else: for n in GROUP_LABELS: clear_source_data(self.sources, 'histogram_%s' % n) def update_y_axis_label(self): new = str(self.y_axis.value) if new: # If new has something in parenthesis, extract and put in front new_split = new.split(' (') if len(new_split) > 1: new_display = "%s %s" % (new_split[1].split(')')[0], new_split[0]) else: new_display = new if new.startswith('DVH Endpoint'): self.plot.yaxis.axis_label = str(self.y_axis.value).split(': ')[1] elif new == 'EUD': self.plot.yaxis.axis_label = 'EUD (Gy)' elif new == 'NTCP/TCP': self.plot.yaxis.axis_label = 'NTCP or TCP' elif self.range_categories[new]['units']: self.plot.yaxis.axis_label = "%s (%s)" % (new_display, self.range_categories[new]['units']) else: self.plot.yaxis.axis_label = new_display def update_options(self): new_options = list(self.range_categories) new_options.extend(['EUD', 'NTCP/TCP']) for ep in self.sources.endpoint_calcs.data: if ep.startswith('V_') or ep.startswith('D_'): new_options.append("DVH Endpoint: %s" % ep) new_options.sort() new_options.insert(0, '') self.y_axis.options = new_options self.y_axis.value = ''
class ProfileTimePlot(DashboardComponent): """Time plots of the current resource usage on the cluster This is two plots, one for CPU and Memory and another for Network I/O """ def __init__(self, server, doc=None, **kwargs): if doc is not None: self.doc = weakref.ref(doc) try: self.key = doc.session_context.request.arguments.get("key", None) except AttributeError: self.key = None if isinstance(self.key, list): self.key = self.key[0] if isinstance(self.key, bytes): self.key = self.key.decode() self.task_names = ["All", self.key] if self.key else ["All"] else: self.key = None self.task_names = ["All"] self.server = server self.start = None self.stop = None self.ts = {"count": [], "time": []} self.state = profile.create() data = profile.plot_data(self.state, profile_interval) self.states = data.pop("states") self.profile_plot, self.source = profile.plot_figure(data, **kwargs) changing = [False] # avoid repeated changes from within callback @without_property_validation def cb(attr, old, new): if changing[0] or len(new) == 0: return with log_errors(): data = profile.plot_data(self.states[new[0]], profile_interval) del self.states[:] self.states.extend(data.pop("states")) changing[0] = True # don't recursively trigger callback update(self.source, data) self.source.selected.indices = old changing[0] = False self.source.selected.on_change("indices", cb) self.ts_source = ColumnDataSource({"time": [], "count": []}) self.ts_plot = figure( title="Activity over time", height=150, x_axis_type="datetime", active_drag="xbox_select", tools="xpan,xwheel_zoom,xbox_select,reset", sizing_mode="stretch_width", toolbar_location="above", ) self.ts_plot.line("time", "count", source=self.ts_source) self.ts_plot.circle( "time", "count", source=self.ts_source, color=None, selection_color="orange" ) self.ts_plot.yaxis.visible = False self.ts_plot.grid.visible = False def ts_change(attr, old, new): with log_errors(): selected = self.ts_source.selected.indices if selected: start = self.ts_source.data["time"][min(selected)] / 1000 stop = self.ts_source.data["time"][max(selected)] / 1000 self.start, self.stop = min(start, stop), max(start, stop) else: self.start = self.stop = None self.trigger_update(update_metadata=False) self.ts_source.selected.on_change("indices", ts_change) self.reset_button = Button(label="Reset", button_type="success") self.reset_button.on_click(lambda: self.update(self.state)) self.update_button = Button(label="Update", button_type="success") self.update_button.on_click(self.trigger_update) self.select = Select(value=self.task_names[-1], options=self.task_names) def select_cb(attr, old, new): if new == "All": new = None self.key = new self.trigger_update(update_metadata=False) self.select.on_change("value", select_cb) self.root = column( row( self.select, self.reset_button, self.update_button, sizing_mode="scale_width", height=250, ), self.profile_plot, self.ts_plot, **kwargs, ) @without_property_validation def update(self, state, metadata=None): with log_errors(): self.state = state data = profile.plot_data(self.state, profile_interval) self.states = data.pop("states") update(self.source, data) if metadata is not None and metadata["counts"]: self.task_names = ["All"] + sorted(metadata["keys"]) self.select.options = self.task_names if self.key: ts = metadata["keys"][self.key] else: ts = metadata["counts"] times, counts = zip(*ts) self.ts = {"count": counts, "time": [t * 1000 for t in times]} self.ts_source.data.update(self.ts) @without_property_validation def trigger_update(self, update_metadata=True): async def cb(): with log_errors(): prof = await self.server.get_profile( key=self.key, start=self.start, stop=self.stop ) if update_metadata: metadata = await self.server.get_profile_metadata() else: metadata = None if isinstance(prof, gen.Future): prof, metadata = await asyncio.gather(prof, metadata) self.doc().add_next_tick_callback(lambda: self.update(prof, metadata)) self.server.loop.add_callback(cb)
def asset_crossfilter(output_by_assets,baseline): """Creates a crossfilter bokeh plot of results by asset :output_by_assets: Contains output by asset :type output_by_assets: dataframe :returns: :rtype: """ df_all = output_by_assets.copy() df = df_all[df_all['asset_category']!='Intellectual Property'].copy() # sort categories df['sort_order'] = df['asset_category'] df['sort_order'].replace(asset_category_order,inplace=True) df.sort_values(by="sort_order",axis=0,ascending=True,inplace=True) df.reset_index(inplace=True) # update asset_category names for better printing df['asset_category'].replace(asset_categories_for_print,inplace=True) columns = sorted(df.columns) discrete = [x for x in columns if df[x].dtype == object] continuous = [x for x in columns if x not in discrete] quantileable = [x for x in continuous if len(df[x].unique()) > 20] x = Select(title='X-Axis', value='metr_c', options=columns) x.on_change('value', update) y = Select(title='Y-Axis', value='asset_category', options=columns) y.on_change('value', update) size = Select(title='Size', value='assets_c', options=['None'] + quantileable) size.on_change('value', update) # color = Select(title='Color', value='None', options=['None'] + quantileable) # color.on_change('value', update) color = Select(title='Color', value='None', options=['None'] + discrete) color.on_change('value', update) controls = widgetbox([x, y, color, size], width=200) #layout = row(controls, create_figure(df,x,y,discrete,quantileable,continuous,size,color,controls)) layout = row(create_figure(df,x,y,discrete,quantileable,continuous,size,color,controls)) curdoc().add_root(layout) curdoc().title = "Crossfilter" # # open a session to keep our local document in sync with server # session = push_session(curdoc()) # session.show() # open the document in a browser # # session.loop_until_closed() # run forever # save plot to html plot = curdoc() #plot.circle([1,2], [3,4]) html = file_html(plot, CDN, "my plot") file = open(baseline+"crossfilter_html.html","wb") #open file in binary mode file.writelines(html) file.close()
plot.y_range.start = min(source_cut.data['importance']) plot.y_range.end = max(source_cut.data['importance']) def update_slider(attrname, old, new): n_samples = int(samples_slider.value) newdata = get_ends(n_samples) source_cut.data = dict(newdata.to_dict('list')) plot.x_range.factors = newdata['aspects'].tolist() # this was missing plot.y_range.start = min(source_cut.data['importance']) plot.y_range.end = max(source_cut.data['importance']) data_table = DataTable(source=source_cut, columns=columns, width=700, height=500) dataset_select.on_change('value', update_dataset) exponent_slider.on_change('value', update_dataset) ratings_box.on_change('value', update_dataset) samples_slider.on_change('value', update_slider) # Set up layout selects = row(dataset_select) inputs = column(selects, widgetbox(exponent_slider, ratings_box, samples_slider)) table = widgetbox(data_table) tab1 = Panel(child=table, title="Data") tab2 = Panel(child=plot, title="Bar Plot") tabs = Tabs(tabs=[tab1, tab2]) lay = layout([[inputs,tabs],]) # Add to document
'West Virginia': { 'name': 'West Virginia', }, 'Wisconsin': { 'name': 'Wisconsin', }, 'Wyoming': { 'name': 'Wyoming', } } # create dropdown menus state_select = Select(value=state, title='State', options=sorted(states.keys())) data_select = Select(value=selection, title='Group By:', options=['Race','Sex','Totals Only']) # counts dataframe used to determine if the sample size for a given race in a given state is large enough counts = df.pivot_table(values='science_degree',index='State',columns=['race_recode'],aggfunc="count") # get initial data source = get_dataset(df, states[state]['name'], counts, selection) # make the plot plot = make_plot(source, states[state]['name']) # add interactivity to menus state_select.on_change('value', update_plot) data_select.on_change('value', update_plot) # set up layout of plot controls = HBox(state_select, data_select) curdoc().add_root(VBox(controls, plot))
'title': 'Austin, TX', }, 'Boston': { 'airport': 'BOS', 'title': 'Boston, MA', }, 'Seattle': { 'airport': 'SEA', 'title': 'Seattle, WA', } } city_select = Select(value=city, title='City', options=sorted(cities.keys())) distribution_select = Select(value=distribution, title='Distribution', options=['Discrete', 'Smoothed']) df = pd.read_excel(join(dirname(__file__), 'data/2015_weather.xlsx')) source = get_dataset(df, cities[city]['airport'], distribution) plot = make_plot(source, "Weather data for " + cities[city]['title']) city_select.on_change('value', update_plot) distribution_select.on_change('value', update_plot) controls = column(city_select, distribution_select) curdoc().add_root(row(plot, controls)) curdoc().title = "Weather" show(plot)
class CrossFilter(DashboardComponent): def __init__(self, worker, **kwargs): with log_errors(): self.worker = worker quantities = [ 'nbytes', 'duration', 'bandwidth', 'count', 'start', 'stop' ] colors = ['inout-color', 'type-color', 'key-color'] # self.source = ColumnDataSource({name: [] for name in names}) self.source = ColumnDataSource({ 'nbytes': [1, 2], 'duration': [0.01, 0.02], 'bandwidth': [0.01, 0.02], 'count': [1, 2], 'type': ['int', 'str'], 'inout-color': ['blue', 'red'], 'type-color': ['blue', 'red'], 'key': ['add', 'inc'], 'start': [1, 2], 'stop': [1, 2] }) self.x = Select(title='X-Axis', value='nbytes', options=quantities) self.x.on_change('value', self.update_figure) self.y = Select(title='Y-Axis', value='bandwidth', options=quantities) self.y.on_change('value', self.update_figure) self.size = Select(title='Size', value='None', options=['None'] + quantities) self.size.on_change('value', self.update_figure) self.color = Select(title='Color', value='inout-color', options=['black'] + colors) self.color.on_change('value', self.update_figure) if 'sizing_mode' in kwargs: kw = {'sizing_mode': kwargs['sizing_mode']} else: kw = {} self.control = widgetbox([self.x, self.y, self.size, self.color], width=200, **kw) self.last_outgoing = 0 self.last_incoming = 0 self.kwargs = kwargs self.layout = row(self.control, self.create_figure(**self.kwargs), **kw) self.root = self.layout def update(self): with log_errors(): outgoing = self.worker.outgoing_transfer_log n = self.worker.outgoing_count - self.last_outgoing n = min(n, 1000) outgoing = [outgoing[-i].copy() for i in range(1, n)] self.last_outgoing = self.worker.outgoing_count incoming = self.worker.incoming_transfer_log n = self.worker.incoming_count - self.last_incoming n = min(n, 1000) incoming = [incoming[-i].copy() for i in range(1, n)] self.last_incoming = self.worker.incoming_count out = [] for msg in incoming: if msg['keys']: d = self.process_msg(msg) d['inout-color'] = 'red' out.append(d) for msg in outgoing: if msg['keys']: d = self.process_msg(msg) d['inout-color'] = 'blue' out.append(d) if out: out = transpose(out) if (len(self.source.data['stop']) and min(out['start']) > self.source.data['stop'][-1] + 10): self.source.data.update(out) else: self.source.stream(out, rollover=1000) def create_figure(self, **kwargs): with log_errors(): fig = figure(title='', tools='', **kwargs) size = self.size.value if size == 'None': size = 1 fig.circle(source=self.source, x=self.x.value, y=self.y.value, color=self.color.value, size=10, alpha=0.5, hover_alpha=1) fig.xaxis.axis_label = self.x.value fig.yaxis.axis_label = self.y.value fig.add_tools( # self.hover, ResetTool(), PanTool(), WheelZoomTool(), BoxZoomTool(), ) return fig def update_figure(self, attr, old, new): with log_errors(): fig = self.create_figure(**self.kwargs) self.layout.children[1] = fig def process_msg(self, msg): try: def func(k): return msg['keys'].get(k, 0) main_key = max(msg['keys'], key=func) typ = self.worker.types.get(main_key, object).__name__ keyname = key_split(main_key) d = { 'nbytes': msg['total'], 'duration': msg['duration'], 'bandwidth': msg['bandwidth'], 'count': len(msg['keys']), 'type': typ, 'type-color': color_of(typ), 'key': keyname, 'key-color': color_of(keyname), 'start': msg['start'], 'stop': msg['stop'] } return d except Exception as e: logger.exception(e) raise
for i in range(12): leads[i].x_range.start = low leads[i].x_range.end = high # Set widgets rangeslider = RangeSlider(start=0, end=1000, step=1, value=(0,1000), title="X range") file_selector = Select(value=None, options=nix(None,list_files_bokeh(DATA_DIR))) textboxold = PreText(text="Original points: \t[]") textboxnew = PreText(text="New points: \t[]") retrievebutton = Button(label='Retrieve Segmentation') storebutton = Button(label='Store Segmentation') writebutton = Button(label='Write to File') # Set callbacks file_selector.on_change('value', file_change) source.selected.on_change('indices', selection_change) retrievebutton.on_click(retrieve_segmentation) storebutton.on_click(save_segmentation) writebutton.on_click(write_segmentation) rangeslider.on_change('value',change_range) # set up layout textboxes = row(textboxold,textboxnew) buttons = row(retrievebutton,storebutton,writebutton,rangeslider) layout = column(file_selector,textboxes,buttons,grid) # initialize # update()
active_assay = metadata_dict[assay_key] new_paragarph = Div( text=format_assay_text(active_study, active_assay), width=300, ) return new_paragarph # HTML Elements --------------------------------------------------------------- title_div = Div(text="<h1>Aluminate CrossFilter</h1>") # Controls and Selectors ------------------------------------------------------ source.on_change('selected', tap_select_callback) x_selector = Select(title='X Axis', options=continuous, value=continuous[0]) x_selector.on_change('value', update_plot) y_selector = Select(title='Y-Axis', options=continuous, value=continuous[1]) y_selector.on_change('value', update_plot) color = Select(title='Color', value='None', options=['None'] + discrete) color.on_change('value', update_plot) size = Select(title='Size', value='None', options=['None'] + continuous) size.on_change('value', update_plot) controls = widgetbox([x_selector, y_selector, color, size]) layout = layout(children=[ title_div, [controls, create_figure(),
metric_substr = metric_select.value cutoff_timestamp = ( datetime.datetime.now() - datetime.timedelta(days=30)).strftime('%Y-%m-%d %H:%M:%S UTC') data = metric_history.fetch_data(test_name, cutoff_timestamp) plots = metric_history.make_plots(test_name, metric_substr, data) plot_rows = [row(p) for p in plots] if plots else [] curdoc().clear() curdoc().add_root(column(children=base_rows + plot_rows, )) t1 = time.time() timer.text = '(Execution time: %s seconds)' % round(t1 - t0, 4) # Try to parse the requested test_name from URL args. args = curdoc().session_context.request.arguments current_test_name = test_names[1] if args and 'test_name' in args: passed_in_test_name = str(args['test_name'][0], 'utf-8') current_test_name = passed_in_test_name test_select = Select(title='Select a test:', value=current_test_name, options=test_names) test_select.on_change('value', update) metric_select = TextInput( value="", title="Metric substring (blank to see all). Press enter.") metric_select.on_change('value', update) curdoc().title = 'Metrics History' update('value', '', current_test_name)
#Create the select widget select_widget = Select(options = ['uniform distribution', 'normal distribution'], value = 'uniform distribution', title = 'Select the distribution of your choice') #Define the callback function def callback(attr, old, new): if select_widget.value == 'uniform distribution': function = random else: function = normal data_points.data = {'x': function(size = initial_points), 'y': function(size = initial_points)} select_widget.on_change('value', callback) #Create a layout for the application layout = row(select_widget, plot) #Add the layout to the application curdoc().add_root(layout) #Launching the application bokeh serve --show bokeh.py ---------------------------------------------------------------------------------------------------------- #Creating an application with the button widget
# create a plot and style its properties fig = figure(width=plot_width, height=plot_height, x_axis_type="datetime", toolbar_location='above', tools="pan,box_zoom,xwheel_zoom,reset,save"); fig.quad(top='price_max', bottom='price_min', left='time_left', right='time_right', source=plot_data, color=Blues3[1]) fig.line(x='time_mid', y='price_mean', source=plot_data, color=Blues3[0]); # add widgets title=Div(text="<h1>Stock Price</h1>") description=Paragraph(text='Type in a symbol or select one from Dow 30 \ and Nasdaq 100, and its price on 5/6/2010 will be plotted shortly. Due to its \ high frequency, the series plotted is downsampled, with the shaded area denoting \ the min/max prices at every time point. Details can be revealed by zoom in with \ tools above the plot.') symbol_box=TextInput(value='', title='Type Symbol', width=100) market_select = Select(value='', title='Select ', width=100, options=['', 'Dow 30', 'Nasdaq 100']) symbol_select = Select(value='', title='Symbol ', width=100, options=[]) button = Button(label="GO", button_type='success', width=100) progress_bar=PreText() # add callbacks and interactions button.callback=CustomJS(code=dims_jscode, args=dict(plot=fig, dims=dims)) button.on_click(button_click) fig.x_range.on_change('start', update_plot) #fig.x_range.on_change('end', update_plot) fig.x_range.callback = CustomJS(code=dims_jscode, args=dict(plot=fig, dims=dims)) market_select.on_change('value', update_symbol_select) symbol_select.on_change('value', update_symbol_box) # put the button and plot in a layout and add to the document doc=curdoc() doc.add_root(column([widgetbox([title, description]), row([widgetbox(symbol_box), market_select,symbol_select]), button, progress_bar, fig], sizing_mode='scale_width'))
class BokehEventViewerWaveform(WaveformDisplay): def __init__(self, event_viewer, fig=None): """ A `ctapipe.visualization.bokeh.WaveformDisplay` modified to utilise a `ctapipe.core.container.DataContainer` directly. Parameters ---------- event_viewer : BokehEventViewer The BokehEventViewer this object belongs to fig : bokeh.plotting.figure Figure to store the bokeh plot onto (optional) """ self._event = None self._view = 'r0' self._telid = None self._channel = 0 self._pixel = 0 super().__init__(fig=fig) self._draw_integration_window() self._view_options = { 'r0': lambda e, t, c, p: e.r0.tel[t].waveform[c, p], 'r1': lambda e, t, c, p: e.r1.tel[t].waveform[c, p], 'dl0': lambda e, t, c, p: e.dl0.tel[t].waveform[c, p], 'cleaned': lambda e, t, c, p: e.dl1.tel[t].cleaned[c, p], } self.w_view = None self.event_viewer = event_viewer def _reset(self): for wav in self.event_viewer.waveforms: wav.pixel = 0 def _set_waveform(self): e = self.event v = self.view t = self.telid c = self.channel p = self.pixel if not e: self.event_viewer.log.warning("No event has been provided") return tels = list(e.r0.tels_with_data) if t is None: t = tels[0] if t not in tels: raise KeyError(f"Telescope {t} has no data") try: self.waveform = self._view_options[v](e, t, c, p) self.fig.title.text = f'{v} (Pixel = {p})' except TypeError: self.waveform = None def _draw_integration_window(self): self.intwin1 = Span(location=0, dimension='height', line_color='green', line_dash='dotted') self.intwin2 = Span(location=0, dimension='height', line_color='green', line_dash='dotted') self.fig.add_layout(self.intwin1) self.fig.add_layout(self.intwin2) def _set_integration_window(self): e = self.event t = self.telid c = self.channel p = self.pixel if e: if e.dl1.tel[t].extracted_samples is not None: # Get Windows windows = e.dl1.tel[t].extracted_samples[c, p] length = np.sum(windows) start = np.argmax(windows) end = start + length - 1 self.intwin1.location = start self.intwin2.location = end else: self.event_viewer.log.warning("No event has been provided") def refresh(self): self._set_waveform() self._set_integration_window() @property def event(self): return self._event @event.setter def event(self, val): self._event = val self._set_waveform() self._set_integration_window() def change_event(self, event, telid): if self.event: # Only reset when an event exists self._reset() self._telid = telid self.event = event @property def view(self): return self._view @view.setter def view(self, val): if val not in list(self._view_options.keys()): raise ValueError(f"View is not valid: {val}") self._view = val self._set_waveform() self._set_integration_window() @property def telid(self): return self._telid @telid.setter def telid(self, val): if self.event: # Only reset when an event exists self._reset() self._telid = val self._set_waveform() self._set_integration_window() @property def channel(self): return self._channel @channel.setter def channel(self, val): self._channel = val self._set_waveform() self._set_integration_window() @property def pixel(self): return self._pixel @pixel.setter def pixel(self, val): self._pixel = val self._set_waveform() self._set_integration_window() def _on_waveform_click(self, time): super()._on_waveform_click(time) self.event_viewer.change_time(time) def create_view_widget(self): self.w_view = Select(title="View:", value="", options=[], width=5) self.w_view.on_change('value', self.on_view_widget_change) self.layout = column([self.w_view, self.layout]) def update_view_widget(self): self.w_view.options = list(self._view_options.keys()) self.w_view.value = self.view def on_view_widget_change(self, _, __, ___): if self.view != self.w_view.value: self.view = self.w_view.value
class CrossFilter(DashboardComponent): def __init__(self, worker, **kwargs): with log_errors(): self.worker = worker quantities = [ "nbytes", "duration", "bandwidth", "count", "start", "stop" ] colors = ["inout-color", "type-color", "key-color"] # self.source = ColumnDataSource({name: [] for name in names}) self.source = ColumnDataSource({ "nbytes": [1, 2], "duration": [0.01, 0.02], "bandwidth": [0.01, 0.02], "count": [1, 2], "type": ["int", "str"], "inout-color": ["blue", "red"], "type-color": ["blue", "red"], "key": ["add", "inc"], "start": [1, 2], "stop": [1, 2], }) self.x = Select(title="X-Axis", value="nbytes", options=quantities) self.x.on_change("value", self.update_figure) self.y = Select(title="Y-Axis", value="bandwidth", options=quantities) self.y.on_change("value", self.update_figure) self.size = Select(title="Size", value="None", options=["None"] + quantities) self.size.on_change("value", self.update_figure) self.color = Select(title="Color", value="inout-color", options=["black"] + colors) self.color.on_change("value", self.update_figure) if "sizing_mode" in kwargs: kw = {"sizing_mode": kwargs["sizing_mode"]} else: kw = {} self.control = widgetbox([self.x, self.y, self.size, self.color], width=200, **kw) self.last_outgoing = 0 self.last_incoming = 0 self.kwargs = kwargs self.layout = row(self.control, self.create_figure(**self.kwargs), **kw) self.root = self.layout @without_property_validation def update(self): with log_errors(): outgoing = self.worker.outgoing_transfer_log n = self.worker.outgoing_count - self.last_outgoing n = min(n, 1000) outgoing = [outgoing[-i].copy() for i in range(1, n)] self.last_outgoing = self.worker.outgoing_count incoming = self.worker.incoming_transfer_log n = self.worker.incoming_count - self.last_incoming n = min(n, 1000) incoming = [incoming[-i].copy() for i in range(1, n)] self.last_incoming = self.worker.incoming_count out = [] for msg in incoming: if msg["keys"]: d = self.process_msg(msg) d["inout-color"] = "red" out.append(d) for msg in outgoing: if msg["keys"]: d = self.process_msg(msg) d["inout-color"] = "blue" out.append(d) if out: out = transpose(out) if (len(self.source.data["stop"]) and min(out["start"]) > self.source.data["stop"][-1] + 10): update(self.source, out) else: self.source.stream(out, rollover=1000) def create_figure(self, **kwargs): with log_errors(): fig = figure(title="", tools="", **kwargs) size = self.size.value if size == "None": size = 1 fig.circle( source=self.source, x=self.x.value, y=self.y.value, color=self.color.value, size=10, alpha=0.5, hover_alpha=1, ) fig.xaxis.axis_label = self.x.value fig.yaxis.axis_label = self.y.value fig.add_tools( # self.hover, ResetTool(), PanTool(), WheelZoomTool(), BoxZoomTool(), ) return fig @without_property_validation def update_figure(self, attr, old, new): with log_errors(): fig = self.create_figure(**self.kwargs) self.layout.children[1] = fig def process_msg(self, msg): try: def func(k): return msg["keys"].get(k, 0) status_key = max(msg["keys"], key=func) typ = self.worker.types.get(status_key, object).__name__ keyname = key_split(status_key) d = { "nbytes": msg["total"], "duration": msg["duration"], "bandwidth": msg["bandwidth"], "count": len(msg["keys"]), "type": typ, "type-color": color_of(typ), "key": keyname, "key-color": color_of(keyname), "start": msg["start"], "stop": msg["stop"], } return d except Exception as e: logger.exception(e) raise
''' # Create two dropdown Select widgets: select1, select2 select1 = Select(title='First', options=['A', 'B'], value='A') select2 = Select(title='Second', options=['1', '2', '3'], value='1') # Define a callback function: callback def callback(attr, old, new): # If select1 is 'A' if select1.value == 'A': # Set select2 options to ['1', '2', '3'] select2.options = ['1', '2', '3'] # Set select2 value to '1' select2.value = '1' else: # Set select2 options to ['100', '200', '300'] select2.options = ['100', '200', '300'] # Set select2 value to '100' select2.value = '100' # Attach the callback to the 'value' property of select1 select1.on_change('value', callback) # Create layout and add to current document layout = widgetbox(select1, select2) curdoc().add_root(layout) # To start bokeh server run this command in the current folder # bokeh serve --show 07-synchronize-two-dropdowns.py
class BokehFileViewer(Tool): name = "BokehFileViewer" description = ("Interactively explore an event file using the bokeh " "visualisation package") port = Int(5006, help="Port to open bokeh server onto").tag(config=True) disable_server = Bool(False, help="Do not start the bokeh server " "(useful for testing)").tag(config=True) aliases = Dict(dict( port='BokehFileViewer.port', disable_server='BokehFileViewer.disable_server', r='EventSourceFactory.product', f='EventSourceFactory.input_url', max_events='EventSourceFactory.max_events', ped='CameraR1CalibratorFactory.pedestal_path', tf='CameraR1CalibratorFactory.tf_path', pe='CameraR1CalibratorFactory.pe_path', ff='CameraR1CalibratorFactory.ff_path', extractor='ChargeExtractorFactory.product', extractor_t0='ChargeExtractorFactory.t0', extractor_window_width='ChargeExtractorFactory.window_width', extractor_window_shift='ChargeExtractorFactory.window_shift', extractor_sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG', extractor_sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG', extractor_lwt='ChargeExtractorFactory.lwt', cleaner='WaveformCleanerFactory.product', )) classes = List([ EventSourceFactory, ChargeExtractorFactory, CameraR1CalibratorFactory, CameraDL1Calibrator, WaveformCleanerFactory ]) def __init__(self, **kwargs): super().__init__(**kwargs) self._event = None self._event_index = None self._event_id = None self._telid = None self._channel = None self.w_next_event = None self.w_previous_event = None self.w_event_index = None self.w_event_id = None self.w_goto_event_index = None self.w_goto_event_id = None self.w_telid = None self.w_channel = None self.w_dl1_dict = None self.wb_extractor = None self.layout = None self.reader = None self.seeker = None self.extractor = None self.cleaner = None self.r1 = None self.dl0 = None self.dl1 = None self.viewer = None self._updating_dl1 = False def setup(self): self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]" kwargs = dict(config=self.config, tool=self) default_url = get_dataset_path("gamma_test.simtel.gz") EventSourceFactory.input_url.default_value = default_url self.reader = EventSourceFactory.produce(**kwargs) self.seeker = EventSeeker(self.reader, **kwargs) self.extractor = ChargeExtractorFactory.produce(**kwargs) self.cleaner = WaveformCleanerFactory.produce(**kwargs) self.r1 = CameraR1CalibratorFactory.produce( eventsource=self.reader, **kwargs ) self.dl0 = CameraDL0Reducer(**kwargs) self.dl1 = CameraDL1Calibrator( extractor=self.extractor, cleaner=self.cleaner, **kwargs ) self.viewer = BokehEventViewer(**kwargs) # Setup widgets self.viewer.create() self.viewer.enable_automatic_index_increment() self.create_previous_event_widget() self.create_next_event_widget() self.create_event_index_widget() self.create_goto_event_index_widget() self.create_event_id_widget() self.create_goto_event_id_widget() self.create_telid_widget() self.create_channel_widget() self.create_dl1_widgets() self.update_dl1_widget_values() # Setup layout self.layout = layout([ [self.viewer.layout], [ self.w_previous_event, self.w_next_event, self.w_goto_event_index, self.w_goto_event_id ], [self.w_event_index, self.w_event_id], [self.w_telid, self.w_channel], [self.wb_extractor] ]) def start(self): self.event_index = 0 def finish(self): if not self.disable_server: def modify_doc(doc): doc.add_root(self.layout) doc.title = self.name directory = os.path.abspath(os.path.dirname(__file__)) theme_path = os.path.join(directory, "theme.yaml") template_path = os.path.join(directory, "templates") doc.theme = Theme(filename=theme_path) env = jinja2.Environment( loader=jinja2.FileSystemLoader(template_path) ) doc.template = env.get_template('index.html') self.log.info('Opening Bokeh application on ' 'http://localhost:{}/'.format(self.port)) server = Server({'/': modify_doc}, num_procs=1, port=self.port) server.start() server.io_loop.add_callback(server.show, "/") server.io_loop.start() @property def event_index(self): return self._event_index @event_index.setter def event_index(self, val): try: self.event = self.seeker[val] except IndexError: self.log.warning("Event Index {} does not exist".format(val)) @property def event_id(self): return self._event_id @event_id.setter def event_id(self, val): try: self.event = self.seeker[str(val)] except IndexError: self.log.warning("Event ID {} does not exist".format(val)) @property def telid(self): return self._telid @telid.setter def telid(self, val): self.channel = 0 tels = list(self.event.r0.tels_with_data) if val not in tels: val = tels[0] self._telid = val self.viewer.telid = val self.update_telid_widget() @property def channel(self): return self._channel @channel.setter def channel(self, val): self._channel = val self.viewer.channel = val self.update_channel_widget() @property def event(self): return self._event @event.setter def event(self, val): # Calibrate self.r1.calibrate(val) self.dl0.reduce(val) self.dl1.calibrate(val) self._event = val self.viewer.event = val self._event_index = val.count self._event_id = val.r0.event_id self.update_event_index_widget() self.update_event_id_widget() self._telid = self.viewer.telid self.update_telid_widget() self._channel = self.viewer.channel self.update_channel_widget() def update_dl1_calibrator(self, extractor=None, cleaner=None): """ Recreate the dl1 calibrator with the specified extractor and cleaner Parameters ---------- extractor : ctapipe.image.charge_extractors.ChargeExtractor cleaner : ctapipe.image.waveform_cleaning.WaveformCleaner """ if extractor is None: extractor = self.dl1.extractor if cleaner is None: cleaner = self.dl1.cleaner self.extractor = extractor self.cleaner = cleaner kwargs = dict(config=self.config, tool=self) self.dl1 = CameraDL1Calibrator( extractor=self.extractor, cleaner=self.cleaner, **kwargs ) self.dl1.calibrate(self.event) self.viewer.refresh() def create_next_event_widget(self): self.w_next_event = Button(label=">", button_type="default", width=50) self.w_next_event.on_click(self.on_next_event_widget_click) def on_next_event_widget_click(self): self.event_index += 1 def create_previous_event_widget(self): self.w_previous_event = Button( label="<", button_type="default", width=50 ) self.w_previous_event.on_click(self.on_previous_event_widget_click) def on_previous_event_widget_click(self): self.event_index -= 1 def create_event_index_widget(self): self.w_event_index = TextInput(title="Event Index:", value='') def update_event_index_widget(self): if self.w_event_index: self.w_event_index.value = str(self.event_index) def create_event_id_widget(self): self.w_event_id = TextInput(title="Event ID:", value='') def update_event_id_widget(self): if self.w_event_id: self.w_event_id.value = str(self.event_id) def create_goto_event_index_widget(self): self.w_goto_event_index = Button( label="GOTO Index", button_type="default", width=100 ) self.w_goto_event_index.on_click(self.on_goto_event_index_widget_click) def on_goto_event_index_widget_click(self): self.event_index = int(self.w_event_index.value) def create_goto_event_id_widget(self): self.w_goto_event_id = Button( label="GOTO ID", button_type="default", width=70 ) self.w_goto_event_id.on_click(self.on_goto_event_id_widget_click) def on_goto_event_id_widget_click(self): self.event_id = int(self.w_event_id.value) def create_telid_widget(self): self.w_telid = Select(title="Telescope:", value="", options=[]) self.w_telid.on_change('value', self.on_telid_widget_change) def update_telid_widget(self): if self.w_telid: tels = [str(t) for t in self.event.r0.tels_with_data] self.w_telid.options = tels self.w_telid.value = str(self.telid) def on_telid_widget_change(self, _, __, ___): if self.telid != int(self.w_telid.value): self.telid = int(self.w_telid.value) def create_channel_widget(self): self.w_channel = Select(title="Channel:", value="", options=[]) self.w_channel.on_change('value', self.on_channel_widget_change) def update_channel_widget(self): if self.w_channel: try: n_chan = self.event.r0.tel[self.telid].waveform.shape[0] except AttributeError: n_chan = 1 channels = [str(c) for c in range(n_chan)] self.w_channel.options = channels self.w_channel.value = str(self.channel) def on_channel_widget_change(self, _, __, ___): if self.channel != int(self.w_channel.value): self.channel = int(self.w_channel.value) def create_dl1_widgets(self): self.w_dl1_dict = dict( cleaner=Select(title="Cleaner:", value='', width=5, options=WaveformCleanerFactory.subclass_names), extractor=Select(title="Extractor:", value='', width=5, options=ChargeExtractorFactory.subclass_names), extractor_t0=TextInput(title="T0:", value=''), extractor_window_width=TextInput(title="Window Width:", value=''), extractor_window_shift=TextInput(title="Window Shift:", value=''), extractor_sig_amp_cut_HG=TextInput(title="Significant Amplitude " "Cut (HG):", value=''), extractor_sig_amp_cut_LG=TextInput(title="Significant Amplitude " "Cut (LG):", value=''), extractor_lwt=TextInput(title="Local Pixel Weight:", value='')) for val in self.w_dl1_dict.values(): val.on_change('value', self.on_dl1_widget_change) self.wb_extractor = widgetbox( PreText(text="Charge Extractor Configuration"), self.w_dl1_dict['cleaner'], self.w_dl1_dict['extractor'], self.w_dl1_dict['extractor_t0'], self.w_dl1_dict['extractor_window_width'], self.w_dl1_dict['extractor_window_shift'], self.w_dl1_dict['extractor_sig_amp_cut_HG'], self.w_dl1_dict['extractor_sig_amp_cut_LG'], self.w_dl1_dict['extractor_lwt']) def update_dl1_widget_values(self): if self.w_dl1_dict: for key, val in self.w_dl1_dict.items(): if 'extractor' in key: if key == 'extractor': val.value = self.extractor.__class__.__name__ else: key = key.replace("extractor_", "") try: val.value = str(getattr(self.extractor, key)) except AttributeError: val.value = '' elif 'cleaner' in key: if key == 'cleaner': val.value = self.cleaner.__class__.__name__ else: key = key.replace("cleaner_", "") try: val.value = str(getattr(self.cleaner, key)) except AttributeError: val.value = '' def on_dl1_widget_change(self, _, __, ___): if self.event: if not self._updating_dl1: self._updating_dl1 = True cmdline = [] for key, val in self.w_dl1_dict.items(): if val.value: cmdline.append('--{}'.format(key)) cmdline.append(val.value) self.parse_command_line(cmdline) kwargs = dict(config=self.config, tool=self) extractor = ChargeExtractorFactory.produce(**kwargs) cleaner = WaveformCleanerFactory.produce(**kwargs) self.update_dl1_calibrator(extractor, cleaner) self.update_dl1_widget_values() self._updating_dl1 = False
sumario.source = ColumnDataSource(source.to_df().drop(['index','mes','OPCAO'],axis=1).\ describe().T.reset_index().rename(columns={'index':'Regiao'})) ## Definicao do callback para selecao de dados source.on_change('selected', media) ## Funcao para atualizacao do grafico com base na escolha de um tipo de indice def indice_update(attrname, old, new): source.data = ColumnDataSource(df[df.OPCAO == tipoIPCA.value]).data media(attrname, old, new) ## Definicao do callback no widget seletor de indice tipoIPCA.on_change('value', indice_update) ## Encapsulamento da criacao da figura em uma funcao def create_figure(): p = figure(plot_height=400, plot_width=800, toolbar_location='above', tools="pan,box_select,tap,box_zoom,lasso_select,reset") p.xaxis.formatter = DatetimeTickFormatter(months='%b/%Y') p.yaxis.formatter = NumeralTickFormatter(format='0.00%') glyphs = { d: [ p.circle('mes',
plot.title = algorithm def update_samples_or_dataset(attrname, old, new): global X, y dataset = dataset_select.value algorithm = algorithm_select.value n_clusters = int(clusters_slider.value) n_samples = int(samples_slider.value) X, y = get_dataset(dataset, n_samples) X, y_pred = clustering(X, algorithm, n_clusters) colors = [spectral[i] for i in y_pred] source.data['x'] = X[:, 0] source.data['y'] = X[:, 1] source.data['colors'] = colors algorithm_select.on_change('value', update_algorithm_or_clusters) clusters_slider.on_change('value', update_algorithm_or_clusters) dataset_select.on_change('value', update_samples_or_dataset) samples_slider.on_change('value', update_samples_or_dataset) # set up layout selects = HBox(dataset_select, algorithm_select) inputs = VBox(samples_slider, clusters_slider, selects) # add to document curdoc().add_root(HBox(inputs, plot))
def create_select(self, options_list, value, title): select = Select(options=options_list, value=value, title=title) select.on_change('value', self.callback_on_change_slider) return (select)
cols = ["Skeleton ID", "Merge Class", "Hemisphere", "Pair ID", "lineage"] def update_stats(data): stats.text = data[cols].to_string(index=False) def selection_change(attr, old, new): selected = source.selected.indices data = source.to_df() if selected: update_stats(data.iloc[selected]) x = Select(title="X-Axis", value="dim 0", options=latent_cols) x.on_change("value", update) y = Select(title="Y-Axis", value="dim 1", options=latent_cols) y.on_change("value", update) source.selected.on_change("indices", selection_change) controls = column(x, y, width=200) left_col = column(create_figure()) right_col = column(row(controls), row(stats)) # bottom_row = row(controls, stats) layout = row(left_col, right_col) curdoc().add_root(layout) curdoc().title = BRAIN_VERSION # html = file_html(scatter_fig, CDN, "mb_adj")
data = df[['t1', 't2', 't1_returns', 't2_returns']] source.data = data source_static.data = data update_stats(df, t1, t2) corr.title.text = '%s returns vs. %s returns' % (t1, t2) ts1.title.text, ts2.title.text = t1, t2 def update_stats(data, t1, t2): stats.text = str(data[[t1, t2, t1 + '_returns', t2 + '_returns']].describe()) ticker1.on_change('value', ticker1_change) ticker2.on_change('value', ticker2_change) def selection_change(attrname, old, new): t1, t2 = ticker1.value, ticker2.value data = get_data(t1, t2) selected = source.selected.indices if selected: data = data.iloc[selected, :] update_stats(data, t1, t2) source.selected.on_change('indices', selection_change) button = Button(label="Download", button_type="success")
# Create a new plot: plot plot = figure() # Add circles to the plot plot.circle('x', 'y', source=source) # Define a callback function: update_plot def update_plot(attr, old, new): # If the new Selection is 'female_literacy', update 'y' to female_literacy if new == 'female_literacy': source.data = { 'x' : fertility, 'y' : female_literacy } # Else, update 'y' to population else: source.data = { 'x' : fertility, 'y' : population } # Create a dropdown Select widget: select select = Select(title="distribution", options=['female_literacy', 'population'], value='female_literacy') # Attach the update_plot callback to the 'value' property of select select.on_change('value', update_plot) # Create layout and add to current document layout = row(select, plot) curdoc().add_root(layout)
raid_spell.data = new_data1 raid_swing.data = new_data2 raid_range.data = new_data3 raid_dots.data = new_data4 raid_heals.data = new_data5 raid_hots.data = new_data6 player_total_damage.data = new_data7 player_total_heals.data = new_data8 options = np.ndarray.tolist(Raid_spell_damage.sourceName.unique()) options.append('Orange Team') raid_select = Select(title="Player:", value='Orange Team', options=options) # Attach the callback to the 'value' property of slider raid_select.on_change('value', update_plot) # Create a HoverTool object: hover #show(widgetbox(raid_select)) hover = HoverTool( tooltips=[('sourceName', '@sourceName'), ('spellName', '@spellName'), ('destName', '@destName')]) #create a formated plot plot = figure(title='Raid Direct Spell Damage vs. Time', x_axis_type="datetime", plot_height=450, plot_width=1200, x_axis_label='Time', y_axis_label='Damage',
def produce_graphs(self, context, doc): """ Create timetool data timehistory, timetool vs ipm, and correlation timehistory graphs. Parameters ---------- context = zmq.Context() Creates zmq socket to receive data doc: bokeh.document (I think) Bokeh document to be displayed on webpage """ port = 5006 socket = context.socket(zmq.SUB) # MUST BE FROM SAME MACHINE, CHANGE IF NECESSARY!!! socket.connect("tcp://psanagpu114:%d" % port) socket.setsockopt(zmq.SUBSCRIBE, b"") # Note: Cannot name 'timetool' variables in hvTimeTool and hvIpmAmp the same thing # Otherwise, holoviews will try to sync the axis and throw off the ranges for the plots # since hvIpmAmp only deals with the last 1000 points whereas hvTimeTool deals with all # the points hvTimeTool = hv.DynamicMap(my_partial(hv.Points, kdims=['timestamp', 'timetool']), streams=[self.b_timetool]).options( width=1000, finalize_hooks=[apply_formatter], xrotation=45).redim.label( timestamp='Time in UTC', timetool='Timetool Data') hvIpmAmp = hv.DynamicMap( my_partial(hv.Scatter, kdims=['timetool', 'ipm']), streams=[self.b_IpmAmp]).options(width=500).redim.label( timetool='Last 1000 Timetool Data Points', ipm='Last 1000 Ipm Data Points') hvCorrTimeHistory = hv.DynamicMap( my_partial(hv.Scatter, kdims=['timestamp', 'correlation']), streams=[self.b_corr_timehistory ]).options(width=500, finalize_hooks=[apply_formatter], xrotation=45).redim.label(time='Time in UTC') layout = (hvIpmAmp + hvCorrTimeHistory + hvTimeTool).cols(2) hvplot = renderer.get_plot(layout) def push_data_timetool(buffer): """ Push data to timetool time history graph """ timetool_d = deque(maxlen=self.maxlen) timetool_t = deque(maxlen=self.maxlen) if socket.poll(timeout=0): data_dict = socket.recv_pyobj() timetool_d = data_dict['tt__FLTPOS_PS'] # Get time from data_dict timeData = deque(maxlen=self.maxlen) for time in data_dict['event_time']: num1 = str(time[0]) num2 = str(time[1]) fullnum = num1 + "." + num2 timeData.append(float(fullnum)) timetool_t = timeData # Convert time to seconds so bokeh formatter can get correct datetime times = [1000 * time for time in list(timetool_t)] data = pd.DataFrame({'timestamp': times, 'timetool': timetool_d}) buffer.send(data) def push_data_amp_ipm(buffer): """ Push data into timetool amp vs ipm graph """ timetool_d = deque(maxlen=self.maxlen) ipm_d = deque(maxlen=self.maxlen) if socket.poll(timeout=0): data_dict = socket.recv_pyobj() timetool_d = data_dict['tt__AMPL'] ipm_d = data_dict[self.switchButton] data = pd.DataFrame({'timetool': timetool_d, 'ipm': ipm_d}) buffer.send(data) def push_data_corr_time_history(buffer): """ Calculate correlation between timetool amp and ipm and push to correlation time history graph """ timetool_d = deque(maxlen=self.maxlen) timetool_t = deque(maxlen=self.maxlen) ipm_d = deque(maxlen=self.maxlen) if socket.poll(timeout=0): data_dict = socket.recv_pyobj() timetool_d = data_dict['tt__FLTPOS_PS'] ipm_d = data_dict[self.switchButton] # Get time from data_dict timeData = deque(maxlen=self.maxlen) for time in data_dict['event_time']: num1 = str(time[0]) num2 = str(time[1]) fullnum = num1 + "." + num2 timeData.append(float(fullnum)) timetool_t = timeData # Convert time to seconds so bokeh formatter can get correct datetime times = [1000 * time for time in list(timetool_t)] data = pd.DataFrame({'timetool': timetool_d, 'ipm': ipm_d}) data_corr = data['timetool'].rolling(window=120).corr( other=data['ipm']) # Start at index 119 so we don't get null data final_df = pd.DataFrame({ 'timestamp': times[119:], 'correlation': data_corr[119:] }) buffer.send(final_df) def switch(attr, old, new): """ Update drop down menu value """ self.switchButton = select.value self.clear_buffer() def stop(): """ Add pause and play functionality to graph """ if stopButton.label == 'Play': stopButton.label = 'Pause' self.cb_id_timetool = doc.add_periodic_callback( partial(push_data_timetool, buffer=self.b_timetool), 1000) self.cb_id_amp_ipm = doc.add_periodic_callback( partial(push_data_amp_ipm, buffer=self.b_IpmAmp), 1000) self.cb_id_corr_timehistory = doc.add_periodic_callback( partial(push_data_corr_time_history, buffer=self.b_corr_timehistory), 1000) else: stopButton.label = 'Play' doc.remove_periodic_callback(self.cb_id_timetool) doc.remove_periodic_callback(self.cb_id_amp_ipm) doc.remove_periodic_callback(self.cb_id_corr_timehistory) # Start the callback self.cb_id_timetool = doc.add_periodic_callback( partial(push_data_timetool, buffer=self.b_timetool), 1000) self.cb_id_amp_ipm = doc.add_periodic_callback( partial(push_data_amp_ipm, buffer=self.b_IpmAmp), 1000) self.cb_id_corr_timehistory = doc.add_periodic_callback( partial(push_data_corr_time_history, buffer=self.b_corr_timehistory), 1000) # Use this to test since ipm2 and ipm3 are too similar to see any differences # select = Select(title='ipm value:', value='ipm2__sum', options=['ipm2__sum', 'tt__FLTPOS_PS']) select = Select(title='ipm value:', value='ipm2__sum', options=['ipm2__sum', 'ipm3__sum']) select.on_change('value', switch) stopButton = Button(label='Pause') stopButton.on_click(stop) plot = column(select, stopButton, hvplot.state) doc.add_root(plot)
p.yaxis.axis_label = y_axis_menu.value return p #Añadimos el slider slider = Slider(title='Year', start=1970, end=2010, step=1, value=1970) def callback_slider(attr, old, new): layout.children[1].title.text = 'Gapminder Data for %d' % slider.value new1 = ColumnDataSource(df_year.loc[slider.value]) gapminder.data = new1.data slider.on_change('value', callback_slider) #Añadimos los selectores x_axis_menu = Select(options=['fertility', 'life', 'population', 'child_mortality'], value = 'fertility', title = "x-axis data") y_axis_menu = Select(options=['fertility', 'life', 'population', 'child_mortality'], value = 'life', title = "y-axis data") def callback_select_axis(attr, old, new): layout.children[1] = create_figure() x_axis_menu.on_change('value', callback_select_axis) y_axis_menu.on_change('value', callback_select_axis) col = column(widgetbox(slider), widgetbox(x_axis_menu), widgetbox(y_axis_menu)) layout = row(col, create_figure()) curdoc().add_root(layout) curdoc().title = "Gapminder"
class BokehFileViewer(Tool): name = "BokehFileViewer" description = ("Interactively explore an event file using the bokeh " "visualisation package") port = Int(5006, help="Port to open bokeh server onto").tag(config=True) disable_server = Bool(False, help="Do not start the bokeh server " "(useful for testing)").tag(config=True) default_url = get_dataset_path("gamma_test_large.simtel.gz") EventSource.input_url.default_value = default_url extractor_product = traits.create_class_enum_trait( ImageExtractor, default_value="NeighborPeakWindowSum") aliases = Dict( dict( port="BokehFileViewer.port", disable_server="BokehFileViewer.disable_server", f="EventSource.input_url", max_events="EventSource.max_events", extractor="BokehFileViewer.extractor_product", )) classes = List([ EventSource, ] + traits.classes_with_traits(ImageExtractor)) def __init__(self, **kwargs): super().__init__(**kwargs) self._event = None self._event_index = None self._event_id = None self._telid = None self._channel = None self.w_next_event = None self.w_previous_event = None self.w_event_index = None self.w_event_id = None self.w_goto_event_index = None self.w_goto_event_id = None self.w_telid = None self.w_channel = None self.w_dl1_dict = None self.wb_extractor = None self.layout = None self.reader = None self.seeker = None self.extractor = None self.calibrator = None self.viewer = None self._updating_dl1 = False # make sure, gzip files are seekable self.config.SimTelEventSource.back_seekable = True def setup(self): self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]" self.reader = EventSource.from_config(parent=self) self.seeker = EventSeeker(self.reader, parent=self) self.extractor = ImageExtractor.from_name( self.extractor_product, parent=self, subarray=self.reader.subarray, ) self.calibrator = CameraCalibrator( subarray=self.reader.subarray, parent=self, image_extractor=self.extractor, ) self.viewer = BokehEventViewer(parent=self, subarray=self.reader.subarray) # Setup widgets self.viewer.create() self.viewer.enable_automatic_index_increment() self.create_previous_event_widget() self.create_next_event_widget() self.create_event_index_widget() self.create_goto_event_index_widget() self.create_event_id_widget() self.create_goto_event_id_widget() self.create_telid_widget() self.create_channel_widget() self.create_dl1_widgets() self.update_dl1_widget_values() # Setup layout self.layout = layout([ [self.viewer.layout], [ self.w_previous_event, self.w_next_event, self.w_goto_event_index, self.w_goto_event_id, ], [self.w_event_index, self.w_event_id], [self.w_telid, self.w_channel], [self.wb_extractor], ]) def start(self): self.event_index = 0 def finish(self): if not self.disable_server: def modify_doc(doc): doc.add_root(self.layout) doc.title = self.name directory = os.path.abspath(os.path.dirname(__file__)) theme_path = os.path.join(directory, "theme.yaml") template_path = os.path.join(directory, "templates") doc.theme = Theme(filename=theme_path) env = jinja2.Environment( loader=jinja2.FileSystemLoader(template_path)) doc.template = env.get_template("index.html") self.log.info("Opening Bokeh application on " "http://localhost:{}/".format(self.port)) server = Server({"/": modify_doc}, num_procs=1, port=self.port) server.start() server.io_loop.add_callback(server.show, "/") server.io_loop.start() @property def event_index(self): return self._event_index @event_index.setter def event_index(self, val): try: self.event = self.seeker[val] except IndexError: self.log.warning(f"Event Index {val} does not exist") @property def event_id(self): return self._event_id @event_id.setter def event_id(self, val): try: self.event = self.seeker[str(val)] except IndexError: self.log.warning(f"Event ID {val} does not exist") @property def telid(self): return self._telid @telid.setter def telid(self, val): self.channel = 0 tels = list(self.event.r0.tels_with_data) if val not in tels: val = tels[0] self._telid = val self.viewer.telid = val self.update_telid_widget() @property def channel(self): return self._channel @channel.setter def channel(self, val): self._channel = val self.viewer.channel = val self.update_channel_widget() @property def event(self): return self._event @event.setter def event(self, val): self.calibrator(val) self._event = val self.viewer.event = val self._event_index = val.count self._event_id = val.index.event_id self.update_event_index_widget() self.update_event_id_widget() self._telid = self.viewer.telid self.update_telid_widget() self._channel = self.viewer.channel self.update_channel_widget() def update_dl1_calibrator(self, extractor=None): """ Recreate the dl1 calibrator with the specified extractor and cleaner Parameters ---------- extractor : ctapipe.image.extractor.ImageExtractor """ if extractor is None: extractor = self.calibrator.image_extractor self.extractor = extractor self.calibrator = CameraCalibrator( subarray=self.reader.subarray, parent=self, image_extractor=self.extractor, ) self.viewer.refresh() def create_next_event_widget(self): self.w_next_event = Button(label=">", button_type="default", width=50) self.w_next_event.on_click(self.on_next_event_widget_click) def on_next_event_widget_click(self): self.event_index += 1 def create_previous_event_widget(self): self.w_previous_event = Button(label="<", button_type="default", width=50) self.w_previous_event.on_click(self.on_previous_event_widget_click) def on_previous_event_widget_click(self): self.event_index -= 1 def create_event_index_widget(self): self.w_event_index = TextInput(title="Event Index:", value="") def update_event_index_widget(self): if self.w_event_index: self.w_event_index.value = str(self.event_index) def create_event_id_widget(self): self.w_event_id = TextInput(title="Event ID:", value="") def update_event_id_widget(self): if self.w_event_id: self.w_event_id.value = str(self.event_id) def create_goto_event_index_widget(self): self.w_goto_event_index = Button(label="GOTO Index", button_type="default", width=100) self.w_goto_event_index.on_click(self.on_goto_event_index_widget_click) def on_goto_event_index_widget_click(self): self.event_index = int(self.w_event_index.value) def create_goto_event_id_widget(self): self.w_goto_event_id = Button(label="GOTO ID", button_type="default", width=70) self.w_goto_event_id.on_click(self.on_goto_event_id_widget_click) def on_goto_event_id_widget_click(self): self.event_id = int(self.w_event_id.value) def create_telid_widget(self): self.w_telid = Select(title="Telescope:", value="", options=[]) self.w_telid.on_change("value", self.on_telid_widget_change) def update_telid_widget(self): if self.w_telid: tels = [str(t) for t in self.event.r0.tels_with_data] self.w_telid.options = tels self.w_telid.value = str(self.telid) def on_telid_widget_change(self, _, __, ___): if self.telid != int(self.w_telid.value): self.telid = int(self.w_telid.value) def create_channel_widget(self): self.w_channel = Select(title="Channel:", value="", options=[]) self.w_channel.on_change("value", self.on_channel_widget_change) def update_channel_widget(self): if self.w_channel: try: n_chan = self.event.r0.tel[self.telid].waveform.shape[0] except AttributeError: n_chan = 1 channels = [str(c) for c in range(n_chan)] self.w_channel.options = channels self.w_channel.value = str(self.channel) def on_channel_widget_change(self, _, __, ___): if self.channel != int(self.w_channel.value): self.channel = int(self.w_channel.value) def create_dl1_widgets(self): self.w_dl1_dict = dict( extractor=Select( title="Extractor:", value="", width=5, options=BokehFileViewer.extractor_product.values, ), extractor_window_start=TextInput(title="Window Start:", value=""), extractor_window_width=TextInput(title="Window Width:", value=""), extractor_window_shift=TextInput(title="Window Shift:", value=""), extractor_lwt=TextInput(title="Local Pixel Weight:", value=""), ) for val in self.w_dl1_dict.values(): val.on_change("value", self.on_dl1_widget_change) self.wb_extractor = widgetbox( PreText(text="Charge Extractor Configuration"), self.w_dl1_dict["extractor"], self.w_dl1_dict["extractor_window_start"], self.w_dl1_dict["extractor_window_width"], self.w_dl1_dict["extractor_window_shift"], self.w_dl1_dict["extractor_lwt"], ) def update_dl1_widget_values(self): if self.w_dl1_dict: for key, val in self.w_dl1_dict.items(): if "extractor" in key: if key == "extractor": val.value = self.extractor.__class__.__name__ else: key = key.replace("extractor_", "") try: val.value = str(getattr(self.extractor, key)) except AttributeError: val.value = "" def on_dl1_widget_change(self, _, __, ___): if self.event: if not self._updating_dl1: self._updating_dl1 = True cmdline = [] for key, val in self.w_dl1_dict.items(): k = key.replace("extractor_", "ImageExtractor.") if val.value: cmdline.append(f"--{k}={val.value}") self.parse_command_line(cmdline) extractor = ImageExtractor.from_name(self.extractor_product, parent=self) self.update_dl1_calibrator(extractor) self.update_dl1_widget_values() self._updating_dl1 = False
def modify_doc(symbol): global nearestExpiryDate df = optionUtility.getProcessedOptionChainData(symbol) nearestExpiryDate = optionUtility.nearestExpiryDate expiryDates = [nearestExpiryDate, "All Above"] latestData[symbol] = df allSources = {} def update(attr, old, new): selected_expiryDate = expiry_date_selector.value #selectedStrikePrice = allUniqueStrikePrice[strikePrice_selector.active]; activeStrikePriceIndexes = strikePrice_selector.active # will take top of selected if number is greater activeStrikePriceIndexes.sort() # starting one will be selected index = 0 selectedStrikeNumber = activeStrikePriceIndexes.__sizeof__() processed = [] #print(activeStrikePriceIndexes) for source in allWorkingSources: if (index > (selectedStrikeNumber - 1)): break # in case less number of strike is selected don't through error strikeIndex = activeStrikePriceIndexes[index] selectedStrikePrice = allUniqueStrikePrice[strikeIndex] new_src = 0 new_src = dashboard_tool.make_dataset(int(selectedStrikePrice), selected_expiryDate, symbol, isForTodayOnly) source.data.update(new_src.data) index = index + 1 processed.append(selectedStrikePrice) #print(selectedStrikePrice) # shown prices are these.. #print(processed, activeStrikePriceIndexes); allUniqueStrikePrice = latestData[symbol]['strikePrice'].apply( str).unique().tolist() ATMStrikeindex = int(np.floor(len(allUniqueStrikePrice) / 2)) expiry_date_selector = Select(value=nearestExpiryDate, options=expiryDates) strikePrice_selector = CheckboxButtonGroup( labels=allUniqueStrikePrice, active=[ ATMStrikeindex - 2, ATMStrikeindex - 1, ATMStrikeindex, ATMStrikeindex + 1, ATMStrikeindex + 2 ]) # in case multiple to be shown at once# # strikePrice_selector = RadioButtonGroup( # labels=allUniqueStrikePrice, active=ATMStrikeindex); # strikePrice_selector.on_change('active', update) expiry_date_selector.on_change('value', update) selected_expiryDate = expiry_date_selector.value #selectedStrikePrice = allUniqueStrikePrice[strikePrice_selector.active]; activeStrikePriceIndexes = strikePrice_selector.active allplots = [] allWorkingSources = [] for oneindex in activeStrikePriceIndexes: selectedStrikePrice = allUniqueStrikePrice[oneindex] src = dashboard_tool.make_dataset(int(selectedStrikePrice), selected_expiryDate, symbol, isForTodayOnly) allSources[selectedStrikePrice] = src allWorkingSources.append(src) p = dashboard_tool.make_plot(src, selectedStrikePrice) allplots.append(p) allplotslayout = column(allplots) layout = column(row(expiry_date_selector, strikePrice_selector), allplotslayout) tab = Panel(child=layout, title=symbol) return tab
# Visualization from bokeh.io import curdoc from bokeh.models.widgets import Tabs from bokeh.layouts import row, column from bokeh.models import Select, Panel from bokeh.layouts import gridplot, layout from panels.products_per_region import ProductsPerCountry default_product = "Apples" default_country = "Afghanistan" # # Initialize plot prod_per_country = ProductsPerCountry(df, default_country) # # Initialize selectors. country_select = Select(value=default_country, title='Country', options=sorted(countries)) country_select.on_change('value', prod_per_country.redraw_plot) # Put all the tabs into one application grid = gridplot([[prod_per_country.plot]]) # Panels prod_country_region = Panel(child=grid, title='Product per Region/Country') # Put the tabs in the current document for display curdoc().add_root(layout([[country_select], [prod_per_country.plot]]))
color=c, size=sz, line_color="white", alpha=0.6, hover_color='white', hover_alpha=0.5) return p def update(attr, old, new): layout.children[1] = create_figure() x = Select(title='X-Axis', value='athlete', options=columns) x.on_change('value', update) y = Select(title='Y-Axis', value='distance_km', options=columns) y.on_change('value', update) size = Select(title='Size', value='None', options=['None'] + continuous) size.on_change('value', update) color = Select(title='Color', value='None', options=['None'] + continuous) color.on_change('value', update) controls = column(x, y, color, size, width=200) layout = row(controls, create_figure()) curdoc().add_root(layout) curdoc().title = "Stava Vis"
# update table def update_table(): item_list = main_scatter_source.data['id'] if isinstance(item_list, pandas.core.series.Series): item_list = item_list.values.tolist() if len(current_indices)>0 and len(item_list)>0: selected_id = list() for i in current_indices: selected_item_id = item_list[i] selected_id.append(selected_item_id) new_df = df[(df.attr_name==table_selected_attribute) & (df.id.isin(selected_id))].groupby(['attr_value']).agg({'id':'count'}).sort_values(by='id', ascending=False).rename(columns={'id':'n'}) joined_df = calculate_ratio(new_df) table_source.data = ColumnDataSource._data_from_df(joined_df) else: new_df = df[(df.attr_name==table_selected_attribute)].groupby(['attr_value']).agg({'id':'count'}).sort_values(by='id', ascending=False).rename(columns={'id':'n'}) joined_df = calculate_ratio(new_df) table_source.data = ColumnDataSource._data_from_df(joined_df) # setup interactivity r.data_source.on_change('selected', update_selection_callback) main_dropdown.on_change('value', update_main_callback) table_dropdown.on_change('value', update_table_callback) threshold_input.on_change('value', update_threshold_callback) # open the document in a browser curdoc().add_root(layout) session.show() session.loop_until_closed()
population.xgrid.grid_line_color = None population.legend.location = "center_right" population.x_range.end = 2150 population.yaxis.minor_tick_line_color = None population.yaxis[0].formatter = FuncTickFormatter(code=""" return (Math.abs(tick) / 1e9) + " B" """) # Controls and callbacks year = Select(title="Year:", value="2010", options=years) location = Select(title="Location:", value="World", options=locations) def update(): age = df[(df.Location == location.value) & (df.Year == int(year.value))] ages.data = ColumnDataSource.from_df(age) pop = df[df.Location == location.value].groupby(df.Year).Value.sum() new_known = pop[pop.index <= 2010] new_predicted = pop[pop.index >= 2010] known.data = dict(x=new_known.index.map(str), y=new_known.values) predicted.data = dict(x=new_predicted.index.map(str), y=new_predicted.values) year.on_change('value', lambda attr, old, new: update()) location.on_change('value', lambda attr, old, new: update()) update() controls = column(year, location, width=600) curdoc().add_root(row(column(pyramid, population), controls))
("electronic configuration", "@electronic"), ] ######### CREATES CROSSFILTER ########################## # decide if all columns or crossfilter down to sub properties # The crossfilter widgets # first select code this crossfilters the available options to # available exchanges and elements code = Select(title='Code', value='vasp', options=codes) code.on_change('value', update) # second select exchange element = Select(title='Element', value='Cu', options=_elements) element.on_change('value', update) exchange = Select() # The plotter widgets x = Select(title='X-Axis', value='k-point', options=plottables) x.on_change('value', update)
class CrossFilter(DashboardComponent): def __init__(self, worker, **kwargs): with log_errors(): self.worker = worker quantities = ['nbytes', 'duration', 'bandwidth', 'count', 'start', 'stop'] colors = ['inout-color', 'type-color', 'key-color'] # self.source = ColumnDataSource({name: [] for name in names}) self.source = ColumnDataSource({ 'nbytes': [1, 2], 'duration': [0.01, 0.02], 'bandwidth': [0.01, 0.02], 'count': [1, 2], 'type': ['int', 'str'], 'inout-color': ['blue', 'red'], 'type-color': ['blue', 'red'], 'key': ['add', 'inc'], 'start': [1, 2], 'stop': [1, 2] }) self.x = Select(title='X-Axis', value='nbytes', options=quantities) self.x.on_change('value', self.update_figure) self.y = Select(title='Y-Axis', value='bandwidth', options=quantities) self.y.on_change('value', self.update_figure) self.size = Select(title='Size', value='None', options=['None'] + quantities) self.size.on_change('value', self.update_figure) self.color = Select(title='Color', value='inout-color', options=['black'] + colors) self.color.on_change('value', self.update_figure) if 'sizing_mode' in kwargs: kw = {'sizing_mode': kwargs['sizing_mode']} else: kw = {} self.control = widgetbox([self.x, self.y, self.size, self.color], width=200, **kw) self.last_outgoing = 0 self.last_incoming = 0 self.kwargs = kwargs self.layout = row(self.control, self.create_figure(**self.kwargs), **kw) self.root = self.layout @without_property_validation def update(self): with log_errors(): outgoing = self.worker.outgoing_transfer_log n = self.worker.outgoing_count - self.last_outgoing n = min(n, 1000) outgoing = [outgoing[-i].copy() for i in range(1, n)] self.last_outgoing = self.worker.outgoing_count incoming = self.worker.incoming_transfer_log n = self.worker.incoming_count - self.last_incoming n = min(n, 1000) incoming = [incoming[-i].copy() for i in range(1, n)] self.last_incoming = self.worker.incoming_count out = [] for msg in incoming: if msg['keys']: d = self.process_msg(msg) d['inout-color'] = 'red' out.append(d) for msg in outgoing: if msg['keys']: d = self.process_msg(msg) d['inout-color'] = 'blue' out.append(d) if out: out = transpose(out) if (len(self.source.data['stop']) and min(out['start']) > self.source.data['stop'][-1] + 10): self.source.data.update(out) else: self.source.stream(out, rollover=1000) def create_figure(self, **kwargs): with log_errors(): fig = figure(title='', tools='', **kwargs) size = self.size.value if size == 'None': size = 1 fig.circle(source=self.source, x=self.x.value, y=self.y.value, color=self.color.value, size=10, alpha=0.5, hover_alpha=1) fig.xaxis.axis_label = self.x.value fig.yaxis.axis_label = self.y.value fig.add_tools( # self.hover, ResetTool(), PanTool(), WheelZoomTool(), BoxZoomTool(), ) return fig @without_property_validation def update_figure(self, attr, old, new): with log_errors(): fig = self.create_figure(**self.kwargs) self.layout.children[1] = fig def process_msg(self, msg): try: def func(k): return msg['keys'].get(k, 0) main_key = max(msg['keys'], key=func) typ = self.worker.types.get(main_key, object).__name__ keyname = key_split(main_key) d = { 'nbytes': msg['total'], 'duration': msg['duration'], 'bandwidth': msg['bandwidth'], 'count': len(msg['keys']), 'type': typ, 'type-color': color_of(typ), 'key': keyname, 'key-color': color_of(keyname), 'start': msg['start'], 'stop': msg['stop'] } return d except Exception as e: logger.exception(e) raise
lambda n, weight=_: centrality_algorithms.degree_centrality(n), "Closeness Centrality": lambda n, weight=_: centrality_algorithms.closeness_centrality(n), "Betweenness Centrality": centrality_algorithms.betweenness_centrality} def update_centrality(attrname, old, new): centrality = centrality_metrics[select_centrality.value](network, weight='weight') _, nodes_centrality = zip(*sorted(centrality.items())) nodes_source.data['centrality'] = [7 + 10 * t / max(nodes_centrality) for t in nodes_centrality] select_centrality = Select(title="Centrality Metric:", value="Degree Centrality", options=list(centrality_metrics.keys())) select_centrality.on_change('value', update_centrality) def update_layout(): global layout new_layout = networkx.spring_layout(network, k=1.1/sqrt(network.number_of_nodes()), iterations=100) layout = new_layout nodes, nodes_coordinates = zip(*sorted(layout.items())) nodes_xs, nodes_ys = list(zip(*nodes_coordinates)) nodes_source.data['x'] = nodes_xs nodes_source.data['y'] = nodes_ys lines_source.data = get_edges_specs(network, layout) update_layout_button = Button(label="Update Layout")
import pandas as pd import numpy as np from bokeh.io import curdoc from bokeh.plotting import Figure, output_file, show from bokeh.models import ColumnDataSource, Select from bokeh.layouts import column COUNT = 10 df = pd.DataFrame({"x":np.random.rand(COUNT), "y":np.random.rand(COUNT), "color":"white"}) source = ColumnDataSource(df) fig = Figure() fig.circle(source=source, x="x", y="y", fill_color="color", line_color="black", size=40) select = Select(title="Option:", value="white", options=["white", "red", "blue", "yellow"]) def update_color(attrname, old, new): source.data["color"] = [select.value]*COUNT select.on_change('value', update_color) curdoc().add_root(column(fig, select))
# print("rt dropdown_handler: %s" % action) # global min_road_type # #min_road_type = # #session.store_document(document) #dropdown_min_road_type.on_click(dropdown_handler_rt) select_rt = Select(title="Min Road Type:", value=min_road_type, options=['Motorway', 'Trunk', 'Primary', 'Secondary', 'Tertiary']) def on_rt_change(obj, attr, old, new): print "Min Road:", new global min_road_type min_road_type = new select_rt.on_change('value', on_rt_change) ############## ############## # set max distance from gdelt to osm node #select_dist = Select(title="Max Node Distance (km):", # value=str(max_node_dist_km), # options=['0','1','2','3','4','5','6','7','8','9','10']) # #def on_dist_change(obj, attr, old, new): # print "Max Node Dist (km)", new # global max_node_dist_km # max_node_dist_km = int(new) # #select_dist.on_change('value', on_dist_change) select_dist = Slider(title="Max GDELT Distance to Road (km)",
def eda_projects_tab(panel_title): lags_corr_src = ColumnDataSource(data=dict(variable_1=[], variable_2=[], relationship=[], lag=[], r=[], p_value=[])) class Thistab(Mytab): def __init__(self, table, cols, dedup_cols=[]): Mytab.__init__(self, table, cols, dedup_cols) self.table = table self.cols = cols self.DATEFORMAT = "%Y-%m-%d %H:%M:%S" self.df = None self.df1 = None self.df_predict = None self.day_diff = 1 # for normalizing for classification periods of different lengths self.df_grouped = '' self.cl = PythonClickhouse('aion') self.trigger = 0 self.groupby_dict = { 'project_duration': 'sum', 'project_start_delay': 'mean', 'project_end_delay': 'mean', 'project_owner_age': 'mean', 'project_owner_gender': 'mean', 'milestone_duration': 'sum', 'milestone_start_delay': 'mean', 'milestone_end_delay': 'mean', 'milestone_owner_age': 'mean', 'milestone_owner_gender': 'mean', 'task_duration': 'sum', 'task_start_delay': 'sum', 'task_end_delay': 'mean', 'task_owner_age': 'mean', 'task_owner_gender': 'mean' } self.feature_list = list(self.groupby_dict.keys()) self.lag_variable = 'task_duration' self.lag_days = "1,2,3" self.lag = 0 self.lag_menu = [str(x) for x in range(0, 100)] self.strong_thresh = .65 self.mod_thresh = 0.4 self.weak_thresh = 0.25 self.corr_df = None self.div_style = """ style='width:350px; margin-left:25px; border:1px solid #ddd;border-radius:3px;background:#efefef50;' """ self.header_style = """ style='color:blue;text-align:center;' """ self.variables = sorted(list(self.groupby_dict.keys())) self.variable = self.variables[0] self.relationships_to_check = ['weak', 'moderate', 'strong'] self.status = 'all' self.pm_gender = 'all' self.m_gender = 'all' self.t_gender = 'all' self.type = 'all' self.pym = PythonMongo('aion') self.menus = { 'status': ['all', 'open', 'closed'], 'type': [ 'all', 'research', 'reconciliation', 'audit', 'innovation', 'construction', 'manufacturing', 'conference' ], 'gender': ['all', 'male', 'female'], 'variables': list(self.groupby_dict.keys()), 'history_periods': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'], } self.multiline_vars = {'x': 'manager_gender', 'y': 'remuneration'} self.timestamp_col = 'project_startdate_actual' # ------- DIVS setup begin self.page_width = 1250 txt = """<hr/> <div style="text-align:center;width:{}px;height:{}px; position:relative;background:black;margin-bottom:200px"> <h1 style="color:#fff;margin-bottom:300px">{}</h1> </div>""".format(self.page_width, 50, 'Welcome') self.notification_div = { 'top': Div(text=txt, width=self.page_width, height=20), 'bottom': Div(text=txt, width=self.page_width, height=10), } lag_section_head_txt = 'Lag relationships between {} and...'.format( self.variable) self.section_divider = '-----------------------------------' self.section_headers = { 'lag': self.section_header_div(text=lag_section_head_txt, width=600, html_header='h2', margin_top=5, margin_bottom=-155), 'distribution': self.section_header_div(text='Pre-transform distribution:', width=600, html_header='h2', margin_top=5, margin_bottom=-155), 'relationships': self.section_header_div( text='Relationships between variables:{}'.format( self.section_divider), width=600, html_header='h2', margin_top=5, margin_bottom=-155), 'correlations': self.section_header_div(text='Correlations:', width=600, html_header='h3', margin_top=5, margin_bottom=-155), } # ----- UPDATED DIVS END # ---------------------- DIVS ---------------------------- def section_header_div(self, text, html_header='h2', width=600, margin_top=150, margin_bottom=-150): text = """<div style="margin-top:{}px;margin-bottom:-{}px;"><{} style="color:#4221cc;">{}</{}></div>""" \ .format(margin_top, margin_bottom, html_header, text, html_header) return Div(text=text, width=width, height=15) def notification_updater(self, text): txt = """<div style="text-align:center;background:black;width:100%;"> <h4 style="color:#fff;"> {}</h4></div>""".format(text) for key in self.notification_div.keys(): self.notification_div[key].text = txt def reset_adoption_dict(self, variable): self.significant_effect_dict[variable] = [] # ////////////// DIVS ///////////////////////////////// def title_div(self, text, width=700): text = '<h2 style="color:#4221cc;">{}</h2>'.format(text) return Div(text=text, width=width, height=15) def corr_information_div(self, width=400, height=300): div_style = """ style='width:350px; margin-left:-600px; border:1px solid #ddd;border-radius:3px;background:#efefef50;' """ txt = """ <div {}> <h4 {}>How to interpret relationships </h4> <ul style='margin-top:-10px;'> <li> Positive: as variable 1 increases, so does variable 2. </li> <li> Negative: as variable 1 increases, variable 2 decreases. </li> <li> Strength: decisions can be made on the basis of strong and moderate relationships. </li> <li> No relationship/not significant: no statistical support for decision making. </li> <li> The scatter graphs (below) are useful for visual confirmation. </li> <li> The histogram (right) shows the distribution of the variable. </li> </ul> </div> """.format(div_style, self.header_style) div = Div(text=txt, width=width, height=height) return div # ///////////////////////////////////////////////////////////// def filter_df(self, df1): if self.status != 'all': df1 = df1[df1.status == self.status] if self.pm_gender != 'all': df1 = df1[df1.project_owner_gender == self.pm_gender] if self.m_gender != 'all': df1 = df1[df1.milestone_owner_gender == self.m_gender] if self.t_gender != 'all': df1 = df1[df1.task_owner_gender == self.t_gender] if self.type != 'all': df1 = df1[df1.type == self.type] return df1 def prep_data(self, df1): try: ''' df1[self.timestamp_col] = df1[self.timestamp_col].apply(lambda x: datetime(x.year, x.month, x.day, x.hour,0,0)) ''' df1 = df1.set_index(self.timestamp_col) logger.warning('LINE 195 df:%s', df1.head()) # handle lag for all variables df = df1.copy() df = self.filter_df(df) logger.warning('LINE 199: length before:%s', len(df)) slice = df[['project']] df = df[list(self.groupby_dict.keys())] logger.warning('LINE 218: columns:%s', df.head()) df = df.astype(float) df = pd.concat([df, slice], axis=1) df = df.groupby('project').resample(self.resample_period).agg( self.groupby_dict) logger.warning('LINE 201: length after:%s', len(df)) df = df.reset_index() vars = self.feature_list.copy() if int(self.lag) > 0: for var in vars: if self.variable != var: df[var] = df[var].shift(int(self.lag)) df = df.dropna() self.df1 = df logger.warning('line 184- prep data: df:%s', self.df.head(10)) except Exception: logger.error('prep data', exc_info=True) def lags_plot(self, launch): try: df = self.df.copy() df = df[[self.lag_variable, self.variable]] cols = [self.lag_variable] lags = self.lag_days.split(',') for day in lags: try: label = self.lag_variable + '_' + day df[label] = df[self.lag_variable].shift(int(day)) cols.append(label) except: logger.warning('%s is not an integer', day) df = df.dropna() self.lags_corr(df) # plot the comparison logger.warning('in lags plot: df:%s', df.head(10)) return df.hvplot(x=self.variable, y=cols, kind='scatter', alpha=0.4) except Exception: logger.error('lags plot', exc_info=True) # calculate the correlation produced by the lags vector def lags_corr(self, df): try: corr_dict_data = { 'variable_1': [], 'variable_2': [], 'relationship': [], 'lag': [], 'r': [], 'p_value': [] } a = df[self.variable].tolist() for col in df.columns: if col not in [self.timestamp_col, self.variable]: # find lag var = col.split('_') try: tmp = int(var[-1]) lag = tmp except Exception: lag = 'None' b = df[col].tolist() slope, intercept, rvalue, pvalue, txt = self.corr_label( a, b) corr_dict_data['variable_1'].append(self.variable) corr_dict_data['variable_2'].append(col) corr_dict_data['relationship'].append(txt) corr_dict_data['lag'].append(lag) corr_dict_data['r'].append(round(rvalue, 4)) corr_dict_data['p_value'].append(round(pvalue, 4)) lags_corr_src.stream(corr_dict_data, rollover=(len(corr_dict_data['lag']))) columns = [ TableColumn(field="variable_1", title="variable 1"), TableColumn(field="variable_2", title="variable 2"), TableColumn(field="relationship", title="relationship"), TableColumn(field="lag", title="lag(days)"), TableColumn(field="r", title="r"), TableColumn(field="p_value", title="p_value"), ] data_table = DataTable(source=lags_corr_src, columns=columns, width=500, height=280) return data_table except Exception: logger.error('lags corr', exc_info=True) def correlation_table(self, launch): try: corr_dict = { 'Variable 1': [], 'Variable 2': [], 'Relationship': [], 'r': [], 'p-value': [] } # prep df df = self.df1 # get difference for money columns df = df.drop(self.timestamp_col, axis=1) # df = df.compute() a = df[self.variable].tolist() for col in self.feature_list: logger.warning('col :%s', col) if col != self.variable: logger.warning('%s:%s', col, self.variable) b = df[col].tolist() slope, intercept, rvalue, pvalue, txt = self.corr_label( a, b) # add to dict corr_dict['Variable 1'].append(self.variable) corr_dict['Variable 2'].append(col) corr_dict['Relationship'].append(txt) corr_dict['r'].append(round(rvalue, 4)) corr_dict['p-value'].append(round(pvalue, 4)) df = pd.DataFrame({ 'Variable 1': corr_dict['Variable 1'], 'Variable 2': corr_dict['Variable 2'], 'Relationship': corr_dict['Relationship'], 'r': corr_dict['r'], 'p-value': corr_dict['p-value'] }) # logger.warning('df:%s',df.head(23)) return df.hvplot.table(columns=[ 'Variable 1', 'Variable 2', 'Relationship', 'r', 'p-value' ], width=550, height=200, title='Correlation between variables') except Exception: logger.error('correlation table', exc_info=True) def non_parametric_relationship_table(self, launch): try: corr_dict = { 'Variable 1': [], 'Variable 2': [], 'Relationship': [], 'stat': [], 'p-value': [] } # prep df df = self.df1 # get difference for money columns df = df.drop(self.timestamp_col, axis=1) # df = df.compute() # logger.warning('line df:%s',df.head(10)) a = df[self.variable].tolist() for col in self.feature_list: logger.warning('col :%s', col) if col != self.variable: logger.warning('%s:%s', col, self.variable) b = df[col].tolist() stat, pvalue, txt = self.mann_whitneyu_label(a, b) corr_dict['Variable 1'].append(self.variable) corr_dict['Variable 2'].append(col) corr_dict['Relationship'].append(txt) corr_dict['stat'].append(round(stat, 4)) corr_dict['p-value'].append(round(pvalue, 4)) df = pd.DataFrame({ 'Variable 1': corr_dict['Variable 1'], 'Variable 2': corr_dict['Variable 2'], 'Relationship': corr_dict['Relationship'], 'stat': corr_dict['stat'], 'p-value': corr_dict['p-value'] }) # logger.warning('df:%s',df.head(23)) return df.hvplot.table( columns=[ 'Variable 1', 'Variable 2', 'Relationship', 'stat', 'p-value' ], width=550, height=200, title='Non parametric relationship between variables') except Exception: logger.error('non parametric table', exc_info=True) def hist(self, launch): try: return self.df.hvplot.hist(y=self.feature_list, subplots=True, shared_axes=False, bins=25, alpha=0.3, width=300).cols(4) except Exception: logger.warning('histogram', exc_info=True) def matrix_plot(self, launch=-1): try: logger.warning('line 306 self.feature list:%s', self.feature_list) df = self.df1 if df is not None: # thistab.prep_data(thistab.df) if self.timestamp_col in df.columns: df = df.drop(self.timestamp_col, axis=1) df = df.fillna(0) # logger.warning('line 302. df: %s',df.head(10)) cols_temp = self.feature_list.copy() if self.variable in cols_temp: cols_temp.remove(self.variable) # variable_select.options = cols_lst p = df.hvplot.scatter(x=self.variable, y=cols_temp, width=330, subplots=True, shared_axes=False, xaxis=False).cols(4) else: p = df.hvplot.scatter(x=[0, 0, 0], y=[0, 0, 0], width=330) return p except Exception: logger.error('matrix plot', exc_info=True) def multiline(self, launch=1): try: yvar = self.multiline_vars['y'] xvar = self.multiline_vars['x'] df = self.df.copy() df = df[[xvar, yvar, self.timestamp_col]] df = df.set_index(self.timestamp_col) df = df.groupby(xvar).resample(self.resample_period).agg( {yvar: 'mean'}) df = df.reset_index() lines = df[xvar].unique() # split data frames dfs = {} for idx, line in enumerate(lines): dfs[line] = df[df[xvar] == line] dfs[line] = dfs[line].fillna(0) logger.warning('LINE 428:%s - %s:', line, dfs[line].head()) if idx == 0: p = dfs[line].hvplot.line(x=self.timestamp_col, y=yvar, width=1200, height=500).relabel(line) else: p *= dfs[line].hvplot.line(x=self.timestamp_col, y=yvar, width=2, height=500).relabel(line) return p except Exception: logger.error('multiline plot', exc_info=True) def update_variable(attr, old, new): thistab.notification_updater("Calculations in progress! Please wait.") thistab.prep_data(thistab.df) if 'milestone owner gender' == new: thistab.variable = 'm_gender_code' if 'project owner gender' == new: thistab.variable = 'pm_gender_code' if 'task owner gender' == new: thistab.variable = 't_gender_code' if thistab.variable in thistab.adoption_variables['developer']: thistab.reset_adoption_dict(thistab.variable) thistab.section_head_updater('lag', thistab.variable) thistab.trigger += 1 stream_launch_matrix.event(launch=thistab.trigger) stream_launch_corr.event(launch=thistab.trigger) thistab.notification_updater("Ready!") def update_lag_plot_variable(attr, old, new): thistab.notification_updater("Calculations in progress! Please wait.") thistab.lag_variable = new thistab.prep_data(thistab.df) thistab.trigger += 1 stream_launch_lags_var.event(launch=thistab.trigger) thistab.notification_updater("Ready!") def update_IVs(attrname, old, new): thistab.notification_updater("Calculations in progress! Please wait.") thistab.pm_gender = pm_gender_select.value thistab.m_gender = m_gender_select.value thistab.t_gender = t_gender_select.value thistab.status = status_select.value thistab.type = type_select.value thistab.prep_data(thistab.df) thistab.trigger += 1 stream_launch_matrix.event(launch=thistab.trigger) stream_launch_corr.event(launch=thistab.trigger) thistab.notification_updater("Ready!") def update_lag(attr, old, new): # update lag & cryptocurrency thistab.notification_updater("Calculations in progress! Please wait.") thistab.lag = int(lag_select.value) thistab.prep_data(thistab.df) thistab.trigger += 1 stream_launch_matrix.event(launch=thistab.trigger) stream_launch_corr.event(launch=thistab.trigger) thistab.notification_updater("Ready!") def update(attrname, old, new): thistab.notification_updater( "Calculations underway. Please be patient") thistab.df = thistab.pym.load_df(start_date=datepicker_start.value, end_date=datepicker_end.value, cols=[], table=thistab.table, timestamp_col=thistab.timestamp_col) thistab.df['project_owner_gender'] = thistab.df[ 'project_owner_gender'].apply(lambda x: 1 if x == 'male' else 2) thistab.df['milestone_owner_gender'] = thistab.df[ 'milestone_owner_gender'].apply(lambda x: 1 if x == 'male' else 2) thistab.df['task_owner_gender'] = thistab.df[ 'task_owner_gender'].apply(lambda x: 1 if x == 'male' else 2) thistab.prep_data(thistab.df) thistab.trigger += 1 stream_launch_matrix.event(launch=thistab.trigger) stream_launch_corr.event(launch=thistab.trigger) thistab.notification_updater("Ready!") def update_resample(attrname, old, new): thistab.notification_updater( "Calculations underway. Please be patient") thistab.resample_period = new thistab.prep_data(thistab.df) thistab.trigger += 1 stream_launch_matrix.event(launch=thistab.trigger) stream_launch_corr.event(launch=thistab.trigger) stream_launch.event(launch=thistab.trigger) thistab.notification_updater("Ready!") def update_lags_selected(): thistab.notification_updater("Calculations in progress! Please wait.") thistab.lag_days = lags_input.value logger.warning('line 381, new checkboxes: %s', thistab.lag_days) thistab.trigger += 1 stream_launch_lags_var.event(launch=thistab.trigger) stream_launch.event(launch=thistab.trigger) thistab.notification_updater("Ready!") def update_multiline(attrname, old, new): thistab.notification_updater("Calculations in progress! Please wait.") thistab.multiline_vars['x'] = multiline_x_select.value thistab.multiline_vars['y'] = multiline_y_select.value thistab.trigger += 1 stream_launch.event(launch=thistab.trigger) thistab.notification_updater("Ready!") try: # SETUP table = 'project_composite1' thistab = Thistab(table, [], []) # setup dates first_date_range = datetime.strptime("2013-04-25 00:00:00", "%Y-%m-%d %H:%M:%S") last_date_range = datetime.now().date() last_date = dashboard_config['dates']['last_date'] - timedelta(days=2) first_date = last_date - timedelta(days=30) # initial function call thistab.df = thistab.pym.load_df(start_date=first_date, end_date=last_date, cols=[], table=thistab.table, timestamp_col=thistab.timestamp_col) if len(thistab.df) > 0: thistab.df['manager_gender'] = thistab.df['project_owner_gender'] thistab.df['project_owner_gender'] = thistab.df[ 'project_owner_gender'].apply(lambda x: 1 if x == 'male' else 2) thistab.df['milestone_owner_gender'] = thistab.df[ 'milestone_owner_gender'].apply(lambda x: 1 if x == 'male' else 2) thistab.df['task_owner_gender'] = thistab.df[ 'task_owner_gender'].apply(lambda x: 1 if x == 'male' else 2) logger.warning('LINE 527:columns %s', list(thistab.df.columns)) thistab.prep_data(thistab.df) # MANAGE STREAM stream_launch_hist = streams.Stream.define('Launch', launch=-1)() stream_launch_matrix = streams.Stream.define('Launch_matrix', launch=-1)() stream_launch_corr = streams.Stream.define('Launch_corr', launch=-1)() stream_launch_lags_var = streams.Stream.define('Launch_lag_var', launch=-1)() stream_launch = streams.Stream.define('Launch', launch=-1)() # CREATE WIDGETS datepicker_start = DatePicker(title="Start", min_date=first_date_range, max_date=last_date_range, value=first_date) datepicker_end = DatePicker(title="End", min_date=first_date_range, max_date=last_date_range, value=last_date) variable_select = Select(title='Select variable', value=thistab.variable, options=thistab.variables) lag_variable_select = Select(title='Select lag variable', value=thistab.lag_variable, options=thistab.feature_list) lag_select = Select(title='Select lag', value=str(thistab.lag), options=thistab.lag_menu) type_select = Select(title='Select project type', value=thistab.type, options=thistab.menus['type']) status_select = Select(title='Select project status', value=thistab.status, options=thistab.menus['status']) pm_gender_select = Select(title="Select project owner's gender", value=thistab.pm_gender, options=thistab.menus['gender']) m_gender_select = Select(title="Select milestone owner's gender", value=thistab.m_gender, options=thistab.menus['gender']) t_gender_select = Select(title="Select task owner's gender", value=thistab.t_gender, options=thistab.menus['gender']) resample_select = Select(title='Select resample period', value='D', options=['D', 'W', 'M', 'Q']) multiline_y_select = Select(title='Select comparative DV(y)', value=thistab.multiline_vars['y'], options=[ 'remuneration', 'delay_start', 'delay_end', 'project_duration' ]) multiline_x_select = Select( title='Select comparative IV(x)', value=thistab.multiline_vars['x'], options=['manager_gender', 'type', 'status']) lags_input = TextInput( value=thistab.lag_days, title="Enter lags (integer(s), separated by comma)", height=55, width=300) lags_input_button = Button(label="Select lags, then click me!", width=10, button_type="success") # --------------------- PLOTS---------------------------------- columns = [ TableColumn(field="variable_1", title="variable 1"), TableColumn(field="variable_2", title="variable 2"), TableColumn(field="relationship", title="relationship"), TableColumn(field="lag", title="lag(days)"), TableColumn(field="r", title="r"), TableColumn(field="p_value", title="p_value"), ] lags_corr_table = DataTable(source=lags_corr_src, columns=columns, width=500, height=200) hv_matrix_plot = hv.DynamicMap(thistab.matrix_plot, streams=[stream_launch_matrix]) hv_corr_table = hv.DynamicMap(thistab.correlation_table, streams=[stream_launch_corr]) hv_nonpara_table = hv.DynamicMap( thistab.non_parametric_relationship_table, streams=[stream_launch_corr]) # hv_hist_plot = hv.DynamicMap(thistab.hist, streams=[stream_launch_hist]) hv_lags_plot = hv.DynamicMap(thistab.lags_plot, streams=[stream_launch_lags_var]) hv_multiline = hv.DynamicMap(thistab.multiline, streams=[stream_launch]) matrix_plot = renderer.get_plot(hv_matrix_plot) corr_table = renderer.get_plot(hv_corr_table) nonpara_table = renderer.get_plot(hv_nonpara_table) lags_plot = renderer.get_plot(hv_lags_plot) multiline = renderer.get_plot(hv_multiline) # setup divs # handle callbacks variable_select.on_change('value', update_variable) lag_variable_select.on_change('value', update_lag_plot_variable) lag_select.on_change('value', update_lag) # individual lag resample_select.on_change('value', update_resample) pm_gender_select.on_change('value', update_IVs) m_gender_select.on_change('value', update_IVs) t_gender_select.on_change('value', update_IVs) datepicker_start.on_change('value', update) datepicker_end.on_change('value', update) lags_input_button.on_click(update_lags_selected) # lags array status_select.on_change('value', update_IVs) type_select.on_change('value', update_IVs) multiline_x_select.on_change('value', update_multiline) multiline_y_select.on_change('value', update_multiline) # COMPOSE LAYOUT # put the controls in a single element controls_lag = WidgetBox(lags_input, lags_input_button, lag_variable_select) controls_multiline = WidgetBox(multiline_x_select, multiline_y_select) controls_page = WidgetBox(datepicker_start, datepicker_end, variable_select, type_select, status_select, resample_select, pm_gender_select, m_gender_select, t_gender_select) controls_gender = WidgetBox(pm_gender_select, m_gender_select, t_gender_select) # create the dashboards grid = gridplot( [[thistab.notification_div['top']], [Spacer(width=20, height=70)], [thistab.section_headers['relationships']], [Spacer(width=20, height=30)], [matrix_plot.state, controls_page], [thistab.section_headers['correlations']], [Spacer(width=20, height=30)], [corr_table.state, thistab.corr_information_div()], [thistab.title_div('Compare levels in a variable', 400)], [Spacer(width=20, height=30)], [multiline.state, controls_multiline], [thistab.section_headers['lag']], [Spacer(width=20, height=30)], [lags_plot.state, controls_lag], [lags_corr_table], [thistab.notification_div['bottom']]]) # Make a tab with the layout tab = Panel(child=grid, title=panel_title) return tab except Exception: logger.error('EDA projects:', exc_info=True) return tab_error_flag(panel_title)
offset, amplitude = 0, 2 phase = 0 # initialize bokeh figure objects for 'trig' data and 'psd' data props = {'toolbar_location': 'right', 'tools': 'xbox_zoom, undo, redo, reset', 'logo': 'grey', 'width': 500, 'height': 250} trig = figure(x_range=(0, 1), y_range=(-10, 10), x_axis_label='time, sec', **props) psd = figure(title='power spectral density', y_axis_type='log', x_axis_label='frequency, Hz', **props) # initialize glyphs on trig and psd figures as empty data sources trig.circle('x', 'y', source=ColumnDataSource(data={'x': [], 'y': []}), size=1) trig.line('x', 'y', source=ColumnDataSource(data={'x': [], 'y': []}), line_width=2) psd.line('x', 'y', source=ColumnDataSource(data={'x': [], 'y': []}), line_width=3) # create a Select widget controlling the trig function selection, start a list of widgets select = Select(title='Trig Function', options=['sin', 'cos']) select.on_change('value', callback_select) widgets = [widgetbox(select, width=465)] # initialize lists of sliders with inc / dec buttons to control trig attributes controls = [ [Button(label='-'), Slider(title='Sample Rate', value=fs, start=2, end=400, step=1), Button(label='+')], [Button(label='-'), Slider(title='Frequency', value=f, start=1, end=100, step=1), Button(label='+')], [Button(label='-'), Slider(title='Offset', value=offset, start=-10, end=10, step=1), Button(label='+')], [Button(label='-'), Slider(title='Amplitude', value=amplitude, start=0, end=10, step=1), Button(label='+')], [Button(label='-'), Slider(title='Phase', value=phase, start=0, end=6, step=1), Button(label='+')]] # setup callbacks for controls, update the list of widgets for control in controls: tmp = [] for c in control: if isinstance(c, Button):
# set initial values for the dropdown # Selection box criteria intervals = ['Y', 'Q', 'M', 'W', 'D', 'H'] weather_data_options = ['Temperature', 'Humidity', 'Pressure'] statistical_options = ['average', 'standard', 'variance'] # Create the initial source for the plot grouped = get_stat(temperature_df, frequency, statistical_method) source = ColumnDataSource(grouped) # Create list of dropdowns selection_frequency = Select(value=frequency, title='Time Interval', options=intervals) selection_frequency.on_change('value', update_data_set) selection_statistical_method = Select(value=statistical_method, title='Statistical Method', options=statistical_options) selection_statistical_method.on_change('value', update_data_set) selection_data_set = Select(value=weather_data_value, title='Weather Data Set', options=weather_data_options) selection_data_set.on_change('value', update_data_set) # instantiate the plot, the layout, and the tab plot3 = setup_weather_plot(source, frequency, statistical_method) l4 = layout([[ plot3, selection_data_set, selection_frequency,
km = KMeans(n_clusters=k_cluster, random_state=0) km.fit(tweet_vecs) predictions = km.predict(tweet_vecs) colors = get_colors(predictions) km_data.data = dict(colors=colors, x=tsne_vecs[:,0], y=tsne_vecs[:,1], tweet=tweets) s_slider.on_change('value', update_s_clusters) def update_time_window(attrname, old, new): start_m = int(month_select_1.value) start_d = int(day_select_1.value) end_m = int(month_select_2.value) end_d = int(day_select_2.value) print(start_m, start_d, end_m, end_d) update_time_series(start_m, start_d, end_m, end_d) month_select_1.on_change('value', update_time_window) day_select_1.on_change('value', update_time_window) month_select_2.on_change('value', update_time_window) day_select_2.on_change('value', update_time_window) def update_sentiment(time_stamps, tweets): cached = ' Date time || Negative Tweets \n' cached += ' ======================================================================================\n' for time_, tweet_ in zip(time_stamps, tweets): if negSent(tweet_): cached += ' {0} || {1} \n'.format(str(time_), tweet_) cached += "\n\n\n" cached += ' Date time || Positive Tweets \n'
class BokehEventViewerCamera(CameraDisplay): def __init__(self, event_viewer, fig=None): """ A `ctapipe.visualization.bokeh.CameraDisplay` modified to utilise a `ctapipe.core.container.DataContainer` directly. Parameters ---------- event_viewer : BokehEventViewer The BokehEventViewer this object belongs to fig : bokeh.plotting.figure Figure to store the bokeh plot onto (optional) """ self._event = None self._view = 'r0' self._telid = None self._channel = 0 self._time = 0 super().__init__(fig=fig) self._view_options = { 'r0': lambda e, t, c, time: e.r0.tel[t].waveform[c, :, time], 'r1': lambda e, t, c, time: e.r1.tel[t].waveform[c, :, time], 'dl0': lambda e, t, c, time: e.dl0.tel[t].waveform[c, :, time], 'dl1': lambda e, t, c, time: e.dl1.tel[t].image[c, :], 'peakpos': lambda e, t, c, time: e.dl1.tel[t].peakpos[c, :], 'cleaned': lambda e, t, c, time: e.dl1.tel[t].cleaned[c, :, time], } self.w_view = None self._geom_tel = None self.event_viewer = event_viewer def _reset(self): self.reset_pixels() self.event_viewer.change_time(0) def _set_image(self): e = self.event v = self.view t = self.telid c = self.channel time = self.time if not e: self.event_viewer.log.warning("No event has been provided") return tels = list(e.r0.tels_with_data) if t is None: t = tels[0] if t not in tels: raise KeyError(f"Telescope {t} has no data") try: self.image = self._view_options[v](e, t, c, time) self.fig.title.text = f'{v} (T = {time})' except TypeError: self.image = None def _update_geometry(self): e = self.event t = self.telid if e: # Check if geom actually needs to be changed if not t == self._geom_tel: self.geom = e.inst.subarray.tel[t].camera self._geom_tel = t else: self.event_viewer.log.warning("No event has been provided") def refresh(self): self._set_image() @property def event(self): return self._event @event.setter def event(self, val): self._event = val self._update_geometry() self._set_image() def change_event(self, event, telid): if self.event: # Only reset when an event exists self._reset() self._telid = telid self.event = event @property def view(self): return self._view @view.setter def view(self, val): if val not in list(self._view_options.keys()): raise ValueError(f"View is not valid: {val}") self._view = val self._set_image() @property def telid(self): return self._telid @telid.setter def telid(self, val): if self.event: # Only reset when an event exists self._reset() self._telid = val self._update_geometry() self._set_image() @property def channel(self): return self._channel @channel.setter def channel(self, val): self._channel = val self._set_image() @property def time(self): return self._time @time.setter def time(self, val): self._time = int(val) self._set_image() def _on_pixel_click(self, pix_id): super()._on_pixel_click(pix_id) ai = self.active_index self.event_viewer.waveforms[ai].pixel = pix_id def create_view_widget(self): self.w_view = Select(title="View:", value="", options=[], width=5) self.w_view.on_change('value', self.on_view_widget_change) self.layout = column([self.w_view, self.layout]) def update_view_widget(self): self.w_view.options = list(self._view_options.keys()) self.w_view.value = self.view def on_view_widget_change(self, _, __, ___): if self.view != self.w_view.value: self.view = self.w_view.value
createInnerPlot = iP.CreaterInnerPlot() #------------------------------------------------------- # Generate patient selection schema #------------------------------------------------------- def updatePatientCallback(attr, oldFile, newFile): entirePage.children[1] = createInnerPlot(folder, newFile) fileName = newFile return dataSelect = Select(title='Select Data', options=fileNames) dataSelect.on_change('value', updatePatientCallback) def increaseCallback(): selects = iP.getSelects(entirePage.children[1].children[0].children) print(selects) createInnerPlot.toAddText(selects) entirePage.children[1] = createInnerPlot(folder, fileName) return addButton = Button(label='Add Sentence Outer') addButton.on_click(increaseCallback)
class BokehFileViewer(Tool): name = "BokehFileViewer" description = ("Interactively explore an event file using the bokeh " "visualisation package") port = Int(5006, help="Port to open bokeh server onto").tag(config=True) disable_server = Bool(False, help="Do not start the bokeh server " "(useful for testing)").tag(config=True) aliases = Dict( dict( port='BokehFileViewer.port', disable_server='BokehFileViewer.disable_server', r='EventSourceFactory.product', f='EventSourceFactory.input_url', max_events='EventSourceFactory.max_events', ped='CameraR1CalibratorFactory.pedestal_path', tf='CameraR1CalibratorFactory.tf_path', pe='CameraR1CalibratorFactory.pe_path', ff='CameraR1CalibratorFactory.ff_path', extractor='ChargeExtractorFactory.product', extractor_t0='ChargeExtractorFactory.t0', extractor_window_width='ChargeExtractorFactory.window_width', extractor_window_shift='ChargeExtractorFactory.window_shift', extractor_sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG', extractor_sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG', extractor_lwt='ChargeExtractorFactory.lwt', cleaner='WaveformCleanerFactory.product', )) classes = List([ EventSourceFactory, ChargeExtractorFactory, CameraR1CalibratorFactory, CameraDL1Calibrator, WaveformCleanerFactory ]) def __init__(self, **kwargs): super().__init__(**kwargs) self._event = None self._event_index = None self._event_id = None self._telid = None self._channel = None self.w_next_event = None self.w_previous_event = None self.w_event_index = None self.w_event_id = None self.w_goto_event_index = None self.w_goto_event_id = None self.w_telid = None self.w_channel = None self.w_dl1_dict = None self.wb_extractor = None self.layout = None self.reader = None self.seeker = None self.extractor = None self.cleaner = None self.r1 = None self.dl0 = None self.dl1 = None self.viewer = None self._updating_dl1 = False def setup(self): self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]" kwargs = dict(config=self.config, tool=self) default_url = get_dataset_path("gamma_test.simtel.gz") EventSourceFactory.input_url.default_value = default_url self.reader = EventSourceFactory.produce(**kwargs) self.seeker = EventSeeker(self.reader, **kwargs) self.extractor = ChargeExtractorFactory.produce(**kwargs) self.cleaner = WaveformCleanerFactory.produce(**kwargs) self.r1 = CameraR1CalibratorFactory.produce(eventsource=self.reader, **kwargs) self.dl0 = CameraDL0Reducer(**kwargs) self.dl1 = CameraDL1Calibrator(extractor=self.extractor, cleaner=self.cleaner, **kwargs) self.viewer = BokehEventViewer(**kwargs) # Setup widgets self.viewer.create() self.viewer.enable_automatic_index_increment() self.create_previous_event_widget() self.create_next_event_widget() self.create_event_index_widget() self.create_goto_event_index_widget() self.create_event_id_widget() self.create_goto_event_id_widget() self.create_telid_widget() self.create_channel_widget() self.create_dl1_widgets() self.update_dl1_widget_values() # Setup layout self.layout = layout([[self.viewer.layout], [ self.w_previous_event, self.w_next_event, self.w_goto_event_index, self.w_goto_event_id ], [self.w_event_index, self.w_event_id], [self.w_telid, self.w_channel], [self.wb_extractor]]) def start(self): self.event_index = 0 def finish(self): if not self.disable_server: def modify_doc(doc): doc.add_root(self.layout) doc.title = self.name directory = os.path.abspath(os.path.dirname(__file__)) theme_path = os.path.join(directory, "theme.yaml") template_path = os.path.join(directory, "templates") doc.theme = Theme(filename=theme_path) env = jinja2.Environment( loader=jinja2.FileSystemLoader(template_path)) doc.template = env.get_template('index.html') self.log.info('Opening Bokeh application on ' 'http://localhost:{}/'.format(self.port)) server = Server({'/': modify_doc}, num_procs=1, port=self.port) server.start() server.io_loop.add_callback(server.show, "/") server.io_loop.start() @property def event_index(self): return self._event_index @event_index.setter def event_index(self, val): try: self.event = self.seeker[val] except IndexError: self.log.warning("Event Index {} does not exist".format(val)) @property def event_id(self): return self._event_id @event_id.setter def event_id(self, val): try: self.event = self.seeker[str(val)] except IndexError: self.log.warning("Event ID {} does not exist".format(val)) @property def telid(self): return self._telid @telid.setter def telid(self, val): self.channel = 0 tels = list(self.event.r0.tels_with_data) if val not in tels: val = tels[0] self._telid = val self.viewer.telid = val self.update_telid_widget() @property def channel(self): return self._channel @channel.setter def channel(self, val): self._channel = val self.viewer.channel = val self.update_channel_widget() @property def event(self): return self._event @event.setter def event(self, val): # Calibrate self.r1.calibrate(val) self.dl0.reduce(val) self.dl1.calibrate(val) self._event = val self.viewer.event = val self._event_index = val.count self._event_id = val.r0.event_id self.update_event_index_widget() self.update_event_id_widget() self._telid = self.viewer.telid self.update_telid_widget() self._channel = self.viewer.channel self.update_channel_widget() def update_dl1_calibrator(self, extractor=None, cleaner=None): """ Recreate the dl1 calibrator with the specified extractor and cleaner Parameters ---------- extractor : ctapipe.image.charge_extractors.ChargeExtractor cleaner : ctapipe.image.waveform_cleaning.WaveformCleaner """ if extractor is None: extractor = self.dl1.extractor if cleaner is None: cleaner = self.dl1.cleaner self.extractor = extractor self.cleaner = cleaner kwargs = dict(config=self.config, tool=self) self.dl1 = CameraDL1Calibrator(extractor=self.extractor, cleaner=self.cleaner, **kwargs) self.dl1.calibrate(self.event) self.viewer.refresh() def create_next_event_widget(self): self.w_next_event = Button(label=">", button_type="default", width=50) self.w_next_event.on_click(self.on_next_event_widget_click) def on_next_event_widget_click(self): self.event_index += 1 def create_previous_event_widget(self): self.w_previous_event = Button(label="<", button_type="default", width=50) self.w_previous_event.on_click(self.on_previous_event_widget_click) def on_previous_event_widget_click(self): self.event_index -= 1 def create_event_index_widget(self): self.w_event_index = TextInput(title="Event Index:", value='') def update_event_index_widget(self): if self.w_event_index: self.w_event_index.value = str(self.event_index) def create_event_id_widget(self): self.w_event_id = TextInput(title="Event ID:", value='') def update_event_id_widget(self): if self.w_event_id: self.w_event_id.value = str(self.event_id) def create_goto_event_index_widget(self): self.w_goto_event_index = Button(label="GOTO Index", button_type="default", width=100) self.w_goto_event_index.on_click(self.on_goto_event_index_widget_click) def on_goto_event_index_widget_click(self): self.event_index = int(self.w_event_index.value) def create_goto_event_id_widget(self): self.w_goto_event_id = Button(label="GOTO ID", button_type="default", width=70) self.w_goto_event_id.on_click(self.on_goto_event_id_widget_click) def on_goto_event_id_widget_click(self): self.event_id = int(self.w_event_id.value) def create_telid_widget(self): self.w_telid = Select(title="Telescope:", value="", options=[]) self.w_telid.on_change('value', self.on_telid_widget_change) def update_telid_widget(self): if self.w_telid: tels = [str(t) for t in self.event.r0.tels_with_data] self.w_telid.options = tels self.w_telid.value = str(self.telid) def on_telid_widget_change(self, _, __, ___): if self.telid != int(self.w_telid.value): self.telid = int(self.w_telid.value) def create_channel_widget(self): self.w_channel = Select(title="Channel:", value="", options=[]) self.w_channel.on_change('value', self.on_channel_widget_change) def update_channel_widget(self): if self.w_channel: try: n_chan = self.event.r0.tel[self.telid].waveform.shape[0] except AttributeError: n_chan = 1 channels = [str(c) for c in range(n_chan)] self.w_channel.options = channels self.w_channel.value = str(self.channel) def on_channel_widget_change(self, _, __, ___): if self.channel != int(self.w_channel.value): self.channel = int(self.w_channel.value) def create_dl1_widgets(self): self.w_dl1_dict = dict( cleaner=Select(title="Cleaner:", value='', width=5, options=WaveformCleanerFactory.subclass_names), extractor=Select(title="Extractor:", value='', width=5, options=ChargeExtractorFactory.subclass_names), extractor_t0=TextInput(title="T0:", value=''), extractor_window_width=TextInput(title="Window Width:", value=''), extractor_window_shift=TextInput(title="Window Shift:", value=''), extractor_sig_amp_cut_HG=TextInput(title="Significant Amplitude " "Cut (HG):", value=''), extractor_sig_amp_cut_LG=TextInput(title="Significant Amplitude " "Cut (LG):", value=''), extractor_lwt=TextInput(title="Local Pixel Weight:", value='')) for val in self.w_dl1_dict.values(): val.on_change('value', self.on_dl1_widget_change) self.wb_extractor = widgetbox( PreText(text="Charge Extractor Configuration"), self.w_dl1_dict['cleaner'], self.w_dl1_dict['extractor'], self.w_dl1_dict['extractor_t0'], self.w_dl1_dict['extractor_window_width'], self.w_dl1_dict['extractor_window_shift'], self.w_dl1_dict['extractor_sig_amp_cut_HG'], self.w_dl1_dict['extractor_sig_amp_cut_LG'], self.w_dl1_dict['extractor_lwt']) def update_dl1_widget_values(self): if self.w_dl1_dict: for key, val in self.w_dl1_dict.items(): if 'extractor' in key: if key == 'extractor': val.value = self.extractor.__class__.__name__ else: key = key.replace("extractor_", "") try: val.value = str(getattr(self.extractor, key)) except AttributeError: val.value = '' elif 'cleaner' in key: if key == 'cleaner': val.value = self.cleaner.__class__.__name__ else: key = key.replace("cleaner_", "") try: val.value = str(getattr(self.cleaner, key)) except AttributeError: val.value = '' def on_dl1_widget_change(self, _, __, ___): if self.event: if not self._updating_dl1: self._updating_dl1 = True cmdline = [] for key, val in self.w_dl1_dict.items(): if val.value: cmdline.append('--{}'.format(key)) cmdline.append(val.value) self.parse_command_line(cmdline) kwargs = dict(config=self.config, tool=self) extractor = ChargeExtractorFactory.produce(**kwargs) cleaner = WaveformCleanerFactory.produce(**kwargs) self.update_dl1_calibrator(extractor, cleaner) self.update_dl1_widget_values() self._updating_dl1 = False
df = pd.read_sql_query(q.statement, con=db.connection()) # Both Micrograph and Sample tables have an 'id' field... # loading the whole dataset into a pandas df yields two 'id' columns # drop the id field that results from Micrograph.sample.id # df = df.T.groupby(level=0).last().T df = df.replace(np.nan, -9999) # bokeh (because json) can't deal with NaN values # convert times to minutes, in place df.ix[df.anneal_time_unit=='H', 'anneal_time'] *= 60 # set default form data to draw the default plot default_representation = 'vgg16_block5_conv3-vlad-32.h5' representations = list(map(os.path.basename, glob.glob('static/embed/*.h5'))) representation = Select(title='Representation', value=default_representation, options=representations) representation.on_change('value', update_map_points) manifold_methods = ['PCA', 't-SNE', 'MDS', 'LLE', 'Isomap', 'SpectralEmbedding'] manifold = Select(title='Manifold method', value='t-SNE', options=manifold_methods) manifold.on_change('value', update_map_points) markersize = Select( title='Marker size', value='None', options=['None', 'anneal_temperature', 'anneal_time'] ) markersize.on_change('value', update_markersize) markercolor = Select( title='Marker color', value='primary microconstituent',
cities = { 'Austin': { 'airport': 'AUS', 'title': 'Austin, TX', }, 'Boston': { 'airport': 'BOS', 'title': 'Boston, MA', }, 'Seattle': { 'airport': 'SEA', 'title': 'Seattle, WA', } } city_select = Select(value=city, title='City', options=sorted(cities.keys())) distribution_select = Select(value=distribution, title='Distribution', options=['Discrete', 'Smooth']) df = pd.read_csv(join(dirname(__file__), 'data/2015_weather.csv')) source = get_dataset(df, cities[city]['airport'], distribution) plot = make_plot(source, cities[city]['title']) city_select.on_change('value', update_plot) distribution_select.on_change('value', update_plot) controls = VBox(city_select, distribution_select) # add to document curdoc().add_root(HBox(controls, plot))
def candlestick_plot(): def obv_indicator(data): res = talib.OBV(data.close.values, data.volume.values) return res def rsi_indicator(data): res = talib.RSI(data.close.values, timeperiod=14) return res def cci_indicator(data): res = talib.CCI(data.high.values, data.low.values, data.close.values, timeperiod=14) return res def technical_indicator(data, indicator): if indicator == 'CCI': data['tech'] = cci_indicator(data) elif indicator == 'RSI': data['tech'] = rsi_indicator(data) else: data['tech'] = obv_indicator(data) return data def load_data(obid, start, end, freq='1d'): print('running....') data = get_price(obid, start, end, freqency=freq).reset_index() data['pct_change'] = data['close'].pct_change() # data.dropna(inplace=True) data['pct_change'] = data['pct_change'].apply(lambda x: str(round(x * 100, 2)) + '%') data['index'] = list(np.arange(len(data))) data['date'] = data['date'].apply(lambda x: x.strftime("%Y%m%d")) return data def moving_average(data, selection): selection_mapping = {k: int(k.split('_')[-1]) for k in selection} for k, v in selection_mapping.items(): data[k] = data['close'].rolling(window=v).mean() return data def update_lines(attr, old, new): line_0.visible = 0 in average_selection.active line_1.visible = 1 in average_selection.active line_2.visible = 2 in average_selection.active line_3.visible = 3 in average_selection.active line_4.visible = 4 in average_selection.active line_5.visible = 5 in average_selection.active def update_plot(attr, old, new): indicator = indicator_selection.value new_data = technical_indicator(data, indicator) new_source = ColumnDataSource(new_data) source.data.update(new_source.data) def update_data(): # global obid, start, end obid = order_book_id.value start = start_date.value end = end_date.value # 提取数据,均线根据选取与否进行添加 new_data = load_data(obid, start, end) new_data_1 = moving_average(new_data, average_labels) new_data_2 = technical_indicator(new_data, indicator_selection.value) new_source = ColumnDataSource(new_data_2) new_source_1 = ColumnDataSource(new_data_1) source.data.update(new_source.data) source_1.data.update(new_source_1.data) inc = new_data.close >= new_data.open dec = new_data.close < new_data.open inc_source.data = inc_source.from_df(new_data_2.loc[inc]) dec_source.data = dec_source.from_df(new_data_2.loc[dec]) p.title.text = instruments(obid).symbol p.x_range.end = len(new_data) + 1 p2.xaxis.major_label_overrides = {i: date for i, date in enumerate(new_data['date'])} today = datetime.now().date() average_labels = ["MA_5", "MA_10", "MA_20", 'MA_30', 'MA_60', 'MA_120'] average_selection = CheckboxGroup(labels=average_labels, active=[0, 1, 2, 3, 4, 5, 6]) indicator_selection = Select(title='TechnicalIndicator', value='RSI', options=['OBV', 'RSI', 'CCI']) order_book_id = TextInput(title='StockCode', value='002916.XSHE') symbol = instruments(order_book_id.value).symbol start_date = DatePicker(title="StartDate", value='2018-01-01', min_date='2015-01-01', max_date=today) end_date = DatePicker(title="EndDate", value=today, min_date=start_date.value, max_date=today) # labels = [average_selection.labels[i] for i in average_selection.active] data = load_data(order_book_id.value, start_date.value, end_date.value) # 均线计算 data_1 = moving_average(data, average_labels) # 计算各种长度的均线 # 技术指标计算 data_2 = technical_indicator(data, indicator_selection.value) source = ColumnDataSource(data_2) source_1 = ColumnDataSource(data_1) inc = data.close >= data.open dec = data.open > data.close inc_source = ColumnDataSource(data_2.loc[inc]) dec_source = ColumnDataSource(data_2.loc[dec]) TOOLS = 'save, pan, box_zoom, reset, wheel_zoom' hover = HoverTool(tooltips=[('date', '@date'), ('open', '@open'), ('high', '@high'), ('low', '@low'), ('close', '@close'), ('pct_change', "@pct_change") ] ) length = len(data) p = figure(plot_width=1000, plot_height=500, title='{}'.format(symbol), tools=TOOLS, x_range=(0, length + 1)) p.xaxis.visible = False # 隐藏x-axis p.min_border_bottom = 0 # 均线图 line_0 = p.line(x='index', y='MA_5', source=source_1, color=Spectral6[5]) line_1 = p.line(x='index', y='MA_10', source=source_1, color=Spectral6[4]) line_2 = p.line(x='index', y='MA_20', source=source_1, color=Spectral6[3]) line_3 = p.line(x='index', y='MA_30', source=source_1, color=Spectral6[2]) line_4 = p.line(x='index', y='MA_60', source=source_1, color=Spectral6[1]) line_5 = p.line(x='index', y='MA_120', source=source_1, color=Spectral6[0]) p.segment(x0='index', y0='high', x1='index', y1='low', color='red', source=inc_source) p.segment(x0='index', y0='high', x1='index', y1='low', color='green', source=dec_source) p.vbar('index', 0.5, 'open', 'close', fill_color='red', line_color='red', source=inc_source, hover_fill_alpha=0.5) p.vbar('index', 0.5, 'open', 'close', fill_color='green', line_color='green', source=dec_source, hover_fill_alpha=0.5) p.add_tools(hover) p1 = figure(plot_width=p.plot_width, plot_height=200, x_range=p.x_range, toolbar_location=None) p1.vbar('index', 0.5, 0, 'volume', color='red', source=inc_source) p1.vbar('index', 0.5, 0, 'volume', color='green', source=dec_source) p1.xaxis.visible = False p2 = figure(plot_width=p.plot_width, plot_height=p1.plot_height, x_range=p.x_range, toolbar_location=None) p2.line(x='index', y='tech', source=source) p2.xaxis.major_label_overrides = {i: date for i, date in enumerate(data['date'])} p2.xaxis.major_label_orientation = pi / 4 p2.min_border_bottom = 0 button = Button(label="ClickToChange", button_type="success") button.on_click(update_data) average_selection.inline = True average_selection.width = 500 average_selection.on_change('active', update_lines) indicator_selection.on_change('value', update_plot) widgets = column(row(order_book_id, start_date, end_date, button), row(indicator_selection, average_selection)) layouts = column(widgets, p, p1, p2) # doc.add_root(pp) # make a layout tab = Panel(child=layouts, title='StockPrice') return tab
label = "%s vs %s" % (x.value.title(), y.value.title()) kdims = [x.value, y.value] opts, style = {}, {} opts['color_index'] = color.value if color.value != 'None' else None if size.value != 'None': opts['size_index'] = size.value opts['scaling_factor'] = (1./df[size.value].max())*200 points = hv.Points(df, kdims=kdims, label=label).opts(plot=opts, style=style) return renderer.get_plot(points).state def update(attr, old, new): layout.children[1] = create_figure() x = Select(title='X-Axis', value='mpg', options=quantileable) x.on_change('value', update) y = Select(title='Y-Axis', value='hp', options=quantileable) y.on_change('value', update) size = Select(title='Size', value='None', options=['None'] + quantileable) size.on_change('value', update) color = Select(title='Color', value='None', options=['None'] + quantileable) color.on_change('value', update) controls = widgetbox([x, y, color, size], width=200) layout = row(controls, create_figure()) curdoc().add_root(layout) curdoc().title = "Crossfilter"
def update_plot(attrname, old, new): src = download_data(cities[city_select.value]['ID'], startYear, endYear, baseTemp) source.data.update(src.data) # source.data.update(cityData[city_select.value].data) plot.title = city_select.value city = 'St. John\'s' startYear = 2015 endYear = 2015 baseTemp=10 cities = { 'Calgary': {'ID':50430}, 'Montreal' : {'ID':51157}, 'St. John\'s' : {'ID':50089} } city_select = Select(value=city, title='City:', options=list(cities.keys())) for c in cities.keys(): cityData[c] = download_data(cities[city]['ID'], startYear, endYear, baseTemp) source = cityData[city] plot = make_plot(source, city) city_select.on_change('value', update_plot) # add to document curdoc().add_root(HBox(city_select, plot))