|
1 | | -import dask.array as da |
2 | 1 | import numpy as np |
3 | 2 | import pytest |
4 | | -import xarray as xr |
5 | 3 |
|
6 | 4 | from xrspatial import aspect |
7 | 5 | from xrspatial.utils import doesnt_have_cuda |
8 | 6 |
|
| 7 | +from xrspatial.tests.general_checks import create_test_raster |
| 8 | +from xrspatial.tests.general_checks import assert_numpy_equals_dask_numpy |
| 9 | +from xrspatial.tests.general_checks import assert_numpy_equals_cupy |
| 10 | +from xrspatial.tests.general_checks import assert_nan_edges_effect |
9 | 11 | from xrspatial.tests.general_checks import general_output_checks |
10 | 12 |
|
11 | 13 |
|
12 | | -INPUT_DATA = np.asarray([ |
13 | | - [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], |
14 | | - [1584.8767, 1584.8767, 1585.0546, 1585.2324, 1585.2324, 1585.2324], |
15 | | - [1585.0546, 1585.0546, 1585.2324, 1585.588, 1585.588, 1585.588], |
16 | | - [1585.2324, 1585.4102, 1585.588, 1585.588, 1585.588, 1585.588], |
17 | | - [1585.588, 1585.588, 1585.7659, 1585.7659, 1585.7659, 1585.7659], |
18 | | - [1585.7659, 1585.9437, 1585.7659, 1585.7659, 1585.7659, 1585.7659], |
19 | | - [1585.9437, 1585.9437, 1585.9437, 1585.7659, 1585.7659, 1585.7659]], |
20 | | - dtype=np.float32 |
21 | | -) |
22 | | - |
23 | | -QGIS_OUTPUT = np.asarray([ |
24 | | - [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], |
25 | | - [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], |
26 | | - [330.94687, 335.55496, 320.70786, 330.94464, 0., 0.], |
27 | | - [333.43494, 333.43494, 329.03394, 341.56897, 0., 18.434948], |
28 | | - [338.9621, 338.20062, 341.56506, 0., 0., 45.], |
29 | | - [341.56506, 351.8699, 26.56505, 45., -1., 90.], |
30 | | - [351.86676, 11.306906, 45., 45., 45., 108.431015]], dtype=np.float32 |
31 | | -) |
32 | | - |
| 14 | +def input_data(backend='numpy'): |
| 15 | + data = np.asarray([ |
| 16 | + [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], |
| 17 | + [1584.8767, 1584.8767, 1585.0546, 1585.2324, 1585.2324, 1585.2324], |
| 18 | + [1585.0546, 1585.0546, 1585.2324, 1585.588, 1585.588, 1585.588], |
| 19 | + [1585.2324, 1585.4102, 1585.588, 1585.588, 1585.588, 1585.588], |
| 20 | + [1585.588, 1585.588, 1585.7659, 1585.7659, 1585.7659, 1585.7659], |
| 21 | + [1585.7659, 1585.9437, 1585.7659, 1585.7659, 1585.7659, 1585.7659], |
| 22 | + [1585.9437, 1585.9437, 1585.9437, 1585.7659, 1585.7659, 1585.7659]], |
| 23 | + dtype=np.float32 |
| 24 | + ) |
| 25 | + raster = create_test_raster(data, backend, attrs={'res': (10.0, 10.0)}) |
| 26 | + return raster |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def qgis_output(): |
| 31 | + result = np.array([ |
| 32 | + [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], |
| 33 | + [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], |
| 34 | + [330.94687, 335.55496, 320.70786, 330.94464, 0., 0.], |
| 35 | + [333.43494, 333.43494, 329.03394, 341.56897, 0., 18.434948], |
| 36 | + [338.9621, 338.20062, 341.56506, 0., 0., 45.], |
| 37 | + [341.56506, 351.8699, 26.56505, 45., -1., 90.], |
| 38 | + [351.86676, 11.306906, 45., 45., 45., 108.431015]], dtype=np.float32 |
| 39 | + ) |
| 40 | + return result |
33 | 41 |
|
34 | | -def test_numpy_equals_qgis(): |
35 | 42 |
|
36 | | - small_da = xr.DataArray(INPUT_DATA, attrs={'res': (10.0, 10.0)}) |
37 | | - xrspatial_aspect = aspect(small_da, name='numpy_aspect') |
| 43 | +def test_numpy_equals_qgis(qgis_output): |
| 44 | + numpy_agg = input_data() |
| 45 | + xrspatial_aspect = aspect(numpy_agg, name='numpy_aspect') |
38 | 46 |
|
39 | | - general_output_checks(small_da, xrspatial_aspect) |
| 47 | + general_output_checks(numpy_agg, xrspatial_aspect, verify_dtype=True) |
40 | 48 | assert xrspatial_aspect.name == 'numpy_aspect' |
41 | 49 |
|
42 | | - # validate output values |
43 | 50 | xrspatial_vals = xrspatial_aspect.data[1:-1, 1:-1] |
44 | | - qgis_vals = QGIS_OUTPUT[1:-1, 1:-1] |
| 51 | + qgis_vals = qgis_output[1:-1, 1:-1] |
45 | 52 | # aspect is nan if nan input |
46 | 53 | # aspect is invalid (-1) if slope equals 0 |
47 | | - # otherwise aspect are from 0 - 360 |
| 54 | + # otherwise aspect are from 0 to 360 |
48 | 55 | np.testing.assert_allclose(xrspatial_vals, qgis_vals, equal_nan=True) |
49 | | - |
50 | 56 | # nan edge effect |
51 | | - xrspatial_edges = [ |
52 | | - xrspatial_aspect.data[0, :], |
53 | | - xrspatial_aspect.data[-1, :], |
54 | | - xrspatial_aspect.data[:, 0], |
55 | | - xrspatial_aspect.data[:, -1], |
56 | | - ] |
57 | | - for edge in xrspatial_edges: |
58 | | - np.testing.assert_allclose( |
59 | | - edge, np.full(edge.shape, np.nan), equal_nan=True |
60 | | - ) |
61 | | - |
62 | | - |
63 | | -def test_numpy_equals_dask(): |
64 | | - small_numpy_based_data_array = xr.DataArray( |
65 | | - INPUT_DATA, attrs={'res': (10.0, 10.0)} |
66 | | - ) |
67 | | - small_dask_based_data_array = xr.DataArray( |
68 | | - da.from_array(INPUT_DATA, chunks=(2, 2)), attrs={'res': (10.0, 10.0)} |
69 | | - ) |
| 57 | + assert_nan_edges_effect(xrspatial_aspect) |
70 | 58 |
|
71 | | - numpy_result = aspect(small_numpy_based_data_array, name='numpy_result') |
72 | | - dask_result = aspect(small_dask_based_data_array, |
73 | | - name='dask_result') |
74 | | - general_output_checks(small_dask_based_data_array, dask_result) |
75 | | - np.testing.assert_allclose( |
76 | | - numpy_result.data, dask_result.data.compute(), equal_nan=True) |
77 | 59 |
|
| 60 | +def test_numpy_equals_dask_qgis_data(): |
| 61 | + # compare using the data run through QGIS |
| 62 | + numpy_agg = input_data('numpy') |
| 63 | + dask_agg = input_data('dask+numpy') |
| 64 | + assert_numpy_equals_dask_numpy(numpy_agg, dask_agg, aspect) |
78 | 65 |
|
79 | | -@pytest.mark.skipif(doesnt_have_cuda(), reason="CUDA Device not Available") |
80 | | -def test_cpu_equals_gpu(): |
81 | 66 |
|
82 | | - import cupy |
| 67 | +@pytest.mark.parametrize("size", [(2, 4), (10, 15)]) |
| 68 | +@pytest.mark.parametrize( |
| 69 | + "dtype", [np.int32, np.int64, np.uint32, np.uint64, np.float32, np.float64]) |
| 70 | +def test_numpy_equals_dask_random_data(random_data): |
| 71 | + numpy_agg = create_test_raster(random_data, backend='numpy') |
| 72 | + dask_agg = create_test_raster(random_data, backend='dask') |
| 73 | + assert_numpy_equals_dask_numpy(numpy_agg, dask_agg, aspect) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.skipif(doesnt_have_cuda(), reason="CUDA Device not Available") |
| 77 | +def test_numpy_equals_cupy_qgis_data(): |
| 78 | + # compare using the data run through QGIS |
| 79 | + numpy_agg = input_data() |
| 80 | + cupy_agg = input_data('cupy') |
| 81 | + assert_numpy_equals_cupy(numpy_agg, cupy_agg, aspect) |
83 | 82 |
|
84 | | - small_da = xr.DataArray(INPUT_DATA, attrs={'res': (10.0, 10.0)}) |
85 | | - small_da_cupy = xr.DataArray(cupy.asarray(INPUT_DATA), |
86 | | - attrs={'res': (10.0, 10.0)}) |
87 | 83 |
|
88 | | - # aspect by xrspatial |
89 | | - cpu = aspect(small_da, name='aspect_agg') |
90 | | - gpu = aspect(small_da_cupy, name='aspect_agg') |
91 | | - general_output_checks(small_da_cupy, gpu) |
92 | | - np.testing.assert_allclose(cpu.data, gpu.data.get(), equal_nan=True) |
| 84 | +@pytest.mark.skipif(doesnt_have_cuda(), reason="CUDA Device not Available") |
| 85 | +@pytest.mark.parametrize("size", [(2, 4), (10, 15)]) |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "dtype", [np.int32, np.int64, np.uint32, np.uint64, np.float32, np.float64]) |
| 88 | +def test_numpy_equals_cupy_random_data(random_data): |
| 89 | + numpy_agg = create_test_raster(random_data, backend='numpy') |
| 90 | + cupy_agg = create_test_raster(random_data, backend='cupy') |
| 91 | + assert_numpy_equals_cupy(numpy_agg, cupy_agg, aspect, atol=1e-6, rtol=1e-6) |
0 commit comments