[Numpy-discussion] RuntimeWarning: divide by zero encountered in log
Neal Becker
ndbecker2 at gmail.com
Fri May 17 09:14:35 EDT 2013
Neal Becker wrote:
> Nathaniel Smith wrote:
>
>> On 16 May 2013 19:48, "Jonathan Helmus" <jjhelmus at gmail.com> wrote:
>>>
>>> On 05/16/2013 01:42 PM, Neal Becker wrote:
>>> > Is there a way to get a traceback instead of just printing the
>>> > line that triggered the error?
>>> >
>>> > _______________________________________________
>>> > NumPy-Discussion mailing list
>>> > NumPy-Discussion at scipy.org
>>> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>> Neal,
>>>
>>> Look at the numpy.seterr function. You can use it to change how
>>> floating-point errors are handled, including raising a
>>> FloatingPointError with a traceback as opposed to printing a
>>> RuntimeWarning.
>>>
>>> Example
>>>
>>> $ cat foo.py
>>> import numpy as np
>>>
>>> np.seterr(divide='raise')
>>>
>>> a = np.array([1,1,1], dtype='float32')
>>> a / 0
>>>
>>> $ python foo.py
>>> Traceback (most recent call last):
>>> File "test.py", line 6, in <module>
>>> a / 0
>>> FloatingPointError: divide by zero encountered in divide
>>
>> You also have the option of using Python's general ability to customize how
>> any warning is handled - see the 'warnings' module and -W switch.
>>
>> If you just want a traceback printed without an exception then I think you
>> can do that with np.seterr too (using np.seterrcall).
>>
>> -n
>
> I tried this:
>
> import traceback
>
> np.seterrcall (lambda a,b: traceback.print_stack)
> np.seterr (all='call')
> np.seterrcall (lambda a,b: traceback.print_stack)
>
> but it doesn't seem to do anything, I still see numpy warning as before.
Here's a trivial test:
import numpy as np
import traceback
np.seterrcall (lambda a,b: traceback.print_stack)
np.seterr (all='call')
np.seterrcall (lambda a,b: traceback.print_stack)
print 1./0
More information about the NumPy-Discussion
mailing list