[Numpy-discussion] Masked Array Usage Problems

Lane Brooks lane@brooks...
Sun Apr 11 00:00:10 CDT 2010


On Apr 10, 2010, at 5:17 AM, josef.pktd@gmail.com wrote:

> On Sat, Apr 10, 2010 at 3:49 AM, Lane Brooks <lane@brooks.nu> wrote:
>> I am trying out masked arrays for the first time and having some
>> problems. I have a 2-D image as dtype=numpy.int16
>>
>> I create a mask of all False to not mask out any pixels.
>>
>> I calculate the mean of the image original image and it comes out  
>> ~597.
>> I calculate the mean of the masked array and it comes out differently
>> around -179.  It produces the same negative mean value no matter what
>> masks I try, e.g. (all True, all False, etc).  Furthermore there  
>> are no
>> negative samples in the entire array.
>>
>> Any ideas on what am I doing wrong?
>>
>> Here is some sample code showing the behavior:
>>
>> In [1]: img.dtype, img.shape
>> Out[1]: (dtype('int16'), (3200, 3456))
>>
>> In [2]: mask = numpy.zeros(img.shape, dtype=bool)
>>
>> In [3]: imgma = ma.masked_array(img, mask)
>>
>> In [4]: img.mean()
>> Out[4]: 597.15437617549185
>>
>> In [5]: imgma.mean()
>> Out[5]: -179.56858678747108
>>
>> In [6]: imgma.min()
>> Out[6]: 25
>>
>> In [7]: numpy.__version__
>> Out[7]: '1.3.0'
>>
>> In [8]: numpy.ma.__version__
>> Out[8]: '1.0'
>
> Just a guess untill Pierre replies:
>
> It looks to me like an integer overflow bug. Can you try
>
> imgma.mean(dtype=float)
>
> to do the accumulation with floating points?
>
> Josef
>>

Indeed using dtype=float solved the problem. The numpy.mean doc string  
says the default accumulator type for all int types is a float. Why is  
ma.mean different, especially since the    ma.mean doc string says to  
see the numpy.mean doc string?

Thanks
Lane 


More information about the NumPy-Discussion mailing list