|
7 | 7 | import pytest
|
8 | 8 | from numpy import array, nan
|
9 | 9 |
|
10 |
| -from xarray import DataArray, Dataset, cftime_range, concat |
| 10 | +from xarray import DataArray, Dataset, cftime_range, concat, testing |
11 | 11 | from xarray.core import dtypes, duck_array_ops
|
12 | 12 | from xarray.core.duck_array_ops import (
|
13 | 13 | array_notnull_equiv,
|
@@ -895,26 +895,44 @@ def test_push_dask():
|
895 | 895 | np.testing.assert_equal(actual, expected)
|
896 | 896 |
|
897 | 897 |
|
898 |
| -@pytest.mark.parametrize("shape", [(200, 1), (10, 10, 2), (4, 50)]) |
| 898 | +@pytest.mark.parametrize("shape", [(200), (10, 10, 2), (4, 50)]) |
899 | 899 | @pytest.mark.parametrize("tolerance", [1e-2, 1e-4, 1e-6])
|
900 |
| -@pytest.mark.parametrize("dask", [False, True]) |
901 |
| -def test_isin_tolerance(shape, tolerance, dask): |
902 |
| - if dask and not has_dask: |
| 900 | +@pytest.mark.parametrize("dask_for_A", [True, False]) |
| 901 | +@pytest.mark.parametrize("dask_for_B", [True, False]) |
| 902 | +def test_isin_tolerance(shape, tolerance, dask_for_A, dask_for_B): |
| 903 | + if (dask_for_A or dask_for_B) and not has_dask: |
903 | 904 | pytest.skip("requires dask")
|
904 | 905 |
|
905 |
| - in_margin = tolerance / 2 # within acceptable margin |
| 906 | + in_margin = tolerance / 2 # measure within acceptable margin |
906 | 907 | arrayA = np.arange(-10.0, 10.0, 0.1).reshape(shape)
|
907 |
| - |
908 | 908 | expected = np.array([item % 2 == 0 for item in range(0, arrayA.size)]).reshape(
|
909 | 909 | shape
|
910 | 910 | )
|
911 |
| - for offset_direction in [1, -1]: |
912 |
| - # generate test set |
913 |
| - arrayB = -99 * (~expected) + (in_margin + arrayA * expected) |
914 |
| - if dask: |
915 |
| - import dask.array |
916 |
| - |
917 |
| - arrayB = dask.array.from_array(arrayB) |
918 |
| - |
919 |
| - actual = isin_tolerance(arrayA, arrayB, tolerance) |
920 |
| - np.testing.assert_equal(actual, expected) |
| 911 | + if dask_for_A or dask_for_B: # tests including dask arrays |
| 912 | + import dask.array |
| 913 | + |
| 914 | + if dask_for_A: |
| 915 | + arrayA = dask.array.from_array(arrayA) |
| 916 | + expected = dask.array.from_array(expected) |
| 917 | + for offset_direction in [1, -1]: |
| 918 | + # generate test set |
| 919 | + if dask_for_B: |
| 920 | + arrayB = dask.array.from_array( |
| 921 | + [-99 * (~expected) + (in_margin + arrayA * expected)] |
| 922 | + ) |
| 923 | + else: |
| 924 | + arrayB = -99 * (~expected) + (in_margin + arrayA * expected) |
| 925 | + # test function |
| 926 | + actual = isin_tolerance(arrayA, arrayB, tolerance) |
| 927 | + |
| 928 | + testing.assert_duckarray_equal(actual, expected) |
| 929 | + assert type(actual) == type(expected) |
| 930 | + else: # test only using numpy |
| 931 | + for offset_direction in [1, -1]: |
| 932 | + # generate test set |
| 933 | + arrayB = -99 * (~expected) + (in_margin + arrayA * expected) |
| 934 | + # test function |
| 935 | + actual = isin_tolerance(arrayA, arrayB, tolerance) |
| 936 | + |
| 937 | + testing.assert_duckarray_equal(actual, expected) |
| 938 | + assert type(actual) == type(expected) |
0 commit comments