From 3bbc1f767238cfd97f450911e9dfb343bad02cb7 Mon Sep 17 00:00:00 2001 From: Ricardo Vieira Date: Wed, 29 Mar 2023 20:12:10 +0200 Subject: [PATCH] Raise NotImplementedError when trying to convert MaskedArrays --- pytensor/scalar/basic.py | 3 +++ pytensor/tensor/sharedvar.py | 3 +++ tests/tensor/test_basic.py | 13 +++++++++++++ tests/tensor/test_sharedvar.py | 6 ++++++ 4 files changed, 25 insertions(+) diff --git a/pytensor/scalar/basic.py b/pytensor/scalar/basic.py index 36f1527c66..b0639ff588 100644 --- a/pytensor/scalar/basic.py +++ b/pytensor/scalar/basic.py @@ -240,6 +240,9 @@ def convert(x, dtype=None): The dtype to use for the conversion of `x`. """ + if isinstance(x, np.ma.MaskedArray): + raise NotImplementedError("MaskedArrays are not supported") + if dtype is not None: # in this case, the semantics are that the caller is forcing the dtype x_ = _asarray(x, dtype=dtype) diff --git a/pytensor/tensor/sharedvar.py b/pytensor/tensor/sharedvar.py index 5620383670..5976a43b58 100644 --- a/pytensor/tensor/sharedvar.py +++ b/pytensor/tensor/sharedvar.py @@ -65,6 +65,9 @@ def tensor_constructor( optional `shape` argument will override this default. """ + if isinstance(value, np.ma.MaskedArray): + raise NotImplementedError("MaskedArrays are not supported") + if broadcastable is not None: warnings.warn( "The `broadcastable` keyword is deprecated; use `shape`.", diff --git a/tests/tensor/test_basic.py b/tests/tensor/test_basic.py index a3f3177229..cd19bd6164 100644 --- a/tests/tensor/test_basic.py +++ b/tests/tensor/test_basic.py @@ -537,6 +537,12 @@ def test_constant(): assert np.array_equal(z.data, x_data) +def test_constant_masked_array_not_implemented(): + x = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3) + with pytest.raises(NotImplementedError, match="MaskedArrays are not supported"): + constant(x) + + class TestAsTensorVariable: """ Unit test for ensuring that as_tensor_variable handles Apply objects @@ -688,6 +694,13 @@ def make_node(self, a, b): with pytest.raises(TypeError): at.as_tensor(TestOp(matrix(), matrix())) + def test_masked_array_not_implemented( + self, + ): + x = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3) + with pytest.raises(NotImplementedError, match="MaskedArrays are not supported"): + at.as_tensor(x) + class TestAlloc: dtype = config.floatX diff --git a/tests/tensor/test_sharedvar.py b/tests/tensor/test_sharedvar.py index e207e8aad3..547a73dea2 100644 --- a/tests/tensor/test_sharedvar.py +++ b/tests/tensor/test_sharedvar.py @@ -688,3 +688,9 @@ def test_scalar_shared_options(): def test_get_vector_length(): x = pytensor.shared(np.array((2, 3, 4, 5))) assert get_vector_length(x) == 4 + + +def test_shared_masked_array_not_implemented(): + x = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3) + with pytest.raises(NotImplementedError, match="MaskedArrays are not supported"): + pytensor.shared(x)