diff options
author | Ralf Gommers <ralf.gommers@googlemail.com> | 2015-07-12 00:29:34 +0200 |
---|---|---|
committer | Ralf Gommers <ralf.gommers@googlemail.com> | 2015-07-12 00:29:34 +0200 |
commit | ddc53885e1b66b6e4e7d299d2c808ddc472913b9 (patch) | |
tree | 0a6eb451c9d79a0a3b77f2387e31bb1486b4578d /numpy/testing/utils.py | |
parent | 57e6b4b1de8c50641119b3f11ba62e660eece95a (diff) | |
parent | ad40c230c4da2ca336bed6b093e8efaba590eec3 (diff) | |
download | numpy-ddc53885e1b66b6e4e7d299d2c808ddc472913b9.tar.gz |
Merge pull request #6049 from charris/pep8-numpy-testing
PEP8 and pyflakes fixups for numpy/testing and numpy/testing/tests
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r-- | numpy/testing/utils.py | 165 |
1 files changed, 94 insertions, 71 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 6244b3ea7..75d974b18 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -34,7 +34,7 @@ __all__ = ['assert_equal', 'assert_almost_equal', 'assert_approx_equal', verbose = 0 -def assert_(val, msg='') : +def assert_(val, msg=''): """ Assert that works in release mode. Accepts callable msg to allow deferring evaluation until failure. @@ -45,7 +45,7 @@ def assert_(val, msg='') : For documentation on usage, refer to the Python documentation. """ - if not val : + if not val: try: smsg = msg() except TypeError: @@ -118,27 +118,68 @@ def rand(*args): f[i] = random.random() return results -if sys.platform[:5]=='linux': - def jiffies(_proc_pid_stat = '/proc/%s/stat'%(os.getpid()), +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()) try: - f=open(_proc_pid_stat, 'r') + f = open(_proc_pid_stat, 'r') l = f.readline().split(' ') f.close() return int(l[13]) except: 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. + def memusage(_proc_pid_stat='/proc/%s/stat' % (os.getpid())): + """ + Return virtual memory size in bytes of the running python. + """ try: - f=open(_proc_pid_stat, 'r') + f = open(_proc_pid_stat, 'r') l = f.readline().split(' ') f.close() return int(l[22]) @@ -149,49 +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): @@ -256,11 +273,11 @@ def assert_equal(actual,desired,err_msg='',verbose=True): """ if isinstance(desired, dict): - if not isinstance(actual, dict) : + if not isinstance(actual, dict): raise AssertionError(repr(type(actual))) assert_equal(len(actual), len(desired), err_msg, verbose) for k, i in desired.items(): - if k not in actual : + if k not in actual: raise AssertionError(repr(k)) assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k, err_msg), verbose) return @@ -486,7 +503,7 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): return except (NotImplementedError, TypeError): pass - if round(abs(desired - actual), decimal) != 0 : + if round(abs(desired - actual), decimal) != 0: raise AssertionError(_build_err_msg()) @@ -550,7 +567,7 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): import numpy as np (actual, desired) = map(float, (actual, desired)) - if desired==actual: + if desired == actual: return # Normalized the numbers to be in range (-10.0,10.0) # scale = float(pow(10,math.floor(math.log10(0.5*(abs(desired)+abs(actual)))))) @@ -583,7 +600,7 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): return except (TypeError, NotImplementedError): pass - if np.abs(sc_desired - sc_actual) >= np.power(10., -(significant-1)) : + if np.abs(sc_desired - sc_actual) >= np.power(10., -(significant-1)): raise AssertionError(msg) def assert_array_compare(comparison, x, y, err_msg='', verbose=True, @@ -625,13 +642,13 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, assert_array_equal(x_id, y_id) except AssertionError: msg = build_err_msg([x, y], - err_msg + '\nx and y %s location mismatch:' \ + err_msg + '\nx and y %s location mismatch:' % (hasval), verbose=verbose, header=header, names=('x', 'y'), precision=precision) raise AssertionError(msg) try: - cond = (x.shape==() or y.shape==()) or x.shape == y.shape + cond = (x.shape == () or y.shape == ()) or x.shape == y.shape if not cond: msg = build_err_msg([x, y], err_msg @@ -639,7 +656,7 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, y.shape), verbose=verbose, header=header, names=('x', 'y'), precision=precision) - if not cond : + if not cond: raise AssertionError(msg) if isnumber(x) and isnumber(y): @@ -684,9 +701,9 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, + '\n(mismatch %s%%)' % (match,), verbose=verbose, header=header, names=('x', 'y'), precision=precision) - if not cond : + 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) @@ -834,6 +851,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): from numpy.core import around, number, float_, result_type, array from numpy.core.numerictypes import issubdtype from numpy.core.fromnumeric import any as npany + def compare(x, y): try: if npany(gisinf(x)) or npany( gisinf(y)): @@ -856,7 +874,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): z = abs(x-y) if not issubdtype(z.dtype, number): - z = z.astype(float_) # handle object arrays + z = z.astype(float_) # handle object arrays return around(z, decimal) <= 10.0**(-decimal) @@ -967,7 +985,7 @@ def assert_string_equal(actual, desired): # delay import of difflib to reduce startup time import difflib - if not isinstance(actual, str) : + if not isinstance(actual, str): raise AssertionError(repr(type(actual))) if not isinstance(desired, str): raise AssertionError(repr(type(desired))) @@ -986,7 +1004,7 @@ def assert_string_equal(actual, desired): if d2.startswith('? '): l.append(d2) d2 = diff.pop(0) - if not d2.startswith('+ ') : + if not d2.startswith('+ '): raise AssertionError(repr(d2)) l.append(d2) d3 = diff.pop(0) @@ -1002,7 +1020,7 @@ def assert_string_equal(actual, desired): if not diff_list: return msg = 'Differences in strings:\n%s' % (''.join(diff_list)).rstrip() - if actual != desired : + if actual != desired: raise AssertionError(msg) @@ -1028,7 +1046,8 @@ def rundocs(filename=None, raise_on_error=True): >>> np.lib.test(doctests=True) #doctest: +SKIP """ - import doctest, imp + import doctest + import imp if filename is None: f = sys._getframe(1) filename = f.f_globals['__file__'] @@ -1249,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): @@ -1313,6 +1333,7 @@ def assert_allclose(actual, desired, rtol=1e-7, atol=0, equal_nan=False, """ import numpy as np + def compare(x, y): return np.core.numeric.isclose(x, y, rtol=rtol, atol=atol, equal_nan=equal_nan) @@ -1422,7 +1443,7 @@ def assert_array_max_ulp(a, b, maxulp=1, dtype=None): import numpy as np ret = nulp_diff(a, b, dtype) if not np.all(ret <= maxulp): - raise AssertionError("Arrays are not almost equal up to %g ULP" % \ + raise AssertionError("Arrays are not almost equal up to %g ULP" % maxulp) return ret @@ -1468,7 +1489,7 @@ def nulp_diff(x, y, dtype=None): y = np.array(y, dtype=t) if not x.shape == y.shape: - raise ValueError("x and y do not have the same shape: %s - %s" % \ + raise ValueError("x and y do not have the same shape: %s - %s" % (x.shape, y.shape)) def _diff(rx, ry, vdt): @@ -1486,7 +1507,7 @@ def _integer_repr(x, vdt, comp): # http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm rx = x.view(vdt) if not (rx.size == 1): - rx[rx < 0] = comp - rx[rx<0] + rx[rx < 0] = comp - rx[rx < 0] else: if rx < 0: rx = comp - rx @@ -1562,6 +1583,7 @@ class WarningManager(object): It is copied so it can be used in NumPy with older Python versions. """ + def __init__(self, record=False, module=None): self._record = record if module is None: @@ -1579,6 +1601,7 @@ class WarningManager(object): self._showwarning = self._module.showwarning if self._record: log = [] + def showwarning(*args, **kwargs): log.append(WarningMessage(*args, **kwargs)) self._module.showwarning = showwarning @@ -1627,7 +1650,7 @@ def assert_warns(warning_class, func, *args, **kw): raise AssertionError("No warning raised when calling %s" % func.__name__) if not l[0].category is warning_class: - raise AssertionError("First warning for %s is not a " \ + raise AssertionError("First warning for %s is not a " "%s( is %s)" % (func.__name__, warning_class, l[0])) return result @@ -1690,7 +1713,7 @@ def _gen_alignment_data(dtype=float32, type='binary', max_size=24): for o in range(3): for s in range(o + 2, max(o + 3, max_size)): if type == 'unary': - inp = lambda : arange(s, dtype=dtype)[o:] + inp = lambda: arange(s, dtype=dtype)[o:] out = empty((s,), dtype=dtype)[o:] yield out, inp(), ufmt % (o, o, s, dtype, 'out of place') yield inp(), inp(), ufmt % (o, o, s, dtype, 'in place') @@ -1703,8 +1726,8 @@ def _gen_alignment_data(dtype=float32, type='binary', max_size=24): yield inp()[1:], inp()[:-1], ufmt % \ (o + 1, o, s - 1, dtype, 'aliased') if type == 'binary': - inp1 = lambda :arange(s, dtype=dtype)[o:] - inp2 = lambda :arange(s, dtype=dtype)[o:] + inp1 = lambda: arange(s, dtype=dtype)[o:] + inp2 = lambda: arange(s, dtype=dtype)[o:] out = empty((s,), dtype=dtype)[o:] yield out, inp1(), inp2(), bfmt % \ (o, o, o, s, dtype, 'out of place') |