def cpu(doc): fig = figure(title="CPU Utilization [%]", sizing_mode="stretch_both", y_range=[0, 100]) cpu = psutil.cpu_percent(percpu=True) left = list(range(len(cpu))) right = [l + 0.8 for l in left] source = ColumnDataSource({"left": left, "right": right, "cpu": cpu}) mapper = LinearColorMapper(palette=all_palettes['RdYlBu'][4], low=0, high=100) fig.quad(source=source, left="left", right="right", bottom=0, top="cpu", color={ "field": "cpu", "transform": mapper }) doc.title = "CPU Usage" doc.add_root(fig) def cb(): source.data.update({"cpu": psutil.cpu_percent(percpu=True)}) doc.add_periodic_callback(cb, 200)
def gpu(doc): fig = figure(title="GPU Utilization", sizing_mode="stretch_both", x_range=[0, 100]) def get_utilization(): return [pynvml.nvmlDeviceGetUtilizationRates( gpu_handles[i]).gpu for i in range(ngpus)] gpu = get_utilization() y = list(range(len(gpu))) # right = [l + 0.8 for l in left] source = ColumnDataSource({"right": y, "gpu": gpu}) mapper = LinearColorMapper( palette=all_palettes['RdYlBu'][4], low=0, high=100) fig.hbar( source=source, y="right", right='gpu', height=0.8, color={"field": "gpu", "transform": mapper} ) fig.toolbar_location = None doc.title = "GPU Utilization [%]" doc.add_root(fig) def cb(): source.data.update({"gpu": get_utilization()}) doc.add_periodic_callback(cb, 200)
def plot_image(image, width=800, downsample=2, title=None): """ plots image downsampled, returning bokeh figure of requested width """ #- Downsample image 2x2 (or whatever downsample specifies) ny, nx = image.shape image2 = downsample_image(image, downsample) #- Default image scaling zscale = ZScaleInterval() zmin, zmax = zscale.get_limits(image2) #- Experimental: rescale to uint8 to save space u8img = (255 * (image2.clip(zmin, zmax) - zmin) / (zmax - zmin)).astype( np.uint8) colormap = LinearColorMapper(palette=gray(256), low=0, high=255) #- Create figure fig = bk.figure(width=width, height=width - 50, active_drag='box_zoom', active_scroll='wheel_zoom') fig.image([ u8img, ], 0, 0, nx, ny, color_mapper=colormap) fig.x_range.start = 0 fig.x_range.end = nx fig.y_range.start = 0 fig.y_range.end = ny if title is not None: fig.title.text = title return fig
def correlation_plot(marker_value, pats_data): """ Shows correlation between measueremts :param marker_value: the selecte marker :param pats_data: dict of patients data {"measurments_name" : dataframe_with_patient_data} :return: figure, (a heatmap) """ if marker_value != "None": patients_list = list(pats_data.keys()) p = figure(title="correlation on marker " + str(marker_value), x_range=patients_list, y_range=patients_list, plot_width=800, plot_height=800, tools='pan, box_zoom,reset, wheel_zoom', tooltips=[('rate', '@rate%')], y_axis_location="right") 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 = pi / 2 df = pd.DataFrame(index=pats_data[patients_list[0]].index.copy()) for pat in patients_list: df[pat] = pats_data[pat][marker_value] if marker_value in pats_data[ pat].columns else np.NaN df.columns = patients_list df = pd.DataFrame(df.corr().stack(), columns=['rate']).reset_index() mapper = LinearColorMapper(palette=hf.create_color_map(), high=1, high_color='red', low=-1, low_color='blue') color_bar = ColorBar(color_mapper=mapper, location=(0, 0)) p.rect(x="level_0", y="level_1", width=1, height=1, source=df, fill_color={ 'field': 'rate', 'transform': mapper }, line_color=None) p.add_layout(color_bar, 'left') else: p = figure(plot_height=800, plot_width=800, tools='pan, box_zoom, reset, wheel_zoom', toolbar_location="above") return p
def test_LinearColorMapper(): mapper = LinearColorMapper() yield ( check_properties_existence, mapper, ["palette", "low", "high", "nan_color"], )
def make_color_array(self): dc = self.data[self.colorcol] colmax = np.amax(dc) colmin = np.amin(dc) delt = 1. * (colmax - colmin) / (len(self.colortable) - 1) bins = (dc - colmin) // delt bins = bins.astype(np.int8) self.binedges = np.arange(colmin, colmax + delt, (colmax - colmin) / 8) colors = self.colortable[bins] self.color_array = colors self.data['colors'] = colors self.ticker = FixedTicker(ticks=self.binedges) self.mapper = LinearColorMapper(palette=self.colortable) if self.color_bar is not None: self.color_bar.ticker = self.ticker self.color_bar.color_mapper = self.mapper #self.formatter = NumeralTickFormatter(format="%5.2e") self.mapper.low = colmin self.mapper.high = colmax print("Done with color array")
def set_colors(cval, plt_name='cosmo'): if (plt_name == 'cosmo'): plt = cosmo_palatte() # colormap1=RGBAColorMapper(min(cval),max(cval),plt1) # rgb1=colormap1.color(cval) # plt = ["#%02x%02x%02x" % (c[0],c[1],c[2]) for c in rgb1] if (plt_name == 'Spectral6'): plt = Spectral6 if (plt_name == 'Inferno256'): plt = Inferno256 if (plt_name == 'Viridis256'): plt = Viridis256 if (plt_name == 'Greys256'): plt = Greys256 if (plt_name == 'Magma256'): plt = Magma256 if (plt_name == 'Plasma256'): plt = Plasma256 colormap = RGBAColorMapper(min(cval), max(cval), plt) rgb = colormap.color(cval) colors = ["#%02x%02x%02x" % (c[0], c[1], c[2]) for c in rgb] color_mapper = LinearColorMapper(palette=plt, low=min(cval), high=max(cval)) colorbar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) return colors, colorbar
def plot_tango_attribute(ns): redirect_bokeh_output() n = ns.attr.count("/") if not n: ap = tango.AttributeProxy(ns.attr) av = ap.read() fqan = ap.get_device_proxy().name() + "/" + ap.name() title = ns.attr + " [" + fqan + "]" elif n == 3: dn, _, an = ns.attr.rpartition("/") dp = tango.DeviceProxy(dn) av = dp.read_attribute(an) fqan = ns.attr else: raise Exception( "invalid attribute name specified - expected an alias or something like 'fully/qualified/attribute/name'" ) kwargs = dict() kwargs['tools'] = 'pan,wheel_zoom,box_select,reset,hover' kwargs['title'] = fqan + ' @ ' + datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") if ns.width is not None: kwargs['plot_width'] = ns.width if ns.height is not None: kwargs['plot_height'] = ns.height upsidedown = ns.upsidedown if ns.upsidedown is not None else False plot = None if av.data_format == tango.AttrDataFormat.SCALAR: print(fqan + " = " + str(av.value)) elif av.data_format == tango.AttrDataFormat.SPECTRUM: kwargs['toolbar_location'] = 'above' plot = figure(**kwargs) plot.line(range(av.value.shape[0]), av.value, line_width=1) plot.circle(range(av.value.shape[0]), av.value, size=3) elif av.data_format == tango.AttrDataFormat.IMAGE: kwargs['toolbar_location'] = 'right' lcm = LinearColorMapper(palette=Plasma256) ymin = 0 if not upsidedown else av.dim_y ymax = av.dim_y if not upsidedown else 0 plot = figure(x_range=(0, av.dim_x), y_range=(ymin, ymax), **kwargs) image = av.value if not upsidedown else av.value[::-1] plot.image(image=[image], x=0, y=ymin, dw=av.dim_x, dh=av.dim_y, color_mapper=lcm) else: print(fqan + " has an unknown/unsupported attribute data format [{}]".format( str(av.data_format))) if plot: ht = plot.select(HoverTool)[0] ht.tooltips = [("index", "$index"), ("(x,y)", "(@x, @y)")] plot.toolbar.active_drag = None plot.toolbar.active_scroll = None plot.toolbar.logo = None tango_attribute_plots[fqan] = show(plot, notebook_handle=True)
def handle_palette_change(self, attr, old, new): """ Changes the figures' palette according to the user's selection. """ palette = palette_dict[new] for plane in self.figures: image_model = self.get_image_model(plane) image_model.glyph.color_mapper = LinearColorMapper(palette=palette)
def pci(doc): tx_fig = figure(title="TX Bytes [MB/s]", sizing_mode="stretch_both", y_range=[0, 5000]) pci_tx = [pynvml.nvmlDeviceGetPcieThroughput( gpu_handles[i], pynvml.NVML_PCIE_UTIL_TX_BYTES)/1024 for i in range(ngpus)] left = list(range(len(pci_tx))) right = [l + 0.8 for l in left] source = ColumnDataSource({"left": left, "right": right, "pci-tx": pci_tx}) mapper = LinearColorMapper( palette=all_palettes['RdYlBu'][4], low=0, high=5000) tx_fig.quad( source=source, left="left", right="right", bottom=0, top="pci-tx", color={"field": "pci-tx", "transform": mapper} ) rx_fig = figure(title="RX Bytes [MB/s]", sizing_mode="stretch_both", y_range=[0, 5000]) pci_rx = [pynvml.nvmlDeviceGetPcieThroughput( gpu_handles[i], pynvml.NVML_PCIE_UTIL_RX_BYTES)/1024 for i in range(ngpus)] left = list(range(len(pci_rx))) right = [l + 0.8 for l in left] source = ColumnDataSource({"left": left, "right": right, "pci-rx": pci_rx}) mapper = LinearColorMapper( palette=all_palettes['RdYlBu'][4], low=0, high=5000) rx_fig.quad( source=source, left="left", right="right", bottom=0, top="pci-rx", color={"field": "pci-rx", "transform": mapper} ) doc.title = "PCI Throughput" doc.add_root( column(tx_fig, rx_fig, sizing_mode="stretch_both") ) def cb(): src_dict = {} src_dict["pci-tx"] = [pynvml.nvmlDeviceGetPcieThroughput( gpu_handles[i], pynvml.NVML_PCIE_UTIL_TX_BYTES)/1024 for i in range(ngpus)] src_dict["pci-rx"] = [pynvml.nvmlDeviceGetPcieThroughput( gpu_handles[i], pynvml.NVML_PCIE_UTIL_RX_BYTES)/1024 for i in range(ngpus)] source.data.update(src_dict) doc.add_periodic_callback(cb, 200)
def plot(self, lowest_value=None, highest_value=None, palette=None, **fig_args): if not palette: palette = list(reversed(Viridis256)) if not lowest_value: lowest_value = np.min(np.abs(self.array)) if not highest_value: highest_value = np.max(np.abs(self.array)) default_args = { 'width': 900, 'height': 400, 'x_axis_label': 'time [s]', 'y_axis_label': 'frequency [Hz]', 'tools': 'hover,pan,wheel_zoom,box_zoom,zoom_in,zoom_out,save,reset', 'active_drag': 'pan', 'active_inspect': None, 'active_scroll': None, 'toolbar_location': 'above', 'tooltips': [('time [s]', '$x{0.000}'), ('frequency [Hz]', '$y{0.}'), ['amplitude', '@image']], } if self.power == 2: default_args['tooltips'][2][0] = 'power' if self.decibels: default_args['tooltips'][2][0] += ' [dB]' fig = figure(**{**default_args, **fig_args}) if isinstance(fig.x_range, DataRange1d): fig.x_range.range_padding = 0 if isinstance(fig.y_range, DataRange1d): fig.y_range.range_padding = 0 mapper = LinearColorMapper(palette=palette, low=lowest_value, high=highest_value) fig.image([np.abs(self.array)], x=self.times[0], y=self.frequencies[0], dw=self.times[-1], dh=self.frequencies[-1], color_mapper=mapper) return fig
def _create_mapper(adata, key): """ Helper function to create CategoricalColorMapper from annotated data. Params -------- adata: AnnData annotated data object key: str key in `adata.obs.obs_keys()` or `adata.var_names`, for which we want the colors; if no colors for given column are found in `adata.uns[key_colors]`, use `viridis` palette Returns -------- mapper: bokeh.models.mappers.CategoricalColorMapper mapper which maps valuems from `adata.obs[key]` to colors """ if not key in adata.obs_keys(): assert key in adata.var_names, f'`{key}` not found in `adata.obs_keys()` or `adata.var_names`' ix = np.where(adata.var_names == key)[0][0] vals = list(adata.X[:, ix]) palette = cm.get_cmap('viridis', adata.n_obs) mapper = dict(zip(vals, range(len(vals)))) palette = to_hex_palette(palette([mapper[v] for v in vals])) return LinearColorMapper(palette=palette, low=np.min(vals), high=np.max(vals)) is_categorical = adata.obs[key].dtype.name == 'category' default_palette = cm.get_cmap('viridis', adata.n_obs if not is_categorical else len(adata.obs[key].unique())) palette = adata.uns.get(f'{key}_colors', default_palette) if palette is default_palette: vals = adata.obs[key].unique() if is_categorical else adata.obs[key] mapper = dict(zip(vals, range(len(vals)))) palette = palette([mapper[v] for v in vals]) palette = to_hex_palette(palette) if is_categorical: return CategoricalColorMapper(palette=palette, factors=list(map(str, adata.obs[key].cat.categories))) return LinearColorMapper(palette=palette, low=np.min(adata.obs[key]), high=np.max(adata.obs[key]))
def get_color_mapper(column, df, palette='Viridis256'): ''' Return a color mapper instace for a given category or continuous properties Args: column : str name of the color that should be color mapped df : pandas.DataFrame data frame ''' cmaps = { 'block': 'Set1_4', 'period': 'Dark2_7', 'name_series': 'Spectral10', 'group_name': viridis(18), 'is_radioactive': d3['Category10'][4][2:], 'is_monoisotopic': d3['Category10'][4][2:], 'goldschmidt_class': 'Set2_4', 'geochemical_class': d3['Category10'][10], } if column in cmaps.keys(): factors = list(df[column].unique()) if any(x is None for x in factors): factors = sorted([x for x in factors if x is not None]) + [None] elif column == 'group_name': factors = [ str(x) for x in sorted(int(s) for s in factors if s != 'f block') ] + ['f block'] else: factors = sorted(factors) ccm = CategoricalColorMapper(palette=cmaps[column], factors=factors, nan_color='#ffffff') elif column == 'value': if df[column].skew() > SKEW_THRS: ccm = LogColorMapper(palette=palette, low=df[column].min(), high=df[column].max(), nan_color='#ffffff') else: ccm = LinearColorMapper(palette=palette, low=df[column].min(), high=df[column].max(), nan_color='#ffffff') else: ccm = None return ccm
def _glyph_properties(self, plot, element, source, ranges): properties = super(RasterPlot, self)._glyph_properties(plot, element, source, ranges) properties = {k: v for k, v in properties.items()} val_dim = [d.name for d in element.vdims][0] low, high = ranges.get(val_dim) if 'cmap' in properties: palette = mplcmap_to_palette(properties.pop('cmap', None)) cmap = LinearColorMapper(palette, low=low, high=high) properties['color_mapper'] = cmap if 'color_mapper' not in self.handles: self.handles['color_mapper'] = cmap return properties
def plot_spectrogram(): # Spectrogram image plt = figure(plot_width=WIDTHS[0], plot_height=HEIGHTS[0], toolbar_location=None, tools="", x_range=[0, SPEC_WIDTH], y_range=[0, SPEC_HEIGHT]) plt.image('value', x=0, y=0, dw=SPEC_WIDTH, dh=SPEC_HEIGHT, name='spectrogram', color_mapper=LinearColorMapper(SPEC_PALETTE, low=0, high=100), source=SPECTROGRAM) # X ticks plt.xaxis[0].ticker = FixedTicker(ticks=[]) # X axis plt.xaxis.axis_line_color = None # Y ticks plt.yaxis[0].ticker = FixedTicker(ticks=[]) plt.yaxis.major_label_text_font_size = '0pt' plt.yaxis.major_tick_line_color = None # Y axis plt.yaxis.axis_line_color = None plt.yaxis.axis_label = 'Mel bands' plt.yaxis.axis_label_text_font = TEXT_FONT plt.yaxis.axis_label_text_font_size = '8pt' plt.yaxis.axis_label_text_font_style = 'normal' # Plot fill/border plt.background_fill_color = GRID_COLOR plt.outline_line_color = GRID_COLOR plt.min_border = 10 # Plot title plt.title.text = 'Mel-scaled power spectrogram (perceptually weighted):' plt.title.align = 'left' plt.title.text_color = TEXT_COLOR plt.title.text_font = TEXT_FONT plt.title.text_font_size = '9pt' plt.title.text_font_style = 'normal' return plt
def makeplot(self): dc = self.data[self.colorcol] colmax = np.amax(dc) colmin = np.amin(dc) title = self.datafile.split("/")[-1] + '\n' + self.sheetname self.source = ColumnDataSource(self.data) self.x = self.data[self.xcol] self.y = self.data[self.ycol] self.figure = figure(tools=self.tools, title=title, plot_width=600, plot_height=600, toolbar_location='above', min_border_right=100, min_border_left=100, min_border_top=100) self.plot = self.figure.circle(x='x', y='y', source=self.source, size='sizes', fill_color='colors', fill_alpha=0.95, line_color='#888888', line_width=.5, selection_line_color='blue', nonselection_fill_color='colors', nonselection_fill_alpha=0.65) self.mapper = LinearColorMapper(palette=self.colortable, low=colmin, high=colmax) self.color_bar = ColorBar( color_mapper=self.mapper, ticker=self.ticker, #formatter=self.formatter, label_standoff=12, border_line_color=None, location=(0, 0)) self.figure.add_layout(self.color_bar, 'right') self.plot.data_source.on_change('selected', self.update) self.figure.xaxis.axis_label = self.xcol self.figure.yaxis.axis_label = self.ycol print("plot done")
def get_color_mapper(column, df, palette="Viridis256"): """ Return a color mapper instace for a given category or continuous properties Args: column : str name of the color that should be color mapped df : pandas.DataFrame data frame """ cmaps = { "block": "Set1_4", "period": "Dark2_7", "name_series": "Spectral10", "group_name": viridis(18), "is_radioactive": d3["Category10"][4][2:], "is_monoisotopic": d3["Category10"][4][2:], "goldschmidt_class": "Set2_4", "geochemical_class": d3["Category10"][10], } if column in cmaps.keys(): factors = list(df[column].unique()) if any(x is None for x in factors): factors = sorted([x for x in factors if x is not None]) + [None] elif column == "group_name": factors = [str(x) for x in sorted(int(s) for s in factors if s != "f block")] + ["f block"] else: factors = sorted(factors) ccm = CategoricalColorMapper(palette=cmaps[column], factors=factors, nan_color="#ffffff") elif column == "value": if df[column].skew() > SKEW_THRS: ccm = LogColorMapper(palette=palette, low=df[column].min(), high=df[column].max(), nan_color="#ffffff") else: ccm = LinearColorMapper(palette=palette, low=df[column].min(), high=df[column].max(), nan_color="#ffffff") else: ccm = None return ccm
def gpu_mem(doc): def get_mem(): return [ pynvml.nvmlDeviceGetMemoryInfo(handle).used for handle in gpu_handles ] def get_total(): return pynvml.nvmlDeviceGetMemoryInfo(gpu_handles[0]).total fig = figure(title="GPU Memory", sizing_mode="stretch_both", x_range=[0, get_total()]) gpu = get_mem() y = list(range(len(gpu))) source = ColumnDataSource({"right": y, "gpu": gpu}) mapper = LinearColorMapper(palette=all_palettes["RdYlBu"][8], low=0, high=get_total()) fig.hbar( source=source, y="right", right="gpu", height=0.8, color={ "field": "gpu", "transform": mapper }, ) fig.xaxis[0].formatter = NumeralTickFormatter(format="0.0 b") fig.xaxis.major_label_orientation = -math.pi / 12 fig.toolbar_location = None doc.title = "GPU Memory" doc.add_root(fig) def cb(): mem = get_mem() source.data.update({"gpu": mem}) fig.title.text = "GPU Memory: {}".format(format_bytes(sum(mem))) doc.add_periodic_callback(cb, 200)
def _plot_Ts_grid(self): rdylbu = bpal.RdYlBu[11] cmapper = LinearColorMapper(palette=rdylbu, low=32, high=112) self.img = self.plot.image([self.Ts], [0], [0.1], [1], [150], color_mapper=cmapper) self.img.level = 'underlay' self.plot.xaxis.axis_label = 'Albedo' self.plot.yaxis.axis_label = 'Greenhouse effect' title_text = ('Surface Temp. for solar input of {:.2f} W/m^2' ''.format(self.S0)) self.plot.title.text = title_text color_bar = ColorBar(color_mapper=cmapper, major_label_text_font_size="12pt", label_standoff=6, location=(0, 0)) self.plot.add_layout(color_bar, 'right')
def plot_map(self, estados, ano): lati = [] long = [] for i in range(0, len(estados)): lati.append(estados['Latitude'][i]) long.append(estados['Longitude'][i]) vinculo = self.__appendData(ano) map_options = GMapOptions(lat=-16.1610697, lng=-59.8988937, map_type="roadmap", zoom=5) plot = GMapPlot(x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options) plot.title.text = "Mapa de gastos do Brasil de " + ano # Para pegar uma chave acesse: # https://developers.google.com/maps/documentation/javascript/get-api-key plot.api_key = "API GOOGLE" source = ColumnDataSource( data=dict(lat=lati, lon=long, size=vinculo, color=vinculo)) color_mapper = LinearColorMapper(palette=Viridis5) circle = Circle(x="lon", y="lat", size="size", fill_color={ 'field': 'color', 'transform': color_mapper }, fill_alpha=0.4, line_color=None) plot.add_glyph(source, circle) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) plot.add_layout(color_bar, 'right') plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool()) output_notebook() show(plot)
def linear_cmap(field_name, palette, low, high, low_color=None, high_color=None, nan_color="gray"): ''' Create a ``DataSpec`` dict to apply a client-side ``LinearColorMapper`` transformation to a ``ColumnDataSource`` column. Args: field_name (str) : a field name to configure ``DataSpec`` with palette (seq[color]) : a list of colors to use for colormapping low (float) : a minimum value of the range to map into the palette. Values below this are clamped to ``low``. high (float) : a maximum value of the range to map into the palette. Values above this are clamped to ``high``. low_color (color, optional) : color to be used if data is lower than ``low`` value. If None, values lower than ``low`` are mapped to the first color in the palette. (default: None) high_color (color, optional) : color to be used if data is higher than ``high`` value. If None, values higher than ``high`` are mapped to the last color in the palette. (default: None) nan_color (color, optional) : a default color to use when mapping data from a column does not succeed (default: "gray") ''' return field( field_name, LinearColorMapper(palette=palette, low=low, high=high, nan_color=nan_color, low_color=low_color, high_color=high_color))
def fig2a(D, x_max=30, y_max=10, palette=palette_20, title='P(survive to adulthood)', show=True, plot_width=500, plot_height=400, tools=''): fig = bkp.figure(x_range=(0, x_max), y_range=(0, y_max), plot_width=plot_width, plot_height=plot_height, tools=tools, title=title) fig.title.text_font_size = '12pt' fig.title.align = 'center' tweak_fig(fig, three_ticks_enable=False, grid=False) cmap = LinearColorMapper(high=1.0, low=0.0, palette=palette) Dmat = fig.image(image=[D], x=0, y=0, dw=x_max, dh=y_max, color_mapper=cmap) fig.xaxis.axis_label = "Birth age (month)" fig.yaxis.axis_label = "Brain size (radius, cm)" # color_bar = bkm.ColorBar(color_mapper=cmap, label_standoff=12, location=(0,0), # border_line_color=None, bar_line_color='black', major_tick_line_color='black') # # fig.add_layout(color_bar, 'right') if show: handle = bkp.show(fig, notebook_handle=True) else: handle = None return handle, fig, Dmat.data_source.data
def update_plots(new): print("Starting update") nonlocal Estimators if not isinstance(Estimators, (type(np.array), list)): Estimators = np.array(Estimators) estimator_names = np.array(list(estimator_select.value)) ix = np.isin(Estimator_Names, estimator_names) estimator_indices = [int(i) for i in np.where(ix)[0].flatten()] estimators = np.array(Estimators)[estimator_indices] variable1 = drop1.value variable2 = drop2.value y = drop3.value #Things to update: # image background i.e. image source √ # observation source √ #Color mapper values√ #hover tool values √ #Figure ranges √ #Model score text things √ #Lets calculate all the image and observation data first plots = [None for i in range(len(estimators))] image_sources = [None for i in range(len(estimators))] observation_sources = [None for i in range(len(estimators))] hover_tools = [None for i in range(len(estimators))] model_score_sources = [None for i in range(len(estimators))] glyphs0 = [None for i in range(len(estimators))] color_bars = [None for i in range(len(estimators))] p_circles = [None for i in range(len(estimators))] p_images = [None for i in range(len(estimators))] #Iterate over the estimators for idx, estimator in enumerate(estimators): #Find the title for each plot estimator_name = str(estimator()).split('(')[0] #Extract the needed data full_mat = X[[variable1, variable2, y]].dropna(how="any", axis=0) #Define a class bijection for class colour mapping unique_classes, y_bijection = np.unique(full_mat[y], return_inverse=True) full_mat['y_bijection'] = y_bijection #Rescale the X Data so that the data fits nicely on the axis/predictions are reliable full_mat[variable1 + "_s"] = StandardScaler().fit_transform( full_mat[variable1].values.reshape((-1, 1))) full_mat[variable2 + "_s"] = StandardScaler().fit_transform( full_mat[variable2].values.reshape((-1, 1))) #Define the Step size in the mesh delta = Delta #Separate the data into arrays so it is easy to work with X1 = full_mat[variable1 + "_s"].values X2 = full_mat[variable2 + "_s"].values Y = full_mat["y_bijection"].values #Define the mesh-grid co-ordiantes over which to colour in x1_min, x1_max = X1.min() - 0.5, X1.max() + 0.5 x2_min, x2_max = X2.min() - 0.5, X2.max() + 0.5 #Create the meshgrid itself x1, x2 = np.arange(x1_min, x1_max, delta), np.arange(x2_min, x2_max, delta) x1x1, x2x2 = np.meshgrid(x1, x2) #Create the train test split X_train, X_test, y_train, y_test = train_test_split( full_mat[[variable1 + "_s", variable2 + "_s"]], Y, test_size=Test_Size, random_state=Random_State) #Fit and predict/score the model model = estimator().fit(X=X_train, y=y_train) # train_preds = model.predict(X_train) # test_preds = model.predict(X_test) model_score = model.score(X_test, y_test) model_score_text = "Model score: %.2f" % model_score if hasattr(model, "decision_function"): Z = model.decision_function(np.c_[x1x1.ravel(), x2x2.ravel()]) elif hasattr(model, "predict_proba"): Z = model.predict_proba(np.c_[x1x1.ravel(), x2x2.ravel()]) else: print( "This Estimator doesn't have a decision_function attribute and can't predict probabilities" ) Z = np.argmax(Z, axis=1) Z_uniques = np.unique(Z) unique_predictions = unique_classes[Z_uniques] Z = Z.reshape(x1x1.shape) #Add in the probabilities and predicitions for the tooltips full_mat["probability"] = np.amax(model.predict_proba( full_mat[[variable1 + "_s", variable2 + "_s"]]), axis=1) bijected_predictions = model.predict( full_mat[[variable1 + "_s", variable2 + "_s"]]) full_mat["prediction"] = unique_classes[bijected_predictions] #Add an associated color to the predictions number_of_colors = len(np.unique(y_bijection)) #Create the hover tool to be updated hover = HoverTool(tooltips=[( variable1, "@" + variable1), (variable2, "@" + variable2), ("Probability", "@probability"), ("Prediction", "@prediction"), ("Actual", "@" + y)]) #Create the axes for all the plots plots[idx] = figure(x_axis_label=variable1, y_axis_label=variable2, title=estimator_name, x_range=(x1x1.min(), x1x1.max()), y_range=(x2x2.min(), x2x2.max()), plot_height=600, plot_width=600) #Create all the image sources image_data = dict() image_data['x'] = np.array([x1x1.min()]) image_data["y"] = np.array([x2x2.min()]) image_data['dw'] = np.array([x1x1.max() - x1x1.min()]) image_data['dh'] = np.array([x2x2.max() - x2x2.min()]) image_data['boundaries'] = [Z] image_sources[idx] = ColumnDataSource(image_data) #Create all the updatable images (boundaries) p_images[idx] = plots[idx].image(image='boundaries', x='x', y='y', dw='dw', dh='dh', palette="RdBu11", source=image_sources[idx]) #Create the sources to update the observation points observation_sources[idx] = ColumnDataSource(data=full_mat) #Create all the updatable points low = full_mat["y_bijection"].min() high = full_mat["y_bijection"].max() cbar_mapper = LinearColorMapper(palette=RdBu[number_of_colors], high=high, low=low) p_circles[idx] = plots[idx].circle( x=variable1 + "_s", y=variable2 + "_s", color=dict(field='y_bijection', transform=cbar_mapper), source=observation_sources[idx], line_color="black") #Create the hovertool for each plot hover_tools[idx] = hover #Add the hover tools to each plot plots[idx].add_tools(hover_tools[idx]) #Create all the text sources (model scores) for the plots model_score_sources[idx] = ColumnDataSource( data=dict(x=[x1x1.min() + 0.3], y=[x2x2.min() + 0.3], text=[model_score_text])) #Add the model scores to all the plots score_as_text = Text(x="x", y="y", text="text") glyphs0[idx] = plots[idx].add_glyph(model_score_sources[idx], score_as_text) #Add a colorbar color_bars[idx] = ColorBar( color_mapper=cbar_mapper, ticker=BasicTicker(desired_num_ticks=number_of_colors), label_standoff=12, location=(0, 0), bar_line_color="black") plots[idx].add_layout(color_bars[idx], "right") plots[idx].add_tools(LassoSelectTool(), WheelZoomTool()) # configure so that no drag tools are active plots[idx].toolbar.tools = plots[idx].toolbar.tools[1:] plots[idx].toolbar.tools[0], plots[idx].toolbar.tools[ -2] = plots[idx].toolbar.tools[-2], plots[ idx].toolbar.tools[0] layout = gridplot([ widgetbox(drop1, drop2, drop3, estimator_select, update_drop) ], [row(plot) for plot in plots]) return layout #Finished the callback print("Ending Update") push_notebook(handle=handle0)
colors = YlOrRd8[0:5][::-1] sources = {} for i in range(len(colors)): mask = df[(df['FF_PCT'] > bins[i]) & (df['FF_PCT'] < bins[i + 1])] cds = ColumnDataSource(mask) sources[i] = cds fig.circle('x', 'y', line_color=None, fill_color=colors[i], size=5, source=sources[i]) # ColorBar Legend color_mapper = LinearColorMapper(palette=colors, low=0, high=100) color_bar = ColorBar(color_mapper=color_mapper, ticker=FixedTicker(ticks=[0, 20, 40, 60, 80, 100]), label_standoff=12, border_line_color=None, location=(0, 0), title='Percentile') fig.add_layout(color_bar, 'right') ## Callback function for widgets # Change data source (i.e. which DataFrame to use) def callback_change_dataframe(new): # New datasources global df_dict, card_type_dict card_type = card_type_dict[metrocard_type_buttons.active] # Get card type
def create(cls): # ============================================================================== # creates initial layout and data # ============================================================================== obj = cls() # initialize data source obj.source = ColumnDataSource(data=dict(z=[])) # initialize controls # slider controlling stepsize of the solver obj.value2 = Slider(title="value2", name='value2', value=1, start=-1, end=+1, step=.1) # slider controlling initial value of the ode obj.value1 = Slider(title="value1", name='value1', value=0, start=-1, end=+1, step=.1) # initialize plot toolset = "crosshair,pan,reset,resize,wheel_zoom,box_zoom" # Generate a figure container plot = figure( title_text_font_size="12pt", plot_height=400, plot_width=400, tools=toolset, # title=obj.text.value, title="somestuff", x_range=[-1, 1], y_range=[-1, 1]) # Plot the numerical solution by the x,t values in the source property plot.image( image='z', x=-1, y=-1, dw=2, dh=2, #palette="Spectral11", color_mapper=LinearColorMapper(palette=svg_palette_jet, low=-2, high=2), source=obj.source) obj.plot = plot # calculate data obj.update_data() # lists all the controls in our app obj.controls = VBoxForm(children=[obj.value1, obj.value2]) # make layout obj.children.append(obj.plot) obj.children.append(obj.controls) # don't forget to return! return obj
def plot_ortho(self): # Plot the map plot_ortho = figure( aspect_ratio=2, toolbar_location=None, x_range=(-2, 2), y_range=(-1, 1), id="plot_ortho", name="plot_ortho", min_border_left=0, min_border_right=0, css_classes=["plot_ortho_{:d}".format(self.counter)], ) plot_ortho.axis.visible = False plot_ortho.grid.visible = False plot_ortho.outline_line_color = None color_mapper = LinearColorMapper( palette="Plasma256", nan_color="white", low=self.vmin_o, high=self.vmax_o, ) plot_ortho.image( image="ortho", x=-1, y=-1, dw=2, dh=2, color_mapper=color_mapper, source=self.source_ortho, ) plot_ortho.toolbar.active_drag = None plot_ortho.toolbar.active_scroll = None plot_ortho.toolbar.active_tap = None # Plot the lat/lon grid lat_lines = get_ortho_latitude_lines(inc=self.inc) for x, y in lat_lines: plot_ortho.line(x, y, line_width=1, color="black", alpha=0.25) for i in range(len(self.lon_x[0])): plot_ortho.line( "x{:d}".format(i), "y{:d}".format(i), line_width=1, color="black", alpha=0.25, source=self.source_ortho_lon, ) self.add_border(plot_ortho, "ortho") # Interaction: Rotate the star as the mouse wheel moves mouse_wheel_callback = CustomJS( args={ "source_ortho": self.source_ortho, "source_index": self.source_index, "source_flux": self.source_flux, "source_ortho_lon": self.source_ortho_lon, "lon_x": self.lon_x, "lon_y": self.lon_y, "nlon": len(self.lon_x[0]), "ortho": self.ortho, "flux0": self.flux0, "flux": self.flux, "npix_o": self.npix_o, "nt": self.nt, "speed": self.nt / 200, }, code=""" // Update the current theta index var delta = cb_obj["delta"]; var t = source_index.data["t"][0]; t += delta * speed; while (t < 0) t += nt; while (t > nt - 1) t -= nt; source_index.data["t"][0] = t; source_index.change.emit(); var tidx = Math.floor(t); while (tidx < 0) tidx += nt; while (tidx > nt - 1) tidx -= nt; // Update the map source_ortho.data["ortho"][0] = ortho[tidx]; source_ortho.change.emit(); // Update the longitude lines var k; for (var k = 0; k < nlon; k++) { source_ortho_lon.data["x" + k] = lon_x[tidx][k]; source_ortho_lon.data["y" + k] = lon_y[tidx][k]; } source_ortho_lon.change.emit(); // Update the flux source_flux.data["flux"] = flux[tidx]; source_flux.data["flux0"] = flux0[tidx]; source_flux.change.emit(); """, ) plot_ortho.js_on_event(MouseWheel, mouse_wheel_callback) mouse_enter_callback = CustomJS(code=""" DISABLE_WHEEL = true; """) plot_ortho.js_on_event(MouseEnter, mouse_enter_callback) mouse_leave_callback = CustomJS(code=""" DISABLE_WHEEL = false; """) plot_ortho.js_on_event(MouseLeave, mouse_leave_callback) return plot_ortho
def plot_moll(self): # Plot the map plot_moll = figure( aspect_ratio=2, toolbar_location=None, x_range=(-2, 2), y_range=(-1, 1), id="plot_moll", name="plot_moll", ) plot_moll.axis.visible = False plot_moll.grid.visible = False plot_moll.outline_line_color = None color_mapper = LinearColorMapper( palette="Plasma256", nan_color="white", low=self.vmin_m, high=self.vmax_m, ) plot_moll.image( image="moll", x=-2, y=-1, dw=4, dh=2, color_mapper=color_mapper, source=self.source_moll, ) plot_moll.toolbar.active_drag = None plot_moll.toolbar.active_scroll = None plot_moll.toolbar.active_tap = None # Plot the lat/lon grid lat_lines = get_moll_latitude_lines() lon_lines = get_moll_longitude_lines() for x, y in lat_lines: plot_moll.line( x / np.sqrt(2), y / np.sqrt(2), line_width=1, color="black", alpha=0.25, ) for x, y in lon_lines: plot_moll.line( x / np.sqrt(2), y / np.sqrt(2), line_width=1, color="black", alpha=0.25, ) self.add_border(plot_moll, "moll") # Interaction: show spectra at different points as mouse moves mouse_move_callback = CustomJS( args={ "source_spec": self.source_spec, "moll": self.moll, "spec": self.spec, "npix_m": self.npix_m, "nc": self.nc, "nws": self.nws, }, code=""" var x = cb_obj["x"]; var y = cb_obj["y"]; if ((x > - 2) && (x < 2) && (y > -1) && (y < 1)) { // Image index below cursor var i = Math.floor(0.25 * (x + 2) * npix_m); var j = Math.floor(0.5 * (y + 1) * npix_m); // Compute weighted spectrum if (!isNaN(moll[0][j][i])) { var local_spec = new Array(nws).fill(0); for (var k = 0; k < nc; k++) { var weight = moll[k][j][i]; for (var l = 0; l < nws; l++) { local_spec[l] += weight * spec[k][l] } } // Update the plot source_spec.data["spec"] = local_spec; source_spec.change.emit(); } } """, ) plot_moll.js_on_event(MouseMove, mouse_move_callback) # Interaction: Cycle through wavelength as mouse wheel moves mouse_wheel_callback = CustomJS( args={ "source_moll": self.source_moll, "source_index": self.source_index, "spec_vline": self.spec_vline, "moll": self.moll, "spec": self.spec, "wavs": self.wavs, "npix_m": self.npix_m, "nc": self.nc, "nws": self.nws, }, code=""" // Update the current wavelength index var delta = Math.floor(cb_obj["delta"]); var l = source_index.data["l"][0]; l += delta; if (l < 0) l = 0; if (l > nws - 1) l = nws - 1; source_index.data["l"][0] = l; source_index.change.emit(); spec_vline.location = wavs[l]; // Update the map var local_moll = new Array(npix_m).fill(0).map(() => new Array(npix_m).fill(0)); for (var k = 0; k < nc; k++) { var weight = spec[k][l]; for (var i = 0; i < npix_m; i++) { for (var j = 0; j < npix_m; j++) { local_moll[j][i] += weight * moll[k][j][i]; } } } source_moll.data["moll"][0] = local_moll; source_moll.change.emit(); """, ) plot_moll.js_on_event(MouseWheel, mouse_wheel_callback) mouse_enter_callback = CustomJS(code=""" DISABLE_WHEEL= true; """) plot_moll.js_on_event(MouseEnter, mouse_enter_callback) mouse_leave_callback = CustomJS(code=""" DISABLE_WHEEL = false; """) plot_moll.js_on_event(MouseLeave, mouse_leave_callback) return plot_moll
def tool_handler_2d(self, doc): from bokeh import events from bokeh.layouts import row, column, widgetbox, Spacer from bokeh.models import ColumnDataSource, widgets from bokeh.models.mappers import LinearColorMapper from bokeh.models.widgets.markups import Div from bokeh.plotting import figure arr = self.arr # Set up the data x_coords, y_coords = arr.coords[arr.dims[0]], arr.coords[arr.dims[1]] # Styling default_palette = self.default_palette if arr.S.is_subtracted: default_palette = cc.coolwarm error_alpha = 0.3 error_fill = '#3288bd' # Application Organization self.app_context.update({ 'data': arr, 'data_range': { 'x': (np.min(x_coords.values), np.max(x_coords.values)), 'y': (np.min(y_coords.values), np.max(y_coords.values)), }, 'show_stat_variation': False, 'color_mode': 'linear', }) def stats_patch_from_data(data, subsampling_rate=None): if subsampling_rate is None: subsampling_rate = int(min(data.values.shape[0] / 50, 5)) if subsampling_rate == 0: subsampling_rate = 1 x_values = data.coords[data.dims[0]].values[::subsampling_rate] values = data.values[::subsampling_rate] sq = np.sqrt(values) lower, upper = values - sq, values + sq return { 'x': np.append(x_values, x_values[::-1]), 'y': np.append(lower, upper[::-1]), } def update_stat_variation(plot_name, data): patch_data = stats_patch_from_data(data) if plot_name != 'right': # the right plot is on transposed axes plots[plot_name + '_marginal_err'].data_source.data = patch_data else: plots[plot_name + '_marginal_err'].data_source.data = { 'x': patch_data['y'], 'y': patch_data['x'], } figures, plots, app_widgets = self.app_context[ 'figures'], self.app_context['plots'], self.app_context['widgets'] if self.cursor_default is not None and len(self.cursor_default) == 2: self.cursor = self.cursor_default else: self.cursor = [ np.mean(self.app_context['data_range']['x']), np.mean(self.app_context['data_range']['y']) ] # try a sensible default # create the main inset plot main_image = arr prepped_main_image = self.prep_image(main_image) self.app_context['color_maps']['main'] = LinearColorMapper( default_palette, low=np.min(prepped_main_image), high=np.max(prepped_main_image), nan_color='black') main_tools = ["wheel_zoom", "tap", "reset", "save"] main_title = 'Bokeh Tool: WARNING Unidentified' try: main_title = "Bokeh Tool: %s" % arr.S.label[:60] except: pass figures['main'] = figure(tools=main_tools, plot_width=self.app_main_size, plot_height=self.app_main_size, min_border=10, min_border_left=50, toolbar_location='left', x_axis_location='below', y_axis_location='right', title=main_title, x_range=self.app_context['data_range']['x'], y_range=self.app_context['data_range']['y']) figures['main'].xaxis.axis_label = arr.dims[0] figures['main'].yaxis.axis_label = arr.dims[1] figures['main'].toolbar.logo = None figures['main'].background_fill_color = "#fafafa" plots['main'] = figures['main'].image( [prepped_main_image.T], x=self.app_context['data_range']['x'][0], y=self.app_context['data_range']['y'][0], dw=self.app_context['data_range']['x'][1] - self.app_context['data_range']['x'][0], dh=self.app_context['data_range']['y'][1] - self.app_context['data_range']['y'][0], color_mapper=self.app_context['color_maps']['main']) app_widgets['info_div'] = Div(text='', width=self.app_marginal_size, height=100) # Create the bottom marginal plot bottom_marginal = arr.sel(**dict([[arr.dims[1], self.cursor[1]]]), method='nearest') figures['bottom_marginal'] = figure( plot_width=self.app_main_size, plot_height=200, title=None, x_range=figures['main'].x_range, y_range=(np.min(bottom_marginal.values), np.max(bottom_marginal.values)), x_axis_location='above', toolbar_location=None, tools=[]) plots['bottom_marginal'] = figures['bottom_marginal'].line( x=bottom_marginal.coords[arr.dims[0]].values, y=bottom_marginal.values) plots['bottom_marginal_err'] = figures['bottom_marginal'].patch( x=[], y=[], color=error_fill, fill_alpha=error_alpha, line_color=None) # Create the right marginal plot right_marginal = arr.sel(**dict([[arr.dims[0], self.cursor[0]]]), method='nearest') figures['right_marginal'] = figure( plot_width=200, plot_height=self.app_main_size, title=None, y_range=figures['main'].y_range, x_range=(np.min(right_marginal.values), np.max(right_marginal.values)), y_axis_location='left', toolbar_location=None, tools=[]) plots['right_marginal'] = figures['right_marginal'].line( y=right_marginal.coords[arr.dims[1]].values, x=right_marginal.values) plots['right_marginal_err'] = figures['right_marginal'].patch( x=[], y=[], color=error_fill, fill_alpha=error_alpha, line_color=None) cursor_lines = self.add_cursor_lines(figures['main']) # Attach tools and callbacks toggle = widgets.Toggle(label="Show Stat. Variation", button_type="success", active=False) def set_show_stat_variation(should_show): self.app_context['show_stat_variation'] = should_show if should_show: main_image_data = arr update_stat_variation( 'bottom', main_image_data.sel(**dict([[arr.dims[1], self.cursor[1]]]), method='nearest')) update_stat_variation( 'right', main_image_data.sel(**dict([[arr.dims[0], self.cursor[0]]]), method='nearest')) plots['bottom_marginal_err'].visible = True plots['right_marginal_err'].visible = True else: plots['bottom_marginal_err'].visible = False plots['right_marginal_err'].visible = False toggle.on_click(set_show_stat_variation) scan_keys = [ 'x', 'y', 'z', 'pass_energy', 'hv', 'location', 'id', 'probe_pol', 'pump_pol' ] scan_info_source = ColumnDataSource({ 'keys': [k for k in scan_keys if k in arr.attrs], 'values': [ str(v) if isinstance(v, float) and np.isnan(v) else v for v in [arr.attrs[k] for k in scan_keys if k in arr.attrs] ], }) scan_info_columns = [ widgets.TableColumn(field='keys', title='Attr.'), widgets.TableColumn(field='values', title='Value'), ] POINTER_MODES = [ ( 'Cursor', 'cursor', ), ( 'Path', 'path', ), ] COLOR_MODES = [ ( 'Adaptive Hist. Eq. (Slow)', 'adaptive_equalization', ), # ('Histogram Eq.', 'equalization',), # not implemented ( 'Linear', 'linear', ), # ('Log', 'log',), # not implemented ] def on_change_color_mode(attr, old, new_color_mode): self.app_context['color_mode'] = new_color_mode if old is None or old != new_color_mode: right_image_data = arr.sel(**dict( [[arr.dims[0], self.cursor[0]]]), method='nearest') bottom_image_data = arr.sel(**dict( [[arr.dims[1], self.cursor[1]]]), method='nearest') main_image_data = arr prepped_right_image = self.prep_image(right_image_data) prepped_bottom_image = self.prep_image(bottom_image_data) prepped_main_image = self.prep_image(main_image_data) plots['right'].data_source.data = { 'image': [prepped_right_image] } plots['bottom'].data_source.data = { 'image': [prepped_bottom_image.T] } plots['main'].data_source.data = { 'image': [prepped_main_image.T] } update_main_colormap(None, None, main_color_range_slider.value) color_mode_dropdown = widgets.Dropdown(label='Color Mode', button_type='primary', menu=COLOR_MODES) color_mode_dropdown.on_change('value', on_change_color_mode) symmetry_point_name_input = widgets.TextInput( title='Symmetry Point Name', value="G") snap_checkbox = widgets.CheckboxButtonGroup(labels=['Snap Axes'], active=[]) place_symmetry_point_at_cursor_button = widgets.Button( label="Place Point", button_type="primary") def update_symmetry_points_for_display(): pass def place_symmetry_point(): cursor_dict = dict(zip(arr.dims, self.cursor)) skip_dimensions = {'eV', 'delay', 'cycle'} if 'symmetry_points' not in arr.attrs: arr.attrs['symmetry_points'] = {} snap_distance = { 'phi': 2, 'beta': 2, 'kx': 0.01, 'ky': 0.01, 'kz': 0.01, 'kp': 0.01, 'hv': 4, } cursor_dict = { k: v for k, v in cursor_dict.items() if k not in skip_dimensions } snapped = copy.copy(cursor_dict) if 'Snap Axes' in [ snap_checkbox.labels[i] for i in snap_checkbox.active ]: for axis, value in cursor_dict.items(): options = [ point[axis] for point in arr.attrs['symmetry_points'].values() if axis in point ] options = sorted(options, key=lambda x: np.abs(x - value)) if options and np.abs(options[0] - value) < snap_distance[axis]: snapped[axis] = options[0] arr.attrs['symmetry_points'][ symmetry_point_name_input.value] = snapped place_symmetry_point_at_cursor_button.on_click(place_symmetry_point) main_color_range_slider = widgets.RangeSlider( start=0, end=100, value=( 0, 100, ), title='Color Range (Main)') layout = row( column(figures['main'], figures['bottom_marginal']), column(figures['right_marginal'], Spacer(width=200, height=200)), column( widgetbox( widgets.Dropdown(label='Pointer Mode', button_type='primary', menu=POINTER_MODES)), widgets.Tabs(tabs=[ widgets.Panel(child=widgetbox( Div(text='<h2>Colorscale:</h2>'), color_mode_dropdown, main_color_range_slider, Div(text= '<h2 style="padding-top: 30px;">General Settings:</h2>' ), toggle, self._cursor_info, sizing_mode='scale_width'), title='Settings'), widgets.Panel(child=widgetbox( app_widgets['info_div'], Div(text= '<h2 style="padding-top: 30px; padding-bottom: 10px;">Scan Info</h2>' ), widgets.DataTable(source=scan_info_source, columns=scan_info_columns, width=400, height=400), sizing_mode='scale_width', width=400), title='Info'), widgets.Panel(child=widgetbox( Div(text='<h2>Preparation</h2>'), symmetry_point_name_input, snap_checkbox, place_symmetry_point_at_cursor_button, sizing_mode='scale_width'), title='Preparation'), ], width=400))) update_main_colormap = self.update_colormap_for('main') def on_click_save(event): save_dataset(arr) print(event) def click_main_image(event): self.cursor = [event.x, event.y] right_marginal_data = arr.sel(**dict( [[arr.dims[0], self.cursor[0]]]), method='nearest') bottom_marginal_data = arr.sel(**dict( [[arr.dims[1], self.cursor[1]]]), method='nearest') plots['bottom_marginal'].data_source.data = { 'x': bottom_marginal_data.coords[arr.dims[0]].values, 'y': bottom_marginal_data.values, } plots['right_marginal'].data_source.data = { 'y': right_marginal_data.coords[arr.dims[1]].values, 'x': right_marginal_data.values, } if self.app_context['show_stat_variation']: update_stat_variation('right', right_marginal_data) update_stat_variation('bottom', bottom_marginal_data) figures['bottom_marginal'].y_range.start = np.min( bottom_marginal_data.values) figures['bottom_marginal'].y_range.end = np.max( bottom_marginal_data.values) figures['right_marginal'].x_range.start = np.min( right_marginal_data.values) figures['right_marginal'].x_range.end = np.max( right_marginal_data.values) self.save_app() figures['main'].on_event(events.Tap, click_main_image) main_color_range_slider.on_change('value', update_main_colormap) doc.add_root(layout) doc.title = "Bokeh Tool" self.load_app() self.save_app()
def pci(doc): # Use device-0 to get "upper bound" pci_gen = pynvml.nvmlDeviceGetMaxPcieLinkGeneration(gpu_handles[0]) pci_width = pynvml.nvmlDeviceGetMaxPcieLinkWidth(gpu_handles[0]) pci_bw = { # Keys = PCIe-Generation, Values = Max PCIe Lane BW (per direction) # [Note: Using specs at https://en.wikipedia.org/wiki/PCI_Express] 1: (250.0 / 1024.0), 2: (500.0 / 1024.0), 3: (985.0 / 1024.0), 4: (1969.0 / 1024.0), 5: (3938.0 / 1024.0), 6: (7877.0 / 1024.0), } # Max PCIe Throughput = (BW-per-lane / Width) max_rxtx_tp = pci_width * pci_bw[pci_gen] pci_tx = [ pynvml.nvmlDeviceGetPcieThroughput(gpu_handles[i], pynvml.NVML_PCIE_UTIL_TX_BYTES) / (1024.0 * 1024.0) # Convert KB/s -> GB/s for i in range(ngpus) ] pci_rx = [ pynvml.nvmlDeviceGetPcieThroughput(gpu_handles[i], pynvml.NVML_PCIE_UTIL_RX_BYTES) / (1024.0 * 1024.0) # Convert KB/s -> GB/s for i in range(ngpus) ] left = list(range(ngpus)) right = [l + 0.8 for l in left] source = ColumnDataSource({ "left": left, "right": right, "pci-tx": pci_tx, "pci-rx": pci_rx }) mapper = LinearColorMapper(palette=all_palettes["RdYlBu"][4], low=0, high=max_rxtx_tp) tx_fig = figure(title="TX Bytes [GB/s]", sizing_mode="stretch_both", y_range=[0, max_rxtx_tp]) tx_fig.quad( source=source, left="left", right="right", bottom=0, top="pci-tx", color={ "field": "pci-tx", "transform": mapper }, ) tx_fig.toolbar_location = None rx_fig = figure(title="RX Bytes [GB/s]", sizing_mode="stretch_both", y_range=[0, max_rxtx_tp]) rx_fig.quad( source=source, left="left", right="right", bottom=0, top="pci-rx", color={ "field": "pci-rx", "transform": mapper }, ) rx_fig.toolbar_location = None doc.title = "PCI Throughput" doc.add_root(column(tx_fig, rx_fig, sizing_mode="stretch_both")) def cb(): src_dict = {} src_dict["pci-tx"] = [ pynvml.nvmlDeviceGetPcieThroughput( gpu_handles[i], pynvml.NVML_PCIE_UTIL_TX_BYTES) / (1024.0 * 1024.0) # Convert KB/s -> GB/s for i in range(ngpus) ] src_dict["pci-rx"] = [ pynvml.nvmlDeviceGetPcieThroughput( gpu_handles[i], pynvml.NVML_PCIE_UTIL_RX_BYTES) / (1024.0 * 1024.0) # Convert KB/s -> GB/s for i in range(ngpus) ] source.data.update(src_dict) doc.add_periodic_callback(cb, 200)
def main(): import argparse from load_config import load_config parser = argparse.ArgumentParser() parser.add_argument('pickle_dump') parser.add_argument('output_file') parser.add_argument('-c', '--config_filename', dest='config_filename', help="Config file with private info", default=None) parser.add_argument( '--max_happy_commute', default=45, type=float, help= "For plot that overlays all the commutes, what's the longest not colored red?" ) parser.add_argument('--center_lat', default=34.053695, help="latitude to center on") parser.add_argument('--center_lng', default=-118.430208, help="longitutde to center on") parser.add_argument( '--zoom', default=11, type=int, help="initial zoom of maps. goes 1 (least zoomed) to 20 (most zoomed)" ) parser.add_argument( '--map_type', default='roadmap', help="initial zoom of maps. goes 1 (least zoomed) to 20 (most zoomed)" ) parser.add_argument( '--palette', default='Viridis', help="Palette to use. Must be in bokeh.palettes.all_palettes") parser.add_argument( '--ncolors', type=int, default=256, help= "Number of colors to use. Must be able to access bokeh.palettes.all_palettes[<palette>][<ncolors>]" ) parser.add_argument('--cbar_min', default=15, type=float) parser.add_argument('--cbar_max', default=75, type=float) args = parser.parse_args() config, timezome = load_config(args.config_filename) api_key = config['api_key'] with open(args.pickle_dump, 'rb') as f: print(f"Loading from {args.pickle_dump}") data = pickle.load(f) center_lats = np.array(data.pop('lat')) center_longs = np.array(data.pop('long')) names = set(k.split('_')[0] for k in data) dx = np.max(center_longs[1:] - center_longs[:-1]) / 2 dy = np.max(center_lats[1:] - center_lats[:-1]) / 2 xcoords = [[xc - dx, xc - dx, xc + dx, xc + dx] for xc in center_longs] ycoords = [[yc - dy, yc + dy, yc + dy, yc - dy] for yc in center_lats] print(f"Plotting {len(xcoords)} squares") try: args.center_lat = float(args.center_lat) except ValueError: args.center_lat = (min([yc[0] for yc in ycoords]) + max([yc[1] for yc in ycoords])) / 2 try: args.center_lng = float(args.center_lng) except ValueError: args.center_lng = (min([xc[0] for xc in ycoords]) + max([xc[2] for xc in ycoords])) / 2 plots = [] bk.output_file(args.output_file, title="Commute times"), #mode="inlne") moptions = GMapOptions(lat=args.center_lat, lng=args.center_lng, zoom=args.zoom, map_type=args.map_type) allkeys = [ f'{name}_{destkey}' for name, destkey in product(names, ['towork', 'tohome']) ] dsets = {restructure_key(key): data[key] for key in allkeys} color_mapper = LinearColorMapper( palette=all_palettes[args.palette][args.ncolors], low=args.cbar_min, high=args.cbar_max) nhappy = np.zeros(len(xcoords), dtype=int) for name in names: for destkey in ['towork', 'tohome']: key = f'{name}_{destkey}' colors = data[key] plots.append( plot_patches_on_gmap(xcoords, ycoords, api_key, values=colors, map_options=moptions, title=restructure_key(key), color_mapper=color_mapper)) msk = np.array(colors) <= args.max_happy_commute nhappy[msk] += 1 ## now overlap all the commutes: title = f'Areas where all commutes are < {args.max_happy_commute} minutes' plot = plot_patches_on_gmap( list(np.array(xcoords)[nhappy < len(allkeys) - 1]), list(np.array(ycoords)[nhappy < len(allkeys) - 1]), api_key, map_options=moptions, title=title, solid_fill='red') data = dict(xs=list(np.array(xcoords)[nhappy == len(allkeys) - 1]), ys=list(np.array(ycoords)[nhappy == len(allkeys) - 1])) source_patches = bk.ColumnDataSource(data=data) patches_glyph = plot.patches('xs', 'ys', fill_alpha=0.25, fill_color='orange', source=source_patches, line_width=0) plots.append(plot) ## now show grid = gridplot(plots, ncols=2) bk.show(grid)