|
1 | 1 | # Test methods with long descriptive names can omit docstrings
|
2 | 2 | # pylint: disable=all
|
3 |
| -from orangewidget.tests.utils import simulate |
4 |
| -from orangewidget.utils.itemmodels import PyListModel |
5 |
| - |
| 3 | +import pickle |
6 | 4 | from unittest import TestCase
|
7 | 5 | import numpy as np
|
8 | 6 | from numpy.testing import assert_array_equal
|
9 | 7 |
|
| 8 | +from orangewidget.tests.utils import simulate |
| 9 | +from orangewidget.utils.itemmodels import PyListModel |
| 10 | + |
10 | 11 | from AnyQt.QtCore import QModelIndex, QItemSelectionModel, Qt, QItemSelection
|
11 | 12 | from AnyQt.QtWidgets import QAction, QComboBox, QLineEdit, QStyleOptionViewItem
|
12 | 13 | from AnyQt.QtTest import QTest, QSignalSpy
|
|
25 | 26 | AsString, AsCategorical, AsContinuous, AsTime,
|
26 | 27 | table_column_data, ReinterpretVariableEditor, CategoricalVector,
|
27 | 28 | VariableEditDelegate, TransformRole,
|
28 |
| - RealVector, TimeVector, StringVector) |
| 29 | + RealVector, TimeVector, StringVector, make_dict_mapper, DictMissingConst, |
| 30 | + LookupMappingTransform, as_float_or_nan) |
29 | 31 | from Orange.widgets.data.owcolor import OWColor, ColorRole
|
30 | 32 | from Orange.widgets.tests.base import WidgetTest, GuiTest
|
31 | 33 | from Orange.tests import test_filename, assert_array_nanequal
|
32 | 34 |
|
33 | 35 |
|
34 | 36 | MArray = np.ma.MaskedArray
|
35 | 37 |
|
| 38 | + |
36 | 39 | class TestReport(TestCase):
|
37 | 40 | def test_rename(self):
|
38 | 41 | var = Real("X", (-1, ""), ())
|
@@ -676,3 +679,67 @@ def test_reinterpret_string(self):
|
676 | 679 | [1.0, 1., np.nan, "1.0", 2020., 1., 1577836800., "2020"],
|
677 | 680 | ], dtype=object)
|
678 | 681 | )
|
| 682 | + |
| 683 | + |
| 684 | +class TestUtils(TestCase): |
| 685 | + def test_mapper(self): |
| 686 | + mapper = make_dict_mapper({"a": 1, "b": 2}) |
| 687 | + r = mapper(["a", "a", "b"]) |
| 688 | + assert_array_equal(r, [1, 1, 2]) |
| 689 | + self.assertEqual(r.dtype, np.dtype("O")) |
| 690 | + r = mapper(["a", "a", "b"], dtype=float) |
| 691 | + assert_array_equal(r, [1, 1, 2]) |
| 692 | + self.assertEqual(r.dtype, np.dtype(float)) |
| 693 | + r = mapper(["a", "a", "b"], dtype=int) |
| 694 | + self.assertEqual(r.dtype, np.dtype(int)) |
| 695 | + |
| 696 | + mapper = make_dict_mapper({"a": 1, "b": 2}, dtype=int) |
| 697 | + r = mapper(["a", "a", "b"]) |
| 698 | + self.assertEqual(r.dtype, np.dtype(int)) |
| 699 | + |
| 700 | + r = np.full(3, -1, dtype=float) |
| 701 | + r_ = mapper(["a", "a", "b"], out=r) |
| 702 | + self.assertIs(r, r_) |
| 703 | + assert_array_equal(r, [1, 1, 2]) |
| 704 | + |
| 705 | + def test_dict_missing(self): |
| 706 | + d = DictMissingConst("<->", {1: 1, 2: 2}) |
| 707 | + self.assertEqual(d[1], 1) |
| 708 | + self.assertEqual(d[-1], "<->") |
| 709 | + # must be sufficiently different from defaultdict to warrant existence |
| 710 | + self.assertEqual(d, {1: 1, 2: 2}) |
| 711 | + |
| 712 | + def test_as_float_or_nan(self): |
| 713 | + a = np.array(["a", "1.1", ".2", "NaN"], object) |
| 714 | + r = as_float_or_nan(a) |
| 715 | + assert_array_equal(r, [np.nan, 1.1, .2, np.nan]) |
| 716 | + |
| 717 | + a = np.array([1, 2, 3], dtype=int) |
| 718 | + r = as_float_or_nan(a) |
| 719 | + assert_array_equal(r, [1., 2., 3.]) |
| 720 | + |
| 721 | + r = as_float_or_nan(r, dtype=np.float32) |
| 722 | + assert_array_equal(r, [1., 2., 3.]) |
| 723 | + self.assertEqual(r.dtype, np.dtype(np.float32)) |
| 724 | + |
| 725 | + |
| 726 | +class TestLookupMappingTransform(TestCase): |
| 727 | + def setUp(self) -> None: |
| 728 | + self.lookup = LookupMappingTransform( |
| 729 | + StringVariable("S"), |
| 730 | + DictMissingConst(np.nan, {"": np.nan, "a": 0, "b": 1}), |
| 731 | + dtype=float, |
| 732 | + ) |
| 733 | + |
| 734 | + def test_transform(self): |
| 735 | + r = self.lookup.transform(np.array(["", "a", "b", "c"])) |
| 736 | + assert_array_equal(r, [np.nan, 0, 1, np.nan]) |
| 737 | + |
| 738 | + def test_pickle(self): |
| 739 | + lookup = self.lookup |
| 740 | + lookup_ = pickle.loads(pickle.dumps(lookup)) |
| 741 | + c = np.array(["", "a", "b", "c"]) |
| 742 | + r = lookup.transform(c) |
| 743 | + assert_array_equal(r, [np.nan, 0, 1, np.nan]) |
| 744 | + r_ = lookup_.transform(c) |
| 745 | + assert_array_equal(r_, [np.nan, 0, 1, np.nan]) |
0 commit comments