def test_dynamic_overlay_memoization(self): """Tests that Callable memoizes unchanged callbacks""" def fn(x, y): return Scatter([(x, y)]) dmap = DynamicMap(fn, kdims=[], streams=[PositionXY()]) counter = [0] def fn2(x, y): counter[0] += 1 return Image(np.random.rand(10, 10)) dmap2 = DynamicMap(fn2, kdims=[], streams=[PositionXY()]) overlaid = dmap * dmap2 overlay = overlaid[()] self.assertEqual(overlay.Scatter.I, fn(0, 0)) dmap.event(x=1, y=2) overlay = overlaid[()] # Ensure dmap return value was updated self.assertEqual(overlay.Scatter.I, fn(1, 2)) # Ensure dmap2 callback was called only once self.assertEqual(counter[0], 1)
def test_subscribers(self): subscriber1 = TestSubscriber() subscriber2 = TestSubscriber() position = PositionXY(subscribers=[subscriber1, subscriber2]) kwargs = dict(x=3, y=4) position.update(**kwargs) self.assertEqual(subscriber1.kwargs, kwargs) self.assertEqual(subscriber2.kwargs, kwargs)
def test_dynamic_streams_refresh(self): stream = PositionXY() dmap = DynamicMap(lambda x, y: Points([(x, y)]), kdims=[], streams=[stream]) plot = mpl_renderer.get_plot(dmap) plot.initialize_plot() pre = mpl_renderer(plot, fmt='png') stream.update(x=1, y=1) plot.refresh() post = mpl_renderer(plot, fmt='png') self.assertNotEqual(pre, post)
def test_dynamic_event_renaming_valid(self): def fn(x, y): return Scatter([(x, y)]) xy = PositionXY(rename={'x': 'x1', 'y': 'y1'}) dmap = DynamicMap(fn, kdims=[], streams=[xy]) dmap.event(x1=1, y1=2)
def test_stream_callback(self): dmap = DynamicMap(lambda x, y: Points([(x, y)]), kdims=[], streams=[PositionXY()]) plot = bokeh_renderer.get_plot(dmap) bokeh_renderer(plot) plot.callbacks[0].on_msg({"x": 10, "y": -10}) data = plot.handles['source'].data self.assertEqual(data['x'], np.array([10])) self.assertEqual(data['y'], np.array([-10]))
def test_dynamic_collate_layout_with_spec_stream_mapping(self): def callback(x, y): return Image(np.array([[0, 1], [2, 3]])) + Text(0, 0, 'Test') stream = PositionXY() cb_callable = Callable(callback, stream_mapping={'Image': [stream]}) dmap = DynamicMap(cb_callable, kdims=[], streams=[stream]) layout = dmap.collate() self.assertEqual(list(layout.keys()), [('Image', 'I'), ('Text', 'I')]) self.assertIs(stream.source, layout.Image.I)
def test_dynamic_event_renaming_invalid(self): def fn(x, y): return Scatter([(x, y)]) xy = PositionXY(rename={'x': 'x1', 'y': 'y1'}) dmap = DynamicMap(fn, kdims=[], streams=[xy]) regexp = '(.+?)does not correspond to any stream parameter' with self.assertRaisesRegexp(KeyError, regexp): dmap.event(x=1, y=2)
def test_dynamic_collate_layout_raise_no_remapping_error(self): def callback(x, y): return Image(np.array([[0, 1], [2, 3]])) + Text(0, 0, 'Test') stream = PositionXY() cb_callable = Callable(callback) dmap = DynamicMap(cb_callable, kdims=[], streams=[stream]) with self.assertRaisesRegexp( ValueError, 'The following streams are set to be automatically linked'): layout = dmap.collate()
def test_dynamic_collate_layout_raise_ambiguous_remapping_error(self): def callback(x, y): return Image(np.array([[0, 1], [2, 3]])) + Image( np.array([[0, 1], [2, 3]])) stream = PositionXY() cb_callable = Callable(callback, stream_mapping={'Image': [stream]}) dmap = DynamicMap(cb_callable, kdims=[], streams=[stream]) with self.assertRaisesRegexp( ValueError, 'The stream_mapping supplied on the Callable is ambiguous'): layout = dmap.collate()
def test_dynamic_collate_grid_with_key_stream_mapping(self): def callback(): return GridSpace({(i, j): Image(np.array([[i, j], [2, 3]])) for i in range(1, 3) for j in range(1, 3)}) stream = PositionXY() cb_callable = Callable(callback, stream_mapping={(1, 2): [stream]}) dmap = DynamicMap(cb_callable, kdims=[]) grid = dmap.collate() self.assertEqual(list(grid.keys()), [(i, j) for i in range(1, 3) for j in range(1, 3)]) self.assertEqual(stream.source, grid[(1, 2)])
def test_dynamic_collate_ndlayout_with_key_stream_mapping(self): def callback(x, y): return NdLayout( {i: Image(np.array([[i, 1], [2, 3]])) for i in range(1, 3)}) stream = PositionXY() cb_callable = Callable(callback, stream_mapping={(1, ): [stream]}) dmap = DynamicMap(cb_callable, kdims=[], streams=[stream]) layout = dmap.collate() self.assertEqual(list(layout.keys()), [1, 2]) self.assertIs(stream.source, layout[1])
def test_stream_callback_with_ids(self): dmap = DynamicMap(lambda x, y: Points([(x, y)]), kdims=[], streams=[PositionXY()]) plot = bokeh_renderer.get_plot(dmap) bokeh_renderer(plot) hover = plot.state.select(type=HoverTool)[0] plot.callbacks[0].on_msg({ "x": { 'id': hover.ref['id'], 'value': 10 }, "y": { 'id': hover.ref['id'], 'value': -10 } }) data = plot.handles['source'].data self.assertEqual(data['x'], np.array([10])) self.assertEqual(data['y'], np.array([-10]))
def test_positionXY_update_contents(self): position = PositionXY() position.update(x=5, y=10) self.assertEqual(position.contents, dict(x=5, y=10))
def test_no_streams_two_stream_substitution(self): result = wrap_tuple_streams( (None, None), [Dimension('x'), Dimension('y')], [PositionXY(x=0, y=5)]) self.assertEqual(result, (0, 5))
def test_no_streams_one_stream_substitution(self): result = wrap_tuple_streams( (None, 3), [Dimension('x'), Dimension('y')], [PositionXY(x=-5, y=10)]) self.assertEqual(result, (-5, 3))
def test_subscriber_disabled(self): subscriber = TestSubscriber() position = PositionXY(subscribers=[subscriber]) kwargs = dict(x=3, y=4) position.update(trigger=False, **kwargs) self.assertEqual(subscriber.kwargs, None)
def test_exception_subscriber(self): subscriber = TestSubscriber() position = PositionXY(subscribers=[subscriber]) kwargs = dict(x=3, y=4) position.update(**kwargs) self.assertEqual(subscriber.kwargs, kwargs)
def test_positionXY_init_contents(self): position = PositionXY(x=1, y=3) self.assertEqual(position.contents, dict(x=1, y=3))
def test_group_preprocessor(self): position = PositionXY([Group('mygroup')], x=1, y=3) self.assertEqual(position.contents, dict(mygroup={'x': 1, 'y': 3}))
def test_rename_preprocessor(self): position = PositionXY([Rename(x='x1', y='y1')], x=1, y=3) self.assertEqual(position.contents, dict(x1=1, y1=3))