[Scipy-tickets] [SciPy] #1140: Numpy 'where' cannot handle masked arrays with boolean arrays for masks

SciPy Trac scipy-tickets@scipy....
Sat Jul 17 05:06:25 CDT 2010


#1140: Numpy 'where' cannot handle masked arrays with boolean arrays for masks
----------------------------+-----------------------------------------------
  Reporter:  brendanarnold  |       Owner:  somebody   
      Type:  defect         |      Status:  closed     
  Priority:  normal         |   Milestone:  Unscheduled
 Component:  Other          |     Version:  0.7.0      
Resolution:  fixed          |    Keywords:             
----------------------------+-----------------------------------------------
Changes (by rgommers):

  * status:  new => closed
  * resolution:  => fixed
  * milestone:  0.8.0 => Unscheduled


Old description:

> np.where cannot handle masked arrays with boolean arrays for masks, i.e.
> when the mask is a simple Boolean, it works okay, but when the mask is an
> array it chokes.
>
> Numpy version ...
>
> In [16]: np.__version__
> Out[16]: '1.3.0'
>
> Scipy version
>
> In [17]: scipy.__version__
> Out[17]: '0.7.1'
>
> To reproduce ...
>
> In [1]: a = np.ma.arange(5)
> In [2]: a
> Out[2]:
> masked_array(data = [0 1 2 3 4],
>              mask = False,
>        fill_value = 999999)
> In [3]: np.where(a == 1)
> Out[3]:
> (masked_array(data = [1],
>              mask = False,
>        fill_value = 999999)
> ,)
> In [4]: a.mask = a == 1
> In [5]: a
> Out[5]:
> masked_array(data = [0 -- 2 3 4],
>              mask = [False  True False False False],
>        fill_value = 999999)
> In [6]: np.where(a == 1)
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call
> last)
>
> C:\Documents and Settings\ba1224\My Documents\Python\<ipython console> in
> <module>()
>
> C:\Python26\lib\site-packages\numpy\ma\core.pyc in
> __array_finalize__(self, obj)
>    2445         # Finalize the mask ...........
>
>    2446         if self._mask is not nomask:
> -> 2447             self._mask.shape = self.shape
>    2448         return
>    2449     #..................................
>

> ValueError: total size of new array must be unchanged

New description:

 np.where cannot handle masked arrays with boolean arrays for masks, i.e.
 when the mask is a simple Boolean, it works okay, but when the mask is an
 array it chokes.

 Numpy version ...
 {{{
 In [16]: np.__version__
 Out[16]: '1.3.0'
 }}}
 Scipy version
 {{{
 In [17]: scipy.__version__
 Out[17]: '0.7.1'
 }}}
 To reproduce ...
 {{{
 In [1]: a = np.ma.arange(5)
 In [2]: a
 Out[2]:
 masked_array(data = [0 1 2 3 4],
              mask = False,
        fill_value = 999999)
 In [3]: np.where(a == 1)
 Out[3]:
 (masked_array(data = [1],
              mask = False,
        fill_value = 999999)
 ,)
 In [4]: a.mask = a == 1
 In [5]: a
 Out[5]:
 masked_array(data = [0 -- 2 3 4],
              mask = [False  True False False False],
        fill_value = 999999)
 In [6]: np.where(a == 1)
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 C:\Documents and Settings\ba1224\My Documents\Python\<ipython console> in
 <module>()

 C:\Python26\lib\site-packages\numpy\ma\core.pyc in
 __array_finalize__(self, obj)
    2445         # Finalize the mask ...........

    2446         if self._mask is not nomask:
 -> 2447             self._mask.shape = self.shape
    2448         return
    2449     #..................................


 ValueError: total size of new array must be unchanged
 }}}

--

Comment:

 This is a numpy issue. It's also fixed:
 {{{
 >>> a.mask = a == 1
 >>> a
 masked_array(data = [0 -- 2 3 4],
              mask = [False  True False False False],
        fill_value = 999999)

 >>> np.where(a == 1)
 (masked_array(data = [],
              mask = False,
        fill_value = 999999)
 ,)
 >>> np.__version__
 '1.4.1'
 }}}

-- 
Ticket URL: <http://projects.scipy.org/scipy/ticket/1140#comment:1>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list