Skip to content

String representation of exotic masked structured arrays with ndim=0 fails with ValueError #6729

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
gerritholl opened this issue Nov 25, 2015 · 1 comment · Fixed by #6733

Comments

@gerritholl
Copy link
Contributor

Even after the fixes in pull request #6094, there are still some cases where string representation fails, in particular where some members of a structured array are multi-dimensional:

In [1]: ar = array((0, [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], 0.0), dtype="int, (2,3)float, float")

In [2]: arm = ma.masked_array(ar)

In [3]: x = str(arm)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-f98bbc78fe11> in <module>()
----> 1 x = str(arm)

/home/users/gholl/venv/stable-3.5/lib/python3.5/site-packages/numpy/ma/core.py in __str__(self)
   3698                 if m.shape == ():
   3699                     if m.dtype.names:
-> 3700                         m = m.view((bool, len(m.dtype)))
   3701                         if m.any():
   3702                             return str(tuple((f if _m else _d) for _d, _m in

ValueError: new type not compatible with array.

And without the m = m.view(...) line, m.any() would fail with TypeError: cannot perform reduce with flexible type.

A bit more of an overview, illustrating what we would like to see:

In [26]: m1 = ma.masked_array(data=[(0, [0, 0], 0)], mask=[(True,[False, True],True)], dtype="int, (2)float, int")

In [27]: m2 = ma.masked_array(data=(0, [0, 0], 0), mask=(True,[False, True],True), dtype="int, (2)float, int")

In [28]: str(m1)
Out[28]: '[(--, [0.0, --], --)]'

In [29]: str(m1[0])
Out[29]: '(--, [0.0, --], --)'

In [30]: str(m2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-3ae8d0bbdb8a> in <module>()
----> 1 str(m2)

/home/users/gholl/venv/stable-3.5/lib/python3.5/site-packages/numpy/ma/core.py in __str__(self)
   3698                 if m.shape == ():
   3699                     if m.dtype.names:
-> 3700                         m = m.view((bool, len(m.dtype)))
   3701                         if m.any():
   3702                             return str(tuple((f if _m else _d) for _d, _m in

ValueError: new type not compatible with array.

In [34]: numpy.ma.core.mvoid.__str__(m2)
Out[34]: '(--, [0.0, --], --)'
@gerritholl
Copy link
Contributor Author

The stack trace was off in the first version of this report, now it points to the correct place.

@gerritholl gerritholl changed the title String representation of some exotic masked structured arrays still fails String representation of exotic masked structured arrays with ndim=0 fails with ValueError Nov 26, 2015
gerritholl added a commit to gerritholl/numpy that referenced this issue Dec 1, 2015
Fix representation of a structured masked array with dimension zero.
The effect of representing a masked array with dimension zero is now
similar to respresenting an mvoid.  This commit fixes numpy#6729.
charris added a commit that referenced this issue Dec 1, 2015
colbych added a commit to colbych/numpy that referenced this issue Dec 6, 2015
* 'master' of git://github.com/numpy/numpy: (24 commits)
  BENCH: allow benchmark suite to run on Python 3
  TST: test f2py, fallback on f2py2.7 etc., fixes numpy#6718
  BUG: link cblas library if cblas is detected
  BUG/TST: Fix for numpy#6724, make numpy.ma.mvoid consistent with numpy.void
  BUG/TST: Fix numpy#6760 by correctly describing mask on nested subdtypes
  BUG: resizing empty array with complex dtype failed
  DOC: Add changelog for numpy#6734 and numpy#6748.
  Use integer division to avoid casting to int.
  Allow to change the maximum width with a class variable.
  Add some tests for mask creation with mask=True or False.
  Test that the mask dtype if MaskType before using np.zeros/ones
  BUG/TST: Fix for numpy#6729
  ENH: Avoid memory peak and useless computations when printing a MaskedArray.
  ENH: Avoid memory peak when creating a MaskedArray with mask=True/False (numpy#6732).
  BUG: Readd fallback CBLAS detection on linux.
  TST: Fix travis-ci test for numpy wheels.
  MAINT: Localize variables only used with relaxed stride checking.
  BUG: Fix for numpy#6719
  MAINT: enable Werror=vla in travis
  BUG: Include relevant files from numpy/linalg/lapack_lite in sdist.
  ...
jaimefrio pushed a commit to jaimefrio/numpy that referenced this issue Mar 22, 2016
Fix representation of a structured masked array with dimension zero.
The effect of representing a masked array with dimension zero is now
similar to respresenting an mvoid.  This commit fixes numpy#6729.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants