overflow and other warnings

Hello,

I am attempting to write a small simulation to reproduce something
from a paper. However, I'm having a whale of a time getting it to
work (as far as I have been able to tell, the calculations from the
paper at least are identical to what I am trying to do, which makes
this all the more frustrating). I recently discovered that if I import
pylab after importing numpy, I get tons of warnings:

Warning: divide by zero encountered in divide
Warning: invalid value encountered in multiply
Warning: overflow encountered in long_scalars

with the last occurring far most often (interestingly, I do not get
these warnings if pylab is imported before numpy). My question then is
two fold:

(1) Why do I only get these warnings depending on when I import pylab?

(2) (a more general python question) How can I get these warning
    messages to give me *useful* information, such as where the problems
    are happening?

Thanks,

···

--
Michael V. DePalatis
Georgia Institute of Technology
School of Physics
837 State Street
Atlanta, GA 30332-0430

em vee dee at gatech dot edu
http://mike.depalatis.net

...

I recently discovered that if I import
pylab after importing numpy, I get tons of warnings:

Warning: divide by zero encountered in divide
Warning: invalid value encountered in multiply
Warning: overflow encountered in long_scalars

with the last occurring far most often (interestingly, I do not get
these warnings if pylab is imported before numpy). My question then is
two fold:

(1) Why do I only get these warnings depending on when I import pylab?

I am not sure: with the latest svn builds I get the message both ways. Maybe the default error handling is different between the version of numpy and matplotlib you have installed.

(2) (a more general python question) How can I get these warning
    messages to give me *useful* information, such as where the problems
    are happening?

In numpy you can cause these to be exceptions by setting

numpy.seterr("raise")

Now you can see exactly where the exception happens.

If you don't want to stop execution at this point, you use a callback. This will pop-open the debugger at the point of error, but allow you to continue.

def f(err,flag):
     import pdb; pdb.set_trace()

numpy.seterr("call")
numpy.seterrcall(f)

In general, with python warnings you can change them to exceptions by using warnings.simplefilter('error',Warning) (see warnings — Warning control — Python 3.12.0 documentation for details), however numpy does not seem to issue warnings, instead opting to directly print a message. (Anyone know why the warnings library is not used? Is this a bug that should be fixed?)

Michael.

···

On 10 Nov 2007, at 7:51 AM, Michael V. DePalatis wrote:

It would be better to discuss this on the numpy mailing list.

Darren

···

On Saturday 10 November 2007 04:52:31 pm Michael McNeil Forbes wrote:

On 10 Nov 2007, at 7:51 AM, Michael V. DePalatis wrote:
...

> I recently discovered that if I import
> pylab after importing numpy, I get tons of warnings:
>
> Warning: divide by zero encountered in divide
> Warning: invalid value encountered in multiply
> Warning: overflow encountered in long_scalars
>
> with the last occurring far most often (interestingly, I do not get
> these warnings if pylab is imported before numpy). My question then is
> two fold:
>
> (1) Why do I only get these warnings depending on when I import pylab?

I am not sure: with the latest svn builds I get the message both
ways. Maybe the default error handling is different between the
version of numpy and matplotlib you have installed.

> (2) (a more general python question) How can I get these warning
> messages to give me *useful* information, such as where the
> problems
> are happening?

In numpy you can cause these to be exceptions by setting

numpy.seterr("raise")

Now you can see exactly where the exception happens.

If you don't want to stop execution at this point, you use a
callback. This will pop-open the debugger at the point of error, but
allow you to continue.

def f(err,flag):
     import pdb; pdb.set_trace()

numpy.seterr("call")
numpy.seterrcall(f)

In general, with python warnings you can change them to exceptions by
using warnings.simplefilter('error',Warning) (see http://
docs.python.org/lib/module-warnings.html for details), however numpy
does not seem to issue warnings, instead opting to directly print a
message. (Anyone know why the warnings library is not used? Is this
a bug that should be fixed?)