Skip to content

Commit

Permalink
Merge pull request #2396 from ales-erjavec/numpy-1.13.0
Browse files Browse the repository at this point in the history
[FIX] tests: Fix test errors when running with numpy 1.13.0
  • Loading branch information
kernc authored Jun 12, 2017
2 parents ce1d881 + 209908b commit b9f65fe
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
26 changes: 26 additions & 0 deletions Orange/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
from contextlib import contextmanager

import numpy as np
import Orange


Expand All @@ -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.
Expand Down
21 changes: 10 additions & 11 deletions Orange/tests/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")])
Expand Down Expand Up @@ -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")],
Expand Down
16 changes: 1 addition & 15 deletions Orange/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions Orange/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b9f65fe

Please sign in to comment.