diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-07-05 10:18:57 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-07-05 10:35:01 -0600 |
commit | ad40c230c4da2ca336bed6b093e8efaba590eec3 (patch) | |
tree | 18abebb0ba7b594a122a690c1866be75cae2fdea | |
parent | c80c609950fe5aeecf3082d397c7b5149a834fca (diff) | |
download | numpy-ad40c230c4da2ca336bed6b093e8efaba590eec3.tar.gz |
MAINT: pyflakes for numpy/testing, numpy/testing/tests.
-rw-r--r-- | numpy/testing/decorators.py | 1 | ||||
-rw-r--r-- | numpy/testing/tests/test_decorators.py | 3 | ||||
-rw-r--r-- | numpy/testing/tests/test_utils.py | 32 | ||||
-rw-r--r-- | numpy/testing/utils.py | 106 |
4 files changed, 89 insertions, 53 deletions
diff --git a/numpy/testing/decorators.py b/numpy/testing/decorators.py index 22026ec42..56962b93c 100644 --- a/numpy/testing/decorators.py +++ b/numpy/testing/decorators.py @@ -247,7 +247,6 @@ def deprecated(conditional=True): # Local import to avoid a hard nose dependency and only incur the # import time overhead at actual test-time. import nose - from .noseclasses import KnownFailureTest def _deprecated_imp(*args, **kwargs): # Poor man's replacement for the with statement diff --git a/numpy/testing/tests/test_decorators.py b/numpy/testing/tests/test_decorators.py index 1c7f6c047..f8a5be672 100644 --- a/numpy/testing/tests/test_decorators.py +++ b/numpy/testing/tests/test_decorators.py @@ -1,7 +1,6 @@ from __future__ import division, absolute_import, print_function -import numpy as np -from numpy.testing import * +from numpy.testing import dec, assert_, assert_raises, run_module_suite from numpy.testing.noseclasses import KnownFailureTest import nose diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index e75914833..a31fce4af 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -9,17 +9,19 @@ from numpy.testing import ( assert_array_almost_equal, build_err_msg, raises, assert_raises, assert_warns, assert_no_warnings, assert_allclose, assert_approx_equal, assert_array_almost_equal_nulp, assert_array_max_ulp, - clear_and_catch_warnings, run_module_suite) + clear_and_catch_warnings, run_module_suite + ) import unittest + class _GenericTest(object): + def _test_equal(self, a, b): self._assert_func(a, b) def _test_not_equal(self, a, b): try: self._assert_func(a, b) - passed = True except AssertionError: pass else: @@ -61,7 +63,9 @@ class _GenericTest(object): def test_array_likes(self): self._test_equal([1, 2, 3], (1, 2, 3)) + class TestArrayEqual(_GenericTest, unittest.TestCase): + def setUp(self): self._assert_func = assert_array_equal @@ -139,7 +143,9 @@ class TestArrayEqual(_GenericTest, unittest.TestCase): self._test_not_equal(c, b) + class TestBuildErrorMessage(unittest.TestCase): + def test_build_err_msg_defaults(self): x = np.array([1.00001, 2.00002, 3.00003]) y = np.array([1.00002, 2.00003, 3.00004]) @@ -182,7 +188,9 @@ class TestBuildErrorMessage(unittest.TestCase): '1.000000002, 2.00003 , 3.00004 ])') self.assertEqual(a, b) + class TestEqual(TestArrayEqual): + def setUp(self): self._assert_func = assert_equal @@ -217,7 +225,9 @@ class TestEqual(TestArrayEqual): self._assert_func(x, x) self._test_not_equal(x, y) + class TestArrayAlmostEqual(_GenericTest, unittest.TestCase): + def setUp(self): self._assert_func = assert_array_almost_equal @@ -259,6 +269,7 @@ class TestArrayAlmostEqual(_GenericTest, unittest.TestCase): class TestAlmostEqual(_GenericTest, unittest.TestCase): + def setUp(self): self._assert_func = assert_almost_equal @@ -321,7 +332,9 @@ class TestAlmostEqual(_GenericTest, unittest.TestCase): # remove anything that's not the array string self.assertEqual(str(e).split('%)\n ')[1], b) + class TestApproxEqual(unittest.TestCase): + def setUp(self): self._assert_func = assert_approx_equal @@ -368,7 +381,9 @@ class TestApproxEqual(unittest.TestCase): self.assertRaises(AssertionError, lambda: self._assert_func(ainf, anan)) + class TestRaises(unittest.TestCase): + def setUp(self): class MyException(Exception): pass @@ -382,11 +397,11 @@ class TestRaises(unittest.TestCase): pass def test_correct_catch(self): - f = raises(self.e)(self.raises_exception)(self.e) + raises(self.e)(self.raises_exception)(self.e) # raises? def test_wrong_exception(self): try: - f = raises(self.e)(self.raises_exception)(RuntimeError) + raises(self.e)(self.raises_exception)(RuntimeError) # raises? except RuntimeError: return else: @@ -394,13 +409,15 @@ class TestRaises(unittest.TestCase): def test_catch_no_raise(self): try: - f = raises(self.e)(self.does_not_raise_exception)() + raises(self.e)(self.does_not_raise_exception)() # raises? except AssertionError: return else: raise AssertionError("should have raised an AssertionError") + class TestWarns(unittest.TestCase): + def test_warn(self): def f(): warnings.warn("yo") @@ -436,7 +453,9 @@ class TestWarns(unittest.TestCase): if failed: raise AssertionError("wrong warning caught by assert_warn") + class TestAssertAllclose(unittest.TestCase): + def test_simple(self): x = 1e-3 y = 1e-9 @@ -473,6 +492,7 @@ class TestAssertAllclose(unittest.TestCase): msg = exc.args[0] self.assertTrue("mismatch 25.0%" in msg) + class TestArrayAlmostEqualNulp(unittest.TestCase): def test_float64_pass(self): @@ -641,6 +661,7 @@ class TestArrayAlmostEqualNulp(unittest.TestCase): class TestULP(unittest.TestCase): + def test_equal(self): x = np.random.randn(10) assert_array_max_ulp(x, x, maxulp=0) @@ -743,6 +764,7 @@ def test_clear_and_catch_warnings(): class my_cacw(clear_and_catch_warnings): + class_modules = (sys.modules[__name__],) diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 97408addb..75d974b18 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -118,11 +118,50 @@ def rand(*args): f[i] = random.random() return results -if sys.platform[:5] == 'linux': +if os.name == 'nt': + # Code "stolen" from enthought/debug/memusage.py + def GetPerformanceAttributes(object, counter, instance=None, + inum=-1, format=None, machine=None): + # NOTE: Many counters require 2 samples to give accurate results, + # including "% Processor Time" (as by definition, at any instant, a + # thread's CPU usage is either 0 or 100). To read counters like this, + # you should copy this function, but keep the counter open, and call + # CollectQueryData() each time you need to know. + # See http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp + # My older explanation for this was that the "AddCounter" process forced + # the CPU to 100%, but the above makes more sense :) + import win32pdh + if format is None: + format = win32pdh.PDH_FMT_LONG + path = win32pdh.MakeCounterPath( (machine, object, instance, None, inum, counter)) + hq = win32pdh.OpenQuery() + try: + hc = win32pdh.AddCounter(hq, path) + try: + win32pdh.CollectQueryData(hq) + type, val = win32pdh.GetFormattedCounterValue(hc, format) + return val + finally: + win32pdh.RemoveCounter(hc) + finally: + win32pdh.CloseQuery(hq) + + def memusage(processName="python", instance=0): + # from win32pdhutil, part of the win32all package + import win32pdh + return GetPerformanceAttributes("Process", "Virtual Bytes", + processName, instance, + win32pdh.PDH_FMT_LONG, None) +elif sys.platform[:5] == 'linux': def jiffies(_proc_pid_stat='/proc/%s/stat' % (os.getpid()), _load_time=[]): - """ Return number of jiffies (1/100ths of a second) that this - process has been scheduled in user mode. See man 5 proc. """ + """ + Return number of jiffies elapsed. + + Return number of jiffies (1/100ths of a second) that this + process has been scheduled in user mode. See man 5 proc. + + """ import time if not _load_time: _load_time.append(time.time()) @@ -135,7 +174,9 @@ if sys.platform[:5] == 'linux': return int(100*(time.time()-_load_time[0])) def memusage(_proc_pid_stat='/proc/%s/stat' % (os.getpid())): - """ Return virtual memory size in bytes of the running python. + """ + Return virtual memory size in bytes of the running python. + """ try: f = open(_proc_pid_stat, 'r') @@ -149,51 +190,25 @@ else: # Using time is safe but inaccurate, especially when process # was suspended or sleeping. def jiffies(_load_time=[]): - """ Return number of jiffies (1/100ths of a second) that this - process has been scheduled in user mode. [Emulation with time.time]. """ + """ + Return number of jiffies elapsed. + + Return number of jiffies (1/100ths of a second) that this + process has been scheduled in user mode. See man 5 proc. + + """ import time if not _load_time: _load_time.append(time.time()) return int(100*(time.time()-_load_time[0])) def memusage(): - """ Return memory usage of running python. [Not implemented]""" - raise NotImplementedError + """ + Return memory usage of running python. [Not implemented] -if os.name == 'nt': - # Code "stolen" from enthought/debug/memusage.py - def GetPerformanceAttributes(object, counter, instance=None, - inum=-1, format=None, machine=None): - # NOTE: Many counters require 2 samples to give accurate results, - # including "% Processor Time" (as by definition, at any instant, a - # thread's CPU usage is either 0 or 100). To read counters like this, - # you should copy this function, but keep the counter open, and call - # CollectQueryData() each time you need to know. - # See http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp - # My older explanation for this was that the "AddCounter" process forced - # the CPU to 100%, but the above makes more sense :) - import win32pdh - if format is None: - format = win32pdh.PDH_FMT_LONG - path = win32pdh.MakeCounterPath( (machine, object, instance, None, inum, counter)) - hq = win32pdh.OpenQuery() - try: - hc = win32pdh.AddCounter(hq, path) - try: - win32pdh.CollectQueryData(hq) - type, val = win32pdh.GetFormattedCounterValue(hc, format) - return val - finally: - win32pdh.RemoveCounter(hc) - finally: - win32pdh.CloseQuery(hq) + """ + raise NotImplementedError - def memusage(processName="python", instance=0): - # from win32pdhutil, part of the win32all package - import win32pdh - return GetPerformanceAttributes("Process", "Virtual Bytes", - processName, instance, - win32pdh.PDH_FMT_LONG, None) def build_err_msg(arrays, err_msg, header='Items are not equal:', verbose=True, names=('ACTUAL', 'DESIRED'), precision=8): @@ -688,7 +703,7 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, names=('x', 'y'), precision=precision) if not cond: raise AssertionError(msg) - except ValueError as e: + except ValueError: import traceback efmt = traceback.format_exc() header = 'error during assertion:\n\n%s\n\n%s' % (efmt, header) @@ -1253,23 +1268,24 @@ def measure(code_str,times=1,label=None): elapsed = jiffies() - elapsed return 0.01*elapsed + def _assert_valid_refcount(op): """ Check that ufuncs don't mishandle refcount of object `1`. Used in a few regression tests. """ import numpy as np - a = np.arange(100 * 100) + b = np.arange(100*100).reshape(100, 100) c = b - i = 1 rc = sys.getrefcount(i) for j in range(15): d = op(b, c) - assert_(sys.getrefcount(i) >= rc) + del d # for pyflakes + def assert_allclose(actual, desired, rtol=1e-7, atol=0, equal_nan=False, err_msg='', verbose=True): |