def graph(self, epoch=-1, batch_start=-1, batch_end=-1, phase=''): cpu_util = self._cpu_util gpu_util = self._gpu_util if epoch != -1: phase_index = -1 if phase: phase_index = PHASE_INDEX[phase] st, et = self._get_st_et(batch_start, batch_end, phase_index) cpu_util = self._filter_cpu_util(st, et) gpu_util = self._filter_gpu_util(st, et) cpu_vdims = [('Util', 'Utilization')] cpu_ds = hv.Dataset(cpu_util, ['Time', 'CPU_Id'], cpu_vdims) gpu_vdims = [('Util', 'Utilization')] gpu_ds = hv.Dataset(gpu_util, ['Time', 'GPU_Id'], gpu_vdims) cpu_agg = cpu_ds.aggregate('Time', function=np.mean, spreadfn=np.std) gpu_agg = gpu_ds.aggregate('Time', function=np.mean, spreadfn=np.std) cpu_plt = hv.Curve(cpu_agg, label='CPU').options(ylim=(0, 100)) gpu_plt = hv.Curve(gpu_agg, label='GPU') overlay = cpu_plt * gpu_plt return overlay
def plot_pge_vs_trace(self, bnum_it=None): import holoviews as hv from holoviews.operation import decimate import pandas as pd, numpy as np hv.extension('bokeh') def byte_to_color(idx): return hv.Palette.colormaps['Category20'](idx / 16.0) if bnum_it is None: bnum_it = range(16) ret = self.pge_vs_trace(bnum_it[0]) curve = hv.Curve( (ret[0], ret[1]), "Traces Used in Calculation", "Partial Guessing Entrop of Byte").options(title="PGE Vs. Traces") for bnum in bnum_it[1:]: ret = self.pge_vs_trace(bnum) curve *= hv.Curve( (ret[0], ret[1])).opts(color=byte_to_color(bnum)).opts(width=900, height=600) return curve
def create_fit(self, filename): if filename in self.data["keys"]: Rc, std, fit = self.calculate_resistance(filename, fit=True) x = self.data[filename]["data"]["current"] y = self.data[filename]["data"]["voltage_vsrc"] scatter = hv.Scatter((x, y), kdims=self.measurements[1], vdims=self.measurements[2]) scatter.opts(color='green') curve = scatter * hv.Curve((x, fit)) curve.opts(**self.config["CBKR"].get("General", {}), xrotation=45, title="Index: " + str(self.filename_df.loc[self.filename_df['Filename'] == filename].index[0]), ylim=(np.sign(y.min()) * abs(y.min()) - abs((y.max() - y.min()) * (1/8)), y.max() * (9/8))) #just a complicated way to set ylim() because ymin() can be < or > 0 self.PlotDict["All"] = self.PlotDict["All"] + curve
def symmetric_fit_plot(d, params, add_offset=True): """Quick intensity plot. Compare data with params generated curve. Args: params: (center, height, width, fwhm, cutoff_fwhm, npoints) """ dataplot = hv.Scatter((d.xum, d.Xsym * 1e6), label='data', kdims=['um'], vdims=['uV']) fitx = np.linspace(min(d.xum), max(d.xum), 200) if add_offset: offset = (max(d.Xsym) + min(d.Xsym)) / 2.0 fit = (oersted_func(fitx/1e6, **params) + offset) * 1e6 fitplot = hv.Curve((fitx, fit), label='fit', kdims=['um'], vdims=['uV']) return dataplot * fitplot
def regression_lines_spread(data, meas_stats, estim_stats): '''given (x_vals[N], y_vals[NumExperiments, N], return hv.Spread overlaid with jittered values''' x_vals, y_meas = data y_meas_ave, y_meas_std = meas_stats y_estim_ave, y_estim_std = estim_stats NumExperiments, N = y_meas.shape h_spread_pts = hv.Spread((x_vals, y_estim_ave, y_estim_std))*\ hv.Spread((x_vals, y_meas_ave, y_meas_std))*\ hv.Curve( (x_vals, y_estim_ave)) return h_spread_pts
def plot(self, epochs: List[int], title: str = "Learning Rate Schedule") -> hv.Layout: """ Compute the set of learning rates for each corresponding epochs and plots it. :param epochs: Compute the set of learning rates for each corresponding epoch. :param title: The title of the plot. :return: """ lrs = [self(i) for i in epochs] data = {'learning rate': lrs, 'epoch': epochs} return hv.Curve(data, kdims='epoch', vdims='learning rate').opts(title=title)
def test_holoviews_with_widgets_not_shown(document, comm): hmap = hv.HoloMap({(i, chr(65 + i)): hv.Curve([i]) for i in range(3)}, kdims=['X', 'Y']) hv_pane = HoloViews(hmap, show_widgets=False) layout_obj = Column(hv_pane, hv_pane.widget_box) layout = layout_obj.get_root(document, comm) model = layout.children[0] assert len(hv_pane.widget_box.objects) == 2 assert hv_pane.widget_box.objects[0].name == 'X' assert hv_pane.widget_box.objects[1].name == 'Y' assert hv_pane._models[layout.ref['id']][0] is model hmap = hv.HoloMap({(i, chr(65 + i)): hv.Curve([i]) for i in range(3)}, kdims=['A', 'B']) hv_pane.object = hmap assert len(hv_pane.widget_box.objects) == 2 assert hv_pane.widget_box.objects[0].name == 'A' assert hv_pane.widget_box.objects[1].name == 'B'
def p2(self, tx_ts, rx_ts, xlim=None, rxlim=None, txlim=None): assert len(tx_ts) == len( rx_ts), 'tx and rx time series must be the same length' tx_signal = hv.Dimension('tx_signal', label='TX Signal', unit='A') rx_signal = hv.Dimension('rx_signal', label='RX Signal', unit='V') # assume 4096 Hz if self.end: dt_times = pd.date_range(start=self.start, end=self.end, periods=len(tx_ts), tz=None, normalize=False, name=None, closed=None) #.values dt_times = dt_times.tz_localize(None).astype('datetime64[ns]') hv_time_range = (self.start.tz_localize(None), self.end.tz_localize(None)) #dt_start = np.datetime64(dt_start.tz_localize(None)) #dt_end = np.datetime64(dt_end.tz_localize(None)) x_dim = hv.Dimension('time', label='Time', range=hv_time_range) else: dt_times = np.arange(len(tx_ts)) x_dim = hv.Dimension('sample', label='Sample number') tx_curve = hv.Curve((dt_times, tx_ts), x_dim, tx_signal) rx_curve = hv.Curve((dt_times, rx_ts), x_dim, rx_signal) txp = self.ds(tx_curve).opts(height=200, width=800) rxp = self.ds(rx_curve).opts(height=200, width=800) if xlim: txp = txp.opts(xlim=xlim) rxp = rxp.opts(xlim=xlim) if rxlim: rxp = rxp.opts(ylim=rxlim) if txlim: txp = txp.opts(ylim=txlim) return (txp + rxp).cols(1)
def stats_grid_plot(data): plots = [] for stat in FIELD_STATS: subdata = data[data['stat'] == stat] overlay = [] for i in sorted(set(data['network'])): overlay.append( holoviews.Curve(subdata[subdata['network'] == i], kdims=['turn'], vdims=('value', stat), label=i)) plots.append(holoviews.Overlay(overlay)) return holoviews.Layout(plots)
def add_face_to_plot(self, face): """Adiciona uma face ao plot. Função mais útil durante o debugging. Args: face (wingededge.Face): face. """ vertices = self.we.get_face_vertices(face.id) data = {"x": [], "y": []} for vertex_id in [*vertices, vertices[0]]: coords = self.we.vertices[vertex_id].coords data["x"].append(coords[0]) data["y"].append(coords[1]) self.plot *= hv.Curve(data, "x", "y", label=f"Face {face.id}")
def hv_plot(df_tidy, to_plot = 'all'): if to_plot == 'all': hv_fig = hv.Curve(df_tidy, kdims = ['time', 'concentration'], vdims = ['species'] ).groupby('species' ).overlay( ).opts(frame_height=250,frame_width=250 * 3 // 2) else: df_small = df_tidy.loc[df_tidy['species'].isin(to_plot), :] hv_fig = hv.Curve(df_small, kdims = ['time', 'concentration'], vdims = ['species'] ).groupby('species' ).overlay( ).opts(frame_height=250,frame_width=250 * 3 // 2) # Take out the Bokeh object p = hv.render(hv_fig) return p
def get_plot_curve(x, y): mdist, mdata = extract_timeseries(x, y, data.sx, data.sy, dataset) if mdist > data.mdist: mdata = mdata * np.nan hdynamic = hv.Curve((data.time, mdata)).opts(color='k', line_width=2, line_dash='dotted') hcurve = hv.HoloMap({ 'dynamic': hdynamic, **{(i + 1): k for i, k in enumerate(data.curve)} }).overlay() return hcurve
def _create_promince_plot(pkx, pky, peak_props, color=GREEN): line_x = pkx line_y0 = pky line_y1 = pky - peak_props["prominences"] lines = [] for xxx, yy0, yy1 in zip(line_x, line_y0, line_y1): data = { "x": [xxx, xxx], "y": [yy0, yy1], } plot = hv.Curve(data).opts(color=color, line_width=4, default_tools=[]) lines.append(plot) return hv.Overlay(lines)
def test_holoviews_widgets_update_plot(document, comm): hmap = hv.HoloMap({(i, chr(65 + i)): hv.Curve([i]) for i in range(3)}, kdims=['X', 'Y']) hv_pane = HoloViews(hmap, backend='bokeh') layout = hv_pane.get_root(document, comm) cds = layout.children[0].select_one(ColumnDataSource) assert cds.data['y'] == np.array([0]) hv_pane.widget_box[0].value = 1 hv_pane.widget_box[1].value = chr(65 + 1) assert cds.data['y'] == np.array([1])
def visualize_time_series(self, stats:List[str], rename_cols:Dict[str, str]=dict(), options:opts=opts()): """ Plots the given stats over a period of a time :param stats: names of statistics to plot :param rename_cols: any human readable names for the statistics :param options: plotting options for holoviews """ df = pd.DataFrame(self.trend).fillna(0) stats = list(map(lambda s: rename_cols.get(s, s), df.columns & stats)) df = df.rename(columns=rename_cols) point_curves = [hv.Scatter(df[["date", statistic]], label=statistic) for statistic in stats] line_curves = [hv.Curve(points) for points in point_curves] return (hv.Overlay(line_curves + point_curves)). opts(opts.Scatter(tools=["hover"], size=6)). opts(padding=0.05, height=375, legend_position="bottom", title=""). opts(options)
def abx(va, vb, vx, Jax, Jbx, Jab): peaklist = abx_wrapper(va, vb, vx, Jax, Jbx, Jab) vs = [va, vb, vx] vmax = max(vs) vmin = min(vs) # sufficient datapoints to mitigate inaccurate intensities and "jittering" datapoints = max(int(vmax - vmin) * 100, 800) xy = lineshape_from_peaklist(peaklist, points=datapoints) plot = hv.Curve(zip(*xy)) return plot.options(axiswise=True, invert_xaxis=True, xlabel='𝜈', height=300, responsive=True ).redim(y=hv.Dimension('intensity', range=(-0.4, 1.2)))
def test_holoviews_with_widgets(document, comm): hmap = hv.HoloMap({(i, chr(65 + i)): hv.Curve([i]) for i in range(3)}, kdims=['X', 'Y']) hv_pane = HoloViews(hmap) layout = hv_pane._get_root(document, comm) model = layout.children[0] assert len(hv_pane.widget_box.objects) == 2 assert hv_pane.widget_box.objects[0].name == 'X' assert hv_pane.widget_box.objects[1].name == 'Y' assert layout.ref['id'] in hv_pane._callbacks assert hv_pane._models[layout.ref['id']] is model hmap = hv.HoloMap({(i, chr(65 + i)): hv.Curve([i]) for i in range(3)}, kdims=['A', 'B']) hv_pane.object = hmap assert len(hv_pane.widget_box.objects) == 2 assert hv_pane.widget_box.objects[0].name == 'A' assert hv_pane.widget_box.objects[1].name == 'B'
def map_indicators(self): self.indicateur_dpt_plot() for idx in self.df_indic_dpt.index: an_indic = self.df_indic_dpt.loc[idx, 'Parametre'] color = self.determine_color(an_indic, self.df_indic_dpt.loc[idx, 'value'], 20) self.df_indic_dpt.at[idx, 'color'] = color dates_to_print = [ date.strftime(format='%d-%m-%Y') for date in [self.date_ini, self.date_final] ] tooltips = [ ("Taux d'incidence", '@tx_incid_2'), ('Facteur de reproduction', '@R_2'), ("Taux d'occupation des lits en réa", '@taux_occupation_sae_2'), ('Taux de positivité', '@tx_pos_2'), ('Région', '@libelle_reg'), ('Département', '@libelle_dep') ] hover = HoverTool(tooltips=tooltips) key_dimensions = ['Longitude', 'Latitude', 'Date', 'Parametre'] MapDataSet = hv.Dataset(self.df_indic_dpt, vdims=[ 'value', 'color', 'libelle_reg', 'libelle_dep', 'tx_incid_2', 'R_2', 'taux_occupation_sae_2', 'tx_pos_2' ], kdims=key_dimensions) MapDataSet = MapDataSet.to(geoviews.Polygons) MapRel = MapDataSet.opts( width=1000, height=560, tools=[hover], color='color', xaxis=None, yaxis=None, title= f"Cartes des indicateurs du {dates_to_print[0]} au {dates_to_print[1]}" ) MapRel_tot = MapRel * gvts.CartoLight sum_map = MapRel_tot text = hv.Curve((0, 0)).opts(xaxis=None, yaxis=None) * hv.Text( 0, 0, 'Source: Santé Publique France\nGraph: C.Houzard') MapOutput = (geoviews.Layout(sum_map + text)).cols(1) renderer = hv.renderer('bokeh') #renderer.save(MapOutput, os.path.normcase(f'map')) file_fct.save_fig(MapOutput, 'Map_France_Indic', self.date_final)
def test_holoviews_widgets_from_holomap(): hmap = hv.HoloMap({(i, chr(65+i)): hv.Curve([i]) for i in range(3)}, kdims=['X', 'Y']) widgets, _ = HoloViews.widgets_from_dimensions(hmap) assert isinstance(widgets[0], DiscreteSlider) assert widgets[0].name == 'X' assert widgets[0].options == OrderedDict([(str(i), i) for i in range(3)]) assert widgets[0].value == 0 assert isinstance(widgets[1], Select) assert widgets[1].name == 'Y' assert widgets[1].options == ['A', 'B', 'C'] assert widgets[1].value == 'A'
def test_holoviews_updates_widgets(document, comm): hmap = hv.HoloMap({(i, chr(65+i)): hv.Curve([i]) for i in range(3)}, kdims=['X', 'Y']) hv_pane = HoloViews(hmap) layout = hv_pane.get_root(document, comm) hv_pane.widgets = {'X': Select} assert isinstance(hv_pane.widget_box[0], Select) assert isinstance(layout.children[1].children[0].children[0], BkSelect) hv_pane.widgets = {'X': DiscreteSlider} assert isinstance(hv_pane.widget_box[0], DiscreteSlider) assert isinstance(layout.children[1].children[0].children[0], BkColumn) assert isinstance(layout.children[1].children[0].children[0].children[1], BkSlider)
def _showfp(track): data = fpos[fpos.track == track] beads = data.bead.unique() dtl = cls.detailed(dico.config, data, precision=precision) disp = getattr(Detailed(dico, dtl), 'display')(zero=False).display() crv = hv.Curve((list(rng), [3, 3])).options(linewidth=20, alpha=.5) ovr = hv.Overlay(list(disp) + [crv]) if scatter: scatt = (hv.Scatter(data, 'bead', 'z').options(jitter=.8) * hv.Scatter( (np.concatenate([beads] * 2), [rng[0]] * len(beads) + [rng[0]] * len(beads)))) return (ovr + scatt).cols(1) return ovr
def plot_peak_waveforms(t_reference, time_range, peaks, width=600, show_largest=10, time_dim=None): """Plot the sum waveforms of peaks :param width: Plot width in pixels :param show_largest: Maximum number of peaks to show :param time_dim: Holoviews time dimension; will create new one if not provided. """ if show_largest is not None and len(peaks) > show_largest: show_i = np.argsort(peaks['area'])[-show_largest::] peaks = peaks[show_i] curves = [] for p in peaks: # label = {1: 's1', 2: 's2'}.get( # p['type'], 'unknown') color = {1: 'b', 2: 'g'}.get(p['type'], 'k') # It's better to plot amplitude /time than per bin, since # sampling times are now variable y = p['data'][:p['length']] / p['dt'] t_edges = np.arange(p['length'] + 1, dtype=np.int64) t_edges = t_edges * p['dt'] + p['time'] t_edges = seconds_from(t_edges, t_reference) # Make a 'step' plot. Unlike matplotlib's steps-mid, # this also analyses the final edges correctly t_ = np.zeros(2 * len(y)) y_ = np.zeros(2 * len(y)) t_[0::2] = t_edges[:-1] t_[1::2] = t_edges[1:] y_[0::2] = y y_[1::2] = y if time_dim is None: time_dim = hv.Dimension('time', label='Time', unit='sec') curves.append( hv.Curve(dict(time=t_, amplitude=y_), kdims=time_dim, vdims=hv.Dimension('amplitude', label='Amplitude', unit='PE/ns'), group='PeakSumWaveform').opts(style=dict(color=color))) return hv.Overlay(items=curves).opts(plot=dict(width=width))
def _plot_reconstructed_position(self, index): """ Function which plots the nVETO event position according to its azimuthal angle. :param index: Which event to plot. """ x = (0, np.real(np.exp(self.event_df.loc[index, 'angle'] * 1j)) * 400) y = (0, np.imag(np.exp(self.event_df.loc[index, 'angle'] * 1j)) * 400) angle = hv.Curve((x, y)).opts(color='orange', line_dash='dashed', xlim=(-350, 350), ylim=(-350, 350)) return angle
def plot(self, *, x=None, scale_factor=1, show=False, show_label=False, label=None, color=None, size=10, xlabel='x', ylabel='y'): """ Draw plots for model fit results. Params: x: A custom x over which to draw fits scale_factor: Scale all y values by this factor show: Controls whether display show the plot or return the hv obj show_label: Controls whether to add labels to traces or not label: A string with which to label the fit color: The color of the fit line """ import easier as ezr p = self._params if x is None: x = p.x_train line_color = color if color else ezr.cc.b import holoviews as hv if 'bokeh' not in hv.Store.registry: hv.extension('bokeh') # label_val = 'Data' if show_label else '' label_val = '' c = hv.Scatter((p.x_train, scale_factor * p.y_train), xlabel, ylabel, label=label_val).options(color=ezr.cc.a, size=size, alpha=.5) label_val = label if label else 'Fit' label_val = label_val if show_label else '' c = c * hv.Curve((x, scale_factor * self.predict(x)), label=label_val).options(color=line_color) if show: display(c) # noqa else: return c
def hv_plot_param(df_tidy, species = 'H', param = 'N'): hv_fig = hv.Curve(df_tidy, kdims = ['time', species], vdims = [param], ).groupby(param ).overlay( ).opts(frame_height=250,frame_width=250 * 3 // 2) hv_fig.opts(opts.Curve(color=hv.Palette('Viridis'), width=600)) # Take out the Bokeh object p = hv.render(hv_fig) return p
def plot_traces(*traces: Trace, **kwargs): # pragma: no cover _cmaps = [["lightblue", "darkblue"], ["lightcoral", "red"], ["lime", "green"], ["orange", "darkorange"], ["plum", "deeppink"], ["peru", "chocolate"], ["cyan", "darkcyan"]] dss = [] for i, trace in enumerate(traces): line = hv.Curve((range(len(trace)), trace.samples), kdims="x", vdims="y", **kwargs) dss.append( datashade(line, normalization="log", cmap=_cmaps[i % len(_cmaps)])) return reduce(lambda x, y: x * y, dss)
def test_holoviews_pane_initialize_empty(document, comm): pane = HoloViews() # Create pane row = pane.get_root(document, comm=comm) assert isinstance(row, BkRow) assert len(row.children) == 1 model = row.children[0] assert isinstance(model, BkSpacer) pane.object = hv.Curve([1, 2, 3]) model = row.children[0] assert isinstance(model, Figure)
def plot_df(df_list): renderer = hv.renderer('bokeh').instance(mode='server') print(df_list) lines = { i: hv.Curve(df_list[i], kdims=['x'], vdims=['y']) for i in range(len(df_list)) } linespread = dynspread(datashade(hv.NdOverlay(lines, kdims='k')), aggregator=ds.count_cat('k')).opts( **{'plot': { 'height': 400, 'width': 1400 }}) return renderer.get_plot(linespread).state
def make_document(doc): seis_data = pd.DataFrame({ 'time': [1, 2, 3, 1, 2, 3, 1, 2, 3], 'counts': random.random(9), 'channel': ['LEH'] * 3 + ['CON'] * 3 + ['E1S'] * 3 }) plot_seis = lambda channel: hv.Curve(data=seis_data[seis_data.channel == channel]) seis_dmap = hv.DynamicMap(plot_seis, kdims=hv.Dimension('channel', values=['LEH', 'CON', 'E1S'])) layout = seis_dmap.layout('channel') doc.add_root(renderer.get_plot(layout).state) return doc
def plot_predictions_ahead(dataset='IMOSCurrentsVel', t_ahead_i=6, start=0, window_steps=1800): d = next(iter(ds_predss[dataset].values())).isel(t_ahead=t_ahead_i).isel(t_source=slice(start, start+window_steps)) p = hv.Scatter({ 'x': d.t_target, 'y': d.y_true }, label='true').opts(color='black', framewise=True) for model in results[dataset].keys(): ds_preds = ds_predss[dataset][model] d = ds_preds.isel(t_ahead=t_ahead_i).isel(t_source=slice(start, start+window_steps)) p *= hv.Curve({'x': d.t_target, 'y':d.y_pred}, label=model).relabel(label=f"{model}") p = p.opts(title=f"Dataset: {dataset}, {d.freq}*{t_ahead_i} ahead", height=250, legend_position='top', ylabel=d.targets) return p.opts(framewise=True)