Skip to content

Commit

Permalink
Update cuspatial
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed May 6, 2024
1 parent a13df21 commit ea7522a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
46 changes: 36 additions & 10 deletions holoviews/element/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,38 @@ def spatial_select_gridded(xvals, yvals, geometry):
sel_mask = spatial_select_columnar(xvals.flatten(), yvals.flatten(), geometry)
return sel_mask.reshape(xvals.shape)

def _cuspatial_old(xvals, yvals, geometry):
import cudf
import cuspatial

result = cuspatial.point_in_polygon(
xvals,
yvals,
cudf.Series([0], index=["selection"]),
[0],
geometry[:, 0],
geometry[:, 1],
)
return result.values


def _cuspatial_new(xvals, yvals, geometry):
import cudf
import cuspatial
import geopandas
from shapely.geometry import Polygon

df = cudf.DataFrame({'x':xvals, 'y':yvals})
points = cuspatial.GeoSeries.from_points_xy(
df.interleave_columns().astype('float')
)
polygons = cuspatial.GeoSeries(
geopandas.GeoSeries(Polygon(geometry)), index=["selection"]
)
result = cuspatial.point_in_polygon(points,polygons)
return result.values.ravel()


def spatial_select_columnar(xvals, yvals, geometry, geom_method=None):
if 'cudf' in sys.modules:
import cudf
Expand All @@ -88,16 +120,10 @@ def spatial_select_columnar(xvals, yvals, geometry, geom_method=None):
xvals = xvals.values.astype('float')
yvals = yvals.values.astype('float')
try:
import cuspatial
result = cuspatial.point_in_polygon( # TODO: now only takes two arguments
xvals,
yvals,
cudf.Series([0], index=["selection"]),
[0],
geometry[:, 0],
geometry[:, 1],
)
return result.values
try:
return _cuspatial_old(xvals, yvals, geometry)
except TypeError:
return _cuspatial_new(xvals, yvals, geometry)
except ImportError:
xvals = cp.asnumpy(xvals)
yvals = cp.asnumpy(yvals)
Expand Down
1 change: 0 additions & 1 deletion holoviews/tests/element/test_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,6 @@ def test_cudf(self, geometry, pt_mask, pandas_df, _method, unimport):
mask = spatial_select_columnar(df.x, df.y, geometry, _method)
assert np.array_equal(cp.asnumpy(mask), pt_mask)

@pytest.mark.xfail(reason='cuspatial.point_in_polygon API has changed')
@pytest.mark.gpu
def test_cuspatial(self, geometry, pt_mask, pandas_df, _method):
import cudf
Expand Down

0 comments on commit ea7522a

Please sign in to comment.