Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize downsample1d when data is shared between layers #6075

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions holoviews/operation/downsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,27 @@ class downsample1d(ResampleOperation1D):
Whether to add the neighbor points to the range before downsampling.
By default this is only enabled for the viewport algorithm.""")

def _process(self, element, key=None):
def _process(self, element, key=None, shared_data=None):
if isinstance(element, (Overlay, NdOverlay)):
_process = partial(self._process, key=key)
# Shared data is so we only slice the given data once
kwargs = {'key': key, 'shared_data': {}}
_process = partial(self._process, **kwargs)
if isinstance(element, Overlay):
elements = [v.map(_process) for v in element]
else:
elements = {k: v.map(_process) for k, v in element.items()}
return element.clone(elements)

if self.p.x_range:
mask = self._compute_mask(element)
element = element[mask]
key = (id(element.data), str(element.kdims[0]))
if shared_data is not None and key in shared_data:
element = element.clone(shared_data[key])
else:
mask = self._compute_mask(element)
element = element[mask]
if shared_data is not None:
shared_data[key] = element.data

if len(element) <= self.p.width:
return element
xs, ys = (element.dimension_values(i) for i in range(2))
Expand Down
44 changes: 43 additions & 1 deletion holoviews/tests/operation/test_downsample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
import pytest

import holoviews as hv
Expand All @@ -10,7 +11,8 @@
tsdownsample = None

algorithms = _ALGORITHMS.copy()
algorithms.pop('viewport', None) # viewport return slice(len(data)) no matter the width
algorithms.pop("viewport", None) # viewport return slice(len(data)) no matter the width


@pytest.mark.parametrize("plottype", ["overlay", "ndoverlay"])
def test_downsample1d_multi(plottype):
Expand All @@ -28,6 +30,46 @@ def test_downsample1d_multi(plottype):
assert value.size == downsample1d.width


def test_downsample1d_shared_data():
runs = [0]

class mocksample(downsample1d):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would have used unittest.mock.patch on the Interface but this is fine.

def _compute_mask(self, element):
# Use _compute_mask as this should only be called once
# and then it should be cloned.
runs[0] += 1
return super()._compute_mask(element)

N = 1000
df = pd.DataFrame({c: range(N) for c in "xyz"})
figure = hv.Overlay([hv.Curve(df, kdims="x", vdims=c) for c in "yz"])

# We set x_range to trigger _compute_mask
mocksample(figure, dynamic=False, x_range=(0, 500))
assert runs[0] == 1


# Should be fixed when https://github.com/holoviz/holoviews/pull/6061 is merged
@pytest.mark.xfail(reason="This will make a copy of the data")
def test_downsample1d_shared_data_index():
runs = [0]

class mocksample(downsample1d):
def _compute_mask(self, element):
# Use _compute_mask as this should only be called once
# and then it should be cloned.
runs[0] += 1
return super()._compute_mask(element)

N = 1000
df = pd.DataFrame({c: range(N) for c in "xyz"})
figure = hv.Overlay([hv.Curve(df, kdims="index", vdims=c) for c in "xyz"])

# We set x_range to trigger _compute_mask
mocksample(figure, dynamic=False, x_range=(0, 500))
assert runs[0] == 1


@pytest.mark.parametrize("algorithm", algorithms.values(), ids=algorithms)
def test_downsample_algorithm(algorithm, unimport):
unimport("tsdownsample")
Expand Down
Loading