Skip to content

Commit 33deba8

Browse files
ricardoV94michaelosthege
authored andcommitted
Raise NotImplementedError when trying to convert MaskedArrays
1 parent cbef7d5 commit 33deba8

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

pytensor/scalar/basic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ def convert(x, dtype=None):
240240
The dtype to use for the conversion of `x`.
241241
242242
"""
243+
if isinstance(x, np.ma.MaskedArray):
244+
raise NotImplementedError("MaskedArrays are not supported")
245+
243246
if dtype is not None:
244247
# in this case, the semantics are that the caller is forcing the dtype
245248
x_ = _asarray(x, dtype=dtype)

pytensor/tensor/sharedvar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def tensor_constructor(
6565
optional `shape` argument will override this default.
6666
6767
"""
68+
if isinstance(value, np.ma.MaskedArray):
69+
raise NotImplementedError("MaskedArrays are not supported")
70+
6871
if broadcastable is not None:
6972
warnings.warn(
7073
"The `broadcastable` keyword is deprecated; use `shape`.",

tests/tensor/test_basic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ def test_constant():
537537
assert np.array_equal(z.data, x_data)
538538

539539

540+
def test_constant_masked_array_not_implemented():
541+
x = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3)
542+
with pytest.raises(NotImplementedError, match="MaskedArrays are not supported"):
543+
constant(x)
544+
545+
540546
class TestAsTensorVariable:
541547
"""
542548
Unit test for ensuring that as_tensor_variable handles Apply objects
@@ -688,6 +694,13 @@ def make_node(self, a, b):
688694
with pytest.raises(TypeError):
689695
at.as_tensor(TestOp(matrix(), matrix()))
690696

697+
def test_masked_array_not_implemented(
698+
self,
699+
):
700+
x = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3)
701+
with pytest.raises(NotImplementedError, match="MaskedArrays are not supported"):
702+
at.as_tensor(x)
703+
691704

692705
class TestAlloc:
693706
dtype = config.floatX

tests/tensor/test_sharedvar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,9 @@ def test_scalar_shared_options():
688688
def test_get_vector_length():
689689
x = pytensor.shared(np.array((2, 3, 4, 5)))
690690
assert get_vector_length(x) == 4
691+
692+
693+
def test_shared_masked_array_not_implemented():
694+
x = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3)
695+
with pytest.raises(NotImplementedError, match="MaskedArrays are not supported"):
696+
pytensor.shared(x)

0 commit comments

Comments
 (0)