[SciPy-User] catching warnings without error/raise

105 views
Skip to first unread message

josef...@gmail.com

unread,
Sep 22, 2011, 12:16:45 PM9/22/11
to SciPy Users List
A python question, but hopefully someone can answer

I would like wrap functions that sometimes issue warnings, I want to
suppress the warning but I want to record whether a warning has been
issued, for later display.

python 2.6 has "with warnings.catch_warnings(record=True) as w:" that
seems to do what I want (from reading the description).

Is there a different way how to do this that also works for python 2.5

examples:

>>> from scipy import stats
>>> stats.kurtosistest(np.arange(5))
(-0.57245889052982701, 0.56701112882584059)
C:\Python26\lib\site-packages\scipy\stats\stats.py:1198: UserWarning:
kurtosistest only valid for n>=20 ... continuing anyway, n=5
int(n))

>>> stats.ansari(np.arange(5), np.arange(5)**2)
(18.5, 0.12260027475751481)
C:\Python26\lib\site-packages\scipy\stats\morestats.py:731:
UserWarning: Ties preclude use of exact statistic.
warnings.warn("Ties preclude use of exact statistic.")

Thanks,
Josef
_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

David Cournapeau

unread,
Sep 22, 2011, 2:04:40 PM9/22/11
to SciPy Users List
On Thu, Sep 22, 2011 at 12:16 PM, <josef...@gmail.com> wrote:
> A python question, but hopefully someone can answer
>
> I would like wrap functions that sometimes issue warnings, I want to
> suppress the warning but I want to record whether a warning has been
> issued, for later display.
>
> python 2.6 has "with warnings.catch_warnings(record=True) as w:" that
> seems to do what I want (from reading the description).

The with statement is "just" syntax sugar, so if you read the sources
for the corresponding context manager, you should be able to reproduce
the code in a 2.5-compatible way.

cheers,

David

josef...@gmail.com

unread,
Sep 22, 2011, 2:49:07 PM9/22/11
to SciPy Users List
On Thu, Sep 22, 2011 at 2:04 PM, David Cournapeau <cour...@gmail.com> wrote:
> On Thu, Sep 22, 2011 at 12:16 PM,  <josef...@gmail.com> wrote:
>> A python question, but hopefully someone can answer
>>
>> I would like wrap functions that sometimes issue warnings, I want to
>> suppress the warning but I want to record whether a warning has been
>> issued, for later display.
>>
>> python 2.6 has "with warnings.catch_warnings(record=True) as w:" that
>> seems to do what I want (from reading the description).
>
> The with statement is "just" syntax sugar, so if you read the sources
> for the corresponding context manager, you should be able to reproduce
> the code in a 2.5-compatible way.

I forgot we can sometimes look at the python source

import warnings
cache_showwarning = warnings.showwarning
log = []
###python 2.6
##def showwarning(*args, **kwargs):
## log.append(warnings.WarningMessage(*args, **kwargs))

#python 2.5 : no warnings.WarningMessage
def showwarning(*args, **kwargs):
log.append((args, kwargs))
warnings.showwarning = showwarning

import numpy as np
from scipy import stats
warnings.simplefilter("always") #stats doesn't always warn ?
stats.ansari(np.arange(5), np.arange(5)**2)
stats.kurtosistest(np.arange(5))

warnings.showwarning = cache_showwarning
print log

---------

After looking at the source, I see in the documentation for
showwarning for both 2.5 and 2.6
"You may replace this function with an alternative implementation by
assigning to warnings.showwarning"
which sounded too "monkey" for me to pay attention.

Thanks,

Josef

Reply all
Reply to author
Forward
0 new messages