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

╄→гoц情女王★ 提交于 2019-12-05 00:43:40

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.

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