def test_numpy_equals_qgis(): small_da = xr.DataArray(INPUT_DATA, attrs={'res': (10.0, 10.0)}) xrspatial_aspect = aspect(small_da, name='numpy_aspect') general_output_checks(small_da, xrspatial_aspect) assert xrspatial_aspect.name == 'numpy_aspect' # validate output values xrspatial_vals = xrspatial_aspect.data[1:-1, 1:-1] qgis_vals = QGIS_OUTPUT[1:-1, 1:-1] # aspect is nan if nan input # aspect is invalid (-1) if slope equals 0 # otherwise aspect are from 0 - 360 np.testing.assert_allclose(xrspatial_vals, qgis_vals, equal_nan=True) # nan edge effect xrspatial_edges = [ xrspatial_aspect.data[0, :], xrspatial_aspect.data[-1, :], xrspatial_aspect.data[:, 0], xrspatial_aspect.data[:, -1], ] for edge in xrspatial_edges: np.testing.assert_allclose(edge, np.full(edge.shape, np.nan), equal_nan=True)
def test_slope_against_qgis_cpu(): # slope by xrspatial agg_numpy = xr.DataArray(elevation, attrs={'res': (10.0, 10.0)}) xrspatial_slope_numpy = slope(agg_numpy, name='slope_numpy') general_output_checks(agg_numpy, xrspatial_slope_numpy) assert xrspatial_slope_numpy.name == 'slope_numpy' agg_dask = xr.DataArray(da.from_array(elevation, chunks=(3, 3)), attrs={'res': (10.0, 10.0)}) xrspatial_slope_dask = slope(agg_dask, name='slope_dask') general_output_checks(agg_dask, xrspatial_slope_dask) assert xrspatial_slope_dask.name == 'slope_dask' # numpy and dask case produce same results np.testing.assert_allclose(xrspatial_slope_numpy.data, xrspatial_slope_dask.compute().data, equal_nan=True) # nan border edges xrspatial_edges = [ xrspatial_slope_numpy.data[0, :], xrspatial_slope_numpy.data[-1, :], xrspatial_slope_numpy.data[:, 0], xrspatial_slope_numpy.data[:, -1], ] for edge in xrspatial_edges: np.testing.assert_allclose(edge, np.full(edge.shape, np.nan), equal_nan=True) # test against QGIS xrspatial_vals = xrspatial_slope_numpy.values[1:-1, 1:-1] qgis_vals = qgis_slope[1:-1, 1:-1] np.testing.assert_allclose(xrspatial_vals, qgis_vals, equal_nan=True)
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_true_color_cpu(): # vanilla numpy version red_numpy = create_test_arr(red_data) green_numpy = create_test_arr(green_data) blue_numpy = create_test_arr(blue_data) numpy_result = true_color(red_numpy, green_numpy, blue_numpy, name='np_true_color') assert numpy_result.name == 'np_true_color' general_output_checks(red_numpy, numpy_result, verify_attrs=False) # dask red_dask = create_test_arr(red_data, backend='dask') green_dask = create_test_arr(green_data, backend='dask') blue_dask = create_test_arr(blue_data, backend='dask') dask_result = true_color(red_dask, green_dask, blue_dask, name='dask_true_color') assert dask_result.name == 'dask_true_color' general_output_checks(red_numpy, numpy_result, verify_attrs=False) np.testing.assert_allclose(numpy_result.data, dask_result.compute().data, equal_nan=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_equal_interval_cupy(result_equal_interval): k, expected_result = result_equal_interval cupy_agg = input_data(backend='cupy') cupy_result = equal_interval(cupy_agg, k=k) general_output_checks(cupy_agg, cupy_result, expected_result, verify_dtype=True)
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_natural_breaks_cpu(): k = 5 expected_results = np.asarray([[0., 0., 0., 1., np.nan], [1., 1., 2., 2., 2.], [2., 3., 3., 3., 3.], [4., 4., 4., 4., np.nan]]) # vanilla numpy numpy_natural_breaks = natural_breaks(numpy_agg, k=k) general_output_checks(numpy_agg, numpy_natural_breaks, expected_results)
def test_quantile_cupy(result_quantile): k, expected_result = result_quantile cupy_agg = input_data('cupy') cupy_result = quantile(cupy_agg, k=k) 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_focal_stats_dask_numpy(data_focal_stats): data, kernel, expected_result = data_focal_stats dask_numpy_agg = create_test_raster(data, backend='dask') dask_numpy_focalstats = focal_stats(dask_numpy_agg, kernel) general_output_checks(dask_numpy_agg, dask_numpy_focalstats, verify_attrs=False, expected_results=expected_result)
def test_curvature_on_concave_surface(concave_surface): concave_data, expected_result = concave_surface numpy_agg = create_test_raster(concave_data, attrs={'res': (1, 1)}) numpy_result = curvature(numpy_agg) general_output_checks(numpy_agg, numpy_result, expected_result, verify_dtype=True)
def test_equal_interval_numpy(result_equal_interval): k, expected_result = result_equal_interval numpy_agg = input_data('numpy') numpy_result = equal_interval(numpy_agg, k=k) general_output_checks(numpy_agg, numpy_result, expected_result, verify_dtype=True)
def test_equal_interval_dask_numpy(result_equal_interval): k, expected_result = result_equal_interval dask_agg = input_data('dask+numpy') dask_numpy_result = equal_interval(dask_agg, k=k) general_output_checks(dask_agg, dask_numpy_result, expected_result, verify_dtype=True)
def test_natural_breaks_cupy_num_sample(result_natural_breaks_num_sample): cupy_agg = input_data('cupy') k, num_sample, expected_result = result_natural_breaks_num_sample cupy_natural_breaks = natural_breaks(cupy_agg, k=k, num_sample=num_sample) general_output_checks(cupy_agg, cupy_natural_breaks, expected_result, verify_dtype=True)
def test_natural_breaks_cupy(result_natural_breaks): cupy_agg = input_data('cupy') k, expected_result = result_natural_breaks cupy_natural_breaks = natural_breaks(cupy_agg, k=k) general_output_checks(cupy_agg, cupy_natural_breaks, expected_result, verify_dtype=True)
def test_natural_breaks_numpy(result_natural_breaks): numpy_agg = input_data() k, expected_result = result_natural_breaks numpy_natural_breaks = natural_breaks(numpy_agg, k=k) general_output_checks(numpy_agg, numpy_natural_breaks, expected_result, verify_dtype=True)
def test_focal_stats_gpu(data_focal_stats): data, kernel, expected_result = data_focal_stats cupy_agg = create_test_raster(data, backend='cupy') cupy_focalstats = focal_stats(cupy_agg, kernel) general_output_checks(cupy_agg, cupy_focalstats, verify_attrs=False, expected_results=expected_result)
def test_quantile_numpy(result_quantile): k, expected_result = result_quantile numpy_agg = input_data() numpy_quantile = quantile(numpy_agg, k=k) general_output_checks(numpy_agg, numpy_quantile, expected_result, verify_dtype=True)
def test_manhattan_proximity(test_raster, result_manhattan_proximity): manhattan_prox = proximity(test_raster, x='lon', y='lat', distance_metric='MANHATTAN') general_output_checks(test_raster, manhattan_prox, result_manhattan_proximity, verify_dtype=True)
def test_great_circle_proximity(test_raster, result_great_circle_proximity): great_circle_prox = proximity(test_raster, x='lon', y='lat', distance_metric='GREAT_CIRCLE') general_output_checks(test_raster, great_circle_prox, result_great_circle_proximity, verify_dtype=True)
def test_focal_stats_numpy(data_focal_stats): data, kernel, expected_result = data_focal_stats numpy_agg = create_test_raster(data) numpy_focalstats = focal_stats(numpy_agg, kernel) general_output_checks(numpy_agg, numpy_focalstats, verify_attrs=False, expected_results=expected_result) assert numpy_focalstats.ndim == 3
def test_max_distance_direction(test_raster, result_max_distance_direction): max_distance, expected_result = result_max_distance_direction max_distance_direction = direction(test_raster, x='lon', y='lat', max_distance=max_distance) general_output_checks(test_raster, max_distance_direction, expected_result, verify_dtype=True)
def test_hillshade(): """ Assert Simple Hillshade transfer function """ da_gaussian = xr.DataArray(data_gaussian) da_gaussian_shade = hillshade(da_gaussian, name='hillshade_agg') general_output_checks(da_gaussian, da_gaussian_shade) assert da_gaussian_shade.name == 'hillshade_agg' assert da_gaussian_shade.mean() > 0 assert da_gaussian_shade[60, 60] > 0
def test_max_distance_proximity(test_raster, result_max_distance_proximity): max_distance, expected_result = result_max_distance_proximity max_distance_prox = proximity(test_raster, x='lon', y='lat', max_distance=max_distance) general_output_checks(test_raster, max_distance_prox, expected_result, verify_dtype=True)
def test_target_proximity(test_raster, result_target_proximity): target_values, expected_result = result_target_proximity target_prox = proximity(test_raster, x='lon', y='lat', target_values=target_values) general_output_checks(test_raster, target_prox, expected_result, verify_dtype=True)
def test_hotspot_gpu(data_hotspots): data, kernel, expected_result = data_hotspots cupy_agg = create_test_raster(data, backend='cupy') cupy_hotspots = hotspots(cupy_agg, kernel) general_output_checks(cupy_agg, cupy_hotspots, expected_result) # dask + cupy case not implemented dask_cupy_agg = create_test_raster(data, backend='dask+cupy') with pytest.raises(NotImplementedError) as e_info: hotspots(dask_cupy_agg, kernel) assert e_info
def test_ndmi_cpu(): nir_numpy = create_test_arr(nir_data) swir1_numpy = create_test_arr(swir1_data) numpy_result = ndmi(nir_numpy, swir1_numpy) general_output_checks(nir_numpy, numpy_result, ndmi_expected_results) # dask nir_dask = create_test_arr(nir_data, backend='dask') swir1_dask = create_test_arr(swir1_data, backend='dask') dask_result = ndmi(nir_dask, swir1_dask) general_output_checks(nir_dask, dask_result, ndmi_expected_results)
def test_nbr2_cpu(): swir1_numpy = create_test_arr(swir1_data) swir2_numpy = create_test_arr(swir2_data) numpy_result = nbr2(swir1_numpy, swir2_numpy) general_output_checks(swir1_numpy, numpy_result, nbr2_expected_results) # dask swir1_dask = create_test_arr(swir1_data, backend='dask') swir2_dask = create_test_arr(swir2_data, backend='dask') dask_result = nbr2(swir1_dask, swir2_dask) general_output_checks(swir1_dask, dask_result, nbr2_expected_results)
def test_apply_gpu(data_apply): data, kernel, expected_result = data_apply # cupy case cupy_agg = create_test_raster(data, backend='cupy') cupy_apply = apply(cupy_agg, kernel, func_zero) general_output_checks(cupy_agg, cupy_apply, expected_result) # dask + cupy case not implemented dask_cupy_agg = create_test_raster(data, backend='dask+cupy') with pytest.raises(NotImplementedError) as e_info: apply(dask_cupy_agg, kernel, func_zero) assert e_info