def test_rect_geom_selection_inverted(self): rect = Rectangles([(0, 1, 2, 3), (1, 3, 1.5, 4), (2.5, 4.2, 3.5, 4.8)]).opts(invert_axes=True) geom = np.array([(-0.4, -0.1), (3.2, -0.1), (3.2, 4.1), (-0.1, 4.2)]) expr, bbox, region = rect._get_selection_expr_for_stream_value(geometry=geom) self.assertEqual(bbox, {'y0': np.array([-0.4, 3.2, 3.2, -0.1]), 'x0': np.array([-0.1, -0.1, 4.1, 4.2]), 'y1': np.array([-0.4, 3.2, 3.2, -0.1]), 'x1': np.array([-0.1, -0.1, 4.1, 4.2])}) self.assertEqual(expr.apply(rect), np.array([True, False, False])) self.assertEqual(region, Rectangles([]) * Path([list(geom)+[(-0.4, -0.1)]]))
def test_segs_selection_numeric_inverted(self): segs = Segments([(0, 1, 2, 3), (1, 3, 1.5, 4), (2.5, 4.2, 3.5, 4.8)]).opts(invert_axes=True) expr, bbox, region = segs._get_selection_expr_for_stream_value(bounds=(0.9, 0.5, 4.9, 3.4)) self.assertEqual(bbox, {'x0': (0.5, 3.4), 'y0': (0.9, 4.9), 'x1': (0.5, 3.4), 'y1': (0.9, 4.9)}) self.assertEqual(expr.apply(segs), np.array([False, True, False])) self.assertEqual(region, Rectangles([(0.9, 0.5, 4.9, 3.4)]) * Path([])) expr, bbox, region = segs._get_selection_expr_for_stream_value(bounds=(0.9, 0, 4.9, 3.5)) self.assertEqual(bbox, {'x0': (0, 3.5), 'y0': (0.9, 4.9), 'x1': (0, 3.5), 'y1': (0.9, 4.9)}) self.assertEqual(expr.apply(segs), np.array([True, True, True])) self.assertEqual(region, Rectangles([(0.9, 0, 4.9, 3.5)]) * Path([]))
def test_box_edit_callback(self): boxes = Rectangles([(-0.5, -0.5, 0.5, 0.5)]) box_edit = BoxEdit(source=boxes) plot = bokeh_server_renderer.get_plot(boxes) self.assertIsInstance(plot.callbacks[0], BoxEditCallback) callback = plot.callbacks[0] source = plot.handles['cds'] self.assertEqual(source.data['x'], [0]) self.assertEqual(source.data['y'], [0]) self.assertEqual(source.data['width'], [1]) self.assertEqual(source.data['height'], [1]) data = {'x': [0, 1], 'y': [0, 1], 'width': [0.5, 2], 'height': [2, 0.5]} callback.on_msg({'data': data}) element = Rectangles([(-0.25, -1, 0.25, 1), (0, 0.75, 2, 1.25)]) self.assertEqual(box_edit.element, element)
def test_rectangles_simple(self): rectangles = Tiles("") * Rectangles([ (0, 0, self.x_range[1], self.y_range[1]), (self.x_range[0], self.y_range[0], 0, 0), ]).redim.range(x=self.x_range, y=self.y_range) state = self._get_plot_state(rectangles) self.assertEqual(state["data"][1]["type"], "scattermapbox") self.assertEqual(state["data"][1]["mode"], "lines") self.assertEqual(state["data"][1]["showlegend"], False) self.assertEqual(state["data"][1]["line"]["color"], default_shape_color) self.assertEqual( state["data"][1]["lon"], np.array([ 0, 0, self.lon_range[1], self.lon_range[1], 0, np.nan, self.lon_range[0], self.lon_range[0], 0, 0, self.lon_range[0], np.nan ])) self.assertEqual( state["data"][1]["lat"], np.array([ 0, self.lat_range[1], self.lat_range[1], 0, 0, np.nan, self.lat_range[0], 0, 0, self.lat_range[0], self.lat_range[0], np.nan ])) self.assertEqual(state['layout']['mapbox']['center'], { 'lat': self.lat_center, 'lon': self.lon_center })
def test_boxes_simple(self): boxes = Rectangles([(0, 0, 1, 1), (2, 2, 4, 3)]) state = self._get_plot_state(boxes) shapes = state['layout']['shapes'] self.assertEqual(len(shapes), 2) self.assertEqual( shapes[0], { 'type': 'rect', 'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1, 'xref': 'x', 'yref': 'y', 'name': '', 'line': { 'color': default_shape_color } }) self.assertEqual( shapes[1], { 'type': 'rect', 'x0': 2, 'y0': 2, 'x1': 4, 'y1': 3, 'xref': 'x', 'yref': 'y', 'name': '', 'line': { 'color': default_shape_color } }) self.assertEqual(state['layout']['xaxis']['range'], [0, 4]) self.assertEqual(state['layout']['yaxis']['range'], [0, 3])
def test_scatter_selection_numeric(self): scatter = Scatter([3, 2, 1, 3, 4]) expr, bbox, region = scatter._get_selection_expr_for_stream_value( bounds=(1, 0, 3, 2)) self.assertEqual(bbox, {'x': (1, 3), 'y': (0, 2)}) self.assertEqual(expr.apply(scatter), np.array([False, True, True, False, False])) self.assertEqual(region, Rectangles([(1, 0, 3, 2)]))
def test_scatter_selection_categorical(self): scatter = Scatter((['B', 'A', 'C', 'D', 'E'], [3, 2, 1, 3, 4])) expr, bbox, region = scatter._get_selection_expr_for_stream_value( bounds=(0, 1, 2, 3), x_selection=['B', 'A', 'C'], y_selection=None ) self.assertEqual(bbox, {'x': ['B', 'A', 'C'], 'y': (1, 3)}) self.assertEqual(expr.apply(scatter), np.array([True, True, True, False, False])) self.assertEqual(region, Rectangles([(0, 1, 2, 3)]) * Path([]))
def test_points_selection_geom_inverted(self): points = Points([3, 2, 1, 3, 4]).opts(invert_axes=True) geom = np.array([(-0.1, -0.1), (1.4, 0), (1.4, 2.2), (-0.1, 2.2)]) expr, bbox, region = points._get_selection_expr_for_stream_value(geometry=geom) self.assertEqual(bbox, {'y': np.array([-0.1, 1.4, 1.4, -0.1]), 'x': np.array([-0.1, 0, 2.2, 2.2])}) self.assertEqual(expr.apply(points), np.array([False, False, True, False, False])) self.assertEqual(region, Rectangles([]) * Path([list(geom)+[(-0.1, -0.1)]]))
def test_scatter_selection_numeric_inverted(self): scatter = Scatter([3, 2, 1, 3, 4]).opts(invert_axes=True) expr, bbox, region = scatter._get_selection_expr_for_stream_value( bounds=(0, 1, 2, 3)) self.assertEqual(bbox, {'x': (1, 3), 'y': (0, 2)}) self.assertEqual(expr.apply(scatter), np.array([False, True, True, False, False])) self.assertEqual(region, Rectangles([(0, 1, 2, 3)]) * Path([]))
def test_points_selection_numeric(self): points = Points([3, 2, 1, 3, 4]) expr, bbox, region = points._get_selection_expr_for_stream_value( bounds=(1, 0, 3, 2)) self.assertEqual(bbox, {'x': (1, 3), 'y': (0, 2)}) self.assertEqual(expr.apply(points), np.array([False, True, True, False, False])) self.assertEqual(region, Rectangles([(1, 0, 3, 2)]) * Path([]))
def test_points_selection_numeric_inverted(self): points = Points([3, 2, 1, 3, 4]).opts(invert_axes=True) expr, bbox, region = points._get_selection_expr_for_stream_value( bounds=(0, 1, 2, 3)) self.assertEqual(bbox, {'x': (1, 3), 'y': (0, 2)}) self.assertEqual(expr.apply(points), np.array([False, True, True, False, False])) self.assertEqual(region, Rectangles([(0, 1, 2, 3)]))
def test_poly_selection_numeric_inverted(self): poly = Polygons([ [(0, 0), (0.2, 0.1), (0.3, 0.4), (0.1, 0.2)], [(0.25, -.1), (0.4, 0.2), (0.6, 0.3), (0.5, 0.1)], [(0.3, 0.3), (0.5, 0.4), (0.6, 0.5), (0.35, 0.45)] ]).opts(invert_axes=True) expr, bbox, region = poly._get_selection_expr_for_stream_value(bounds=(0.2, -0.2, 0.6, 0.6)) self.assertEqual(bbox, {'y': (0.2, 0.6), 'x': (-0.2, 0.6)}) self.assertEqual(expr.apply(poly, expanded=False), np.array([False, False, True])) self.assertEqual(region, Rectangles([(0.2, -0.2, 0.6, 0.6)]) * Path([]))
def test_rgb_selection_numeric(self): img = RGB(([0, 1, 2], [0, 1, 2, 3], np.random.rand(4, 3, 3))) expr, bbox, region = img._get_selection_expr_for_stream_value( bounds=(0.5, 1.5, 2.1, 3.1)) self.assertEqual(bbox, {'x': (0.5, 2.1), 'y': (1.5, 3.1)}) self.assertEqual( expr.apply(img, expanded=True, flat=False), np.array([[False, False, False], [False, False, False], [False, True, True], [False, True, True]])) self.assertEqual(region, Rectangles([(0.5, 1.5, 2.1, 3.1)]) * Path([]))
def test_image_selection_numeric_inverted(self): img = Image(([0, 1, 2], [0, 1, 2, 3], np.random.rand(4, 3))).opts(invert_axes=True) expr, bbox, region = img._get_selection_expr_for_stream_value(bounds=(1.5, 0.5, 3.1, 2.1)) self.assertEqual(bbox, {'x': (0.5, 2.1), 'y': (1.5, 3.1)}) self.assertEqual(expr.apply(img, expanded=True, flat=False), np.array([ [False, False, False], [False, False, False], [False, True, True], [False, True, True] ])) self.assertEqual(region, Rectangles([(1.5, 0.5, 3.1, 2.1)]) * Path([]))
def test_poly_geom_selection_inverted(self): poly = Polygons([ [(0, 0), (0.2, 0.1), (0.3, 0.4), (0.1, 0.2)], [(0.25, -.1), (0.4, 0.2), (0.6, 0.3), (0.5, 0.1)], [(0.3, 0.3), (0.5, 0.4), (0.6, 0.5), (0.35, 0.45)] ]).opts(invert_axes=True) geom = np.array([(0.2, -0.15), (0.5, 0), (0.75, 0.6), (0.1, 0.6)]) expr, bbox, region = poly._get_selection_expr_for_stream_value(geometry=geom) self.assertEqual(bbox, {'y': np.array([0.2, 0.5, 0.75, 0.1]), 'x': np.array([-0.15, 0, 0.6, 0.6])}) self.assertEqual(expr.apply(poly, expanded=False), np.array([False, False, True])) self.assertEqual(region, Rectangles([]) * Path([list(geom)+[(0.2, -0.15)]]))
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 test_img_selection_geom_inverted(self): img = Image(([0, 1, 2], [0, 1, 2, 3], np.random.rand(4, 3))).opts(invert_axes=True) geom = np.array([(-0.4, -0.1), (0.6, -0.1), (0.4, 1.7), (-0.1, 1.7)]) expr, bbox, region = img._get_selection_expr_for_stream_value(geometry=geom) self.assertEqual(bbox, {'y': np.array([-0.4, 0.6, 0.4, -0.1]), 'x': np.array([-0.1, -0.1, 1.7, 1.7])}) self.assertEqual(expr.apply(img, expanded=True, flat=False), np.array([ [ True, True, False], [ False, False, False], [ False, False, False], [False, False, False] ])) self.assertEqual(region, Rectangles([]) * Path([list(geom)+[(-0.4, -0.1)]]))
def test_rect_selection_numeric(self): rect = Rectangles([(0, 1, 2, 3), (1, 3, 1.5, 4), (2.5, 4.2, 3.5, 4.8)]) expr, bbox, region = rect._get_selection_expr_for_stream_value(bounds=(0.5, 0.9, 3.4, 4.9)) self.assertEqual(bbox, {'x0': (0.5, 3.4), 'y0': (0.9, 4.9), 'x1': (0.5, 3.4), 'y1': (0.9, 4.9)}) self.assertEqual(expr.apply(rect), np.array([False, True, False])) self.assertEqual(region, Rectangles([(0.5, 0.9, 3.4, 4.9)]) * Path([])) expr, bbox, region = rect._get_selection_expr_for_stream_value(bounds=(0, 0.9, 3.5, 4.9)) self.assertEqual(bbox, {'x0': (0, 3.5), 'y0': (0.9, 4.9), 'x1': (0, 3.5), 'y1': (0.9, 4.9)}) self.assertEqual(expr.apply(rect), np.array([True, True, True])) self.assertEqual(region, Rectangles([(0, 0.9, 3.5, 4.9)]) * Path([]))
def test_quadmesh_selection_inverted(self): n = 4 coords = np.linspace(-1.5,1.5,n) X,Y = np.meshgrid(coords, coords); Qx = np.cos(Y) - np.cos(X) Qy = np.sin(Y) + np.sin(X) Z = np.sqrt(X**2 + Y**2) qmesh = QuadMesh((Qx, Qy, Z)).opts(invert_axes=True) expr, bbox, region = qmesh._get_selection_expr_for_stream_value(bounds=(0, -0.5, 0.7, 1.5)) self.assertEqual(bbox, {'x': (-0.5, 1.5), 'y': (0, 0.7)}) self.assertEqual(expr.apply(qmesh, expanded=True, flat=False), np.array([ [False, False, False, True], [False, False, True, True], [False, True, False, False], [True, False, False, False] ])) self.assertEqual(region, Rectangles([(0, -0.5, 0.7, 1.5)]) * Path([]))
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)