Skip to content

Raise NotImplementedError when trying to convert MaskedArrays #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pytensor/scalar/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pytensor/tensor/sharedvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.",
Expand Down
13 changes: 13 additions & 0 deletions tests/tensor/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/tensor/test_sharedvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)