def water_hover_gen(self, x_range, FFT_Size='256', Percent_Overlap='50', Num_Avgs='1', Window='blackmanharris'): width = 128 height = 128 fft_size = int(FFT_Size) if fft_size < 128: width = fft_size height = fft_size if self.hover_count > self.update_count: self.gen_spec_points(x_range, FFT_Size, Percent_Overlap, Num_Avgs, Window) self.hover_count += 1 opts_hover = dict(tools=['hover'], alpha=0, hover_alpha=0.2, fill_alpha=0) agg = aggregate(self.points, width=width, height=height, dynamic=False, aggregator=ds.max('PowerdB'), x_sampling=self.step_size, y_sampling=1) return hv.QuadMesh(agg).options(**opts_hover)
def to_geoimage(data, dynamic=True, style_opts={'cmap': 'RdBu_r'}, plot_opts={ 'width': 600, 'toolbar': 'above', 'colorbar': True }, hover=False): gvd = gv.Dataset(data) if len(gvd.dimensions()) < 4: gvimg = gvd.to(gv.Image, kdims=['lon', 'lat'])(plot=plot_opts)(style=style_opts) else: gvimg = gvd.to(gv.Image, kdims=['lon', 'lat'], dynamic=dynamic)(plot=plot_opts)(style=style_opts) if hover: projected = gv.operation.project_image(gvimg) gvimg = hv.QuadMesh( projected, kdims=gvimg.kdims, vdims=gvimg.vdims)(plot=plot_opts)(style=style_opts) gvimg = gvimg(plot={'tools': ['hover']}) #gvimg*=gvd.to(gv.Points,kdims=['lon','lat'])(style={'alpha':0,'marker':'square','size':6})(plot={'tools':['hover']}) return gvimg #*gv.feature.coastline
def plot_probability_grid(model, X): probabilities, xs, ys = predict_grid(model, X) data = pd.DataFrame() qmesh = hv.QuadMesh((xs, ys, probabilities)).opts(colorbar=True, width=500, height=400, cmap="YlGnBu") return qmesh
def plot_landscape(data): x, y, xx, yy, z, xlim, ylim = data # xx, yy, memory_grid, _, xlim, ylim, x, y = data try: memory_vals = z.reshape(xx.shape) except ValueError: memory_vals = numpy.ones_like(xx) mesh = holoviews.QuadMesh((xx, yy, memory_vals)) plot = (mesh * holoviews.Scatter((x, y))).opts(xlim=xlim, ylim=ylim) return plot
def plot_landscape(data): x, y, xx, yy, z, xlim, ylim = data zz = griddata((x, y), z, (xx, yy), method="linear") mesh = holoviews.QuadMesh((xx, yy, zz)).redim( x=holoviews.Dimension("x", range=xlim), y=holoviews.Dimension("y", range=ylim), ) contour = holoviews.operation.contours(mesh, levels=8) scatter = holoviews.Scatter((x, y)) contour_mesh = mesh * contour * scatter return contour_mesh
def plot_landscape(data): """Overlay the walkers on top of the bins defined by the memory grid.""" x, y, xx, yy, z, xlim, ylim = data try: memory_vals = z.reshape(xx.shape) except ValueError: # Avoid errors when initializing the plot. memory_vals = numpy.ones_like(xx) mesh = holoviews.QuadMesh((xx, yy, memory_vals)) plot = (mesh * holoviews.Scatter((x, y))).opts(xlim=xlim, ylim=ylim) return plot
def plot_landscape(data): """Display the walkers overlied on a mesh representing the probability \ assigned by the :class:`Critic`.""" x, y, xx, yy, z, xlim, ylim = data try: memory_vals = z.reshape(xx.shape) except ValueError: # Avoid errors when initializing the plot. memory_vals = numpy.ones_like(xx) mesh = holoviews.QuadMesh((xx, yy, memory_vals)) plot = (mesh * holoviews.Scatter((x, y))).opts(xlim=xlim, ylim=ylim) return plot
def __plot_decision_boundaries(X, y, y_pred, resolution: int = 100, embedding=None): if embedding is None: embedding = TSNE(n_components=2, random_state=160290).fit_transform(X) x_min, x_max = safe_bounds(embedding[:, 0]) y_min, y_max = safe_bounds(embedding[:, 1]) xx, yy = np.meshgrid(np.linspace(x_min, x_max, resolution), np.linspace(y_min, y_max, resolution)) # approximate Voronoi tesselation on resolution x resolution grid using 1-NN background_model = KNeighborsClassifier(n_neighbors=1).fit( embedding, y_pred) voronoi_bg = background_model.predict(np.c_[xx.ravel(), yy.ravel()]) voronoi_bg = voronoi_bg.reshape((resolution, resolution)) mesh = hv.QuadMesh((xx, yy, voronoi_bg)).opts(cmap="viridis") points = hv.Scatter( { "x": embedding[:, 0], "y": embedding[:, 1], "pred": y_pred, "class": y }, kdims=["x", "y"], vdims=["pred", "class"], ) errors = y_pred != y failed_points = hv.Scatter( { "x": embedding[errors, 0], "y": embedding[errors, 1] }, kdims=["x", "y"]).opts(color="red", size=5, alpha=0.9) points = points.opts(color="pred", cmap="viridis", line_color="grey", size=10, alpha=0.8, tools=["hover"]) plot = mesh * points * failed_points plot = plot.opts(xaxis=None, yaxis=None, width=500, height=450, title="Decision boundaries on TSNE") return plot
def _generate_mesh(self, generate_mesh=False, hide_mesh=False): "Callback returning generated QuadMesh element" if not self.ready: return self.qmesh.opts(fill_alpha=0, line_alpha=0) elif hide_mesh or (not generate_mesh): return self.qmesh.opts(fill_alpha=0, line_alpha=0) if self.ready: gdf = self.boundary kwargs = dict(shape=(self.xres, self.yres), ul_idx=self.ul_idx) if self.focus is not None: kwargs['focus'] = self.focus self.grid = pgg.Gridgen(gdf.geometry.x, gdf.geometry.y, gdf.polarity, **kwargs) xdim, ydim = self.grid.x.shape zs = np.ones((xdim-1, ydim-1)) self.qmesh = hv.QuadMesh((np.array(self.grid.x), np.array(self.grid.y), zs)) return self.qmesh.opts(**self.mesh_style, fill_alpha=0)
def __init__(self, data=None, **params): data_params = {} if data is None else {k:v for k,v in data.items() if k not in self._columns} params = dict(data_params, **params) data = {k:[] for k in self._columns} if (data is None) else data super().__init__(**params) def install_handle(plot, element): "Handle needed to make the draw_tool available in the JS callback" plot.handles['draw_tool'] = plot.state.tools[-1] node_style = dict(self.node_style, tools=['tap'], color=hv.dim('polarity'), fill_alpha=hv.dim('polarity').categorize({'0':0, '+':1, '-':1 }), show_legend=False, hooks=[install_handle]) # PointDraw Stream that enables the PointDraw Bokeh tool self._node_stream = hv.streams.PointDraw(data=data, num_objects=self.max_nodes, empty_value = '+') # Nodes is a DynamicMap returning hv.Points along the boundary self.nodes = hv.DynamicMap(self.points, streams=[self._node_stream, self.param.insert_points, self.param.node_size]).opts(**node_style) # DynamicMap drawing the boundary as a hv.Path element self.boundary_dmap = hv.DynamicMap(self._boundary, streams=[self._node_stream, hv.streams.Selection1D()]) # DynamicMap placing the start indicator self.start_marker = hv.DynamicMap(self._start_marker, streams=[self._node_stream] ).opts(**self.start_indicator_style) # Initial, empty mesh self.qmesh = hv.QuadMesh((np.zeros((2,2)), np.zeros((2,2)), np.zeros((2,2)))) self._selected_edge_index = None
def make_sky(self, object_type, ra_range=None, dec_range=None, x_range=None, y_range=None, **kwargs): if object_type == 'all': dset = self.ds else: dset = self.ds.select(label=object_type) if x_range is not None and y_range is not None: dset = dset.select(x=x_range, y=y_range) self._selected = dset.data.id pts = dset.to(hv.Points, kdims=['ra', 'dec'], vdims=['y'], groupby=[]) agg = aggregate(pts, width=100, height=100, x_range=ra_range, y_range=dec_range, aggregator=ds.mean('y'), dynamic=False) hover = hv.QuadMesh(agg).opts( '[tools=["hover"]] (alpha=0 hover_alpha=0.2)') shaded = dynspread( datashade(pts, x_range=ra_range, y_range=dec_range, dynamic=False, cmap=cc.palette['coolwarm'], aggregator=ds.mean('y'))) shaded = shaded.opts('RGB [width=400, height=400]') return (shaded * hover).relabel('{} ({})'.format( object_type, len(dset)))
def figure(self): v1, v2 = self.v(1), self.v(2) meshes = [] curve = (hv.Curve( (self.ds.binsXC, self.ds.median1), 'binsX', 'binsY').opts(opts.Curve(color='k', line_width=4, tools=['hover']))) for i in range(self.ds.xc.size): x = self.ds.binsX.isel(x=[i, i + 1]).values y = self.ds.binsY.isel(xc=[i, i]).values z = self.ds.pdf.isel(xc=i).values.reshape(-1, 1) submesh = hv.QuadMesh((x, y, z), vdims=['pdf']) meshes.append(submesh) mesh = hv.Overlay(meshes) * curve return mesh.opts( opts.QuadMesh(colorbar=True, width=800, height=400, xlabel=v1, ylabel=v2, tools=['hover'], cmap=self.cmap))
def plot_landscape(data): """ Plot the data as an energy landscape. Args: data: (x, y, xx, yy, z, xlim, ylim). x, y, z represent the \ coordinates of the points that will be interpolated. xx, yy \ represent the meshgrid used to interpolate the points. xlim, \ ylim are tuples containing the limits of the x and y axes. Returns: Plot representing the interpolated energy landscape of the target points. """ x, y, xx, yy, z, xlim, ylim = data zz = griddata((x, y), z, (xx, yy), method="linear") mesh = holoviews.QuadMesh((xx, yy, zz)) contour = holoviews.operation.contours(mesh, levels=8) scatter = holoviews.Scatter((x, y)) contour_mesh = mesh * contour * scatter return contour_mesh.redim( x=holoviews.Dimension("x", range=xlim), y=holoviews.Dimension("y", range=ylim), )
def heatmap_n_req(n_req, clabel='Required sample size'): """Plots a heatmap of required number of samples as a function of number of features and true correlation. Parameters ---------- n_req : xr.DataArray elements represent number of samples. Must have dimensions ``px`` and ``r``. All other dimensions will be averaged over. clabel : str label for colorbar Returns ------- fig : hv.QuadMesh """ if not (('ptot' in n_req.dims) and ('r' in n_req.dims)): raise ValueError('DataArray `n` must have dimensions `ptot` and `r`.') n_req = n_req.rename('y') # so that "redim" below works other_dims = [d for d in n_req.dims if not d in ['ptot', 'r']] if len(other_dims) > 0: n_req = n_req.stack(DUMMYDIMENSION=other_dims).mean('DUMMYDIMENSION') return ( hv.QuadMesh( n_req, kdims=['ptot', 'r'], ) ).redim( r=r'$r_\mathrm{true}$', ptot='Number of features', y=clabel )
def plot_regions(cls, model, X): regions, xs, ys = cls.predict_classes(model, X) qmesh = hv.QuadMesh((xs, ys, regions)).opts(tools=["hover"]) return qmesh
def plot_probability_grid(cls, model, X): probabilities, xs, ys = cls.predict_grid(model, X) qmesh = hv.QuadMesh((xs, ys, probabilities)).opts(tools=["hover"]) return qmesh
def plotter_2D_test_histogram(name): # IMPORTING AND FOMRATTING DATA # ===| open root demo file with pedestal and noise values |=== t = uproot.open("Data/CalibTree.root")["calibTree"] #padData = t.pandas.df("Pedestals", flatten = False) padData = t.array(name) # ===| pad plane plane meta data |=== d = pd.read_csv("Data/pad_plane_data.txt", sep='\t') # ===| fuction to prepare input root demo file for plotting |=== [vcor, vtri] = configureData(padData, d) # PLOTTING hd.shade.cmap = [ '#FBFCBF', '#FD9F6C', '#DD4968', '#8C2980', '#3B0F6F', '#000003' ] cvs = ds.Canvas(plot_height=400, plot_width=400) trim = hv.TriMesh((vtri, hv.Points(vcor, vdims='za'))).opts(show_grid=True) trim2 = hv.TriMesh((vtri, hv.Points(vcor, vdims='zc'))).opts(show_grid=True) trim.opts(colorbar=True) trim.opts(cmap='Blues') trimesh = hd.datashade(trim, aggregator=ds.mean('za'), precompute=True, link_inputs=False) trimesh2 = hd.datashade(trim2, aggregator=ds.mean('zc'), precompute=True, link_inputs=False) trimesh.opts(height=450, width=450, show_grid=False, xaxis=None, yaxis=None) trimesh2.opts(height=450, width=450, show_grid=False, xaxis=None, yaxis=None) # ADDING INTERACTIVITY # Small hover tool tooltips_small = [("X:", "$x"), ("Y:", "$y"), ("Value:", "NaN")] hover_small = HoverTool(tooltips=tooltips_small) dynamic = hv.util.Dynamic(hd.aggregate(trim, width=30, height=30, streams=[RangeXY]), operation=hv.QuadMesh) \ .opts(tools=[hover_small], alpha=0, hover_alpha=0, hover_line_color='black',hover_line_alpha=0) # Sector select hover tool sector_edge_phi = np.linspace(0, np.pi * 2, 19) sector_edge_r = np.array([850, 2530]) Phi, R = np.meshgrid(sector_edge_phi, sector_edge_r) Qx = np.cos(Phi) * np.abs(R) Qy = np.sin(Phi) * np.abs(R) Z = np.linspace(0, 17, 18).reshape(1, 18) #Z = Z*0 hover_data = dict(x=Qx, y=Qy, z=Z) tooltips_a = [("Side", "A"), ("Sector", "@z")] tooltips_c = [("Side", "C"), ("Sector", "@z")] hover_a = HoverTool(tooltips=tooltips_a) hover_c = HoverTool(tooltips=tooltips_c) qmesh_a = hv.QuadMesh(hover_data)\ .opts(tools=[hover_a,'tap'], alpha=0, hover_fill_alpha=0.1, hover_color='white', hover_line_color='black',hover_line_alpha=1) qmesh_c = hv.QuadMesh(hover_data)\ .opts(tools=[hover_c], alpha=0, hover_fill_alpha=0.1, hover_color='white', hover_line_color='black',hover_line_alpha=1) # CREATING OUTPUT tpc_plot_a = trimesh * qmesh_a * hv.Text(0, 0, 'A', fontsize=40) tpc_plot_c = trimesh2 * qmesh_c * hv.Text(0, 0, 'C', fontsize=40) final_layout = (tpc_plot_a + tpc_plot_c).opts(merge_tools=False) return final_layout
def plot_decision_boundaries( X_train, y_train, y_pred_train, X_test, y_test, y_pred_test, resolution: int = 100, embedding=None, ): X = np.concatenate([X_train, X_test]) y = np.concatenate([y_train, y_test]) y_pred = np.concatenate([y_pred_train, y_pred_test]) if embedding is None: try: embedding = umap.UMAP(n_components=2, random_state=160290).fit_transform(X) except: from sklearn.manifold import TSNE embedding = TSNE(n_components=2, random_state=160290).fit_transform(X) x_min, x_max = safe_bounds(embedding[:, 0]) y_min, y_max = safe_bounds(embedding[:, 1]) xx, yy = np.meshgrid(np.linspace(x_min, x_max, resolution), np.linspace(y_min, y_max, resolution)) # approximate Voronoi tesselation on resolution x resolution grid using 1-NN background_model = KNeighborsClassifier(n_neighbors=1).fit( embedding, y_pred) voronoi_bg = background_model.predict(np.c_[xx.ravel(), yy.ravel()]) voronoi_bg = voronoi_bg.reshape((resolution, resolution)) mesh = hv.QuadMesh((xx, yy, voronoi_bg)).opts(cmap="viridis", alpha=0.6) points_train = hv.Scatter( { "x": embedding[:len(y_train), 0], "y": embedding[:len(y_train), 1], "pred": y_pred_train, "class": y_train, }, kdims=["x", "y"], vdims=["pred", "class"], ) points_test = hv.Scatter( { "x": embedding[len(y_train):, 0], "y": embedding[len(y_train):, 1], "pred": y_pred_test, "class": y_test, }, kdims=["x", "y"], vdims=["pred", "class"], ) errors = y_pred != y failed_points = hv.Scatter( { "x": embedding[errors, 0], "y": embedding[errors, 1] }, kdims=["x", "y"]).opts(color="red", size=2, alpha=0.9) points_train = points_train.opts(color="class", cmap="viridis", line_color="grey", size=10, alpha=0.8, tools=["hover"]) points_test = points_test.opts( color="class", cmap="viridis", line_color="grey", size=10, alpha=0.8, tools=["hover"], marker="square", ) plot = mesh * points_train * points_test * failed_points plot = plot.opts(xaxis=None, yaxis=None, width=500, height=450, title="Fronteras de decisiĆ³n") return plot
def make_quadmesh( db: history.DB, contour_view: ContourView, ) -> holoviews.QuadMesh: """Make a single Quadmesh representation""" struct_data = get_regular_mesh_data(db) node_skeleton = history.NodePosition._all_nones()._replace( result_case_num=contour_view.db_case_num) node_coords = { node_pos.node_num: node_pos for node_pos in db.get_all_matching(node_skeleton) } # Node positions X = numpy.zeros(shape=struct_data.node_indices.shape) Y = numpy.zeros(shape=struct_data.node_indices.shape) for idx_x, along_y in enumerate(struct_data.node_indices): for idx_y, node_num in enumerate(along_y): X[idx_x, idx_y] = node_coords[node_num].x Y[idx_x, idx_y] = node_coords[node_num].y # The values are element centred. Z = numpy.zeros(shape=struct_data.elem_indices.shape) contour_val_skeleton = history.ContourValue._all_nones()._replace( result_case_num=contour_view.db_case_num, contour_key_num=contour_view.contour_key.value) contour_vals = { contour_val.elem_num: contour_val.value for contour_val in db.get_all_matching(contour_val_skeleton) } # TEMP - to get rasterize to work, need to make this node-centred. Z_nodal_shape = (4, ) + struct_data.node_indices.shape Z_nodal = numpy.empty(shape=Z_nodal_shape) Z_nodal[:] = numpy.nan for idx_x, along_y in enumerate(struct_data.elem_indices): for idx_y, elem_num in enumerate(along_y): c_val = contour_vals.get(elem_num, 0.0) Z[idx_x, idx_y] = c_val for layer, (z_nodal_idx_x, z_nodal_idx_y) in enumerate( itertools.product((idx_x, idx_x + 1), (idx_y, idx_y + 1))): Z_nodal[layer, z_nodal_idx_x, z_nodal_idx_y] = c_val Z_nodal_flat = numpy.nanmean(Z_nodal, axis=0) qmesh = holoviews.QuadMesh((X, Y, Z_nodal_flat), vdims='level', group=contour_view.title) qmesh.options(cmap='viridis') qmesh.opts(aspect='equal', line_width=0.1, padding=0.1, colorbar=True, width=FIG_SIZE[0], height=FIG_SIZE[1]) qmesh.data.hvplot.image('x', 'y', label=contour_view.title).options( width=FIG_SIZE[0], height=FIG_SIZE[1]) return qmesh