Python warnings.catch_warnings() Examples
The following are 30
code examples of warnings.catch_warnings().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
warnings
, or try the search function
.
Example #1
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 7 votes |
def test_global_norm_clip(): stypes = ['default', 'row_sparse'] def check_global_norm_clip(stype, check_isfinite): x1 = mx.nd.ones((3,3)).tostype(stype) x2 = mx.nd.ones((4,4)).tostype(stype) norm = gluon.utils.clip_global_norm([x1, x2], 1.0, check_isfinite=check_isfinite) assert norm == 5.0 assert_almost_equal(x1.asnumpy(), np.ones((3,3))/5) assert_almost_equal(x2.asnumpy(), np.ones((4,4))/5) x3 = mx.nd.array([1.0, 2.0, float('nan')]).tostype(stype) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") gluon.utils.clip_global_norm([x1, x3], 2.0, check_isfinite=check_isfinite) assert len(w) == check_isfinite for stype in stypes: for check_isfinite in [True, False]: check_global_norm_clip(stype, check_isfinite)
Example #2
Source File: frd_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_evalfr_deprecated(self): sys_tf = ct.tf([1], [1, 2, 1]) frd_tf = FRD(sys_tf, np.logspace(-1, 1, 3)) # Deprecated version of the call (should generate warning) import warnings with warnings.catch_warnings(): # Make warnings generate an exception warnings.simplefilter('error') # Make sure that we get a pending deprecation warning self.assertRaises(PendingDeprecationWarning, frd_tf.evalfr, 1.) # FRD.evalfr() is being deprecated import warnings with warnings.catch_warnings(): # Make warnings generate an exception warnings.simplefilter('error') # Make sure that we get a pending deprecation warning self.assertRaises(PendingDeprecationWarning, frd_tf.evalfr, 1.)
Example #3
Source File: test_cmd_init.py From grimoirelab-sortinghat with GNU General Public License v3.0 | 6 votes |
def test_connection_error(self): """Check connection errors""" kwargs = { 'user': 'nouser', 'password': 'nopassword', 'database': None, 'host': self.kwargs['host'], 'port': self.kwargs['port'], 'reuse': False } cmd = Init(**kwargs) code = cmd.initialize(self.name) self.assertEqual(code, CODE_DATABASE_ERROR) # Context added to catch deprecation warnings raised on Python 3 with warnings.catch_warnings(record=True): output = sys.stderr.getvalue().strip() self.assertRegexpMatches(output, DB_ACCESS_ERROR % {'user': 'nouser'})
Example #4
Source File: test_cmd_init.py From grimoirelab-sortinghat with GNU General Public License v3.0 | 6 votes |
def test_connection_error(self): """Check connection errors""" kwargs = { 'user': 'nouser', 'password': 'nopassword', 'database': None, 'host': self.kwargs['host'], 'port': self.kwargs['port'] } cmd = Init(**kwargs) code = cmd.run(self.name) self.assertEqual(code, CODE_DATABASE_ERROR) with warnings.catch_warnings(record=True): output = sys.stderr.getvalue().strip() self.assertRegexpMatches(output, DB_ACCESS_ERROR % {'user': 'nouser'})
Example #5
Source File: simple_moving_average.py From TradzQAI with Apache License 2.0 | 6 votes |
def simple_moving_average(data, period): """ Simple Moving Average. Formula: SUM(data / N) """ check_for_period_error(data, period) # Mean of Empty Slice RuntimeWarning doesn't affect output so it is # supressed with warnings.catch_warnings(): warnings.simplefilter("ignore", category=RuntimeWarning) sma = list(map( lambda idx: np.mean(data[idx-(period-1):idx+1]), range(0, len(data)) )) sma = fill_for_noncomputable_vals(data, sma) return sma
Example #6
Source File: test_case.py From jawfish with MIT License | 6 votes |
def testAssertWarnsCallable(self): def _runtime_warn(): warnings.warn("foo", RuntimeWarning) # Success when the right warning is triggered, even several times self.assertWarns(RuntimeWarning, _runtime_warn) self.assertWarns(RuntimeWarning, _runtime_warn) # A tuple of warning classes is accepted self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn) # *args and **kwargs also work self.assertWarns(RuntimeWarning, warnings.warn, "foo", category=RuntimeWarning) # Failure when no warning is triggered with self.assertRaises(self.failureException): self.assertWarns(RuntimeWarning, lambda: 0) # Failure when another warning is triggered with warnings.catch_warnings(): # Force default filter (in case tests are run with -We) warnings.simplefilter("default", RuntimeWarning) with self.assertRaises(self.failureException): self.assertWarns(DeprecationWarning, _runtime_warn) # Filters for other warnings are not modified with warnings.catch_warnings(): warnings.simplefilter("error", RuntimeWarning) with self.assertRaises(RuntimeWarning): self.assertWarns(DeprecationWarning, _runtime_warn)
Example #7
Source File: didyoumean_sugg_tests.py From DidYouMean-Python with MIT License | 6 votes |
def test_import_star(self): """'import *' in nested functions.""" # NICE_TO_HAVE codes = [ func_gen( 'func1', body=func_gen('func2', body='from math import *\nTrue')), func_gen( 'func1', body='from math import *\n' + func_gen('func2', body='True')), ] sys.setrecursionlimit(1000) # needed for weird PyPy versions with warnings.catch_warnings(): warnings.simplefilter("ignore", category=SyntaxWarning) for code in codes: self.throws(code, IMPORTSTAR) sys.setrecursionlimit(initial_recursion_limit)
Example #8
Source File: statefbk_array_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gram_wc_deprecated(self): A = np.array([[1., -2.], [3., -4.]]) B = np.array([[5., 6.], [7., 8.]]) C = np.array([[4., 5.], [6., 7.]]) D = np.array([[13., 14.], [15., 16.]]) sys = ss(A, B, C, D) # Check that default type generates a warning # TODO: remove this check with matrix type is deprecated with warnings.catch_warnings(record=True) as w: use_numpy_matrix(True) self.assertTrue(issubclass(w[-1].category, UserWarning)) Wc = gram(sys, 'c') self.assertTrue(isinstance(Wc, np.ndarray)) use_numpy_matrix(False)
Example #9
Source File: test_async.py From chainerrl with MIT License | 6 votes |
def test_run_async_exit_code(self): def run_with_exit_code_0(process_idx): sys.exit(0) def run_with_exit_code_11(process_idx): os.kill(os.getpid(), signal.SIGSEGV) with warnings.catch_warnings(record=True) as ws: async_.run_async(4, run_with_exit_code_0) # There should be no AbnormalExitWarning self.assertEqual( sum(1 if issubclass( w.category, async_.AbnormalExitWarning) else 0 for w in ws), 0) with warnings.catch_warnings(record=True) as ws: async_.run_async(4, run_with_exit_code_11) # There should be 4 AbnormalExitWarning self.assertEqual( sum(1 if issubclass( w.category, async_.AbnormalExitWarning) else 0 for w in ws), 4)
Example #10
Source File: test_case.py From jawfish with MIT License | 5 votes |
def testAssertWarnsRegexCallable(self): def _runtime_warn(msg): warnings.warn(msg, RuntimeWarning) self.assertWarnsRegex(RuntimeWarning, "o+", _runtime_warn, "foox") # Failure when no warning is triggered with self.assertRaises(self.failureException): self.assertWarnsRegex(RuntimeWarning, "o+", lambda: 0) # Failure when another warning is triggered with warnings.catch_warnings(): # Force default filter (in case tests are run with -We) warnings.simplefilter("default", RuntimeWarning) with self.assertRaises(self.failureException): self.assertWarnsRegex(DeprecationWarning, "o+", _runtime_warn, "foox") # Failure when message doesn't match with self.assertRaises(self.failureException): self.assertWarnsRegex(RuntimeWarning, "o+", _runtime_warn, "barz") # A little trickier: we ask RuntimeWarnings to be raised, and then # check for some of them. It is implementation-defined whether # non-matching RuntimeWarnings are simply re-raised, or produce a # failureException. with warnings.catch_warnings(): warnings.simplefilter("error", RuntimeWarning) with self.assertRaises((RuntimeWarning, self.failureException)): self.assertWarnsRegex(RuntimeWarning, "o+", _runtime_warn, "barz")
Example #11
Source File: statefbk_array_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_obsv_siso_deprecated(self): A = np.array([[1., 2.], [3., 4.]]) C = np.array([[5., 7.]]) # Check that default type generates a warning # TODO: remove this check with matrix type is deprecated with warnings.catch_warnings(record=True) as w: use_numpy_matrix(True, warn=False) # warnings off self.assertEqual(len(w), 0) Wo = obsv(A, C) self.assertTrue(isinstance(Wo, np.matrix)) use_numpy_matrix(False)
Example #12
Source File: statesp_array_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_evalfr(self): """Evaluate the frequency response at one frequency.""" A = [[-2, 0.5], [0.5, -0.3]] B = [[0.3, -1.3], [0.1, 0.]] C = [[0., 0.1], [-0.3, -0.2]] D = [[0., -0.8], [-0.3, 0.]] sys = StateSpace(A, B, C, D) resp = [[4.37636761487965e-05 - 0.0152297592997812j, -0.792603938730853 + 0.0261706783369803j], [-0.331544857768052 + 0.0576105032822757j, 0.128919037199125 - 0.143824945295405j]] # Correct versions of the call np.testing.assert_almost_equal(evalfr(sys, 1j), resp) np.testing.assert_almost_equal(sys._evalfr(1.), resp) # Deprecated version of the call (should generate warning) import warnings with warnings.catch_warnings(record=True) as w: # Set up warnings filter to only show warnings in control module warnings.filterwarnings("ignore") warnings.filterwarnings("always", module="control") # Make sure that we get a pending deprecation warning sys.evalfr(1.) assert len(w) == 1 assert issubclass(w[-1].category, PendingDeprecationWarning) # Leave the warnings filter like we found it warnings.resetwarnings()
Example #13
Source File: base.py From verge3d-blender-addon with GNU General Public License v3.0 | 5 votes |
def __enter__(self): # The __warningregistry__'s need to be in a pristine state for tests # to work properly. for v in sys.modules.values(): if getattr(v, '__warningregistry__', None): v.__warningregistry__ = {} self.warnings_manager = warnings.catch_warnings(record=True) self.warnings = self.warnings_manager.__enter__() warnings.simplefilter("always", self.expected) return self
Example #14
Source File: support.py From verge3d-blender-addon with GNU General Public License v3.0 | 5 votes |
def _ignore_deprecated_imports(ignore=True): """Context manager to suppress package and module deprecation warnings when importing them. If ignore is False, this context manager has no effect.""" if ignore: with warnings.catch_warnings(): warnings.filterwarnings("ignore", ".+ (module|package)", DeprecationWarning) yield else: yield
Example #15
Source File: runner.py From firefox_decrypt with GNU General Public License v3.0 | 5 votes |
def run(self, test): result = self._makeResult() registerResult(result) result.failfast = self.failfast result.buffer = self.buffer with warnings.catch_warnings(): if getattr(self, "warnings", None): # if self.warnings is set, use it to filter all the warnings warnings.simplefilter(self.warnings) # if the filter is 'default' or 'always', special-case the # warnings from the deprecated unittest methods to show them # no more than once per module, because they can be fairly # noisy. The -Wd and -Wa flags can be used to bypass this # only when self.warnings is None. if self.warnings in ['default', 'always']: warnings.filterwarnings( 'module', category=DeprecationWarning, message='Please use assert\w+ instead.') startTestRun = getattr(result, 'startTestRun', None) if startTestRun is not None: result.total_tests = test.countTestCases() startTestRun() try: test(result) finally: stopTestRun = getattr(result, 'stopTestRun', None) if stopTestRun is not None: stopTestRun() return result
Example #16
Source File: statesp_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_evalfr(self): """Evaluate the frequency response at one frequency.""" A = [[-2, 0.5], [0.5, -0.3]] B = [[0.3, -1.3], [0.1, 0.]] C = [[0., 0.1], [-0.3, -0.2]] D = [[0., -0.8], [-0.3, 0.]] sys = StateSpace(A, B, C, D) resp = [[4.37636761487965e-05 - 0.0152297592997812j, -0.792603938730853 + 0.0261706783369803j], [-0.331544857768052 + 0.0576105032822757j, 0.128919037199125 - 0.143824945295405j]] # Correct versions of the call np.testing.assert_almost_equal(evalfr(sys, 1j), resp) np.testing.assert_almost_equal(sys._evalfr(1.), resp) # Deprecated version of the call (should generate warning) import warnings with warnings.catch_warnings(record=True) as w: # Set up warnings filter to only show warnings in control module warnings.filterwarnings("ignore") warnings.filterwarnings("always", module="control") # Make sure that we get a pending deprecation warning sys.evalfr(1.) assert len(w) == 1 assert issubclass(w[-1].category, PendingDeprecationWarning)
Example #17
Source File: test_KnownRVPlanetsUniverse.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): # print '[setup] ', specs = json.loads(ScriptLiteral) with RedirectStreams(stdout=self.dev_null): with warnings.catch_warnings(): # filter out warnings about the votable RVplanets file warnings.filterwarnings("ignore", ".*votable") self.fixture = KnownRVPlanetsUniverse(**specs)
Example #18
Source File: imp.py From jawfish with MIT License | 5 votes |
def load_module(name, file, filename, details): """**DEPRECATED** Load a module, given information returned by find_module(). The module name must include the full package name, if any. """ suffix, mode, type_ = details with warnings.catch_warnings(): warnings.simplefilter('ignore') if mode and (not mode.startswith(('r', 'U')) or '+' in mode): raise ValueError('invalid file open mode {!r}'.format(mode)) elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: msg = 'file object required for import (type code {})'.format(type_) raise ValueError(msg) elif type_ == PY_SOURCE: return load_source(name, filename, file) elif type_ == PY_COMPILED: return load_compiled(name, filename, file) elif type_ == C_EXTENSION and load_dynamic is not None: if file is None: with open(filename, 'rb') as opened_file: return load_dynamic(name, filename, opened_file) else: return load_dynamic(name, filename, file) elif type_ == PKG_DIRECTORY: return load_package(name, filename) elif type_ == C_BUILTIN: return init_builtin(name) elif type_ == PY_FROZEN: return init_frozen(name) else: msg = "Don't know how to import {} (type code {})".format(name, type_) raise ImportError(msg, name=name)
Example #19
Source File: testwith.py From jawfish with MIT License | 5 votes |
def test_with_statement_nested(self): with catch_warnings(record=True): with patch('%s.something' % __name__) as mock_something, patch('%s.something_else' % __name__) as mock_something_else: self.assertEqual(something, mock_something, "unpatched") self.assertEqual(something_else, mock_something_else, "unpatched") self.assertEqual(something, sentinel.Something) self.assertEqual(something_else, sentinel.SomethingElse)
Example #20
Source File: test_assertions.py From jawfish with MIT License | 5 votes |
def testAssertDictContainsSubset(self): with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}), ["^Missing: 'key'$", "^oops$", "^Missing: 'key'$", "^Missing: 'key' : oops$"])
Example #21
Source File: test_KnownRVPlanetsTargetList.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): # print '[setup] ', specs = json.loads(ScriptLiteral) with RedirectStreams(stdout=self.dev_null): with warnings.catch_warnings(): # filter out warnings about the votable RVplanets file warnings.filterwarnings("ignore", ".*votable") self.fixture = KnownRVPlanetsTargetList(**specs)
Example #22
Source File: test_case.py From jawfish with MIT License | 5 votes |
def testAssertWarnsContext(self): # Believe it or not, it is preferrable to duplicate all tests above, # to make sure the __warningregistry__ $@ is circumvented correctly. def _runtime_warn(): warnings.warn("foo", RuntimeWarning) _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1] with self.assertWarns(RuntimeWarning) as cm: _runtime_warn() # A tuple of warning classes is accepted with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm: _runtime_warn() # The context manager exposes various useful attributes self.assertIsInstance(cm.warning, RuntimeWarning) self.assertEqual(cm.warning.args[0], "foo") self.assertIn("test_case.py", cm.filename) self.assertEqual(cm.lineno, _runtime_warn_lineno + 1) # Same with several warnings with self.assertWarns(RuntimeWarning): _runtime_warn() _runtime_warn() with self.assertWarns(RuntimeWarning): warnings.warn("foo", category=RuntimeWarning) # Failure when no warning is triggered with self.assertRaises(self.failureException): with self.assertWarns(RuntimeWarning): pass # Failure when another warning is triggered with warnings.catch_warnings(): # Force default filter (in case tests are run with -We) warnings.simplefilter("default", RuntimeWarning) with self.assertRaises(self.failureException): with self.assertWarns(DeprecationWarning): _runtime_warn() # Filters for other warnings are not modified with warnings.catch_warnings(): warnings.simplefilter("error", RuntimeWarning) with self.assertRaises(RuntimeWarning): with self.assertWarns(DeprecationWarning): _runtime_warn()
Example #23
Source File: ZIFA.py From ZIFA with MIT License | 5 votes |
def decayCoefObjectiveFn(x, Y, EX2): """ Computes the objective function for terms involving lambda in the M-step. Checked. Input: x: value of lambda Y: the matrix of observed values EX2: the matrix of values of EX2 estimated in the E-step. Returns: obj: value of objective function grad: gradient """ with warnings.catch_warnings(): warnings.simplefilter("ignore") y_squared = Y ** 2 Y_is_zero = np.abs(Y) < 1e-6 exp_Y_squared = np.exp(-x * y_squared) log_exp_Y = np.nan_to_num(np.log(1 - exp_Y_squared)) exp_ratio = np.nan_to_num(exp_Y_squared / (1 - exp_Y_squared)) obj = sum(sum(Y_is_zero * (-EX2 * x) + (1 - Y_is_zero) * log_exp_Y)) grad = sum(sum(Y_is_zero * (-EX2) + (1 - Y_is_zero) * y_squared * exp_ratio)) if (type(obj) is not np.float64) or (type(grad) is not np.float64): raise Exception("Unexpected behavior in optimizing decay coefficient lambda. Please contact emmap1@cs.stanford.edu.") if type(obj) is np.float64: obj = -np.array([obj]) if type(grad) is np.float64: grad = -np.array([grad]) return obj, grad
Example #24
Source File: test_case.py From jawfish with MIT License | 5 votes |
def testAssertDictContainsSubset(self): with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) self.assertDictContainsSubset({}, {}) self.assertDictContainsSubset({}, {'a': 1}) self.assertDictContainsSubset({'a': 1}, {'a': 1}) self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2}) self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2}) with self.assertRaises(self.failureException): self.assertDictContainsSubset({1: "one"}, {}) with self.assertRaises(self.failureException): self.assertDictContainsSubset({'a': 2}, {'a': 1}) with self.assertRaises(self.failureException): self.assertDictContainsSubset({'c': 1}, {'a': 1}) with self.assertRaises(self.failureException): self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) with self.assertRaises(self.failureException): self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) one = ''.join(chr(i) for i in range(255)) # this used to cause a UnicodeDecodeError constructing the failure msg with self.assertRaises(self.failureException): self.assertDictContainsSubset({'foo': one}, {'foo': '\uFFFD'})
Example #25
Source File: test_keywords.py From myhdl with GNU Lesser General Public License v2.1 | 5 votes |
def test_multiple_conversion(): sig_1 = Signal(True) sig_2 = Signal(True) a_block = valid(sig_1, sig_2) # conversions with keyword should fail with warnings.catch_warnings() as w: warnings.simplefilter('error') a_block.convert(hdl='VHDL') a_block.convert(hdl='VHDL')
Example #26
Source File: case.py From jawfish with MIT License | 5 votes |
def __enter__(self): # The __warningregistry__'s need to be in a pristine state for tests # to work properly. for v in sys.modules.values(): if getattr(v, '__warningregistry__', None): v.__warningregistry__ = {} self.warnings_manager = warnings.catch_warnings(record=True) self.warnings = self.warnings_manager.__enter__() warnings.simplefilter("always", self.expected) return self
Example #27
Source File: fixtures.py From gnocchi with Apache License 2.0 | 5 votes |
def stop_fixture(self): """Clean up the config fixture and storage artifacts.""" if hasattr(self, 'metricd_thread'): self.metricd_thread.stop() self.metricd_thread.join() if hasattr(self, 'fixtures'): for f in reversed(self.fixtures): f.cleanUp() if hasattr(self, 'index'): self.index.disconnect() # Swallow noise from missing tables when dropping # database. with warnings.catch_warnings(): warnings.filterwarnings('ignore', module='sqlalchemy.engine.default') sqlalchemy_utils.drop_database(self.conf.indexer.url) if self.tmp_dir: shutil.rmtree(self.tmp_dir) if hasattr(self, 'coord'): self.coord.stop() self.conf.reset() if not os.getenv("GNOCCHI_TEST_DEBUG"): self.output.cleanUp()
Example #28
Source File: scanorama.py From scanorama with MIT License | 5 votes |
def transform(curr_ds, curr_ref, ds_ind, ref_ind, sigma=SIGMA, cn=False, batch_size=None): # Compute the matching. match_ds = curr_ds[ds_ind, :] match_ref = curr_ref[ref_ind, :] bias = match_ref - match_ds if cn: match_ds = match_ds.toarray() curr_ds = curr_ds.toarray() bias = bias.toarray() with warnings.catch_warnings(): warnings.filterwarnings('error') try: avg_bias = batch_bias(curr_ds, match_ds, bias, sigma=sigma, batch_size=batch_size) except RuntimeWarning: sys.stderr.write('WARNING: Oversmoothing detected, refusing to batch ' 'correct, consider lowering sigma value.\n') return csr_matrix(curr_ds.shape, dtype=float) except MemoryError: if batch_size is None: sys.stderr.write('WARNING: Out of memory, consider turning on ' 'batched computation with batch_size parameter.\n') else: sys.stderr.write('WARNING: Out of memory, consider lowering ' 'the batch_size parameter.\n') return csr_matrix(curr_ds.shape, dtype=float) if cn: avg_bias = csr_matrix(avg_bias) return avg_bias # Finds alignments between datasets and uses them to construct # panoramas. "Merges" datasets by correcting gene expression # values.
Example #29
Source File: problem.py From pymoo with Apache License 2.0 | 5 votes |
def _calc_gradient(self, out, keys): deriv = {} for key in keys: val = out[key] # calculate the jacobian matrix and set it - (ignore warnings of autograd here) with warnings.catch_warnings(): warnings.simplefilter("ignore") jac = calc_jacobian(out["__autograd__"], val) deriv["d" + key] = jac return deriv
Example #30
Source File: test_response.py From sanic with MIT License | 5 votes |
def test_response_body_bytes_deprecated(app): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") HTTPResponse(body_bytes=b"bytes") assert len(w) == 1 assert issubclass(w[0].category, DeprecationWarning) assert ( "Parameter `body_bytes` is deprecated, use `body` instead" in str(w[0].message) )