我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用numpy.VisibleDeprecationWarning()。
def test_bool_flat_indexing_invalid_nr_elements(self, level=rlevel): s = np.ones(10, dtype=float) x = np.array((15,), dtype=float) def ia(x, s, v): x[(s > 0)] = v # After removing deprecation, the following are ValueErrors. # This might seem odd as compared to the value error below. This # is due to the fact that the new code always uses "nonzero" logic # and the boolean special case is not taken. with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', np.VisibleDeprecationWarning) self.assertRaises(IndexError, ia, x, s, np.zeros(9, dtype=float)) self.assertRaises(IndexError, ia, x, s, np.zeros(11, dtype=float)) # Old special case (different code path): self.assertRaises(ValueError, ia, x.flat, s, np.zeros(9, dtype=float)) self.assertRaises(ValueError, ia, x.flat, s, np.zeros(11, dtype=float))
def test_simple(self): arr = np.ones((5, 4, 3)) index = np.array([True]) #self.assert_deprecated(arr.__getitem__, args=(index,)) assert_warns(np.VisibleDeprecationWarning, arr.__getitem__, index) index = np.array([False] * 6) #self.assert_deprecated(arr.__getitem__, args=(index,)) assert_warns(np.VisibleDeprecationWarning, arr.__getitem__, index) index = np.zeros((4, 4), dtype=bool) #self.assert_deprecated(arr.__getitem__, args=(index,)) assert_warns(np.VisibleDeprecationWarning, arr.__getitem__, index) #self.assert_deprecated(arr.__getitem__, args=((slice(None), index),)) assert_warns(np.VisibleDeprecationWarning, arr.__getitem__, (slice(None), index))
def test_numpy_reloading(): # gh-7844. Also check that relevant globals retain their identity. import numpy as np import numpy._globals _NoValue = np._NoValue VisibleDeprecationWarning = np.VisibleDeprecationWarning ModuleDeprecationWarning = np.ModuleDeprecationWarning reload(np) assert_(_NoValue is np._NoValue) assert_(ModuleDeprecationWarning is np.ModuleDeprecationWarning) assert_(VisibleDeprecationWarning is np.VisibleDeprecationWarning) assert_raises(RuntimeError, reload, numpy._globals) reload(np) assert_(_NoValue is np._NoValue) assert_(ModuleDeprecationWarning is np.ModuleDeprecationWarning) assert_(VisibleDeprecationWarning is np.VisibleDeprecationWarning)
def test_bool_flat_indexing_invalid_nr_elements(self, level=rlevel): s = np.ones(10, dtype=float) x = np.array((15,), dtype=float) def ia(x, s, v): x[(s > 0)] = v # After removing deprecation, the following are ValueErrors. # This might seem odd as compared to the value error below. This # is due to the fact that the new code always uses "nonzero" logic # and the boolean special case is not taken. with suppress_warnings() as sup: sup.filter(DeprecationWarning) sup.filter(FutureWarning) sup.filter(np.VisibleDeprecationWarning) self.assertRaises(IndexError, ia, x, s, np.zeros(9, dtype=float)) self.assertRaises(IndexError, ia, x, s, np.zeros(11, dtype=float)) # Old special case (different code path): self.assertRaises(ValueError, ia, x.flat, s, np.zeros(9, dtype=float)) self.assertRaises(ValueError, ia, x.flat, s, np.zeros(11, dtype=float))
def assert_no_warnings(func, *args, **kw): # XXX: once we may depend on python >= 2.6, this can be replaced by the # warnings module context manager. # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') result = func(*args, **kw) if hasattr(np, 'VisibleDeprecationWarning'): # Filter out numpy-specific warnings in numpy >= 1.9 w = [e for e in w if e.category is not np.VisibleDeprecationWarning] if len(w) > 0: raise AssertionError("Got warnings when calling %s: %s" % (func.__name__, w)) return result
def test_multidim(self): # Automatically test combinations with complex indexes on 2nd (or 1st) # spot and the simple ones in one other spot. with warnings.catch_warnings(): # This is so that np.array(True) is not accepted in a full integer # index, when running the file separately. warnings.filterwarnings('error', '', DeprecationWarning) warnings.filterwarnings('error', '', np.VisibleDeprecationWarning) def isskip(idx): return isinstance(idx, str) and idx == "skip" for simple_pos in [0, 2, 3]: tocheck = [self.fill_indices, self.complex_indices, self.fill_indices, self.fill_indices] tocheck[simple_pos] = self.simple_indices for index in product(*tocheck): index = tuple(i for i in index if not isskip(i)) self._check_multi_index(self.a, index) self._check_multi_index(self.b, index) # Check very simple item getting: self._check_multi_index(self.a, (0, 0, 0, 0)) self._check_multi_index(self.b, (0, 0, 0, 0)) # Also check (simple cases of) too many indices: assert_raises(IndexError, self.a.__getitem__, (0, 0, 0, 0, 0)) assert_raises(IndexError, self.a.__setitem__, (0, 0, 0, 0, 0), 0) assert_raises(IndexError, self.a.__getitem__, (0, 0, [1], 0, 0)) assert_raises(IndexError, self.a.__setitem__, (0, 0, [1], 0, 0), 0)
def test_1d(self): a = np.arange(10) with warnings.catch_warnings(): warnings.filterwarnings('error', '', np.VisibleDeprecationWarning) for index in self.complex_indices: self._check_single_index(a, index)
def setUp(self): self.warn_ctx = warnings.catch_warnings(record=True) self.log = self.warn_ctx.__enter__() # Do *not* ignore other DeprecationWarnings. Ignoring warnings # can give very confusing results because of # http://bugs.python.org/issue4180 and it is probably simplest to # try to keep the tests cleanly giving only the right warning type. # (While checking them set to "error" those are ignored anyway) # We still have them show up, because otherwise they would be raised warnings.filterwarnings("always", category=np.VisibleDeprecationWarning) warnings.filterwarnings("always", message=self.message, category=np.VisibleDeprecationWarning)
def test_basic(self): a = np.arange(10) self.assert_deprecated(a.__getitem__, args=((Ellipsis, Ellipsis),)) with warnings.catch_warnings(): warnings.filterwarnings('ignore', '', np.VisibleDeprecationWarning) # Just check that this works: b = a[...,...] assert_array_equal(a, b) assert_raises(IndexError, a.__getitem__, ((Ellipsis, ) * 3,))
def rank(obj): """ maskedarray version of the numpy function. .. note:: Deprecated since 1.10.0 """ # 2015-04-12, 1.10.0 warnings.warn( "`rank` is deprecated; use the `ndim` function instead. ", np.VisibleDeprecationWarning) return np.ndim(getdata(obj))
def issue_deprecation_warning(msg): from numpy import VisibleDeprecationWarning warnings.warn(msg, VisibleDeprecationWarning, stacklevel=3)
def test(self): a = np.arange(10) assert_warns(np.VisibleDeprecationWarning, np.rank, a)
def rank(obj): """ maskedarray version of the numpy function. .. note:: Deprecated since 1.10.0 """ # 2015-04-12, 1.10.0 warnings.warn( "`rank` is deprecated; use the `ndim` function instead. ", np.VisibleDeprecationWarning, stacklevel=2) return np.ndim(getdata(obj))
def assert_warns(warning_class, func, *args, **kw): """Test that a certain warning occurs. Parameters ---------- warning_class : the warning class The class to test for, e.g. UserWarning. func : callable Calable object to trigger warnings. *args : the positional arguments to `func`. **kw : the keyword arguments to `func` Returns ------- result : the return value of `func` """ # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. result = func(*args, **kw) if hasattr(np, 'VisibleDeprecationWarning'): # Filter out numpy-specific warnings in numpy >= 1.9 w = [e for e in w if e.category is not np.VisibleDeprecationWarning] # Verify some things if not len(w) > 0: raise AssertionError("No warning raised when calling %s" % func.__name__) found = any(warning.category is warning_class for warning in w) if not found: raise AssertionError("%s did not give warning: %s( is %s)" % (func.__name__, warning_class, w)) return result