def test_hotspots_zero_global_std(): data = np.zeros((10, 20)) agg = create_test_raster(data) kernel = np.zeros((3, 3)) msg = "Standard deviation of the input raster values is 0." with pytest.raises(ZeroDivisionError, match=msg): hotspots(agg, kernel)
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_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_hotspot(): n, m = 10, 10 raster = xr.DataArray(np.zeros((n, m), dtype=float), dims=['y', 'x']) raster['x'] = np.linspace(0, n, n) raster['y'] = np.linspace(0, m, m) cellsize_x, cellsize_y = calc_cellsize(raster) kernel = circle_kernel(cellsize_x, cellsize_y, 2.0) all_idx = zip(*np.where(raster.values == 0)) nan_cells = [(i, i) for i in range(m)] for cell in nan_cells: raster[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: raster[p[0], p[1]] = 10000 for p in cold_region: raster[p[0], p[1]] = -10000 no_significant_region = [ id for id in all_idx if id not in hot_region and id not in cold_region ] hotspots_output = hotspots(raster, kernel) # check output's properties # output must be an xarray DataArray assert isinstance(hotspots_output, xr.DataArray) assert isinstance(hotspots_output.values, np.ndarray) assert issubclass(hotspots_output.values.dtype.type, np.int8) # shape, dims, coords, attr preserved assert raster.shape == hotspots_output.shape assert raster.dims == hotspots_output.dims assert raster.attrs == hotspots_output.attrs for coord in raster.coords: assert np.all(raster[coord] == hotspots_output[coord]) # no nan in output assert not np.isnan(np.min(hotspots_output)) # output of extreme regions are non-zeros # hot spots hot_spot = np.asarray([hotspots_output[p] for p in hot_region]) assert np.all(hot_spot >= 0) assert np.sum(hot_spot) > 0 # cold spots cold_spot = np.asarray([hotspots_output[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([hotspots_output[p] for p in no_significant_region]) assert np.all(no_sign == 0)
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, verify_attrs=False) # validate attrs assert cupy_hotspots.shape == cupy_agg.shape assert cupy_hotspots.dims == cupy_agg.dims for coord in cupy_agg.coords: np.testing.assert_allclose(cupy_hotspots[coord].data, cupy_agg[coord].data, equal_nan=True) assert cupy_hotspots.attrs['unit'] == '%' # 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_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_hotspots_dask_numpy(data_hotspots): data, kernel, expected_result = data_hotspots dask_numpy_agg = create_test_raster(data, backend='dask') dask_numpy_hotspots = hotspots(dask_numpy_agg, kernel) general_output_checks(dask_numpy_agg, dask_numpy_hotspots, expected_result, verify_attrs=False) # validate attrs assert dask_numpy_hotspots.shape == dask_numpy_agg.shape assert dask_numpy_hotspots.dims == dask_numpy_agg.dims for coord in dask_numpy_agg.coords: np.testing.assert_allclose(dask_numpy_hotspots[coord].data, dask_numpy_agg[coord].data, equal_nan=True) assert dask_numpy_hotspots.attrs['unit'] == '%'
def test_hotspot_invalid(): kernel = Kernel(radius=1) invalid_raster_type = np.array([0, 1, 2, 3]) with pytest.raises(Exception) as e_info: hotspots(invalid_raster_type, kernel) assert e_info invalid_raster_dtype = xr.DataArray(np.array([['cat', 'dog']])) with pytest.raises(Exception) as e_info: hotspots(invalid_raster_dtype, kernel) assert e_info invalid_raster_shape = xr.DataArray(np.array([0, 0])) with pytest.raises(Exception) as e_info: hotspots(invalid_raster_shape, kernel) assert e_info invalid_raster_std = xr.DataArray(np.ones((10, 10))) # std of the raster is 0 with pytest.raises(Exception) as e_info: hotspots(invalid_raster_std, kernel) assert e_info
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_hotspots_dask_numpy(data_hotspots): data, kernel, expected_result = data_hotspots dask_numpy_agg = create_test_raster(data, backend='dask') dask_numpy_hotspots = hotspots(dask_numpy_agg, kernel) general_output_checks(dask_numpy_agg, dask_numpy_hotspots, expected_result)
def test_hotspots_numpy(data_hotspots): data, kernel, expected_result = data_hotspots numpy_agg = create_test_raster(data) numpy_hotspots = hotspots(numpy_agg, kernel) general_output_checks(numpy_agg, numpy_hotspots, expected_result)
def time_hotspots(self, nx, kernelsize, type): hotspots(self.agg, self.kernel)