Why can't I suppress numpy warnings

Question:

I really want to avoid these annoying numpy warnings since I have to deal with a lot of NaNs. I know this is usually done with seterr, but for some reason here it does not work:

import numpy as np
data = np.random.random(100000).reshape(10, 100, 100) * np.nan
np.seterr(all="ignore")
np.nanmedian(data, axis=[1, 2])

It gives me a runtime warning even though I set numpy to ignore all errors…any help?

Edit (this is the warning that is recieved):

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-p‌​ackages/numpy/lib/nanfunctions.py:612: RuntimeWarning: All-NaN slice encountered warnings.warn("All-NaN slice encountered", RuntimeWarning)

Asked By: HansSnah

||

Answers:

Warnings can often be useful and in most cases I wouldn’t advise this, but you can always make use of the Warnings module to ignore all warnings with filterwarnings:

warnings.filterwarnings('ignore')

Should you want to suppress uniquely your particular error, you could specify it with:

with warnings.catch_warnings():
    warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
Answered By: miradulo

The warnings controlled by seterr() are those issued by the numpy ufunc machinery; e.g. when A / B creates a NaN in the C code that implements the division, say because there was an inf/inf somewhere in those arrays. Other numpy code may issue their own warnings for other reasons. In this case, you are using one of the NaN-ignoring reduction functions, like nanmin() or the like. You are passing it an array that contains all NaNs, or at least all NaNs along an axis that you requested the reduction along. Since the usual reason one uses nanmin() is to not get another NaN out, nanmin() will issue a warning that it has no choice but to give you a NaN. This goes directly to the standard library warnings machinery and not the numpy ufunc error control machinery since it isn’t a ufunc and this production of a NaN isn’t the same as what seterr(invalid=...) otherwise deals with.

Answered By: Robert Kern

You may want to avoid suppressing the warning, because numpy raises this for a good reason. If you want to clean up your output, maybe handle it by explicitly returning a pre-defined value when your array is all nan.

def clean_nanmedian(s):
    if np.all(np.isnan(s)):
        return np.nan
    return np.nanmedian(s)

Also, keep in mind that this RuntimeWarning is raised only the first time that this happens in your run-time.

Answered By: idnavid
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.