Skip to content

Commit

Permalink
Add tests for downsample (#1157)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Høxbro Hansen <simon.hansen@me.com>
  • Loading branch information
maximlt and hoxbro authored Oct 6, 2023
1 parent 0a798c8 commit 5eb8dfe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/user_guide/Timeseries_Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
"metadata": {},
"source": [
"### Downsample time series\n",
"\n",
"*(Available with HoloViews >= 1.16)*\n",
"\n",
"An option when working with large time series is to downsample the data before plotting it. This can be done with `downsample=True`, which applies the `lttb` (Largest Triangle Three Buckets) algorithm to the data."
]
},
Expand Down
7 changes: 5 additions & 2 deletions hvplot/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class HoloViewsConverter:
downsample (default=False):
Whether to apply LTTB (Largest Triangle Three Buckets)
downsampling to the element (note this is only well behaved for
timeseries data).
timeseries data). Requires HoloViews >= 1.16.
dynspread (default=False):
For plots generated with datashade=True or rasterize=True,
automatically increase the point size when the data is sparse
Expand Down Expand Up @@ -1280,7 +1280,10 @@ def method_wrapper(ds, x, y):
opts['height'] = self._plot_opts['height']

if self.downsample:
from holoviews.operation.downsample import downsample1d
try:
from holoviews.operation.downsample import downsample1d
except ImportError:
raise ImportError('Downsampling requires HoloViews >=1.16')

if self.x_sampling:
opts['x_sampling'] = self.x_sampling
Expand Down
22 changes: 22 additions & 0 deletions hvplot/tests/testoperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,25 @@ def test_plot_resolution_with_rasterize(self, kind, element):
x_sampling=5, y_sampling=2)
assert all(plot.data.x.diff('x').round(0) == 5)
assert all(plot.data.y.diff('y').round(0) == 2)


class TestDownsample(ComparisonTestCase):
def setUp(self):
import hvplot.pandas # noqa
self.df = pd.DataFrame(np.random.random(100))

def test_downsample_default(self):
from holoviews.operation.downsample import downsample1d

plot = self.df.hvplot.line(downsample=True)

assert isinstance(plot.callback.operation, downsample1d)
assert plot.callback.operation.algorithm == "lttb"

def test_downsample_opts(self):
plot = self.df.hvplot.line(downsample=True, width=100, height=50, x_sampling=5, xlim=(0, 5))

assert plot.callback.operation.p.width == 100
assert plot.callback.operation.p.height == 50
assert plot.callback.operation.p.x_sampling == 5
assert plot.callback.operation.p.x_range == (0, 5)

0 comments on commit 5eb8dfe

Please sign in to comment.