def test_layout_title_format(self): title_str = ('Label: {label}, group: {group}, ' 'dims: {dimensions}, type: {type}') layout = NdLayout( { 'Element 1': Scatter( [], label='ONE', group='first', ), 'Element 2': Scatter( [], label='TWO', group='second', ) }, kdims='MYDIM', label='the_label', group='the_group', ).opts(opts.NdLayout(title=title_str), opts.Scatter(title=title_str)) # Title of NdLayout title = bokeh_renderer.get_plot(layout).handles['title'] self.assertIsInstance(title, Div) text = 'Label: the_label, group: the_group, dims: , type: NdLayout' self.assertEqual(re.split('>|</', title.text)[1], text) # Titles of subplots plot = render(layout) titles = {title.text for title in list(plot.select({'type': Title}))} titles_correct = { 'Label: ONE, group: first, dims: MYDIM: Element 1, type: Scatter', 'Label: TWO, group: second, dims: MYDIM: Element 2, type: Scatter', } self.assertEqual(titles_correct, titles)
def test_deep_apply_transform_element_type(self): fn = lambda i: Curve(np.arange(i)) dmap = DynamicMap(fn, kdims=[Dimension('Test', range=(10, 20))]) dmap[10] mapped = dmap.apply(lambda x: Scatter(x)) area = mapped[11] self.assertEqual(area, Scatter(fn(11)))
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_scatter_selection_numeric_index_cols(self): scatter = Scatter([3, 2, 1, 3, 2]) expr, bbox, region = scatter._get_selection_expr_for_stream_value( bounds=(1, 0, 3, 2), index_cols=['y'] ) self.assertEqual(bbox, {'x': (1, 3), 'y': (0, 2)}) self.assertEqual(expr.apply(scatter), np.array([False, False, True, False, False])) self.assertEqual(region, None)
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_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_dataset_conversion_groupby_with_index(self): df = pd.DataFrame({'y': [1, 2, 3], 'x': [0, 0, 1]}, index=[0, 1, 2]) scatters = Dataset(df).to(Scatter, 'index', 'y') hmap = HoloMap( { 0: Scatter(([0, 1], [1, 2]), 'index', 'y'), 1: Scatter([(2, 3)], 'index', 'y') }, 'x') self.assertEqual(scatters, hmap)
def test_data_link_nan(self): arr = np.random.rand(3, 5) arr[0, 0] = np.nan data = {k: v for k, v in zip(['x', 'y', 'z'], arr)} a = Scatter(data, 'x', 'z') b = Scatter(data, 'x', 'y') DataLink(a, b) try: bokeh_renderer.get_plot(a + b) except: self.fail()
def test_dynamic_groupby_kdims_and_streams(self): def plot_function(mydim, data): return Scatter(data[data[:, 2]==mydim]) buff = Buffer(data=np.empty((0, 3))) dmap = DynamicMap(plot_function, streams=[buff], kdims='mydim').redim.values(mydim=[0, 1, 2]) ndlayout = dmap.groupby('mydim', container_type=NdLayout) self.assertIsInstance(ndlayout[0], DynamicMap) data = np.array([(0, 0, 0), (1, 1, 1), (2, 2, 2)]) buff.send(data) self.assertEqual(ndlayout[0][()], Scatter([(0, 0)])) self.assertEqual(ndlayout[1][()], Scatter([(1, 1)])) self.assertEqual(ndlayout[2][()], Scatter([(2, 2)]))
def test_overlay_framewise_norm(self): a = {'X': [0, 1, 2], 'Y': [0, 1, 2], 'Z': [0, 50, 100]} b = {'X': [3, 4, 5], 'Y': [0, 10, 20], 'Z': [50, 50, 150]} sa = Scatter(a, 'X', ['Y', 'Z']).opts(color='Z', framewise=True) sb = Scatter(b, 'X', ['Y', 'Z']).opts(color='Z', framewise=True) plot = bokeh_renderer.get_plot(sa * sb) sa_plot, sb_plot = plot.subplots.values() sa_cmapper = sa_plot.handles['color_color_mapper'] sb_cmapper = sb_plot.handles['color_color_mapper'] self.assertEqual(sa_cmapper.low, 0) self.assertEqual(sb_cmapper.low, 0) self.assertEqual(sa_cmapper.high, 150) self.assertEqual(sb_cmapper.high, 150)
def test_dynamic_keywords_and_kwargs(self): def fn(A='default', x=1, y=2, **kws): return Scatter([(x, y)], label=A) xy = streams.PointerXY(x=1, y=2) dmap = DynamicMap(fn, kdims=['A'], streams=[xy]) self.assertEqual(dmap['Test'], Scatter([(1, 2)], label='Test'))
def test_dynamic_streams_only_keywords(self): def fn(**kwargs): return Scatter([(kwargs['x'], kwargs['y'])], label='default') xy = streams.PointerXY(x=1, y=2) dmap = DynamicMap(fn, kdims=[], streams=[xy]) self.assertEqual(dmap[:], Scatter([(1, 2)], label='default'))
def test_dynamic_kdims_args_only(self): def fn(*args): (A, B) = args return Scatter([(A, 2)], label=A) dmap = DynamicMap(fn, kdims=['A', 'B']) self.assertEqual(dmap['Test', 1], Scatter([(1, 2)], label='Test'))
def test_dynamic_mixed_kwargs(self): def fn(x, A, y): return Scatter([(x, y)], label=A) xy = streams.PointerXY(x=1, y=2) dmap = DynamicMap(fn, kdims=['A'], streams=[xy]) self.assertEqual(dmap['Test'], Scatter([(1, 2)], label='Test'))
def test_colorbar_label_style_mapping(self): scatter = Scatter(np.random.rand(100, 3), vdims=["y", "color"]).options(color='color', colorbar=True) plot = mpl_renderer.get_plot(scatter) cbar_ax = plot.handles['cax'] self.assertEqual(cbar_ax.get_ylabel(), 'color')
def test_scatter_colors(self): scatter = Scatter([ (0, 1, 'red'), (1, 2, 'green'), (2, 3, 'blue') ], vdims=['y', 'color']).options(color='color') state = self._get_plot_state(scatter) self.assertEqual(state['data'][0]['marker']['color'], np.array(['red', 'green', 'blue']))
def test_active_tools_scroll(self): curve = Curve([1, 2, 3]) scatter = Scatter([1, 2, 3]) overlay = (scatter * curve).options(active_tools=['wheel_zoom']) plot = bokeh_renderer.get_plot(overlay) toolbar = plot.state.toolbar self.assertIsInstance(toolbar.active_scroll, tools.WheelZoomTool)
def test_active_tools_tap(self): curve = Curve([1, 2, 3]) scatter = Scatter([1, 2, 3]).options(tools=['tap']) overlay = (scatter * curve).options(active_tools=['tap']) plot = bokeh_renderer.get_plot(overlay) toolbar = plot.state.toolbar self.assertIsInstance(toolbar.active_tap, tools.TapTool)
def test_active_tools_draw_stream(self): scatter = Scatter([1, 2, 3]).options(active_tools=['point_draw']) PointDraw(source=scatter) plot = bokeh_renderer.get_plot(scatter) toolbar = plot.state.toolbar self.assertIsInstance(toolbar.active_tap, tools.PointDrawTool) self.assertIsInstance(toolbar.active_drag, tools.PointDrawTool)
def test_overlay_gridstyle_applies(self): grid_style = {'grid_line_color': 'blue', 'grid_line_width': 2} overlay = (Scatter([(10,10)]).options(gridstyle=grid_style, show_grid=True, size=20) * Labels([(10, 10, 'A')])) plot = bokeh_renderer.get_plot(overlay) self.assertEqual(plot.state.xgrid[0].grid_line_color, 'blue') self.assertEqual(plot.state.xgrid[0].grid_line_width, 2)
def test_scatter_markers(self): scatter = Scatter([(0, 1, 'square'), (1, 2, 'circle'), (2, 3, 'triangle-up')], vdims=['y', 'marker']).options(marker='marker') state = self._get_plot_state(scatter) self.assertEqual(state['data'][0]['marker']['symbol'], np.array(['square', 'circle', 'triangle-up']))
def test_scatter_selectedpoints(self): scatter = Scatter([( 0, 1, ), (1, 2), (2, 3)]).options(selectedpoints=[1, 2]) state = self._get_plot_state(scatter) self.assertEqual(state['data'][0]['selectedpoints'], [1, 2])
def test_get_range_from_element(self): dim = Dimension('y', soft_range=(0, 3), range=(0, 2)) element = Scatter([1, 2, 3], vdims=dim) drange, srange, hrange = get_range(element, {}, dim) self.assertEqual(drange, (1, 3)) self.assertEqual(srange, (0, 3)) self.assertEqual(hrange, (0, 2))
def test_scatter_color_mapped(self): scatter = Scatter([3, 2, 1]).options(color='x') state = self._get_plot_state(scatter) self.assertEqual(state['data'][0]['marker']['color'], np.array([0, 1, 2])) self.assertEqual(state['data'][0]['marker']['cmin'], 0) self.assertEqual(state['data'][0]['marker']['cmax'], 2)
def test_scatter_state(self): scatter = Scatter([3, 2, 1]) state = self._get_plot_state(scatter) self.assertEqual(state['data'][0]['type'], 'scatter') self.assertEqual(state['data'][0]['y'], np.array([3, 2, 1])) self.assertEqual(state['data'][0]['mode'], 'markers') self.assertEqual(state['layout']['yaxis']['range'], [1, 3])
def test_categorical_overlay_dimension_values_skip_factor(self): curve = Curve([('C', 1), ('B', 3)]) scatter = Scatter([('A', 2)]) plot = bokeh_renderer.get_plot( (curve * scatter).redim.values(x=['A', 'C'])) x_range = plot.handles['x_range'] self.assertEqual(x_range.factors, ['A', 'C'])
def test_scatter_state(self): # Precompute coordinates xs = [3000000, 2000000, 1000000] ys = [-3000000, -2000000, -1000000] x_range = (-5000000, 4000000) x_center = sum(x_range) / 2.0 y_range = (-3000000, 2000000) y_center = sum(y_range) / 2.0 lon_centers, lat_centers = Tiles.easting_northing_to_lon_lat([x_center], [y_center]) lon_center, lat_center = lon_centers[0], lat_centers[0] lons, lats = Tiles.easting_northing_to_lon_lat(xs, ys) scatter = Tiles('') * Scatter((xs, ys)).redim.range(x=x_range, y=y_range) state = self._get_plot_state(scatter) self.assertEqual(state['data'][1]['type'], 'scattermapbox') self.assertEqual(state['data'][1]['lon'], lons) self.assertEqual(state['data'][1]['lat'], lats) self.assertEqual(state['data'][1]['mode'], 'markers') self.assertEqual( state['layout']['mapbox']['center'], {'lat': lat_center, 'lon': lon_center} ) # There xaxis and yaxis should not be in the layout self.assertFalse('xaxis' in state['layout']) self.assertFalse('yaxis' in state['layout'])
def setUp(self): self.dmap_element = DynamicMap(lambda: Image([])) self.dmap_overlay = DynamicMap(lambda: Overlay([Curve([]), Points([])])) self.dmap_ndoverlay = DynamicMap(lambda: NdOverlay({0: Curve([]), 1: Curve([])})) self.element = Scatter([]) self.el1, self.el2 = Path([]), HLine(0) self.overlay = Overlay([self.el1, self.el2]) self.ndoverlay = NdOverlay({0: VectorField([]), 1: VectorField([])})
def test_get_range_from_ranges(self): dim = Dimension('y', soft_range=(0, 3), range=(0, 2)) element = Scatter([1, 2, 3], vdims=dim) ranges = {'y': {'soft': (-1, 4), 'hard': (-1, 3), 'data': (-0.5, 2.5)}} drange, srange, hrange = get_range(element, ranges, dim) self.assertEqual(drange, (-0.5, 2.5)) self.assertEqual(srange, (-1, 4)) self.assertEqual(hrange, (-1, 3))
def test_dynamic_split_args_and_kwargs(self): # Corresponds to the old style of kdims as posargs and streams # as kwargs, captured as *args and **kwargs def fn(*args, **kwargs): return Scatter([(kwargs['x'], kwargs['y'])], label=args[0]) xy = streams.PointerXY(x=1, y=2) dmap = DynamicMap(fn, kdims=['A'], streams=[xy]) self.assertEqual(dmap['Test'], Scatter([(1, 2)], label='Test'))