def plotData(target, popsize, cylList, step, m): for s in range(max_length): last_entry = Entry(None, None, None, None) entries.append(last_entry) source = AjaxDataSource(data_url='http://*****:*****@cylStr"), ("Energy", "@y"), ("Generation", "@x"), ] ) p = figure(tools=[hover], x_range=(0,step)) p.plot_width=500 p.plot_height=400 p.line('x', 'y', 'cylStr', source=source) p.xaxis.axis_label = 'Generations' p.yaxis.axis_label = 'Best energy [eV/atom]' t = Thread(target=gen_entry, args=(target, popsize, cylList, step, m,)) t.daemon = True t.start() show(p)
def plot(self): plot = figure() plot.quad(top=self.freqs[:, 1], left=self.freqs[:, 1], bottom=np.zeros(len(self.freqs[:, 1])), right=self.freqs[:, 1], x_range=list(self.freqs[:, 0])) plot.xaxis().major_label_orientation = np.pi / 3 show(plot)
def __init__(self, document, channels, open_browser=False, start_server=False, server_url=None, **kwargs): if not BOKEH_AVAILABLE: raise ImportError if server_url is None: server_url = config.bokeh_server self.plots = {} self.start_server = start_server self.document = document self.server_url = server_url self._startserver() # Create figures for each group of channels self.p = [] self.p_indices = {} for i, channel_set in enumerate(channels): channel_set_opts = {} if isinstance(channel_set, dict): channel_set_opts = channel_set channel_set = channel_set_opts.pop("channels") channel_set_opts.setdefault("title", "{} #{}".format(document, i + 1)) channel_set_opts.setdefault("x_axis_label", "iterations") channel_set_opts.setdefault("y_axis_label", "value") self.p.append(figure(**channel_set_opts)) for channel in channel_set: self.p_indices[channel] = i if open_browser: show() kwargs.setdefault("after_epoch", True) kwargs.setdefault("before_first_epoch", True) super(Plot, self).__init__(**kwargs)
def main(sa): rf_pickle_name = sa[0] chem_dict_filename = sa[1] try: rf = pickle.load(open(rf_pickle_name, "rb")) except IOError as e: sys.stderr.write("IOError: " + str(e) + "\nError in main()\n") sys.exit() drug_list = FileHandler.WikiScrapedDB(chem_dict_filename).get_drug_list() drug_list = build_prediction_result_array(rf, drug_list) p1 = plot_scatter([drug.get_rf_prediction_prob() for drug in drug_list], [drug.get_side_effect_count() for drug in drug_list], "Chemical promiscuity vs. # side effects", "Predicted probability of PAINS classification", "Number of side effects") p2 = plot_scatter([drug.get_rf_prediction_prob() for drug in drug_list], [drug.get_gene_effect_count() for drug in drug_list], "Chemical promiscuity vs. # gene expression changes", "Predicted probability of PAINS classification", "Number of identified gene expression effects") results = polyfit([drug.get_rf_prediction_prob() for drug in drug_list], [drug.get_side_effect_count() for drug in drug_list], 1) print results output_file("lregress.html") show(HBox(p1, p2))
def main(sa): pains_filename = sa[0] fp_list = FileHandler.SlnFile(pains_filename).get_fingerprint_list() clusters = clust(fp_list) p = pca_plot(fp_list, clusters) output_file("PCA_w_hclust.html") show(p)
def create_figure(): xs = df[x.value].values ys = df[y.value].values x_title = x.value.title() y_title = y.value.title() kw = dict() if x.value in discrete: kw['x_range'] = sorted(set(xs)) if y.value in discrete: kw['y_range'] = sorted(set(ys)) kw['title'] = "%s vs %s" % (x_title, y_title) p = figure(plot_height=600, plot_width=800, tools='pan,box_zoom,reset', **kw) p.xaxis.axis_label = x_title p.yaxis.axis_label = y_title if x.value in discrete: p.xaxis.major_label_orientation = pd.np.pi / 4 sz = 9 if size.value != 'None': groups = pd.qcut(df[size.value].values, len(SIZES)) sz = [SIZES[xx] for xx in groups.codes] c = "#31AADE" if color.value != 'None': groups = pd.qcut(df[color.value].values, len(COLORS)) c = [COLORS[xx] for xx in groups.codes] p.circle(x=xs, y=ys, color=c, size=sz, line_color="white", alpha=0.6, hover_color='white', hover_alpha=0.5) show(p) return p
def bokeh2(X): Y = de_mean_matrix(X) TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,previewsave,box_select" hover = HoverTool( tooltips=[ ("index", "$index"), ("(x,y)", "($x, $y)"), ("desc", "@desc"), ] ) bplt.output_file("data.html", title="Rescaling") s1 = bplt.figure(width=500, plot_height=250, title="Data", tools=[hover, TOOLS]) s1.scatter(X[:,0].A1, X[:,1].A1, marker="circle", line_color="#6666ee", fill_color="#ee6666", fill_alpha=0.5, size=12) s2 = bplt.figure(width=500, plot_height=250, title="De-meaned", tools=TOOLS) s2.scatter(Y[:,0].A1, Y[:,1].A1, marker="circle", line_color="#6666ee", fill_color="#ee6666", fill_alpha=0.5, size=12) # put all the plots in a VBox p = bplt.vplot(s1, s2) # show the results bplt.show(p)
def main(): xs = np.linspace(-np.pi, np.pi, 100, endpoint=True) xs = np.linspace(0, 4*np.pi, 100) ys_exp = np.exp(xs) ys_sin = np.sin(xs) ys_cos = np.sin(xs) ys_tan = np.tan(xs) output_file("grid_example.html") fig1 = figure(width=250, plot_height=250, title=None) fig1.circle(xs, ys_exp, size=10, color="navy", alpha=0.5) fig2 = figure(width=250, plot_height=250, x_range=fig1.x_range, title=None) fig2.triangle(xs, ys_sin, size=10, color="firebrick", alpha=0.5) fig3 = figure(width=250, height=250, x_range=fig2.x_range, y_range=fig2.y_range, title=None) fig3.square(xs, ys_cos, color="olive") fig4 = figure(width=250, height=250, title=None) fig4.line(xs, ys_tan, color="green") show(gridplot([[fig1, fig2], [fig3, fig4]]))
def plot(size, text, free): print text print size print free data = { 'left': [], 'right': [], 'text': [], 'color': [], 'text_center': [] } left = 0 for i in xrange(0, len(size)): data['left'].append(left) data['right'].append(left + size[i] * graph_width) left = data['right'][i] if free[i] is 1: data['color'].append("Green") else: data['color'].append("Yellow") data['text'].append(text[i]) data['text_center'].append((data['right'][i] - data['left'][i])/2 + data['left'][i]) print data['text'] output_file("test.html") plot = figure(width=graph_width, height=300) plot.quad(top=1, bottom=0, left=data['left'], right=data['right'], color=data['color'], name="hello", line_color="black") plot.text(x=data['text_center'], y=0.5, angle=math.pi/2, text=data['text'], text_align="center") show(plot)
def plot_k(df): i_src = ColumnDataSource(df[df.open < df.close]) d_src = ColumnDataSource(df[df.open >= df.close]) w = 16*60*60*1000 # half day in ms TOOLS = "pan,xwheel_zoom,ywheel_zoom,box_zoom,reset,save" p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1500, plot_height=640, title = "MSFT Candlestick") # hei 880 p.toolbar.active_scroll = "auto" p.xaxis.major_label_orientation = pi/4 p.grid.grid_line_alpha=0.3 p.background_fill_color = "black" p.segment('date', 'high', 'date', 'low', source=i_src, color="red") p.segment('date', 'high', 'date', 'low', source=d_src, color="green") p.vbar('date', w, 'open', 'close', source=i_src, name="kline", fill_color="red", line_color="red") p.vbar('date', w, 'open', 'close', source=d_src, name="kline", fill_color="green", line_color="green") p.add_tools(HoverTool(tooltips= [("date","@ToolTipDates"), ("close","@close{0,0.00}"), ("high","@high{0,0.00}"), ("low","@low{0,0.00}")], names= ["kline",], )) p.add_tools(CrosshairTool(line_color='grey')) inc_process(df, p) output_file("candlestick.html", title="candlestick.py example", mode='inline') gridplot() show(p) # open a browser
def main(): """ create empty binary tree Mobile make a balanced mobile out of it """ max_depth = 3 prob_child = .75 mobile1 = Mobile(max_depth, prob_child) mobile1.print_mobile() print x = 20 y = 30 stringLength = 5 mass = 10.0 armLength = 12.0 armLength_L = 6 mobile1.build_balanced_mobile_top_down(x, y, stringLength, mass, armLength, armLength_L) mobile1.print_mobile() output_file("test.html") # create a new plot with a title and axis labels s1 = figure(title="The Mobile - " + " Mass: " + str(mass) + ", Depth: " + str(mobile1.depth), x_axis_label='x', y_axis_label='y', title_text_font_size='14pt', plot_width=700) s1.xgrid.grid_line_color = None s1.ygrid.grid_line_color = None # draw the mobile mobile1.draw_mobile(s1) show(s1)
def __init__(self, document, channels, open_browser=False, start_server=False, server_url=None, **kwargs): if not BOKEH_AVAILABLE: raise ImportError if server_url is None: server_url = config.bokeh_server self.plots = {} self.start_server = start_server self.document = document self.server_url = server_url self._startserver() # Create figures for each group of channels self.p = [] self.p_indices = {} for i, channel_set in enumerate(channels): self.p.append(figure(title='{} #{}'.format(document, i + 1))) for channel in channel_set: self.p_indices[channel] = i if open_browser: show() kwargs.setdefault('after_epoch', True) kwargs.setdefault("before_first_epoch", True) super(Plot, self).__init__(**kwargs)
def bokeh_plot(df): tooltip = """ <div> <div> <img src="@image_files" height="60" alt="image" style="float: left; margin: 0px 15px 15px 0px; image-rendering: pixelated;" border="2" ></img> </div> <div> <span style="font-size: 17px;">@source_filenames</span> </div> </div> """ filenames = b64_image_files(df['images']) df['image_files'] = filenames colors_raw = cm.viridis((df['time'] - df['time'].min()) / (df['time'].max() - df['time'].min()), bytes=True) colors_str = ['#%02x%02x%02x' % tuple(c[:3]) for c in colors_raw] df['color'] = colors_str source = ColumnDataSource(df) bplot.output_file('plot.html') hover0 = HoverTool(tooltips=tooltip) hover1 = HoverTool(tooltips=tooltip) tools0 = [t() for t in TOOLS] + [hover0] tools1 = [t() for t in TOOLS] + [hover1] pca = bplot.figure(tools=tools0) pca.circle('PC1', 'PC2', color='color', source=source) tsne = bplot.figure(tools=tools1) tsne.circle('tSNE-0', 'tSNE-1', color='color', source=source) p = bplot.gridplot([[pca, tsne]]) bplot.show(p)
def choropleth_usa_states(scores, filename, title='USA States Choropleth'): """ Plot choropleth of US states based on a score for each state """ states = us_states.data.copy() del states['HI'] del states['AK'] state_xs = [states[code]['lons'] for code in states] state_ys = [states[code]['lats'] for code in states] state_colors = [] for state in states: try: ind = 4 - min(int(scores[state.lower()]*4), 4) state_colors.append(PuRd5[ind]) except KeyError: state_colors.append('white') p = figure(title=title, toolbar_location='left', plot_width=1100, plot_height=700) p.patches(state_xs, state_ys, fill_color=state_colors, fill_alpha=0.7, line_color='#884444', line_width=2) output_file(filename, title=title) show(p) return
def plot_plane(orb, plane='XY', show_steps=True, show_plot=True, width=500, height=500): r = orb.s0.body.mean_radius.value x, y, z = np.array(orb.rx), np.array(orb.ry), np.array(orb.rz) unit = orb.s0.r.unit x_label, y_label = plane[0], plane[1] if plane == 'XY': x, y, z = x, y, z x0 = orb.s0.r[0].value y0 = orb.s0.r[1].value if orb.interpolate: xs = orb._states[0,:] ys = orb._states[1,:] elif plane == 'XZ': x, y, z = x, z, y x0 = orb.s0.r[0].value y0 = orb.s0.r[2].value if orb.interpolate: xs = orb._states[0,:] ys = orb._states[2,:] elif plane == 'YZ': x, y, z = y, z, x x0 = orb.s0.r[1].value y0 = orb.s0.r[2].value if orb.interpolate: xs = orb._states[1,:] ys = orb._states[2,:] magnitudes = np.sqrt(np.square(x)+np.square(y)) limit = np.maximum(r, magnitudes.max()) * 1.2 f = figure( height = height, width = width, title = plane, x_range = (-limit, limit), y_range = (-limit, limit), x_axis_label = "{} [{}]".format(x_label, unit), y_axis_label = "{} [{}]".format(y_label, unit), ) ind = (magnitudes < r) & (z < 0) nan = float('nan') x_bg = x.copy() y_bg = y.copy() x_fg = x.copy() y_fg = y.copy() x_bg[~ind] = nan y_bg[~ind] = nan x_fg[ind] = nan y_fg[ind] = nan f.circle(x=0, y=0, radius=r, alpha=0.5) f.line(x_fg, y_fg, line_width=2, color='blue') f.circle(x_bg, y_bg, size=2, color='darkblue') if orb.interpolate and show_steps: f.cross(x=xs[1:-1], y=ys[1:-1], size=15, line_width=3, color='darkblue') f.cross(x=x0, y=y0, size=15, line_width=3, color='red') if orb.interpolate: f.x(x=x[-1], y=y[-1], size=12, line_width=3, color='red') if show_plot: show(f) else: return f
def shows(self, lines, baseday='2015-01-01'): (lineX, lineY) = self.getXY(lines, baseday) df = self.__data df = self.__data[self.__data.Date > baseday] df["Date"] = pd.to_datetime(df["Date"]) mids = (df.Open + df.Close) / 2 spans = abs(df.Close-df.Open) inc = df.Close >= df.Open dec = df.Close < df.Open w = 12*60*60*1000 TOOLS = "pan,wheel_zoom,box_zoom,hover,crosshair,reset,save" p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, toolbar_location="left") hover = HoverTool() p.add_tools(hover) p.title = "MSFT Candlestick" p.xaxis.major_label_orientation = pi/4 p.grid.grid_line_alpha = 0.5 p.segment(df.Date[inc], df.High[inc], df.Date[inc], df.Low[inc], color="red") p.rect(df.Date[inc], mids[inc], w, spans[inc], fill_color="red", line_color="red") p.segment(df.Date[dec], df.High[dec], df.Date[dec], df.Low[dec], color="green") p.rect(df.Date[dec], mids[dec], w, spans[dec], fill_color="green", line_color="green") p.line(lineX, lineY, line_width=2, color='navy') show(p)
def plot(self, output_file="termite.html"): t = blz.Data(self.input_file) df = pd.read_csv(self.input_file) MAX = blz.compute(t.weight.max()) MIN = blz.compute(t.weight.min()) # Create a size variable to define the size of the the circle for the plot. t = blz.transform(t, size=blz.sqrt((t.weight - MIN)/(MAX - MIN))*50) WORDS = t['word'].distinct() WORDS = into(list, WORDS) topics = t['topic'].distinct() topics = into(list, topics) # Convert topics to strings TOPICS = [str(i) for i in topics] source = into(pd.DataFrame, t) plt.output_file(output_file) data_source = ColumnDataSource(source) p = plt.figure(x_range=TOPICS, y_range=WORDS, plot_width=1000, plot_height=1700, title=self.title) p.circle(x="topic", y="word", size="size", fill_alpha=0.6, source=data_source) #p.xaxis().major_label_orientation = np.pi/3 logging.info("generating termite plot for file %s" % self.input_file) plt.show(p)
def analyze_amendement(): api = NDApi() df = pandas.DataFrame(api.synthese()) colormap = { u'UMP': '#0000CD', u'ECOLO': '#32CD32', u'SRC': '#FF69B4', u'NI': 'grey', u'GDR': '#DC143C', u'UDI': '#87CEFA', u'RRDP': '#FFFF00', } df['colors'] = df.groupe_sigle.map(lambda x: colormap[x]) print df.describe() output_file('amendement_analysis.html', title=u'Analyse amendement') p = figure(title=u'Amendement') p.xaxis.axis_label = u'Signé' p.yaxis.axis_label = u'Adopté' p.scatter(df.amendements_signes, df.amendements_adoptes, color=df.colors, fill_alpha=0.6, size=10) show(p)
def spectra_ranges(qsos): x, y = [[] for i in range(8)], [[] for i in range(8)] tab = [] color = ["indigo", "green", "red", "cyan", "orange", "yellow", "black", "pink"] for i, qso in enumerate(qsos): y1 = first_non_masked(qso.wlen) y2 = first_non_masked(qso.wlen[::-1]) z = int(np.rint(qso.z)) x[z].append([i, i]) y[z].append([y1, y2]) for z, coordinates in enumerate(x): if coordinates: title = "Spectrum ranges for Quasars with z~" + str(z) p = figure(plot_width=975, plot_height=300, y_range=(0, 8000), title=title) p.multi_line(x[z], y[z], color=color[z]) p.xaxis.axis_label = "nth QSO" p.xaxis.axis_label_text_font_size = "10pt" p.yaxis.axis_label = "Wavelength(A)" p.yaxis.axis_label_text_font_size = "10pt" p.yaxis.axis_label_standoff = 15 title = "z~" + str(z) tab.append(Panel(child=p, title=title)) tabs = Tabs(tabs=tab) show(tabs)
def plot_2_flux_variability_analysis_bokeh(fva_result1, fva_result2, grid=None, width=None, height=None, title=None, axis_font_size=None, color1="blue", color2="orange"): factors = list(fva_result1.index) left1 = list(fva_result1.upper_bound) right1 = list(fva_result1.lower_bound) top1 = [i for i in range(0, len(factors))] bottom1 = [i + 0.5 for i in range(0, len(factors))] left2 = list(fva_result2.upper_bound) right2 = list(fva_result2.lower_bound) top2 = [i for i in range(0, len(factors))] bottom2 = [i - 0.5 for i in range(0, len(factors))] x_range = [min([min(bottom1), min(bottom2)]) - 5, max([max(top1), max(top2)]) + 5] title = "Comparing Flux Variability Results" if title is None else title p = _figure(title, width, height, x_range=x_range, y_range=factors) p.quad(top=top1, bottom=bottom1, left=left1, right=right1, color=color1) p.quad(top=top2, bottom=bottom2, left=left2, right=right2, color=color2) if axis_font_size is not None: p.xaxis.axis_label_text_font_size = axis_font_size p.yaxis.axis_label_text_font_size = axis_font_size if grid is not None: grid.append(p) else: plotting.show(p)
def main(): xs=np.linspace(-5,5,num=100,endpoint=True) ys=xs**2 output_file("example_with_option.html") p=figure(title="quadratic curve",x_axis_label="x",y_axis_label="y") p.line(xs,ys,legend="y=x^2") show(p)
def create_report_v2(energy=None, applicator=None, ssd=None, filepath=None, inverted_factor=False): width, length, eqPonA, factor, label = pull_data( energy=energy, applicator=applicator, ssd=ssd, return_label=True) if inverted_factor: factor = 1/factor tag = "!!INVERTED_FACTOR!!" else: tag = "" number_of_measurements = len(width) if number_of_measurements < 8: sufficient = False else: sufficient = True if filepath is None: filepath = ( "interactive_reports/" + str(energy) + "MeV_" + str(applicator) + "app_" + str(ssd) + "ssd_" + str(number_of_measurements) + "datapoints" + tag + ".html") output_file(filepath) if sufficient: result = interactive_v2(width, length, eqPonA, factor, label) show(result)
def values(generations): generation_indexes = range(len(generations)) fig = figure() fig.xaxis.axis_label = 'generation' maxes = [max([value for game, value in generation]) for generation in generations] mins = [min([value for game, value in generation]) for generation in generations] averages = [sum([value for game, value in generation]) / len(generation) for generation in generations] value_groups = ( ('max value', 'green', maxes), ('min value', 'red', mins), ('average value', 'blue', averages), ) for name, color, values in value_groups: fig.line(generation_indexes, values, color=color, legend=name) output_file("values.html") show(fig)
def bokeh_plot(self): import os from bokeh.plotting import line from bokeh.plotting import hold, figure, show, output_file import numpy as np import bokeh.embed as embed figure() hold() for i in self.SensorDict: line( self.SensorDict[i]['dates'], self.SensorDict[i]['values'] ) print(len(self.SensorDict[i]['dates'])) #print(self.SensorDict[i]['dates'][0]) #print(self.SensorDict[i]['values'][0]) print('tjo') os.chdir('..') os.chdir('..') output_file('plot.html', title='Plot22') show()
def main(): xs=list(range(-5,5+1,1)) ys=list(map(lambda x:x**2,xs)) output_file("minimum_example.html") p=figure() p.line(xs,ys) show(p)
def plot(self, output_file="termite.html"): import blaze as blz from odo import into import pandas as pd import bokeh.plotting as plt from bokeh.models.sources import ColumnDataSource t = blz.Data(self.input_file) MAX = blz.compute(t.weight.max()) MIN = blz.compute(t.weight.min()) # Create a size variable to define the size of the the circle for the plot. t = blz.transform(t, size=blz.sqrt((t.weight - MIN)/(MAX - MIN))*50) WORDS = t['word'].distinct() WORDS = into(list, WORDS) topics = t['topic'].distinct() topics = into(list, topics) # Convert topics to strings TOPICS = [str(i) for i in topics] source = into(pd.DataFrame, t) plt.output_file(output_file) data_source = ColumnDataSource(source) p = plt.figure(x_range=TOPICS, y_range=WORDS, plot_width=1000, plot_height=1700, title=self.title) p.circle(x="topic", y="word", size="size", fill_alpha=0.6, source=data_source) plt.show(p)
def render(xmin, xmax, ymin, ymax, numDisplayed): start_time = time.time() (xpoints, ypoints) = RDDDB.crossfilter(xmin, xmax, ymin, ymax, numDisplayed) x = np.random.random(size=numDisplayed) * 100 y = np.random.random(size=numDisplayed) * 100 displayArea = (xmax - xmin) * (ymax - ymin) radii = math.sqrt(.1 * displayArea/600) print "Radii size = " + str(radii) colors = ["#%02x%02x%02x" % (r, g, 100) for r, g in zip(np.floor(50 + 2 * x), np.floor(30 + 2 * y))] # output to static HTML file (with CDN resources) # output_file("color_scatter.html", title="color_scatter.py example", mode="cdn") TOOLS = "resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select" # set output to notebook widget environment output_notebook() # create a new plot with the tools above, and explicit ranges p = figure(tools=TOOLS, x_range=(xmin, xmax), y_range=(ymin, ymax)) # add a circle renderer with vecorized colors and sizes p.circle(xpoints, ypoints, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None) end_time = time.time() print "Time taken to render : " + str(end_time - start_time) show(p)
def notebook_plot(x_key, y_key, **kwargs): r"""Live plot log entries in the current notebook. Parameters ---------- x_key : str The key in the serialized JSON object that contains the x-axis value. y_key : str See `x_key`. \*\*kwargs Connection parameters passed to the `connect` function. """ subscriber, sequence, x, y = connect(x_key, y_key, **kwargs) session = push_session(curdoc()) output_notebook() fig = figure() plot = fig.line(x, y) show(fig) def notebook_update(): update(x_key, y_key, sequence, subscriber, plot) push_notebook() curdoc().add_periodic_callback(notebook_update, 50) session.loop_until_closed()
def plot(self, thresh): # Source the lists defined earlier (which will contain values once one of the # above methods is called. This step is necessary for the functionality of the HoverTool created later. source1 = ColumnDataSource(data=dict(ggx=gx, ggy=gy)) source2 = ColumnDataSource(data=dict(rrx=rx, rry=ry)) source3 = ColumnDataSource(data=dict(ggsize=gsize, rrsize=rsize)) # Name the bokeh output. output_file("quality.html") # Generate the dynamic bokeh figure, called 'p.' p = figure(plot_width=1000, plot_height=500) p.title = "Quality-ranked Variants across Genomic Position" # Generate a 'threshold line' based on the user's quality score threshold. thresh_line = Span(location = thresh, dimension='width', line_color='blue', line_width=3) p.renderers.extend([thresh_line]) # Add x and y axis labels. p.yaxis.axis_label = "Phred-scaled Quality Score" p.xaxis.axis_label = "Position (kilobases)" # Generate the hover function for each red and green circle that is plotted. r1 = p.circle(gx, gy, size=gsize, color='green', alpha=0.5, legend="Pass Variant", source=source1) r1_hover = HoverTool(renderers=[r1], tooltips=[('Position (kb)', '@ggx') , ('Quality', '@ggy')]) p.add_tools(r1_hover) r2 = p.circle(rx, ry, size=gsize, color='red', alpha=0.5, legend="Fail Variant", source=source2) r2_hover = HoverTool(renderers=[r2], tooltips=[('Position (kb)', '@rrx') , ('Quality', '@rry')]) p.add_tools(r2_hover) # Add a legend to the figure. p.legend.location = "top_left" show(p)
def plot_flux_variability_analysis_bokeh(fva_result, grid=None, width=None, height=None, title=None, axis_font_size=None, color="blue"): title = "Flux Variability Analysis" if title is None else title factors = list(fva_result.index) x0 = list(fva_result.lower_bound.values) x1 = list(fva_result.upper_bound.values) min_value = min([min(x0), min(x1)]) max_value = max([max(x0), max(x1)]) p = _figure(title, width, height, y_range=factors, x_range=[min_value - 5, max_value + 5]) line_width = (width - 30) / len(factors) / 2 p.segment(x0, factors, x1, factors, line_width=line_width, line_color=color) for x_0, x_1, f in zip(x0, x1, factors): if x_0 == x_1: p.segment([x_0 - 0.01], [f], [x_1 + 0.01], [f], line_width=line_width, color=color) p.line([0, 0], [0, len(factors) + 1], line_color="black", line_width=1, line_alpha=0.3) if axis_font_size is not None: p.xaxis.axis_label_text_font_size = axis_font_size p.yaxis.axis_label_text_font_size = axis_font_size if grid is not None: grid.append(p) else: plotting.show(p)
x_range=xdr, y_range=ydr, plot_width=300, plot_height=300, h_symmetry=False, v_symmetry=False, min_border=0, toolbar_location=None) glyph = DiamondCross(x="x", y="y", size="sizes", line_color="#386cb0", fill_color=None, line_width=2) plot.add_glyph(source, glyph) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) doc = Document() doc.add(plot) show(plot)
graph_layout = dict(zip(airports.index.astype(str), zip(airports.Longitude, airports.Latitude))) layout_provider = StaticLayoutProvider(graph_layout=graph_layout) fig = figure(x_range=(-180, -60), y_range=(15,75), x_axis_label="Longitude", y_axis_label="Latitude", width=800, height=600, background_fill_color=Set3_12[4], background_fill_alpha=0.2, tools='box_zoom,reset') fig.patches(xs="lons", ys="lats", line_color='grey', line_width=1.0, fill_color=Set3_12[10], source=source) r = fig.graph(airports, routes, layout_provider, ## node style props node_fill_color=Set3_12[3], node_fill_alpha=0.4, node_line_color="black", node_line_alpha=0.3, node_nonselection_fill_color=Set3_12[3], node_nonselection_fill_alpha=0.2, node_nonselection_line_alpha=0.1, node_selection_fill_color=Set3_12[3], node_selection_fill_alpha=0.8, node_selection_line_alpha=0.3, ## edge style props edge_line_color="black", edge_line_alpha=0.04, edge_hover_line_alpha=0.6, edge_hover_line_color=Set3_12[1], edge_nonselection_line_color="black", edge_nonselection_line_alpha=0.01, edge_selection_line_alpha=0.6, edge_selection_line_color=Set3_12[1], ## graph policies inspection_policy=NodesAndLinkedEdges(), selection_policy=NodesAndLinkedEdges()) hover = HoverTool(tooltips=[("Airport", "@Name (@IATA), @City ")], renderers=[r]) tap = TapTool(renderers=[r]) fig.add_tools(hover, tap) show(fig)
p1 = figure(match_aspect=True, title="Circle touches all 4 sides of square") p1.rect(0, 0, 300, 300, line_color='black') p1.circle(x=0, y=0, radius=150, line_color='black', fill_color='grey', radius_units='data') def draw_test_figure(aspect_scale=1, width=300, height=300): p = figure(plot_width=width, plot_height=height, match_aspect=True, aspect_scale=aspect_scale, title="Aspect scale = {0}".format(aspect_scale)) p.circle([-1, +1, +1, -1], [-1, -1, +1, +1]) return p aspect_scales = [0.25, 0.5, 1, 2, 4] p2s = [draw_test_figure(aspect_scale=i) for i in aspect_scales] sizes = [(100, 400), (200, 400), (400, 200), (400, 100)] p3s = [draw_test_figure(width=a, height=b) for (a, b) in sizes] layout = layout(children=[[p1], p2s, p3s]) output_file("aspect.html") show(layout)
input_ports=[], output_ports=["OutEven"]) self.addBlock(Counter(block_name="counter")) self.addBlock(Double(block_name="double")) self.addConnection("counter", "double", input_port_name="InNumber", output_port_name="OutCount") self.addConnection("double", "OutEven", output_port_name="OutDouble") cbd = EvenNumberGen("number_gen") draw(cbd, "number_gen.dot") draw(cbd.getBlockByName("counter"), "counter.dot") cbd.run(10) times = [] output = [] for timeValuePair in cbd.getSignal("OutEven"): times.append(timeValuePair.time) output.append(timeValuePair.value) #Plot output_file("./number_gen.html", title="Even Numbers") p = figure(title="Even Numbers", x_axis_label='time', y_axis_label='N') p.circle(x=times, y=output, legend="Even numbers") show(p)
def show(self): show(self.app)
def nb_view_patches3d(Y_r, A, C, dims, image_type='mean', Yr=None, max_projection=False, axis=0, thr=0.9, denoised_color=None, cmap='jet'): """ Interactive plotting utility for ipython notbook Parameters: ----------- Y_r: np.ndarray residual of each trace A,C,b,f: np.ndarrays outputs of matrix factorization algorithm dims: tuple of ints dimensions of movie (x, y and z) image_type: 'mean', 'max' or 'corr' image to be overlaid to neurons (average of shapes, maximum of shapes or nearest neigbor correlation of raw data) Yr: np.ndarray movie, only required if image_type=='corr' to calculate correlation image max_projection: boolean plot max projection along specified axis if True, plot layers if False axis: int (0, 1 or 2) axis along which max projection is performed or layers are shown thr: scalar between 0 and 1 Energy threshold for computing contours denoised_color: string or None color name (e.g. 'red') or hex color code (e.g. '#F0027F') cmap: string name of colormap (e.g. 'viridis') used to plot image_neurons Raise: ------ ValueError("image_type must be 'mean', 'max' or 'corr'") """ bokeh.io.curdoc().clear( ) # prune old orphaned models, otherwise filesize blows up d = A.shape[0] order = list(range(4)) order.insert(0, order.pop(axis)) Y_r = Y_r + C index_permut = np.reshape(np.arange(d), dims, order='F').transpose( order[:-1]).reshape(d, order='F') A = csc_matrix(A)[index_permut, :] dims = tuple(np.array(dims)[order[:3]]) d1, d2, d3 = dims colormap = cm.get_cmap(cmap) grayp = [mpl.colors.rgb2hex(m) for m in colormap(np.arange(colormap.N))] nr, T = C.shape x = np.arange(T) source = ColumnDataSource(data=dict(x=x, y=Y_r[0] / 100, y2=C[0] / 100)) source_ = ColumnDataSource(data=dict(z=Y_r / 100, z2=C / 100)) sourceN = ColumnDataSource(data=dict(N=[nr], nan=np.array([np.nan]))) if max_projection: if image_type == 'corr': tmp = [(local_correlations(Yr.reshape(dims + (-1, ), order='F'))[:, ::-1]).max(i) for i in range(3)] elif image_type == 'mean': tmp = [ (np.array(A.mean(axis=1)).reshape(dims, order='F')[:, ::-1]).max(i) for i in range(3) ] elif image_type == 'max': tmp = [(A.max(axis=1).toarray().reshape(dims, order='F')[:, ::-1]).max(i) for i in range(3)] else: raise ValueError("image_type must be 'mean', 'max' or 'corr'") image_neurons = np.nan * np.ones( (int(1.05 * (d1 + d2)), int(1.05 * (d1 + d3)))) image_neurons[:d2, -d3:] = tmp[0][::-1] image_neurons[:d2, :d1] = tmp[2].T[::-1] image_neurons[-d1:, -d3:] = tmp[1] offset1 = image_neurons.shape[1] - d3 offset2 = image_neurons.shape[0] - d1 proj_ = [ coo_matrix([ A[:, nnrr].toarray().reshape(dims, order='F').max(i).reshape(-1, order='F') for nnrr in range(A.shape[1]) ]) for i in range(3) ] proj_ = [pproj_.T for pproj_ in proj_] coors = [ get_contours(proj_[i], tmp[i].shape, thr=thr) for i in range(3) ] pl.close() K = np.max([[len(cor['coordinates']) for cor in cc] for cc in coors]) cc1 = np.nan * np.zeros(np.shape(coors) + (K, )) cc2 = np.nan * np.zeros(np.shape(coors) + (K, )) for i, cor in enumerate(coors[0]): cc1[0, i, :len(cor['coordinates'])] = cor['coordinates'][:, 0] + offset1 cc2[0, i, :len(cor['coordinates'])] = cor['coordinates'][:, 1] for i, cor in enumerate(coors[2]): cc1[1, i, :len(cor['coordinates'])] = cor['coordinates'][:, 1] cc2[1, i, :len(cor['coordinates'])] = cor['coordinates'][:, 0] for i, cor in enumerate(coors[1]): cc1[2, i, :len(cor['coordinates'])] = cor['coordinates'][:, 0] + offset1 cc2[2, i, :len(cor['coordinates'])] = cor['coordinates'][:, 1] + offset2 c1x = cc1[0][0] c2x = cc2[0][0] c1y = cc1[1][0] c2y = cc2[1][0] c1z = cc1[2][0] c2z = cc2[2][0] source2_ = ColumnDataSource(data=dict(cc1=cc1, cc2=cc2)) source2 = ColumnDataSource( data=dict(c1x=c1x, c1y=c1y, c1z=c1z, c2x=c2x, c2y=c2y, c2z=c2z)) callback = CustomJS(args=dict(source=source, source_=source_, sourceN=sourceN, source2=source2, source2_=source2_), code=""" var data = source.get('data'); var data_ = source_.get('data'); var f = cb_obj.get('value')-1 x = data['x'] y = data['y'] y2 = data['y2'] for (i = 0; i < x.length; i++) { y[i] = data_['z'][i+f*x.length] y2[i] = data_['z2'][i+f*x.length] } var data2_ = source2_.get('data'); var data2 = source2.get('data'); c1x = data2['c1x']; c2x = data2['c2x']; c1y = data2['c1y']; c2y = data2['c2y']; c1z = data2['c1z']; c2z = data2['c2z']; cc1 = data2_['cc1']; cc2 = data2_['cc2']; var N = sourceN.get('data')['N'][0]; for (i = 0; i < c1x.length; i++) { c1x[i] = cc1[f*c1x.length + i] c2x[i] = cc2[f*c1x.length + i] } for (i = 0; i < c1x.length; i++) { c1y[i] = cc1[N*c1y.length + f*c1y.length + i] c2y[i] = cc2[N*c1y.length + f*c1y.length + i] } for (i = 0; i < c1x.length; i++) { c1z[i] = cc1[2*N*c1z.length + f*c1z.length + i] c2z[i] = cc2[2*N*c1z.length + f*c1z.length + i] } source2.trigger('change'); source.trigger('change'); """) else: if image_type == 'corr': image_neurons = local_correlations( Yr.reshape(dims + (-1, ), order='F'))[:-1, ::-1] elif image_type == 'mean': image_neurons = np.array(A.mean(axis=1)).reshape( dims, order='F')[:, ::-1] elif image_type == 'max': image_neurons = A.max(axis=1).toarray().reshape(dims, order='F')[:, ::-1] else: raise ValueError('image_type must be mean, max or corr') cmap = bokeh.models.mappers.LinearColorMapper( [mpl.colors.rgb2hex(m) for m in colormap(np.arange(colormap.N))]) cmap.high = image_neurons.max() coors = get_contours(A, dims, thr=thr) pl.close() cc1 = [[(l[:, 0]) for l in n['coordinates']] for n in coors] cc2 = [[(l[:, 1]) for l in n['coordinates']] for n in coors] length = np.ravel([list(map(len, cc)) for cc in cc1]) idx = np.cumsum(np.concatenate([[0], length[:-1]])) cc1 = np.concatenate(list(map(np.concatenate, cc1))) cc2 = np.concatenate(list(map(np.concatenate, cc2))) linit = int( round(coors[0]['CoM'] [0])) # pick initial layer in which first neuron lies K = length.max() c1 = np.nan * np.zeros(K) c2 = np.nan * np.zeros(K) c1[:length[linit]] = cc1[idx[linit]:idx[linit] + length[linit]] c2[:length[linit]] = cc2[idx[linit]:idx[linit] + length[linit]] source2 = ColumnDataSource(data=dict(c1=c1, c2=c2)) source2_ = ColumnDataSource(data=dict(cc1=cc1, cc2=cc2)) source2_idx = ColumnDataSource(data=dict(idx=idx, length=length)) source3 = ColumnDataSource(data=dict(image=[image_neurons[linit]], im=[image_neurons], x=[0], y=[d2], dw=[d3], dh=[d2])) callback = CustomJS(args=dict(source=source, source_=source_, sourceN=sourceN, source2=source2, source2_=source2_, source2_idx=source2_idx), code=""" var data = source.data; var data_ = source_.data; var f = slider_neuron.value-1; var l = slider_layer.value-1; x = data['x'] y = data['y'] y2 = data['y2'] for (i = 0; i < x.length; i++) { y[i] = data_['z'][i+f*x.length] y2[i] = data_['z2'][i+f*x.length] } var data2 = source2.data; var data2_ = source2_.data; var data2_idx = source2_idx.data; var idx = data2_idx['idx']; c1 = data2['c1']; c2 = data2['c2']; var nz = idx.length / sourceN.data['N'][0]; var nan = sourceN.data['nan'][0]; for (i = 0; i < c1.length; i++) { c1[i] = nan; c2[i] = nan; } for (i = 0; i < data2_idx['length'][l+f*nz]; i++) { c1[i] = data2_['cc1'][idx[l+f*nz] + i]; c2[i] = data2_['cc2'][idx[l+f*nz] + i]; } source2.trigger('change'); source.trigger('change'); """) callback_layer = CustomJS(args=dict(source=source3, sourceN=sourceN, source2=source2, source2_=source2_, source2_idx=source2_idx), code=""" var f = slider_neuron.value-1; var l = slider_layer.value-1; var dh = source.data['dh'][0]; var dw = source.data['dw'][0]; var image = source.data['image'][0]; var images = source.data['im'][0]; for (var i = 0; i < x.length; i++) { for (var j = 0; j < dw; j++){ image[i*dh+j] = images[l*dh*dw + i*dh + j]; } } var data2 = source2.data; var data2_ = source2_.data; var data2_idx = source2_idx.data; var idx = data2_idx['idx'] c1 = data2['c1']; c2 = data2['c2']; var nz = idx.length / sourceN.data['N'][0]; var nan = sourceN.data['nan'][0]; for (i = 0; i < c1.length; i++) { c1[i] = nan; c2[i] = nan; } for (i = 0; i < data2_idx['length'][l+f*nz]; i++) { c1[i] = data2_['cc1'][idx[l+f*nz] + i]; c2[i] = data2_['cc2'][idx[l+f*nz] + i]; } source.trigger('change'); source2.trigger('change'); """) plot = bpl.figure(plot_width=600, plot_height=300) plot.line('x', 'y', source=source, line_width=1, line_alpha=0.6) if denoised_color is not None: plot.line('x', 'y2', source=source, line_width=1, line_alpha=0.6, color=denoised_color) slider = bokeh.models.Slider(start=1, end=Y_r.shape[0], value=1, step=1, title="Neuron Number", callback=callback) xr = Range1d(start=0, end=image_neurons.shape[1] if max_projection else d3) yr = Range1d(start=image_neurons.shape[0] if max_projection else d2, end=0) plot1 = bpl.figure(x_range=xr, y_range=yr, plot_width=300, plot_height=300) if max_projection: plot1.image(image=[image_neurons[::-1, :]], x=0, y=image_neurons.shape[0], dw=image_neurons.shape[1], dh=image_neurons.shape[0], palette=grayp) plot1.patch('c1x', 'c2x', alpha=0.6, color='purple', line_width=2, source=source2) plot1.patch('c1y', 'c2y', alpha=0.6, color='purple', line_width=2, source=source2) plot1.patch('c1z', 'c2z', alpha=0.6, color='purple', line_width=2, source=source2) layout = bokeh.layouts.layout( [[slider], [bokeh.layouts.row(plot1, plot)]], sizing_mode="scale_width") else: slider_layer = bokeh.models.Slider(start=1, end=d1, value=linit + 1, step=1, title="Layer", callback=callback_layer) callback.args['slider_neuron'] = slider callback.args['slider_layer'] = slider_layer callback_layer.args['slider_neuron'] = slider callback_layer.args['slider_layer'] = slider_layer plot1.image(image='image', x='x', y='y', dw='dw', dh='dh', color_mapper=cmap, source=source3) plot1.patch('c1', 'c2', alpha=0.6, color='purple', line_width=2, source=source2) layout = bokeh.layouts.layout( [[slider], [slider_layer], [bokeh.layouts.row(plot1, plot)]], sizing_mode="scale_width") bpl.show(layout) return Y_r
def nb_view_patches(Yr, A, C, b, f, d1, d2, YrA=None, image_neurons=None, thr=0.99, denoised_color=None, cmap='jet'): """ Interactive plotting utility for ipython notebook Parameters: ----------- Yr: np.ndarray movie A,C,b,f: np.ndarrays outputs of matrix factorization algorithm d1,d2: floats dimensions of movie (x and y) YrA: np.ndarray ROI filtered residual as it is given from update_temporal_components If not given, then it is computed (K x T) image_neurons: np.ndarray image to be overlaid to neurons (for instance the average) thr: double threshold regulating the extent of the displayed patches denoised_color: string or None color name (e.g. 'red') or hex color code (e.g. '#F0027F') cmap: string name of colormap (e.g. 'viridis') used to plot image_neurons """ colormap = cm.get_cmap(cmap) grayp = [mpl.colors.rgb2hex(m) for m in colormap(np.arange(colormap.N))] nr, T = C.shape nA2 = np.ravel(np.power(A, 2).sum(0)) if type(A) == np.ndarray else np.ravel( A.power(2).sum(0)) b = np.squeeze(b) f = np.squeeze(f) if YrA is None: Y_r = np.array( spdiags(old_div(1, nA2), 0, nr, nr) * (A.T * np.matrix(Yr) - (A.T * np.matrix(b[:, np.newaxis])) * np.matrix(f[np.newaxis]) - A.T.dot(A) * np.matrix(C)) + C) else: Y_r = C + YrA x = np.arange(T) z = old_div(np.squeeze(np.array(Y_r[:, :].T)), 100) if image_neurons is None: image_neurons = A.mean(1).reshape((d1, d2), order='F') coors = get_contours(A, (d1, d2), thr) cc1 = [cor['coordinates'][:, 0] for cor in coors] cc2 = [cor['coordinates'][:, 1] for cor in coors] c1 = cc1[0] c2 = cc2[0] # split sources up, such that Bokeh does not warn # "ColumnDataSource's columns must be of the same length" source = ColumnDataSource(data=dict(x=x, y=z[:, 0], y2=C[0] / 100)) source_ = ColumnDataSource(data=dict(z=z.T, z2=C / 100)) source2 = ColumnDataSource(data=dict(c1=c1, c2=c2)) source2_ = ColumnDataSource(data=dict(cc1=cc1, cc2=cc2)) callback = CustomJS(args=dict(source=source, source_=source_, source2=source2, source2_=source2_), code=""" var data = source.get('data') var data_ = source_.get('data') var f = cb_obj.get('value')-1 x = data['x'] y = data['y'] y2 = data['y2'] for (i = 0; i < x.length; i++) { y[i] = data_['z'][i+f*x.length] y2[i] = data_['z2'][i+f*x.length] } var data2_ = source2_.get('data'); var data2 = source2.get('data'); c1 = data2['c1']; c2 = data2['c2']; cc1 = data2_['cc1']; cc2 = data2_['cc2']; for (i = 0; i < c1.length; i++) { c1[i] = cc1[f][i] c2[i] = cc2[f][i] } source2.trigger('change') source.trigger('change') """) plot = bpl.figure(plot_width=600, plot_height=300) plot.line('x', 'y', source=source, line_width=1, line_alpha=0.6) if denoised_color is not None: plot.line('x', 'y2', source=source, line_width=1, line_alpha=0.6, color=denoised_color) slider = bokeh.models.Slider(start=1, end=Y_r.shape[0], value=1, step=1, title="Neuron Number", callback=callback) xr = Range1d(start=0, end=image_neurons.shape[1]) yr = Range1d(start=image_neurons.shape[0], end=0) plot1 = bpl.figure(x_range=xr, y_range=yr, plot_width=300, plot_height=300) plot1.image(image=[image_neurons[::-1, :]], x=0, y=image_neurons.shape[0], dw=d2, dh=d1, palette=grayp) plot1.patch('c1', 'c2', alpha=0.6, color='purple', line_width=2, source=source2) bpl.show(bokeh.layouts.layout([[slider], [bokeh.layouts.row(plot1, plot)]])) return Y_r
def plot_waterfall_relative_importance(self, incremental_rsquare_df): index = list(incremental_rsquare_df['Features'].values) data = { 'Percentage Relative Importance': list(incremental_rsquare_df['percentage_incremental_r2'].values) } df = pd.DataFrame(data=data, index=index) net = df['Percentage Relative Importance'].sum() # print("Net ",net) df['running_total'] = df['Percentage Relative Importance'].cumsum() df['y_start'] = df['running_total'] - df[ 'Percentage Relative Importance'] df['label_pos'] = df['running_total'] df_net = pd.DataFrame.from_records( [(net, net, 0, net)], columns=[ 'Percentage Relative Importance', 'running_total', 'y_start', 'label_pos' ], index=["net"]) df = df.append(df_net) df['color'] = '#1de9b6' df.loc[df['Percentage Relative Importance'] == 100, 'color'] = '#29b6f6' df.loc[df['Percentage Relative Importance'] < 0, 'label_pos'] = df.label_pos - 10000 df["bar_label"] = df["Percentage Relative Importance"].map( '{:,.1f}'.format) TOOLS = "reset,save" source = ColumnDataSource(df) p = figure(tools=TOOLS, x_range=list(df.index), y_range=(0, net + 10), plot_width=1000, title="Percentage Relative Importance Waterfall") p.segment(x0='index', y0='y_start', x1="index", y1='running_total', source=source, color="color", line_width=35) p.grid.grid_line_alpha = 0.4 p.yaxis[0].formatter = NumeralTickFormatter(format="(0 a)") p.xaxis.axis_label = "Predictors" p.yaxis.axis_label = "Percentage Relative Importance(%)" p.xaxis.axis_label_text_font_size = '12pt' p.yaxis.axis_label_text_font_size = '12pt' labels = LabelSet(x='index', y='label_pos', text='bar_label', text_font_size="11pt", level='glyph', x_offset=-14, y_offset=0, source=source) p.add_layout(labels) p.xaxis.major_label_orientation = -math.pi / 4 show(p)
def nb_plot_contour(image, A, d1, d2, thr=None, thr_method='max', maxthr=0.2, nrgthr=0.9, face_color=None, line_color='red', alpha=0.4, line_width=2, coordinates=None, show=True, cmap='jet', **kwargs): """Interactive Equivalent of plot_contours for ipython notebook Args: A: np.ndarray or sparse matrix Matrix of Spatial components (d x K) Image: np.ndarray (2D) Background image (e.g. mean, correlation) d1,d2: floats dimensions os image thr: scalar between 0 and 1 Energy threshold for computing contours Kept for backwards compatibility. If not None then thr_method = 'nrg', and nrgthr = thr thr_method: [optional] string Method of thresholding: 'max' sets to zero pixels that have value less than a fraction of the max value 'nrg' keeps the pixels that contribute up to a specified fraction of the energy maxthr: [optional] scalar Threshold of max value nrgthr: [optional] scalar Threshold of energy display_number: Boolean Display number of ROIs if checked (default True) max_number: int Display the number for only the first max_number components (default None, display all numbers) cmap: string User specifies the colormap (default None, default colormap) """ p = nb_imshow(image, cmap=cmap) p.plot_width = 600 p.plot_height = 600 * d1 // d2 center = com(A, d1, d2) p.circle(center[:, 1], center[:, 0], size=10, color="black", fill_color=None, line_width=2, alpha=1) if coordinates is None: coors = get_contours(A, np.shape(image), thr, thr_method) else: coors = coordinates cc1 = [np.clip(cor['coordinates'][1:-1, 0], 0, d2) for cor in coors] cc2 = [np.clip(cor['coordinates'][1:-1, 1], 0, d1) for cor in coors] p.patches(cc1, cc2, alpha=1, color=face_color, line_color=line_color, line_width=2, **kwargs) if show: bpl.show(p) return p
#Defining the chart output_notebook() plot_chart = bp.figure(plot_width=700, plot_height=600, title="A map/plot of 5000 word vectors", tools="pan,wheel_zoom,box_zoom,reset,hover,previewsave", x_axis_type=None, y_axis_type=None, min_border=1) #Extracting the list of word vectors, limiting to 5000, each is of 200 dimensions word_vectors = [model[w] for w in list(model.wv.vocab.keys())[:5000]] # Reducing dimensionality by converting the vectors to 2d vectors from sklearn.manifold import TSNE tsne_model = TSNE(n_components=2, verbose=1, random_state=0) tsne_w2v = tsne_model.fit_transform(word_vectors) # Storing data in a dataframe tsne_df = pd.DataFrame(tsne_w2v, columns=['x', 'y']) tsne_df['words'] = list(model.wv.vocab.keys())[:5000] # Corresponding word appears when you hover on the data point. plot_chart.scatter(x='x', y='y', source=tsne_df) hover = plot_chart.select(dict(type=HoverTool)) hover.tooltips = {"word": "@words"} show(plot_chart) #Save word embedding model model_file = 'imdb_word2vec_embedding.txt' model.wv.save_word2vec_format(filename, binary=False)
def back_test_performance(self): if len(self.sim_account.trades ) > 0 and self.sim_account.trades[-1].side == 1: self.sim_account.trades.append( Order(price=self.tick_data["ma1"][-1], side=-1, symbol=self.trade_symbol, quantity=self.position, at=self.bolling.minute_q[-1].time_stamp)) self.sim_account.nets.append(self.sim_account.balance + self.sim_account.position * self.tick_data["ma1"][-1]) super(Strategy, self).back_test_performance() def random_color(): import random x = random.random() * 256 * 256 * 256 prefix = '#' for i in range(6 - len(str(hex(int(x))[2:]))): prefix = prefix + '0' return prefix + str(hex(int(x))[2:]).upper() from bokeh.plotting import figure, show, Column buy_orders = {"time": [], "price": []} sell_orders = {"time": [], "price": []} for trade in self.sim_account.trades: assert isinstance(trade, Order) t_time = trade.at + datetime.timedelta(hours=8) if trade.side > 0: buy_orders["time"].append(t_time) buy_orders["price"].append(trade.price) else: sell_orders["time"].append(t_time) sell_orders["price"].append(trade.price) plot = figure(x_axis_type="datetime", width=1300) bar_plot = figure(x_axis_type="datetime", width=1300) # plot.line(x=self.tick_data["time"], y=self.tick_data["ma1"], legend="ma10", color=random_color()) plot.circle_x(x=buy_orders["time"], y=buy_orders["price"], color="red", fill_color=None, size=20, line_width=5) plot.square_cross(x=sell_orders["time"], y=sell_orders["price"], color="blue", fill_color=None, size=20, line_width=5) plot.line(x=self.tick_data["time"], y=self.tick_data["ma5"], legend="ma30", color=random_color()) plot.line(x=self.tick_data["time"], y=self.tick_data["ma15"], legend="ma90", color=random_color()) plot.line(x=self.tick_data["time"], y=self.tick_data["mean"], legend="mean", color='black') plot.line(x=self.tick_data["time"], y=self.tick_data["upper"], legend="upper", color='black') plot.line(x=self.tick_data["time"], y=self.tick_data["lower"], legend="lower", color='black') plot.scatter(x=self.tick_data["time"], y=self.tick_data["bid"], legend="bid", color=random_color()) plot.scatter(x=self.tick_data["time"], y=self.tick_data["ask"], legend="ask", color=random_color()) bar_plot.vbar(x=self.tick_data["time"], top=self.tick_data["std"], bottom=0, width=1) bar_plot.line(x=self.tick_data["time"], y=self.tick_data["avg_std"]) show(Column(plot, bar_plot))
inc = df.close > df.open dec = df.open > df.close w = 12 * 60 * 60 * 1000 # half day in ms TOOLS = "pan,wheel_zoom,box_zoom,reset,save" p = figure(x_axis_type="datetime", tools=TOOLS, width=1000, title="MSFT Candlestick") p.xaxis.major_label_orientation = pi / 4 p.grid.grid_line_alpha = 0.3 p.segment(df.date, df.high, df.date, df.low, color="black") p.vbar(df.date[inc], w, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black") p.vbar(df.date[dec], w, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black") output_file("candlestick.html", title="candlestick.py example") show(p) # open a browser
p2 = make_plot("Log Normal Distribution (μ=0, σ=0.5)", hist, x, pdf, cdf) # Gamma Distribution k, theta = 7.5, 1.0 measured = np.random.gamma(k, theta, 1000) hist, edges = np.histogram(measured, density=True, bins=50) x = np.linspace(0.0001, 20.0, 1000) pdf = x**(k-1) * np.exp(-x/theta) / (theta**k * scipy.special.gamma(k)) cdf = scipy.special.gammainc(k, x/theta) p3 = make_plot("Gamma Distribution (k=7.5, θ=1)", hist, x, pdf, cdf) # Weibull Distribution lam, k = 1, 1.25 measured = lam*(-np.log(np.random.uniform(0, 1, 1000)))**(1/k) hist, edges = np.histogram(measured, density=True, bins=50) x = np.linspace(0.0001, 8, 1000) pdf = (k/lam)*(x/lam)**(k-1) * np.exp(-(x/lam)**k) cdf = 1 - np.exp(-(x/lam)**k) p4 = make_plot("Weibull Distribution (λ=1, k=1.25)", hist, x, pdf, cdf) output_file('histogram.html', title="histogram.py example") show(gridplot([p1,p2,p3,p4], ncols=2, plot_width=400, plot_height=400, toolbar_location=None))
line_width=2.2) #lower panel ts = figure(x_axis_type='datetime', x_axis_label='time', y_axis_label='index', plot_width=800, plot_height=400, y_range=(-4, 4)) ts.line(datetime, pcpexp3, legend_label="Tau03", line_color="black", line_width=2) ts.line(datetime, pcpexp10, legend_label="Tau10", line_color="blue", line_width=2) ts.line(datetime, pcpexp20, legend_label="Tau20", line_color="green", line_width=2) ts.line(datetime, pcpexp36, legend_label="Tau36", line_color="red", line_width=2) show(column(corr, ts))
def rental_dist_per_station(df, interval_minutes, temporal='week'): if temporal == 'week': t_size = 60 * 24 * 7 // interval_minutes elif temporal == 'day': t_size = 60 * 24 // interval_minutes t_min = df['time_id'].min() df.loc[:, 'week_index'] = df.loc[:, 'time_id'].apply(lambda x: (x - t_min) // t_size) station_groups = df.groupby(['station_id', 'week_index']).sum() station_groups[ 'tot'] = station_groups['arrivals'] + station_groups['departures'] weekly = station_groups.groupby('station_id').mean() weekly['count_bin'], bin_vals = pd.cut(weekly['tot'], bins=range(0, 3500, 50), retbins=True) weekly = weekly.reset_index() weekly['mid_point'] = weekly['count_bin'].apply(lambda x: int(x.mid)) histogram = weekly.groupby('mid_point').count().reset_index() source = ColumnDataSource(histogram) colors = brewer['PRGn'][4] p = figure(plot_width=800, plot_height=400, toolbar_location='above', y_range=(0, 80), x_range=(0, 3400)) p.vbar(x='mid_point', top='tot', source=source, fill_color=colors[0], line_color=colors[-1], width=50) p.xaxis.axis_label = 'average weekly departures+arrivals at station' p.yaxis.axis_label = '# stations' #show(p) print(station_groups) dd = station_groups.reset_index().groupby('week_index').sum() dd = dd.loc[:52 * 3, :] x = dd.index.to_list() y = dd['departures'].to_list() source = ColumnDataSource({'x': x, 'y': y}) p = figure(plot_width=800, plot_height=400, toolbar_location='above', x_range=(0, 52 * 3)) p.line(x='x', y='y', source=source, color=colors[0], line_width=3) p.xaxis.axis_label = 'weeks since 1st January 2017' p.xaxis.ticker = FixedTicker(ticks=list(range(0, 160, 20))) p.yaxis.axis_label = '# rental events in week' p.yaxis.formatter = NumeralTickFormatter(format='0a') #show(p) df_x = df.loc[df['week_index'] == 20] tt = df_x.groupby('time_id').sum() x = tt.index y = tt['departures'].to_list() source = ColumnDataSource({'x': x, 'y': y}) p = figure(plot_width=800, plot_height=400, toolbar_location='above', y_range=(0, 2600)) p.xgrid.grid_line_alpha = 0.0 p.line(x='x', y='y', source=source, color=colors[0], line_width=3) p.yaxis.axis_label = '# rentals in half hour' p.xaxis.ticker = FixedTicker(ticks=list(range(8040, 8350, 48))) p.xaxis.major_label_overrides = { 8040: 'Wednesday', 8088: 'Thursday', 8136: 'Friday', 8184: 'Saturday', 8232: 'Sunday', 8280: 'Monday', 8328: 'Tuesday' } show(p)
def index(): photometry_time, photometry_mag, photometry_sigma, photometry_band, detection = querry_single_osc( "DES17C1ffz") lumdist, redshift = querry_distance("DES17C1ffz") N = 200 t_original = np.linspace(0, 100, N) t = t_original M = 5 * (1.989 * (10**33)) f = 0.5 k = 0.25 v = 12 * (10**8) tn = 8.8 B = 13.8 c = 3 * (10**10) E = 3.9 * (10**10) td = (((2 * k * M) / (B * c * v))**(1. / 2.)) / 60. / 60. / 24. integrand = (t / td) * (np.exp(t**2 / td**2)) * (np.exp(-t / tn)) my_int = integrate.cumtrapz(t, integrand, initial=0) L = ((2 * M * f) / (td * 24 * 60 * 60)) * (np.exp(-t**2) / td**2) * E * my_int source = ColumnDataSource( data=dict(x=t, y=L, yB=L, yr=L, yi=L, yV=L, yU=L)) callback2 = CustomJS(args=dict(source=source), code=""" var request = new XMLHttpRequest(); request.open('GET', 'https://astrocats.space/api/DES17C1ffz/lumdist+redshift'); console.log(request); """) callback = CustomJS(args=dict(source=source), code=""" function numerically_integrate(a, b, dx, f,td) { // calculate the number of trapezoids n = (b - a) / dx; // define the variable for area Area = 0; //loop to calculate the area of each trapezoid and sum. for (i = 1; i <= n; i++) { //the x locations of the left and right side of each trapezpoid x0 = a + (i-1)*dx; x1 = a + i*dx; // the area of each trapezoid Ai = dx * (f(x0,td) + f(x1,td))/ 2.; // cumulatively sum the areas Area = Area + Ai } return Area; } //define function to be integrated function f1(x,td){ return x / td * Math.exp(Math.pow(x/td,2)) * Math.exp(-x/8.77); } function f2(x,td){ return x / td * Math.exp(Math.pow(x/td,2)) * Math.exp(-x/111); } dx = 0.5; // width of the trapezoids var tni = 8.8; var tco = 111.3; var epni = 3.9e10; var epco = 6.8e9; var beta = 13.8; var c = 1.0e10; var msol = 2e33; var m = mej.value; m = m * msol; var wav = 6e-5; var mni = fni.value; mni = mni * m; var T = T.value; var v = vej.value; v = v * Math.pow (10,5); var k = k.value; var td = Math.sqrt(2. * k * m / (beta * c * v)) / 86400; var data = source.data; var xstop = 0.0; x = data['x'] y = data['y'] console.log(x); console.log(y); var distance = distance.value * 3e24; var redshift = redshift.value; redshift = parseFloat(redshift); for (j = 0; j < x.length; j++) { xstop = j * dx; int1 = numerically_integrate(0,xstop,dx,f1,td); int2 = numerically_integrate(0,xstop,dx,f2,td); factor = 2 * mni / td * Math.exp(-Math.pow(xstop/td,2)); L = factor * ((epni-epco) * int1 + epco * int2) / (4.*3.14*Math.pow(distance,2)); y[j] = -2.5 * Math.log10(L*wav/c)-48.3; x[j] = (dx*(1+redshift)) * j + T; } source.change.emit(); """) plot = figure( plot_height=400, plot_width=400, title="Super cool blackbody curve thing", tools="crosshair,pan,reset,save,wheel_zoom", x_range=[np.min(photometry_time) - 20, np.max(photometry_time) + 100], y_range=[np.max(photometry_mag), np.min(photometry_mag)]) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) #plot.line('x', 'yB', source=source, line_width=3, line_alpha=0.6, color="pink") #plot.line('x', 'yr', source=source, line_width=3, line_alpha=0.6, color="orange") #plot.line('x', 'yi', source=source, line_width=3, line_alpha=0.6, color="blue") #plot.line('x', 'yV', source=source, line_width=3, line_alpha=0.6, color="turquoise") #plot.line('x', 'yU', source=source, line_width=3, line_alpha=0.6, color="purple") arrayoftimes = np.array(photometry_time) text = TextInput(title="title", value='my parabola', callback=callback2) lumdist_input = TextInput(title="title", value=str(lumdist)) redshift_input = TextInput(title="title", value=str(redshift)) M_slider = Slider(start=0.1, end=10, value=1, step=.1, title="Ejecta Mass", callback=callback) f_slider = Slider(start=0.01, end=1.0, value=0.1, step=.01, title="Nickel Fraction", callback=callback) v_slider = Slider(start=5000, end=20000, value=10000, step=1000, title="Ejecta Velocity", callback=callback) k_slider = Slider(start=0.1, end=0.4, value=0.2, step=.01, title="Opacity", callback=callback) T_slider = Slider(title="Time", value=arrayoftimes.min() - 10, start=arrayoftimes.min() - 10, end=arrayoftimes.max() + 10, step=10, callback=callback) callback.args["mej"] = M_slider callback.args["fni"] = f_slider callback.args["vej"] = v_slider callback.args["k"] = k_slider callback.args["T"] = T_slider callback.args["distance"] = lumdist_input callback.args["redshift"] = redshift_input count = 0 for x in photometry_time: if photometry_band[count] == "r": plot.circle(x, photometry_mag[count], size=5, color="orange", alpha=0.5) elif photometry_band[count] == "i": plot.circle(x, photometry_mag[count], size=5, color="blue", alpha=0.5) elif photometry_band[count] == "g": plot.circle(x, photometry_mag[count], size=5, color="turquoise", alpha=0.5) elif photometry_band[count] == "z": plot.circle(x, photometry_mag[count], size=5, color="purple", alpha=0.5) elif photometry_band[count] == "B": plot.circle(x, photometry_mag[count], size=5, color="pink", alpha=0.5) count += 1 text.on_change('value', update_title) def update_data(attrname, old, new): # Get the current slider values M = M_slider.value * 2.e33 f = f_slider.value v = v_slider.value * 1.e5 k = k_slider.value t = t_original tn = 8.8 B = 13.8 c = 3 * (10**10) E = 3.9 * (10**10) td = (((2. * k * M) / (B * c * v))**(1. / 2.)) / 60. / 60. / 24. integrand = (t / td) * (np.exp(t**2 / td**2)) * (np.exp(-t / tn)) my_int = integrate.cumtrapz(integrand, t, initial=0) L = ((2. * M * f) / (td)) * (np.exp((-t**2) / td**2)) * E * my_int magnitude = -2.5 * np.log10(L / 4e33) + 4.3 radii = v * t * 24 * 60 * 60 temperature = (L / (4 * np.pi * (radii**2) * (5.67 * 10**-5)))**0.25 county = 0 wavelength = 5 * 10**-5 wavelength_B = 4.45 * 10**-5 wavelength_r = 6.58 * 10**-5 wavelength_i = 8.06 * 10**-5 wavelength_V = 5.51 * 10**-5 wavelength_U = 3.65 * 10**-5 distance = lumdist * (3 * 10**24) luminosityblackbody = blackbody( radii, temperature, 5. * (10**-5) * np.ones(len(radii))) * wavelength**2 / c / distance**2 luminosityblackbody_B = blackbody( radii, temperature, 5. * (10**-5) * np.ones(len(radii))) * wavelength_B**2 / c / distance**2 luminosityblackbody_r = blackbody( radii, temperature, 5. * (10**-5) * np.ones(len(radii))) * wavelength_r**2 / c / distance**2 luminosityblackbody_i = blackbody( radii, temperature, 5. * (10**-5) * np.ones(len(radii))) * wavelength_i**2 / c / distance**2 luminosityblackbody_V = blackbody( radii, temperature, 5. * (10**-5) * np.ones(len(radii))) * wavelength_V**2 / c / distance**2 luminosityblackbody_U = blackbody( radii, temperature, 5. * (10**-5) * np.ones(len(radii))) * wavelength_U**2 / c / distance**2 magblackbody = -2.5 * np.log10(luminosityblackbody) - 48.6 magblackbody_B = -2.5 * np.log10(luminosityblackbody_B) - 48.6 magblackbody_r = -2.5 * np.log10(luminosityblackbody_r) - 48.6 magblackbody_i = -2.5 * np.log10(luminosityblackbody_i) - 48.6 magblackbody_V = -2.5 * np.log10(luminosityblackbody_V) - 48.6 magblackbody_U = -2.5 * np.log10(luminosityblackbody_U) - 48.6 # Generate the new curve L = ((2. * M * f) / (td)) * (np.exp((-t**2) / td**2)) * E * my_int magnitude = -2.5 * np.log10(L / 4e33) + 4.3 source.data = dict(x=t * (1. + redshift) + T_slider.value, y=magblackbody, yB=magblackbody_B, yr=magblackbody_r, yi=magblackbody_i, yV=magblackbody_V, yU=magblackbody_U) #for w in [M_slider,f_slider,v_slider, k_slider, T_slider]: # w.on_change('value', update_data) # Set up layouts and add to document inputs = widgetbox(text, M_slider, f_slider, v_slider, k_slider, T_slider) layout = row( plot, inputs, ) output_file("NewBokeh.html") script, div = components(plot) return render_template("blah.html", script=script, div=div) show(layout)
import matplotlib import matplotlib.mlab as mlab import matplotlib.pyplot as plt import numpy as np matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) # Create a simple contour plot with labels using default colors. The # inline argument to clabel will control whether the labels are draw # over the line segments of the contour, removing the lines beneath # the label plt.figure() CS = plt.contour(X, Y, Z) plt.clabel(CS, inline=1, fontsize=10) plt.title('Simplest default with labels') output_file("mpl_contour.html", title="mpl_contour.py example") show(mpl.to_bokeh())
def graph_info(df_gw, max_dist=1.0, weight_type='median_norm', lower_weight=None, common_weight=None, scale=1.0): def _map_color(vals, pallette): ret = [] for s in vals: ind_palette = int((len(pallette) - 1) * s / max_dist) ret.append(pallette[ind_palette]) return ret def _make_distance(x): return max_dist * np.exp(-1.0 * x * scale) df_gw = df_gw[['StartStation Id', 'EndStation Id', weight_type]] df_gw['StartStation Id'] = df_gw['StartStation Id'].astype(int) df_gw['EndStation Id'] = df_gw['EndStation Id'].astype(int) df_gw[weight_type] = df_gw[weight_type].astype(float) df_gw = df_gw.loc[~df_gw['StartStation Id'].isin(EXCLUDE_STATIONS)] df_gw = df_gw.loc[~df_gw['EndStation Id'].isin(EXCLUDE_STATIONS)] #TTT = [1,11,12,13,14,15] #df_gw = df_gw.loc[df_gw['StartStation Id'].isin(TTT)] #df_gw = df_gw.loc[df_gw['EndStation Id'].isin(TTT)] nstations = len(df_gw['StartStation Id'].unique()) #df1 = df_gw.loc[df_gw['StartStation Id'] != df_gw['EndStation Id']].set_index(['StartStation Id', 'EndStation Id']) df1 = df_gw.set_index(['StartStation Id', 'EndStation Id']) df2 = df1.reorder_levels(['EndStation Id', 'StartStation Id']) df2 = df2.reindex(df1.index, fill_value=0.0) df3 = pd.concat([df1, df2], axis=1).mean(axis=1) df3 = df3.reset_index() if not lower_weight is None: df3 = df3.loc[df3[0] >= lower_weight] if not common_weight is None: df3[0] = common_weight df3['distance'] = df3[0].apply(_make_distance) df3['distance'] = np.where(df3['StartStation Id'] == df3['EndStation Id'], 0.0, df3['distance']) df_w = pd.pivot(df3, index='StartStation Id', columns='EndStation Id', values='distance').fillna(max_dist) clustering = AgglomerativeClustering(n_clusters=6, affinity='precomputed', linkage='average').fit( df_w.to_numpy()) print(clustering.labels_) # clustering = SpectralClustering(n_clusters=6, affinity='precomputed').fit(df_w.to_numpy()) # clustering = DBSCAN(metric='precomputed').fit(df_w.to_numpy()) ss1 = pd.Series(clustering.labels_, df_w.index, name='cluster_id_s') ss2 = pd.Series(clustering.labels_, df_w.index, name='cluster_id_e') df_full = pd.DataFrame(df_w.stack()) df_full = df_full.join(ss1, on='StartStation Id') df_full = df_full.join(ss2, on='EndStation Id') df_full = df_full.reset_index() clust_aa = df_full.groupby(['cluster_id_s', 'cluster_id_e' ]).mean().sort_values(by=0).index.to_list() gg = [x for x in clust_aa if x[0] != x[1]] print(gg) ppp = [] for g in gg: g1 = g[0] g2 = g[1] if not g1 in ppp: ppp.append(g1) if not g2 in ppp: ppp.append(g2) ddd = dict([(val, key) for key, val in enumerate(ppp)]) print(ddd) df_full['cluster_id_s'] = df_full['cluster_id_s'].replace(ddd) df_full['cluster_id_e'] = df_full['cluster_id_e'].replace(ddd) df_full = df_full.sort_values(['cluster_id_s', 'StartStation Id']) vals = [] start_s = [] end_s = [] for nrow in range(nstations): hh = df_full.iloc[nrow * nstations:(nrow + 1) * nstations] hh = hh.sort_values(['cluster_id_e', 'EndStation Id']) vals.extend(hh[0].to_list()) start_s.extend(hh['StartStation Id'].to_list()) end_s.extend(hh['EndStation Id'].to_list()) colors = _map_color(vals, list(cc.gray)) #colors = _map_color(vals, list(reversed(cc.rainbow))) xx = list(range(nstations)) data = dict(x=xx * nstations, y=sorted(xx * nstations, reverse=True), weight=vals, start_s=start_s, end_s=end_s, colors=colors) print(data) p = figure(x_range=(-1, nstations), y_range=(-1, nstations), tooltips=[('stations', '@start_s, @end_s'), ('value', '@weight')], width=800, height=800) p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.xaxis.major_label_text_font_size = '0pt' p.yaxis.major_label_text_font_size = '0pt' p.rect('x', 'y', 0.9, 0.9, source=data, color='colors', line_color=None) show(p)
""" % prop) for i, line in enumerate(lines): widget.callback.args['line%i' % i] = line widget.callback.args['nlines'] = len(lines) def make_slider(prop, start, end, value): slider = models.Slider(title=prop, start=start, end=end, value=value) add_callback(slider, prop) return slider def make_dropdown(prop, menu): dropdown = models.Dropdown(label=prop, menu=menu) add_callback(dropdown, prop) return dropdown sliders = [ make_slider('line_width', start=0.2, end=16, value=5), make_slider('line_dash_offset', start=0, end=100, value=1), make_dropdown('line_cap', [("butt", "butt"), ("round", "round"), ("square", "square")]), make_dropdown('line_join', [("miter", "miter"), ("round", "round"), ("bevel", "bevel")]), ] output_file("line_compare.html", title="line_compare.py example") sliders = models.VBox(*sliders) show(models.HBox(sliders, p1, p2))
p[serv].y_range.end = int(df['curr_con'].max()) + 150 p[serv].add_tools(hover) p[serv].title.text_font_size = "20px" p[serv].line("Date", "curr_con", source=source, alpha=0.5, color='#5cb85c', line_width=2, legend="Conn") p[serv].line("Date", "curr_ssl_con", source=source, alpha=0.5, color="#5d9ceb", line_width=2, legend="SSL con") p[serv].line("Date", "sess_rate", source=source, alpha=0.5, color="#33414e", line_width=2, legend="Sessions") p[serv].legend.orientation = "horizontal" p[serv].legend.location = "top_left" p[serv].legend.padding = 5 plots = [] for key, value in p.items(): plots.append(value) grid = gridplot(plots, ncols=2, plot_width=800, plot_height=250, toolbar_location = "left", toolbar_options=dict(logo=None)) show(grid) if form.getvalue('waf_metrics'): from datetime import timedelta from bokeh.plotting import figure, output_file, show from bokeh.models import ColumnDataSource, HoverTool, DatetimeTickFormatter, DatePicker from bokeh.layouts import widgetbox, gridplot from bokeh.models.widgets import Button, RadioButtonGroup, Select import pandas as pd import http.cookies cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_id = cookie.get('uuid') servers = sql.select_waf_servers_metrics(user_id.value) servers = sorted(servers)
def build_bokeh(input_fp, output_fp, title, seperator=',', remcols=None, color_by=None, palette=None, legend_pos='top_right'): data = pd.DataFrame.from_csv(input_fp, sep=seperator) # Put depth as first column cols = data.columns.tolist() popped = cols.pop(7) cols.insert(0, popped) data = data[cols] # Change odd depths to regular ones data['Depth (m)'].replace(47, 50, inplace=True) #data['Depth (m)'].replace(258, 200, inplace=True) # Drop unwanted columns if needed if remcols: remcols = list(remcols) data.drop(remcols, axis=1, inplace=True) # Build out the colors for the graph if needed legend = [] if color_by is not None: groups = data.groupby(color_by).groups group_list = sorted(groups.keys(), reverse=True) # Grab colormap or use provided if len(palette.split(',')) > 1: # Format into colormap-like object p = palette.split(',') hold = dict(x.split(':') for x in p) colormap = [hold[str(g)] for g in group_list] elif len(groups) > 9: raise ValueError( "Can only support up to 9 groups, " "%s has %d groups" % color_by, len(groups)) else: colormap = standard_palettes[palette] # Build colormap index = [] colors = [] for group_num, group in enumerate(group_list): vals = groups[group] index.extend(vals) colors.extend([colormap[group_num]] * len(vals)) # build legend colors list legend.append((str(group), colormap[group_num])) data['colormap'] = pd.Series(colors, index=index) # Build the actual graph page source = ColumnDataSource(data=data) callback = CustomJS(args=dict(source=source), code=""" var data = source.get('data'); var title = cb_obj.get('title'); var value = cb_obj.get('value'); if(title == "X-axis:") { data['x'] = data[value]; } else { data['y'] = data[value]; } source.trigger('change'); """) select_x = Select(title="X-axis:", value=data.columns[0], options=list(data.columns), callback=callback) select_y = Select(title="Y-axis:", value=data.columns[0], options=list(data.columns), callback=callback) output_file(output_fp, title=title) p = figure(title=title) p.xaxis.axis_label = 'X-axis' p.yaxis.axis_label = 'Y-axis' # Create graph itself, with colormap and color legend if needed if color_by is not None: p.circle(x=data[data.columns[0]], y=data[data.columns[0]], size=10, source=source, legend=False, color=data['colormap'], fill_alpha=0, line_width=2) # Add legend spots for title, color in legend[::-1]: p.circle(x=[], y=[], size=10, color=color, legend=title, fill_alpha=0, line_width=2) p.legend.orientation = legend_pos else: p.circle(x=data[data.columns[0]], y=data[data.columns[0]], size=10, source=source, legend=False, fill_alpha=0, line_width=2) layout = vform(p, select_x, select_y) show(layout)
# pupa_plot.line(larva_4_sr.index, larva_4_sr, pupa_plot.line(larva_4_period.index, larva_4_period[runs.columns[1]], line_width=line_width, color=colors[4], line_dash='dotted', legend='Heterozygous Cannibalism:{}'.format(np.round(param.cannib_4, 3))) # pupa_plot.line(larva_4_ss.index, larva_4_ss, pupa_plot.line(larva_4_period.index, larva_4_period[runs.columns[2]], line_width=line_width, color=colors[4], line_dash='dashed', legend='Susceptible Cannibalism:{}'.format(np.round(param.cannib_4, 3))) pupa_plot.legend.location = "bottom_right" pupa_plot.legend.label_text_font_size = legend_font_size pupa_plot.title.text_font_size = title_font_size pupa_plot.yaxis.axis_line_width = axis_line_width pupa_plot.xaxis.axis_line_width = axis_line_width pupa_plot.yaxis.axis_label_text_font_size = axis_font_size pupa_plot.xaxis.axis_label_text_font_size = axis_font_size pupa_plot.yaxis.major_label_text_font_size = axis_tick_font_size pupa_plot.xaxis.major_label_text_font_size = axis_tick_font_size pupa_plot.ygrid.grid_line_width = grid_line_width pupa_plot.xgrid.grid_line_width = grid_line_width layout = lay.column(allele_plot, pupa_plot) plt.show(layout)
y = y.astype('float32') # load json and create model json_file = open('regression_future.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("regression_future.h5") print("Loaded model from disk") loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mean_squared_error']) y_pred = model.predict(X) y_pred_actuals = scalery.inverse_transform(y_pred) x_axis = list(range(history_points, len(y_pred) + 1)) s1 = figure(title='Open Price', plot_width=1000, plot_height=300, x_axis_label="datetime", y_axis_label='Open Price') s1.line(x_axis, y_pred_actuals[:, 0], line_width=2, color='red') s1.line(range(1, len(info.exp_C) + 1), info.exp_C, line_width=2, color='blue') show(s1)
#Imports: numpy, matplotlib, bokeh import numpy as np import matplotlib.pyplot as plt from bokeh.plotting import figure, output_file, show from bokeh.layouts import row #Generate some data for graphing x = np.linspace(0, 2 * np.pi, 50) y = np.sin(x) y2 = np.cos(x) y3 = -1 / 6 * x + 1 #Plot using matplotlib #plt.plot(x,y,'b',x,y2,'r',x,y3,'k') #plt.show() #Basic plot using Bokeh output_file("basics.html") #Output to static HTML file p = figure(title="Basic Example", x_axis_label='x', y_axis_label='y') p.line(x, y, line_width=5) # add a line renderer show(p) # show the results #Layout and share axis #output_file("layout.html") #Output to static HTML file #p = figure(title="Layout Example", x_axis_label='x', y_axis_label='y') #p.line(x, y, color="blue", line_width=2) # add a line renderer #p.line(x, y2, color="red", line_width=2) #p2 = figure(title="Layout Example 2", x_axis_label='x', y_axis_label='y', # x_range=p.x_range) #p2.line(x, y3, color="red", line_width=2) #show(row(p,p2)) # show the results
def plot_kde( density, lower, upper, density_q, xmin, xmax, ymin, ymax, gridsize, values, values2, rug, label, quantiles, rotated, contour, fill_last, plot_kwargs, fill_kwargs, rug_kwargs, contour_kwargs, contourf_kwargs, pcolormesh_kwargs, ax, legend, backend_kwargs, show, ): """Bokeh kde plot.""" if backend_kwargs is None: backend_kwargs = {} backend_kwargs = { **backend_kwarg_defaults( ("tools", "plot.bokeh.tools"), ("output_backend", "plot.bokeh.output_backend"), ("width", "plot.bokeh.figure.width"), ("height", "plot.bokeh.figure.height"), ), **backend_kwargs, } if ax is None: ax = bkp.figure(**backend_kwargs) if legend and label is not None: plot_kwargs["legend_label"] = label if values2 is None: if plot_kwargs is None: plot_kwargs = {} plot_kwargs.setdefault( "line_color", mpl_rcParams["axes.prop_cycle"].by_key()["color"][0]) if fill_kwargs is None: fill_kwargs = {} fill_kwargs.setdefault( "fill_color", mpl_rcParams["axes.prop_cycle"].by_key()["color"][0]) if rug: if rug_kwargs is None: rug_kwargs = {} rug_kwargs = rug_kwargs.copy() if "cds" in rug_kwargs: cds_rug = rug_kwargs.pop("cds") rug_varname = rug_kwargs.pop("y", "y") else: rug_varname = "y" cds_rug = ColumnDataSource({rug_varname: np.asarray(values)}) rug_kwargs.setdefault("size", 8) rug_kwargs.setdefault("line_color", plot_kwargs["line_color"]) rug_kwargs.setdefault("line_width", 1) rug_kwargs.setdefault("line_alpha", 0.35) if not rotated: rug_kwargs.setdefault("angle", np.pi / 2) if isinstance(cds_rug, dict): for _cds_rug in cds_rug.values(): if not rotated: glyph = Dash(x=rug_varname, y=0.0, **rug_kwargs) else: glyph = Dash(x=0.0, y=rug_varname, **rug_kwargs) ax.add_glyph(_cds_rug, glyph) else: if not rotated: glyph = Dash(x=rug_varname, y=0.0, **rug_kwargs) else: glyph = Dash(x=0.0, y=rug_varname, **rug_kwargs) ax.add_glyph(cds_rug, glyph) x = np.linspace(lower, upper, len(density)) if quantiles is not None: fill_kwargs.setdefault("fill_alpha", 0.75) fill_kwargs.setdefault("line_color", None) quantiles = sorted(np.clip(quantiles, 0, 1)) if quantiles[0] != 0: quantiles = [0] + quantiles if quantiles[-1] != 1: quantiles = quantiles + [1] for quant_0, quant_1 in zip(quantiles[:-1], quantiles[1:]): idx = (density_q > quant_0) & (density_q < quant_1) if idx.sum(): patch_x = np.concatenate( (x[idx], [x[idx][-1]], x[idx][::-1], [x[idx][0]])) patch_y = np.concatenate( (np.zeros_like(density[idx]), [density[idx][-1]], density[idx][::-1], [0])) if not rotated: ax.patch(patch_x, patch_y, **fill_kwargs) else: ax.patch(patch_y, patch_x, **fill_kwargs) else: if fill_kwargs.get("fill_alpha", False): patch_x = np.concatenate((x, [x[-1]], x[::-1], [x[0]])) patch_y = np.concatenate((np.zeros_like(density), [density[-1]], density[::-1], [0])) if not rotated: ax.patch(patch_x, patch_y, **fill_kwargs) else: ax.patch(patch_y, patch_x, **fill_kwargs) if not rotated: ax.line(x, density, **plot_kwargs) else: ax.line(density, x, **plot_kwargs) else: if contour_kwargs is None: contour_kwargs = {} if contourf_kwargs is None: contourf_kwargs = {} if pcolormesh_kwargs is None: pcolormesh_kwargs = {} g_s = complex(gridsize[0]) x_x, y_y = np.mgrid[xmin:xmax:g_s, ymin:ymax:g_s] if contour: scaled_density, *scaled_density_args = _scale_axis(density) contour_generator = _contour.QuadContourGenerator( x_x, y_y, scaled_density, None, True, 0) if "levels" in contour_kwargs: levels = contour_kwargs.get("levels") elif "levels" in contourf_kwargs: levels = contourf_kwargs.get("levels") else: levels = 11 if isinstance(levels, Integral): levels_scaled = np.linspace(0, 1, levels) levels = _rescale_axis(levels_scaled, scaled_density_args) else: levels_scaled_nonclip = _scale_axis(np.asarray(levels), scaled_density_args) levels_scaled = np.clip(levels_scaled_nonclip, 0, 1) cmap = contourf_kwargs.pop("cmap", "viridis") if isinstance(cmap, str): cmap = get_cmap(cmap) if isinstance(cmap, Callable): colors = [ rgb2hex(item) for item in cmap(np.linspace(0, 1, len(levels_scaled) + 1)) ] else: colors = cmap contour_kwargs.update(contourf_kwargs) contour_kwargs.setdefault("line_color", "black") contour_kwargs.setdefault("line_alpha", 0.25) contour_kwargs.setdefault("fill_alpha", 1) for i, (level, level_upper, color) in enumerate( zip(levels_scaled[:-1], levels_scaled[1:], colors[1:])): if not fill_last and (i == 0): continue vertices, _ = contour_generator.create_filled_contour( level, level_upper) for seg in vertices: ax.patch(*seg.T, fill_color=color, **contour_kwargs) if fill_last: ax.background_fill_color = colors[0] ax.xgrid.grid_line_color = None ax.ygrid.grid_line_color = None ax.x_range = Range1d(xmin, xmax) ax.y_range = Range1d(ymin, ymax) else: cmap = pcolormesh_kwargs.pop("cmap", "viridis") if isinstance(cmap, str): cmap = get_cmap(cmap) if isinstance(cmap, Callable): colors = [ rgb2hex(item) for item in cmap(np.linspace(0, 1, 256)) ] else: colors = cmap ax.image(image=[density.T], x=xmin, y=ymin, dw=(xmax - xmin) / density.shape[0], dh=(ymax - ymin) / density.shape[1], palette=colors, **pcolormesh_kwargs) ax.x_range.range_padding = ax.y_range.range_padding = 0 if backend_show(show): bkp.show(ax, toolbar_location="above") return ax
def accelplot(quakelist, starttime_s, endtime_s, time_s, timeunits, accelunits, accelx, accely, accelz): quakecounter = 0 for j in quakelist: quakecounter = quakecounter + 1 if quakecounter > 2: continue print('Generating spectrogram for earthquake ' + str(quakecounter) + '/' + str(len(quakelist)) + ':') print(' ' + str(round(j[2])) + 'M ' + j[0]) quaketime = j[3] quaketime = datetime.utcfromtimestamp(quaketime) quaketime = quaketime.strftime("%Y-%m-%d %H:%M:%S UTC") windowstart = (j[5]) - 6 #start spectrogram 1min before arrival windowend = windowstart + 24 #end spectrogram after 4min if windowstart < starttime_s: windowstart = starttime_s #if window starts before data, cut window to data if windowend > endtime_s: windowend = endtime_s #if window ends before data, cut window to data windowindices = [] for kindex, k in enumerate( time_s): #find indices of times in window for if k <= windowstart: continue if k >= windowend: continue #print(windowend) windowindices.append(kindex) window_accelx = accelx[ windowindices] #cut down arrays to times in the window window_accely = accely[windowindices] window_accelz = accelz[windowindices] window_time_s = [] for row in windowindices: window_time_s.append(time_s[row]) def interpolateaccel(axis): f = interpolate.interp1d( window_time_s, axis, kind='cubic') #make interpolation function timelength = int( (windowend - windowstart) * 1000) #max(window_time_s) timenew = np.linspace(window_time_s[0], window_time_s[-1], timelength) #generate even time values accelaxisnew = f(timenew) #actually use interpolation function return accelaxisnew f = interpolate.interp1d(window_time_s, window_accelx, kind='cubic') #make interpolation function timelength = int((windowend - windowstart) * 1000) timenew = np.linspace(window_time_s[0], window_time_s[-1], timelength) #generate even time values accelxnew = f(timenew) #actually use interpolation function #accelxnew = interpolateaccel(window_accelx) accelynew = interpolateaccel(window_accely) accelznew = interpolateaccel(window_accelz) timenew = np.linspace(-6000, 18000, 24000) windowname = j[0] #set name of window to location of quake windowname = windowname.replace( " ", "") #strip whitespace to make a valid filename #windowname = windowname + '_' + j[3] + '_' windowfilename = windowname + '.png' #generate filename def accelplot2(axis, axisname, axisnumber): output_file( str(round(j[2])) + 'M_' + windowname + axisname + '_acceleration.html') axistop = max(axis) + 0.2 axisbot = min(axis) - 0.2 medianaxis = statistics.median(axis) stddevaxis = statistics.stdev(axis) #axishigh = medianaxis + (2 * stddevaxis) #axislow = medianaxis - (2 * stddevaxis) axishigh = medianaxis + stddevaxis axislow = medianaxis - stddevaxis p = figure(plot_width=1900, plot_height=900, tools="pan,box_zoom,reset,save", y_range=[axisbot, axistop], title=(axisname + ' Acceleration'), x_axis_label='Time (' + timeunits + ')', y_axis_label='Acceleration (' + accelunits + ')') p.line(timenew, timenew, legend="y=x") p.circle(timenew, timenew, legend="time", fill_color="white", size=8) p.line(timenew, axis, legend=axisname, line_width=1) p.line(timenew, statistics.mean(axis), legend='average value', line_color="red") p.line(timenew, axishigh, line_color="orange", legend='1 std deviation') p.line(timenew, axislow, line_color="orange") show(p) def accelplot3(): #set output file name output_file( str(round(j[2])) + 'M_' + windowname + '_acceleration.html') #create three plots p1 = figure( plot_width=1800, plot_height=250, title='x', ) p1.line(timenew, accelxnew, legend='x', line_width=1) p2 = figure( plot_width=1800, plot_height=250, title='y', ) p2.line(timenew, accelynew, legend='y', line_width=1) p3 = figure( plot_width=1800, plot_height=250, title='z', ) p3.line(timenew, accelznew, legend='z', line_width=1) #grid = gridplot([[p1],[p2],[p3]]) accelgrid = gridplot([p1, p2, p3], ncols=1) #show(grid) return accelgrid accelfig = plt.figure(figsize=(12, 6)) def accelplot(axis, axisname, axisnumber): #plot acceleration graphs in a column plt.subplot(3, 1, axisnumber) plt.plot(timenew, axis, linewidth=0.5) plt.title(axisname + ' Acceleration') plt.xlabel('Time (' + timeunits + ')') plt.ylabel('Acceleration (' + accelunits + ')') axistop = max(axis) + 0.2 #axistop = 2 axisbot = min(axis) - 0.2 #axisbot = -2 plt.ylim(axisbot, axistop) plt.set_cmap('magma') #accelplot2(accelxnew,'x',1) #call accelplot #accelplot2(accelynew,'y',2) #accelplot2(accelznew,'z',3) accelgrid = accelplot3() #plt.suptitle(str(j[2]) + 'M ' + j[0] + '\n' + quaketime) # main plot title #plt.tight_layout() #add padding between subplots #plt.subplots_adjust(top=0.88) #plt.savefig(str(round(j[2])) + 'M_' + windowname + '_acceleration.png', dpi = 300) #plt.close('all') #compute and plot fft of data in window #start_time = 80 # seconds #end_time = 90 # seconds accelspec = plt.figure(figsize=(8, 10)) def fftaccel(axis, axisname, axisnumber): sampling_rate = 1000 # Hz N = 24000 # array size #accelxshort = accelxnew[(start_time*sampling_rate):(end_time*sampling_rate)] # Nyquist Sampling Criteria (for interpolated data) T = 1 / sampling_rate xfft = np.linspace(0.0, 1.0 / (2.0 * T), int(N / 2)) # FFT algorithm yr = fft(axis) # "raw" FFT with both + and - frequencies yfft = 2 / N * np.abs(yr[0:np.int(N / 2)]) # positive freqs only # Plotting the results plt.subplot(3, 2, axisnumber) plt.plot(xfft, yfft) plt.xlabel('Frequency (Hz)') plt.ylabel('Vibration (g)') #plt.xlim(0,20) plt.title(axisname + ' Frequency Spectrum') #plt.savefig(windowname + '_' + axisname + '_freq.png') plt.subplot(3, 2, axisnumber + 1) f, t2, Sxx = signal.spectrogram(axis, 1000, nperseg=1500) plt.pcolormesh(t2, f, np.log(Sxx)) plt.set_cmap('inferno') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.title(axisname + ' Spectrogram') plt.ylim(0, 20) #plt.savefig(windowname + '_' + axisname + '_spec.png') return xfft, yfft, yr def fftaccel2(axis, axisname, axisnumber): sampling_rate = 1000 # Hz N = 24000 # array size #accelxshort = accelxnew[(start_time*sampling_rate):(end_time*sampling_rate)] # Nyquist Sampling Criteria (for interpolated data) T = 1 / sampling_rate xfft = np.linspace(0.0, 1.0 / (2.0 * T), int(N / 2)) # FFT algorithm yr = fft(axis) # "raw" FFT with both + and - frequencies yfft = 2 / N * np.abs(yr[0:np.int(N / 2)]) # positive freqs only #plotname = 'p' + axisname #plotname = figure(plot_width=1800,plot_height=250,title=axisname) f, t2, Sxx = signal.spectrogram(axis, 1000, nperseg=1000) i = 0 df_spectogram = pd.DataFrame() for freq in range(f.shape[0]): for time in range(t2.shape[0]): df_spectogram.loc[i] = [f[freq], t2[time], Sxx[freq][time]] i = i + 1 TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" PALETTE = [ '#081d58', '#253494', '#225ea8', '#1d91c0', '#41b6c4', '#7fcdbb', '#c7e9b4', '#edf8b1', '#ffffd9' ] mapper = LinearColorMapper(palette=PALETTE, low=np.min(Sxx), high=np.max(Sxx)) spectogram_figure = figure(title="Spectogram", x_axis_location="below", plot_width=900, plot_height=400, tools=TOOLS) spectogram_figure.background_fill_color = "#eaeaea" spectrogram_source = ColumnDataSource( data=dict(Sxx=df_spectogram['Sxx'], Frequency=df_spectogram['Frequency'], Time=df_spectogram['Time'])) #spectogram_figure.circle(x="Time", y="Frequency", source=spectrogram_source, fill_color={'field': 'Sxx', 'transform': mapper}, line_color=None) spectrogram_figure.quad(color == { 'field': 'Sxx', 'transform': mapper }, source=spectrogram_source, width="Time", height="Frequency") spectrogram_figure.grid.visible = False show(spectrogram_figure) def fftaccel3(axis, axisname): sampling_rate = 1000 # Hz #N = windowend - windowstart # array size N = 24000 #accelxshort = accelxnew[(start_time*sampling_rate):(end_time*sampling_rate)] # Nyquist Sampling Criteria (for interpolated data) T = 1 / sampling_rate xfft = np.linspace(0.0, 1.0 / (2.0 * T), int(N / 2)) # FFT algorithm yr = fft(axis) # "raw" FFT with both + and - frequencies yfft = 2 / N * np.abs(yr[0:np.int(N / 2)]) # positive freqs only p1 = figure(tooltips=[("x", "$x"), ("y", "$y")]) p1.x_range = Range1d(0, 200) #p.line(timenew,np.real(yr)) p1.line(xfft, yfft) #show(p1) f, t2, Sxx = signal.spectrogram(axis, fs=1000, nperseg=1000) p = figure(tooltips=[("time", "$t2"), ("freq.", "$f"), ("value", "@image")]) p.x_range.range_padding = p.y_range.range_padding = 0 p.image(image=[np.log(Sxx)], x=0, y=0, dw=10, dh=10, palette="Spectral11") output_file( str(round(j[2])) + 'M_' + windowname + axisname + '_fft.html') fftgrid = gridplot([p1, p], ncols=2) #show(grid) return fftgrid def meshgrid_example(): #import numpy as np #from bokeh.plotting import figure, show, output_file N = 500 x = np.linspace(0, 10, N) y = np.linspace(0, 10, N) xx, yy = np.meshgrid(x, y) d = np.sin(xx) * np.cos(yy) p = figure(tooltips=[("x", "$x"), ("y", "$y"), ("value", "@image")]) p.x_range.range_padding = p.y_range.range_padding = 0 # must give a vector of image data for image parameter p.image(image=[d], x=0, y=0, dw=10, dh=10, palette="Spectral11") output_file("image.html", title="image.py example") show(p) # open a browser fftgrid = fftaccel3(accelxnew, 'x') #fftaccel(accelxnew,'x',1) l = gridplot([[accelgrid], [fftgrid]]) show(l)
def cluster_usage(): from bokeh.io import show, output_notebook, push_notebook from bokeh.resources import CDN from bokeh.plotting import figure from bokeh.models import ( ColumnDataSource, HoverTool, LinearColorMapper, BasicTicker, ColorBar, ) output_notebook(resources=CDN) # Initial values source = ColumnDataSource( data={ "node_ip_address": ['127.0.0.1'], "time": ['0.5'], "num_tasks": ['1'], "length": [1] }) # Define the color schema colors = [ "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d" ] mapper = LinearColorMapper(palette=colors, low=0, high=2) TOOLS = "hover, save, xpan, box_zoom, reset, xwheel_zoom" # Create the plot p = figure(title="Cluster Usage", y_range=list(set(source.data['node_ip_address'])), x_axis_location="above", plot_width=900, plot_height=500, tools=TOOLS, toolbar_location='below') # Format the plot axes p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "10pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = np.pi / 3 # Plot rectangles p.rect(x="time", y="node_ip_address", width="length", height=1, source=source, fill_color={ "field": "num_tasks", "transform": mapper }, line_color=None) # Add legend to the side of the plot color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="8pt", ticker=BasicTicker(desired_num_ticks=len(colors)), label_standoff=6, border_line_color=None, location=(0, 0)) p.add_layout(color_bar, "right") # Define hover tool p.select_one(HoverTool).tooltips = [ ("Node IP Address", "@node_ip_address"), ("Number of tasks running", "@num_tasks"), ("Time", "@time") ] # Define the axis labels p.xaxis.axis_label = "Time in seconds" p.yaxis.axis_label = "Node IP Address" handle = show(p, notebook_handle=True) workers = ray.global_state.workers() # Function to update the heat map def heat_map_update(abs_earliest, abs_latest, abs_num_tasks, tasks): if len(tasks) == 0: return earliest = time.time() latest = 0 node_to_tasks = dict() # Determine which task has the earlest start time out of the ones # passed into the update function for task_id, data in tasks.items(): if data["score"] > latest: latest = data["score"] if data["score"] < earliest: earliest = data["score"] worker_id = data["worker_id"] node_ip = workers[worker_id]["node_ip_address"] if node_ip not in node_to_tasks: node_to_tasks[node_ip] = {} node_to_tasks[node_ip][task_id] = data nodes = [] times = [] lengths = [] num_tasks = [] for node_ip, task_dict in node_to_tasks.items(): left, right, top = compute_utilizations(earliest, latest, abs_num_tasks, task_dict, 100, True) for (l, r, t) in zip(left, right, top): nodes.append(node_ip) times.append((l + r) / 2) lengths.append(r - l) num_tasks.append(t) # Set the y range of the plot to be the node IP addresses p.y_range.factors = list(set(nodes)) mapper.low = min(min(num_tasks), 0) mapper.high = max(max(num_tasks), 1) # Update plot with new data based on slider and text box values source.data = { "node_ip_address": nodes, "time": times, "num_tasks": num_tasks, "length": lengths } push_notebook(handle=handle) get_sliders(heat_map_update)
# THe output_file object is the file the graph will be made made in output_file("line.html") x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5] # The figure object is the graph that you're going to be working in p = figure(plot_width=400, plot_height=400) # Label the axis p.xaxis.axis_label = 'Time' p.yaxis.axis_label = 'Value' # add a line renderer p.line(x, y, line_width=2) # the show object is used for showing the graph show(p) """ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Different types of plots Line, Circle(Scatter), Time series, ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- """ # Create a green line p = figure(plot_width=400, plot_height=400) p.line(x, y, line_width=2, line_color='green') # show(p) # plot a circle plot p = figure(plot_width=400, plot_height=400) p.circle(x, y, size=10, line_color='red', fill_color='blue') # show(p)
def plot_utilization(): # Create the Bokeh plot time_series_fig = figure( title="CPU Utilization", tools=["save", "hover", "wheel_zoom", "box_zoom", "pan"], background_fill_color="#FFFFFF", x_range=[0, 1], y_range=[0, 1]) # Create the data source that the plot will pull from time_series_source = ColumnDataSource( data=dict(left=[], right=[], top=[])) # Plot the rectangles representing the distribution time_series_fig.quad(left="left", right="right", top="top", bottom=0, source=time_series_source, fill_color="#B3B3B3", line_color="#033649") # Label the plot axes time_series_fig.xaxis.axis_label = "Time in seconds" time_series_fig.yaxis.axis_label = "Number of CPUs used" handle = show(gridplot(time_series_fig, ncols=1, plot_width=500, plot_height=500, toolbar_location="below"), notebook_handle=True) def update_plot(abs_earliest, abs_latest, abs_num_tasks, tasks): num_buckets = 100 left, right, top = compute_utilizations(abs_earliest, abs_latest, abs_num_tasks, tasks, num_buckets) time_series_source.data = { "left": left, "right": right, "top": top } x_range = (max(0, min(left)) if len(left) else 0, max(right) if len(right) else 1) y_range = (0, max(top) + 1 if len(top) else 1) # Define the axis ranges x_range = helpers._get_range(x_range) time_series_fig.x_range.start = x_range.start time_series_fig.x_range.end = x_range.end y_range = helpers._get_range(y_range) time_series_fig.y_range.start = y_range.start time_series_fig.y_range.end = num_cpus # Push the updated data to the notebook push_notebook(handle=handle) get_sliders(update_plot)
b.image(image=[np.flipud(x_test[index])], x=0, y=0, dw=14, dh=14, palette="Spectral11") index = 11 c = figure(title='Original Image at index 11', plot_width=300, plot_height=300) c.image(image=[np.flipud(x_test[index])], x=0, y=0, dw=14, dh=14, palette="Spectral11") index = 16 d = figure(title='Original Image at index 16', plot_width=300, plot_height=300) d.image(image=[np.flipud(x_test[index])], x=0, y=0, dw=14, dh=14, palette="Spectral11") # using grid plot to show all of the bokeh visualisation in one html file ( same Plot ) h = gridplot([[p, q, r, s], [a, b, c, d], [acc, val, f]]) output_file('graph.html', ) # Show 2 of the images from the training dataset in a grid show(h)