def test_hotspot_gpu_equals_cpu(): n, m = 10, 10 data = np.zeros((n, m), dtype=float) nan_cells = [(i, i) for i in range(m)] for cell in nan_cells: data[cell[0], cell[1]] = np.nan # add some extreme values hot_region = [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)] cold_region = [(7, 7), (7, 8), (7, 9), (8, 7), (8, 8), (8, 9), (9, 7), (9, 8), (9, 9)] for p in hot_region: data[p[0], p[1]] = 10000 for p in cold_region: data[p[0], p[1]] = -10000 numpy_agg = xr.DataArray(data, dims=['y', 'x']) numpy_agg['x'] = np.linspace(0, n, n) numpy_agg['y'] = np.linspace(0, m, m) cellsize_x, cellsize_y = calc_cellsize(numpy_agg) kernel = circle_kernel(cellsize_x, cellsize_y, 2.0) # numpy case numpy_hotspots = hotspots(numpy_agg, kernel) # cupy case import cupy cupy_agg = xr.DataArray(cupy.asarray(data)) cupy_hotspots = hotspots(cupy_agg, kernel) assert isinstance(cupy_hotspots.data, cupy.ndarray) assert np.isclose( numpy_hotspots, cupy_hotspots.data.get(), equal_nan=True ).all() # dask + cupy case not implemented dask_cupy_agg = xr.DataArray( da.from_array(cupy.asarray(data), chunks=(3, 3)) ) with pytest.raises(NotImplementedError) as e_info: hotspots(dask_cupy_agg, kernel) assert e_info
def test_kernel(): data = convolve_2d_data m, n = data.shape agg = xr.DataArray(data, dims=['y', 'x']) agg['x'] = np.linspace(0, n, n) agg['y'] = np.linspace(0, m, m) cellsize_x, cellsize_y = calc_cellsize(agg) kernel1 = circle_kernel(cellsize_x, cellsize_y, 2) expected_kernel1 = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) assert isinstance(kernel1, np.ndarray) np.testing.assert_allclose(kernel1, expected_kernel1, equal_nan=True) kernel2 = annulus_kernel(cellsize_x, cellsize_y, 2, 0.5) expected_kernel2 = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) assert isinstance(kernel2, np.ndarray) np.testing.assert_allclose(kernel2, expected_kernel2, equal_nan=True)
def test_hotspot_gpu_equals_cpu(): n, m = 10, 10 data = np.asarray([[np.nan, 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 10000., 10000., 10000., 0., 0., 0., 0., 0., 0.], [0., 10000., 10000., 10000., 0., 0., 0., 0., 0., 0.], [0., 10000., 10000., 10000., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., np.nan, 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., np.nan, 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., np.nan, 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., -10000., -10000., -10000.], [0., 0., 0., 0., 0., 0., 0., -10000., -10000., -10000.], [0., 0., 0., 0., 0., 0., 0., -10000., -10000., -10000.]]) numpy_agg = xr.DataArray(data, dims=['y', 'x']) numpy_agg['x'] = np.linspace(0, n, n) numpy_agg['y'] = np.linspace(0, m, m) cellsize_x, cellsize_y = calc_cellsize(numpy_agg) kernel = circle_kernel(cellsize_x, cellsize_y, 2.0) # numpy case numpy_hotspots = hotspots(numpy_agg, kernel) # cupy case import cupy cupy_agg = xr.DataArray(cupy.asarray(data)) cupy_hotspots = hotspots(cupy_agg, kernel) np.testing.assert_allclose(numpy_hotspots.data, cupy_hotspots.data.get(), equal_nan=True) # dask + cupy case not implemented dask_cupy_agg = xr.DataArray( da.from_array(cupy.asarray(data), chunks=(3, 3))) with pytest.raises(NotImplementedError) as e_info: hotspots(dask_cupy_agg, kernel) assert e_info
def test_hotspots(): n, m = 10, 10 data = np.asarray([[np.nan, 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 10000., 10000., 10000., 0., 0., 0., 0., 0., 0.], [0., 10000., 10000., 10000., 0., 0., 0., 0., 0., 0.], [0., 10000., 10000., 10000., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., np.nan, 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., np.nan, 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., np.nan, 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., -10000., -10000., -10000.], [0., 0., 0., 0., 0., 0., 0., -10000., -10000., -10000.], [0., 0., 0., 0., 0., 0., 0., -10000., -10000., -10000.]]) numpy_agg = xr.DataArray(data, dims=['y', 'x']) numpy_agg['x'] = np.linspace(0, n, n) numpy_agg['y'] = np.linspace(0, m, m) cellsize_x, cellsize_y = calc_cellsize(numpy_agg) kernel = circle_kernel(cellsize_x, cellsize_y, 2.0) expected_results = np.array( [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 90, 0, 0, 0, 0, 0, 0, 0], [0, 90, 95, 90, 0, 0, 0, 0, 0, 0], [0, 0, 90, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, -90, 0], [0, 0, 0, 0, 0, 0, 0, -90, -95, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.int8) # numpy case numpy_hotspots = hotspots(numpy_agg, kernel) general_output_checks(numpy_agg, numpy_hotspots, expected_results) # dask + numpy dask_numpy_agg = xr.DataArray(da.from_array(data, chunks=(3, 3))) dask_numpy_hotspots = hotspots(dask_numpy_agg, kernel) general_output_checks(dask_numpy_agg, dask_numpy_hotspots, expected_results)
def test_hotspot(): n, m = 10, 10 data = np.zeros((n, m), dtype=float) all_idx = zip(*np.where(data == 0)) nan_cells = [(i, i) for i in range(m)] for cell in nan_cells: data[cell[0], cell[1]] = np.nan # add some extreme values hot_region = [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)] cold_region = [(7, 7), (7, 8), (7, 9), (8, 7), (8, 8), (8, 9), (9, 7), (9, 8), (9, 9)] for p in hot_region: data[p[0], p[1]] = 10000 for p in cold_region: data[p[0], p[1]] = -10000 numpy_agg = xr.DataArray(data, dims=['y', 'x']) numpy_agg['x'] = np.linspace(0, n, n) numpy_agg['y'] = np.linspace(0, m, m) cellsize_x, cellsize_y = calc_cellsize(numpy_agg) kernel = circle_kernel(cellsize_x, cellsize_y, 2.0) no_significant_region = [id for id in all_idx if id not in hot_region and id not in cold_region] # numpy case numpy_hotspots = hotspots(numpy_agg, kernel) # dask + numpy dask_numpy_agg = xr.DataArray(da.from_array(data, chunks=(3, 3))) dask_numpy_hotspots = hotspots(dask_numpy_agg, kernel) assert isinstance(dask_numpy_hotspots.data, da.Array) # both output same results assert np.isclose(numpy_hotspots.data, dask_numpy_hotspots.data.compute(), equal_nan=True).all() # check output's properties # output must be an xarray DataArray assert isinstance(numpy_hotspots, xr.DataArray) assert isinstance(numpy_hotspots.values, np.ndarray) assert issubclass(numpy_hotspots.values.dtype.type, np.int8) # shape, dims, coords, attr preserved assert numpy_agg.shape == numpy_hotspots.shape assert numpy_agg.dims == numpy_hotspots.dims assert numpy_agg.attrs == numpy_hotspots.attrs for coord in numpy_agg.coords: assert np.all(numpy_agg[coord] == numpy_hotspots[coord]) # no nan in output assert not np.isnan(np.min(numpy_hotspots)) # output of extreme regions are non-zeros # hot spots hot_spot = np.asarray([numpy_hotspots[p] for p in hot_region]) assert np.all(hot_spot >= 0) assert np.sum(hot_spot) > 0 # cold spots cold_spot = np.asarray([numpy_hotspots[p] for p in cold_region]) assert np.all(cold_spot <= 0) assert np.sum(cold_spot) < 0 # output of no significant regions are 0s no_sign = np.asarray([numpy_hotspots[p] for p in no_significant_region]) assert np.all(no_sign == 0)
def test_calc_cellsize_no_attrs(convolve_2d_data): agg = create_test_raster(convolve_2d_data) cellsize = calc_cellsize(agg) assert cellsize == (1, 1)
def test_calc_cellsize_unit_input_attrs(convolve_2d_data): agg = create_test_raster(convolve_2d_data, attrs={'res': 1, 'unit': 'km'}) cellsize = calc_cellsize(agg) assert cellsize == (1000, 1000)