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

WIP: Ensure rectilinear rasters support geometry selections #5414

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ def values(cls, dataset, dim, expanded=True, flat=True, compute=True, keep_index
return data.T.flatten() if flat and not keep_index else data
elif expanded:
data = cls.coords(dataset, dim.name, expanded=True)
if keep_index:
da = dataset.data[dataset.vdims[0].name].copy().rename(dim.name)
da.data[:] = data
return da
Copy link
Member Author

@philippjfr philippjfr Sep 1, 2022

Choose a reason for hiding this comment

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

Need to find a better solution here. This basically ensures that keep_index + expanded returns a 2D coordinate DataArray even for a rectilinear (i.e. 1D coordinate) array. This implementation does not handle:

  • packed vdims
  • dask backed value arrays

return data.T.flatten() if flat else data
else:
if keep_index:
Expand Down
4 changes: 3 additions & 1 deletion holoviews/element/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def aggregate(self, dimensions=None, function=None, spreadfn=None, **kwargs):
agg = super().aggregate(dimensions, function, spreadfn, **kwargs)
return Curve(agg) if isinstance(agg, Dataset) and len(self.vdims) == 1 else agg

def select(self, selection_specs=None, **selection):
def select(self, selection_expr=None, selection_specs=None, **selection):
"""
Allows selecting data by the slices, sets and scalar values
along a particular dimension. The indices should be supplied as
Expand All @@ -413,6 +413,8 @@ def select(self, selection_specs=None, **selection):
supplied, which will ensure the selection is only applied if the
specs match the selected object.
"""
if selection_expr is not None:
return super().select(selection_expr, selection_specs, **selection)
if selection_specs and not any(self.matches(sp) for sp in selection_specs):
return self

Expand Down
2 changes: 1 addition & 1 deletion holoviews/element/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def spatial_select_gridded(xvals, yvals, geometry):
target = Image((xs, ys, np.empty(ys.shape+xs.shape)))
poly = Polygons([geometry])
mask = rasterize(poly, target=target, dynamic=False, aggregator='any')
return mask.dimension_values(2, flat=False)
return mask.interface.values(mask, mask.vdims[0], keep_index=True)
else:
mask = spatial_select_columnar(xvals.flatten(), yvals.flatten(), geometry)
return mask.reshape(xvals.shape)
Copy link
Member Author

Choose a reason for hiding this comment

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

This branch should also ensure that the mask that is returned is still an xarray object if the input is an xarray object.

Expand Down