diff options
Diffstat (limited to 'numpy/testing')
-rw-r--r-- | numpy/testing/__init__.py | 3 | ||||
-rw-r--r-- | numpy/testing/nosetester.py | 6 | ||||
-rw-r--r-- | numpy/testing/tests/test_utils.py | 17 | ||||
-rw-r--r-- | numpy/testing/utils.py | 41 |
4 files changed, 53 insertions, 14 deletions
diff --git a/numpy/testing/__init__.py b/numpy/testing/__init__.py index 258cbe928..dcc02ad57 100644 --- a/numpy/testing/__init__.py +++ b/numpy/testing/__init__.py @@ -10,7 +10,6 @@ from __future__ import division, absolute_import, print_function from unittest import TestCase from . import decorators as dec +from .nosetester import run_module_suite, NoseTester as Tester from .utils import * -from .nosetester import NoseTester as Tester -from .nosetester import run_module_suite test = Tester().test diff --git a/numpy/testing/nosetester.py b/numpy/testing/nosetester.py index a06d559e7..b802f4472 100644 --- a/numpy/testing/nosetester.py +++ b/numpy/testing/nosetester.py @@ -90,7 +90,7 @@ def run_module_suite(file_to_run=None, argv=None): argv: list of strings Arguments to be passed to the nose test runner. ``argv[0]`` is ignored. All command line arguments accepted by ``nosetests`` - will work. + will work. If it is the default value None, sys.argv is used. .. versionadded:: 1.9.0 @@ -117,7 +117,7 @@ def run_module_suite(file_to_run=None, argv=None): raise AssertionError if argv is None: - argv = ['', file_to_run] + argv = sys.argv + [file_to_run] else: argv = argv + [file_to_run] @@ -246,6 +246,8 @@ class NoseTester(object): import numpy print("NumPy version %s" % numpy.__version__) + relaxed_strides = numpy.ones((10, 1), order="C").flags.f_contiguous + print("NumPy relaxed strides checking option:", relaxed_strides) npdir = os.path.dirname(numpy.__file__) print("NumPy is installed in %s" % npdir) diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index aa0a2669f..34608125d 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -244,6 +244,14 @@ class TestArrayAlmostEqual(_GenericTest, unittest.TestCase): self.assertRaises(AssertionError, lambda : self._assert_func(a, b)) + def test_subclass(self): + a = np.array([[1., 2.], [3., 4.]]) + b = np.ma.masked_array([[1., 2.], [0., 4.]], + [[False, False], [True, False]]) + assert_array_almost_equal(a, b) + assert_array_almost_equal(b, a) + assert_array_almost_equal(b, b) + class TestAlmostEqual(_GenericTest, unittest.TestCase): def setUp(self): self._assert_func = assert_almost_equal @@ -449,6 +457,15 @@ class TestAssertAllclose(unittest.TestCase): # Should not raise: assert_allclose(a, a) + def test_report_fail_percentage(self): + a = np.array([1, 1, 1, 1]) + b = np.array([1, 1, 1, 2]) + try: + assert_allclose(a, b) + msg = '' + except AssertionError as exc: + msg = exc.args[0] + self.assertTrue("mismatch 25.0%" in msg) class TestArrayAlmostEqualNulp(unittest.TestCase): @dec.knownfailureif(True, "Github issue #347") diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index d93fbd630..b9bdaeb87 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -10,6 +10,9 @@ import re import operator import warnings from functools import partial +import shutil +import contextlib +from tempfile import mkdtemp from .nosetester import import_nose from numpy.core import float32, empty, arange, array_repr, ndarray @@ -156,7 +159,7 @@ else: """ Return memory usage of running python. [Not implemented]""" raise NotImplementedError -if os.name=='nt' and sys.version[:3] > '2.3': +if os.name=='nt': # Code "stolen" from enthought/debug/memusage.py def GetPerformanceAttributes(object, counter, instance = None, inum=-1, format = None, machine=None): @@ -219,7 +222,7 @@ def build_err_msg(arrays, err_msg, header='Items are not equal:', def assert_equal(actual,desired,err_msg='',verbose=True): """ - Raise an assertion if two objects are not equal. + Raises an AssertionError if two objects are not equal. Given two objects (scalars, lists, tuples, dictionaries or numpy arrays), check that all elements of these objects are equal. An exception is raised @@ -371,7 +374,8 @@ def print_assert_equal(test_string, actual, desired): def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): """ - Raise an assertion if two items are not equal up to desired precision. + Raises an AssertionError if two items are not equal up to desired + precision. .. note:: It is recommended to use one of `assert_allclose`, `assert_array_almost_equal_nulp` or `assert_array_max_ulp` @@ -488,7 +492,8 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): """ - Raise an assertion if two items are not equal up to significant digits. + Raises an AssertionError if two items are not equal up to significant + digits. .. note:: It is recommended to use one of `assert_allclose`, `assert_array_almost_equal_nulp` or `assert_array_max_ulp` @@ -669,7 +674,7 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, def assert_array_equal(x, y, err_msg='', verbose=True): """ - Raise an assertion if two array_like objects are not equal. + Raises an AssertionError if two array_like objects are not equal. Given two array_like objects, check that the shape is equal and all elements of these objects are equal. An exception is raised at @@ -735,7 +740,8 @@ def assert_array_equal(x, y, err_msg='', verbose=True): def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): """ - Raise an assertion if two objects are not equal up to desired precision. + Raises an AssertionError if two objects are not equal up to desired + precision. .. note:: It is recommended to use one of `assert_allclose`, `assert_array_almost_equal_nulp` or `assert_array_max_ulp` @@ -823,7 +829,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): # make sure y is an inexact type to avoid abs(MIN_INT); will cause # casting of x later. dtype = result_type(y, 1.) - y = array(y, dtype=dtype, copy=False) + y = array(y, dtype=dtype, copy=False, subok=True) z = abs(x-y) if not issubdtype(z.dtype, number): @@ -838,7 +844,8 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): def assert_array_less(x, y, err_msg='', verbose=True): """ - Raise an assertion if two array_like objects are not ordered by less than. + Raises an AssertionError if two array_like objects are not ordered by less + than. Given two array_like objects, check that the shape is equal and all elements of the first object are strictly smaller than those of the @@ -1240,7 +1247,8 @@ def _assert_valid_refcount(op): def assert_allclose(actual, desired, rtol=1e-7, atol=0, err_msg='', verbose=True): """ - Raise an assertion if two objects are not equal up to desired tolerance. + Raises an AssertionError if two objects are not equal up to desired + tolerance. The test is equivalent to ``allclose(actual, desired, rtol, atol)``. It compares the difference between `actual` and `desired` to @@ -1281,7 +1289,7 @@ def assert_allclose(actual, desired, rtol=1e-7, atol=0, """ import numpy as np def compare(x, y): - return np.allclose(x, y, rtol=rtol, atol=atol) + return np.core.numeric._allclose_points(x, y, rtol=rtol, atol=atol) actual, desired = np.asanyarray(actual), np.asanyarray(desired) header = 'Not equal to tolerance rtol=%g, atol=%g' % (rtol, atol) @@ -1692,3 +1700,16 @@ def _gen_alignment_data(dtype=float32, type='binary', max_size=24): class IgnoreException(Exception): "Ignoring this exception due to disabled feature" + + +@contextlib.contextmanager +def tempdir(*args, **kwargs): + """Context manager to provide a temporary test folder. + + All arguments are passed as this to the underlying tempfile.mkdtemp + function. + + """ + tmpdir = mkdtemp(*args, **kwargs) + yield tmpdir + shutil.rmtree(tmpdir) |