def test_allocation():
    # create test raster, all non-zero cells are unique,
    # this is to test against corresponding proximity
    raster = create_test_raster()

    allocation_agg = allocation(raster, x='lon', y='lat')
    # output must be an xarray DataArray
    assert isinstance(allocation_agg, xa.DataArray)
    assert type(allocation_agg.values[0][0]) == raster.dtype
    assert allocation_agg.shape == raster.shape
    # targets not specified,
    # Thus, targets are set to non-zero values of input @raster
    targets = np.unique(raster.data[np.where((raster.data != 0)
                                             & np.isfinite(raster.data))])
    # non-zero cells (a.k.a targets) remain the same
    for t in targets:
        ry, rx = np.where(raster.data == t)
        for y, x in zip(ry, rx):
            assert allocation_agg.values[y, x] == t
    # values of allocation output
    assert (np.unique(allocation_agg.data) == targets).all()

    # check against corresponding proximity
    proximity_agg = proximity(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 = euclidean_distance(xcoords[x], xcoords[px[0]], ycoords[y],
                                   ycoords[py[0]])
            assert proximity_agg.data[y, x] == d
示例#2
0
def test_max_distance_allocation(test_raster, result_max_distance_allocation):
    max_distance, expected_result = result_max_distance_allocation
    max_distance_alloc = allocation(test_raster,
                                    x='lon',
                                    y='lat',
                                    max_distance=max_distance)
    general_output_checks(test_raster,
                          max_distance_alloc,
                          expected_result,
                          verify_dtype=True)
示例#3
0
def test_default_allocation_against_proximity(test_raster,
                                              result_default_proximity):
    allocation_agg = allocation(test_raster, x='lon', y='lat')
    # check against corresponding proximity
    xcoords = allocation_agg['lon'].data
    ycoords = allocation_agg['lat'].data
    for y in range(test_raster.shape[0]):
        for x in range(test_raster.shape[1]):
            a = allocation_agg.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 = euclidean_distance(xcoords[x], xcoords[px[0]], ycoords[y],
                                   ycoords[py[0]])
            np.testing.assert_allclose(result_default_proximity[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
示例#5
0
def test_default_allocation(test_raster, result_default_allocation):
    allocation_agg = allocation(test_raster, x='lon', y='lat')
    general_output_checks(test_raster,
                          allocation_agg,
                          result_default_allocation,
                          verify_dtype=True)