4

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)

4
  • 4
    It's generally helpful if you post code that actually displays the problem you are asking about, rather than replacing it with a placeholder. It relieves the reader from cooking up their own example. Commented Feb 19, 2012 at 13:21
  • Try to catch (except) the "ArithmeticError" exception. Commented Feb 19, 2012 at 13:29
  • @Marcin: It happens in between a very complicated code, with several objects that are defined. But you are right, I will make a small example showing what I am after. Thanks Commented Feb 19, 2012 at 13:33
  • @gimel: It did not work. I will soon update the question with a real example Commented Feb 19, 2012 at 13:34

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

:Thank you for your answer. The problem with this solution is that i silence down everything, not just for this function which is what I want.
You can do olderr = numpy.seterr(over='ignore'); try: func(); finally: numpy.seterr(**olderr)
Thanks guys, this was a good solution that I will accept. Again, thanks.
Just wanted to point out that the with statement gives an AttributeError in Python 2.7.1, the reason is (partly) explained in the answer to this question: link. The try + finally suggested by pv. works fine.
2

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.

Comments

0

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.

1 Comment

Thank you for your answer. I was a bit afraid that this was the only solution, but I wanted to investigate other options first, as explicitly "silencing" the errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.