[Numpy-discussion] Putmask/take ?

Travis Oliphant oliphant.travis at ieee.org
Fri Sep 22 12:24:00 CDT 2006


Stefan van der Walt wrote:
> On Fri, Sep 22, 2006 at 02:17:57AM -0500, Robert Kern wrote:
>   
>>> According to the putmask docstring:
>>>
>>>     a.putmask(values, mask) sets a.flat[n] = v[n] for each n where
>>>     mask.flat[n] is true. v can be scalar.
>>>
>>> This would mean that 'w' is not of the right length. 
>>>       
>> There are 4 true values in m and 4 values in w. What's the wrong
>>     
> length?
>
> The way I read the docstring, you use putmask like this:
>
> In [4]: x = N.array([1,2,3,4])
>
> In [5]: x.putmask([4,3,2,1],[1,0,0,1])
>
> In [6]: x
> Out[6]: array([4, 2, 3, 1])
>
>   
>>
>> Out[9] and Out[18] should have been the same, but elements 6 and 9 are flipped. 
>> It's pretty clear that this is a bug in .putmask().
>>     
>
> Based purely on what I read in the docstring, I would expect the above to do
>
> x[0] = w[0]
> x[6] = w[6]
> x[9] = w[9]
> x[11] = w[11]
>
> Since w is of length 4, you'll probably get indices modulo 4:
>
> w[6] == w[2] == -3
> w[9] == w[1] == -2
> w[11] == w[3] == -4
>
> Which seems to explain what you are seeing.
>   

Yes, this does explain what you are seeing.    It is the behavior of 
Numeric's putmask (where this method came from).   It does seem 
counter-intuitive, and I'm not sure what to do with it.  In some sense 
putmask should behave the same as x[m] = w.   But, on the other-hand, 
was anybody actually using the "modular" indexing "feature" of "putmask".

Here are our options:

1) "fix-it" and risk breaking code for people who used putmask and the 
modular indexing "feature," 
2) Get rid of it as a method (and keep it as a function so that 
oldnumeric can use it.)
3) Keep everything the way it is.

-Travis





More information about the Numpy-discussion mailing list