Skip to content

Commit dad5abb

Browse files
authored
focal apply: drop gpu support (#706)
* focal apply: drop gpu support * flake8
1 parent 7d9b339 commit dad5abb

File tree

5 files changed

+8
-48
lines changed

5 files changed

+8
-48
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ In the GIS world, rasters are used for representing continuous phenomena (e.g. e
9393

9494
| Name | NumPy xr.DataArray | Dask xr.DataArray | CuPy GPU xr.DataArray | Dask GPU xr.DataArray |
9595
|:----------:|:----------------------:|:--------------------:|:-------------------:|:------:|
96-
| [Apply](xrspatial/focal.py) | ✅️ | ✅️ | ✅️ | |
96+
| [Apply](xrspatial/focal.py) | ✅️ | ✅️ | | |
9797
| [Hotspots](xrspatial/focal.py) | ✅️ | ✅️ | ✅️ | |
9898
| [Mean](xrspatial/focal.py) | ✅️ | ✅️ | ✅️ | |
9999
| [Focal Statistics](xrspatial/focal.py) | ✅️ | ✅️ | ✅️ | |

benchmarks/benchmarks/focal.py

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def setup(self, nx, kernelsize, type):
1818

1919

2020
class FocalApply(Focal):
21+
params = ([100, 300, 1000, 3000], [(5, 5), (25, 25)], ["numpy"])
22+
2123
def time_apply(self, nx, kernelsize, type):
2224
apply(self.agg, self.kernel)
2325

xrspatial/classify.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,7 @@ def natural_breaks(agg: xr.DataArray,
630630
Parameters
631631
----------
632632
agg : xarray.DataArray
633-
2D NumPy, CuPy, NumPy-backed Dask, or Cupy-backed Dask array
634-
of values to be reclassified.
633+
2D NumPy DataArray of values to be reclassified.
635634
num_sample : int, default=20000
636635
Number of sample data points used to fit the model.
637636
Natural Breaks (Jenks) classification is indeed O(n²) complexity,

xrspatial/focal.py

+4-30
Original file line numberDiff line numberDiff line change
@@ -287,33 +287,6 @@ def _apply_dask_numpy(data, kernel, func):
287287
return out
288288

289289

290-
def _apply_cupy(data, kernel, func):
291-
data = data.astype(cupy.float32)
292-
kernel = cupy.asarray(kernel)
293-
294-
out = cupy.zeros(data.shape, dtype=data.dtype)
295-
out[:] = cupy.nan
296-
297-
rows, cols = data.shape
298-
krows, kcols = kernel.shape
299-
hrows, hcols = int(krows / 2), int(kcols / 2)
300-
kernel_values = cupy.zeros_like(kernel, dtype=data.dtype)
301-
302-
for y in prange(rows):
303-
for x in prange(cols):
304-
# kernel values are all nans at the beginning of each step
305-
kernel_values.fill(np.nan)
306-
for ky in range(y - hrows, y + hrows + 1):
307-
for kx in range(x - hcols, x + hcols + 1):
308-
if ky >= 0 and ky < rows and kx >= 0 and kx < cols:
309-
kyidx, kxidx = ky - (y - hrows), kx - (x - hcols)
310-
if kernel[kyidx, kxidx] == 1:
311-
kernel_values[kyidx, kxidx] = data[ky, kx]
312-
out[y, x] = func(kernel_values)
313-
314-
return out
315-
316-
317290
def apply(raster, kernel, func=_calc_mean, name='focal_apply'):
318291
"""
319292
Returns custom function applied array using a user-created window.
@@ -322,7 +295,7 @@ def apply(raster, kernel, func=_calc_mean, name='focal_apply'):
322295
----------
323296
raster : xarray.DataArray
324297
2D array of input values to be filtered. Can be a NumPy backed,
325-
CuPy backed, or Dask with NumPy backed DataArray.
298+
or Dask with NumPy backed DataArray.
326299
kernel : numpy.ndarray
327300
2D array where values of 1 indicate the kernel.
328301
func : callable, default=xrspatial.focal._calc_mean
@@ -432,10 +405,11 @@ def apply(raster, kernel, func=_calc_mean, name='focal_apply'):
432405
# the function func must be a @ngjit
433406
mapper = ArrayTypeFunctionMapping(
434407
numpy_func=_apply_numpy,
435-
cupy_func=_apply_cupy,
408+
cupy_func=lambda *args: not_implemented_func(
409+
*args, messages='apply() does not support cupy backed DataArray.'),
436410
dask_func=_apply_dask_numpy,
437411
dask_cupy_func=lambda *args: not_implemented_func(
438-
*args, messages='apply() does not support dask with cupy backed DataArray.'), # noqa
412+
*args, messages='apply() does not support dask with cupy backed DataArray.'),
439413
)
440414
out = mapper(raster)(raster.data, kernel, func)
441415
result = DataArray(out,

xrspatial/tests/test_focal.py

-15
Original file line numberDiff line numberDiff line change
@@ -319,21 +319,6 @@ def test_apply_dask_numpy(data_apply):
319319
general_output_checks(dask_numpy_agg, dask_numpy_apply, expected_result)
320320

321321

322-
@cuda_and_cupy_available
323-
def test_apply_gpu(data_apply):
324-
data, kernel, expected_result = data_apply
325-
# cupy case
326-
cupy_agg = create_test_raster(data, backend='cupy')
327-
cupy_apply = apply(cupy_agg, kernel, func_zero)
328-
general_output_checks(cupy_agg, cupy_apply, expected_result)
329-
330-
# dask + cupy case not implemented
331-
dask_cupy_agg = create_test_raster(data, backend='dask+cupy')
332-
with pytest.raises(NotImplementedError) as e_info:
333-
apply(dask_cupy_agg, kernel, func_zero)
334-
assert e_info
335-
336-
337322
@pytest.fixture
338323
def data_focal_stats():
339324
data = np.arange(16).reshape(4, 4)

0 commit comments

Comments
 (0)