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 hv_plot_std(d: xr.Dataset): """Plot predictions 2 standard deviations.""" xf = d.t_target yp = d.y_pred s = d.y_pred_std return hv.Spread((xf, yp, s * 2), label='2*std').opts(alpha=0.5, line_width=0)
def roi_curves(data): if not data or not any(len(d) for d in data.values()): return hv.NdOverlay({0: hv.Curve([], 'ens', 'coefs')}) curves = {} data = zip(data['x0'], data['x1'], data['y0'], data['y1']) for i, (x0, x1, y0, y1) in enumerate(data): selection = ds.select(x=(x0, x1), y=(y1, y0)) # swap y0 and y1 when inverted y-axis curves[i] = hv.Spread(selection.aggregate('ens', np.mean, np.std)) return hv.NdOverlay(curves)
def plot_predictions_ahead(dataset='IMOSCurrentsVel', t_ahead_i=6, start=0, window_steps=1800): ds_preds = ds_predss[dataset] l = hv.Layout() for model in ds_preds.keys(): d = ds_preds[model].isel(t_ahead=t_ahead_i).isel(t_source=slice(start, start+window_steps)) x = d.t_target y = d.y_pred s = d.y_pred_std p = hv.Scatter({ 'x': d.t_target, 'y': d.y_true }).opts(color='black') p *= hv.Curve({'x': x, 'y':y}) p *= hv.Spread((x, y, s * 2)).opts(alpha=0.5, line_width=0) l += p.opts(title=f"Dataset: {dataset}, model={model}, {d.freq}*{t_ahead_i} ahead", height=250, legend_position='top', ylabel=d.targets) return l.cols(1)
def sed_plot(data, selected=None): df_columns = list(config.main_df.columns) with open(config.settings["sed_file"], "r") as fp: bands = json.load(fp) new_data = [] for i in bands: mag = -99 if i in df_columns: if len(selected.data[i]) > 0: if bands[i]["wavelength"] == -99: continue else: mag = selected.data[i][0] elif i in list(config.settings.keys()): if config.settings[i] in df_columns: if len(selected.data[config.settings[i]]) > 0: if bands[i]["wavelength"] == -99: continue else: mag = selected.data[config.settings[i]][0] wavelength = bands[i]["wavelength"] if len(selected.data[f"{config.settings['id_col']}"]) > 0: if type(bands[i]["wavelength"]) == str: if wavelength in config.main_df.columns: wavelength = selected.data[wavelength][0] elif config.settings[wavelength] in config.main_df.columns: wavelength = selected.data[config.settings[wavelength]][0] else: continue fwhm = bands[i]["FWHM"] if len(selected.data[f"{config.settings['id_col']}"]) > 0: if type(bands[i]["FWHM"]) == str: if fwhm in config.main_df.columns: fwhm = selected.data[fwhm][0] elif config.settings[fwhm] in config.main_df.columns: fwhm = selected.data[config.settings[fwhm]][0] else: continue mag_err = bands[i]["error"] if len(selected.data[f"{config.settings['id_col']}"]) > 0: if type(bands[i]["error"]) == str: if mag_err in config.main_df.columns: mag_err = selected.data[mag_err][0] elif config.settings[mag_err] in config.main_df.columns: mag_err = selected.data[config.settings[mag_err]][0] else: continue if mag == -99: continue new_data.append([ wavelength, mag, fwhm, mag_err, ]) new_data = pd.DataFrame( new_data, columns=["wavelength (µm)", "magnitude", "FWHM", "error"]) new_data = new_data[new_data["magnitude"] != -99] if len(new_data) > 0: plot = create_plot( new_data, "wavelength (µm)", "magnitude", plot_type="line", colours=False, legend=False, show_selected=False, slow_render=True, ) points = hv.Scatter( new_data, kdims=["wavelength (µm)"], ).opts( fill_color="black", marker="circle", alpha=0.5, size=4, active_tools=["pan", "wheel_zoom"], ) error_data_x = [( new_data["wavelength (µm)"].values[i], new_data["magnitude"].values[i], new_data["error"].values[i], ) for i in range(len(new_data))] error_data_y = [( new_data["wavelength (µm)"].values[i], new_data["magnitude"].values[i], new_data["FWHM"].values[i] * 0.5, ) for i in range(len(new_data))] errors_x = hv.Spread(error_data_x, horizontal=True) errors_y = hv.ErrorBars(error_data_y, horizontal=True) plot = plot * points * errors_x * errors_y plot.opts(invert_yaxis=True, logx=True) else: plot = hv.Scatter( pd.DataFrame({ "wavelength (µm)": [], "magnitude": [] }), vdims=["wavelength (µm)", "magnitude"], kdims=["magnitude"], ) return plot
def plot_concatenated( dataframe, x=None, y=None, err=None, xlabel=None, ylabel=None, points=True, line=True, errors=True, width=800, height=300, journal=None, file_id_level=0, hdr_level=None, axis=1, mean_end="_mean", std_end="_std", cycle_end="cycle_index", legend_title="cell-type", marker_size=None, cmap="default_colors", spread=False, extension="bokeh", edges=False, **kwargs, ): """Create a holoviews plot of the concatenated summary. This function is still under development. Feel free to contribute. Args: dataframe: the concatenated summary x: colum-name for the x variable (not implemented yet) y: colum-name for the y variable (not implemented yet) err: colum-name for the std variable (not implemented yet) xlabel: label for x-axis ylabel: label for y-axis points (bool): plot points if True line (bool): plot line if True errors (bool): plot line if True width: width of plot height: height of plot journal: batch.journal object file_id_level: the level (multiindex-level) where the cell-names are. hdr_level: the level (multiindex-level) where the parameter names are. axis: what axis to use when looking in the data-frame (row-based or col-based). mean_end: used for searching for y-column names std_end: used for searching for e-column names cycle_end: used for searching for x-column name legend_title: title to put over the legends marker_size: size of the markers used cmap: color-map to use spread (bool): plot error-bands instead of error-bars if True extension (str): "matplotlib" or "bokeh". Note, this uses hv.extension) and will affect the state of your notebook edges (bool): show all axes **kwargs: key-word arguments sent to hv.NdOverlay Example: >>> my_mpl_plot = plot_concatenated( >>> cap_cycle_norm_fast_1000, journal=b.experiment.journal, >>> height=500, marker_size=5, >>> extension="matplotlib", >>> edges=True, >>> ) >>> my_bokeh_plot = plot_concatenated( >>> cap_cycle_norm_fast_1000, journal=b.experiment.journal, >>> height=500, marker_size=5, >>> edges=True, >>> ) Example: >>> # Simple conversion from bokeh to matplotlib >>> # NB! make sure you have only used matplotlib-bokeh convertable key-words (not marker_size) >>> hv.extension("matplotlib") >>> my_plot.opts(aspect="auto", fig_inches=(12,7), fig_size=90, legend_position="top_right", >>> legend_cols = 2, >>> show_frame=True) """ if not hv_available: print("This function uses holoviews. But could not import it." "So I am aborting...") return hv.extension(extension, logo=False) if hdr_level is None: hdr_level = 0 if file_id_level == 1 else 1 averaged = True columns = list(set(dataframe.columns.get_level_values(hdr_level))) hdr_x, lab_x = find_column(columns, label=xlabel, end=cycle_end) hdr_y, lab_y = find_column(columns, label=ylabel, end=mean_end) if hdr_y is None: averaged = False errors = False if hdr_x is not None: columns.remove(hdr_x) hdr_y = columns[0] if ylabel is None: lab_y = hdr_y.replace("_", " ") else: lab_y = ylabel if errors: hdr_e, _ = find_column(columns, end=std_end) grouped = dataframe.groupby(axis=axis, level=file_id_level) curve_dict = dict() if not averaged and journal is not None: journal_pages = journal.pages[[ hdr_journal["group"], hdr_journal["sub_group"] ]].copy() journal_pages["g"] = 0 journal_pages["sg"] = 0 markers = itertools.cycle(['s', 'o', '<', '*', '+', 'x']) colors = itertools.cycle(hv.Cycle(cmap).values) j = journal_pages.groupby(hdr_journal["group"]) for i, (jn, jg) in enumerate(j): journal_pages.loc[journal_pages["group"] == jn, "g"] = i journal_pages.loc[journal_pages["group"] == jn, "sg"] = list(range(len(jg))) markers = [next(markers) for _ in range(journal_pages["sg"].max() + 1)] colors = [next(colors) for _ in range(journal_pages["g"].max() + 1)] journal_pages = journal_pages[["g", "sg"]] for i, (name, group) in enumerate(grouped): group.columns = group.columns.droplevel(file_id_level) if hdr_x is None: group = group.reset_index() hdr_x = group.columns[0] if lab_x is None: lab_x = hdr_x.replace("_", " ") if not averaged and journal is not None: g = journal_pages.loc[name, "g"] sg = journal_pages.loc[name, "sg"] color = colors[g] marker = markers[sg] curve = hv.Curve(group, (hdr_x, lab_x), (hdr_y, lab_y)).opts(color=color) else: curve = hv.Curve(group, (hdr_x, lab_x), (hdr_y, lab_y)) if points: if not averaged and journal is not None: scatter = hv.Scatter(curve).opts(color=color, marker=marker) if edges and extension == "matplotlib": scatter = scatter.opts(edgecolor='k') if edges and extension == "bokeh": scatter = scatter.opts(line_color='k', line_width=1) if marker_size is not None and extension == "bokeh": scatter = scatter.opts(size=marker_size) else: scatter = hv.Scatter(curve) if marker_size is not None and extension == "bokeh": scatter = scatter.opts(size=marker_size) if points and line: curve *= scatter elif points: curve = scatter if errors: if spread: curve *= hv.Spread(group, hdr_x, [hdr_y, hdr_e]) else: curve *= hv.ErrorBars( group, hdr_x, [hdr_y, hdr_e ]) # should get the color from curve and set it here curve_dict[name] = curve if extension == "matplotlib": overlay_opts = { "aspect": "auto", "fig_inches": (width * 0.016, height * 0.012), "show_frame": True, } else: overlay_opts = { "width": width, "height": height, } final_plot = hv.NdOverlay(curve_dict, kdims=legend_title).opts(**overlay_opts, **kwargs) return final_plot
def plot(self, *args, **kwds): import holoviews freq, edges = self._hist.numpy return holoviews.Spread( (edges[numpy.repeat(numpy.arange(len(freq) + 1), 2)[1:-1]], ) + (freq[numpy.repeat(numpy.arange(len(freq)), 2)] / 2.0, ) * 2)