def test_zeppelin_with_notebook_handle(self): load_zeppelin_args = (Resources(), True, True, 1000, 'zeppelin') io.output_notebook(*load_zeppelin_args) with pytest.raises(Exception) as ex: p = Plot() io.show(p, notebook_handle=True) assert "Zeppelin doesn't support notebook_handle." == str(ex.value)
def plot_matplobli(self): from omicexperiment.plotting.plot_bokeh import plot_interactive fig = plot_interactive(self.data_df) from bokeh.io import show, output_notebook output_notebook() show(fig) return fig
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 test_args(self, mock_load_notebook): # call _install_notebook_hook to register the patched `bokeh.io.load_notebook` from bokeh.io import _install_notebook_hook _install_notebook_hook() load_jupyter_args = (Resources(), True, True, 1000) io.output_notebook(*load_jupyter_args) self._check_func_called(io._state.output_notebook, ('jupyter',), {}) self._check_func_called(mock_load_notebook, load_jupyter_args, {'notebook_type': 'jupyter'})
def test_noarg(self, mock_load_notebook): # call _install_notebook_hook to register the patched `bokeh.io.load_notebook` from bokeh.io import _install_notebook_hook _install_notebook_hook() default_load_jupyter_args = (None, False, False, 5000) io.output_notebook() self._check_func_called(io._state.output_notebook, ('jupyter',), {}) self._check_func_called(mock_load_notebook, default_load_jupyter_args, {'notebook_type': 'jupyter'})
def cdp_plot(counts_by_ref, seq1, seq2, nt, onscreen, file_fig, file_name, pub, bok): """ Scatter plot of alignments to references :param counts_by_ref: dict of (x,y) counts for each reference (dict) :param seq1: x label (str) :param seq2: y label (str) :param nt: aligned read length (int) :param file_fig: output plot to pdf (bool) :param file_name: output filename (str) :param onscreen: show plot on screen (bool) :param pub: publication plot (bool) """ results_list = [] # list of results for counts in counts_by_ref.values(): results_list.append((counts[0] + 0.01, counts[1] + 0.01)) # hack that allows zero values to be plotted on a log scale results_list = sorted(results_list) _max = max(results_list[-1][0], results_list[-1][1]) # sets up max x and y scale values _max += float(_max / 2) if not bok: plt.scatter(*list(zip(*results_list)), s=10, color=_nt_colour(nt), marker='o', label="{0} nt".format(nt)) arrow(0.1, 0.1, _max, _max, color='r') xscale('log') yscale('log') xlim(0.1, _max) ylim(0.1, _max) if pub: _pub_plot() else: plt.legend(loc='upper left', fancybox=True, framealpha=0.5) xlabel(seq1) ylabel(seq2) _shared_plot(file_fig, file_name, onscreen) else: output_notebook() x_vals = [] y_vals = [] for point in counts_by_ref.values(): x_vals.append(point[0]+ 0.01) y_vals.append(point[1]+ 0.01) p = figure(plot_width=600, plot_height=600, x_axis_type="log", y_axis_type="log", x_range=(0.1, _max), y_range=(0.1, _max)) p.circle(x_vals, y_vals, size=5, color=_nt_colour(nt), alpha=0.9) p.line([0.1,_max],[0.1,_max]) p.xaxis.axis_label = seq1 p.yaxis.axis_label = seq2 show(p)
def den_multi_plot_21_22_24(x_ref, y_fwd_smoothed_21, y_rvs_smoothed_21, y_fwd_smoothed_22, y_rvs_smoothed_22, y_fwd_smoothed_24, y_rvs_smoothed_24, file_fig, file_name, onscreen, x_label, plot_y_lim, pub=False, bok=False): """ 21, 22 and 24nt combined alignment profile :param x_ref: x co-ords (list(int)) :param y_fwd_smoothed_21: 21 nt positive y co-ords (list(float)) :param y_rvs_smoothed_21: 21 nt negative y co-ords (list(float)) :param y_fwd_smoothed_22: 22 nt positive y co-ords (list(float)) :param y_rvs_smoothed_22: 22 nt negative y co-ords (list(float)): :param y_fwd_smoothed_24: 24 nt positive y co-ords (list(float)) :param y_rvs_smoothed_24: 24 nt negative y co-ords (list(float)) :param nt: aligned read length (int) :param file_fig: output plot to pdf (bool) :param file_name: output filename (str) :param onscreen: show plot on screen (bool) :param x_label: x label (str) :param plot_y_lim: + / - y-axis limit (int) :param pub: publication plot (bool) """ if not bok: plt.plot(x_ref, y_fwd_smoothed_21, color='#00CC00', label='21 nt', lw=2) plt.plot(x_ref, y_rvs_smoothed_21, color='#00CC00', lw=2) plt.plot(x_ref, y_fwd_smoothed_22, color='#FF3399', label='22 nt', lw=2) plt.plot(x_ref, y_rvs_smoothed_22, color='#FF3399', lw=2) plt.plot(x_ref, y_fwd_smoothed_24, color='#3333FF', label='24 nt', lw=2) plt.plot(x_ref, y_rvs_smoothed_24, color='#3333FF', lw=2) axhline(y=0) if pub: _pub_plot() else: # no_publication xlabel(x_label) ylabel('Reads per million reads') plt.rc('font') #remove? plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=3, mode="expand", borderaxespad=0., fontsize=12) _generate_profile(file_fig, file_name, onscreen, plot_y_lim) else: ##Test bokeh package output_notebook() if plot_y_lim!=0: p = figure(plot_width=700, plot_height=400, y_range=(-plot_y_lim, plot_y_lim)) else: p = figure(plot_width=700, plot_height=400) p.line(x_ref, y_fwd_smoothed_21, line_width=2, color="#00CC00", legend= "21 nt", alpha=0.9) p.line(x_ref, y_rvs_smoothed_21, line_width=2, color="#00CC00", alpha=0.9) p.line(x_ref, y_fwd_smoothed_22, line_width=2, color="#FF3399", legend= "22 nt", alpha=0.9) p.line(x_ref, y_rvs_smoothed_22, line_width=2, color="#FF3399", alpha=0.9) p.line(x_ref, y_fwd_smoothed_24, line_width=2, color="#3333FF", legend= "24 nt", alpha=0.9) p.line(x_ref, y_rvs_smoothed_24, line_width=2, color="#3333FF", alpha=0.9) show(p)
def timeplot(data): #input data is a DataFrame time = pd.DatetimeIndex(data['ltime']) #String list to store column names from the third column of the dataframe columns = [] for x in data.columns[1:]: columns.append(x) #change string to float in the data for x in columns[0:(len(columns)-2)]: if (type(data[x][0]) is str): for i in range(len(data[x])): data[x][i] = float(data[x][i].replace(',','')) output_notebook() y = data[columns[1]] x = time p = Figure(x_axis_type = 'datetime', title = "TimeSeries Plotting") source = ColumnDataSource(data=dict(x=x, y=y, d=data)) #create a new columndatasoure to pass column name to CustomJS source2 = ColumnDataSource(data = dict(columns = columns)) p.line('x', 'y', source = source) p.xaxis.axis_label = "Time" p.yaxis.axis_label = "Selected Y" callback = CustomJS(args = dict(source = source, columns=source2), code=""" var data = source.get('data'); var columns = columns.get('data')['columns']; var f = cb_obj.get('value'); y = data['y']; console.log('y'); console.log(y); var d = data['d']; //get the index of the chosen column from the widget for(i = 0; i<columns.length;i++){ if(f[0]==columns[i]){ index = i; } } //make the column transpose since the CustomJS //takes dataframe as an array of arrays which is //a row of the DataFrame for (i = 0; i < d.length; i++) { y[i] = d[i][index+1]; } console.log('y'); console.log(y.length); source.trigger('change'); """) select = MultiSelect(title="Y_Option:", value=[columns[0]], options=columns, callback=callback) layout = vform(select, p) show(layout)
def visualize_segments(self): output_notebook() g = figure(x_axis_type="datetime") for day in pd.date_range(self.date_start, self.date_end - timedelta(days=1), normalize=True): mask = (self.newdf.date >= day) & (self.newdf.date < (day + timedelta(days=1))) g.line(self.times, self.newdf.loc[mask][2]) g.circle([s for s, t in self.segs], [10 for s, t in self.segs], color="red") show(g)
def doRender(self, handlerId): clientHasBokeh = self.options.get("nostore_bokeh", "false") == "true" if not clientHasBokeh: output_notebook(hide_banner=True) data = self.entity.getNextData() if data is None: return x = None y = None if isinstance(data, (list,np.ndarray)): x = list(range(self.windowSize)) if self.glyphRenderer is None else self.glyphRenderer.data_source.data['x'] y = data if self.glyphRenderer is None else self._concatArrays(self.glyphRenderer.data_source.data['y'],data) if len(y) < self.windowSize: y = [0]*(self.windowSize-len(y)) + y elif len(y) > self.windowSize: y = self._delWindowElements(y) elif isinstance(data, pandas.core.frame.DataFrame): pd = pd.drop(pd.index[[0]]) #pd.index = list(range(len(pd.index))) pd['x'] = list(range(len(pd.index))) else: x = data[0] y = data[1] if self.glyphRenderer is None: self.glyphRenderer = self.createGlyphRenderer( self.figure, x, y ) else: self.updateGlyphRenderer( self.figure, self.glyphRenderer) if self.glyphRenderer is None: print("Error: no glyphRenderer found") return self.glyphRenderer.data_source.data['x'] = x self.glyphRenderer.data_source.data['y'] = y if not self.comms_handle: state = curstate() doc = state.document if self.figure not in doc.roots: doc.add_root(self.figure) self.comms_handle = show_doc(self.figure, state, notebook_handle=True) else: push_notebook(handle = self.comms_handle)
def __init__(self, data,name='scatter_plot', **kwargs): output_notebook(hide_banner=True) self._data = data mapper = PlotMapper(data, button_type='ddown',button_pos='top', name='mapper', mode='interactive') if isinstance(data, pd.DataFrame): tooltip = BokehDataFrameTooltip(data, name='tooltip', mode='interactive') ToggleMenu.__init__(self, children=[mapper,tooltip], name=name,**kwargs) else: ToggleMenu.__init__(self, children=[mapper], name=name,**kwargs) self.buttons.value = 'mapper' self.mapper.buttons.value = 'y' if isinstance(self._data,pd.DataFrame): self.mapper.y.data_slicer.columns_slicer.dd_selector.value =\ self.mapper.y.data_slicer.columns_slicer.dd_selector.target.options[1] self.observe(self.update) self.update_source_df() self.init_plot() self.update()
def output_chart(issues_df,output_mode='static'): import datetime import bokeh from bokeh.models import HoverTool # Add timestamp to title issues_chart = Bar(issues_df, label='value_delivered', values='status', agg='count', stack='status', title=ISSUES_TITLE+" (Updated "+datetime.datetime.now().strftime('%m/%d/%Y')+")", xlabel="Value Delivered",ylabel="Number of Use Cases", legend='top_right', tools='hover', color=brewer["GnBu"][3] ) issues_chart.plot_width = DESTINATION_FRAME_WIDTH - (HTML_BODY_MARGIN * 2) issues_chart.plot_height = DESTINATION_FRAME_HEIGHT - (HTML_BODY_MARGIN * 2) issues_chart.logo = None issues_chart.toolbar_location = None hover = issues_chart.select(dict(type=HoverTool)) hover.tooltips = [ ("Value Delivered", "$x")] #--- Configure output --- reset_output() if output_mode == 'static': # Static file. CDN is most space efficient output_file(ISSUES_FILE, title=ISSUES_TITLE, autosave=False, mode='cdn', root_dir=None ) # Generate file save(issues_chart,filename=ISSUES_FILE) elif output_mode == 'notebook': output_notebook() # Show inline show(issues_chart) else: # Server (using internal server IP, rather than localhost or external) session = bokeh.session.Session(root_url = BOKEH_SERVER_IP, load_from_config=False) output_server("ddod_chart", session=session) show(issues_chart)
def mc_init_notebook(plot_lib='bokeh', i=1): lib = plot_lib.lower() if lib == 'bokeh': if bp is None: raise ImportError('bokeh') if i: from bokeh.io import output_notebook output_notebook() environ.notebook = 2 elif lib == 'matplotlib': if plt is None: raise ImportError('matplotlib') plt.rcParams['figure.figsize'] = (15, 12) plt.rcParams['font.family'] = 'serif' plt.rcParams['font.size'] = 20 plt.rcParams['font.serif'] = 'Times New Roman' plt.rcParams['legend.scatterpoints'] = 1 plt.rcParams['legend.handlelength'] = 0 environ.notebook = 1 else: raise ValueError('expected bokeh or matplotlib, got {0!r}'.format(plot_lib))
def den_plot(x_ref, y_fwd_smoothed, y_rvs_smoothed, nt, file_fig, file_name, onscreen, x_label, plot_y_lim, pub=False, bok=False): """ Single alignment profile :param x_ref: x co-ords (list(int)) :param y_fwd_smoothed: positive y co-ords (list(float)) :param y_rvs_smoothed: negative y co-ords (list(float)) :param nt: aligned read length (int) :param file_fig: output plot to pdf (bool) :param file_name: output filename (str) :param onscreen: show plot on screen (bool) :param x_label: x label (str) :param plot_y_lim: + / - y-axis limit (int) :param pub: publication plot (bool) """ if not bok: plt.plot(x_ref, y_fwd_smoothed, color=_nt_colour(nt), label='{0} nt'.format(nt), lw=2) plt.plot(x_ref, y_rvs_smoothed, color=_nt_colour(nt), lw=2) axhline(y=0) if pub: _pub_plot() else: xlabel(x_label) ylabel('Reads per million reads') plt.legend(loc='best', fancybox=True, framealpha=0.5) _generate_profile(file_fig, file_name, onscreen, plot_y_lim) else: output_notebook() if plot_y_lim!=0: p = figure(plot_width=700, plot_height=400, y_range=(-plot_y_lim, plot_y_lim)) else: p = figure(plot_width=700, plot_height=400) p.line(x_ref, y_fwd_smoothed, line_width=2, color=_nt_colour(nt), legend='{0} nt'.format(nt), alpha=0.9) p.line(x_ref, y_rvs_smoothed, line_width=2, color=_nt_colour(nt), alpha=0.9) show(p)
# Standard Library import re # Third Party import numpy as np from bokeh.io import output_notebook, push_notebook, show from bokeh.layouts import column, row from bokeh.models import ColumnDataSource, CustomJS, Div, HoverTool from bokeh.models.glyphs import Circle, Line, Quad from bokeh.plotting import figure, show # First Party from smdebug.profiler.utils import TimeUnits output_notebook(hide_banner=True) class TimelineCharts: def __init__( self, system_metrics_reader, framework_metrics_reader, starttime=0, endtime=None, select_dimensions=[".*"], select_events=[".*"], x=1000, show_workers=True, ): self.select_dimensions = select_dimensions
def class_plot(self, change): global global_handle nx = len(self.xaxis.value) ny = len(self.yaxis.value) selected = self.plot_selection.value if nx * ny > 0: reset_output() output_notebook() p_all = [] for xxa in self.xaxis.value: for iax, yya in enumerate(self.yaxis.value): p = figure(title="Class Output", plot_height=300) xmax = -1e9 xmin = 1e9 ymax = -1e9 ymin = 1e9 legend_items = [] r = [] for ir, irun in enumerate(self.outp): name = irun.input.name if selected.count(name) == 1: if xxa == 't': x = irun.out.t xlab = 'time [h]' elif xxa == 'h': x = irun.out.h xlab = 'h [m]' elif xxa == 'theta': x = irun.out.theta xlab = 'theta [K]' elif xxa == 'q': x = irun.out.q * 1000 xlab = 'g [g/kg]' else: x = None xlab = '' xmax = max(xmax, max(x)) xmin = min(xmin, min(x)) if yya == 't': y = irun.out.t ylab = 'time [h]' elif yya == 'h': y = irun.out.h ylab = 'h [m]' elif yya == 'theta': y = irun.out.theta ylab = 'theta [K]' elif yya == 'q': y = irun.out.q * 1000 ylab = 'g [g/kg]' else: y = None ylab = '' ymax = max(ymax, max(y)) ymin = min(ymin, min(y)) #self.r[ir].data_source.data['x'] = x #self.r[ir].data_source.data['y'] = y r.append( p.line(x, y, color=self.colors[ir], legend=name, line_width=3)) #legend_items.append((name,r[ir])) p.x_range.start = xmin p.x_range.end = xmax p.y_range.start = ymin p.y_range.end = ymax p.xaxis.axis_label = xlab p.yaxis.axis_label = ylab p.legend.location = "top_left" p_all.append(p) # show(column(p_all)) # does not work yet to keep one graph. Idea comes from https://bokeh.pydata.org/en/latest/docs/user_guide/notebook.html # Or this may work: http://localhost:8889/notebooks/Untitled3.ipynb?kernel_name=python3 if (not global_handle): global_handle = show(obj=p, new='tab', notebook_handle=True) else: push_notebook(handle=global_handle)
async def _update(self, device: str, datapoint): # If a chart has been registered to the device, update it. if device not in self.data: self.data[device] = [] self.data[device].append(datapoint) if self._data_file is not None: line = json.dumps({ "device": device, "timestamp": datapoint.timestamp, "experiment_elapsed_time": datapoint.experiment_elapsed_time, "data": datapoint.data, "unit": self.protocol.apparatus[device]._unit, }) async with aiofiles.open(self._data_file, "a+") as f: await f.write(line + "\n") if get_ipython() is None: return if not self._graphs_shown: logger.debug("Graphs not shown. Initializing...") for sensor, output in self._sensor_outputs.items(): # type: ignore logger.trace(f"Initializing graph for {sensor}") # bind the height of the graph to the selected plot height output.layout.height = f"{self._plot_height}px" with output: # create the figure object p = figure( title=f"{sensor} data", plot_height=self._plot_height, plot_width=600, ) r = p.line( source=self._transformed_data[sensor.name], x="timestamps", y="datapoints", color="#2222aa", line_width=3, ) p.xaxis.axis_label = "Experiment elapsed time (seconds)" p.yaxis.axis_label = self._device_name_to_unit[sensor.name] # since we're in the with-statement, this will show up in the accordion output_notebook(resources=INLINE, hide_banner=True) target = show(p, notebook_handle=True) # save the target and plot for later updating self._charts[sensor.name] = (target, r) logger.trace( f"Sucessfully initialized graph for {sensor.name}") logger.trace("All graphs successfully initialized") self._graphs_shown = True if device in self._transformed_data: target, r = self._charts[device] self._transformed_data[device]["datapoints"].append(datapoint.data) self._transformed_data[device]["timestamps"].append( datapoint.experiment_elapsed_time) r.data_source.data["datapoints"] = self._transformed_data[device][ "datapoints"] r.data_source.data["timestamps"] = self._transformed_data[device][ "timestamps"] push_notebook(handle=target)
dfs = [] files = file_io.get_matching_files(source) for f in files: dfs.append(pd.read_csv(f, names=names)) df = pd.concat(dfs) if variables.get("target_lambda", False): df["target"] = df.apply(eval(variables.get("target_lambda", "")), axis=1) else: df["target"] = df["target"].apply( lambda x: 1 if x == variables.get("trueclass", "true") else 0) fpr, tpr, thresholds = roc_curve( df["target"], df[variables.get("true_score_column", "true")]) df = pd.DataFrame({"fpr": fpr, "tpr": tpr, "thresholds": thresholds}) else: # Load data from generated csv file. df = pd.read_csv(source, header=None, names=["fpr", "tpr", "thresholds"]) # Create visualization. output_notebook() p = figure(tools="pan,wheel_zoom,box_zoom,reset,hover,previewsave") p.line("fpr", "tpr", line_width=2, source=df) hover = p.select(dict(type=HoverTool)) hover.tooltips = [("Threshold", "@thresholds")] show(row(p, sizing_mode="scale_width"))
def interact(self, lc=None): """Display an interactive IPython Notebook widget to inspect the data. The widget will show both the lightcurve and pixel data. By default, the lightcurve shown is obtained by calling the `to_lightcurve()` method, unless the user supplies a custom `LightCurve` object. Note: at this time, this feature only works inside an active Jupyter Notebook, and tends to be too slow when more than ~30,000 cadences are contained in the TPF (e.g. short cadence data). Parameters ---------- lc : LightCurve object An optional pre-processed lightcurve object to show. """ try: from ipywidgets import interact import ipywidgets as widgets from bokeh.io import push_notebook, show, output_notebook from bokeh.plotting import figure, ColumnDataSource from bokeh.models import Span, Range1d, LinearAxis, LogColorMapper from bokeh.layouts import row from bokeh.models.tools import HoverTool from IPython.display import display output_notebook() except ImportError: raise ImportError('The quicklook tool requires Bokeh and ipywidgets. ' 'See the .interact() tutorial') ytitle = 'Flux' if lc is None: lc = self.to_lightcurve() ytitle = 'Flux (e/s)' # Bokeh cannot handle many data points ## https://github.com/bokeh/bokeh/issues/7490 if len(lc.cadenceno) > 30000: raise RuntimeError('Interact cannot display more than 20000 cadences.') # Map cadence to index for quick array slicing. n_lc_cad = len(lc.cadenceno) n_cad, nx, ny = self.flux.shape lc_cad_matches = np.in1d(self.cadenceno, lc.cadenceno) if lc_cad_matches.sum() != n_lc_cad: raise ValueError("The lightcurve provided has cadences that are not " "present in the Target Pixel File.") min_cadence, max_cadence = np.min(self.cadenceno), np.max(self.cadenceno) cadence_lookup = {cad: j for j, cad in enumerate(self.cadenceno)} cadence_full_range = np.arange(min_cadence, max_cadence, 1, dtype=np.int) missing_cadences = list(set(cadence_full_range)-set(self.cadenceno)) # Convert binary quality numbers into human readable strings qual_strings = [] for bitmask in lc.quality: flag_str_list = KeplerQualityFlags.decode(bitmask) if len(flag_str_list) == 0: qual_strings.append(' ') if len(flag_str_list) == 1: qual_strings.append(flag_str_list[0]) if len(flag_str_list) > 1: qual_strings.append("; ".join(flag_str_list)) # Convert time into human readable strings, breaks with NaN time # See https://github.com/KeplerGO/lightkurve/issues/116 if (self.time == self.time).all(): human_time = self.timeobj.isot[lc_cad_matches] else: human_time = [' '] * n_lc_cad # Each data source will later become a hover-over tooltip source = ColumnDataSource(data=dict( time=lc.time, time_iso=human_time, flux=lc.flux, cadence=lc.cadenceno, quality_code=lc.quality, quality=np.array(qual_strings))) # Provide extra metadata in the title if self.mission == 'K2': title = "Quicklook lightcurve for EPIC {} (K2 Campaign {})".format( self.keplerid, self.campaign) elif self.mission == 'Kepler': title = "Quicklook lightcurve for KIC {} (Kepler Quarter {})".format( self.keplerid, self.quarter) # Figure 1 shows the lightcurve with steps, tooltips, and vertical line fig1 = figure(title=title, plot_height=300, plot_width=600, tools="pan,wheel_zoom,box_zoom,save,reset") fig1.yaxis.axis_label = ytitle fig1.xaxis.axis_label = 'Time - 2454833 days [BKJD]' fig1.step('time', 'flux', line_width=1, color='gray', source=source, nonselection_line_color='gray', mode="center") r = fig1.circle('time', 'flux', source=source, fill_alpha=0.3, size=8, line_color=None, selection_color="firebrick", nonselection_fill_alpha=0.0, nonselection_line_color=None, nonselection_line_alpha=0.0, fill_color=None, hover_fill_color="firebrick", hover_alpha=0.9, hover_line_color="white") fig1.add_tools(HoverTool(tooltips=[("Cadence", "@cadence"), ("Time (BKJD)", "@time{0,0.000}"), ("Time (ISO)", "@time_iso"), ("Flux", "@flux"), ("Quality Code", "@quality_code"), ("Quality Flag", "@quality")], renderers=[r], mode='mouse', point_policy="snap_to_data")) # Vertical line to indicate the cadence shown in Fig 2 vert = Span(location=0, dimension='height', line_color='firebrick', line_width=4, line_alpha=0.5) fig1.add_layout(vert) # Figure 2 shows the Target Pixel File stamp with log screen stretch fig2 = figure(plot_width=300, plot_height=300, tools="pan,wheel_zoom,box_zoom,save,reset", title='Pixel data (CCD {}.{})'.format( self.module, self.output)) fig2.yaxis.axis_label = 'Pixel Row Number' fig2.xaxis.axis_label = 'Pixel Column Number' pedestal = np.nanmin(self.flux[lc_cad_matches, :, :]) stretch_dims = np.prod(self.flux[lc_cad_matches, :, :].shape) screen_stretch = self.flux[lc_cad_matches, :, :].reshape(stretch_dims) - pedestal screen_stretch = screen_stretch[np.isfinite(screen_stretch)] # ignore NaNs screen_stretch = screen_stretch[screen_stretch > 0.0] vlo = np.min(screen_stretch) vhi = np.max(screen_stretch) vstep = (np.log10(vhi) - np.log10(vlo)) / 300.0 # assumes counts >> 1.0! lo, med, hi = np.nanpercentile(screen_stretch, [1, 50, 95]) color_mapper = LogColorMapper(palette="Viridis256", low=lo, high=hi) fig2_dat = fig2.image([self.flux[0, :, :] - pedestal], x=self.column, y=self.row, dw=self.shape[2], dh=self.shape[1], dilate=False, color_mapper=color_mapper) # The figures appear before the interactive widget sliders show(row(fig1, fig2), notebook_handle=True) # The widget sliders call the update function each time def update(cadence, log_stretch): """Function that connects to the interact widget slider values""" fig2_dat.glyph.color_mapper.high = 10**log_stretch[1] fig2_dat.glyph.color_mapper.low = 10**log_stretch[0] if cadence not in missing_cadences: index_val = cadence_lookup[cadence] vert.update(line_alpha=0.5) if self.time[index_val] == self.time[index_val]: vert.update(location=self.time[index_val]) else: vert.update(line_alpha=0.0) fig2_dat.data_source.data['image'] = [self.flux[index_val, :, :] - pedestal] else: vert.update(line_alpha=0) fig2_dat.data_source.data['image'] = [self.flux[0, :, :] * np.NaN] push_notebook() # Define the widgets that enable the interactivity play = widgets.Play(interval=10, value=min_cadence, min=min_cadence, max=max_cadence, step=1, description="Press play", disabled=False) play.show_repeat, play._repeat = False, False cadence_slider = widgets.IntSlider( min=min_cadence, max=max_cadence, step=1, value=min_cadence, description='Cadence', layout=widgets.Layout(width='40%', height='20px')) screen_slider = widgets.FloatRangeSlider( value=[np.log10(lo), np.log10(hi)], min=np.log10(vlo), max=np.log10(vhi), step=vstep, description='Pixel Stretch (log)', style={'description_width': 'initial'}, continuous_update=False, layout=widgets.Layout(width='30%', height='20px')) widgets.jslink((play, 'value'), (cadence_slider, 'value')) ui = widgets.HBox([play, cadence_slider, screen_slider]) out = widgets.interactive_output(update, {'cadence': cadence_slider, 'log_stretch': screen_slider}) display(ui, out)
def cpu_usage(): from bokeh.layouts import gridplot from bokeh.plotting import figure, show, helpers from bokeh.resources import CDN from bokeh.io import output_notebook, push_notebook from bokeh.models import ColumnDataSource output_notebook(resources=CDN) # Parse the client table to determine how many CPUs are available num_cpus = 0 client_table = ray.global_state.client_table() for node_ip, client_list in client_table.items(): for client in client_list: if "CPU" in client: num_cpus += client["CPU"] # Update the plot based on the sliders 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) plot_utilization()
""" This module implements the Container class. """ import sys import webbrowser import random from tempfile import NamedTemporaryFile from typing import Any, Dict from bokeh.io import output_notebook from bokeh.embed import components from bokeh.resources import INLINE from jinja2 import Environment, PackageLoader from ..utils import is_notebook output_notebook(INLINE, hide_banner=True) # for offline usage ENV_LOADER = Environment(loader=PackageLoader("dataprep", "eda/templates"), ) TAB_VISUAL_TYPES = { "missing_impact_1v1", "missing_impact", "categorical_column", "numerical_column", "datetime_column", "cat_and_num_cols", "two_num_cols", "two_cat_cols", "dt_and_num_cols", "dt_and_cat_cols", "dt_cat_num_cols",
def show_interact_widget( tpf, notebook_url="localhost:8888", lc=None, max_cadences=200000, aperture_mask="default", exported_filename=None, transform_func=None, ylim_func=None, vmin=None, vmax=None, scale="log", cmap="Viridis256", ): """Display an interactive Jupyter Notebook widget to inspect the pixel data. The widget will show both the lightcurve and pixel data. The pixel data supports pixel selection via Bokeh tap and box select tools in an interactive javascript user interface. Note: at this time, this feature only works inside an active Jupyter Notebook, and tends to be too slow when more than ~30,000 cadences are contained in the TPF (e.g. short cadence data). Parameters ---------- tpf : lightkurve.TargetPixelFile Target Pixel File to interact with notebook_url: str Location of the Jupyter notebook page (default: "localhost:8888") When showing Bokeh applications, the Bokeh server must be explicitly configured to allow connections originating from different URLs. This parameter defaults to the standard notebook host and port. If you are running on a different location, you will need to supply this value for the application to display properly. If no protocol is supplied in the URL, e.g. if it is of the form "localhost:8888", then "http" will be used. max_cadences: int Raise a RuntimeError if the number of cadences shown is larger than this value. This limit helps keep browsers from becoming unresponsive. aperture_mask : array-like, 'pipeline', 'threshold', 'default', or 'all' A boolean array describing the aperture such that `True` means that the pixel will be used. If None or 'all' are passed, all pixels will be used. If 'pipeline' is passed, the mask suggested by the official pipeline will be returned. If 'threshold' is passed, all pixels brighter than 3-sigma above the median flux will be used. If 'default' is passed, 'pipeline' mask will be used when available, with 'threshold' as the fallback. exported_filename: str An optional filename to assign to exported fits files containing the custom aperture mask generated by clicking on pixels in interact. The default adds a suffix '-custom-aperture-mask.fits' to the TargetPixelFile basename. transform_func: function A function that transforms the lightcurve. The function takes in a LightCurve object as input and returns a LightCurve object as output. The function can be complex, such as detrending the lightcurve. In this way, the interactive selection of aperture mask can be evaluated after inspection of the transformed lightcurve. The transform_func is applied before saving a fits file. Default: None (no transform is applied). ylim_func: function A function that returns ylimits (low, high) given a LightCurve object. The default is to return an expanded window around the 10-90th percentile of lightcurve flux values. scale: str Color scale for tpf figure. Default is 'log' vmin: int [optional] Minimum color scale for tpf figure vmax: int [optional] Maximum color scale for tpf figure cmap: str Colormap to use for tpf plot. Default is 'Viridis256' """ try: import bokeh if bokeh.__version__[0] == "0": warnings.warn("interact() requires Bokeh version 1.0 or later", LightkurveWarning) except ImportError: log.error("The interact() tool requires the `bokeh` Python package; " "you can install bokeh using e.g. `conda install bokeh`.") return None aperture_mask = tpf._parse_aperture_mask(aperture_mask) if ~aperture_mask.any(): log.error( "No pixels in `aperture_mask`, finding optimum aperture using `tpf.create_threshold_mask`." ) aperture_mask = tpf.create_threshold_mask() if ~aperture_mask.any(): log.error("No pixels in `aperture_mask`, using all pixels.") aperture_mask = tpf._parse_aperture_mask("all") if exported_filename is None: exported_filename = make_default_export_name(tpf) try: exported_filename = str(exported_filename) except: log.error("Invalid input filename type for interact()") raise if ".fits" not in exported_filename.lower(): exported_filename += ".fits" if lc is None: lc = tpf.to_lightcurve(aperture_mask=aperture_mask) tools = "tap,box_select,wheel_zoom,reset" else: lc = lc.copy() tools = "wheel_zoom,reset" aperture_mask = np.zeros(tpf.flux.shape[1:]).astype(bool) aperture_mask[0, 0] = True lc.meta["APERTURE_MASK"] = aperture_mask if transform_func is not None: lc = transform_func(lc) npix = tpf.flux[0, :, :].size pixel_index_array = np.arange(0, npix, 1).reshape(tpf.flux[0].shape) # Bokeh cannot handle many data points # https://github.com/bokeh/bokeh/issues/7490 n_cadences = len(lc.cadenceno) if n_cadences > max_cadences: log.error( f"Error: interact cannot display more than {max_cadences} " "cadences without suffering significant performance issues. " "You can limit the number of cadences show using slicing, e.g. " "`tpf[0:1000].interact()`. Alternatively, you can override " "this limitation by passing the `max_cadences` argument.") elif n_cadences > 30000: log.warning(f"Warning: the pixel file contains {n_cadences} cadences. " "The performance of interact() is very slow for such a " "large number of frames. Consider using slicing, e.g. " "`tpf[0:1000].interact()`, to make interact run faster.") def create_interact_ui(doc): # The data source includes metadata for hover-over tooltips lc_source = prepare_lightcurve_datasource(lc) tpf_source = prepare_tpf_datasource(tpf, aperture_mask) # Create the lightcurve figure and its vertical marker fig_lc, vertical_line = make_lightcurve_figure_elements( lc, lc_source, ylim_func=ylim_func) # Create the TPF figure and its stretch slider pedestal = -np.nanmin(tpf.flux.value) + 1 if scale == "linear": pedestal = 0 fig_tpf, stretch_slider = make_tpf_figure_elements( tpf, tpf_source, pedestal=pedestal, fiducial_frame=0, vmin=vmin, vmax=vmax, scale=scale, cmap=cmap, tools=tools, ) # Helper lookup table which maps cadence number onto flux array index. tpf_index_lookup = {cad: idx for idx, cad in enumerate(tpf.cadenceno)} # Interactive slider widgets and buttons to select the cadence number cadence_slider = Slider( start=np.min(tpf.cadenceno), end=np.max(tpf.cadenceno), value=np.min(tpf.cadenceno), step=1, title="Cadence Number", width=490, ) r_button = Button(label=">", button_type="default", width=30) l_button = Button(label="<", button_type="default", width=30) export_button = Button(label="Save Lightcurve", button_type="success", width=120) message_on_save = Div(text=" ", width=600, height=15) # Callbacks def _create_lightcurve_from_pixels(tpf, selected_pixel_indices, transform_func=transform_func): """Create the lightcurve from the selected pixel index list""" selected_indices = np.array(selected_pixel_indices) selected_mask = np.isin(pixel_index_array, selected_indices) lc_new = tpf.to_lightcurve(aperture_mask=selected_mask) lc_new.meta["APERTURE_MASK"] = selected_mask if transform_func is not None: lc_transformed = transform_func(lc_new) if len(lc_transformed) != len(lc_new): warnings.warn( "Dropping cadences in `transform_func` is not " "yet supported due to fixed time coordinates." "Skipping the transformation...", LightkurveWarning, ) else: lc_new = lc_transformed lc_new.meta["APERTURE_MASK"] = selected_mask return lc_new def update_upon_pixel_selection(attr, old, new): """Callback to take action when pixels are selected.""" # Check if a selection was "re-clicked", then de-select if (sorted(old) == sorted(new)) & (new != []): # Trigger recursion tpf_source.selected.indices = new[1:] if new != []: lc_new = _create_lightcurve_from_pixels( tpf, new, transform_func=transform_func) lc_source.data["flux"] = lc_new.flux.value if ylim_func is None: ylims = get_lightcurve_y_limits(lc_source) else: ylims = ylim_func(lc_new) fig_lc.y_range.start = ylims[0] fig_lc.y_range.end = ylims[1] else: lc_source.data["flux"] = lc.flux.value * 0.0 fig_lc.y_range.start = -1 fig_lc.y_range.end = 1 message_on_save.text = " " export_button.button_type = "success" def update_upon_cadence_change(attr, old, new): """Callback to take action when cadence slider changes""" if new in tpf.cadenceno: frameno = tpf_index_lookup[new] fig_tpf.select("tpfimg")[0].data_source.data["image"] = [ tpf.flux.value[frameno, :, :] + pedestal ] vertical_line.update(location=tpf.time.value[frameno]) else: fig_tpf.select("tpfimg")[0].data_source.data["image"] = [ tpf.flux.value[0, :, :] * np.NaN ] lc_source.selected.indices = [] def go_right_by_one(): """Step forward in time by a single cadence""" existing_value = cadence_slider.value if existing_value < np.max(tpf.cadenceno): cadence_slider.value = existing_value + 1 def go_left_by_one(): """Step back in time by a single cadence""" existing_value = cadence_slider.value if existing_value > np.min(tpf.cadenceno): cadence_slider.value = existing_value - 1 def save_lightcurve(): """Save the lightcurve as a fits file with mask as HDU extension""" if tpf_source.selected.indices != []: lc_new = _create_lightcurve_from_pixels( tpf, tpf_source.selected.indices, transform_func=transform_func) lc_new.to_fits( exported_filename, overwrite=True, flux_column_name="SAP_FLUX", aperture_mask=lc_new.meta["APERTURE_MASK"].astype(np.int), SOURCE="lightkurve interact", NOTE="custom mask", MASKNPIX=np.nansum(lc_new.meta["APERTURE_MASK"]), ) if message_on_save.text == " ": text = '<font color="black"><i>Saved file {} </i></font>' message_on_save.text = text.format(exported_filename) export_button.button_type = "success" else: text = '<font color="gray"><i>Saved file {} </i></font>' message_on_save.text = text.format(exported_filename) else: text = ( '<font color="gray"><i>No pixels selected, no mask saved</i></font>' ) export_button.button_type = "warning" message_on_save.text = text def jump_to_lightcurve_position(attr, old, new): if new != []: cadence_slider.value = lc.cadenceno[new[0]] # Map changes to callbacks r_button.on_click(go_right_by_one) l_button.on_click(go_left_by_one) tpf_source.selected.on_change("indices", update_upon_pixel_selection) lc_source.selected.on_change("indices", jump_to_lightcurve_position) export_button.on_click(save_lightcurve) cadence_slider.on_change("value", update_upon_cadence_change) # Layout all of the plots sp1, sp2, sp3, sp4 = ( Spacer(width=15), Spacer(width=30), Spacer(width=80), Spacer(width=60), ) widgets_and_figures = layout( [fig_lc, fig_tpf], [ l_button, sp1, r_button, sp2, cadence_slider, sp3, stretch_slider ], [export_button, sp4, message_on_save], ) doc.add_root(widgets_and_figures) output_notebook(verbose=False, hide_banner=True) return show(create_interact_ui, notebook_url=notebook_url)
def show_skyview_widget(tpf, notebook_url="localhost:8888", magnitude_limit=18): """skyview Parameters ---------- tpf : lightkurve.TargetPixelFile Target Pixel File to interact with notebook_url: str Location of the Jupyter notebook page (default: "localhost:8888") When showing Bokeh applications, the Bokeh server must be explicitly configured to allow connections originating from different URLs. This parameter defaults to the standard notebook host and port. If you are running on a different location, you will need to supply this value for the application to display properly. If no protocol is supplied in the URL, e.g. if it is of the form "localhost:8888", then "http" will be used. magnitude_limit : float A value to limit the results in based on Gaia Gmag. Default, 18. """ try: import bokeh if bokeh.__version__[0] == "0": warnings.warn("interact_sky() requires Bokeh version 1.0 or later", LightkurveWarning) except ImportError: log.error( "The interact_sky() tool requires the `bokeh` Python package; " "you can install bokeh using e.g. `conda install bokeh`.") return None # Try to identify the "fiducial frame", for which the TPF WCS is exact zp = (tpf.pos_corr1 == 0) & (tpf.pos_corr2 == 0) (zp_loc, ) = np.where(zp) if len(zp_loc) == 1: fiducial_frame = zp_loc[0] else: fiducial_frame = 0 def create_interact_ui(doc): # The data source includes metadata for hover-over tooltips tpf_source = None # Create the TPF figure and its stretch slider fig_tpf, stretch_slider = make_tpf_figure_elements( tpf, tpf_source, fiducial_frame=fiducial_frame, plot_width=640, plot_height=600, ) fig_tpf, r = add_gaia_figure_elements(tpf, fig_tpf, magnitude_limit=magnitude_limit) # Optionally override the default title if tpf.mission == "K2": fig_tpf.title.text = ( "Skyview for EPIC {}, K2 Campaign {}, CCD {}.{}".format( tpf.targetid, tpf.campaign, tpf.module, tpf.output)) elif tpf.mission == "Kepler": fig_tpf.title.text = ( "Skyview for KIC {}, Kepler Quarter {}, CCD {}.{}".format( tpf.targetid, tpf.quarter, tpf.module, tpf.output)) elif tpf.mission == "TESS": fig_tpf.title.text = "Skyview for TESS {} Sector {}, Camera {}.{}".format( tpf.targetid, tpf.sector, tpf.camera, tpf.ccd) # Layout all of the plots widgets_and_figures = layout([fig_tpf, stretch_slider]) doc.add_root(widgets_and_figures) output_notebook(verbose=False, hide_banner=True) return show(create_interact_ui, notebook_url=notebook_url)
def notebook(self, flag): warnings.warn("Chart property 'notebook' was deprecated in 0.11 \ and will be removed in the future.") from bokeh.io import output_notebook output_notebook()
sqrt, sin, cos, tan, sec, csc, cot, log, exp, e, asin, acos, atan #Plotting and fitting from qexpy.plotting import Plot, MakePlot from qexpy.fitting import XYDataSet, XYFitter, DataSetFromFile from qexpy.plot_utils import bk_plot_dataset, bk_add_points_with_error_bars,\ bk_plot_function __version__ = '1.0.1' # The following will initialize bokeh if running in a notebook, # and hacks the _nb_loaded variable which is required for all plots # to show when Run All is used in a notebook. This bug arrived in # bokeh 12.1, hopefully they get rid of it... import qexpy.utils as qu import bokeh.io as bi from qexpy.utils import get_data_from_file if qu.in_notebook(): qu.mpl_output_notebook() # calls matplotlib inline bi.output_notebook() qu.bokeh_ouput_notebook_called = True '''This hack is required as there is a bug in bokeh preventing it from knowing that it was in fact loaded. ''' bi._nb_loaded = True
def bokplot(self, title='Placeholder'): from bokeh.io import output_notebook from bokeh.plotting import figure, show, output_file output_notebook() p1 = figure(title=title, x_axis_type="datetime", plot_width=1400, plot_height=400, background_fill_color="#EFE8E2") # check if scaling has been applied # use the same indices for both cases if self.scale == True: # check if ys_train exists, and plot if yes if self.data['ys_train'] is not None: if isinstance(self.data['ys_train'], self.pd.DataFrame): local_y_train = self.data['ys_train'].values.astype( 'float32') p1.line(self.data['y_train_index'], local_ys_train.reshape(len(local_ys_train)), color='#E08E79', legend='ys_train') else: # if it's a np array p1.line(self.data['y_train_index'], local_y_train.reshape(len(self.data['ys_train'])), color='#E08E79', legend='ys_train') # check if ys_val exists, and plot if yes if self.data['ys_val'] is not None: if isinstance(self.data['ys_val'], self.pd.DataFrame): local_ys_val = self.data['ys_val'].values.astype('float32') p1.line(self.data['y_val_index'], local_ys_val.reshape(len(local_ys_val)), color='#3B8686', legend='ys_val') else: # if it's an np.array p1.line(self.data['y_val_index'], local_ys_val.reshape(len(self.data['ys_val'])), color='#3B8686', legend='ys_val') if self.scale == False: if self.data['y_train'] is not None: if isinstance(self.data['y_train'], self.pd.DataFrame): local_y_train = self.data['y_train'].values.astype( 'float32') p1.line(self.data['y_train_index'], local_y_train.reshape(len(local_y_train)), color='#E08E79', legend='y_train') else: # if it's an np.array p1.line(self.data['y_train_index'], local_y_train.reshape(len(local_y_train)), color='#E08E79', legend='y_train') if self.data['y_val'] is not None: if isinstance(self.data['y_val'], self.pd.DataFrame): local_y_val = self.data['y_val'].values.astype('float32') p1.line(self.data['y_val_index'], local_y_val.reshape(len(local_y_val)), color='#3B8686', legend='y_val') else: # if it's an np.array p1.line(self.data['y_val_index'], local_y_val.reshape(len(local_y_val)), color='#3B8686', legend='y_val') # aesthetic mapping p1.grid.grid_line_alpha = 0.1 p1.xaxis.axis_label = "Date" p1.yaxis.axis_label = "Price" p1.legend.location = "top_left" p1.ygrid.band_fill_alpha = 0.2 show(p1)
def test_noarg(self, mock_load_notebook): default_load_notebook_args = (None, False, False) io.output_notebook() self._check_func_called(io._state.output_notebook, (), {}) self._check_func_called(mock_load_notebook, default_load_notebook_args, {})
import colorsys import math import numpy as np from bokeh.charts import Bar from bokeh.plotting import figure, ColumnDataSource import bokeh.io as io from bokeh.models import HoverTool, OpenURL, TapTool AVAIL_COLORS = ["#1F77B4", "firebrick", "goldenrod", "aqua", "brown", "chartreuse", "darkmagenta" "aquamarine", "blue", "red", "blueviolet", "darkorange", "forestgreen", "lime"] # AVAIL_MARKERS: circle, diamond, triangle, square, inverted_triangle, asterisk, # circle_cross, circle_x, cross, diamond_cross, square_cross, square_x, asterisk, diamond io.output_notebook() class ColorScale(): def __init__(self, num_values, val_min, val_max, middle_color="yellow", reverse=False): self.num_values = num_values self.num_val_1 = num_values - 1 self.value_min = val_min self.value_max = val_max self.reverse = reverse self.value_range = self.value_max - self.value_min self.color_scale = [] if middle_color.startswith("y"): # middle color yellow hsv_tuples = [(0.0 + ((x * 0.35) / (self.num_val_1)), 0.99, 0.9) for x in range(self.num_values)] self.reverse = not self.reverse
def __init__(self, gc, name='scatter_plot', **kwargs): node_mapper_dict = {'size':{'max':100, 'min':5, 'step':0.5, 'high':75, 'low':25, 'default':60, 'map_data':False, 'fixed_active':False, }, 'line_width':{'max':50, 'min':0, 'step':0.5, 'high':5, 'low':1, 'default':2, 'map_data':False, 'fixed_active':False, }, 'fill_alpha':{'max':1.0, 'min':0., 'step':0.05, 'high':0.95, 'low':0.3, 'default':1., 'map_data':False, 'fixed_active':False, }, 'line_alpha':{'max':1.0, 'min':0., 'step':0.05, 'high':0.95, 'low':0.3, 'default':1., 'map_data':False, 'fixed_active':False, }, 'line_color':{'default_color':'black','map_data':False,'step':0.05,'min':0.0,'low':0.0}, 'fill_color':{'default_color':'#11D4CA','map_data':False,'step':0.05,'min':0.0,'low':0.0} } edge_mapper_dict = {'size':{'max':100, 'min':5, 'step':0.5, 'high':20, 'low':7, 'default':7, 'map_data':False, 'fixed_active':False, }, 'line_width':{'max':50, 'min':0, 'step':0.5, 'high':5, 'low':1, 'default':2, 'map_data':False, 'fixed_active':False, }, 'fill_alpha':{'max':1.0, 'min':0., 'step':0.05, 'high':0.95, 'low':0.3, 'default':1., 'map_data':False, 'fixed_active':False, }, 'line_alpha':{'max':1.0, 'min':0., 'step':0.05, 'high':0.95, 'low':0.3, 'default':1., 'map_data':False, 'fixed_active':False, }, 'line_color':{'default_color':'black','map_data':False,'step':0.05,'min':0.0,'low':0.0}, 'fill_color':{'default_color':'#EDB021','map_data':False,'step':0.05,'min':0.0,'low':0.0} } gc.name = 'gc' output_notebook(hide_banner=True) edge_mapper_data = self.prepare_edge_mapper_data(gc) node_mapper = PlotMapper(gc.node, mapper_dict=node_mapper_dict, button_type='ddown',button_pos='top', name='node_mapper', mode='interactive') edge_mapper = PlotMapper(edge_mapper_data, mapper_dict=edge_mapper_dict, button_type='ddown',button_pos='top', name='edge_mapper', mode='interactive') node_tooltip_data = gc.node.combine_first(gc.node_metrics.T) node_tooltip = BokehDataFrameTooltip(node_tooltip_data, name='node_tooltip') edge_tooltip = BokehDataFrameTooltip(edge_mapper_data, name='edge_tooltip') ToggleMenu.__init__(self, children=[gc,node_mapper, edge_mapper, node_tooltip, edge_tooltip], name=name,**kwargs) self.observe(self.update) self.gc.calculate.observe(self.update) self.node_mapper.marker.marker_type.observe(self._on_marker_change) self.edge_mapper.marker.marker_type.observe(self._on_marker_change) self.update_source_df() self.init_plot() self.update() self._init_layout()
import bisect from pymongo import MongoClient import pandas as pd from bokeh.plotting import show from bokeh.plotting import figure, show, gridplot, curdoc from bokeh.io import output_notebook, reset_output from bokeh.models import Spacer, DatetimeTickFormatter, Legend from bokeh.resources import INLINE import numpy as np import holoviews as hv from datetime import datetime, timedelta reset_output() output_notebook(resources = INLINE, verbose=False, hide_banner=True) #hv.extension('bokeh') def print_dict(data): for key in data: print(f'{key:<14}: {data[key]:>4}') def make_histogram_plot(name, hist, edges, x_label=None): p = figure(title=name + ' distribution', tools='', background_fill_color="#fafafa") p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="navy", line_color="white", alpha=0.5) p.y_range.start = 0 p.yaxis.formatter.use_scientific = False if x_label is None: x_label = name + ' count' p.xaxis.axis_label = x_label p.yaxis.axis_label = 'user count' p.grid.grid_line_color="white"
def test_args(self): kwargs = dict(url="url", docname="docname", session="session", name="name") io.output_notebook(**kwargs) self._check_func_called(io._state.output_notebook, (), kwargs)
wh.groupby(by=['country'])['suicides_no'].sum().reset_index().sort_values( ['suicides_no'], ascending=True).tail(15).country wh[wh.country.isin( wh.groupby(by=['country'])['suicides_no'].sum().reset_index().sort_values( ['suicides_no'], ascending=True).tail(5).country)].groupby( by=['sex', 'country'])['suicides_no'].sum().unstack().plot( kind='bar', stacked=True) #wh[wh.country.isin(wh.groupby(by=['country'])['suicides_no'].sum().reset_index().sort_values(['suicides_no'],ascending=True).tail(5).country)].groupby(by=['age','country'])['suicides_no'].sum().unstack().plot(kind='bar',stacked=True) #https://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframehttps://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframe #wh[wh.country.isin(wh.groupby(by=['country'])['suicides_no'].sum().reset_index().sort_values(['suicides_no'],ascending=True).tail(5).country)].groupby(by=['year','country'])['suicides_no'].sum().unstack().plot() #https://gist.github.com/dela3499/e159b388258b5f1a7a3bac42fc0179fd from bokeh.plotting import figure, output_file, show, ColumnDataSource from bokeh.models import HoverTool, CategoricalColorMapper from bokeh.io import output_notebook output_notebook() source = ColumnDataSource(data=wh) hover = HoverTool( tooltips=[("year", "@year"), ("sex", "@sex"), ("country", "@country"), ("suicides_no", "@suicides_no")]) p = figure(plot_width=700, plot_height=700, tools=[hover], title="Mouse over the dots") #https://www.kaggle.com/kanncaa1/visualization-bokeh-tutorial-part-1
import pandas as pd import numpy as np # Bokeh libraries from bokeh.io import output_file, output_notebook from bokeh.plotting import figure, show from bokeh.models import ColumnDataSource from bokeh.layouts import row, column, gridplot from bokeh.models.widgets import Tabs, Panel # Prepare the data # Determine where the visualization will be rendered output_file('filename.html') # Render to static HTML, or output_notebook() # Render inline in a Jupyter Notebook # Set up the figure(s) fig = figure() # Instantiate a figure() object # Connect to and draw the data # Organize the layout # Preview and save show(fig) # See what I made, and save if I like it
def __init__(self, dataf, labels): output_notebook() self.dataf = dataf self.labels = labels self.charts = []
def load_ipython_extension(shell): # Initiate bokeh output_notebook(hide_banner=True) shell.register_magics(IProfilerMagics)
from bokeh.models import ColumnDataSource, HoverTool, Select, Paragraph, TextInput from bokeh.layouts import widgetbox, column, row from ipywidgets import interact import pandas as pd df_bokeh = pd.read_csv("Data/cosmetic_TSNE.csv") """ This is for Bokeh to create all possible combinations based on the options from category of products and skin type """ # cosmetic filtering options option_1 = ['Moisturizer', 'Cleanser', 'Treatment', 'Face Mask', 'Eye cream', 'Sun protect'] option_2 = ['Combination', 'Dry', 'Normal', 'Oily', 'Sensitive'] output_notebook() # plot will be shown inside the notebook """ Creating plot, glyphs and adding hover tool""" # make a source and scatter bokeh plot source = ColumnDataSource(df_bokeh) plot = figure(x_axis_label = 'T-SNE 1', y_axis_label = 'T-SNE 2', width = 500, height = 400) plot.circle(x = 'X', y = 'Y', source = source, size = 10, color = '#FF7373', alpha = .8) plot.background_fill_color = "beige" plot.background_fill_alpha = 0.2 # add hover tool hover = HoverTool(tooltips = [
def __init__(self, **options): if in_ipnb(): from bokeh.io import output_notebook output_notebook(hide_banner=True) super(BokehPlotter, self).__init__(**options)
# Import Bokeh modules for interactive plotting import bokeh.io as bi import bokeh.plotting as bp # Set up Bokeh for inline viewing bi.output_notebook() # Set up plot p = bp.figure(width=650, height=400, x_axis_label='x', y_axis_label='dy/dx', tools='pan,wheel_zoom,box_zoom,resize,reset') # Populate glyphs p.circle((x[1:] + x[:-1]) / 2.0, deriv, color='gray', legend='fin. diff.') p.line(x, deriv_exact, color='black', line_width=2, legend='exact') # Position legend p.legend.location = 'top_center' # Display plot bp.show(p)
def event_display_interactive( events, peaks, to_pe, run_id, context, bottom_pmt_array=True, only_main_peaks=False, only_peak_detail_in_wf=False, plot_all_pmts=False, plot_record_matrix=False, plot_records_threshold=10, xenon1t=False, colors=('gray', 'blue', 'green'), yscale=('linear', 'linear', 'linear'), log=True, ): """ Interactive event display for XENONnT. Plots detailed main/alt S1/S2, bottom and top PMT hit pattern as well as all other peaks in a given event. :param bottom_pmt_array: If true plots bottom PMT array hit-pattern. :param only_main_peaks: If true plots only main peaks into detail plots as well as PMT arrays. :param only_peak_detail_in_wf: Only plots main/alt S1/S2 into waveform. Only plot main peaks if only_main_peaks is true. :param plot_all_pmts: Bool if True, colors switched off PMTs instead of showing them in gray, useful for graphs shown in talks. :param plot_record_matrix: If true record matrix is plotted below. waveform. :param plot_records_threshold: Threshold at which zoom level to display record matrix as polygons. Larger values may lead to longer render times since more polygons are shown. :param xenon1t: Flag to use event display with 1T data. :param colors: Colors to be used for peaks. Order is as peak types, 0 = Unknown, 1 = S1, 2 = S2. Can be any colors accepted by bokeh. :param yscale: Defines scale for main/alt S1 == 0, main/alt S2 == 1, waveform plot == 2. Please note, that the log scale can lead to funny glyph renders for small values. :param log: If true color sclae is used for hitpattern plots. Note: How to use: > st.event_display(context, > run_id, > time_range=(event['time'], > event['endtime']) > ) Warning: Raises an error if the user queries a time range which contains more than a single event. :return: bokeh.plotting.figure instance. """ st = context if len(yscale) != 3: raise ValueError( f'"yscale" needs three entries, but you passed {len(yscale)}.') if not hasattr(st, '_BOKEH_CONFIGURED_NOTEBOOK'): st._BOKEH_CONFIGURED_NOTEBOOK = True # Configure show to show notebook: from bokeh.io import output_notebook output_notebook() if len(events) != 1: raise ValueError('The time range you specified contains more or' ' less than a single event. The event display ' ' only works with individual events for now.') if peaks.shape[0] == 0: raise ValueError( 'Found an event without peaks this should not had have happened.') # Select main/alt S1/S2s based on time and endtime in event: m_other_peaks = np.ones(len(peaks), dtype=np.bool_) # To select non-event peaks endtime = strax.endtime(peaks) signal = {} if only_main_peaks: s1_keys = ['s1'] s2_keys = ['s2'] labels = {'s1': 'S1', 's2': 'S2'} else: s1_keys = ['s1', 'alt_s1'] s2_keys = ['s2', 'alt_s2'] labels = {'s1': 'MS1', 'alt_s1': 'AS1', 's2': 'MS2', 'alt_s2': 'AS2'} for s_x in labels.keys(): # Loop over Main/Alt Sx and get store S1/S2 Main/Alt in signals, # store information about other peaks as "m_other_peaks" m = (peaks['time'] == events[f'{s_x}_time']) & (endtime == events[f'{s_x}_endtime']) signal[s_x] = peaks[m] m_other_peaks &= ~m # Detail plots for main/alt S1/S2: fig_s1, fig_s2 = plot_detail_plot_s1_s2( signal, s1_keys, s2_keys, labels, colors, yscale[:2], ) # PMT arrays: if not only_main_peaks: # Plot all keys into both arrays: top_array_keys = s2_keys + s1_keys bottom_array_keys = s1_keys + s2_keys else: top_array_keys = s2_keys bottom_array_keys = s1_keys fig_top, fig_bottom = plot_pmt_arrays_and_positions(top_array_keys, bottom_array_keys, signal, to_pe, labels, plot_all_pmts, xenon1t=xenon1t, log=log) m_other_s2 = m_other_peaks & (peaks['type'] == 2) if np.any(m_other_s2) and not only_main_peaks: # Now we have to add the positions of all the other S2 to the top pmt array # if not only main peaks. fig_top, plot = plot_posS2s(peaks[m_other_s2], label='OS2s', fig=fig_top, s2_type_style_id=2) plot.visible = False # Main waveform plot: if only_peak_detail_in_wf: # If specified by the user only plot main/alt S1/S2 peaks = peaks[~m_other_peaks] waveform = plot_event(peaks, signal, labels, events[0], colors, yscale[-1]) # Create tile: title = _make_event_title(events[0], run_id) # Put everything together: if bottom_pmt_array: upper_row = [fig_s1, fig_s2, fig_top, fig_bottom] else: upper_row = [fig_s1, fig_s2, fig_top] upper_row = bokeh.layouts.Row(children=upper_row) plots = bokeh.layouts.gridplot( children=[upper_row, waveform], sizing_mode='scale_both', ncols=1, merge_tools=True, toolbar_location='above', ) event_display = bokeh.layouts.Column( children=[title, plots], sizing_mode='scale_both', max_width=1600, ) # Add record matrix if asked: if plot_record_matrix: if st.is_stored(run_id, 'records'): # Check if records can be found and load: r = st.get_array(run_id, 'records', time_range=(events[0]['time'], events[0]['endtime'])) elif st.is_stored(run_id, 'raw_records'): warnings.warn( f'Cannot find records for {run_id}, making them from raw_records instead.' ) p = st.get_single_plugin(run_id, 'records') r = st.get_array(run_id, 'raw_records', time_range=(events[0]['time'], events[0]['endtime'])) r = p.compute(r, events[0]['time'], events[0]['endtime'])['records'] else: warnings.warn( f'Can neither find records nor raw_records for run {run_id}, proceed without record ' f'matrix.') plot_record_matrix = False if plot_record_matrix: straxen._BOKEH_X_RANGE = None # First get hook to for x_range: x_range_hook = lambda plot, element: hook( plot, x_range=straxen._BOKEH_X_RANGE, debug=False) # Create datashader plot: wf, record_points, time_stream = _hvdisp_plot_records_2d( records=r, to_pe=to_pe, t_reference=peaks[0]['time'], event_range=(waveform.x_range.start, waveform.x_range.end), config=st.config, hooks=[x_range_hook], tools=[], ) # Create record polygons: polys = plot_record_polygons(record_points, width=1.1) records_in_window = polys.apply(get_records_matrix_in_window, streams=[time_stream], time_slice=plot_records_threshold) # Render plot to initialize x_range: import holoviews as hv import panel _ = hv.render(wf) # Set x-range of event plot: bokeh_set_x_range(waveform, straxen._BOKEH_X_RANGE, debug=False) event_display = panel.Column(event_display, wf * records_in_window, sizing_mode='scale_width') return event_display
def get_jupyter(self): bokeh_plot = self.plot() output_notebook() show(bokeh_plot)
import pandas as pd import numpy as np import colorsys import networkx as nx from bokeh.resources import CDN from bokeh.io import output_notebook output_notebook(resources=CDN) pd.set_option('max_colwidth', 200) pd.set_option('max_rows', 500) from bokeh.plotting import figure, output_file, show, ColumnDataSource, save from bokeh.io import show from bokeh.models import ColumnDataSource from bokeh.plotting import figure def _pseudocolor(val): """Convert number to color""" h = (1.0 - val) * 120 / 360 r, g, b = colorsys.hsv_to_rgb(h, 1., 1.) return r * 255, g * 255, b * 255 def _rgb2hex(color): """Converts a list or tuple of color to an RGB string Args: color (list|tuple): the list or tuple of integers (e.g. (127, 127, 127))
def __init__(self, map_size, team_size): self.draw_init_plot(map_size, team_size) output_notebook() self.nb_handle = show(self.plt_combo, notebook_handle=True)
def test_args(self, mock_load_notebook): load_notebook_args = (Resources(), True, True) io.output_notebook(*load_notebook_args) self._check_func_called(io._state.output_notebook, (), {}) self._check_func_called(mock_load_notebook, load_notebook_args, {})
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Nov 21 16:06:38 2017 @author: jmmauricio """ from bokeh.io import output_notebook, show from bokeh.plotting import figure from bokeh.models import ColumnDataSource, HoverTool from bokeh.io import push_notebook from bokeh.resources import INLINE output_notebook(INLINE) def plot_results(grid): grid.bokeh_tools() p = figure(width=600, height=400, title='Results') # trafos: source = ColumnDataSource(grid.transformer_data) trafo = p.multi_line(source=source, xs='x_s', ys='y_s', color="green", alpha=0.5, line_width=5) # lines:
def test_args(self, mock_load_notebook): load_zeppelin_args = (Resources(), True, True, 1000, 'zeppelin') io.output_notebook(*load_zeppelin_args) self._check_func_called(io._state.output_notebook, ('zeppelin',), {}) self._check_func_called(mock_load_notebook, load_zeppelin_args, {})
def visualize_portfolio_transform_bokeh(portfolio_state_list, save_path=None, is_save=False, is_show=True, is_jupyter=True): """ ["now_price_array", "portfoilo_vector", "mean_cost_price_array", "all_assets", "datetime"] """ # テータの取り出し ticker_names = portfolio_state_list[0].names colors = d3["Category20"][len(ticker_names)] all_price_array = np.stack( [one_state.now_price_array for one_state in portfolio_state_list], axis=1) all_portfolio_vector = np.stack( [one_state.portfolio_vector for one_state in portfolio_state_list], axis=1) all_mean_cost_price_array = np.stack([ one_state.mean_cost_price_array for one_state in portfolio_state_list ], axis=1) all_assets_array = np.array( [one_state.all_assets for one_state in portfolio_state_list]) all_datetime_array = np.array([ get_naive_datetime_from_datetime(one_state.datetime) for one_state in portfolio_state_list ]) x = np.arange(0, len(portfolio_state_list)) # sorceの作成 portfolio_vector_source = {"x": x, "datetime": all_datetime_array} price_source_x = [] price_source_y = [] mean_cost_price_source_x = [] mean_cost_price_source_y = [] for i, ticker_name in enumerate(ticker_names): portfolio_vector_source[ticker_name] = all_portfolio_vector[i, :] price_source_x.append(x) price_source_y.append(all_price_array[i, :] / all_price_array[i, 0]) mean_cost_price_source_x.append(x) mean_cost_price_source_y.append(all_mean_cost_price_array[i, :] / all_mean_cost_price_array[i, 0]) # ホバーツールの設定 #tool_tips = [("x", "@x")] tool_tips = [("datetime", "@datetime{%F %H:%M:%S}")] tool_tips.extend([(ticker_name, "@" + ticker_name + "{0.000}") for ticker_name in ticker_names]) hover_tool = HoverTool(tooltips=tool_tips, formatters={'@datetime': 'datetime'}) # 描画 p1_text = Div(text=make_ticker_text(all_price_array[:, 0], ticker_names)) p1 = bokeh.plotting.figure(plot_width=1200, plot_height=500, title="正規化価格・ポートフォリオ") p1.add_tools(hover_tool) p1.extra_y_ranges = {"portfolio_vector": Range1d(start=0, end=3)} p1.add_layout(LinearAxis(y_range_name="portfolio_vector"), 'right') p1.vbar_stack(ticker_names, x='x', width=1, color=colors, y_range_name="portfolio_vector", source=portfolio_vector_source, legend_label=ticker_names, alpha=0.8) p1.multi_line(xs=price_source_x, ys=price_source_y, line_color=colors, line_width=2) y_min, y_max = make_y_limit_multi(price_source_y, lowwer_ratio=0.1, upper_ratio=0.1) y_min -= (y_max - y_min) * 0.66 # ポートフォリオ割合のためのオフセット p1.y_range = Range1d(start=y_min, end=y_max) p1.yaxis[0].axis_label = "正規化価格" p1.yaxis[1].axis_label = "保有割合" p1.xaxis.major_label_overrides = { str(one_x): str(all_datetime_array[i]) for i, one_x in enumerate(x) } p2_text = Div( text=make_ticker_text(all_mean_cost_price_array[:, 0], ticker_names)) p2 = bokeh.plotting.figure(plot_width=1200, plot_height=300, title="正規化平均取得価格・全資産") p2.multi_line(xs=mean_cost_price_source_x, ys=mean_cost_price_source_y, line_color=colors, line_width=2) y_min, y_max = make_y_limit_multi(mean_cost_price_source_y, lowwer_ratio=0.1, upper_ratio=0.1) p2.y_range = Range1d(start=y_min, end=y_max) y_max, y_min = make_y_limit(all_assets_array, upper_ratio=0.1, lowwer_ratio=0.1) p2.extra_y_ranges = {"all_assets": Range1d(start=y_max, end=y_min)} p2.add_layout(LinearAxis(y_range_name="all_assets"), 'right') p2.line(x, all_assets_array, color="red", legend_label="all_assets", line_width=4, y_range_name="all_assets") # 疑似的なレジェンドをつける for ticker_name, color in zip(ticker_names, colors): p2.line([], [], legend_label=ticker_name, color=color, line_width=2) p2.yaxis[0].axis_label = "正規化平均取得価格" p2.yaxis[1].axis_label = "全資産 [円]" p2.xaxis.major_label_overrides = { str(one_x): str(all_datetime_array[i]) for i, one_x in enumerate(x) } layout_list = [p1_text, p1, p2_text, p2] created_figure = bokeh.layouts.column(*layout_list) if is_save: if save_path.suffix == ".png": bokeh.io.export_png(created_figure, filename=save_path) elif save_path.suffix == ".html": output_file(save_path) bokeh.io.save(created_figure, filename=save_path, title="trading process") else: raise Exception( "The suffix of save_path is must be '.png' or '.html'.") return None if is_show: try: reset_output() if is_jupyter: output_notebook() show(created_figure) except: if is_jupyter: output_notebook() show(created_figure) return None if not is_save and not is_show: return layout_list
def test_noarg(self): default_kwargs = dict(url=None, docname=None, session=None, name=None) io.output_notebook() self._check_func_called(io._state.output_notebook, (), default_kwargs)
import numpy as np from bokeh.plotting import figure, ColumnDataSource import bokeh.io as io from bokeh.models import HoverTool AVAIL_COLORS = [ "#1F77B4", "firebrick", "goldenrod", "aqua", "brown", "chartreuse", "darkmagenta" "aquamarine", "blue", "red", "blueviolet", "darkorange", "forestgreen", "lime" ] # AVAIL_MARKERS: circle, diamond, triangle, square, inverted_triangle, asterisk, # circle_cross, circle_x, cross, diamond_cross, square_cross, square_x, asterisk, diamond io.output_notebook() class ColorScale(): def __init__(self, num_values, val_min, val_max, middle_color="yellow", reverse=False): self.num_values = num_values self.num_val_1 = num_values - 1 self.value_min = val_min self.value_max = val_max self.reverse = reverse self.value_range = self.value_max - self.value_min
def task_completion_time_distribution(): from bokeh.models import ColumnDataSource from bokeh.layouts import gridplot from bokeh.plotting import figure, show, helpers from bokeh.io import output_notebook, push_notebook from bokeh.resources import CDN output_notebook(resources=CDN) # Create the Bokeh plot p = figure(title="Task Completion Time Distribution", 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 pulls from source = ColumnDataSource(data={ "top": [], "left": [], "right": [] }) # Plot the histogram rectangles p.quad(top="top", bottom=0, left="left", right="right", source=source, fill_color="#B3B3B3", line_color="#033649") # Label the plot axes p.xaxis.axis_label = "Duration in seconds" p.yaxis.axis_label = "Number of tasks" handle = show(gridplot(p, ncols=1, plot_width=500, plot_height=500, toolbar_location="below"), notebook_handle=True) # Function to update the plot def task_completion_time_update(abs_earliest, abs_latest, abs_num_tasks, tasks): if len(tasks) == 0: return # Create the distribution to plot distr = [] for task_id, data in tasks.items(): distr.append(data["store_outputs_end"] - data["get_arguments_start"]) # Create a histogram from the distribution top, bin_edges = np.histogram(distr, bins="auto") left = bin_edges[:-1] right = bin_edges[1:] source.data = {"top": top, "left": left, "right": right} # Set the x and y ranges x_range = (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) x_range = helpers._get_range(x_range) p.x_range.start = x_range.start p.x_range.end = x_range.end y_range = helpers._get_range(y_range) p.y_range.start = y_range.start p.y_range.end = y_range.end # Push updates to the plot push_notebook(handle=handle) get_sliders(task_completion_time_update)
def __init__(self, notebook=False): self.notebook = notebook if notebook: output_notebook()
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)
def get_jupyter(self): output_notebook() show(self.plot())
def plot_process_tree( data: pd.DataFrame, schema: ProcSchema = None, output_var: str = None, legend_col: str = None, show_table: bool = False, **kwargs, ) -> Tuple[figure, LayoutDOM]: """ Plot a Process Tree Visualization. Parameters ---------- data : pd.DataFrame DataFrame containing one or more Process Trees schema : ProcSchema, optional The data schema to use for the data set, by default None (if None the schema is inferred) output_var : str, optional Output variable for selected items in the tree, by default None legend_col : str, optional The column used to color the tree items, by default None show_table: bool Set to True to show a data table, by default False. Other Parameters ---------------- height : int, optional The height of the plot figure (the default is 700) width : int, optional The width of the plot figure (the default is 900) title : str, optional Title to display (the default is None) Returns ------- Tuple[figure, LayoutDOM]: figure - The main bokeh.plotting.figure Layout - Bokeh layout structure. Raises ------ ProcessTreeSchemaException If the data set schema is not valid for the plot. Notes ----- The `output_var` variable will be overwritten with any selected values. """ check_kwargs(kwargs, _DEFAULT_KWARGS) reset_output() output_notebook() data, schema, levels, n_rows = _pre_process_tree(data, schema) if schema is None: raise ProcessTreeSchemaException( "Could not infer schema from data set.") source = ColumnDataSource(data=data) # Get legend/color bar map fill_map, color_bar = _create_fill_map(source, legend_col) max_level = max(levels) + 3 min_level = min(levels) plot_height: int = kwargs.pop("height", 700) plot_width: int = kwargs.pop("width", 900) title: str = kwargs.pop("title", "ProcessTree") if color_bar: title += " (color bar = {legend_col})" visible_range = int(plot_height / 35) y_start_range = (n_rows - visible_range, n_rows + 1) b_plot = figure( title=title, plot_width=plot_width, plot_height=plot_height, x_range=(min_level, max_level), y_range=y_start_range, tools=["ypan", "reset", "save", "tap"], toolbar_location="above", ) hover = HoverTool(tooltips=_get_tool_tips(schema), formatters={"TimeGenerated": "datetime"}) b_plot.add_tools(hover) # dodge to align rectangle with grid rect_x = dodge("Level", 1.75, range=b_plot.x_range) rect_plot_params = dict(width=3.5, height=0.95, source=source, fill_alpha=0.4, fill_color=fill_map) if color_bar: b_plot.add_layout(color_bar, "right") elif legend_col: rect_plot_params["legend_field"] = legend_col rect_plot = b_plot.rect(x=rect_x, y="Row", **rect_plot_params) if legend_col and not color_bar: b_plot.legend.title = legend_col text_props = { "source": source, "text_align": "left", "text_baseline": "middle" } def x_dodge(x_offset): return dodge("Level", x_offset, range=b_plot.x_range) def y_dodge(y_offset): return dodge("Row", y_offset, range=b_plot.y_range) b_plot.text(x=x_dodge(0.1), y=y_dodge(-0.2), text="cmd", text_font_size="7pt", **text_props) b_plot.text(x=x_dodge(0.1), y=y_dodge(0.25), text="Exe", text_font_size="8pt", **text_props) b_plot.text(x=x_dodge(1.8), y=y_dodge(0.25), text="PID", text_font_size="8pt", **text_props) # Plot options _set_plot_option_defaults(b_plot) b_plot.xaxis.ticker = sorted(levels) b_plot.xgrid.ticker = sorted(levels) b_plot.hover.renderers = [rect_plot] # only hover element boxes # Selection callback if output_var is not None: get_selected = _create_js_callback(source, output_var) b_plot.js_on_event("tap", get_selected) box_select = BoxSelectTool(callback=get_selected) b_plot.add_tools(box_select) range_tool = _create_vert_range_tool( data=source, min_y=0, max_y=n_rows, plot_range=b_plot.y_range, width=90, height=plot_height, x_col="Level", y_col="Row", fill_map=fill_map, ) plot_elems = row(b_plot, range_tool) if show_table: data_table = _create_data_table(source, schema, legend_col) plot_elems = column(plot_elems, data_table) show(plot_elems) return b_plot, plot_elems
def plot_entity_graph( entity_graph: nx.Graph, node_size: int = 25, font_size: Union[int, str] = 10, height: int = 800, width: int = 800, scale: int = 2, ) -> figure: """ Plot entity graph with Bokeh. Parameters ---------- entity_graph : nx.Graph The entity graph as a networkX graph node_size : int, optional Size of the nodes in pixels, by default 25 font_size : int, optional Font size for node labels, by default 10 Can be an integer (point size) or a string (e.g. "10pt") width : int, optional Width in pixels, by default 800 height : int, optional Image height (the default is 800) scale : int, optional Position scale (the default is 2) Returns ------- bokeh.plotting.figure The network plot. """ output_notebook() font_pnt = f"{font_size}pt" if isinstance(font_size, int) else font_size node_attrs = { node: attrs["color"] for node, attrs in entity_graph.nodes(data=True) } nx.set_node_attributes(entity_graph, node_attrs, "node_color") plot = figure( title="Alert Entity graph", x_range=(-3, 3), y_range=(-3, 3), width=width, height=height, ) plot.add_tools( HoverTool(tooltips=[ ("node_type", "@node_type"), ("name", "@name"), ("description", "@description"), ])) graph_renderer = from_networkx(entity_graph, nx.spring_layout, scale=scale, center=(0, 0)) graph_renderer.node_renderer.glyph = Circle(size=node_size, fill_color="node_color", fill_alpha=0.5) plot.renderers.append(graph_renderer) # pylint: disable=no-member # Create labels for name, pos in graph_renderer.layout_provider.graph_layout.items(): label = Label( x=pos[0], y=pos[1], x_offset=5, y_offset=5, text=name, text_font_size=font_pnt, ) plot.add_layout(label) # pylint: enable=no-member return plot
from bokeh.plotting import figure, output_file, show, ColumnDataSource from bokeh.models import HoverTool, CustomJS import pandas as pd from bokeh.io import output_notebook import numpy as np from bokeh.models import ( GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, PanTool, WheelZoomTool, BoxSelectTool, ResetTool, RedoTool, UndoTool ) from bokeh.models import GeoJSONDataSource output_notebook() #Get Data df = pd.read_csv("final_no_outlier") latitude = df['latitude'].tolist() longitude = df['longitude'].tolist() price = df['price per room'].tolist() r = [0.0003] * len(price) address = df['address'].tolist() distance = df['edge_distance'].tolist() sqft = df['sqft per room'].tolist() food = df['avg_rating'].tolist() cafes = df['#cafes'].tolist() pubs = df['#pubs'].tolist() # Create color span based on price colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"]
def visualize_portfolio_rl_bokeh(portfolio_state_list, reward_list, save_path=None, is_save=False, is_show=True, is_jupyter=True): """ [datetime] """ all_datetime_array = np.array([ get_naive_datetime_from_datetime(one_state.datetime) for one_state in portfolio_state_list ]) reward_array = np.array(reward_list) x = np.arange(0, len(portfolio_state_list)) layout_list = visualize_portfolio_transform_bokeh(portfolio_state_list, is_save=False, is_show=False) add_p1 = bokeh.plotting.figure(plot_width=1200, plot_height=300, title="報酬") add_p1.line(x, reward_array, legend_label="reward", line_width=2, color="green") add_p1.xaxis.major_label_overrides = { str(one_x): str(all_datetime_array[i]) for i, one_x in enumerate(x) } add_p1.yaxis[0].axis_label = "報酬" layout_list.extend([add_p1]) created_figure = bokeh.layouts.column(*layout_list) if is_save: if save_path.suffix == ".png": bokeh.io.export_png(created_figure, filename=save_path) elif save_path.suffix == ".html": output_file(save_path) bokeh.io.save(created_figure, filename=save_path, title="trading process") else: raise Exception( "The suffix of save_path is must be '.png' or '.html'.") return None if is_show: try: reset_output() if is_jupyter: output_notebook() show(created_figure) except: if is_jupyter: output_notebook() show(created_figure) return None if not is_save and not is_show: return layout_list