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_default_direction_against_allocation(test_raster, result_default_allocation): direction_agg = direction(test_raster, x='lon', y='lat') xcoords = direction_agg['lon'].data ycoords = direction_agg['lat'].data for y in range(test_raster.shape[0]): for x in range(test_raster.shape[1]): a = result_default_allocation.data[y, x] py, px = np.where(test_raster.data == a) # non-zero cells in raster are unique, thus len(px)=len(py)=1 d = _calc_direction(xcoords[x], xcoords[px[0]], ycoords[y], ycoords[py[0]]) np.testing.assert_allclose(direction_agg.data[y, x], d)
def test_direction(): raster = create_test_raster() # add crs for tests raster = _add_EPSG4326_crs_to_da(raster) direction_agg = direction(raster, x='lon', y='lat') # output must be an xarray DataArray, and attrs and coords must be preserved, including crs assert isinstance(direction_agg, xa.DataArray) assert type(direction_agg.values[0][0]) == np.float64 assert direction_agg.shape == raster.shape assert direction_agg.dims == raster.dims assert direction_agg.attrs == raster.attrs for c in direction_agg.coords: assert (direction_agg[c] == raster.coords[c]).all() # in this test case, where no polygon is completely inside another polygon, # number of non-zeros (target pixels) in original image # must be equal to the number of zeros (target pixels) in proximity matrix assert len(np.where((raster.data != 0) & np.isfinite(raster.data))[0]) == \ len(np.where(direction_agg.data == 0)[0]) # values are within [0, 360] assert np.min(direction_agg.data) >= 0 assert np.max(direction_agg.data) <= 360 # test against allocation allocation_agg = allocation(raster, x='lon', y='lat') xcoords = allocation_agg['lon'].data ycoords = allocation_agg['lat'].data for y in range(raster.shape[0]): for x in range(raster.shape[1]): a = allocation_agg.data[y, x] py, px = np.where(raster.data == a) # non-zero cells in raster are unique, thus len(px)=len(py)=1 d = _calc_direction(xcoords[x], xcoords[px[0]], ycoords[y], ycoords[py[0]]) assert direction_agg.data[y, x] == d
def test_default_direction(test_raster, result_default_direction): direction_agg = direction(test_raster, x='lon', y='lat') general_output_checks(test_raster, direction_agg, result_default_direction)