np.seterr doesn't work for masked array?
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 ************************************************************************************
On Fri, Dec 14, 2012 at 1:57 PM, Chao YUE <chaoyuejoy@gmail.com> wrote:
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!!
numpy.ma uses np.seterr(divide='ignore', invalid='ignore') for most of its operations, so it is overriding your settings. This is usually desirable since many of the masked values will be trip these errors spuriously even though they will be masked out in the result. -- Robert Kern
Thanks. You mean actually when numpy handle masked array, it will first treat all the base data, and then apply the mask after the treatment. and normally the base data of maksed elements will very likely to intrigure these errors, and you will see a lot errory warning or print in the process, and will make it impossible to see the error information your want to see for those elements that are not masked but still can intriguer the error you would like to see or check. I didn't realize this. It's a good to overwrite the error setting. thanks for your explanation. Chao On Fri, Dec 14, 2012 at 3:15 PM, Robert Kern <robert.kern@gmail.com> wrote:
On Fri, Dec 14, 2012 at 1:57 PM, Chao YUE <chaoyuejoy@gmail.com> wrote:
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!!
numpy.ma uses np.seterr(divide='ignore', invalid='ignore') for most of its operations, so it is overriding your settings. This is usually desirable since many of the masked values will be trip these errors spuriously even though they will be masked out in the result.
-- Robert Kern _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- *********************************************************************************** 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 ************************************************************************************
On Fri, Dec 14, 2012 at 2:40 PM, Chao YUE <chaoyuejoy@gmail.com> wrote:
Thanks. You mean actually when numpy handle masked array, it will first treat all the base data, and then apply the mask after the treatment. and normally the base data of maksed elements will very likely to intrigure these errors, and you will see a lot errory warning or print in the process, and will make it impossible to see the error information your want to see for those elements that are not masked but still can intriguer the error you would like to see or check. I didn't realize this. It's a good to overwrite the error setting.
Precisely. -- Robert Kern
participants (2)
-
Chao YUE
-
Robert Kern