title: Catching numpy runtimewarning as exception and suppressing them
body: <p>I am aware this have been partly answered <a href="https://stackoverflow.com/questions/15933741/how-do-i-catch-a-numpy-warning-like-its-an-exception-not-just-for-testing" title="here">here</a>.</p>
  
  <p>Anyway I am not sure I'm achieving what I want. I'll briefly explain what I am doing:</p>
  
  <ul>
  <li>Reading through a huge list of json files which have a particularly
  nested structure. </li>
  <li>Extracting the lowest level value from them and averaging when these values are lists. </li>
  <li>Collecting these values into numpy arrays.</li>
  <li>Dumping my numpy arrays into pickled files.</li>
  </ul>
  
  <p>Everything goes quite smooth, but I receive some numpy runtime warning:</p>
  
  <ul>
  <li><code>RuntimeWarning: Mean of empty slice.</code></li>
  <li><code>RuntimeWarning: invalid value encountered in double_scalars</code></li>
  </ul>
  
  <p>Function which gives me trouble is the one who actually extract the values which performs this operation: <code>v = np.mean(v)</code></p>
  
  <p>I know the error can be caused by a list of zeros or by some NaN/Inf or whatever is into the value.</p>
  
  <p>I would like to get rid of them just by throwing away the current .json sample from my data set.</p>
  
  <p>So I've set: <code>np.seterr(all='warn')</code></p>
  
  <p>And I made this awkward code to try to catch it:</p>
  
  <pre><code>def ExtracValues(d):
      for v in d.values():
          if isinstance(v, dict):
              yield from ExtracValues(v)
          else:
              if isinstance(v,list):
                  # v = np.mean(v)            #just averaging vectorial values of features.   #it may be here that raises the empty slice warning of numpy.
                  try:
                      v = np.mean(v)
                  except Warning:
                      return #trying to trash samples which are no behaving good                  
              yield v
  </code></pre>
  
  <p>My problem is that I don't know if it's effectively working, because the warning are still printed on stdout. I suppose the code should have stopped after setting <code>all="Warning"</code>, but how could I easily check if I am right?</p>
  
  <p>Also, is there a more pythonic way to shorten that function. I really don't like the <code>try/except</code> nested in that way. </p>