Example #1
0
def test_quantile_cpu():
    k = 5

    # numpy

    # add crs for tests
    numpy_agg_crs = _add_EPSG4326_crs_to_da(numpy_agg)

    numpy_quantile = quantile(numpy_agg_crs, k=k)

    unique_elements, counts_elements = np.unique(numpy_quantile.data,
                                                 return_counts=True)
    assert isinstance(numpy_quantile.data, np.ndarray)
    assert len(unique_elements) == k
    assert len(np.unique(counts_elements)) == 1
    assert np.unique(counts_elements)[0] == 5

    # crs tests
    assert numpy_quantile.attrs == numpy_agg_crs.attrs
    for coord in numpy_agg_crs.coords:
        assert np.all(numpy_quantile[coord] == numpy_agg_crs[coord])

    # dask + numpy
    dask_quantile = quantile(dask_numpy_agg, k=k)
    assert isinstance(dask_quantile.data, da.Array)
Example #2
0
def test_quantile_cpu_equals_gpu():

    import cupy

    k = 5
    # vanilla numpy version
    cpu = quantile(numpy_agg, k=k, name='numpy_result')
    # cupy
    cupy_agg = xr.DataArray(cupy.asarray(elevation),
                            attrs={'res': (10.0, 10.0)})
    gpu = quantile(cupy_agg, k=k, name='cupy_result')
    general_output_checks(cupy_agg, gpu)
    np.testing.assert_allclose(cpu.data, gpu.data.get(), equal_nan=True)
Example #3
0
def test_quantile_cpu():
    k = 5

    # numpy
    numpy_quantile = quantile(numpy_agg, k=k)

    unique_elements, counts_elements = np.unique(numpy_quantile.data,
                                                 return_counts=True)
    assert isinstance(numpy_quantile.data, np.ndarray)
    assert len(unique_elements) == k
    assert len(np.unique(counts_elements)) == 1
    assert np.unique(counts_elements)[0] == 5

    # dask + numpy
    dask_quantile = quantile(dask_numpy_agg, k=k)
    assert isinstance(dask_quantile.data, da.Array)
Example #4
0
def test_quantile_cpu_equals_gpu():

    import cupy

    k = 5

    # vanilla numpy version
    cpu = quantile(numpy_agg, k=k, name='numpy_result')

    # cupy
    cupy_agg = xr.DataArray(cupy.asarray(elevation),
                            attrs={'res': (10.0, 10.0)})
    gpu = quantile(cupy_agg, k=k, name='cupy_result')

    assert isinstance(gpu.data, cupy.ndarray)
    assert np.isclose(cpu, gpu, equal_nan=True).all()
Example #5
0
def test_quantile_cpu():
    k = 5
    expected_results = np.asarray([[0., 0., 0., 0., np.nan],
                                   [1., 1., 1., 2., 2.], [2., 2., 3., 3., 3.],
                                   [4., 4., 4., 4., np.nan]])
    # numpy
    numpy_quantile = quantile(numpy_agg, k=k)
    general_output_checks(numpy_agg, numpy_quantile, expected_results)

    # dask + numpy
    dask_quantile = quantile(dask_numpy_agg, k=k)
    general_output_checks(dask_numpy_agg, dask_quantile)
    dask_quantile = dask_quantile.compute()
    unique_elements = np.unique(dask_quantile.data[np.isfinite(
        dask_quantile.data)])
    assert len(unique_elements) == k
Example #6
0
def test_quantile_not_enough_unique_values():
    agg = input_data()
    n_uniques = np.isfinite(agg.data).sum()
    k = n_uniques + 1
    result_quantile = quantile(agg, k=k)
    n_uniques_result = np.isfinite(result_quantile.data).sum()
    np.testing.assert_allclose(n_uniques_result, n_uniques)
Example #7
0
def test_quantile_numpy(result_quantile):
    k, expected_result = result_quantile
    numpy_agg = input_data()
    numpy_quantile = quantile(numpy_agg, k=k)
    general_output_checks(numpy_agg,
                          numpy_quantile,
                          expected_result,
                          verify_dtype=True)
Example #8
0
def test_quantile_cupy(result_quantile):
    k, expected_result = result_quantile
    cupy_agg = input_data('cupy')
    cupy_result = quantile(cupy_agg, k=k)
    general_output_checks(cupy_agg,
                          cupy_result,
                          expected_result,
                          verify_dtype=True)
Example #9
0
def test_quantile():
    k = 5
    n, m = 5, 5
    agg = xr.DataArray(np.arange(n * m).reshape((n, m)), dims=['x', 'y'])
    agg['x'] = np.linspace(0, n, n)
    agg['y'] = np.linspace(0, m, m)

    quantile_agg = quantile(agg, k=5)
    assert quantile_agg is not None

    unique_elements, counts_elements = np.unique(quantile_agg.data,
                                                 return_counts=True)
    assert len(unique_elements) == k
    assert len(np.unique(counts_elements)) == 1
Example #10
0
def test_quantile_dask_numpy(result_quantile):
    #     Note that dask's percentile algorithm is
    #     approximate, while numpy's is exact.
    #     This may cause some differences between
    #     results of vanilla numpy and
    #     dask version of the input agg.
    #     https://github.com/dask/dask/issues/3099

    dask_numpy_agg = input_data('dask+numpy')
    k, expected_result = result_quantile
    dask_quantile = quantile(dask_numpy_agg, k=k)
    general_output_checks(dask_numpy_agg, dask_quantile)
    dask_quantile = dask_quantile.compute()
    unique_elements = np.unique(dask_quantile.data[np.isfinite(
        dask_quantile.data)])
    assert len(unique_elements) == k