matiasq
matiasq

Reputation: 537

How do I check for numeric overflow without getting a warning in Python?

I have an expression that overflows for certain values of parameters. In this case, I have derived what the asymptotic result should be using pen and paper, and when I have such a case I just replace with my analytical expression.

At the moment my code does something like this:

values = ExpressionThatOverFlows()
# Check the ones that overflow
indOverFlow = isnan(values)
# Set them to the values I derived by pen and paper
values[indOverFlow] = derivedValues

My problem is that the I/O explodes with "warnings". I know that it is good that it warns me, but I have explicitly taken care of it so I want to silence them. Note that I do not want to silence all types of "overflow" warnings, only the ones here. I thought that something like this would work but it did not:

try:
   values = ExpressionThatOverFlows()
except Warning:
   pass
# and the rest of the code as it is

I have checked around but I just seem to find how to silence these warnings for an entire session or forever, but this is as I pointed out not what I want.

Thanks for your help, much appreciated.

EDIT: Here comes a much smaller code that generates the problem I have:

from scipy import log1p, exp
from numpy import array, isnan

a = array([0.2222, 500.3, 0.3, 700.8, 0.111])

values = log1p(-exp(-exp(10**a - 9**a)))

print values # Note the nan's

indOverflow = isnan(values)
values[indOverflow] = 0

Note how I fix the problem "manually" at the end, but what happens in the I/O is:

Warning: overflow encountered in power
Warning: overflow encountered in power
Warning: invalid value encountered in subtract

I do this kind of computations in a loop, so I want to silence these messages (as they are already fixed and furthermore they take a lot of time to print)

Upvotes: 4

Views: 4619

Answers (3)

DaveP
DaveP

Reputation: 7102

The best approach is to manually examine your expression, and find out which range of input parameters can be accurately handled by your explicit code. It is possible that significant loss of precision is occurring much sooner than numerical overflow.

You should then have an explicit "if" statement on your input variables, and use your asymptotic expression for all values where the numerical error is known to be too high. You may need increase the number of terms in your asymptotic expansion e.g. by doing a Taylor series about infinity. To avoid the tedium of doing this by hand, you may find that maxima is quite helpful.

Upvotes: 0

pv.
pv.

Reputation: 35125

You can silence overflow warnings by numpy.seterr(over='ignore'), see http://docs.scipy.org/doc/numpy/reference/generated/numpy.seterr.html

Upvotes: 5

Thomas K
Thomas K

Reputation: 40330

Assuming the warnings use the Python warning system, you can use the catch_warnings() and simplefilter() functions from the warnings module, as shown in the documentation.

If the warnings don't use that system, it's more complex.

Upvotes: 2

Related Questions