From 6690eac0f415cacfe1ff9a102b3bad940896e9a9 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Fri, 14 Jul 2023 15:52:58 -0700 Subject: [PATCH 1/5] Check hasattr --- holoviews/operation/datashader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/holoviews/operation/datashader.py b/holoviews/operation/datashader.py index 79eaf46b1a..c85c44c777 100644 --- a/holoviews/operation/datashader.py +++ b/holoviews/operation/datashader.py @@ -1388,6 +1388,7 @@ def _process(self, element, key=None): all_supplied_kws = set() instance_params = { k: getattr(self, k) for k in self.__instance_params + if hasattr(self, k) } for predicate, transform in self._transforms: merged_param_values = dict(instance_params, **self.p) From 7208c3e78199c755339d65fa67e7b1ac98681c6a Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Sun, 16 Jul 2023 22:55:13 -0700 Subject: [PATCH 2/5] Add test --- holoviews/tests/operation/test_datashader.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/holoviews/tests/operation/test_datashader.py b/holoviews/tests/operation/test_datashader.py index 795c6f8035..191cf3b1bc 100644 --- a/holoviews/tests/operation/test_datashader.py +++ b/holoviews/tests/operation/test_datashader.py @@ -14,6 +14,7 @@ from holoviews.streams import Tap from holoviews.element.comparison import ComparisonTestCase from numpy import nan +from holoviews.operation import apply_when from packaging.version import Version try: @@ -1169,6 +1170,20 @@ def test_rasterize_image_expand_default(self): output = img.data["z"].to_numpy() assert np.isnan(output).any() + def test_rasterize_apply_when_instance_with_line_width(self): + df = pd.DataFrame( + np.random.multivariate_normal( + (0, 0), [[0.1, 0.1], [0.1, 1.0]], (500000,)) + ) + df.columns = ["a", "b"] + + curve = Curve(df, kdims=["a"], vdims=["b"]) + custom_rasterize = rasterize.instance(line_width=2) + output = apply_when( + curve, operation=custom_rasterize, predicate=lambda x: len(x) > 1000 + ) + assert isinstance(output, Image) + class DatashaderSpreadTests(ComparisonTestCase): def test_spread_rgb_1px(self): From d63a1998678a7491ee3a046783486be740186d5b Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Sun, 16 Jul 2023 23:00:27 -0700 Subject: [PATCH 3/5] Add test --- holoviews/tests/operation/test_datashader.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/holoviews/tests/operation/test_datashader.py b/holoviews/tests/operation/test_datashader.py index 191cf3b1bc..66405bc231 100644 --- a/holoviews/tests/operation/test_datashader.py +++ b/holoviews/tests/operation/test_datashader.py @@ -9,8 +9,9 @@ from holoviews import ( Dimension, Curve, Points, Image, Dataset, RGB, Path, Graph, TriMesh, QuadMesh, NdOverlay, Contours, Spikes, Spread, Area, Rectangles, - Segments, Polygons, Nodes + Segments, Polygons, Nodes, DynamicMap, Overlay ) +from holoviews.util import render from holoviews.streams import Tap from holoviews.element.comparison import ComparisonTestCase from numpy import nan @@ -1182,7 +1183,11 @@ def test_rasterize_apply_when_instance_with_line_width(self): output = apply_when( curve, operation=custom_rasterize, predicate=lambda x: len(x) > 1000 ) - assert isinstance(output, Image) + render(output, "bokeh") + assert isinstance(output, DynamicMap) + overlay = output.items()[0][1] + assert isinstance(overlay, Overlay) + assert len(overlay) == 2 class DatashaderSpreadTests(ComparisonTestCase): From 3103c7cc3fd4585fce6919bcdb20c05b99fafab3 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Mon, 17 Jul 2023 08:33:25 -0400 Subject: [PATCH 4/5] Address comment --- holoviews/tests/operation/test_datashader.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/holoviews/tests/operation/test_datashader.py b/holoviews/tests/operation/test_datashader.py index 66405bc231..fd5d23d157 100644 --- a/holoviews/tests/operation/test_datashader.py +++ b/holoviews/tests/operation/test_datashader.py @@ -1174,14 +1174,15 @@ def test_rasterize_image_expand_default(self): def test_rasterize_apply_when_instance_with_line_width(self): df = pd.DataFrame( np.random.multivariate_normal( - (0, 0), [[0.1, 0.1], [0.1, 1.0]], (500000,)) + (0, 0), [[0.1, 0.1], [0.1, 1.0]], (100,)) ) df.columns = ["a", "b"] curve = Curve(df, kdims=["a"], vdims=["b"]) + # line_width is not a parameter custom_rasterize = rasterize.instance(line_width=2) output = apply_when( - curve, operation=custom_rasterize, predicate=lambda x: len(x) > 1000 + curve, operation=custom_rasterize, predicate=lambda x: len(x) > 10 ) render(output, "bokeh") assert isinstance(output, DynamicMap) From ff26cf6a62951b0e5000c96926146eda3cac6bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8xbro=20Hansen?= Date: Thu, 20 Jul 2023 12:44:20 +0200 Subject: [PATCH 5/5] Save non-parameters for the _process caculation in instance() --- holoviews/operation/datashader.py | 16 ++++++++++------ holoviews/tests/operation/test_datashader.py | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/holoviews/operation/datashader.py b/holoviews/operation/datashader.py index c85c44c777..647efddfc6 100644 --- a/holoviews/operation/datashader.py +++ b/holoviews/operation/datashader.py @@ -1375,21 +1375,25 @@ class rasterize(AggregationOperation): ] __instance_params = set() + __instance_kwargs = {} @bothmethod def instance(self_or_cls, **params): - inst = super().instance(**params) - inst.__instance_params = set(params) + kwargs = set(params) - set(self_or_cls.param) + inst_params = {k: v for k, v in params.items() if k in self_or_cls.param} + inst = super().instance(**inst_params) + inst.__instance_params = set(inst_params) + inst.__instance_kwargs = {k: v for k, v in params.items() if k in kwargs} return inst def _process(self, element, key=None): # Potentially needs traverse to find element types first? all_allowed_kws = set() all_supplied_kws = set() - instance_params = { - k: getattr(self, k) for k in self.__instance_params - if hasattr(self, k) - } + instance_params = dict( + self.__instance_kwargs, + **{k: getattr(self, k) for k in self.__instance_params} + ) for predicate, transform in self._transforms: merged_param_values = dict(instance_params, **self.p) diff --git a/holoviews/tests/operation/test_datashader.py b/holoviews/tests/operation/test_datashader.py index fd5d23d157..7d85920ebf 100644 --- a/holoviews/tests/operation/test_datashader.py +++ b/holoviews/tests/operation/test_datashader.py @@ -1181,6 +1181,7 @@ def test_rasterize_apply_when_instance_with_line_width(self): curve = Curve(df, kdims=["a"], vdims=["b"]) # line_width is not a parameter custom_rasterize = rasterize.instance(line_width=2) + assert {'line_width': 2} == custom_rasterize._rasterize__instance_kwargs output = apply_when( curve, operation=custom_rasterize, predicate=lambda x: len(x) > 10 )