|
1 | | -"""Tests for dask+cupy backends: perlin, terrain, crosstab.""" |
| 1 | +"""Tests for dask+cupy backends: perlin, terrain, crosstab, trim, crop.""" |
2 | 2 |
|
3 | 3 | import numpy as np |
4 | 4 | import xarray as xr |
5 | 5 |
|
6 | 6 | from xrspatial import generate_terrain, perlin |
7 | 7 | from xrspatial.tests.general_checks import cuda_and_cupy_available, dask_array_available |
8 | 8 | from xrspatial.utils import has_cuda_and_cupy |
| 9 | +from xrspatial.zonal import crop, trim |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def _make_raster(shape=(50, 50), backend='numpy', chunks=(10, 10)): |
@@ -156,3 +157,108 @@ def test_crosstab_dask_cupy(): |
156 | 157 | np.testing.assert_array_equal( |
157 | 158 | df_numpy_sorted.values, df_computed_sorted.values, |
158 | 159 | ) |
| 160 | + |
| 161 | + |
| 162 | +# ---- trim: dask, cupy, dask+cupy ---- |
| 163 | + |
| 164 | +_TRIM_ARR = np.array([ |
| 165 | + [0, 0, 0, 0], |
| 166 | + [0, 4, 0, 0], |
| 167 | + [0, 4, 4, 0], |
| 168 | + [0, 1, 1, 0], |
| 169 | + [0, 0, 0, 0], |
| 170 | +], dtype=np.int64) |
| 171 | +_TRIM_EXPECTED_SHAPE = (3, 2) |
| 172 | +_TRIM_EXPECTED = np.array([[4, 0], [4, 4], [1, 1]], dtype=np.int64) |
| 173 | + |
| 174 | + |
| 175 | +@dask_array_available |
| 176 | +def test_trim_dask(): |
| 177 | + import dask.array as da |
| 178 | + |
| 179 | + raster = xr.DataArray( |
| 180 | + da.from_array(_TRIM_ARR, chunks=(3, 2)), dims=['y', 'x'], |
| 181 | + ) |
| 182 | + result = trim(raster, values=(0,)) |
| 183 | + assert result.shape == _TRIM_EXPECTED_SHAPE |
| 184 | + np.testing.assert_array_equal(result.data.compute(), _TRIM_EXPECTED) |
| 185 | + |
| 186 | + |
| 187 | +@cuda_and_cupy_available |
| 188 | +def test_trim_cupy(): |
| 189 | + import cupy |
| 190 | + |
| 191 | + raster = xr.DataArray(cupy.asarray(_TRIM_ARR), dims=['y', 'x']) |
| 192 | + result = trim(raster, values=(0,)) |
| 193 | + assert result.shape == _TRIM_EXPECTED_SHAPE |
| 194 | + np.testing.assert_array_equal(result.data.get(), _TRIM_EXPECTED) |
| 195 | + |
| 196 | + |
| 197 | +@cuda_and_cupy_available |
| 198 | +@dask_array_available |
| 199 | +def test_trim_dask_cupy(): |
| 200 | + import cupy |
| 201 | + import dask.array as da |
| 202 | + |
| 203 | + gpu = cupy.asarray(_TRIM_ARR) |
| 204 | + raster = xr.DataArray(da.from_array(gpu, chunks=(3, 2)), dims=['y', 'x']) |
| 205 | + result = trim(raster, values=(0,)) |
| 206 | + assert result.shape == _TRIM_EXPECTED_SHAPE |
| 207 | + computed = result.data.compute() |
| 208 | + assert isinstance(computed, cupy.ndarray) |
| 209 | + np.testing.assert_array_equal(computed.get(), _TRIM_EXPECTED) |
| 210 | + |
| 211 | + |
| 212 | +# ---- crop: dask, cupy, dask+cupy ---- |
| 213 | + |
| 214 | +_CROP_ARR = np.array([ |
| 215 | + [0, 4, 0, 3], |
| 216 | + [0, 4, 4, 3], |
| 217 | + [0, 1, 1, 3], |
| 218 | + [0, 1, 1, 3], |
| 219 | + [0, 0, 0, 0], |
| 220 | +], dtype=np.int64) |
| 221 | +_CROP_EXPECTED_SHAPE = (4, 3) |
| 222 | +_CROP_EXPECTED = np.array([ |
| 223 | + [4, 0, 3], |
| 224 | + [4, 4, 3], |
| 225 | + [1, 1, 3], |
| 226 | + [1, 1, 3], |
| 227 | +], dtype=np.int64) |
| 228 | + |
| 229 | + |
| 230 | +@dask_array_available |
| 231 | +def test_crop_dask(): |
| 232 | + import dask.array as da |
| 233 | + |
| 234 | + raster = xr.DataArray( |
| 235 | + da.from_array(_CROP_ARR, chunks=(3, 2)), dims=['y', 'x'], |
| 236 | + ) |
| 237 | + result = crop(raster, raster, zones_ids=(1, 3)) |
| 238 | + assert result.shape == _CROP_EXPECTED_SHAPE |
| 239 | + np.testing.assert_array_equal(result.data.compute(), _CROP_EXPECTED) |
| 240 | + |
| 241 | + |
| 242 | +@cuda_and_cupy_available |
| 243 | +def test_crop_cupy(): |
| 244 | + import cupy |
| 245 | + |
| 246 | + raster = xr.DataArray(cupy.asarray(_CROP_ARR), dims=['y', 'x']) |
| 247 | + result = crop(raster, raster, zones_ids=(1, 3)) |
| 248 | + assert result.shape == _CROP_EXPECTED_SHAPE |
| 249 | + np.testing.assert_array_equal(result.data.get(), _CROP_EXPECTED) |
| 250 | + |
| 251 | + |
| 252 | +@cuda_and_cupy_available |
| 253 | +@dask_array_available |
| 254 | +def test_crop_dask_cupy(): |
| 255 | + import cupy |
| 256 | + import dask.array as da |
| 257 | + |
| 258 | + gpu = cupy.asarray(_CROP_ARR) |
| 259 | + raster = xr.DataArray(da.from_array(gpu, chunks=(3, 2)), dims=['y', 'x']) |
| 260 | + result = crop(raster, raster, zones_ids=(1, 3)) |
| 261 | + assert result.shape == _CROP_EXPECTED_SHAPE |
| 262 | + computed = result.data.compute() |
| 263 | + assert isinstance(computed, cupy.ndarray) |
| 264 | + np.testing.assert_array_equal(computed.get(), _CROP_EXPECTED) |
0 commit comments