Skip to content

Broadcasting with masked array doesn't work with divide (after tile instruction) (Trac #2035) #2627

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

Closed
numpy-gitbot opened this issue Oct 19, 2012 · 8 comments

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/2035 on 2012-02-02 by trac user elephantterrible, assigned to unknown.

Broadcasting in a division does not work after a tile instruction.

See attached script to reproduce.

If the tile instruction is commented out, the division works.
Division also works if numpy.divide() is used, or if there's no masked value.

Python gives the following error:

Traceback (most recent call last):
  File "ma_tiling_issue.py", line 18, in <module>
    a/100. #raises ValueError: invalid return array shape
  File "/usr/local/lib/python2.7/dist-packages/numpy/ma/core.py", line 3654, in __div__
    return divide(self, other)
  File "/usr/local/lib/python2.7/dist-packages/numpy/ma/core.py", line 1078, in __call__
    m |= ma
ValueError: non-broadcastable output operand with shape (3,3,3) doesn't match the broadcast shape (1,3,3,3)

The error occurs in all numpy versions (1.5.1, 1.6.1, 2.0.0.dev) with python 2.7.1

The original bug discussion is on numpy-discuss:
http://www.mail-archive.com/numpy-discussion@scipy.org/msg35146.html

@numpy-gitbot
Copy link
Author

Attachment added by trac user elephantterrible on 2012-02-02: ma_tiling_issue.py

@numpy-gitbot
Copy link
Author

@bsouthey wrote on 2012-02-02

The error appears to be in call to 'numpy.core.numeric.array' at line 827 numpy/lib/shape_base.py:

c = _nx.array(A,copy=False,subok=True,ndmin=d)

Also, setting 'copy=True' in the call does not change anything.

>>> import numpy as np
>>> np.__version__
'2.0.0.dev-55472ca'
>>> import numpy.core.numeric as _nx
>>> b=np.ma.array([[1,2,3],[4,5,6]], mask=[[1,0,0],[1,0,0]])
>>> d=4
>>> c = _nx.array(b,copy=True,subok=True,ndmin=d)
>>> b/4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/numpy/ma/core.py", line 3654, in __div__
    return divide(self, other)
  File "/usr/lib64/python2.7/site-packages/numpy/ma/core.py", line 1078, in __call__
    m |= ma
ValueError: non-broadcastable output operand with shape (2,3) doesn't match the broadcast shape (1,1,2,3)
>>> c = _nx.array(b,copy=False,subok=True,ndmin=d)
>>> b/4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/numpy/ma/core.py", line 3654, in __div__
    return divide(self, other)
  File "/usr/lib64/python2.7/site-packages/numpy/ma/core.py", line 1078, in __call__
    m |= ma
ValueError: non-broadcastable output operand with shape (2,3) doesn't match the broadcast shape (1,1,2,3)
>>> 

@numpy-gitbot
Copy link
Author

@rgommers wrote on 2012-02-20

@numpy-gitbot
Copy link
Author

Milestone changed to Unscheduled by @rgommers on 2012-02-20

@charris
Copy link
Member

charris commented Feb 21, 2014

Still open 1.9-devel.

@jjhelmus
Copy link
Contributor

A similar issue with np.tile modifying the mask of MaskedArrays was reported in #3140

@cmarmo
Copy link
Contributor

cmarmo commented Oct 13, 2022

Hello, I have run the snippet of code and it is not failing anymore: possibly fixed at the same time as #3140?

>>> import numpy as np
>>> np.__version__
'1.24.0.dev0+934.gdb7414b7f'
>>> import numpy.core.numeric as _nx
>>> b=np.ma.array([[1,2,3],[4,5,6]], mask=[[1,0,0],[1,0,0]])
>>> d=4
>>> c = _nx.array(b,copy=True,subok=True,ndmin=d)
>>> b/4
masked_array(
  data=[[--, 0.5, 0.75],
        [--, 1.25, 1.5]],
  mask=[[ True, False, False],
        [ True, False, False]],
  fill_value=1e+20)
>>> c = _nx.array(b,copy=False,subok=True,ndmin=d)
>>> b/4
masked_array(
  data=[[--, 0.5, 0.75],
        [--, 1.25, 1.5]],
  mask=[[ True, False, False],
        [ True, False, False]],
  fill_value=1e+20)

Thanks for listening.

@mattip
Copy link
Member

mattip commented Oct 13, 2022

OK, closing. Thanks for checking.

@mattip mattip closed this as completed Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants