question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Control "Mean of empty slice" RunTimeWarning with np.errstate

See original GitHub issue

As there already exists a mechanism in numpy to manage warnings for possibly anomalous inputs, it seems odd that it doesn’t work for all warnings. For example, nanmean raises a warning if given a slice of all nan, and apparently the only way to silence it is to catch it with the python warnings mechanism. np.errstate, despite claiming to handle divide by zero errors, does not:

Python 3.6.2 
In [1]: import numpy as np
In [2]: np.__version__
Out[2]: '1.13.1'

In [3]: allnan = np.array([np.nan, np.nan])
In [4]: with np.errstate(divide='ignore', invalid='ignore'):
   ...:     print(np.nanmean(allnan))
   ...:     
/Users/jonathanshor/anaconda2/envs/Python3/bin/ipython:2: RuntimeWarning: Mean of empty slice
  if __name__ == '__main__':
nan

I would suggest this could fall under the divide case, but invalid seems plausible as well.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
nedclimateriskcommented, Jan 24, 2019

Would this need anything more than a

class AllNanAxisWarning(RuntimeWarning):
    pass

And then replacing those RuntimeWarnings in the rest of https://github.com/numpy/numpy/blob/v1.15.0/numpy/lib/nanfunctions.py ? Because if that’s all that would be required, I can submit a PR.

3reactions
charriscommented, Mar 9, 2018

I’d also be in favor of subclassing RuntimeWarning to give a more easily-caught warning for this and the other nanfunctions.

That’s an excellent suggestion. Maybe “AllNanAxisWarning” or something similar.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mean, nanmean and warning: Mean of empty slice
I have got this runtime warning when I perform np.nanmean over a 3-D array, e.g. (time, lon, lat). Maybe not a direct answer...
Read more >
[Solved] NumPy RuntimeWarning: Mean of empty slice
The reason this warning arises is because you apply the np.nanmean() function on an empty array. The function doesn't cause an error if...
Read more >
Numpy - mbedded.ninja
Numpy prints a warning stating that you are trying to calculate a mean of an empty slice, as well as that there is...
Read more >
Source code for tombo.tombo_stats
Empty : break if slide_span > 0: row_dists = np.array( ... base start positions within raw signal Returns: `np.array::np.float64` containing base mean levels ......
Read more >
warnings.warn("Mean of empty slice.", RuntimeWarning)
Hi, I am new to qiime and trying to demultiplex a set of reads from 9 different samples. If I use split_libraries_fastq.py -i ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found