Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert numpy warnings to errors

Tags:

python

numpy

I am getting

C:\Anaconda\lib\site-packages\numpy\core\_methods.py:59: RuntimeWarning: Mean of empty slice.
    warnings.warn("Mean of empty slice.", RuntimeWarning)

when I run a code I wrote. This is a large codebase. The problem is, I do not see where in my code this warning was fired.

I would rather have this as an error (e.g. an exception), so that I can see where this was generated in my code.

Is it possible to have exceptions instead of warnings?

Note: I solved my problem debugging line by line. But the question still stands.

like image 885
Zargo Avatar asked Dec 09 '15 13:12

Zargo


2 Answers

Setting

numpy.seterr(all='raise')

should do the trick. You should probably check out the details though.

like image 115
fbence Avatar answered Oct 14 '22 05:10

fbence


import warnings

warnings.filterwarnings('error', "Mean of empty slice.")

# ... Your code here.

See the documentation of the warnings module for more details.

like image 24
Robert Kern Avatar answered Oct 14 '22 06:10

Robert Kern