Linking the selection of a particular annotator to highlight the associated curve (line_alpha=1) is sort of working, but I can’t seem to get the previously selected lines to go back to being unhighlighted (line_alpha=.5) when selecting the next line… Any tips?
from holonote.annotate import Annotator
from holonote.app import PanelWidgets
from holonote.app.tabulator import AnnotatorTable
import holoviews as hv; hv.extension('bokeh')
import panel as pn; pn.extension('tabulator')
import numpy as np
import xarray as xr
# Annotator
annotator = Annotator({"height": float, "width": float}, fields=["type"], groupby="type")
color_dim = hv.dim("type").categorize(
categories={"A": "red", "B": "orange", "C": "cyan"}, default="grey"
)
annotator.style.color = color_dim
annotator.style.alpha = .5
panel_widgets = PanelWidgets(annotator)
table_widget = AnnotatorTable(annotator)
annotator_widgets = pn.WidgetBox(panel_widgets, table_widget, horizontal=True)
# Data
width, height, frame = 30, 40, 50
frames = np.arange(frame)
data = np.random.random((width, height, frame))
da = xr.DataArray(
data,
dims=["width", "height", "frame"],
coords={
"width": np.arange(width),
"height": np.arange(height),
"frame": frames
},
name="data"
)
# App
def sel_frame(iframe):
return hv.Image(da.sel(frame=iframe)).opts(title='Image')
frame_slider = pn.widgets.DiscreteSlider(options=list(frames), value=frames[0])
dyn_img = hv.DynamicMap(pn.bind(sel_frame, frame_slider))
ts = pn.pane.HoloViews(hv.Curve([]).opts(xlim = (frames[0], frames[-1]),
title='Create an annotation in the image'))
def plot_ts(event):
curves = {}
df = annotator.df
for i, row in df.iterrows():
group = f'{row['type']}'
label = f'{i[:6]}'
h1, h2, w1, w2 = row[["start[height]", "end[height]", "start[width]", "end[width]"]]
da_sel = da.sel(height=slice(h1, h2), width=slice(w1, w2))
curve = hv.Curve(da_sel.mean(["height", "width"]), 'frame', group=group, label=label)
curve = curve.opts(subcoordinate_y=True,
color=panel_widgets.colormap[group],
line_alpha=.5)
curves[(group, label)] = curve
ts.object = (hv.Overlay(curves, kdims=['annotation'])).opts(
title='Timeseries', xlim = (frames[0], frames[-1]),
show_legend=False,)
annotator.on_event(plot_ts)
annotator.set_regions(height=(5,15), width=(5,15))
annotator.add_annotation(type='A')
annotator.set_regions(height=(25,30), width=(25,30))
annotator.add_annotation(type='B')
def highlight_selected(idx):
if len(idx) == 1:
idx = idx[0]
# highlight alpha of selected curve
row = annotator.df.loc[idx]
group = f'{row['type']}'
label = f'{idx[:6]}'
ts.object = ts.object.opts(hv.opts.Curve(f'{group}.{label}', line_alpha=1), hv.opts.Curve(line_alpha=.5))
pn.bind(highlight_selected, annotator.param.selected_indices, watch=True)
pn.Column(annotator_widgets, pn.Row(pn.Column(annotator * dyn_img, frame_slider), ts))