def test_overlay_points_errorbars(self, dynamic=False): points = Points(self.data) error = ErrorBars(self.data, kdims='x', vdims=['y', 'e']) lnk_sel = link_selections.instance(unselected_color='#ff0000') overlay = points * error if dynamic: overlay = hv.util.Dynamic(overlay) linked = lnk_sel(overlay) current_obj = linked[()] # Check initial base layers self.check_base_points_like(current_obj.Points.I, lnk_sel) self.check_base_points_like(current_obj.ErrorBars.I, lnk_sel) # Check initial selection layers self.check_overlay_points_like(current_obj.Points.II, lnk_sel, self.data) self.check_overlay_points_like(current_obj.ErrorBars.II, lnk_sel, self.data) # Select first and third point boundsxy = lnk_sel._selection_expr_streams[0]._source_streams[0] boundsxy.event(bounds=(0, 0, 4, 2)) current_obj = linked[()] # Check base layers haven't changed self.check_base_points_like(current_obj.Points.I, lnk_sel) self.check_base_points_like(current_obj.ErrorBars.I, lnk_sel) # Check selected layers self.check_overlay_points_like(current_obj.Points.II, lnk_sel, self.data.iloc[[0, 2]]) self.check_overlay_points_like(current_obj.ErrorBars.II, lnk_sel, self.data.iloc[[0, 2]])
def test_points_selection_streaming(self): buffer = hv.streams.Buffer(self.data.iloc[:2], index=False) points = hv.DynamicMap(Points, streams=[buffer]) lnk_sel = link_selections.instance(unselected_color='#ff0000') linked = lnk_sel(points) # Perform selection of first and (future) third point boundsxy = lnk_sel._selection_expr_streams[0].input_streams[0] self.assertIsInstance(boundsxy, hv.streams.SelectionXY) boundsxy.event(bounds=(0, 0, 4, 2)) current_obj = linked[()] # Check initial base layer self.check_base_points_like(current_obj.Points.I, lnk_sel, self.data.iloc[:2]) # Check selection layer self.check_overlay_points_like(current_obj.Points.II, lnk_sel, self.data.iloc[[0]]) # Now stream third point to the DynamicMap buffer.send(self.data.iloc[[2]]) current_obj = linked[()] # Check initial base layer self.check_base_points_like(current_obj.Points.I, lnk_sel, self.data) # Check selection layer self.check_overlay_points_like(current_obj.Points.II, lnk_sel, self.data.iloc[[0, 2]])
def test_scatter_selection(self, dynamic=False): scatter = hv.Scatter(self.data, kdims='x', vdims='y') if dynamic: # Convert scatter to DynamicMap that returns the element scatter = hv.util.Dynamic(scatter) lnk_sel = link_selections.instance() linked = lnk_sel(scatter) current_obj = linked[()] # Check initial state of linked dynamic map self.assertIsInstance(current_obj, hv.Overlay) # Check initial base layer self.check_base_scatter_like(current_obj.Scatter.I, lnk_sel) # Check selection layer self.check_overlay_scatter_like(current_obj.Scatter.II, lnk_sel, self.data) # Perform selection of second and third point boundsxy = lnk_sel._selection_expr_streams[0]._source_streams[0] self.assertIsInstance(boundsxy, hv.streams.BoundsXY) boundsxy.event(bounds=(0, 1, 5, 5)) current_obj = linked[()] # Check that base layer is unchanged self.check_base_scatter_like(current_obj.Scatter.I, lnk_sel) # Check selection layer self.check_overlay_scatter_like(current_obj.Scatter.II, lnk_sel, self.data.iloc[1:])
def test_scatter_selection_streaming(self): buffer = hv.streams.Buffer(self.data.iloc[:2], index=False) scatter = hv.DynamicMap(hv.Scatter, streams=[buffer]) lnk_sel = link_selections.instance() linked = lnk_sel(scatter) # Perform selection of first and (future) third point boundsxy = lnk_sel._selection_expr_streams[0]._source_streams[0] self.assertIsInstance(boundsxy, hv.streams.BoundsXY) boundsxy.event(bounds=(0, 0, 4, 2)) current_obj = linked[()] # Check initial base layer self.check_base_scatter_like( current_obj.Scatter.I, lnk_sel, self.data.iloc[:2] ) # Check selection layer self.check_overlay_scatter_like( current_obj.Scatter.II, lnk_sel, self.data.iloc[[0]] ) # Now stream third point to the DynamicMap buffer.send(self.data.iloc[[2]]) current_obj = linked[()] # Check initial base layer self.check_base_scatter_like( current_obj.Scatter.I, lnk_sel, self.data ) # Check selection layer self.check_overlay_scatter_like( current_obj.Scatter.II, lnk_sel, self.data.iloc[[0, 2]] )
def test_datashade_in_overlay_selection(self): points = Points(self.data) layout = points * dynspread(datashade(points)) lnk_sel = link_selections.instance(unselected_color='#ff0000') linked = lnk_sel(layout) current_obj = linked[()] # Check base points layer self.check_base_points_like(current_obj[()].Points.I, lnk_sel) # Check selection layer self.check_overlay_points_like(current_obj[()].Points.II, lnk_sel, self.data) # Check RGB base layer self.assertEqual( current_obj[()].RGB.I, dynspread( datashade(points, cmap=lnk_sel.unselected_cmap, alpha=255))[()]) # Check RGB selection layer self.assertEqual( current_obj[()].RGB.II, dynspread(datashade(points, cmap=lnk_sel.selected_cmap, alpha=255))[()]) # Perform selection of second and third point selectionxy = TestLinkSelections.get_value_with_key_type( lnk_sel._selection_expr_streams, hv.Points).input_streams[0].input_stream.input_streams[0] self.assertIsInstance(selectionxy, SelectionXY) selectionxy.event(bounds=(0, 1, 5, 5)) current_obj = linked[()] # Check that base points layer is unchanged self.check_base_points_like(current_obj[()].Points.I, lnk_sel) # Check points selection layer self.check_overlay_points_like(current_obj[()].Points.II, lnk_sel, self.data.iloc[1:]) # Check that base RGB layer is unchanged self.assertEqual( current_obj[()].RGB.I, dynspread( datashade(points, cmap=lnk_sel.unselected_cmap, alpha=255))[()]) # Check selection RGB layer self.assertEqual( current_obj[()].RGB.II, dynspread( datashade(points.iloc[1:], cmap=lnk_sel.selected_cmap, alpha=255))[()])
def __init__(self, listfile, **params): time_cube=np.load(listfile[0])[:-1,:,:] self.ds = utils.time_cube_to_xarray(time_cube) self.param.filter_.default = np.zeros( (max(time_cube.shape[:2]), max(time_cube.shape[:2])) ) self.param.selection.default = link_selections.instance(unselected_alpha=0.4) super().__init__(**params) listname = [w.split('/')[-1] for w in listfile] self._init_options(listname)
def test_datashade_selection(self): scatter = hv.Scatter(self.data, kdims='x', vdims='y') layout = scatter + dynspread(datashade(scatter)) lnk_sel = link_selections.instance() linked = lnk_sel(layout) current_obj = linked[()] # Check base scatter layer self.check_base_scatter_like(current_obj[0][()].Scatter.I, lnk_sel) # Check selection layer self.check_overlay_scatter_like(current_obj[0][()].Scatter.II, lnk_sel, self.data) # Check RGB base layer self.assertEqual( current_obj[1][()].RGB.I, dynspread( datashade(scatter, cmap=lnk_sel.unselected_cmap, alpha=255))[()]) # Check RGB selection layer self.assertEqual( current_obj[1][()].RGB.II, dynspread(datashade(scatter, cmap=lnk_sel.selected_cmap, alpha=0))[()]) # Perform selection of second and third point boundsxy = lnk_sel._selection_expr_streams[0]._source_streams[0] self.assertIsInstance(boundsxy, hv.streams.BoundsXY) boundsxy.event(bounds=(0, 1, 5, 5)) current_obj = linked[()] # Check that base scatter layer is unchanged self.check_base_scatter_like(current_obj[0][()].Scatter.I, lnk_sel) # Check scatter selection layer self.check_overlay_scatter_like(current_obj[0][()].Scatter.II, lnk_sel, self.data.iloc[1:]) # Check that base RGB layer is unchanged self.assertEqual( current_obj[1][()].RGB.I, dynspread( datashade(scatter, cmap=lnk_sel.unselected_cmap, alpha=255))[()]) # Check selection RGB layer self.assertEqual( current_obj[1][()].RGB.II, dynspread( datashade(scatter.iloc[1:], cmap=lnk_sel.selected_cmap, alpha=255))[()])
def test_layout_selection_scatter_table(self): scatter = hv.Scatter(self.data, kdims='x', vdims='y') table = hv.Table(self.data) lnk_sel = link_selections.instance() linked = lnk_sel(scatter + table) current_obj = linked[()] # Check initial base scatter self.check_base_scatter_like( current_obj[0][()].Scatter.I, lnk_sel ) # Check initial selection scatter self.check_overlay_scatter_like( current_obj[0][()].Scatter.II, lnk_sel, self.data ) # Check initial table self.assertEqual( self.element_color(current_obj[1][()]), [lnk_sel.unselected_color] * len(self.data) ) # Select first and third point boundsxy = lnk_sel._selection_expr_streams[0]._source_streams[0] boundsxy.event(bounds=(0, 0, 4, 2)) current_obj = linked[()] # Check base scatter self.check_base_scatter_like( current_obj[0][()].Scatter.I, lnk_sel ) # Check selection scatter self.check_overlay_scatter_like( current_obj[0][()].Scatter.II, lnk_sel, self.data.iloc[[0, 2]] ) # Check selected table self.assertEqual( self.element_color(current_obj[1][()]), [ lnk_sel.selected_color, lnk_sel.unselected_color, lnk_sel.selected_color, ] )
def _init_link_selections(self): if self._ls is not None: return doc = pn.state.curdoc if doc not in View._selections and self.selection_group: View._selections[doc] = {} self._ls = View._selections.get(doc, {}).get(self.selection_group) if self._ls is None: from holoviews.selection import link_selections self._ls = link_selections.instance() if self.selection_group: View._selections[doc][self.selection_group] = self._ls if 'selection_expr' in self.param: self._ls.param.watch(self._update_selection_expr, 'selection_expr')
def test_layout_selection_points_table(self): points = Points(self.data) table = Table(self.data) lnk_sel = link_selections.instance(selected_color="#aa0000", unselected_color='#ff0000') linked = lnk_sel(points + table) current_obj = linked[()] # Check initial base points self.check_base_points_like(current_obj[0][()].Points.I, lnk_sel) # Check initial selection points self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data) # Check initial table self.assertEqual(self.element_color(current_obj[1][()]), [lnk_sel.selected_color] * len(self.data)) # Select first and third point selectionxy = TestLinkSelections.get_value_with_key_type( lnk_sel._selection_expr_streams, hv.Points).input_streams[0].input_stream.input_streams[0] selectionxy.event(bounds=(0, 0, 4, 2)) current_obj = linked[()] # Check base points self.check_base_points_like(current_obj[0][()].Points.I, lnk_sel) # Check selection points self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data.iloc[[0, 2]]) # Check selected table self.assertEqual(self.element_color(current_obj[1][()]), [ lnk_sel.selected_color, lnk_sel.unselected_color, lnk_sel.selected_color, ])
def test_points_selection(self, dynamic=False, show_regions=True): points = Points(self.data) if dynamic: # Convert points to DynamicMap that returns the element points = hv.util.Dynamic(points) lnk_sel = link_selections.instance(show_regions=show_regions, unselected_color='#ff0000') linked = lnk_sel(points) current_obj = linked[()] # Check initial state of linked dynamic map self.assertIsInstance(current_obj, hv.Overlay) unselected, selected, region, region2 = current_obj.values() # Check initial base layer self.check_base_points_like(unselected, lnk_sel) # Check selection layer self.check_overlay_points_like(selected, lnk_sel, self.data) # Perform selection of second and third point selectionxy = TestLinkSelections.get_value_with_key_type( lnk_sel._selection_expr_streams, hv.Points).input_streams[0].input_stream.input_streams[0] self.assertIsInstance(selectionxy, hv.streams.SelectionXY) selectionxy.event(bounds=(0, 1, 5, 5)) unselected, selected, region, region2 = linked[()].values() # Check that base layer is unchanged self.check_base_points_like(unselected, lnk_sel) # Check selection layer self.check_overlay_points_like(selected, lnk_sel, self.data.iloc[1:]) if show_regions: self.assertEqual(region, Rectangles([(0, 1, 5, 5)])) else: self.assertEqual(region, Rectangles([]))
def do_crossfilter_points_histogram(self, selection_mode, cross_filter_mode, selected1, selected2, selected3, selected4, points_region1, points_region2, points_region3, points_region4, hist_region2, hist_region3, hist_region4, show_regions=True, dynamic=False): points = Points(self.data) hist = points.hist('x', adjoin=False, normed=False, num_bins=5) if dynamic: # Convert points to DynamicMap that returns the element hist_orig = hist points = hv.util.Dynamic(points) else: hist_orig = hist lnk_sel = link_selections.instance(selection_mode=selection_mode, cross_filter_mode=cross_filter_mode, show_regions=show_regions, selected_color='#00ff00', unselected_color='#ff0000') linked = lnk_sel(points + hist) current_obj = linked[()] # Check initial base points self.check_base_points_like(current_obj[0][()].Points.I, lnk_sel) # Check initial selection overlay points self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data) # Initial region bounds all None self.assertEqual(len(current_obj[0][()].Curve.I), 0) # Check initial base histogram base_hist = current_obj[1][()].Histogram.I self.assertEqual(self.element_color(base_hist), lnk_sel.unselected_color) self.assertEqual(base_hist.data, hist_orig.data) # Check initial selection overlay Histogram selection_hist = current_obj[1][()].Histogram.II self.assertEqual( self.element_color(selection_hist), self.expected_selection_color(selection_hist, lnk_sel)) self.assertEqual(selection_hist, base_hist) # No selection region region_hist = current_obj[1][()].NdOverlay.I.last self.assertEqual(region_hist.data, [None, None]) # (1) Perform selection on points of points [1, 2] points_boundsxy = lnk_sel._selection_expr_streams[0].input_streams[0] self.assertIsInstance(points_boundsxy, SelectionXY) points_boundsxy.event(bounds=(1, 1, 4, 4)) # Get current object current_obj = linked[()] # Check base points unchanged self.check_base_points_like(current_obj[0][()].Points.I, lnk_sel) # Check points selection overlay self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data.iloc[selected1]) # Check points region bounds region_bounds = current_obj[0][()].Rectangles.I self.assertEqual(region_bounds, Rectangles(points_region1)) if show_regions: self.assertEqual(self.element_color(region_bounds), box_region_color) # (2) Perform selection on histogram bars [0, 1] hist_boundsxy = lnk_sel._selection_expr_streams[1].input_streams[0] self.assertIsInstance(hist_boundsxy, SelectionXY) hist_boundsxy.event(bounds=(0, 0, 2.5, 2)) points_unsel, points_sel, points_region, points_region_poly = current_obj[ 0][()].values() # Check points selection overlay self.check_overlay_points_like(points_sel, lnk_sel, self.data.iloc[selected2]) self.assertEqual(points_region, Rectangles(points_region2)) # Check base histogram unchanged base_hist, region_hist, sel_hist = current_obj[1][()].values() self.assertEqual(self.element_color(base_hist), lnk_sel.unselected_color) self.assertEqual(base_hist.data, hist_orig.data) # Check selection region covers first and second bar if show_regions: self.assertEqual(self.element_color(region_hist.last), hist_region_color) if not len(hist_region2) and lnk_sel.selection_mode != 'inverse': self.assertEqual(len(region_hist), 1) else: self.assertEqual(region_hist.last.data, [0, 2.5]) # Check histogram selection overlay self.assertEqual(self.element_color(sel_hist), self.expected_selection_color(sel_hist, lnk_sel)) self.assertEqual( sel_hist.data, hist_orig.pipeline(hist_orig.dataset.iloc[selected2]).data) # (3) Perform selection on points points [0, 2] points_boundsxy = lnk_sel._selection_expr_streams[0].input_streams[0] self.assertIsInstance(points_boundsxy, SelectionXY) points_boundsxy.event(bounds=(0, 0, 4, 2.5)) # Check selection overlay points contains only second point self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data.iloc[selected3]) # Check points region bounds region_bounds = current_obj[0][()].Rectangles.I self.assertEqual(region_bounds, Rectangles(points_region3)) # Check second and third histogram bars selected selection_hist = current_obj[1][()].Histogram.II self.assertEqual( selection_hist.data, hist_orig.pipeline(hist_orig.dataset.iloc[selected3]).data) # Check selection region covers first and second bar region_hist = current_obj[1][()].NdOverlay.I.last if not len(hist_region3) and lnk_sel.selection_mode != 'inverse': self.assertEqual(len(region_hist), 1) else: self.assertEqual(region_hist.data, [0, 2.5]) # (4) Perform selection of bars [1, 2] hist_boundsxy = lnk_sel._selection_expr_streams[1].input_streams[0] self.assertIsInstance(hist_boundsxy, SelectionXY) hist_boundsxy.event(bounds=(1.5, 0, 3.5, 2)) # Check points selection overlay self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data.iloc[selected4]) # Check points region bounds region_bounds = current_obj[0][()].Rectangles.I self.assertEqual(region_bounds, Rectangles(points_region4)) # Check bar selection region region_hist = current_obj[1][()].NdOverlay.I.last if show_regions: self.assertEqual(self.element_color(region_hist), hist_region_color) if not len(hist_region4) and lnk_sel.selection_mode != 'inverse': self.assertEqual(len(region_hist), 1) elif (lnk_sel.cross_filter_mode != 'overwrite' and not (lnk_sel.cross_filter_mode == 'intersect' and lnk_sel.selection_mode == 'overwrite')): self.assertEqual(region_hist.data, [0, 3.5]) else: self.assertEqual(region_hist.data, [1.5, 3.5]) # Check bar selection overlay selection_hist = current_obj[1][()].Histogram.II self.assertEqual( self.element_color(selection_hist), self.expected_selection_color(selection_hist, lnk_sel)) self.assertEqual( selection_hist.data, hist_orig.pipeline(hist_orig.dataset.iloc[selected4]).data)
n = 25 data = { "Name": ["Glom %d" % i for i in range(1, n + 1)], "x": np.random.randn(n).round(2), "y": np.random.randn(n).round(2), "dF/F": np.random.randint(10, 100, size=n), "Odorant": np.random.randint(10, 100, size=n), "-Log(C)": np.random.uniform(3, 12, size=n).round(2) } df = pd.DataFrame(data) #.set_index('Name') #df = pd.DataFrame(np.random.randint(10, 100, size=(25, 3)), columns=['x', 'y', 'z']) scatter = df.hvplot.scatter(x="x", y="y", size="dF/F").opts(height=300, width=300) bars = df.hvplot.bar(x="Name", y="-Log(C)").opts(width=300, xrotation=70) link = link_selections.instance() plots = link(scatter + bars) @param.depends(link.param.selection_expr) def selection_table(_): return hv.Table(hv.Dataset(df).select(link.selection_expr)).opts( width=600, height=200) app = pn.Column(plots, selection_table, height=600) app # - app.show(port=8951, websocket_origin=['spike.asu.edu:8952'])
def __init__(self, **params): # How we are going to modify the values # Absolute => Set to that value # Relative => Base value + new value # Percentage => Base value + percentage self.calculation_type = pn.widgets.RadioButtonGroup( options=["Absolute", "Relative", "Percentage"], align="end") # Replacement value self.spinner = pn.widgets.IntInput(name="Replacement Value", value=0, align="start") # Whether to select Land or Ocean self.land = pn.widgets.Checkbox(name="Select Land", max_width=100, value=True) self.ocean = pn.widgets.Checkbox(name="Select Ocean", max_width=100, value=True) # Buttons self.apply = pn.widgets.Button(name="\u2713 Apply", align="end", button_type="primary") self.undo_button = pn.widgets.Button(name="\u21B6 Undo", align="end", button_type="warning") self.redo_button = pn.widgets.Button(name="Redo \u21B7", align="end", button_type="warning") self.apply_previous_changes = pn.widgets.Button( name="\u2713 Apply Previous Changes", align="end", button_type="primary") self.save_button = pn.widgets.Button(name="Save", align="end", button_type="success") # Description self.description = pn.widgets.input.TextAreaInput( name="Description: ", placeholder="Notable changes ...") # Mask self.mask = pn.widgets.Checkbox(name="Mask", max_width=100) self.mask_value = pn.widgets.IntInput(name="Mask Value", value=0) # Store the variable we want to look at and modify self.attribute = pn.widgets.Select(name="Variable", max_width=200, align="end") # Choose colormap self.colormap = pn.widgets.Select( name="Colormap", options=colormaps, value="Oleron", max_width=200, align="start", ) self.colormap_min = pn.widgets.FloatInput(name="Min Value", width=100) self.colormap_max = pn.widgets.FloatInput(name="Max Value", width=100, align="end") self.colormap_range_slider = pn.widgets.RangeSlider(width=400, show_value=False) self.colormap_delta = pn.widgets.FloatInput( name="Delta between values", value=0, align="end") # Link the viewing of multiple graphs together self.selection = link_selections.instance(unselected_alpha=0.4) # Parts of the display self.file_pane = pn.Column() self.graph_pane = pn.Column() self.options_pane = pn.Column() self.param.ds.default = xr.Dataset() self.param.loaded.default = False super().__init__(**params) self.apply.on_click(self._apply_values) self.apply_previous_changes.on_click(self._apply_previous_changes) self.undo_button.on_click(self.undo) self.redo_button.on_click(self.redo) self.save_button.on_click(self.save) self.save_button.js_on_click( args={ "target": pn.state.curdoc.session_context.request.arguments["redirect"] [0].decode() }, code="window.top.location.href = target", ) self._auto_update_cmap_min = True self._auto_update_cmap_max = True self.curvilinear_coordinates = None self._undo_list = [] self._redo_list = [] self.colormap_min.param.watch(self._colormap_callback, "value") self.colormap_max.param.watch(self._colormap_callback, "value") self.colormap_range_slider.param.watch(self._colormap_callback, "value") self.app = create_app() try: self.file_type = pn.state.curdoc.session_context.request.arguments[ "file_type"][0].decode() except KeyError: pass self.data_file_id = int( pn.state.curdoc.session_context.request.arguments["id"][0]) self._load_ds(self.data_file_id) self._options_pane_setup()