def data_focal_stats(): data = np.arange(16).reshape(4, 4) cellsize = (1, 1) kernel = circle_kernel(*cellsize, 1.5) expected_result = np.asarray([ # mean [[1.66666667, 2., 3., 4.], [4.25, 5., 6., 6.75], [8.25, 9., 10., 10.75], [11., 12., 13., 13.33333333]], # max [[4., 5., 6., 7.], [8., 9., 10., 11.], [12., 13., 14., 15.], [13., 14., 15., 15.]], # min [[0., 0., 1., 2.], [0., 1., 2., 3.], [4., 5., 6., 7.], [8., 9., 10., 11.]], # range [[4., 5., 5., 5.], [8., 8., 8., 8.], [8., 8., 8., 8.], [5., 5., 5., 4.]], # std [[1.69967317, 1.87082869, 1.87082869, 2.1602469], [2.86138079, 2.60768096, 2.60768096, 2.86138079], [2.86138079, 2.60768096, 2.60768096, 2.86138079], [2.1602469, 1.87082869, 1.87082869, 1.69967317]], # var [[2.88888889, 3.5, 3.5, 4.66666667], [8.1875, 6.8, 6.8, 8.1875], [8.1875, 6.8, 6.8, 8.1875], [4.66666667, 3.5, 3.5, 2.88888889]], # sum [[5., 8., 12., 12.], [17., 25., 30., 27.], [33., 45., 50., 43.], [33., 48., 52., 40.]] ]) return data, kernel, expected_result
def test_kernel(kernel_circle_1_1_1, kernel_annulus_2_2_2_1): kernel_circle = circle_kernel(1, 1, 1) assert isinstance(kernel_circle, np.ndarray) np.testing.assert_allclose(kernel_circle, kernel_circle_1_1_1, equal_nan=True) kernel_annulus = annulus_kernel(2, 2, 2, 1) assert isinstance(kernel_annulus, np.ndarray) np.testing.assert_allclose(kernel_annulus, kernel_annulus_2_2_2_1, equal_nan=True)
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_focal_stats_cpu(): data = np.arange(16).reshape(4, 4) numpy_agg = xr.DataArray(data) dask_numpy_agg = xr.DataArray(da.from_array(data, chunks=(3, 3))) cellsize = (1, 1) kernel = circle_kernel(*cellsize, 1.5) expected_results = np.asarray([ # mean [[1.66666667, 2., 3., 4.], [4.25, 5., 6., 6.75], [8.25, 9., 10., 10.75], [11., 12., 13., 13.33333333]], # max [[4., 5., 6., 7.], [8., 9., 10., 11.], [12., 13., 14., 15.], [13., 14., 15., 15.]], # min [[0., 0., 1., 2.], [0., 1., 2., 3.], [4., 5., 6., 7.], [8., 9., 10., 11.]], # range [[4., 5., 5., 5.], [8., 8., 8., 8.], [8., 8., 8., 8.], [5., 5., 5., 4.]], # std [[1.69967317, 1.87082869, 1.87082869, 2.1602469], [2.86138079, 2.60768096, 2.60768096, 2.86138079], [2.86138079, 2.60768096, 2.60768096, 2.86138079], [2.1602469, 1.87082869, 1.87082869, 1.69967317]], # var [[2.88888889, 3.5, 3.5, 4.66666667], [8.1875, 6.8, 6.8, 8.1875], [8.1875, 6.8, 6.8, 8.1875], [4.66666667, 3.5, 3.5, 2.88888889]], # sum [[5., 8., 12., 12.], [17., 25., 30., 27.], [33., 45., 50., 43.], [33., 48., 52., 40.]] ]) numpy_focalstats = focal_stats(numpy_agg, kernel) general_output_checks(numpy_agg, numpy_focalstats, verify_attrs=False, expected_results=expected_results) assert numpy_focalstats.ndim == 3 dask_numpy_focalstats = focal_stats(dask_numpy_agg, kernel) general_output_checks(dask_numpy_agg, dask_numpy_focalstats, verify_attrs=False, expected_results=expected_results)
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)