Skip to content

Commit b9f65fe

Browse files
authored
Merge pull request #2396 from ales-erjavec/numpy-1.13.0
[FIX] tests: Fix test errors when running with numpy 1.13.0
2 parents ce1d881 + 209908b commit b9f65fe

4 files changed

Lines changed: 40 additions & 29 deletions

File tree

Orange/tests/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44
from contextlib import contextmanager
55

6+
import numpy as np
67
import Orange
78

89

@@ -19,6 +20,31 @@ def named_file(content, encoding=None, suffix=''):
1920
os.remove(name)
2021

2122

23+
@np.vectorize
24+
def naneq(a, b):
25+
try:
26+
return (np.isnan(a) and np.isnan(b)) or a == b
27+
except TypeError:
28+
return a == b
29+
30+
31+
def assert_array_nanequal(a, b, *args, **kwargs):
32+
"""
33+
Similar as np.testing.assert_array_equal but with better handling of
34+
object arrays.
35+
36+
Note
37+
----
38+
Is not fast!
39+
40+
Parameters
41+
----------
42+
a : array-like
43+
b : array-like
44+
"""
45+
return np.testing.utils.assert_array_compare(naneq, a, b, *args, **kwargs)
46+
47+
2248
def test_dirname():
2349
"""
2450
Return the absolute path to the Orange.tests package.

Orange/tests/test_instance.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from Orange.data import \
1313
Instance, Domain, Unknown, Value, \
1414
DiscreteVariable, ContinuousVariable, StringVariable
15+
from Orange.tests import assert_array_nanequal
1516

1617

1718
class TestInstance(unittest.TestCase):
@@ -75,11 +76,10 @@ def test_init_xym_no_data(self):
7576
self.assertEqual(inst._metas.shape, (3, ))
7677
self.assertTrue(all(isnan(x) for x in inst._x))
7778
self.assertTrue(all(isnan(x) for x in inst._y))
78-
with warnings.catch_warnings():
79-
warnings.simplefilter("ignore", FutureWarning)
80-
assert_array_equal(inst._metas,
81-
np.array([var.Unknown for var in domain.metas],
82-
dtype=object))
79+
80+
assert_array_nanequal(inst._metas,
81+
np.array([var.Unknown for var in domain.metas],
82+
dtype=object))
8383

8484
def test_init_x_arr(self):
8585
domain = self.create_domain(["x", DiscreteVariable("g", values="MF")])
@@ -162,12 +162,11 @@ def test_init_inst(self):
162162
domain.class_vars,
163163
[self.metas[0], "w", domain[0]])
164164
inst2 = Instance(domain2, inst)
165-
with warnings.catch_warnings():
166-
warnings.simplefilter("ignore", FutureWarning)
167-
assert_array_equal(inst2._x, np.array([Unknown, 0, 43]))
168-
self.assertEqual(inst2._y[0], 1)
169-
assert_array_equal(inst2._metas, np.array([0, Unknown, 42],
170-
dtype=object))
165+
166+
assert_array_nanequal(inst2._x, np.array([Unknown, 0, 43]))
167+
self.assertEqual(inst2._y[0], 1)
168+
assert_array_nanequal(inst2._metas, np.array([0, Unknown, 42],
169+
dtype=object))
171170

172171
def test_get_item(self):
173172
domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],

Orange/tests/test_table.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,7 @@
1414
from Orange import data
1515
from Orange.data import (filter, Unknown, Variable, Table, DiscreteVariable,
1616
ContinuousVariable, Domain, StringVariable)
17-
from Orange.tests import test_dirname
18-
19-
20-
@np.vectorize
21-
def naneq(a, b):
22-
try:
23-
return (isnan(a) and isnan(b)) or a == b
24-
except TypeError:
25-
return a == b
26-
27-
28-
def assert_array_nanequal(*args, **kwargs):
29-
# similar as np.testing.assert_array_equal but with better handling of
30-
# object arrays
31-
return np.testing.utils.assert_array_compare(naneq, *args, **kwargs)
17+
from Orange.tests import test_dirname, assert_array_nanequal
3218

3319

3420
class TableTestCase(unittest.TestCase):

Orange/tests/test_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def test_reprable(self):
4646
var = ContinuousVariable('x')
4747
transform = ReplaceUnknownsRandom(var, Continuous(1, var))
4848

49-
self.assertEqual(repr(transform).replace('\n ', ' '),
49+
self.assertEqual(repr(transform).replace('\n', '').replace(' ', ''),
5050
"ReplaceUnknownsRandom("
51-
"variable=ContinuousVariable(name='x', number_of_decimals=3), "
52-
"distribution=Continuous([[ 0.], [ 0.]]))")
51+
"variable=ContinuousVariable(name='x',number_of_decimals=3),"
52+
"distribution=Continuous([[0.],[0.]]))")
5353

5454
# GH 2275
5555
logit = LogisticRegressionLearner()

0 commit comments

Comments
 (0)