|
1 | 1 | # Test methods with long descriptive names can omit docstrings |
2 | 2 | # pylint: disable=all |
| 3 | +import pickle |
3 | 4 | from itertools import product |
4 | 5 | from unittest import TestCase |
5 | 6 |
|
|
27 | 28 | AsString, AsCategorical, AsContinuous, AsTime, |
28 | 29 | table_column_data, ReinterpretVariableEditor, CategoricalVector, |
29 | 30 | VariableEditDelegate, TransformRole, |
30 | | - RealVector, TimeVector, StringVector) |
| 31 | + RealVector, TimeVector, StringVector, make_dict_mapper, DictMissingConst, |
| 32 | + LookupMappingTransform, as_float_or_nan, column_str_repr |
| 33 | +) |
31 | 34 | from Orange.widgets.data.owcolor import OWColor, ColorRole |
32 | 35 | from Orange.widgets.tests.base import WidgetTest, GuiTest |
33 | 36 | from Orange.tests import test_filename, assert_array_nanequal |
@@ -758,3 +761,81 @@ def test_null_transform(self): |
758 | 761 | domain = table.domain |
759 | 762 | v = apply_transform(domain.metas[0],table, []) |
760 | 763 | self.assertIs(v, domain.metas[0]) |
| 764 | + |
| 765 | + |
| 766 | +class TestUtils(TestCase): |
| 767 | + def test_mapper(self): |
| 768 | + mapper = make_dict_mapper({"a": 1, "b": 2}) |
| 769 | + r = mapper(["a", "a", "b"]) |
| 770 | + assert_array_equal(r, [1, 1, 2]) |
| 771 | + self.assertEqual(r.dtype, np.dtype("O")) |
| 772 | + r = mapper(["a", "a", "b"], dtype=float) |
| 773 | + assert_array_equal(r, [1, 1, 2]) |
| 774 | + self.assertEqual(r.dtype, np.dtype(float)) |
| 775 | + r = mapper(["a", "a", "b"], dtype=int) |
| 776 | + self.assertEqual(r.dtype, np.dtype(int)) |
| 777 | + |
| 778 | + mapper = make_dict_mapper({"a": 1, "b": 2}, dtype=int) |
| 779 | + r = mapper(["a", "a", "b"]) |
| 780 | + self.assertEqual(r.dtype, np.dtype(int)) |
| 781 | + |
| 782 | + r = np.full(3, -1, dtype=float) |
| 783 | + r_ = mapper(["a", "a", "b"], out=r) |
| 784 | + self.assertIs(r, r_) |
| 785 | + assert_array_equal(r, [1, 1, 2]) |
| 786 | + |
| 787 | + def test_dict_missing(self): |
| 788 | + d = DictMissingConst("<->", {1: 1, 2: 2}) |
| 789 | + self.assertEqual(d[1], 1) |
| 790 | + self.assertEqual(d[-1], "<->") |
| 791 | + # must be sufficiently different from defaultdict to warrant existence |
| 792 | + self.assertEqual(d, {1: 1, 2: 2}) |
| 793 | + |
| 794 | + def test_as_float_or_nan(self): |
| 795 | + a = np.array(["a", "1.1", ".2", "NaN"], object) |
| 796 | + r = as_float_or_nan(a) |
| 797 | + assert_array_equal(r, [np.nan, 1.1, .2, np.nan]) |
| 798 | + |
| 799 | + a = np.array([1, 2, 3], dtype=int) |
| 800 | + r = as_float_or_nan(a) |
| 801 | + assert_array_equal(r, [1., 2., 3.]) |
| 802 | + |
| 803 | + r = as_float_or_nan(r, dtype=np.float32) |
| 804 | + assert_array_equal(r, [1., 2., 3.]) |
| 805 | + self.assertEqual(r.dtype, np.dtype(np.float32)) |
| 806 | + |
| 807 | + def test_column_str_repr(self): |
| 808 | + v = StringVariable("S") |
| 809 | + d = column_str_repr(v, np.array(["A", "", "B"])) |
| 810 | + assert_array_equal(d, ["A", "?", "B"]) |
| 811 | + v = ContinuousVariable("C") |
| 812 | + d = column_str_repr(v, np.array([0.1, np.nan, 1.0])) |
| 813 | + assert_array_equal(d, ["0.1", "?", "1"]) |
| 814 | + v = DiscreteVariable("D", ("a", "b")) |
| 815 | + d = column_str_repr(v, np.array([0., np.nan, 1.0])) |
| 816 | + assert_array_equal(d, ["a", "?", "b"]) |
| 817 | + v = TimeVariable("T", have_date=False, have_time=True) |
| 818 | + d = column_str_repr(v, np.array([0., np.nan, 1.0])) |
| 819 | + assert_array_equal(d, ["00:00:00", "?", "00:00:01"]) |
| 820 | + |
| 821 | + |
| 822 | +class TestLookupMappingTransform(TestCase): |
| 823 | + def setUp(self) -> None: |
| 824 | + self.lookup = LookupMappingTransform( |
| 825 | + StringVariable("S"), |
| 826 | + DictMissingConst(np.nan, {"": np.nan, "a": 0, "b": 1}), |
| 827 | + dtype=float, |
| 828 | + ) |
| 829 | + |
| 830 | + def test_transform(self): |
| 831 | + r = self.lookup.transform(np.array(["", "a", "b", "c"])) |
| 832 | + assert_array_equal(r, [np.nan, 0, 1, np.nan]) |
| 833 | + |
| 834 | + def test_pickle(self): |
| 835 | + lookup = self.lookup |
| 836 | + lookup_ = pickle.loads(pickle.dumps(lookup)) |
| 837 | + c = np.array(["", "a", "b", "c"]) |
| 838 | + r = lookup.transform(c) |
| 839 | + assert_array_equal(r, [np.nan, 0, 1, np.nan]) |
| 840 | + r_ = lookup_.transform(c) |
| 841 | + assert_array_equal(r_, [np.nan, 0, 1, np.nan]) |
0 commit comments