Search code examples
pythonnumpyscipynumericscientific-computing

Tracing Python warnings/errors to a line number in numpy and scipy


I am getting the error:

Warning: invalid value encountered in log

From Python and I believe the error is thrown by numpy (using version 1.5.0). However, since I am calling the "log" function in several places, I'm not sure where the error is coming from. Is there a way to get numpy to print the line number that generated this error?

I assume the warning is caused by taking the log of a number that is small enough to be rounded to 0 or smaller (negative). Is that right? What is the usual origin of these warnings?


Solution

  • Putting np.seterr(invalid='raise') in your code (before the errant log call) will cause numpy to raise an exception instead of issuing a warning. That will give you a traceback error message and tell you the line Python was executing when the error occurred.