def test_builder_cross_backend_validation(self): Store.options(val=self.store_mpl, backend='matplotlib') Store.options(val=self.store_bokeh, backend='bokeh') Store.set_current_backend('bokeh') opts.Curve(line_dash='dotted') # Bokeh keyword opts.Curve(linewidth=10) # MPL keyword err = ( "In opts.Curve\(...\), keywords supplied are mixed across backends. " "Keyword\(s\) 'linewidth' are invalid for bokeh, " "'line_dash' are invalid for matplotlib") with self.assertRaisesRegexp(ValueError, err): opts.Curve(linewidth=10, line_dash='dotted') # Bokeh and MPL # Non-existent keyword across backends (bokeh active) err = ( "In opts.Curve\(...\), unexpected option 'foobar' for Curve type " "across all extensions. Similar options for current " "extension \('bokeh'\) are: \['toolbar'\].") with self.assertRaisesRegexp(ValueError, err): opts.Curve(foobar=3) # Non-existent keyword across backends (matplotlib active) Store.set_current_backend('matplotlib') err = ("In opts.Curve\(...\), unexpected option 'foobar' for Curve " "type across all extensions. No similar options found.") with self.assertRaisesRegexp(ValueError, err): opts.Curve(foobar=3)
def load_indices(Index): #scatter = hv.Scatter(multi_df[Index], kdims = ['JDK_RS_ratio', 'JDK_RS_momentum']) scatter = hv.Scatter(multi_df[Index], kdims = ['JDK_RS_momentum']) ##Colors explicit_mapping = {'Leading': 'green', 'Lagging': 'yellow', 'Weakening': 'red', 'Improving': 'blue'} ##Plot Joining all together scatter = scatter.opts(opts.Scatter(tools=['hover'], height = 500, width=500, size = 10, xlim = x_range, ylim = y_range, color = 'Quadrant', cmap=explicit_mapping, )) ##Line connecting the dots #curve = hv.Curve(multi_df[Index], kdims = ['JDK_RS_ratio', 'JDK_RS_momentum']) curve = hv.Curve(multi_df[Index], kdims = [ 'JDK_RS_momentum']) curve = curve.opts(opts.Curve(color = 'black', line_width = 1)) ##Vertical and Horizontal Lines vline = hv.VLine(100).opts(color = 'black', line_width = 1) hline = hv.HLine(100).opts(color = 'black', line_width = 1) #All Together full_scatter = scatter * vline * hline * curve full_scatter = full_scatter.opts(legend_cols= True) return full_scatter
def plot_curve( func: Callable[[np.ndarray], np.ndarray] = lambda x: 1 + (x - 2) ** 2, x_min: int = 0, x_max: int = 5, renderer: hv.renderer = hv.renderer("bokeh"), ): xs = np.linspace(x_min, x_max, 100) ys = [func(x) for x in xs] curve = hv.Curve((xs, ys), "x", hv.Dimension("f(x)"), label="Graph of f") curve.opts(opts.Curve(height=600, width=900, line_width=2.50, tools=["hover"])) # Create plot and save it as html file. We create the plot depending on the input # parameter renderer either via bokeh or via plotly. if renderer == "bokeh": renderer = hv.renderer("bokeh") # Using renderer save renderer.save(curve, "bokeh") elif renderer == "plotly": renderer = hv.renderer("plotly").get_plot(curve).state plotly.offline.plot(renderer, filename="plotly.html") elif renderer == "online": hv.save(curve, 'browse_me.html', )
def __post_init__(self): """ :return: """ data = self.spectral_cube.data self.ds = hv.Dataset((np.arange(data.shape[2]), np.arange( data.shape[1]), np.arange(data.shape[0]), data), [self.spectral_axis_name, 'x', 'y'], 'Cube') # maybe PolyEdit as well # polys = hv.Polygons([hv.Box(int(self.image_width / 2), int(self.image_height / 2), int(self.image_height / 2))]) # self.box_stream = streams.PolyEdit(source=polys) polys = hv.Polygons([]) self.box_stream = streams.BoxEdit(source=polys) hlines = hv.HoloMap({i: hv.VLine(i) for i in range(data.shape[2])}, 'wavelengths') dmap = hv.DynamicMap(self.roi_curves, streams=[self.box_stream]) im = self.ds.to(hv.Image, ['x', 'y'], dynamic=True) self.layout = (im * polys + dmap * hlines).opts( opts.Image(cmap=self.color_map, width=self.image_width, height=self.image_height), opts.Curve(width=650, height=450, framewise=True), opts.Polygons(fill_alpha=0.2, line_color='white'), opts.VLine(color='black'))
def covid_viewer_v2(ds): ''' covid viewer, for actives_vs_beds ''' opts.defaults( opts.Curve(tools=['hover'], width=800, height = 600, ylabel='') ) logtog = pn.widgets.Toggle(name='Log (Y-axis)', button_type='default', value=False) xlim=(np.datetime64('2020-03-01'), np.datetime64('2020-03-25')) hv_ds = hv.Dataset(ds, ['date', 'place'], ['active_per_beds']) avb = hv_ds.to(hv.Curve, 'date', 'active_per_beds').overlay('place').opts( legend_position='top_left', shared_axes=True, ylim=(0, 0.13), xlim=xlim, title='Severe Cases per Open Hospital Bed') avb_log = hv_ds.to(hv.Curve, 'date', 'active_per_beds').overlay('place').opts( legend_position='top_left', shared_axes=True, logy=True, ylim=(1e-6, 10), xlim=xlim, title='Severe Cases per Open Hospital Bed (Log Scale)') max_line = hv.HLine(1).opts( opts.HLine(color='red', line_width=6), opts.Points(color='#D3D3D3')) # layout = (avb_log) # layout.opts( # opts.Curve(width=400, height=300, framewise=True)) # pn_layout = pn.pane.HoloViews(layout) # return pn.Row(logtog, pn_layout) return avb
def open_plot_save(CONC_ALL, CSV_FILE, D1, D2, DIRNAME): hv.extension('bokeh') # %% path = '/Users/diego/flexpart_management/flexpart_management/releases/v03/data/cluster_series_v3.nc' # %% ds = xr.open_dataset(path) da = ds[CONC_ALL].loc[ {'z_column': 'LEV0', 'normalized': 1, 'releases': slice(D1, D2)}] _df = da.to_dataframe()[CONC_ALL] ly = hv.Curve(_df) ly(opts.Curve(height=300, width=900)) # %% df = da.to_dataframe()[[CONC_ALL]] df.index.name = 'UTC' df['BOT'] = df.index + pd.Timedelta(hours=-4) df['H'] = df['BOT'].dt.hour gr = df[['H', CONC_ALL]].groupby('H') # %% # diu = pd.DataFrame() diu = gr[[CONC_ALL]].mean() diu = diu.rename({CONC_ALL: 'c_mean'}, axis=1) diu['c_med'] = gr[CONC_ALL].median() diu['p_05'] = gr[CONC_ALL].quantile(.05) diu['p_95'] = gr[CONC_ALL].quantile(.95) diu.plot() # %% p = DIRNAME p = pjoin(p, CSV_FILE) diu.to_csv(p) return ly
def get_curve(x, y, label, color, height=200): return hv.Curve((x, y), kdims='total_distance_x', vdims='heights', label=label).opts( opts.Curve(color=color, tools=['hover'], height=height))
def covid_viewer(ds): ''' covid viewer, start with MRE view backbone? ''' opts.defaults( opts.Curve(tools=['hover'], width=600, ylabel='') ) logtog = pn.widgets.Toggle(name='Log (Y-axis)', button_type='default', value=False) xlim=(np.datetime64('2020-02-10'), np.datetime64('2020-03-25')) hv_ds = hv.Dataset(ds, ['date', 'country'], ['confirmed', 'dead', 'recovered']) confirmed = hv_ds.to(hv.Curve, 'date', 'confirmed').overlay('country').opts( legend_position='top_left', shared_axes=False, ylim=(-ds.confirmed.values.max()*0.1, ds.confirmed.values.max()*1.1), xlim=xlim, title='Confirmed') confirmed_log = hv_ds.to(hv.Curve, 'date', 'confirmed').overlay('country').opts( legend_position='top_left', shared_axes=False, logy=True, ylim=(1, ds.confirmed.values.max()*2), xlim=xlim, title='Confirmed (Log)') dead = hv_ds.to(hv.Curve, 'date', 'dead').overlay('country').opts( legend_position='top_left', shared_axes=False, ylim=(-ds.dead.values.max()*0.1, ds.dead.values.max()*1.1), xlim=xlim, title='Dead') dead_log = hv_ds.to(hv.Curve, 'date', 'dead').overlay('country').opts( legend_position='top_left', shared_axes=False, logy=True, ylim=(0.1, ds.dead.values.max()*2), xlim=xlim, title='Dead (Log)') recovered = hv_ds.to(hv.Curve, 'date', 'recovered').overlay('country').opts( legend_position='top_left', shared_axes=False, ylim=(-ds.recovered.values.max()*0.1, ds.recovered.values.max()*1.1), xlim=xlim, title='Recovered') recovered_log = hv_ds.to(hv.Curve, 'date', 'recovered').overlay('country').opts( legend_position='top_left', shared_axes=False, logy=True, ylim=(0.1, ds.recovered.values.max()*2), xlim=xlim, title='Recovered (Log)') layout = (confirmed + confirmed_log + dead + dead_log + recovered + recovered_log).cols(2) layout.opts( opts.Curve(width=400, height=250, framewise=True)) # pn_layout = pn.pane.HoloViews(layout) # return pn.Row(logtog, pn_layout) return layout
def plot_curve(): df = download_data(index.value) future_df = download_data_predicted(index.value) title = index.value + " Exchange Rate" # Create stock curve past_label = "Past " + title future_label = "Predicted Future " + title df['label'] = past_label future_df['label'] = future_label new_df = pd.concat([df, future_df], axis=0) curve = hv.Curve(df, 'Date', ('Close', 'label')) curve_pred = hv.Curve(future_df, 'Date', ('Close', 'Price')) # Labels and layout tgt = curve.relabel("Past " + title).opts( #width=width, height=600, show_grid=True, labelled=['y'], default_tools=[hover], hooks=[set_tools], title=title, responsive=True) tgt_pred = curve_pred.relabel("Future " + title).opts( #width=width, height=600, show_grid=True, labelled=['y'], default_tools=[hover], hooks=[set_tools], title=title, responsive=True) src = curve.opts(height=100, yaxis=None, default_tools=[], color='green', responsive=True) src_pred = curve_pred.opts(height=100, yaxis=None, default_tools=[], color='green', responsive=True) circle = hv.Scatter(df, 'Date', ('Close', 'Price')).opts(color='green') circle_pred = hv.Scatter(future_df, 'Date', ('Close', 'Price')).opts(color='blue') RangeToolLink(src, tgt) # Merge rangetool layout = ((tgt * tgt_pred * circle * circle_pred) + (src * src_pred)).cols(1) layout.opts(opts.Layout(shared_axes=False, merge_tools=False), opts.Curve(toolbar=None), opts.Scatter(size=3)) print("kepanggil nih viz") print(df["Close"][0]) print(index.value) return layout
def plot_model( runs, x="step", y="mean_episode_return", model="model", color="#ff0000", subsample=1000, ): hmap = {} # Interpolate the data on an even grid. With min(np.amin(...)) # this starts where the first data starts and ends where the last # data ends. Interpolation of missing data will create artefacts. # An alternative would be to throw away data and the end and do # max(np.amin(...)) etc. xmin = min(np.amin(config["df"][x].values) for _, config in runs.items()) xmax = max(np.amax(config["df"][x].values) for _, config in runs.items()) xnum = max(len(config["df"][x]) for _, config in runs.items()) grid = np.linspace(xmin, xmax, xnum) for run, config in runs.items(): df = config["df"] yvalues = np.interp(grid, df[x].values, df[y].values) df = pd.DataFrame({x: grid, y: yvalues}) p = plot_run(df, x, y, model, color, subsample) p.opts(opts.Curve(f"Curve", color=color, alpha=0.2)) hmap[run] = p hmap = hv.HoloMap(hmap) p_runs = hmap.overlay().relabel("Runs") hmap_mean = hv.HoloMap(hmap) p_mean = hmap_mean.collapse(function=np.mean) p_mean = hv.Curve(p_mean).relabel(model) p_mean.opts(opts.Curve("Curve", color=color)) p = p_runs * p_mean # p = p_runs * p_mean * p_std # Plot options p.opts(opts.NdOverlay("NdOverlay.Runs", show_legend=False)) return p
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 run_experiment(n_iters, model_gen, dl, loss_fn, optim_gen, lr_scheduler, device, print_every=None, seed=1, to_show=True): """ Args: - model_generator (Callable): returns a new model - Must accept two arguments, `device` and `seed` - It returns a model object put in `device` with any weight initialization random-seeded at `seed - optim_gen (Callable): Must take in 'model.parameters()' - eg: functools.partial(torch.optim.Adam, lr=1e-3) - seed (None or int): random seed for clean model (model weights) - None if randomness in initializing model weights is desired - any other int to set the seed - lr_scheduler: TriangleLR or ConstLR """ model = model_gen(device=device, seed=seed) optimizer = optim_gen(model.parameters()) lrs, losses, avg_losses = lr_range_test(model, dl, loss_fn, optimizer, lr_scheduler, device, n_iters=n_iters, print_every=print_every) # Visualization if to_show: hv_lr = show_lr_generator(lr_scheduler, n_iters) layout = (hv_lr.opts(color='red', ylim=(lr_scheduler.min_lr, lr_scheduler.max_lr)) + hv.Curve(losses, label='loss').opts(color='blue')) display( layout.opts( opts.Overlay(shared_axes=False), opts.Curve(padding=0.1, width=800, axiswise=True, shared_axes=False)).cols(1)) return model, lrs, losses, avg_losses
def get_width_curve(x, width, label, color, height=150): return (hv.Curve( (x, width / 2), kdims='total_distance_x', vdims='widths', label=label, ) * hv.Curve( (x, -width / 2), kdims='total_distance_x', vdims='widths', label=label, )).opts(opts.Curve(color=color, tools=['hover'], height=height))
def __init__(self, adh_mod, **params): super(InterpolateMesh, self).__init__(adh_mod=adh_mod, **params) # set defaults for initialized example self.display_range.param.color_range.bounds = (10, 90) self.display_range.color_range = (10, 90) self.cmap_opts.colormap = cc.rainbow self.scatter_projection.set_crs(ccrs.GOOGLE_MERCATOR) self.adh_mod.wmts.source = gv.tile_sources.EsriImagery # print(self.projection.param.UTM_zone_hemi.constant, self.projection.crs_label) self.opts = (opts.Curve(height=self.map_height, width=self.map_width, xaxis=None, line_width=1.50, color='red', tools=['hover']), opts.Path(height=self.map_height, width=self.map_width, line_width=3, color='black'), opts.Image(height=self.map_height, width=self.map_width, cmap=self.cmap_opts.param.colormap, clim=self.display_range.param.color_range, colorbar=True, clipping_colors={ 'NaN': 'transparent', 'min': 'transparent' }, axiswise=True), opts.RGB(height=self.map_height, width=self.map_width), opts.Points(height=self.map_height, width=self.map_width, color_index='z', cmap=self.cmap_opts.param.colormap, clim=self.display_range.param.color_range, size=10, tools=['hover'], padding=(0.1, 0.1), colorbar=True), opts.TriMesh(height=self.map_height, width=self.map_width, color_index='z', cmap=self.cmap_opts.param.colormap, clim=self.display_range.param.color_range, tools=['hover'], padding=(0.1, 0.1), colorbar=True), opts.VLine(color='black'))
def plot_run( df, x="step", y="mean_episode_return", model="model", color="#ff0000", subsample=1000, ): df = df[[x, y]].dropna() df[y] = df[y].rolling(100, min_periods=0).mean() grid = np.linspace(0, df[x].max(), subsample) yvalues = np.interp(grid, df[x].values, df[y].values) df = pd.DataFrame({x: grid, y: yvalues}) p = hv.Curve(hv.Dataset(df, kdims=[x], vdims=[y])) p.opts(opts.Curve("Curve", color=color)) return p
def __init__(self, path, ping_file_path, speed_test_file_path): self.path = path self.ping_file_path = ping_file_path self.speed_test_file_name = speed_test_file_path # Define default layout of graphs hv.extension('bokeh') opts.defaults( opts.Bars(xrotation=45, tools=['hover']), opts.BoxWhisker(width=700, xrotation=30, box_fill_color=Palette('Category20')), opts.Curve(width=700, tools=['hover']), opts.GridSpace(shared_yaxis=True), opts.Scatter(width=700, height=500, color=Palette('Category20'), size=dim('growth')+5, tools=['hover'],alpha=0.5, cmap='Set1'), opts.NdOverlay(legend_position='left')) if os.path.isdir(os.path.join(self.path, "webpage","figures")) is False: os.mkdir(os.path.join(self.path, "webpage","figures")) print("Path 'figures' created successfully") else: print("Path 'figures' initialized") # Load basic configurations config = configparser.ConfigParser() try: config.read('./modules/config_a.ini') # Get values from configuration file self.upper_acceptable_ping_bound = float(config['DEFAULT']['upper_acceptable_ping_bound']) self.upper_ping_issue_bound = float(config['DEFAULT']['upper_ping_issue_bound']) self.acceptable_network_speed = float(config['DEFAULT']['acceptable_network_speed']) except: # In case no config-file is found or another reading error occured print("Configuration file not found/readable.") print("Creating a new configuration file.") # Creating new file with standard values config['DEFAULT'] = {'upper_acceptable_ping_bound': '10', 'upper_ping_issue_bound': '99999', 'acceptable_network_speed': '16'} with open('config_a.ini', 'w') as configfile: config.write(configfile) print("New configuration file was created. Running on default parameters, please restart for changes.") #set default values to continue with program self.upper_acceptable_ping_bound = float(config['DEFAULT']['upper_acceptable_ping_bound']) self.upper_ping_issue_bound = float(config['DEFAULT']['upper_ping_issue_bound']) self.acceptable_network_speed = float(config['DEFAULT']['acceptable_network_speed'])
def plot_confirmed_with_recovered(country): confirmed_country = confirmed.loc[:, (slice(None), country)].sum(axis=1) recovered_country = recovered.loc[:, (slice(None), country)].sum(axis=1) return ((hv.Curve([(i, confirmed_country.loc[i]) for i in confirmed_country.index], label='Confirmed') * hv.Curve([(i, recovered_country.loc[i]) for i in recovered_country.index], label='Recovered')).redim( x='Date', y='Number of Cases').opts( legend_position='top_left').opts( opts.Curve( height=400, width=700, logy=True, ylim=(1, 1e6), title='Confirmed and Recovered Cases', show_frame=False, tools=[nhover])))
def boxes_exploration_interactive(data_path): '''Returns an interactive plot with the agonia boxes with a confidence value above a Score selected by the user with a slider. The user can select a box by clicking on it and mean box Fluorescence and the Caiman DF/F of such box will be ploted.''' data_name, median_projection, fnames, fname_new, results_caiman_path, boxes_path = get_files_names( data_path) cnm = cnmf.load_CNMF(results_caiman_path) img = hv.Image(median_projection, bounds=(0, 0, median_projection.shape[1], median_projection.shape[0])).options(cmap='gray') with open(boxes_path, 'rb') as f: boxes = pickle.load(f) f.close() centers = np.empty((cnm.estimates.A.shape[1], 2)) for i, factor in enumerate(cnm.estimates.A.T): centers[i] = center_of_mass(factor.toarray().reshape( cnm.estimates.dims, order='F')) #scatter = hv.Scatter((centers[:,1], median_projection.shape[0] - centers[:,0])) kdims = [hv.Dimension('Score', values=np.arange(0.05, 1, 0.05))] tap = streams.SingleTap(transient=True, source=img) Experiment = Stream.define('Experiment', data_path=data_path) Centers = Stream.define('Centers', centers=centers) CaImAn_detection = Stream.define('CaImAn_detection', cnm=cnm) dmap = hv.DynamicMap(plot_AGonia_boxes_interactive, kdims=kdims, streams=[Experiment(), tap]) dmap1 = hv.DynamicMap(plot_boxes_traces, kdims=kdims, streams=[Experiment(), tap]) dmap2 = hv.DynamicMap( plot_seeded_traces, kdims=kdims, streams=[CaImAn_detection(), Experiment(), tap, Centers()]) return ((img * dmap).opts(width=500, height=500) + dmap1.opts(width=500, height=250) + dmap2.opts(width=500, height=250)).opts( opts.Curve(framewise=True)).cols(1)
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 config_layout(PlotItem, **kwargs): """Configs the layout of the output""" for key, value in kwargs.items(): try: getattr(PlotItem, key)(value) except AttributeError as err: log.warning( "Option '{}' for plot not possible with error: {}".format( key, err)) try: TOOLTIPS = [("File", "@Name"), ("index", "$index"), ("(x,y)", "($x, $y)")] hover = HoverTool(tooltips=TOOLTIPS) PlotItem.opts( opts.Curve(tools=[hover], toolbar="disable"), opts.Scatter(tools=[hover], toolbar="disable"), opts.Histogram(tools=[hover], toolbar="disable"), opts.Points(tools=[hover], toolbar="disable"), opts.BoxWhisker(tools=[hover], toolbar="disable"), opts.Bars(tools=[ HoverTool(tooltips=[('Value of ID:', ' $x'), ('Value:', '$y')]) ], toolbar="disable"), opts.Violin(tools=[hover], toolbar="disable")) except AttributeError as err: log.error( "Nonetype object encountered while configuring final plots layout. This should not happen! Error: {}" .format(err)) except ValueError as err: if "unexpected option 'tools'" in str(err).lower( ) or "unexpected option 'toolbar'" in str(err).lower(): pass else: raise return PlotItem
imgs_pv = ds_sel['p_values'].hvplot.contour(**hvc_opts) temp = xr.apply_ufunc(combine_pvalues_ufunc, ds_sel['p_values'], input_core_dims=[['ens']], \ output_core_dims = [[]], vectorize = True, dask = 'allowed') hvc_opts = dict(width=300, dynamic=True, \ x = 'x', y = 'y', colorbar = False, \ logy = True, cmap = ['black', 'gray'], \ levels=[0.01,0.05]) imgs_pv2 = temp.hvplot.contour(**hvc_opts) # - # ## Layout # By combining every panel in a layout, we get clear interactive dashboard hl = hv.HLine(0).opts(color='gray', line_dash='dotted') dmap = dmap.opts(xticks=[(i, ens_name) for i, ens_name in enumerate(ens_ls)]) first_panel = im * imgs_pv * polys second_panel = (dmap * hl * hlines).relabel('ROI drawer') hv_div = hv.Div( f"""<h1>{invar} response to {sel_reg} for {month_names[sel_month-1]}</h1>""" ) second_row = ( (im2 * imgs_pv2).relabel('Model average (p-values combined using Z-score)') + hv_div) layout = ((first_panel + second_panel).opts( opts.Curve(width=400, framewise=True), opts.Polygons(fill_alpha=0.2, line_color='green', fill_color='green'), opts.VLine(color='black')) + second_row).cols(2) layout
right_viterbi = 100 * np.count_nonzero( gen_states == model.states(series)) / series.size print(f"Right states with Viterbi: {right_states:.2f}%\n" f"Right states with gamma argmax: {right_viterbi:.2f}%") mt = hv.Scatter((model.matrix.flat, np.genfromtxt("gen_param/A.txt").flat)) avg = hv.Scatter( (model.distr.mean, np.log(np.genfromtxt("gen_param/b.txt")[:, 0]))) std = hv.Scatter((model.distr.std, np.genfromtxt("gen_param/b.txt")[:, 1])) pi = hv.Scatter((model.init_distr, np.genfromtxt("gen_param/pi.txt"))) ze = hv.Scatter( (model.zero_distr, np.genfromtxt("gen_param/zero_distr.txt")[:, 0])) param_max = ( np.array([model.matrix.max(), model.init_distr.max(), model.zero_distr.max()]).max() + 0.02 ) # yapf: disable fit = hv.Curve((*[[0, param_max]] * 2, )) layout = (mt * std * pi * ze * fit).redim(x="Inferred", y="Simulated") layout.opts(opts.Scatter(size=7, tools=["hover"]), opts.Curve(color="green")) # %% cProfile.run("model.baum_welch(series, 5)", sort="cumtime") # %% lp = line_profiler.LineProfiler(model.baum_welch) lp.run("model.baum_welch(series, 3)").print_stats() # %%
# %% from fractions import Fraction from importlib import reload import holoviews as hv import numpy as np from holoviews import opts import hw2 hv.extension('bokeh', 'matplotlib') opts.defaults(opts.Curve(width=650)) conf = np.loadtxt('60.txt') conf100, conf200 = np.loadtxt('100.txt'), np.loadtxt('200.txt') # %% deltas = [Fraction(1, 6), Fraction(1, 3), 1, 2, 3] E, E_corr, err, acc_rate = hw2.point_b(conf, np.array(deltas, np.float)) # yapf: disable print('delta values (in units of d):', [str(d) for d in deltas], 'means and errors:', E.mean(1), err, 'acceptance ratios:', acc_rate, sep='\n') E_plot = hv.NdOverlay({delta: hv.Curve(E[i, ::40]) for i, delta in enumerate(deltas)}).redim(x='t', y='E') acc_plot = hv.Curve((np.array(deltas, np.float), acc_rate)) E_corr_plot = hv.NdOverlay({delta: hv.Curve(E_corr[i, :300]) for i, delta in enumerate(deltas)}) err_plot = hv.Curve((list(map(float, deltas)), err)) # yapf: enable
import pandas as pd import numpy as np import holoviews as hv from holoviews import opts, dim hv.extension('bokeh') macro_df = pd.read_csv('http://assets.holoviews.org/macro.csv', '\t') key_dimensions = [('year', 'Year'), ('country', 'Country')] value_dimensions = [('unem', 'Unemployment'), ('capmob', 'Capital Mobility'), ('gdp', 'GDP Growth'), ('trade', 'Trade')] macro = hv.Table(macro_df, key_dimensions, value_dimensions) gdp_curves = macro.to.curve('Year', 'GDP Growth') gdp_unem_scatter = macro.to.scatter('Year', ['GDP Growth', 'Unemployment']) annotations = hv.Arrow(1973, 8, 'Oil Crisis', 'v') * hv.Arrow(1975, 6, 'Stagflation', 'v') *\ hv.Arrow(1979, 8, 'Energy Crisis', 'v') * hv.Arrow(1981.9, 5, 'Early Eighties\n Recession', 'v') composition=(gdp_curves * gdp_unem_scatter* annotations) composition.opts( opts.Curve(color='k'), opts.Scatter(cmap='Blues', color='Unemployment', line_color='k', size=dim('Unemployment')*1.5), opts.Text(text_font_size='13px'), opts.Overlay(height=400, show_frame=False, width=700)) hv.save(composition, 'holomap.html')
def _plot(self) -> pn.panel: """ Represent the initial and final state of the lattice. Graphical representation of the lattice. The image shows the initial and final state of the grid (in order to compare how the network has evolved), as well as the number of speakers as a function of time. self.track = True is needed to call this method. """ grid_flat = self.memory.reshape(self.memory.shape[0], -1) speakers_a = (grid_flat == 1).sum(1) speakers_b = (grid_flat == -1).sum(1) speakers_ab = (grid_flat == 0).sum(1) total = speakers_a + speakers_b + speakers_ab == (self.width * self.height) * np.ones( len(self.memory), ) if not np.all(total): raise ValueError( "The total number of speakers does not correspond to the lattice size!", ) # Plots colors = ["navy", "white", "red"] data_start = self.memory[0] data_end = self.grid.data grid_start = { "xdata": np.arange(1, data_start.shape[1] + 1), "ydata": np.arange(1, data_start.shape[0] + 1), "zdata": data_start, } grid_end = { "xdata": np.arange(1, data_end.shape[1] + 1), "ydata": np.arange(1, data_end.shape[0] + 1), "zdata": data_end, } plot_start = hv.Image( grid_start, kdims=["xdata", "ydata"], vdims=hv.Dimension("zdata", range=(-1, 1)), label="Initial grid", ) plot_end = hv.Image( grid_end, kdims=["xdata", "ydata"], vdims=hv.Dimension("zdata", range=(-1, 1)), label="Final grid", ) plot_curvea = hv.Curve(speakers_a, label="Speakers A").opts(color="red") plot_curveb = hv.Curve(speakers_b, label="Speakers B").opts(color="navy") plot_curveab = hv.Curve(speakers_ab, label="Speakers AB").opts(color="gray") # Compositions grids = plot_start + plot_end lines = plot_curvea * plot_curveb * plot_curveab layout = grids + lines # Options layout.opts( opts.Image( invert_yaxis=True, cmap=colors, colorbar=True, width=350, labelled=[], colorbar_opts={ "title": "Languages", "title_text_align": "left", "major_label_overrides": {-1: "B", 0: "AB", 1: "A"}, "ticker": FixedTicker(ticks=[-1, 0, 1]), "major_label_text_align": "right", }, ), opts.Curve(xlabel="Iterations", ylabel="Number of speakers", width=700), ) return display(pn.Column(pn.Row(plot_start, plot_end), lines))
def curve_defaults(cls, **kwargs): """ Set defaults for holoviews Curve class. Use kwargs to overwrite elvis defaults and set user-specific defaults """ return opts.defaults(opts.Curve(**_dict_merge(kwargs, cls.DEFAULT_CURVE_OPTS)))
class BasemapModule: """ NAME ---- BasemapModule DESCRIPTION ----------- Blueprint for Basemap objects. Plots seismic survey elements such as polygon, wells, lines and the intersection of these lines while providing interactive tools to improve the experience between data and users. These plots are not images but objects that can be modified by the user and exported as images. ATTRIBUTES ---------- basemap_dataframe : (Pandas)DataFrame Matrix compounded by the coordinates and lines of the seismic survey's corners. Empty by default. wells_dataframe : (Pandas)DataFrame Matrix compounded by wells related information. Empty by default. polygon : Holviews element [Curve] Plot of the seismic survey polygon. wells : Holviews element [Scatter] Plot of the wells inside the seismic survey. seismic_lines : Holviews element [Overlay] Plot of the seismic lines (Inline referred as iline and Crossline referred as xline) and its intersection. basemap : Holviews element [Overlay] Combination of the plots: polygon, wells and seismic_lines. METHODS ------- polygon_plot(**kwargs) Constructs the polygon attribute. wells_plot(**kwargs) Constructs the wells attribute. seismic_line_plot(**kwargS) Constructs the seismic_lines attribute. get_basemap(**kwargs) Constructs the Basemap attribute and provides interactive methods to inspect the plotted data. LIBRARIES --------- Holoviews: BSD open source Python library designed to simplify the visualization of data. More information available at: http://holoviews.org/ Numpy: BSD licensed package for scientific computing with Python. More information available at: https://numpy.org/ Pandas: BSD 3 licensed open source data analysis and manipulation tool, built on top of the Python programming language. More information available at: https://pandas.pydata.org/ Panel: BSD open source Python library that allows to create custom interactive dashboards by connecting user defined widgets to plots. More information available at: https://panel.holoviz.org/index.html ON PROGRESS ----------- Include a GIS element into plots. """ # Holoviews default config plot_tools = ['pan', 'wheel_zoom', 'reset'] font_s = {"title": 16, "labels": 14, "xticks": 10, "yticks": 10} opts.defaults(opts.Curve(tools=plot_tools, default_tools=[], xformatter='%.0f', yformatter='%.0f', fontsize=font_s, height=400, width=400, padding=0.1, toolbar='right'), opts.Scatter(tools=plot_tools, default_tools=[], xformatter='%.0f', yformatter='%.0f', fontsize=font_s, height=400, width=400, padding=0.1, toolbar='right', framewise=True, show_grid=True), opts.GridSpace(fontsize=font_s, shared_yaxis=True, plot_size=(120, 380), toolbar="left"), opts.Overlay(xformatter='%.0f', yformatter='%.0f', fontsize=font_s, toolbar="left", show_grid=True), opts.Points(tools=['box_select', 'lasso_select'], default_tools=[], active_tools=["box_select"], size=3, width=500, height=400, padding=0.01, fontsize={ 'title': 16, 'ylabel': 14, 'xlabel': 14, 'ticks': 10 }, framewise=True, show_grid=True), toolbar="left") def __init__(self, basemap_dataframe, wells_dataframe): """ DESCRIPTION ----------- Instantiates BasemapModule's attributes. For more information, please refer to BasemapModule's docstring. """ self.basemap_dataframe = basemap_dataframe self.wells_dataframe = wells_dataframe self.iline_step = 1 self.xline_step = 1 self.hover_format = [("Utmx", "$x{(0.00)}"), ("Utmy", "$y{(0.00)}")] self.hover_attributes = { "show_arrow": True, "point_policy": "follow_mouse", "anchor": "bottom_right", "attachment": "above", "line_policy": "none" } def polygon_plot(self): """ NAME ---- polygon_plot DESCRIPTION ----------- Constructs the polygon attribute. Plots the boundaries of the seismic survey using Holoviews and bokeh as backend. ARGUMENTS --------- BasemapModule.basemap_dataframe : (Pandas)DataFrame Matrix compounded by the coordinates and lines of the seismic survey's corners. RETURN ------ BasemapModule.polygon : Holviews element [Curve] instance attribute Plot of the seismic survey polygon. """ #Plotting the boundaries of the Seismic Survey. Holoviews Curve element BasemapModule.polygon = hv.Curve(self.basemap_dataframe, ["utmx", "utmy"], label="Polygon") BasemapModule.polygon.opts(line_width=2, color="black") return BasemapModule.polygon def wells_plot(self): """ NAME ---- wells_plot DESCRIPTION ----------- Constructs the wells attribute Plots the wells inside the Seismic Survey's polygon using Holoviews and bokeh as backend. ARGUMENTS --------- BasemapModule.wells_dataframe : (Pandas)DataFrame Matrix compounded by wells related information. RETURN ------ BasemapModule.wells : Holviews element [Scatter] instance attribute Plot of the wells inside the seismic survey. """ # Declaring the Hover tools (each line will use one) wells_hover = HoverTool(tooltips=[("Well", "@name")] + self.hover_format + [("Depth", "@depth{(0)}")]) # Plotting Wells. Holoviews Scatter element BasemapModule.wells = hv.Scatter( self.wells_dataframe, ["utmx", "utmy"], ["name", "cdp_iline", "cdp_xline", "depth"], label="Wells") BasemapModule.wells.opts(line_width=1, color="green", size=10, marker="^") return (BasemapModule.wells) def seismic_line_plot(self, iline_number, xline_number): """ NAME ---- seismic_line_plot DESCRIPTION ----------- Constructs the seismic_lines attribute. Plots seismic lines (given a set of inline and crossline numbers) and the intersection of these using Holoviews and bokeh as backend. ARGUMENTS --------- iline_number : int Number of the chosen inline. The value can be given manually or by Panel's slider widget. xline_number : int Number of the chosen crossline. The value can be given manually or by Panel's slider widget. RETURN ------ BasemapModule.seismic_lines : Holviews element [Overlay] instance attribute Plot of the seismic lines and its intersection. FUNCTIONS --------- seismic_lines_dataframe(**kwargs) Builds a DataFrame for the first line either along inline or crossline direction. seismic_intersection(**kwargs) Computes the coordinates and tracf of the intersection between two seismic lines. REFERENCES ---------- bokeh.pydata.org. Change the attributes of the hover tool. Online document: https://bokeh.pydata.org/en/latest/docs/reference/models/tools.html#bokeh.models.tools.HoverTool.point_policy """ def seismic_lines_dataframe(line_direction, perpendicular_direction): """ NAME ---- seismic_lines_dataframe DESCRIPTION ----------- Builds a DataFrame for the first line either along inline or crossline direction. The coordinates represent the lower end of a seismic line; therefore, these shall be used to draft seismic lines after the computation of the higher end. If the users want to plot a line along inline direction, the code will compute the coordinates of the traces within the first crossline and vice versa. ARGUMENTS --------- basemap_dataframe : (Pandas)DataFrame Matrix compounded by the coordinates and lines of the seismic survey's corners. line_direction : str Seismic line direction. perpendicular_direction : str Direction in which the points are going to be calculated. Is perpendicular to line_direction argument. RETURN ------ dlines : (Pandas)DataFrame Contains the trace coordinates within the first seismic line. """ # Less stresful to read the code df, ld, p_d = self.basemap_dataframe, line_direction, perpendicular_direction #Measure the amount of perpendicular lines within line_direction dif_lines = abs( int(df[f"{perpendicular_direction}"].min() - df[f"{perpendicular_direction}"].max())) + 1 #Computing the coordinates of each utmx = np.linspace( float(df[(df[ld] == df[ld].min()) & (df[p_d] == df[p_d].min())]["utmx"].unique()), float(df[(df[ld] == df[ld].min()) & (df[p_d] == df[p_d].max())]["utmx"].unique()), num=dif_lines, endpoint=True) utmy = np.linspace( float(df[(df[ld] == df[ld].min()) & (df[p_d] == df[p_d].min())]["utmy"].unique()), float(df[(df[ld] == df[ld].min()) & (df[p_d] == df[p_d].max())]["utmy"].unique()), num=dif_lines, endpoint=True) #Array of perpendiculars array = np.arange(df[f"{p_d}"].min(), df[f"{p_d}"].max() + 1, 1) # Making dataframes to ease further calculations dlines = pd.DataFrame({ ld: df[f"{ld}"].min(), p_d: array, "utmx": utmx, "utmy": utmy }) return (dlines) def seismic_intersection(iline_df, xline_df, iline_number, xline_number): """ NAME ---- seismic_intersection DESCRIPTION ----------- Computes the coordinates and tracf of the intersection between two seismic lines. The computation of the intersection uses vector differences. ARGUMENTS --------- iline_df : (Pandas)DataFrame Coordinates of the traces within the first crossline. xline_df : (Pandas)DataFrame Coordinates of the traces within the first inline. iline_number : int Number of the chosen inline. xline_number : int Number of the chosen crossline. RETURN ------ list List of tracf and coordinates of the intersection. """ # Amount of CDP within crosslines dif_lines = abs(self.basemap_dataframe["xline"].max() - self.basemap_dataframe["xline"].min()) + 1 # tracf tracf = (iline_number - self.basemap_dataframe["iline"].min()) * dif_lines + ( xline_number - self.basemap_dataframe["xline"].min()) + 1 # vector diferences. Formula utm = b - a + c tutmx = float( xline_df[xline_df["xline"] == xline_number] ["utmx"]) - xline_df["utmx"].iloc[0] + float( iline_df[iline_df["iline"] == iline_number]["utmx"]) tutmy = float( xline_df[xline_df["xline"] == xline_number] ["utmy"]) - xline_df["utmy"].iloc[0] + float( iline_df[iline_df["iline"] == iline_number]["utmy"]) return [int(tracf), tutmx, tutmy] df = self.basemap_dataframe # Assigning a variable for each dataframe in seismic_lines_dataframe ilines, xlines = seismic_lines_dataframe( df.keys()[1], df.keys()[0]), seismic_lines_dataframe(df.keys()[0], df.keys()[1]) # Extracting the intersection coordinates intersection = seismic_intersection(ilines, xlines, iline_number, xline_number) # Computing the second point to plot the seismic lines (By using vector differences) ## This can be refactored iutmx = float(xlines["utmx"].iloc[-1] - xlines["utmx"].iloc[0] + ilines[ilines["iline"] == iline_number]["utmx"]) iutmy = float(xlines["utmy"].iloc[-1] - xlines["utmy"].iloc[0] + ilines[ilines["iline"] == iline_number]["utmy"]) xutmx = float(ilines["utmx"].iloc[-1] - ilines["utmx"].iloc[0] + xlines[xlines["xline"] == xline_number]["utmx"]) xutmy = float(ilines["utmy"].iloc[-1] - ilines["utmy"].iloc[0] + xlines[xlines["xline"] == xline_number]["utmy"]) # hovers for lines and interception iline_hover = HoverTool(tooltips=[("Inline", f"{iline_number}")] + self.hover_format) xline_hover = HoverTool(tooltips=[("Crossline", f"{xline_number}")] + self.hover_format) int_hover = HoverTool( tooltips=[("Intersection", f"({iline_number}/{xline_number})")] + self.hover_format) #Updating hover attributes for item in [iline_hover, xline_hover, int_hover]: item._property_values.update(self.hover_attributes) # Plotting the Inline. Holoviews Curve element iline = hv.Curve( [(float(ilines[ilines["iline"] == iline_number]["utmx"]), float(ilines[ilines["iline"] == iline_number]["utmy"])), (iutmx, iutmy)], label="I-Line") # Plotting the Crossline. Holoviews Curve element xline = hv.Curve( [(float(xlines[xlines["xline"] == xline_number]["utmx"]), float(xlines[xlines["xline"] == xline_number]["utmy"])), (xutmx, xutmy)], label="C-Line") # Plot the intersection. Holovies Scatter element. intersection = hv.Scatter((intersection[1], intersection[2]), label="Intersection") # Adding the hover tool in to the plots iline.opts(line_width=2, color="red", tools=self.plot_tools + [iline_hover]) xline.opts(line_width=2, color="blue", tools=self.plot_tools + [xline_hover]) intersection.opts(size=7, line_color="black", line_width=2, color="yellow", tools=self.plot_tools + [int_hover]) # Making the overlay of the seismic plot to deploy BasemapModule.seismic_lines = iline * xline * intersection return BasemapModule.seismic_lines def get_basemap(self): """ NAME ---- get_basemap DESCRIPTION ----------- Constructs the basemap attribute and provides interactive methods to inspect the plotted data. Merges polygon, wells and seismic_lines attributes into one object using Holoviews and bokeh as backend. It also includes Panel's widgets and methods to add elements that ease data management. ARGUMENTS --------- BasemapModule.basemap_dataframe : (Pandas)DataFrame Matrix compounded by the coordinates and lines of the seismic survey's corners. Survey.survey_name : str Name of the seismic survey. RETURN ------ Panel Layout [Row] Container of the following indexed elements: [0] WidgetBox [0] Markdown for Survey.survey_name [1] IntSlider for inline number selection [2] IntSlider for crossline number selection [3] Select for well selection [1] basemap attribute FUNCTIONS --------- basemap_plot(**kwargs) Constructs the basemap attribute. update_plot(**kwargs) Links Panel's selection widgets to the basemap attribute. """ df = self.basemap_dataframe # Widgets iline_number = pn.widgets.IntSlider(name="Inline number", start=int(df["iline"].min()), end=int(df["iline"].max()), step=self.iline_step, value=int(df["iline"].min())) xline_number = pn.widgets.IntSlider(name="Crossline number", start=int(df["xline"].min()), end=int(df["xline"].max()), step=self.xline_step, value=int(df["xline"].min())) select_well = pn.widgets.Select(name="Select the well to inspect", options=["None"] + list(self.wells_dataframe["name"]), value="None") @pn.depends(iline_number.param.value, xline_number.param.value, select_well.param.value) def basemap_plot(iline_number, xline_number, select_well): """ NAME ---- basemap_plot DESCRIPTION ----------- Constructs the basemap attribute. Merges seismic survey related plots using Holoviews and bokeh as backend. ARGUMENTS --------- Arguments are given by Panel's widgets through the panel's depend decorator: iline_number : int Number of the chosen inline. xline_number : int Number of the chosen crossline. select_well : str Automatically gives well's line numbers when selected. RETURN ------ basemap : Holviews element [Overlay] instance attribute Combination of the plots: polygon, wells and seismic_lines. """ #new attributes WiggleModule.inline_number = iline_number WiggleModule.crossline_number = xline_number # First element BasemapModule.polygon = BasemapModule.polygon_plot(self) # Second element BasemapModule.wells = BasemapModule.wells_plot(self) # Third element BasemapModule.seismic_lines = BasemapModule.seismic_line_plot( self, iline_number, xline_number) # Final Overlay BasemapModule.basemap = BasemapModule.polygon * BasemapModule.wells * BasemapModule.seismic_lines BasemapModule.basemap.opts(legend_position='top', height=600, width=600) return (BasemapModule.basemap) widgets = pn.WidgetBox(f"## {Survey.survey} Basemap", iline_number, xline_number, select_well) def update_plot(event): """ NAME ---- update_plot DESCRIPTION ----------- Links Panel's selection widgets to the basemap attribute. Modifies the target plot when a well is selected through Panel's selector widget. ARGUMENTS --------- event : str Panel's selector widget value. RETURN ------ basemap : Holviews element [Overlay] instance attribute Combination of the plots: polygon, wells and seismic_lines. """ if select_well.value != "None": iline_number.value = int( self.wells_dataframe["cdp_iline"].loc[str( select_well.value)]) xline_number.value = int( self.wells_dataframe["cdp_xline"].loc[str( select_well.value)]) WiggleModule.inline_number = iline_number.value WiggleModule.crossline_number = xline_number.value select_well.param.watch(update_plot, 'value') return pn.Row(widgets, basemap_plot).servable()
def curve_defaults(cls, **kwargs): return opts.defaults( opts.Curve(**_dict_merge(kwargs, cls.DEFAULT_PLOT_OPTS)))
from .plot_average_data import PlotAverageData from .bokeh_plot_manager import BokehPlotManager import math from . import average_water_view from rti_python.Post_Process.Average.AverageWaterColumn import AverageWaterColumn import pandas as pd import holoviews as hv from holoviews import opts, dim, Palette hv.extension('bokeh') import panel as pn pn.extension() from bokeh.plotting import figure, ColumnDataSource opts.defaults( opts.Bars(xrotation=45, tools=['hover']), opts.BoxWhisker(width=800, xrotation=30, box_fill_color=Palette('Category20')), opts.Curve(width=600, tools=['hover']), opts.GridSpace(shared_yaxis=True), opts.Scatter(width=800, height=400, color=Palette('Category20'), size=dim('growth')+5, tools=['hover']), opts.NdOverlay(legend_position='left')) class AverageWaterVM(average_water_view.Ui_AvgWater, QWidget): increment_ens_sig = pyqtSignal(int) reset_avg_sig = pyqtSignal() avg_taken_sig = pyqtSignal() def __init__(self, parent, rti_config): average_water_view.Ui_AvgWater.__init__(self) QWidget.__init__(self, parent) self.setupUi(self)
def get_individual_plot(current_file): ds = open_files[current_file] # get reference flowline for true values rgi_id = ds.attrs['rgi_id'] # rgi_id = translate_name_rgi[glacier_select.value] for gdir in gdirs: if gdir.rgi_id == rgi_id: fl_ref = gdir.read_pickle('model_flowlines', filesuffix='_combine_true_init')[0] # now calculate data for delta bed_h and w0_m data_bed_h = [] d_bed_h_lim = 0 data_w0_m = [] d_w0_m_lim = 0 for i, fl in enumerate(ds.flowlines.values): x_all = ds.coords['x'][ds.ice_mask].values # bed_h d_bed_h = (fl.bed_h - fl_ref.bed_h)[ds.ice_mask] d_bed_h_lim = np.max([d_bed_h_lim, np.max(np.abs(d_bed_h))]) for el in [(x, i, v) for x, v in zip(x_all, d_bed_h)]: data_bed_h.append(el) # w0_m d_w0_m = (fl._w0_m - fl_ref._w0_m)[ds.ice_mask] d_w0_m_lim = np.max([d_w0_m_lim, np.max(np.abs(d_w0_m))]) for el in [(x, i, v) for x, v in zip(x_all, d_w0_m)]: data_w0_m.append(el) def get_heatmap(data, lim, title, kdim='x', vdim='Iteration', height=200): return hv.HeatMap(data, kdims=[kdim, vdim]).opts( opts.HeatMap(tools=['hover'], colorbar=True, width=350, height=height, invert_yaxis=True, ylabel='Iteration', title=title, clim=(-lim, lim), cmap='RdBu')) # plots for delta bed_h and w0_m delta_bed_h_plot = get_heatmap(data_bed_h, d_bed_h_lim, 'Delta bed_h', kdim='ice_mask_x', height=200) delta_w0_m_plot = get_heatmap(data_w0_m, d_w0_m_lim, 'Delta w0_m', kdim='ice_mask_x', height=200) parameter_indices = ds.attrs['parameter_indices'] # plot for height shift spinup if there if 'height_shift_spinup' in parameter_indices.keys(): height_shift_data = [] for i, unknown_p in enumerate(ds.unknown_parameters.values): height_shift_data.append( (i, unknown_p[parameter_indices['height_shift_spinup']])) height_shift_spinup_plot = hv.Curve( height_shift_data, kdims='Iterations', vdims='shift (m)', ).opts( opts.Curve(title='spinup height shift', tools=['hover'], height=200)) else: height_shift_spinup_plot = None # get gradients data_grad_bed_h = None data_grad_area_bed_h = None data_grad_w0_m = None data_grad_surface_h = None data_grad_height_shift_spinup = None if 'bed_h' in parameter_indices.keys(): data_grad_bed_h = [] grad_bed_h_lim = 0 if 'area_bed_h' in parameter_indices.keys(): data_grad_area_bed_h = [] grad_area_bed_h_lim = 0 if 'w0_m' in parameter_indices.keys(): data_grad_w0_m = [] grad_w0_m_lim = 0 if 'surface_h' in parameter_indices.keys(): data_grad_surface_h = [] grad_surface_h_lim = 0 if 'height_shift_spinup' in parameter_indices.keys(): data_grad_height_shift_spinup = [] for i, grad in enumerate(ds.grads.values): x_ice_mask = ds.coords['x'][ds.ice_mask].values x_all = ds.coords['x'].values # bed_h if 'bed_h' in parameter_indices.keys(): grad_bed_h = grad[parameter_indices['bed_h']] grad_bed_h_lim = np.max( [grad_bed_h_lim, np.max(np.abs(grad_bed_h))]) for el in [(x, i, v) for x, v in zip(x_ice_mask, grad_bed_h)]: data_grad_bed_h.append(el) if 'area_bed_h' in parameter_indices.keys(): grad_area_bed_h = grad[parameter_indices['area_bed_h']] grad_area_bed_h_lim = np.max( [grad_area_bed_h_lim, np.max(np.abs(grad_area_bed_h))]) for el in [(x, i, v) for x, v in zip(x_ice_mask, grad_area_bed_h)]: data_grad_area_bed_h.append(el) if 'w0_m' in parameter_indices.keys(): grad_w0_m = grad[parameter_indices['w0_m']] grad_w0_m_lim = np.max( [grad_w0_m_lim, np.max(np.abs(grad_w0_m))]) for el in [(x, i, v) for x, v in zip(x_ice_mask, grad_w0_m)]: data_grad_w0_m.append(el) if 'surface_h' in parameter_indices.keys(): grad_surface_h = grad[parameter_indices['surface_h']] grad_surface_h_lim = np.max( [grad_surface_h_lim, np.max(np.abs(grad_surface_h))]) for el in [(x, i, v) for x, v in zip(x_all, grad_surface_h)]: data_grad_surface_h.append(el) if 'height_shift_spinup' in parameter_indices.keys(): data_grad_height_shift_spinup.append( (i, grad[parameter_indices['height_shift_spinup']])) grad_plots = None if 'bed_h' in parameter_indices.keys(): grad_plots = pn.Column(get_heatmap(data_grad_bed_h, grad_bed_h_lim, 'Grad bed_h', kdim='ice_mask_x', height=200), sizing_mode='stretch_width') elif 'area_bed_h' in parameter_indices.keys(): grad_plots = pn.Column(get_heatmap(data_grad_area_bed_h, grad_area_bed_h_lim, 'Grad area_bed_h', kdim='ice_mask_x', height=200), sizing_mode='stretch_width') if 'w0_m' in parameter_indices.keys(): grad_plots.append( get_heatmap(data_grad_w0_m, grad_w0_m_lim, 'Grad w0_m', kdim='ice_mask_x', height=200)) if 'surface_h' in parameter_indices.keys(): grad_plots.append( get_heatmap(data_grad_surface_h, grad_surface_h_lim, 'Grad surface_h', kdim='total_distance_x', height=200)) if 'height_shift_spinup' in parameter_indices.keys(): grad_plots.append( hv.Curve( data_grad_height_shift_spinup, kdims='Iterations', vdims='gradient', ).opts( opts.Curve(title='spinup height shift gradient', tools=['hover'], height=200))) # convert c_terms c_terms_conv = {} hover_height = 0 for term in ds.c_terms_description.values: # term = ds.c_terms_description.values[0] for var in term.keys(): var_use = var.replace(':', '_') var_use = var_use.replace('-', '') if type(term[var]) == dict: yr = list(term[var].keys())[0] yr_use = yr.replace('-', '_') if var_use + '_' + yr_use not in c_terms_conv.keys(): c_terms_conv[var_use + '_' + yr_use] = [] c_terms_conv[var_use + '_' + yr_use].append(term[var][yr]) hover_height = np.max( [hover_height, np.max(term[var][yr])]) else: if var_use not in c_terms_conv.keys(): c_terms_conv[var_use] = [] c_terms_conv[var_use].append(term[var]) hover_height = np.max([hover_height, np.max(term[var])]) c_term_area = [] for one_c_term in c_terms_conv.keys(): c_term_area.append( hv.Area( (ds.coords['iteration'].values, c_terms_conv[one_c_term]), kdims='Iterations', vdims='c_terms', label=one_c_term)) overlay_c_terms = hv.Overlay(c_term_area) stack_c_terms = hv.Area.stack(overlay_c_terms) df_c_terms = pd.DataFrame(c_terms_conv) df_c_terms['Iteration'] = ds.coords['iteration'].values df_c_terms['hover_height'] = np.repeat( hover_height / 2, len(ds.coords['iteration'].values)) tooltips_c_terms = [('Iteration', '@{Iteration}')] tooltips_c_terms += [(key, '@{' + key + '}{%0.4f}') for key in c_terms_conv.keys()] hover_c_terms = HoverTool(tooltips=tooltips_c_terms, formatters=dict([ ('@{' + key + '}', 'printf') for key in c_terms_conv.keys() ]), mode='vline') vdims_curve_c_terms = ['hover_height'] for key in c_terms_conv.keys(): vdims_curve_c_terms.append(key) curve_c_terms = hv.Curve(df_c_terms, kdims='Iteration', vdims=vdims_curve_c_terms).opts( tools=[hover_c_terms], line_alpha=0) c_terms_plot = (stack_c_terms * curve_c_terms).opts( width=500, height=200, legend_position='left', title='Cost Terms') # calculate differences of surface height at start, rgi and end for gdir in gdirs: if gdir.rgi_id == rgi_id: fl_ref_rgi = gdir.read_pickle( 'model_flowlines', filesuffix='_combine_true_init')[0] fl_ref_start = gdir.read_pickle('model_flowlines', filesuffix='_spinup')[0] fl_ref_end = gdir.read_pickle( 'model_flowlines', filesuffix='_combine_true_end')[0] def get_performance_sfc_h_array(fct, data, ref_val): return [np.around(fct(val, ref_val), decimals=2) for val in data] def get_sfc_h_table(data, ref_val, title): df = pd.DataFrame({ 'RMSE': get_performance_sfc_h_array(RMSE, data, ref_val), 'BIAS': get_performance_sfc_h_array(BIAS, data, ref_val), 'DIFF': get_performance_sfc_h_array(DIFF, data, ref_val), 'AERR': get_performance_sfc_h_array(AERR, data, ref_val), }) return pn.Column(pn.pane.Markdown('Statistics ' + title), pn.widgets.Tabulator(df, titles={'index': 'I'}, height=200), sizing_mode='stretch_width') # sfc_h_end d_sfc_h_end_lim = 0. data_sfc_h_end = [] table_data_sfc_h_end = [] for i, fl in enumerate(ds.flowlines.values): x_all = ds.coords['x'].values d_sfc_h_end = (fl.surface_h - fl_ref_end.surface_h) d_sfc_h_end_lim = np.max( [d_sfc_h_end_lim, np.max(np.abs(d_sfc_h_end))]) table_data_sfc_h_end.append(fl.surface_h) for el in [(x, i, v) for x, v in zip(x_all, d_sfc_h_end)]: data_sfc_h_end.append(el) delta_sfc_h_end_plot = get_heatmap(data_sfc_h_end, d_sfc_h_end_lim, 'Delta sfc_h_end', kdim='total_distance_x', height=150) delta_sfc_h_end_table = get_sfc_h_table(table_data_sfc_h_end, fl_ref_end.surface_h, 'sfc_h_end') # sfc_h_rgi if 'fl_surface_h:m' in ds.observations_mdl.values[0].keys(): d_sfc_h_rgi_lim = 0. data_sfc_h_rgi = [] table_data_sfc_h_rgi = [] for i, obs in enumerate(ds.observations_mdl.values): x_all = ds.coords['x'].values d_sfc_h_rgi = (list(obs['fl_surface_h:m'].values())[0] - fl_ref_rgi.surface_h) d_sfc_h_rgi_lim = np.max( [d_sfc_h_rgi_lim, np.max(np.abs(d_sfc_h_rgi))]) table_data_sfc_h_rgi.append( list(obs['fl_surface_h:m'].values())[0]) for el in [(x, i, v) for x, v in zip(x_all, d_sfc_h_rgi)]: data_sfc_h_rgi.append(el) delta_sfc_h_rgi_plot = get_heatmap(data_sfc_h_rgi, d_sfc_h_rgi_lim, 'Delta sfc_h_rgi', kdim='total_distance_x', height=150) delta_sfc_h_rgi_table = get_sfc_h_table(table_data_sfc_h_rgi, fl_ref_rgi.surface_h, 'sfc_h_rgi') else: delta_sfc_h_rgi_plot = None delta_sfc_h_rgi_table = None # sfc_h_start d_sfc_h_start_lim = 0. data_sfc_h_start = [] table_data_sfc_h_start = [] for i, tmp_sfc_h in enumerate(ds.sfc_h_start.values): x_all = ds.coords['x'].values d_sfc_h_start = (tmp_sfc_h - fl_ref_start.surface_h) d_sfc_h_start_lim = np.max( [d_sfc_h_start_lim, np.max(np.abs(d_sfc_h_start))]) table_data_sfc_h_start.append(tmp_sfc_h) for el in [(x, i, v) for x, v in zip(x_all, d_sfc_h_start)]: data_sfc_h_start.append(el) delta_sfc_h_start_plot = get_heatmap(data_sfc_h_start, d_sfc_h_start_lim, 'Delta sfc_h_start', kdim='total_distance_x', height=150) delta_sfc_h_start_table = get_sfc_h_table(table_data_sfc_h_start, fl_ref_start.surface_h, 'sfc_h_start') # create Table with performance measures (bed_h, w0_m, sfc_h_start, sfc_h_end, sfc_h_rgi, # fct_calls, time, device) def get_performance_array(fct, attr): return [ np.around(fct(val, getattr(fl_ref, attr)[ds.ice_mask]), decimals=2) for val in [ getattr(fl.values.item(), attr)[ds.ice_mask] for fl in ds.flowlines ] ] def get_performance_table(attr): df = pd.DataFrame({ 'RMSE': get_performance_array(RMSE, attr), 'BIAS': get_performance_array(BIAS, attr), 'DIFF': get_performance_array(DIFF, attr), 'AERR': get_performance_array(AERR, attr), }) return pn.Column(pn.pane.Markdown('Statistics ' + attr), pn.widgets.Tabulator(df, titles={'index': 'I'}, height=200), sizing_mode='stretch_width') def get_minimise_performance_table(): df = pd.DataFrame({ 'forward runs': ds.fct_calls.values, 'computing time': ds.time_needed.values, 'device': np.repeat(ds.attrs['device'], len(ds.time_needed.values)) }) return pn.widgets.Tabulator(df) performance_tables = \ pn.Column(get_performance_table('bed_h'), get_performance_table('_w0_m'), get_minimise_performance_table(), sizing_mode='stretch_width') # create plot for exploration of geometry # thickness at end time data_thick_end = [] thick_end_lim = 0. for i, fl in enumerate(ds.flowlines.values): x_all = ds.coords['x'].values thick_end = fl.thick thick_end_lim = np.max([thick_end_lim, np.max(np.abs(thick_end))]) for el in [(x, i, v) for x, v in zip(x_all, thick_end)]: data_thick_end.append(el) thick_end_plot = get_heatmap(data_thick_end, thick_end_lim, 'Ice thickness at end time', kdim='total_distance_x', height=150) thick_end_true = fl_ref_end.thick thick_end_true_lim = np.max(np.abs(thick_end_true)) x_all = ds.coords['x'].values data_thick_end_true = [] for el in [(x, 0, v) for x, v in zip(x_all, thick_end_true)]: data_thick_end_true.append(el) thick_end_true_plot = get_heatmap(data_thick_end_true, thick_end_true_lim, 'Ice thickness at end time TRUE', kdim='total_distance_x', vdim='true', height=100) # surface widths def get_width_curve(x, width, label, color, height=150): return (hv.Curve( (x, width / 2), kdims='total_distance_x', vdims='widths', label=label, ) * hv.Curve( (x, -width / 2), kdims='total_distance_x', vdims='widths', label=label, )).opts(opts.Curve(color=color, tools=['hover'], height=height)) x_all = ds.coords['x'].values surface_widths_rgi_true_plot = get_width_curve(x_all, fl_ref_rgi.widths_m, 'RGI', 'blue') surface_widths_start_true_plot = get_width_curve( x_all, fl_ref_start.widths_m, 'Start', 'red') surface_widths_end_true_plot = get_width_curve(x_all, fl_ref_end.widths_m, 'End', 'gray') widths_plot = (surface_widths_start_true_plot * surface_widths_rgi_true_plot * surface_widths_end_true_plot).opts( title='Surface widths', # legend_position='right', show_legend=False) # Surface_h with bed_h x_all = ds.coords['x'].values def get_curve(x, y, label, color, height=200): return hv.Curve((x, y), kdims='total_distance_x', vdims='heights', label=label).opts( opts.Curve(color=color, tools=['hover'], height=height)) surface_height_start_true_plot = get_curve(x_all, fl_ref_start.surface_h, 'Start', 'red') surface_height_rgi_true_plot = get_curve(x_all, fl_ref_rgi.surface_h, 'RGI', 'blue') surface_height_end_true_plot = get_curve(x_all, fl_ref_end.surface_h, 'End', 'gray') bed_height_true_plot = get_curve(x_all, fl_ref_rgi.bed_h, 'bed_h', 'black') surface_height_plot = (bed_height_true_plot * surface_height_start_true_plot * surface_height_rgi_true_plot * surface_height_end_true_plot).opts( title='Surface heights', legend_position='bottom', legend_cols=2) return pn.Column('## ' + current_file, pn.Row( pn.Column( pn.Row( pn.Column(delta_bed_h_plot, delta_w0_m_plot, height_shift_spinup_plot, sizing_mode='stretch_width'), grad_plots), pn.Row(c_terms_plot, sizing_mode='stretch_width'), ), pn.Column(delta_sfc_h_start_plot, delta_sfc_h_rgi_plot, delta_sfc_h_end_plot, delta_sfc_h_start_table, delta_sfc_h_rgi_table, delta_sfc_h_end_table, sizing_mode='stretch_width'), pn.Column(thick_end_plot, thick_end_true_plot, widths_plot, surface_height_plot, performance_tables, sizing_mode='stretch_width'), ), sizing_mode='stretch_width')