From 59d0120aeba4da8a3f4da1ccc0d5b80f9fd9f3e4 Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Sat, 1 May 2021 21:58:48 -0600 Subject: [PATCH 1/6] Parametrize test for setitem (for dask array) --- test_sample.py | 9 +++++ xarray/tests/test_dask.py | 74 ++++++++++++++++++++++----------------- 2 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 test_sample.py diff --git a/test_sample.py b/test_sample.py new file mode 100644 index 00000000000..680f25d4529 --- /dev/null +++ b/test_sample.py @@ -0,0 +1,9 @@ +import pytest + + +@pytest.mark.parametrize("a", [1, 2]) +@pytest.mark.parametrize("b", ["x"]) +@pytest.mark.parametrize("c", [5, 10]) +def test_hello(a, b, c): + assert 2 == 2 + print(a, b, c) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 8b875662ab8..29a6731aa41 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -110,39 +110,47 @@ def test_indexing(self): self.assertLazyAndIdentical(u[0], v[0]) self.assertLazyAndIdentical(u[:1], v[:1]) self.assertLazyAndIdentical(u[[0, 1], [0, 1, 2]], v[[0, 1], [0, 1, 2]]) - if LooseVersion(dask.__version__) >= LooseVersion("2021.04.0+17"): - # TODO: use @pytest.mark.parametrize to parametrize this - arr = Variable(("x"), da.array([1, 2, 3, 4])) - expected = Variable(("x"), da.array([99, 2, 3, 4])) - arr[0] = 99 # Indexing by integers - assert_identical(arr, expected) - arr = Variable(("x"), da.array([1, 2, 3, 4])) - expected = Variable(("x"), da.array([99, 99, 99, 4])) - arr[2::-1] = 99 # Indexing by slices - assert_identical(arr, expected) - arr = Variable(("x"), da.array([1, 2, 3, 4])) - expected = Variable(("x"), da.array([99, 99, 3, 99])) - arr[[0, -1, 1]] = 99 # Indexing by a list of integers - assert_identical(arr, expected) - arr = Variable(("x"), da.array([1, 2, 3, 4])) - expected = Variable(("x"), da.array([99, 99, 99, 4])) - arr[np.arange(3)] = 99 # Indexing by a 1-d numpy array of integers - assert_identical(arr, expected) - arr = Variable(("x"), da.array([1, 2, 3, 4])) - expected = Variable(("x"), da.array([1, 99, 99, 99])) - arr[[False, True, True, True]] = 99 # Indexing by a list of booleans - assert_identical(arr, expected) - arr = Variable(("x"), da.array([1, 2, 3, 4])) - expected = Variable(("x"), da.array([1, 99, 99, 99])) - arr[np.arange(4) > 0] = 99 # Indexing by a 1-d numpy array of booleans - assert_identical(arr, expected) - arr = Variable(("x"), da.array([1, 2, 3, 4])) - expected = Variable(("x"), da.array([99, 99, 99, 99])) - arr[arr > 0] = 99 # Indexing by one broadcastable Array of booleans - assert_identical(arr, expected) - else: - with pytest.raises(TypeError, match=r"stored in a dask array"): - v[:1] = 0 + + @pytest.mark.skipif( + LooseVersion(dask.__version__) < LooseVersion("2021.04.1"), + reason="Requires dask v2021.04.1 or later", + ) + @pytest.mark.parametrize( + "input_data, expected_data, index, index_data", + [ + (da.array([1, 2, 3, 4]), da.array([99, 2, 3, 4]), 0, 99), + (da.array([1, 2, 3, 4]), da.array([99, 99, 99, 4]), slice(2, None, -1), 99), + (da.array([1, 2, 3, 4]), da.array([99, 99, 3, 99]), [0, -1, 1], 99), + (da.array([1, 2, 3, 4]), da.array([99, 99, 99, 4]), np.arange(3), 99), + ( + da.array([1, 2, 3, 4]), + da.array([1, 99, 99, 99]), + [False, True, True, True], + 99, + ), + (da.array([1, 2, 3, 4]), da.array([1, 99, 99, 99]), np.arange(4) > 0, 99), + ( + da.array([1, 2, 3, 4]), + da.array([99, 99, 99, 99]), + Variable(("x"), da.array([1, 2, 3, 4])) > 0, + 99, + ), + ], + ) + def test_setitem_dask_array(self, input_data, expected_data, index, index_data): + arr = Variable(("x"), input_data) + expected = Variable(("x"), expected_data) + arr[index] = index_data + assert_identical(arr, expected) + + @pytest.mark.skipif( + LooseVersion(dask.__version__) >= LooseVersion("2021.04.1"), + reason="Requires dask v2021.04.0 or earlier", + ) + def test_setitem_dask_array_error(self): + with pytest.raises(TypeError, match=r"stored in a dask array"): + v = self.lazy_var + v[:1] = 0 def test_squeeze(self): u = self.eager_var From 47c9db4f4770f5cd5996c62ffa2681073aa7b508 Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Sat, 1 May 2021 22:03:09 -0600 Subject: [PATCH 2/6] Remove accidentally committed file --- test_sample.py | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 test_sample.py diff --git a/test_sample.py b/test_sample.py deleted file mode 100644 index 680f25d4529..00000000000 --- a/test_sample.py +++ /dev/null @@ -1,9 +0,0 @@ -import pytest - - -@pytest.mark.parametrize("a", [1, 2]) -@pytest.mark.parametrize("b", ["x"]) -@pytest.mark.parametrize("c", [5, 10]) -def test_hello(a, b, c): - assert 2 == 2 - print(a, b, c) From 81a0455132c8d5c3f9e5cc8b0de183469c1d7903 Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Sun, 2 May 2021 13:30:27 -0600 Subject: [PATCH 3/6] Remove input_data from parameters --- xarray/tests/test_dask.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 29a6731aa41..9539287169e 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -116,29 +116,27 @@ def test_indexing(self): reason="Requires dask v2021.04.1 or later", ) @pytest.mark.parametrize( - "input_data, expected_data, index, index_data", + "expected_data, index, index_data", [ - (da.array([1, 2, 3, 4]), da.array([99, 2, 3, 4]), 0, 99), - (da.array([1, 2, 3, 4]), da.array([99, 99, 99, 4]), slice(2, None, -1), 99), - (da.array([1, 2, 3, 4]), da.array([99, 99, 3, 99]), [0, -1, 1], 99), - (da.array([1, 2, 3, 4]), da.array([99, 99, 99, 4]), np.arange(3), 99), + (da.array([99, 2, 3, 4]), 0, 99), + (da.array([99, 99, 99, 4]), slice(2, None, -1), 99), + (da.array([99, 99, 3, 99]), [0, -1, 1], 99), + (da.array([99, 99, 99, 4]), np.arange(3), 99), ( - da.array([1, 2, 3, 4]), da.array([1, 99, 99, 99]), [False, True, True, True], 99, ), (da.array([1, 2, 3, 4]), da.array([1, 99, 99, 99]), np.arange(4) > 0, 99), ( - da.array([1, 2, 3, 4]), da.array([99, 99, 99, 99]), Variable(("x"), da.array([1, 2, 3, 4])) > 0, 99, ), ], ) - def test_setitem_dask_array(self, input_data, expected_data, index, index_data): - arr = Variable(("x"), input_data) + def test_setitem_dask_array(self, expected_data, index, index_data): + arr = Variable(("x"), da.array([1, 2, 3, 4])) expected = Variable(("x"), expected_data) arr[index] = index_data assert_identical(arr, expected) From bbcaeef7e8f747628e8bb9df764d81ee2ee7e922 Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Sun, 2 May 2021 13:34:18 -0600 Subject: [PATCH 4/6] Remove index_data from parameters --- xarray/tests/test_dask.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 9539287169e..d25ed86c5d7 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -116,29 +116,24 @@ def test_indexing(self): reason="Requires dask v2021.04.1 or later", ) @pytest.mark.parametrize( - "expected_data, index, index_data", + "expected_data, index", [ - (da.array([99, 2, 3, 4]), 0, 99), - (da.array([99, 99, 99, 4]), slice(2, None, -1), 99), - (da.array([99, 99, 3, 99]), [0, -1, 1], 99), - (da.array([99, 99, 99, 4]), np.arange(3), 99), ( - da.array([1, 99, 99, 99]), - [False, True, True, True], - 99, - ), - (da.array([1, 2, 3, 4]), da.array([1, 99, 99, 99]), np.arange(4) > 0, 99), - ( - da.array([99, 99, 99, 99]), - Variable(("x"), da.array([1, 2, 3, 4])) > 0, - 99, + da.array([99, 2, 3, 4]), + 0, ), + (da.array([99, 99, 99, 4]), slice(2, None, -1)), + (da.array([99, 99, 3, 99]), [0, -1, 1]), + (da.array([99, 99, 99, 4]), np.arange(3)), + (da.array([1, 99, 99, 99]), [False, True, True, True]), + (da.array([1, 2, 3, 4]), da.array([1, 99, 99, 99]), np.arange(4) > 0), + (da.array([99, 99, 99, 99]), Variable(("x"), da.array([1, 2, 3, 4])) > 0), ], ) - def test_setitem_dask_array(self, expected_data, index, index_data): + def test_setitem_dask_array(self, expected_data, index): arr = Variable(("x"), da.array([1, 2, 3, 4])) expected = Variable(("x"), expected_data) - arr[index] = index_data + arr[index] = 99 assert_identical(arr, expected) @pytest.mark.skipif( From 40579b5082b9d3dd55d91c0218f714b44c26561b Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Sun, 2 May 2021 13:39:19 -0600 Subject: [PATCH 5/6] Use release version --- xarray/core/indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index f4f76efdcf7..82e4530f428 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -1388,7 +1388,7 @@ def __getitem__(self, key): return value def __setitem__(self, key, value): - if DASK_VERSION >= "2021.04.0+17": + if DASK_VERSION >= "2021.04.1": if isinstance(key, BasicIndexer): self.array[key.tuple] = value elif isinstance(key, VectorizedIndexer): From 67a6e91524aac73e3d6239db44d340b0d640c50e Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Sun, 2 May 2021 13:54:12 -0600 Subject: [PATCH 6/6] Fix typo --- xarray/tests/test_dask.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index d25ed86c5d7..f1896ebe652 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -118,15 +118,12 @@ def test_indexing(self): @pytest.mark.parametrize( "expected_data, index", [ - ( - da.array([99, 2, 3, 4]), - 0, - ), + (da.array([99, 2, 3, 4]), 0), (da.array([99, 99, 99, 4]), slice(2, None, -1)), (da.array([99, 99, 3, 99]), [0, -1, 1]), (da.array([99, 99, 99, 4]), np.arange(3)), (da.array([1, 99, 99, 99]), [False, True, True, True]), - (da.array([1, 2, 3, 4]), da.array([1, 99, 99, 99]), np.arange(4) > 0), + (da.array([1, 99, 99, 99]), np.arange(4) > 0), (da.array([99, 99, 99, 99]), Variable(("x"), da.array([1, 2, 3, 4])) > 0), ], )