Why can't I suppress numpy warnings

PythonNumpy

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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')

Solution 2 - Python

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.

Solution 3 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHansSnahView Question on Stackoverflow
Solution 1 - PythonmiraduloView Answer on Stackoverflow
Solution 2 - PythonRobert KernView Answer on Stackoverflow
Solution 3 - PythonidnavidView Answer on Stackoverflow