From 209908b58733c12f5416c726dee3931f132e1dba Mon Sep 17 00:00:00 2001 From: Ales Erjavec Date: Fri, 9 Jun 2017 19:14:31 +0200 Subject: [PATCH] tests: Fix test errors when running with numpy 1.13.0 * Remove use of `assert_array_equal` for object arrays * Compare the `Reprable` output ignoring whitespace. --- Orange/tests/__init__.py | 26 ++++++++++++++++++++++++++ Orange/tests/test_instance.py | 21 ++++++++++----------- Orange/tests/test_table.py | 16 +--------------- Orange/tests/test_util.py | 6 +++--- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/Orange/tests/__init__.py b/Orange/tests/__init__.py index 0c727965688..e7977913c2b 100644 --- a/Orange/tests/__init__.py +++ b/Orange/tests/__init__.py @@ -3,6 +3,7 @@ import tempfile from contextlib import contextmanager +import numpy as np import Orange @@ -19,6 +20,31 @@ def named_file(content, encoding=None, suffix=''): os.remove(name) +@np.vectorize +def naneq(a, b): + try: + return (np.isnan(a) and np.isnan(b)) or a == b + except TypeError: + return a == b + + +def assert_array_nanequal(a, b, *args, **kwargs): + """ + Similar as np.testing.assert_array_equal but with better handling of + object arrays. + + Note + ---- + Is not fast! + + Parameters + ---------- + a : array-like + b : array-like + """ + return np.testing.utils.assert_array_compare(naneq, a, b, *args, **kwargs) + + def test_dirname(): """ Return the absolute path to the Orange.tests package. diff --git a/Orange/tests/test_instance.py b/Orange/tests/test_instance.py index a73fb6ff73e..b264ce61d00 100644 --- a/Orange/tests/test_instance.py +++ b/Orange/tests/test_instance.py @@ -12,6 +12,7 @@ from Orange.data import \ Instance, Domain, Unknown, Value, \ DiscreteVariable, ContinuousVariable, StringVariable +from Orange.tests import assert_array_nanequal class TestInstance(unittest.TestCase): @@ -75,11 +76,10 @@ def test_init_xym_no_data(self): self.assertEqual(inst._metas.shape, (3, )) self.assertTrue(all(isnan(x) for x in inst._x)) self.assertTrue(all(isnan(x) for x in inst._y)) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - assert_array_equal(inst._metas, - np.array([var.Unknown for var in domain.metas], - dtype=object)) + + assert_array_nanequal(inst._metas, + np.array([var.Unknown for var in domain.metas], + dtype=object)) def test_init_x_arr(self): domain = self.create_domain(["x", DiscreteVariable("g", values="MF")]) @@ -162,12 +162,11 @@ def test_init_inst(self): domain.class_vars, [self.metas[0], "w", domain[0]]) inst2 = Instance(domain2, inst) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - assert_array_equal(inst2._x, np.array([Unknown, 0, 43])) - self.assertEqual(inst2._y[0], 1) - assert_array_equal(inst2._metas, np.array([0, Unknown, 42], - dtype=object)) + + assert_array_nanequal(inst2._x, np.array([Unknown, 0, 43])) + self.assertEqual(inst2._y[0], 1) + assert_array_nanequal(inst2._metas, np.array([0, Unknown, 42], + dtype=object)) def test_get_item(self): domain = self.create_domain(["x", DiscreteVariable("g", values="MF")], diff --git a/Orange/tests/test_table.py b/Orange/tests/test_table.py index a3a0d849e3c..05eb843f2a4 100644 --- a/Orange/tests/test_table.py +++ b/Orange/tests/test_table.py @@ -14,21 +14,7 @@ from Orange import data from Orange.data import (filter, Unknown, Variable, Table, DiscreteVariable, ContinuousVariable, Domain, StringVariable) -from Orange.tests import test_dirname - - -@np.vectorize -def naneq(a, b): - try: - return (isnan(a) and isnan(b)) or a == b - except TypeError: - return a == b - - -def assert_array_nanequal(*args, **kwargs): - # similar as np.testing.assert_array_equal but with better handling of - # object arrays - return np.testing.utils.assert_array_compare(naneq, *args, **kwargs) +from Orange.tests import test_dirname, assert_array_nanequal class TableTestCase(unittest.TestCase): diff --git a/Orange/tests/test_util.py b/Orange/tests/test_util.py index 457cd3bc56a..51e2fb63abf 100644 --- a/Orange/tests/test_util.py +++ b/Orange/tests/test_util.py @@ -46,10 +46,10 @@ def test_reprable(self): var = ContinuousVariable('x') transform = ReplaceUnknownsRandom(var, Continuous(1, var)) - self.assertEqual(repr(transform).replace('\n ', ' '), + self.assertEqual(repr(transform).replace('\n', '').replace(' ', ''), "ReplaceUnknownsRandom(" - "variable=ContinuousVariable(name='x', number_of_decimals=3), " - "distribution=Continuous([[ 0.], [ 0.]]))") + "variable=ContinuousVariable(name='x',number_of_decimals=3)," + "distribution=Continuous([[0.],[0.]]))") # GH 2275 logit = LogisticRegressionLearner()