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)

gimel
  • 83,368
  • 10
  • 76
  • 104
matiasq
  • 537
  • 1
  • 6
  • 12
  • 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. – Marcin Feb 19 '12 at 13:21
  • Try to catch (except) the "ArithmeticError" exception. – gimel Feb 19 '12 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 – matiasq Feb 19 '12 at 13:33
  • @gimel: It did not work. I will soon update the question with a real example – matiasq Feb 19 '12 at 13:34

3 Answers3

5

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

pv.
  • 33,875
  • 8
  • 55
  • 49
  • :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. – matiasq Feb 20 '12 at 12:43
  • 2
    You can do `olderr = numpy.seterr(over='ignore'); try: func(); finally: numpy.seterr(**olderr)` – pv. Feb 20 '12 at 19:44
  • 3
    You can use a with statement: http://docs.scipy.org/doc/numpy/reference/generated/numpy.errstate.html#numpy.errstate – tillsten Feb 20 '12 at 21:50
  • Thanks guys, this was a good solution that I will accept. Again, thanks. – matiasq Feb 22 '12 at 14:13
  • 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](http://stackoverflow.com/questions/7447284/how-to-troubleshoot-an-attributeerror-exit-in-multiproccesing-in-python). The try + finally suggested by pv. works fine. – matiasq Feb 23 '12 at 07:14
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.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
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.

DaveP
  • 6,952
  • 1
  • 24
  • 37
  • 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. – matiasq Feb 20 '12 at 12:47