import datetime as dt from unittest import SkipTest import numpy as np from holoviews.core import NdOverlay from holoviews.core.options import Cycle from holoviews.core.util import pd from holoviews.element import Points from holoviews.streams import Stream from .testplot import TestBokehPlot, bokeh_renderer from ..utils import ParamLogStream try: from bokeh.models import FactorRange, LinearColorMapper, CategoricalColorMapper from bokeh.models import Scatter except: pass class TestPointPlot(TestBokehPlot): def test_points_colormapping(self): points = Points(np.random.rand(10, 4), vdims=['a', 'b']).opts(plot=dict(color_index=3)) self._test_colormapping(points, 3) def test_points_colormapping_with_nonselection(self): opts = dict(plot=dict(color_index=3), style=dict(nonselection_color='red')) points = Points(np.random.rand(10, 4), vdims=['a', 'b']).opts(**opts) self._test_colormapping(points, 3) def test_points_colormapping_categorical(self): points = Points([(i, i*2, i*3, chr(65+i)) for i in range(10)], vdims=['a', 'b']).opts(plot=dict(color_index='b')) plot = bokeh_renderer.get_plot(points) plot.initialize_plot() cmapper = plot.handles['color_mapper'] self.assertIsInstance(cmapper, CategoricalColorMapper) self.assertEqual(cmapper.factors, list(points['b'])) def test_points_color_selection_nonselection(self): opts = dict(color='green', selection_color='red', nonselection_color='blue') points = Points([(i, i*2, i*3, chr(65+i)) for i in range(10)], vdims=['a', 'b']).opts(style=opts) plot = bokeh_renderer.get_plot(points) glyph_renderer = plot.handles['glyph_renderer'] self.assertEqual(glyph_renderer.glyph.fill_color, 'green') self.assertEqual(glyph_renderer.glyph.line_color, 'green') self.assertEqual(glyph_renderer.selection_glyph.fill_color, 'red') self.assertEqual(glyph_renderer.selection_glyph.line_color, 'red') self.assertEqual(glyph_renderer.nonselection_glyph.fill_color, 'blue') self.assertEqual(glyph_renderer.nonselection_glyph.line_color, 'blue') def test_points_alpha_selection_nonselection(self): opts = dict(alpha=0.8, selection_alpha=1.0, nonselection_alpha=0.2) points = Points([(i, i*2, i*3, chr(65+i)) for i in range(10)], vdims=['a', 'b']).opts(style=opts) plot = bokeh_renderer.get_plot(points) glyph_renderer = plot.handles['glyph_renderer'] self.assertEqual(glyph_renderer.glyph.fill_alpha, 0.8) self.assertEqual(glyph_renderer.glyph.line_alpha, 0.8) self.assertEqual(glyph_renderer.selection_glyph.fill_alpha, 1) self.assertEqual(glyph_renderer.selection_glyph.line_alpha, 1) self.assertEqual(glyph_renderer.nonselection_glyph.fill_alpha, 0.2) self.assertEqual(glyph_renderer.nonselection_glyph.line_alpha, 0.2) def test_points_alpha_selection_partial(self): opts = dict(selection_alpha=1.0, selection_fill_alpha=0.2) points = Points([(i, i*2, i*3, chr(65+i)) for i in range(10)], vdims=['a', 'b']).opts(style=opts) plot = bokeh_renderer.get_plot(points) glyph_renderer = plot.handles['glyph_renderer'] self.assertEqual(glyph_renderer.glyph.fill_alpha, 1.0) self.assertEqual(glyph_renderer.glyph.line_alpha, 1.0) self.assertEqual(glyph_renderer.selection_glyph.fill_alpha, 0.2) self.assertEqual(glyph_renderer.selection_glyph.line_alpha, 1) def test_batched_points(self): overlay = NdOverlay({i: Points(np.arange(i)) for i in range(1, 100)}) plot = bokeh_renderer.get_plot(overlay) extents = plot.get_extents(overlay, {}) self.assertEqual(extents, (0, 0, 98, 98)) def test_batched_points_size_and_color(self): opts = {'NdOverlay': dict(plot=dict(legend_limit=0)), 'Points': dict(style=dict(size=Cycle(values=[1, 2])))} overlay = NdOverlay({i: Points([(i, j) for j in range(2)]) for i in range(2)}).opts(opts) plot = bokeh_renderer.get_plot(overlay).subplots[()] size = np.array([1, 1, 2, 2]) color = np.array(['#30a2da', '#30a2da', '#fc4f30', '#fc4f30'], dtype='