def test_dynamic_collate_layout_with_integer_stream_mapping(self): def callback(x, y): return Image(np.array([[0, 1], [2, 3]])) + Text(0, 0, 'Test') stream = PointerXY() cb_callable = Callable(callback, stream_mapping={0: [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_collate_layout_raise_no_remapping_error(self): def callback(x, y): return Image(np.array([[0, 1], [2, 3]])) + Text(0, 0, 'Test') stream = PointerXY() cb_callable = Callable(callback) dmap = DynamicMap(cb_callable, kdims=[], streams=[stream]) with self.assertRaisesRegexp( ValueError, 'The following streams are set to be automatically linked'): 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 = PointerXY() 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_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 = PointerXY() 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'): dmap.collate()
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 = PointerXY() 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_dynamic_opts_link_inputs(self): stream = LinkedStream() inputs = [DynamicMap(lambda: None, streams=[stream])] dmap = DynamicMap(Callable(lambda X: TestObj(None), inputs=inputs), kdims=['X']).redim.range(X=(0, 10)) styled_dmap = dmap.options(plot_opt1='red', clone=False) opts = Store.lookup_options('backend_1', dmap[0], 'plot') self.assertEqual(opts.options, {'plot_opt1': 'red'}) self.assertIs(styled_dmap, dmap) self.assertTrue(dmap.callback.link_inputs) unstyled_dmap = dmap.callback.inputs[0].callback.inputs[0] opts = Store.lookup_options('backend_1', unstyled_dmap[0], 'plot') self.assertEqual(opts.options, {}) original_dmap = unstyled_dmap.callback.inputs[0] self.assertIs(stream, original_dmap.streams[0])
def test_dynamic_callable_disable_callable_memoize(self): # Disabling Callable.memoize means no memoization is applied, # every access to DynamicMap calls callback and adds sample def history_callback(x, history=deque(maxlen=10)): history.append(x) return Curve(list(history)) x = PointerX() callable_obj = Callable(history_callback, memoize=False) dmap = DynamicMap(callable_obj, kdims=[], streams=[x]) # Add stream subscriber mocking plot x.add_subscriber(lambda **kwargs: dmap[()]) for i in range(2): x.event(x=1) self.assertEqual(dmap[()], Curve([1, 1, 1])) for i in range(2): x.event(x=2) self.assertEqual(dmap[()], Curve([1, 1, 1, 2, 2, 2]))
def test_staticmethod_name(self): self.assertEqual( Callable(CallableClass().somestaticmethod).name, 'somestaticmethod')
def test_parameterized_fn_name(self): self.assertEqual(Callable(ParamFunc).name, 'ParamFunc')
def test_callable_paramfunc(self): self.assertEqual(Callable(ParamFunc)(a=3, b=5), 15)
def test_classmethod_name(self): self.assertEqual( Callable(CallableClass().someclsmethod).name, 'CallableClass.someclsmethod')
def test_callable_paramfunc_instance_argspec(self): self.assertEqual(Callable(ParamFunc.instance()).argspec.args, ['a']) self.assertEqual( Callable(ParamFunc.instance()).argspec.keywords, 'params') self.assertEqual(Callable(ParamFunc.instance()).argspec.varargs, None)
def test_callable_partial_argspec(self): self.assertEqual( Callable(partial(lambda x, y: x + y, x=4)).argspec.args, ['y']) self.assertEqual( Callable(partial(lambda x, y: x + y, x=4)).argspec.keywords, None)
def test_simple_function_name(self): def foo(x, y): pass self.assertEqual(Callable(foo).name, 'foo')
def test_operation_instance_name(self): self.assertEqual(Callable(contours.instance()).name, 'contours')
def test_args_invocation(self): c = Callable(lambda x, y: x + y) c(1, 2) self.assertEqual(c.args, (1, 2))
def test_kwargs_invocation(self): c = Callable(lambda x, y: x + y) c(x=1, y=4) self.assertEqual(c.kwargs, dict(x=1, y=4))
def test_kwargs_none_before_invocation(self): c = Callable(lambda x, y: x + y) self.assertEqual(c.kwargs, None)
def test_callable_mixed_2(self): def mixed_example(a, b, c=10, d=20): return a + b + c + d self.assertEqual(Callable(mixed_example)(3, 5, 5), 33)
def test_callable_mixed_1(self): def mixed_example(a, b, c=10, d=20): return a + b + c + d self.assertEqual(Callable(mixed_example)(a=3, b=5), 38)
def test_parameterized_fn_instance_name(self): self.assertEqual(Callable(ParamFunc.instance()).name, 'ParamFunc')
def test_callable_lambda(self): self.assertEqual(Callable(lambda x, y: x + y)(x=3, y=5), 8)
def test_operation_name(self): self.assertEqual(Callable(contours).name, 'contours')
def test_partial_name(self): py2match = '<functools.partial object' py3match = 'functools.partial(' match = py2match if sys.version_info < (3, 0) else py3match cb = Callable(partial(lambda x, y: x, y=4)) self.assertEqual(cb.name.startswith(match), True)
def test_callable_fn(self): def callback(x): return x self.assertEqual(Callable(callback)(x=3), 3)
def test_callable_class_name(self): self.assertEqual(Callable(CallableClass()).name, 'CallableClass')
def test_callable_class_argspec(self): self.assertEqual(Callable(CallableClass()).argspec.args, []) self.assertEqual(Callable(CallableClass()).argspec.keywords, None) self.assertEqual(Callable(CallableClass()).argspec.varargs, 'testargs')
def test_callable_class_call_method(self): self.assertEqual( Callable(CallableClass().__call__).name, 'CallableClass')
def test_simple_lambda_name(self): self.assertEqual(Callable(lambda x: x).name, '<lambda>')
def test_callable_partial(self): self.assertEqual(Callable(partial(lambda x, y: x + y, x=4))(y=5), 9)