Skip to content

Commit

Permalink
Extract compute mask from _process and make it an options
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Jan 30, 2024
1 parent 2a64d0e commit d6b654f
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions holoviews/operation/downsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ class downsample1d(ResampleOperation1D):
values to generate with the minmax algorithm before further
downsampling with LTTB.""")

neighbor_points = param.Boolean(default=None, doc="""
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):
if isinstance(element, (Overlay, NdOverlay)):
_process = partial(self._process, key=key)
Expand All @@ -238,15 +242,7 @@ def _process(self, element, key=None):
return element.clone(elements)

if self.p.x_range:
try:
mask = element.dataset.interface._select_mask_neighbor(
element.dataset, {element.kdims[0]: self.p.x_range}
)
except NotImplementedError:
mask = slice(*self.p.x_range)
except Exception as e:
self.param.warning(f"Could not apply neighbor mask to downsample1d: {e}")
mask = slice(*self.p.x_range)
mask = self._compute_mask(element)
element = element[mask]
if len(element) <= self.p.width:
return element
Expand All @@ -263,3 +259,25 @@ def _process(self, element, key=None):
kwargs['minmax_ratio'] = self.p.minmax_ratio
samples = downsample(xs, ys, self.p.width, parallel=self.p.parallel, **kwargs)
return element.iloc[samples]

def _compute_mask(self, element):
"""
Computes the mask to apply to the element before downsampling.
"""
neighbor_enabled = (
self.p.neighbor_points
if self.p.neighbor_points is not None
else self.p.algorithm == "viewport"
)
if not neighbor_enabled:
return slice(*self.p.x_range)
try:
mask = element.dataset.interface._select_mask_neighbor(
element.dataset, {element.kdims[0]: self.p.x_range}
)
except NotImplementedError:
mask = slice(*self.p.x_range)
except Exception as e:
self.param.warning(f"Could not apply neighbor mask to downsample1d: {e}")
mask = slice(*self.p.x_range)
return mask

0 comments on commit d6b654f

Please sign in to comment.