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

笑着哭i 提交于 2019-12-10 01:47:54

问题


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?


回答1:


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.




回答2:


If you have access to the numpy source, you should be able to find the line that prints that warning (using grep, etc) and edit the corresponding file to force an error (using an assertion, for example) when an invalid value is passed. That will give you a stack trace pointing to the place in your code that called log with the improper value.

I had a brief look in my numpy source, and couldn't find anything that matches the warning you described though (my version of numpy is older than yours, though).

>>> import numpy
>>> numpy.log(0)
-inf
>>> numpy.__version__
'1.3.0'

Is it possible that you're calling some other log function that isn't in numpy? For example, here is one that actually throws an exception when given invalid input.

>>> import math
>>> math.log(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error


来源:https://stackoverflow.com/questions/4190817/tracing-python-warnings-errors-to-a-line-number-in-numpy-and-scipy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!