def plot_overview(): produccion = serie_campo.groupby(by='fecha').mean() estado_mecanico = serie_status elementos_status = dict(status=pd.unique(estado_mecanico.estado_actual),numero=estado_mecanico.estado_actual.value_counts()) elementos_trayectoria = dict(trayectoria=pd.unique(estado_mecanico.trayectoria),numero=estado_mecanico.trayectoria.value_counts()) elementos_pozos = dict(indice=resumen.index[0:6], valores=resumen[0:6]) elementos_volumen = dict(indice=resumen.index[13:], valores=resumen[13:]) tabla_pozos = hv.Table(elementos_pozos,'indice','valores') tabla_pozos.opts(height=500,fontscale=20) tabla_volumen = hv.Table(elementos_volumen,'indice','valores') tabla_volumen.opts(height=500,fontscale=20) plot_prod_aceite = hv.Curve(produccion, 'fecha', 'aceite_Mbd',label='Aceite Mbd') plot_prod_gas = hv.Curve(produccion,'fecha','gas_asociado_MMpcd',label='Gas Asociado MMpcd') plot_produccion = plot_prod_aceite * plot_prod_gas plot_produccion.opts(width=600, fontscale=1.5) plot_trayectoria = hv.Bars(elementos_trayectoria,'trayectoria','numero') plot_trayectoria.opts(stacked=True, color='trayectoria', cmap='Spectral', invert_axes=True, fontscale=1.5, yaxis=None) #fill_color=factor_cmap('trayectoria', palette=Spectral6, factors=elementos_trayectoria['trayectoria'])) plot_status = hv.Bars(elementos_status,'status','numero') plot_status.opts(stacked=True, color='status', fill_color=factor_cmap('status', palette=Spectral6, factors=elementos_status['status']), xrotation=90, invert_axes=True, fontscale=1.5, xticks=None, yaxis=None) row1 = tabla_pozos + plot_status + plot_trayectoria row2 = tabla_volumen + plot_produccion fig1 = hv.render(row1) hv.output(row1, backend='bokeh', fig='html', size=200) fig2 = hv.render(row2) hv.output(row2, backend='bokeh', fig='html', size=200) return
def polygons(gdf, label='', geometry='geometry', col=None, agg=ds.any): """Return a holoviews plot. Multiple holoviews plots can be collected in a list and plotted with hv.Layout(list_of_plots).cols(3). Parameters ---------- gdf : geopandas.GeoDataFrame label : str geometry : geometry column col : str Column on which the datashader data aggregation will be done. The default is `None`. agg : datashader aggregation function The default is `ds.any`. Returns ------- shd : holoviews plot """ hv.output(backend="matplotlib") sgdf = spd.GeoDataFrame(gdf) cvs = ds.Canvas() if col is not None: agg = cvs.polygons(sgdf, geometry, agg=agg(col)) else: agg = cvs.polygons(sgdf, geometry, agg=agg()) shd = hd.shade(hv.Image(agg, label=label)) return shd
def mark_color_3d_plot(self, elevation, azimuth, col1='c00', col2='c01', col3='c02'): hv.output(backend='matplotlib') scatter = [ hv.Scatter3D(mark_pos.loc[:, [col1, col2, col3]]).opts( dict(Scatter3D=dict(bgcolor='black', s=3))) for elec_id, mark_pos in self.unit_spks.items() ] overlay = hv.Overlay(scatter, label='Plot of spikes and their features ' + col1 + ', ' + col2 + ', and ' + col3) overlay = overlay.opts({ 'Scatter3D': { 'plot': { 'fig_size': 400, 'azimuth': int(azimuth), 'elevation': int(elevation) }, 'norm': { 'framewise': True } } }) return overlay
def makeOutput(game): gameFile = 'games/%s.txt' % game seqFile = 'json/%s_sequences.json' % game dataFile = 'json/%s_datapoints.json' % game p1, p2 = parsing.processInfosets(gameFile) parsing.processSeqIDs(p1, p2, seqFile) parsing.getData(p1, p2, dataFile) ''' hm1 = hv.HoloMap({(i.player,name): genCurves(i) for p in (p1,p2) for name,i in p.items()}, kdims=['player','infoset']) ''' p1 = hv.HoloMap({name: genCurves(i) for name,i in p1.items()}, kdims=['infoset']) p2 = hv.HoloMap({name: genCurves(i) for name,i in p2.items()}, kdims=['infoset']) grid1 = hv.GridSpace(p1) grid2 = hv.GridSpace(p2) layout1 = hv.NdLayout(grid1).cols(2) layout2 = hv.NdLayout(grid2).cols(2) hv.output(layout1 + layout2, size=150, filename=game)
def modify_doc(doc, mytabs): start, end = 1, 20 samples_count = 5 slider = Slider(start=start, end=end, value=start, step=1, title="Counts") select = Select(title="Count", value="aux", options=["box", "pack", "image", "user"]) renderer = hv.renderer('bokeh')##.instance(mode='server') hv.extension('bokeh') hv.output(size=200) links = pd.DataFrame(data['links']) print(links.head(3)) nodes = hv.Dataset(pd.DataFrame(data['nodes']), 'index') chord = hv.Chord((links, nodes)).select(value=(samples_count, None)) chord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str())) # Create HoloViews plot and attach the document hvplot = renderer.get_plot(chord, doc) def slider_update(attrname, old, new): # Notify the HoloViews stream of the slider update print ("update received") samples_count = new links = pd.DataFrame(data['links']) print(links.head(3)) nodes = hv.Dataset(pd.DataFrame(data['nodes']), 'index') chord = hv.Chord((links, nodes)).select(value=(samples_count, None)) chord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str())) # Create HoloViews plot and attach the document hvplot = renderer.get_plot(chord, doc) tab3 = Panel(child=row(slider, hvplot.state), title="Chord Plot") mytabs.append(tab3) views = Tabs(tabs = mytabs) layout=row(views) doc.add_root(layout) return doc slider.on_change('value', slider_update) def select_update(attrname, old, new): # Notify the HoloViews stream of the slider update print ("update received. Old: {} New: {}".format(old, new)) select.on_change('value', select_update) # Combine the holoviews plot and widgets in a layout tab3 = Panel(child=row(slider, hvplot.state), title="Chord Plot") mytabs.append(tab3) views = Tabs(tabs = mytabs) layout=row(views) doc.add_root(layout) return doc
def plot_color_3d_dynamic(self, col1, col2, col3): hv.output(backend='matplotlib') dmap = hv.DynamicMap(callback=functools.partial( self.mark_color_3d_plot, col1=col1, col2=col2, col3=col3), kdims=['elevation', 'azimuth'], cache_size=1) dmap = dmap.redim.values(elevation=range(0, 181, 5), azimuth=range( -90, 91, 5)).opts(norm=dict(framewise=True)) return dmap
def frames(self,var='depth', tiles=False, **kwargs): x = kwargs.get('x',self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get('y',self._obj.SCHISM_hgrid_node_y[:].values) try: t = kwargs.get('t',self._obj.time.values) except: pass tes = kwargs.get('tri3',self._obj.SCHISM_hgrid_face_nodes.values[:,:4]) # sort out quads try: mask = np.isnan(tes)[:,3] tr3 = tes[mask][:,:3] tr3_ = quads_to_tris(tes[~mask]) if tr3_ : tri3 = np.append(tr3,tr3_,axis=0).astype(int) else: tri3 = tr3.astype(int) except: tri3 = tes.astype(int) times=kwargs.get('times',self._obj.time.values) z = kwargs.get('z',self._obj[var].values[0,:].flatten()) zmin = self._obj[var].values.min() zmax = self._obj[var].values.max() nodes = pd.DataFrame({'longitude':x,'latitude':y, '{}'.format(var):z}) elems = pd.DataFrame(tri3, columns=['a', 'b', 'c']) width = kwargs.get('width', 800) height = kwargs.get('height', 600) opts.defaults(opts.WMTS(width=width, height=height)) tile = gv.WMTS('https://b.tile.openstreetmap.org/{Z}/{X}/{Y}.png') points = gv.operation.project_points(gv.Points(nodes, vdims=['{}'.format(var)])) trimesh = gv.TriMesh((elems, points)) def time_mesh(time): points.data[var] = self._obj[var].sel(time=time).values return gv.TriMesh((elems, points))#, crs=ccrs.GOOGLE_MERCATOR) meshes = hv.DynamicMap(time_mesh, kdims=['Time']).redim.values(Time=times) imesh = rasterize(meshes, aggregator='mean').opts(cmap='viridis', colorbar=True, padding=.1, tools=['hover'], clim=(zmin, zmax)) if tiles: return hv.output(tile*imesh, holomap='scrubber', fps=1) else: return hv.output(imesh.opts(width=width,height=height), holomap='scrubber', fps=1)
def plot_chordgraph(G, node_data=None, label_col='index', size=200, cmap='Category20', title='', draw_labels=True, edge_color='E-Group', node_color='index'): hv.extension('bokeh') hv.output(size=size) # Get the edge list of the graph H = nx.to_undirected(G) edge_data = nx.to_pandas_edgelist(H) # Enforce that the value column is in the right position in the dataframe print(edge_data.columns) val_loc = 2 cur_loc = edge_data.columns.get_loc("value") cols = list(edge_data.columns.values) swap = cols[val_loc] cols[val_loc] = cols[cur_loc] cols[cur_loc] = swap edge_data = edge_data.reindex(columns=cols) # Account for passed in node dataset if node_data is not None: node_dataset = hv.Dataset(node_data, 'index') chord = hv.Chord((edge_data, node_dataset), label=title).select(value=(5, None)) else: chord = hv.Chord(edge_data, label=title) label_col = 'index' # Draw the desired graph if draw_labels is True: chord.opts( hv.opts.Chord( cmap=cmap, edge_cmap=cmap, edge_color=hv.dim(edge_color).str(), node_color=hv.dim(node_color).str(), labels=label_col, )) else: chord.opts( hv.opts.Chord(cmap=cmap, edge_cmap=cmap, edge_color=hv.dim(edge_color).str(), node_color=hv.dim(node_color).str())) c = hv.render(chord, backend='bokeh') return c
def plot_network(all_transactions): hv.extension('bokeh') hv.output(size=500) links = pd.DataFrame(generate_links(all_transactions)) print(links) hv.Chord(links) nodes = hv.Dataset(pd.DataFrame(generate_nodes(all_transactions)), 'index') nodes.data.head() chord = hv.Chord((links, nodes)).select(value=(1, None)) chord.opts( opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str())) hv.save(chord, 'image.html') print("Network analysis complete, saved as image.html")
def make_chords(dict_df, type_IO='_PhysUse'): hv.extension('bokeh') hv.output(size=250) for key_dict, dataf in dict_df.items(): links = dict_df[key_dict] nodes = hv.Dataset(pd.DataFrame(node_d), 'index') chord = hv.Chord((links, nodes)).select(value=(5, None)) chord.opts( opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str())) hv.save(chord, 'chord_' + str(key_dict) + str(type_IO) + '.html')
def chord(data): hv.extension('bokeh') renderer=hv.renderer('bokeh') hv.output(size=230) links=pd.DataFrame(data['links']) hv.Chord(links) nodes=hv.Dataset(pd.DataFrame(data['nodes']), 'index') chord=hv.Chord((links, nodes)).select(value=(10, None)) chord.opts( opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str())) bokeh_plot=renderer.get_plot(chord).state html=file_html(bokeh_plot,CDN,"my plot") return html
def _make_heatmap(final_grid, density, fname, target): logger.info('Drawing the heatmap') lat = np.linspace(-90, 90, num=180 * density + 1) lon = np.linspace(-179.75, 180, num=360 * density) d = { 'coords': { 'latitude': { 'dims': ('latitude', ), 'data': lat }, 'longitude': { 'dims': ('longitude', ), 'data': lon } }, 'attrs': { 'title': '%s latency heatmap' % target }, 'dims': ['latitude', 'longitude'], 'data_vars': { 'latency': { 'dims': ('latitude', 'longitude'), 'data': final_grid } } } xr_set = xr.Dataset.from_dict(d) xr_dataset = gv.Dataset(xr_set, kdims=['latitude', 'longitude'], vdims=['latency'], crs=crs.PlateCarree()) hv.output(dpi=200, size=500) hv.opts("Image [colorbar=True clipping_colors={'min': 'lightgrey'}]") main_layer = xr_dataset.redim(latency=dict(range=(30, 90))).to( gv.Image, ['longitude', 'latitude']) img = gf.land * main_layer * gf.ocean * gf.borders if fname.endswith('.png'): fname = fname[:-4] renderer = hv.renderer('matplotlib').instance(fig='png') renderer.save(img, fname, style=dict(Image={'cmap': 'RdYlGn_r'}))
def plot_gasto_inicial(): dispersion_qi = hv.Points(tipos,kdims=['first_oil','Qi_hist']) dispersion_qi.opts(width=600, size=20, color='tipo', cmap='Set1', fontscale=1.5, legend_position='top') fig = hv.render(dispersion_qi) hv.output(dispersion_qi, backend='bokeh', fig='html', size=200) return
def chord_chard(data): """ Takes in processed dataframe for multilabel classification problem and computes label co-occurences. Draws chord chard using bokeh and local server. """ hv.extension('bokeh') hv.output(size=200) labels_only = data.drop(labels = ['PIMS_ID', 'language', 'description', 'all_logs', 'text'], axis=1) cooccurrence_matrix = np.dot(labels_only.transpose(),labels_only) cooccurrence_matrix_diagonal = np.diagonal(cooccurrence_matrix) with np.errstate(divide='ignore', invalid='ignore'): cooccurrence_matrix_percentage = np.nan_to_num(np.true_divide(cooccurrence_matrix, cooccurrence_matrix_diagonal[:, None])) coocc = labels_only.T.dot(labels_only) diagonal = np.diagonal(coocc) co_per = np.nan_to_num(np.true_divide(coocc, diagonal[:, None])) df_co_per = pd.DataFrame(co_per) df_co_per = pd.DataFrame(data=co_per, columns=coocc.columns, index=coocc.index) #replace diagonal with 0: coocc.values[[np.arange(coocc.shape[0])]*2] = 0 coocc = coocc.mask(np.triu(np.ones(coocc.shape, dtype=np.bool_))) coocc = coocc.fillna(0) data = hv.Dataset((list(coocc.columns), list(coocc.index), coocc), ['source', 'target'], 'value').dframe() data['value'] = data['value'].astype(int) chord = hv.Chord(data) plot = chord.opts( node_color='index', edge_color='source', label_index='index', cmap='Category20', edge_cmap='Category20', width=400, height=400) bokeh_server = pn.Row(plot).show(port=1234)
def quick_plot(da, dims, redim_range=None, **user_options): options = dict(cmap="viridis", colorbar=True, width=700, height=450) options.update(user_options) name = da.name dataset = hv.Dataset(da) image = dataset.to(hv.QuadMesh, dims, dynamic=True).options(**options) if redim_range is not None: image = image.redim.range(**{name: redim_range}) return hv.output(image, backend="bokeh")
def ChordDiagram(self): # Step 1 Get Data self.sharedNeos = self.GetShared() self.matchedNeos = self.GetMatchedNeos() hv.output(size=200) source = [] target = [] value = [] for i, sam in enumerate(self.sharedNeos): for pair in self.sharedNeos[sam]: source.append(sam + "_" + pair.split(',')[0]) target.append(sam + "_" + pair.split(',')[1]) value.append(self.sharedNeos[sam][pair]) for matched in self.matchedNeos: source.append(matched.split(',')[0]) target.append(matched.split(',')[1]) value.append(self.matchedNeos[matched]) links = pd.DataFrame({ 'source': source, 'target': target, 'value': value }) chord = hv.Chord(links) # chord = hv.Chord((links, nodes)).select(value=(5, None)) chord.opts( opts.Chord(cmap='Category20', edge_cmap='Category20', labels='index', node_color=dim('index').str())) p = hv.render(chord) select = Select(title="Option:", value="foo", options=["foo", "bar", "baz", "quux"]) return (p, select)
def modify_doc(doc): def sine_curve(phase, freq): xvals = [0.1 * i for i in range(100)] return hv.Curve((xvals, [np.sin(phase + freq * x) for x in xvals])) phases = [0, np.pi / 2, np.pi, 3 * np.pi / 2] frequencies = [0.5, 0.75, 1.0, 1.25] curve_dict_2D = {(p, f): sine_curve(p, f) for p in phases for f in frequencies} gridspace = hv.GridSpace(curve_dict_2D, kdims=['phase', 'frequency']) hv.output(size=50) hmap = hv.HoloMap(gridspace) final = hmap + hv.GridSpace(hmap) plot = renderer.get_plot(final) layout = row(plot.state) # renderer.server_doc(layout) doc.add_root(layout)
def plot_chord(predictions, filename): to_use = predictions.copy() for n, row in predictions.iterrows(): if row.MLSynergy_score < 0: to_use.at[n, "MLSynergy_score"] = row.MLSynergy_score * -1 to_use.at[n, "Interaction"] = "Synergy" else: to_use.at[n, "Interaction"] = "Antagony" hv.extension('bokeh') hv.output(size=200) to_use2 = to_use[to_use.NumbDrugs == 2] links = to_use2[["Drug1", "Drug2", "MLSynergy_score", "Interaction"]] drugs = list(links["Drug1"].unique()) + list(links["Drug2"].unique()) nodes = hv.Dataset(drugs, 'Drug') chord = hv.Chord((links, nodes)).select(value=(1, None)) chord.opts(opts.Chord(cmap='Rainbow', edge_cmap='Rainbow',\ edge_color=dim('Interaction').str(), labels='Drug',\ node_color=dim('Drug').str())) output_file(filename) show(hv.render(chord)) return to_use2
def water_gif(ds, fps=2, animate=True): """ Water area animation. Description ---------- Visualize the temporal dynamics of detected water area in animation. Parameters ---------- ds: xr.Dataset A multi-dimensional array with x,y and time dimensions and one or more data variables. fps: int Frame per second (default = 2). animate: boolean If True, output will be displayed in gif format. If False, a control slider will be display for user to manually visualize changes in different time steps. Returns ------- masked_dataset: xr.Dataset A xr.Dataset like the input dataset with only pixels which are within the polygons of the geopandas.Geodataframe. Every other pixel is given the value NaN. """ try: ds[['longitude', 'latitude']] except Exception: print("'latitude' and 'longitude' cannot be found.") try: ds_new = hv.Dataset(ds.water) #set up dataset for animation images = ds_new.to(hv.Image, ['longitude', 'latitude']).options( fig_inches=(6.5, 5), colorbar=True, cmap=plt.cm.Blues) except Exception: print("Unexpected Error.") if animate == True: hv.output(images, holomap='gif', fps=fps) #animation output inline else: return images
def save(self, filename: PathLike, *, dpi: int = 600, title: Optional[str] = None) -> None: """Save the image to disk. Parameters ---------- filename : PathLike Image file dpi : int Image resolution title : str Figure title """ hv.output(dpi=dpi) hv.save( self._figure, filename=filename, fmt=Path(filename).suffix[1:], backend="matplotlib", title=title, )
def setLibrary(library='bokeh'): ## Use Bokeh by default if (library == 'bokeh'): hv.extension('bokeh') hv.archive.auto(filename_formatter="{obj:.7}") ## For notebooks opts.defaults( opts.Scatter(tools=['hover'], width=700, height=700, padding=0.05), opts.HeatMap(tools=['hover'], width=700, height=700, labelled=[], xrotation=45, colorbar=True, cmap=('Blues'))) # opts.HeatMap(tools=['hover'], width=700, height=700, labelled=[], # xrotation=45, colorbar=True, cmap=('Blues'))) ## The library that Bokeh uses to export to SVG is not longer supported ## and so cannot be exported to SVG elif (library == 'matplotlib'): hv.extension('matplotlib') hv.output(fig='svg') opts.defaults( opts.Scatter(fig_size=300, padding=0.05), opts.HeatMap(fig_size=300, labelled=[], xrotation=45, colorbar=True, cmap=('Blues'))) else: raise NotImplementedError("Unknown plotting library.")
def frames(self, var='depth', **kwargs): x = kwargs.get('x', self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get('y', self._obj.SCHISM_hgrid_node_y[:].values) try: t = kwargs.get('t', self._obj.time.values) except: pass tri3 = kwargs.get( 'tri3', self._obj.SCHISM_hgrid_face_nodes.values[:, :3].astype(int)) times = kwargs.get('times', self._obj.time.values) z = kwargs.get('z', self._obj[var].values[0, :].flatten()) nodes = pd.DataFrame({ 'longitude': x, 'latitude': y, '{}'.format(var): z }) elems = pd.DataFrame(tri3, columns=['a', 'b', 'c']) opts.defaults(opts.WMTS(width=800, height=400)) tiles = gv.WMTS( 'https://maps.wikimedia.org/osm-intl/{Z}/{X}/{Y}@2x.png') points = gv.operation.project_points( gv.Points(nodes, vdims=['{}'.format(var)])) trimesh = gv.TriMesh((elems, points)) def time_mesh(time): points.data[var] = self._obj[var].sel(time=time).values return gv.TriMesh((elems, points)) #, crs=ccrs.GOOGLE_MERCATOR) meshes = hv.DynamicMap(time_mesh, kdims=['Time']).redim.values(Time=times) imesh = rasterize(meshes, aggregator='mean').opts(cmap='viridis', colorbar=True, padding=.1, tools=['hover']) return hv.output(tiles * imesh, holomap='scrubber', fps=1)
from bokeh.document import without_document_lock from bokeh.models.widgets import Button, Paragraph, Div from functools import partial from threading import Thread import threader import sqs_nqs_tools.online as online import sqs_nqs_tools.online.bokeh as online_bokeh import sqs_nqs_tools as tools # module configs hv.extension('bokeh') renderer = hv.renderer( 'bokeh') # renderer to convert objects from holoviews to bokeh renderer = renderer.instance(mode="server") hv.output(dpi=300, size=100) doc = curdoc() # DOC for Bokeh Objects ######### PARAMETERS MAY TO BE MODIFIED BY USER ######### # DATA SOURCE ## please comment out the data source you don't want to use - live means grabbing from online live stream, emulated live can access a karabo bridge emulated stream source = 'tcp://10.253.0.142:6666' # LIVE #~ source = 'tcp://127.0.0.1:8010' # emulated live # DEVICES IN DATA STREAM ## booleans defining which devices to pull from stream, when pnccd is running without tof this may lead to errors ## there was no testing on setting other than the default values tof_in_stream = True pnCCD_in_stream = True gmd_in_stream = False
columns=['cent_value']) e_cent_size.reset_index(drop=True, inplace=True) e_cent_size = e_cent_size * 100 page_rank_size = pd.DataFrame.from_dict(page_rank, orient='index', columns=['rank_value']) page_rank_size.reset_index(drop=True, inplace=True) page_rank_size = page_rank_size * 1000 # Add the analysis data to the node dat input for the graph nodes_extended = nodes.join([e_cent_size, page_rank_size]) # Now we switch to holoviews to render the plot # Specify the plot render to use hv.extension('bokeh') hv.output(size=300) # Chord diagram with interactive components edgeList = edges[['Source', 'Target', 'weight']] # Within the holoviews dataset object we define kdim and vdims # Kdims are the independent variables which is Id in this example # Vdims are dependent variables cent_value and rank_value # By defining these here were can use them when creating the graph nodeDS = hv.Dataset(nodes_extended, 'Id', ['cent_value', 'rank_value']) # Coloured interactive chord diagram with node size determined by Vdims kwargs = dict(width=300, height=300, xaxis=None, yaxis=None) opts.defaults(opts.Nodes(**kwargs), opts.Graph(**kwargs)) graph = hv.Graph((edgeList, nodeDS), label='GoT season 1') graph.opts(cmap='Category20',
def append_panels(hv_panel, imgdata_list, chart_format): imgdata_list.append(hv.output(hv_panel, backend='bokeh', fig=chart_format)) return imgdata_list
# In[4]: minian_ds = open_minian_mf(dpath, id_dims, pattern=f_pattern, backend='zarr') # ## estimate shifts # In[5]: temps = minian_ds['Y'].max('frame').compute().rename('temps') shifts = estimate_shifts(temps, max_sh=param_t_dist, dim='session').compute() shifts = shifts.rename('shifts') temps_sh = apply_shifts(temps, shifts).compute().rename('temps_shifted') shiftds = xr.merge([temps, shifts, temps_sh]) hv.output(size=output_size) opts_im = { 'aspect': shiftds.sizes['width'] / shiftds.sizes['height'], 'frame_width': 500, 'cmap': 'viridis' } hv_temps = (hv.Dataset(temps).to( hv.Image, kdims=['width', 'height']).opts(**opts_im).layout('session').cols(1)) hv_temps_sh = (hv.Dataset(temps_sh).to( hv.Image, kdims=['width', 'height']).opts(**opts_im).layout('session').cols(1)) hv.output(size=output_size) opts_im = { 'aspect': shiftds.sizes['width'] / shiftds.sizes['height'],
def frames(self, var="depth", tiles=False, **kwargs): x = kwargs.get("x", self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get("y", self._obj.SCHISM_hgrid_node_y[:].values) try: t = kwargs.get("t", self._obj.time.values) except: pass tes = kwargs.get("tri3", self._obj.SCHISM_hgrid_face_nodes.values[:, :4]) # sort out quads try: mask = np.isnan(tes)[:, 3] tr3 = tes[mask][:, :3] tr3_ = quads_to_tris(tes[~mask]) if tr3_: tri3 = np.append(tr3, tr3_, axis=0).astype(int) else: tri3 = tr3.astype(int) except: tri3 = tes.astype(int) times = kwargs.get("times", self._obj.time.values) z = kwargs.get("z", self._obj[var].values[0, :].flatten()) zmin = self._obj[var].values.min() zmax = self._obj[var].values.max() nodes = pd.DataFrame({ "longitude": x, "latitude": y, "{}".format(var): z }) elems = pd.DataFrame(tri3, columns=["a", "b", "c"]) width = kwargs.get("width", 800) height = kwargs.get("height", 600) opts.defaults(opts.WMTS(width=width, height=height)) tile = gv.WMTS("https://b.tile.openstreetmap.org/{Z}/{X}/{Y}.png") points = gv.operation.project_points( gv.Points(nodes, vdims=["{}".format(var)])) trimesh = gv.TriMesh((elems, points)) def time_mesh(time): points.data[var] = self._obj[var].sel(time=time).values return gv.TriMesh((elems, points)) # , crs=ccrs.GOOGLE_MERCATOR) meshes = hv.DynamicMap(time_mesh, kdims=["Time"]).redim.values(Time=times) imesh = rasterize(meshes, aggregator="mean").opts(cmap="viridis", colorbar=True, padding=0.1, tools=["hover"], clim=(zmin, zmax)) if tiles: return hv.output(tile * imesh, holomap="scrubber", fps=1) else: return hv.output(imesh.opts(width=width, height=height), holomap="scrubber", fps=1)
import holoviews as hv import geoviews as gv from holoviews import opts from holoviews.operation.datashader import datashade, rasterize from datashader.colors import viridis import geoviews.feature as gf import cartopy.crs as ccrs import cartopy.feature as cfeature import xarray as xr import pandas as pd import panel as pn from hvplot import xarray from pyposeidon.utils.quads2tr import quads_to_tris gv.extension("bokeh") hv.output(widget_location="bottom") import sys import os @xr.register_dataset_accessor("hplot") # @xr.register_dataarray_accessor('pplot') class hplot(object): def __init__(self, xarray_obj): self._obj = xarray_obj def contourf(self, var="depth", it=None, tiles=False, **kwargs): x = kwargs.get("x", self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get("y", self._obj.SCHISM_hgrid_node_y[:].values)
def init_renderer(): hv.extension('matplotlib') hv.output(backend='matplotlib') pyplot.switch_backend(RENDERER_NAME)
def interactive( umap_object, labels=None, values=None, hover_data=None, theme=None, cmap="Blues", color_key=None, color_key_cmap="Spectral", background="white", width=800, height=800, point_size=None, subset_points=None, ): """Create an interactive bokeh plot of a UMAP embedding. While static plots are useful, sometimes a plot that supports interactive zooming, and hover tooltips for individual points is much more desireable. This function provides a simple interface for creating such plots. The result is a bokeh plot that will be displayed in a notebook. Note that more complex tooltips etc. will require custom code -- this is merely meant to provide fast and easy access to interactive plotting. Parameters ---------- umap_object: trained UMAP object A trained UMAP object that has a 2D embedding. labels: array, shape (n_samples,) (optional, default None) An array of labels (assumed integer or categorical), one for each data sample. This will be used for coloring the points in the plot according to their label. Note that this option is mutually exclusive to the ``values`` option. values: array, shape (n_samples,) (optional, default None) An array of values (assumed float or continuous), one for each sample. This will be used for coloring the points in the plot according to a colorscale associated to the total range of values. Note that this option is mutually exclusive to the ``labels`` option. hover_data: DataFrame, shape (n_samples, n_tooltip_features) (optional, default None) A dataframe of tooltip data. Each column of the dataframe should be a Series of length ``n_samples`` providing a value for each data point. Column names will be used for identifying information within the tooltip. theme: string (optional, default None) A color theme to use for plotting. A small set of predefined themes are provided which have relatively good aesthetics. Available themes are: * 'blue' * 'red' * 'green' * 'inferno' * 'fire' * 'viridis' * 'darkblue' * 'darkred' * 'darkgreen' cmap: string (optional, default 'Blues') The name of a matplotlib colormap to use for coloring or shading points. If no labels or values are passed this will be used for shading points according to density (largely only of relevance for very large datasets). If values are passed this will be used for shading according the value. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key: dict or array, shape (n_categories) (optional, default None) A way to assign colors to categoricals. This can either be an explicit dict mapping labels to colors (as strings of form '#RRGGBB'), or an array like object providing one color for each distinct category being provided in ``labels``. Either way this mapping will be used to color points according to the label. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key_cmap: string (optional, default 'Spectral') The name of a matplotlib colormap to use for categorical coloring. If an explicit ``color_key`` is not given a color mapping for categories can be generated from the label list and selecting a matching list of colors from the given colormap. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. background: string (optional, default 'white') The color of the background. Usually this will be either 'white' or 'black', but any color name will work. Ideally one wants to match this appropriately to the colors being used for points etc. This is one of the things that themes handle for you. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. width: int (optional, default 800) The desired width of the plot in pixels. height: int (optional, default 800) The desired height of the plot in pixels point_size: int (optional, default None) The size of each point marker subset_points: array, shape (n_samples,) (optional, default None) A way to select a subset of points based on an array of boolean values. Returns ------- """ if theme is not None: cmap = _themes[theme]["cmap"] color_key_cmap = _themes[theme]["color_key_cmap"] background = _themes[theme]["background"] if labels is not None and values is not None: raise ValueError( "Conflicting options; only one of labels or values should be set") points = umap_object.embedding_ if subset_points is not None: if len(subset_points) != points.shape[0]: raise ValueError( "Size of subset points ({}) does not match number of input points ({})" .format(len(subset_points), points.shape[0])) points = points[subset_points] if points.shape[1] != 2: raise ValueError( "Plotting is currently only implemented for 2D embeddings") if point_size is None: point_size = 100.0 / np.sqrt(points.shape[0]) data = pd.DataFrame(umap_object.embedding_, columns=("x", "y")) if labels is not None: data["label"] = labels if color_key is None: unique_labels = np.unique(labels) num_labels = unique_labels.shape[0] color_key = _to_hex( plt.get_cmap(color_key_cmap)(np.linspace(0, 1, num_labels))) if isinstance(color_key, dict): data["color"] = pd.Series(labels).map(color_key) else: unique_labels = np.unique(labels) if len(color_key) < unique_labels.shape[0]: raise ValueError( "Color key must have enough colors for the number of labels" ) new_color_key = { k: color_key[i] for i, k in enumerate(unique_labels) } data["color"] = pd.Series(labels).map(new_color_key) colors = "color" elif values is not None: data["value"] = values palette = _to_hex(plt.get_cmap(cmap)(np.linspace(0, 1, 256))) colors = btr.linear_cmap("value", palette, low=np.min(values), high=np.max(values)) else: colors = matplotlib.colors.rgb2hex(plt.get_cmap(cmap)(0.5)) if subset_points is not None: data = data[subset_points] if hover_data is not None: hover_data = hover_data[subset_points] if points.shape[0] <= width * height // 10: if hover_data is not None: tooltip_dict = {} for col_name in hover_data: data[col_name] = hover_data[col_name] tooltip_dict[col_name] = "@" + col_name tooltips = list(tooltip_dict.items()) else: tooltips = None # bpl.output_notebook(hide_banner=True) # this doesn't work for non-notebook use data_source = bpl.ColumnDataSource(data) plot = bpl.figure( width=width, height=height, tooltips=tooltips, background_fill_color=background, ) plot.circle(x="x", y="y", source=data_source, color=colors, size=point_size) plot.grid.visible = False plot.axis.visible = False # bpl.show(plot) else: if hover_data is not None: warn("Too many points for hover data -- tooltips will not" "be displayed. Sorry; try subssampling your data.") hv.extension("bokeh") hv.output(size=300) hv.opts( 'RGB [bgcolor="{}", xaxis=None, yaxis=None]'.format(background)) if labels is not None: point_plot = hv.Points(data, kdims=["x", "y"]) plot = hd.datashade( point_plot, aggregator=ds.count_cat("color"), color_key=color_key, cmap=plt.get_cmap(cmap), width=width, height=height, ) elif values is not None: min_val = data.values.min() val_range = data.values.max() - min_val data["val_cat"] = pd.Categorical( (data.values - min_val) // (val_range // 256)) point_plot = hv.Points(data, kdims=["x", "y"], vdims=["val_cat"]) plot = hd.datashade( point_plot, aggregator=ds.count_cat("val_cat"), cmap=plt.get_cmap(cmap), width=width, height=height, ) else: point_plot = hv.Points(data, kdims=["x", "y"]) plot = hd.datashade( point_plot, aggregator=ds.count(), cmap=plt.get_cmap(cmap), width=width, height=height, ) return plot