Re: [Numpy-discussion] Operations on masked items

2009-02-03 Thread Ryan May
Ryan May wrote:
 Pierre,
 
 I know you did some preliminary work on helping to make sure that doing
 operations on masked arrays doesn't change the underlying data.  I ran into 
 the
 following today.
 
 import numpy as np
 a = np.ma.array([1,2,3], mask=[False, True, False])
 b = a * 10
 c = 10 * a
 print b.data # Prints [10 2 30] Good!
 print c.data # Prints [10 10 30] Oops.
 
 I tracked it down to __call__ on the _MaskedBinaryOperation class.  If 
 there's a
 mask on the data, you use:
 
   result = np.where(m, da, self.f(da, db, *args, **kwargs))
 
 You can see that if a (and hence da) is a scalar, your masked values end up 
 with
 the value of the scalar.  If this is getting too hairy to handle not touching
 data, I understand.  I just thought I should point out the inconsistency here.

Well, I guess I hit send too soon.  Here's one easy solution (consistent with
what you did for __radd__), change the code for __rmul__ to do:

return multiply(self, other)

instead of:

return multiply(other, self)

That fixes it for me, and I don't see how it would break anything.

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Operations on masked items

2009-02-03 Thread Pierre GM

On Feb 3, 2009, at 4:00 PM, Ryan May wrote:

 Well, I guess I hit send too soon.  Here's one easy solution  
 (consistent with
 what you did for __radd__), change the code for __rmul__ to do:

   return multiply(self, other)

 instead of:

   return multiply(other, self)

 That fixes it for me, and I don't see how it would break anything.

Good call, but once again: Thou shalt not put trust in ye masked  
values [1].

  a = np.ma.array([1,2,3],mask=[0,1,0])
  b = np.ma.array([10, 20, 30], mask=[0,1,0])
  (a*b).data
array([10,  2, 90])
  (b*a).data
array([10, 20, 90])

So yes, __mul__ is not commutative when you deal w/ masked arrays (at  
least, when you try to access the data under a mask). Nothing I can  
do. Remember that preventing the underlying data to be modified is  
NEVER guaranteed...

[1] Epistle of Paul (Dubois).
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Operations on masked items

2009-02-03 Thread Ryan May
Pierre,

I know you did some preliminary work on helping to make sure that doing
operations on masked arrays doesn't change the underlying data.  I ran into the
following today.

import numpy as np
a = np.ma.array([1,2,3], mask=[False, True, False])
b = a * 10
c = 10 * a
print b.data # Prints [10 2 30] Good!
print c.data # Prints [10 10 30] Oops.

I tracked it down to __call__ on the _MaskedBinaryOperation class.  If there's a
mask on the data, you use:

result = np.where(m, da, self.f(da, db, *args, **kwargs))

You can see that if a (and hence da) is a scalar, your masked values end up with
the value of the scalar.  If this is getting too hairy to handle not touching
data, I understand.  I just thought I should point out the inconsistency here.

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Operations on masked items

2009-02-03 Thread Ryan May
Pierre GM wrote:
 On Feb 3, 2009, at 4:00 PM, Ryan May wrote:
 Well, I guess I hit send too soon.  Here's one easy solution  
 (consistent with
 what you did for __radd__), change the code for __rmul__ to do:

  return multiply(self, other)

 instead of:

  return multiply(other, self)

 That fixes it for me, and I don't see how it would break anything.
 
 Good call, but once again: Thou shalt not put trust in ye masked  
 values [1].
 
   a = np.ma.array([1,2,3],mask=[0,1,0])
   b = np.ma.array([10, 20, 30], mask=[0,1,0])
   (a*b).data
 array([10,  2, 90])
   (b*a).data
 array([10, 20, 90])
 
 So yes, __mul__ is not commutative when you deal w/ masked arrays (at  
 least, when you try to access the data under a mask). Nothing I can  
 do. Remember that preventing the underlying data to be modified is  
 NEVER guaranteed...

Fair enough.

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion