def test_reclassify_cpu(): bins = [10, 20, 30] new_values = [1, 2, 3] # numpy # add crs for tests numpy_agg_crs = _add_EPSG4326_crs_to_da(numpy_agg) numpy_reclassify = reclassify(numpy_agg_crs, bins=bins, new_values=new_values, name='numpy_reclassify') unique_elements, counts_elements = np.unique(numpy_reclassify.data, return_counts=True) assert len(unique_elements) == 3 # crs tests assert numpy_reclassify.attrs == numpy_agg_crs.attrs for coord in numpy_agg_crs.coords: assert np.all(numpy_reclassify[coord] == numpy_agg_crs[coord]) # dask + numpy dask_reclassify = reclassify(dask_numpy_agg, bins=bins, new_values=new_values, name='dask_reclassify') assert isinstance(dask_reclassify.data, da.Array) dask_reclassify.data = dask_reclassify.data.compute() assert np.isclose(numpy_reclassify, dask_reclassify, equal_nan=True).all()
def test_reclassify_cpu_equals_gpu(): import cupy bins = [10, 20, 30] new_values = [1, 2, 3] # vanilla numpy version cpu = reclassify(numpy_agg, name='numpy_result', bins=bins, new_values=new_values) # cupy cupy_agg = xr.DataArray(cupy.asarray(elevation), attrs={'res': (10.0, 10.0)}) gpu = reclassify(cupy_agg, name='cupy_result', bins=bins, new_values=new_values) assert isinstance(gpu.data, cupy.ndarray) assert np.isclose(cpu, gpu, equal_nan=True).all() # dask + cupy dask_cupy_agg = xr.DataArray(cupy.asarray(elevation), attrs={'res': (10.0, 10.0)}) dask_cupy_agg.data = da.from_array(dask_cupy_agg.data, chunks=(3, 3)) dask_gpu = reclassify(dask_cupy_agg, name='dask_cupy_result', bins=bins, new_values=new_values) assert isinstance(dask_gpu.data, da.Array) and is_cupy_backed(dask_gpu) dask_gpu.data = dask_gpu.data.compute() assert np.isclose(cpu, dask_gpu, equal_nan=True).all()
def test_reclassify_cpu_equals_gpu(): import cupy bins = [10, 15, np.inf] new_values = [1, 2, 3] # vanilla numpy version cpu = reclassify(numpy_agg, name='numpy_result', bins=bins, new_values=new_values) # cupy cupy_agg = xr.DataArray(cupy.asarray(elevation), attrs={'res': (10.0, 10.0)}) gpu = reclassify(cupy_agg, name='cupy_result', bins=bins, new_values=new_values) general_output_checks(cupy_agg, gpu) np.testing.assert_allclose(cpu.data, gpu.data.get(), equal_nan=True) # dask + cupy dask_cupy_agg = xr.DataArray(cupy.asarray(elevation), attrs={'res': (10.0, 10.0)}) dask_cupy_agg.data = da.from_array(dask_cupy_agg.data, chunks=(3, 3)) dask_gpu = reclassify(dask_cupy_agg, name='dask_cupy_result', bins=bins, new_values=new_values) general_output_checks(dask_cupy_agg, dask_gpu) dask_gpu.data = dask_gpu.data.compute() np.testing.assert_allclose(cpu.data, dask_gpu.data.get(), equal_nan=True)
def test_reclassify_numpy_mismatch_length(): bins = [10] new_values = [1, 2, 3] numpy_agg = input_data() msg = 'bins and new_values mismatch. Should have same length.' with pytest.raises(ValueError, match=msg): reclassify(numpy_agg, bins, new_values)
def test_reclassify_cupy(result_reclassify): bins, new_values, expected_result = result_reclassify cupy_agg = input_data(backend='cupy') cupy_result = reclassify(cupy_agg, bins=bins, new_values=new_values) general_output_checks(cupy_agg, cupy_result, expected_result, verify_dtype=True)
def test_reclassify_dask_numpy(result_reclassify): bins, new_values, expected_result = result_reclassify dask_agg = input_data(backend='dask') dask_result = reclassify(dask_agg, bins=bins, new_values=new_values) general_output_checks(dask_agg, dask_result, expected_result, verify_dtype=True)
def test_reclassify_numpy(result_reclassify): bins, new_values, expected_result = result_reclassify numpy_agg = input_data() numpy_result = reclassify(numpy_agg, bins=bins, new_values=new_values) general_output_checks(numpy_agg, numpy_result, expected_result, verify_dtype=True)
def test_reclassify_cpu(): bins = [10, 15, np.inf] new_values = [1, 2, 3] expected_results = np.asarray([[1., 1., 1., 1., np.nan], [1., 1., 1., 1., 1.], [1., 2., 2., 2., 2.], [2., 3., 3., 3., 3.]]) # numpy numpy_reclassify = reclassify(numpy_agg, bins=bins, new_values=new_values, name='numpy_reclassify') general_output_checks(numpy_agg, numpy_reclassify, expected_results) # dask + numpy dask_reclassify = reclassify(dask_numpy_agg, bins=bins, new_values=new_values, name='dask_reclassify') general_output_checks(dask_numpy_agg, dask_reclassify, expected_results)
def test_reclassify_cpu(): bins = [10, 20, 30] new_values = [1, 2, 3] # numpy numpy_reclassify = reclassify(numpy_agg, bins=bins, new_values=new_values, name='numpy_reclassify') unique_elements, counts_elements = np.unique(numpy_reclassify.data, return_counts=True) assert len(unique_elements) == 3 # dask + numpy dask_reclassify = reclassify(dask_numpy_agg, bins=bins, new_values=new_values, name='dask_reclassify') assert isinstance(dask_reclassify.data, da.Array) dask_reclassify.data = dask_reclassify.data.compute() assert np.isclose(numpy_reclassify, dask_reclassify, equal_nan=True).all()