def test_line_roundtrip(self): xs = [1, 2, 3] ys = [2, 0, 7] path = Path([{ 'x': xs, 'y': ys, 'z': 1 }, { 'x': xs[::-1], 'y': ys[::-1], 'z': 2 }], ['x', 'y'], 'z', datatype=[self.datatype]) self.assertIsInstance(path.data.geometry.dtype, LineDtype) roundtrip = path.clone(datatype=['multitabular']) self.assertEqual(roundtrip.interface.datatype, 'multitabular') expected = Path([{ 'x': xs, 'y': ys, 'z': 1 }, { 'x': xs[::-1], 'y': ys[::-1], 'z': 2 }], ['x', 'y'], 'z', datatype=['multitabular']) self.assertEqual(roundtrip, expected)
def test_multi_df_dataset(self): if not pd: raise SkipTest('Pandas not available') arrays = [pd.DataFrame(np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]), columns=['x', 'y']) for i in range(2)] mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular']) for i, ds in enumerate(mds.split()): self.assertEqual(ds, Path(arrays[i], kdims=['x', 'y'], datatype=['dataframe']))
def test_sort_by_value(self): path = Path([{'x': [1, 2, 3, 4, 5], 'y': [0, 0, 1, 1, 2], 'value': 1}, {'x': [5, 4, 3, 2, 1], 'y': [2, 2, 1, 1, 0], 'value': 0}], vdims='value', datatype=[self.datatype]) self.assertIs(path.interface, self.interface) sorted = Path([{'x': [5, 4, 3, 2, 1], 'y': [2, 2, 1, 1, 0], 'value': 0}, {'x': [1, 2, 3, 4, 5], 'y': [0, 0, 1, 1, 2], 'value': 1}], vdims='value') self.assertEqual(path.sort('value'), sorted)
def test_array_redim(self): arrays = [ np.column_stack([np.arange(i, i + 2), np.arange(i, i + 2)]) for i in range(2) ] mds = Path(arrays, kdims=['x', 'y'], datatype=[self.datatype]).redim(x='x2') self.assertIs(mds.interface, self.interface) self.assertEqual(mds, Path([arrays[i] for i in range(2)], ['x2', 'y']))
def test_multi_array_dataset(self): arrays = [ np.column_stack([np.arange(i, i + 2), np.arange(i, i + 2)]) for i in range(2) ] mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular']) for i, ds in enumerate(mds.split()): self.assertEqual( ds, Path(arrays[i], kdims=['x', 'y'], datatype=['array']))
def test_multi_dict_dataset(self): arrays = [{ 'x': np.arange(i, i + 2), 'y': np.arange(i, i + 2) } for i in range(2)] mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular']) for i, ds in enumerate(mds.split()): self.assertEqual( ds, Path(arrays[i], kdims=['x', 'y'], datatype=['dictionary']))
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_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_replace_object(self): annotator = PathAnnotator(Path([]), annotations=['Label'], vertex_annotations=['Value']) annotator.object = Path([(1, 2), (2, 3), (0, 0)]) self.assertIn('Label', annotator.object) expected = Table([('')], kdims=['Label'], label='PathAnnotator') self.assertEqual(annotator._table, expected) expected = Table([], ['x', 'y'], 'Value', label='PathAnnotator Vertices') self.assertEqual(annotator._vertex_table, expected) self.assertIs(annotator._link.target, annotator._table) self.assertIs(annotator._vertex_link.target, annotator._vertex_table)
def test_path_continuously_varying_line_width_op_update(self): xs = [1, 2, 3, 4] ys = xs[::-1] path = HoloMap({ 0: Path([{'x': xs, 'y': ys, 'line_width': [1, 7, 3, 2]}], vdims='line_width'), 1: Path([{'x': xs, 'y': ys, 'line_width': [3, 8, 2, 3]}], vdims='line_width') }).options(linewidth='line_width') plot = mpl_renderer.get_plot(path) artist = plot.handles['artist'] self.assertEqual(artist.get_linewidths(), [1, 7, 3, 2]) plot.update((1,)) self.assertEqual(artist.get_linewidths(), [3, 8, 2, 3])
def test_dict_dataset_add_dimension_scalar(self): arrays = [{ 'x': np.arange(i, i + 2), 'y': np.arange(i, i + 2) } for i in range(2)] mds = Path(arrays, kdims=['x', 'y'], datatype=[self.datatype ]).add_dimension('A', 0, 'Scalar', True) self.assertIs(mds.interface, self.interface) self.assertEqual( mds, Path([dict(arrays[i], A='Scalar') for i in range(2)], ['x', 'y'], 'A', datatype=['multitabular']))
def test_multi_dict_dataset_add_dimension_scalar(self): arrays = [{ 'x': np.arange(i, i + 2), 'y': np.arange(i, i + 2) } for i in range(2)] mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular' ]).add_dimension('A', 0, 'Scalar', True) for i, ds in enumerate(mds.split()): self.assertEqual( ds, Path(dict(arrays[i], A='Scalar'), ['x', 'y'], 'A', datatype=['dictionary']))
def test_compose_annotators(self): layout1 = annotate(Points([]), annotations=['Label']) layout2 = annotate(Path([]), annotations=['Name']) combined = annotate.compose(layout1, layout2) overlay = combined.DynamicMap.I[()] tables = combined.Annotator.I[()] self.assertIsInstance(overlay, Overlay) self.assertEqual(len(overlay), 2) self.assertEqual(overlay.get(0), Points([], vdims='Label')) self.assertEqual(overlay.get(1), Path([], vdims='Name')) self.assertIsInstance(tables, Overlay) self.assertEqual(len(tables), 3)
def test_array_dataset_add_dimension_scalar(self): arrays = [ np.column_stack([np.arange(i, i + 2), np.arange(i, i + 2)]) for i in range(2) ] mds = Path(arrays, kdims=['x', 'y'], datatype=[self.datatype ]).add_dimension('A', 0, 'Scalar', True) self.assertIs(mds.interface, self.interface) self.assertEqual( mds, Path([{ ('x', 'y'): arrays[i], 'A': 'Scalar' } for i in range(2)], ['x', 'y'], 'A'))
def test_path_continuously_varying_color_op(self): xs = [1, 2, 3, 4] ys = xs[::-1] color = [998, 999, 998, 994] date = np.datetime64(dt.datetime(2018, 8, 1)) data = {'x': xs, 'y': ys, 'color': color, 'date': date} levels = [0, 38, 73, 95, 110, 130, 156, 999] colors = [ '#5ebaff', '#00faf4', '#ffffcc', '#ffe775', '#ffc140', '#ff8f20', '#ff6060' ] path = Path([data], vdims=['color', 'date']).options(color='color', color_levels=levels, cmap=colors, tools=['hover']) plot = bokeh_renderer.get_plot(path) source = plot.handles['source'] cmapper = plot.handles['color_color_mapper'] self.assertEqual( source.data['xs'], [np.array([1, 2]), np.array([2, 3]), np.array([3, 4])]) self.assertEqual( source.data['ys'], [np.array([4, 3]), np.array([3, 2]), np.array([2, 1])]) self.assertEqual(source.data['color'], np.array([998, 999, 998])) self.assertEqual(source.data['date'], np.array([date] * 3)) self.assertEqual(cmapper.low, 994) self.assertEqual(cmapper.high, 999) self.assertEqual(cmapper.palette, colors[-1:])
def test_simple_path(self): path = Path([(0, 0), (1, 1), (0, 1), (0, 0)]) state = self._get_plot_state(path) shapes = state['layout']['shapes'] self.assertEqual(len(shapes), 1) self.assert_path_shape_element(shapes[0], path) self.assert_shape_element_styling(path)
def test_array_values_coordinates_nonexpanded(self): arrays = [np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]) for i in range(2)] mds = Path(arrays, kdims=['x', 'y'], datatype=[self.datatype]) self.assertIs(mds.interface, self.interface) values = mds.dimension_values(0, expanded=False) self.assertEqual(values[0], np.array([0., 1])) self.assertEqual(values[1], np.array([1, 2]))
def test_multi_mixed_dims_raises(self): arrays = [{ 'x': range(10), 'y' if j else 'z': range(10) } for i in range(2) for j in range(2)] with self.assertRaises(DataError): Path(arrays, kdims=['x', 'y'], datatype=['multitabular'])
def test_empty_path_plot(self): path = Path([], vdims=['Intensity']).opts(plot=dict(color_index=2)) plot = bokeh_renderer.get_plot(path) source = plot.handles['source'] self.assertEqual(len(source.data['xs']), 0) self.assertEqual(len(source.data['ys']), 0) self.assertEqual(len(source.data['Intensity']), 0)
def test_path_overlay_hover(self): obj = NdOverlay({i: Path([np.random.rand(10,2)]) for i in range(5)}, kdims=['Test']) opts = {'Path': {'tools': ['hover']}, 'NdOverlay': {'legend_limit': 0}} obj = obj(plot=opts) self._test_hover_info(obj, [('Test', '@{Test}')])
def test_unique_value_dimension_values_not_expanded(self): path = Path([{'x': [1, 2, 3, 4, 5], 'y': [0, 0, 1, 1, 2], 'value': np.full(5, 0)}, {'x': [5, 4, 3, 2, 1], 'y': [2, 2, 1, 1, 0], 'value': np.full(5, 1)}], vdims='value', datatype=[self.datatype]) self.assertIs(path.interface, self.interface) self.assertEqual(path.dimension_values('value', expanded=False), np.array([0, 1]))
def test_multi_array_shape(self): arrays = [ np.column_stack([np.arange(i, i + 2), np.arange(i, i + 2)]) for i in range(2) ] mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular']) self.assertEqual(mds.shape, (5, 2))
def test_mixed_dims_raises(self): arrays = [{ 'x': range(10), 'y' if j else 'z': range(10) } for i in range(2) for j in range(2)] with self.assertRaises(ValueError): Path(arrays, kdims=['x', 'y'], datatype=[self.datatype])
def test_path_colored_by_levels_single_value(self): xs = [1, 2, 3, 4] ys = xs[::-1] color = [998, 998, 998, 998] date = np.datetime64(dt.datetime(2018, 8, 1)) data = {'x': xs, 'y': ys, 'color': color, 'date': date} levels = [0, 38, 73, 95, 110, 130, 156, 999] colors = [ '#5ebaff', '#00faf4', '#ffffcc', '#ffe775', '#ffc140', '#ff8f20', '#ff6060' ] path = Path([data], vdims=['color', 'date']).options(color_index='color', color_levels=levels, cmap=colors) plot = bokeh_renderer.get_plot(path) source = plot.handles['source'] cmapper = plot.handles['color_mapper'] self.assertEqual(source.data['xs'], [np.array([1, 2, 3, 4])]) self.assertEqual(source.data['ys'], [np.array([4, 3, 2, 1])]) self.assertEqual(source.data['color'], np.array([998])) self.assertEqual(source.data['date'], np.array([1533081600000000000])) self.assertEqual(source.data['date_dt_strings'], np.array(['2018-08-01 00:00:00'])) self.assertEqual(cmapper.low, 156) self.assertEqual(cmapper.high, 999) self.assertEqual(cmapper.palette, colors[-1:])
def test_batched_path_line_color_and_color(self): opts = {'NdOverlay': dict(plot=dict(legend_limit=0)), 'Path': dict(style=dict(line_color=Cycle(values=['red', 'blue'])))} overlay = NdOverlay({i: Path([[(i, j) for j in range(2)]]) for i in range(2)}).opts(opts) plot = bokeh_renderer.get_plot(overlay).subplots[()] line_color = ['red', 'blue'] self.assertEqual(plot.handles['source'].data['line_color'], line_color)
def test_multi_array_values_coordinates_nonexpanded(self): arrays = [ np.column_stack([np.arange(i, i + 2), np.arange(i, i + 2)]) for i in range(2) ] mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular']) self.assertEqual(mds.dimension_values(0, expanded=False), np.array([0., 1, 1, 2]))
def test_data_link_list(self): path = Path([[(0, 0, 0), (1, 1, 1), (2, 2, 2)]], vdims='color').options(color='color') table = Table([('A', 1), ('B', 2)], 'A', 'B') DataLink(path, table) layout = path + table plot = bokeh_renderer.get_plot(layout) path_plot, table_plot = (sp.subplots['main'] for sp in plot.subplots.values()) self.assertIs(path_plot.handles['source'], table_plot.handles['source'])
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_path_continuously_varying_alpha_op(self): xs = [1, 2, 3, 4] ys = xs[::-1] alpha = [0.1, 0.7, 0.3, 0.2] data = {'x': xs, 'y': ys, 'alpha': alpha} path = Path([data], vdims='alpha').options(alpha='alpha') with self.assertRaises(Exception): mpl_renderer.get_plot(path)
def test_multi_split(self): arrays = [ np.column_stack([np.arange(i, i + 2), np.arange(i, i + 2)]) for i in range(2) ] mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular']) for arr1, arr2 in zip(mds.split(datatype='array'), arrays): self.assertEqual(arr1, arr2)