Dear all,
I tried to capture the zero divide error when I divide a masked array by
another. It seems that np.seterr is not working for masked array?
when I do np.divide on two masked array, it directly put the zero divides
part as being masked. The np.seterr works if the two arrays for dividing
are not masked arrays.
could anyone explain? thanks!!
np.__version__ = 1.6.2
In [87]: np.seterr(all='print')
Out[87]: {'divide': 'print', 'invalid': 'print', 'over': 'print', 'under':
'print'}
In [88]: a = np.arange(8,dtype=float).reshape(2,4)
In [89]: b = np.ma.masked_less(a,4)
In [90]: b[1,-2:] = 0.
In [91]: b
Out[91]:
masked_array(data =
[[-- -- -- --]
[4.0 5.0 0.0 0.0]],
mask =
[[ True True True True]
[False False False False]],
fill_value = 1e+20)
In [92]: c = a.copy()
In [93]: c[1,-2:] = 0.
In [94]: c
Out[94]:
array([[ 0., 1., 2., 3.],
[ 4., 5., 0., 0.]])
In [95]: np.divide(a,b)
Warning: divide by zero encountered in divide
Out[95]:
masked_array(data =
[[-- -- -- --]
[1.0 1.0 -- --]],
mask =
[[ True True True True]
[False False True True]],
fill_value = 1e+20)
In [96]: np.divide(a,c)
Warning: divide by zero encountered in divide
Out[96]:
array([[ nan, 1., 1., 1.],
[ 1., 1., inf, inf]])
Chao
--
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************