Skip to content
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

does cvxpy support numpy MaskedArray? #2267

Open
qci-amos opened this issue Oct 19, 2023 · 4 comments
Open

does cvxpy support numpy MaskedArray? #2267

qci-amos opened this issue Oct 19, 2023 · 4 comments

Comments

@qci-amos
Copy link

We get the exception:

../../../miniconda3/envs/py311/lib/python3.11/site-packages/cvxpy/atoms/sum_squares.py:33: in sum_squares
    return quad_over_lin(expr, 1)
../../../miniconda3/envs/py311/lib/python3.11/site-packages/cvxpy/atoms/quad_over_lin.py:34: in __init__
    super(quad_over_lin, self).__init__(x, y)
../../../miniconda3/envs/py311/lib/python3.11/site-packages/cvxpy/atoms/atom.py:49: in __init__
    self.args = [Atom.cast_to_const(arg) for arg in args]
../../../miniconda3/envs/py311/lib/python3.11/site-packages/cvxpy/atoms/atom.py:49: in <listcomp>
    self.args = [Atom.cast_to_const(arg) for arg in args]
../../../miniconda3/envs/py311/lib/python3.11/site-packages/cvxpy/expressions/expression.py:535: in cast_to_const
    return expr if isinstance(expr, Expression) else cvxtypes.constant()(expr)
../../../miniconda3/envs/py311/lib/python3.11/site-packages/cvxpy/expressions/constants/constant.py:48: in __init__
    self._value = intf.DEFAULT_INTF.const_to_matrix(value)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <cvxpy.interface.numpy_interface.ndarray_interface.NDArrayInterface object at 0x7f539ac02010>
value = masked_array(data=[Expression(AFFINE, UNKNOWN, (36,)),
                   Expression(AFFINE, UNKNOWN, (36,)),
        ...         Expression(AFFINE, UNKNOWN, (36,))],
             mask=False,
       fill_value='?',
            dtype=object)
convert_scalars = False

    def const_to_matrix(self, value, convert_scalars: bool = False):
        """Convert an arbitrary value into a matrix of type self.target_matrix.

        Args:
            value: The constant to be converted.
            convert_scalars: Should scalars be converted?

        Returns:
            A matrix of type self.target_matrix or a scalar.
        """
        if scipy.sparse.issparse(value):
            result = value.A
        elif isinstance(value, numpy.matrix):
            result = value.A
        elif isinstance(value, list):
            result = numpy.asarray(value).T
        else:
            result = numpy.asarray(value)
        if result.dtype in [numpy.float64] + COMPLEX_TYPES:
            return result
        else:
>           return result.astype(numpy.float64)
E           ValueError: setting an array element with a sequence.

../../../miniconda3/envs/py311/lib/python3.11/site-packages/cvxpy/interface/numpy_interface/ndarray_interface.py:56: ValueError

when we a numpy MaskedArray is used in the expression passed to the cvx.sum_squares function.

@qci-amos
Copy link
Author

This worked in pip install "cvxpy==1.1.17"

That's the most recent version I could find which worked.

@SteveDiamond
Copy link
Collaborator

This worked in pip install "cvxpy==1.1.17"

That's the most recent version I could find which worked.

Do you mean that it worked in earlier versions of cvxpy but not later versions? Please include the version of cvxpy you ran to generate the error and your OS.

@qci-amos
Copy link
Author

I think the error occurred on all versions higher than 1.1.17.

These all raised:

  • 1.1.24
  • <3
  • <4
  • latest

I tried other patch versions of >1.1.17;<1.2, however, pip couldn't find most of those so I couldn't really bisect beyond that.

@SteveDiamond
Copy link
Collaborator

That is helpful to know. I would say that cvxpy is not intended to support masked arrays at present. We will study it more and decide if supporting masked arrays is something we want in the long run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants