question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.tile modifies the mask of the inputted masked array

See original GitHub issue

Here is an example of the problem:

>>> import numpy as np
>>> import numpy.ma as ma
>>> A = ma.masked_array(np.array([1., 2.]), mask=np.array([False, False]))
>>> A
masked_array(data = [1.0 2.0],
             mask = [False False],
       fill_value = 1e+20)

>>> B = np.tile(A, (2, 1))
>>> A
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/numpy/ma/core.py", line 3553, in __repr__
    data=str(self), mask=str(self._mask),
  File "/usr/lib64/python2.7/site-packages/numpy/ma/core.py", line 3537, in __str__
    res[m] = f
ValueError: boolean index array should have 1 dimension
>>> A._data
array([ 1.,  2.])
>>> A._mask
array([[False, False]], dtype=bool)

The issue is that the call to np.tile changed A._mask from array([False, False], dtype=bool) to array([[False, False]], dtype=bool), which made the __repr___() function return the error.

Digging deeper, I found that the issue is with the call _nx.array(A,copy=False,subok=True,ndmin=d) in the tile function. Before this call, A has the correct _mask and after this call, A’s _mask changes.

Does anyone have any ideas how to fix this?

This is on Fedora-rawhide with: python-2.7.3-34.fc19.x86_64 numpy-1.7.0-1.fc19.x86_64

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, Oct 13, 2022

Thanks for the note @cmarmo. Agree, seems this has been fixed years ago! (in gh-10314)

1reaction
cmarmocommented, Oct 13, 2022

Hello, I have run the snippet of code in the description and I am under the impression that this issue can be closed?

Python 3.10.7 (main, Sep  7 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.24.0.dev0+934.gdb7414b7f'
>>> A = ma.masked_array(np.array([1., 2.]), mask=np.array([False, False]))
>>> A
masked_array(data=[1.0, 2.0],
             mask=[False, False],
       fill_value=1e+20)
>>> B = np.tile(A, (2, 1))
>>> A
masked_array(data=[1.0, 2.0],
             mask=[False, False],
       fill_value=1e+20)
>>> A._data
array([1., 2.])
>>> A._mask
array([False, False])

Thanks for listening.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The numpy.ma module — NumPy v1.24 Manual
A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either ... Convert the input to a...
Read more >
Python/Numpy: Using np.tile to tile 2D array of boolean mask ...
You can use: x = np.tile(maskArr, (3, 1, 1)) print(x). Prints: [[[False True False True] [False True True True] [ True True False...
Read more >
Masking and padding with Keras | TensorFlow Core
Masking is a way to tell sequence-processing layers that certain timesteps in an input are missing, and thus should be skipped when ...
Read more >
Indexing - | notebook.community
We can also index masks: If the index mask is an Numpy array of with data type bool , then an element is...
Read more >
Masked array operations — NumPy v1.9 Manual
Determine whether input has masked values. ma.is_mask(m), Return True if m is a valid, standard mask. ma.MaskedArray.data ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found