def test_call(self): f = FeatureFunc("a + 1", [("a", "a")]) self.assertEqual(f({"a": 2}), 3) iris = Table("iris") f = FeatureFunc("sepal_width + 10", [("sepal_width", iris.domain["sepal width"])]) r = f(iris) np.testing.assert_array_equal(r, iris.X[:, 1] + 10)
def test_call(self): iris = Table("iris") f = FeatureFunc("sepal_width + 10", [("sepal_width", iris.domain["sepal width"])]) r = f(iris) np.testing.assert_array_equal(r, iris.X[:, 1] + 10) self.assertEqual(f(iris[0]), iris[0]["sepal width"] + 10)
def test_reconstruct(self): f = FeatureFunc("a * x + c", [("x", "x")], {"a": 2, "c": 10}) self.assertEqual(f({"x": 2}), 14) f1 = pickle.loads(pickle.dumps(f)) self.assertEqual(f1({"x": 2}), 14) fc = copy.copy(f) self.assertEqual(fc({"x": 3}), 16)
def test_string_casting(self): zoo = Table("zoo") f = FeatureFunc("name[0]", [("name", zoo.domain["name"])]) r = f(zoo) self.assertEqual(r, [x[0] for x in zoo.metas[:, 0]]) self.assertEqual(f(zoo[0]), str(zoo[0, "name"])[0])
def test_hash_eq(self): iris = Table("iris") f = FeatureFunc("1 / petal_length", [("petal_length", iris.domain["petal length"])]) g = copy.deepcopy(f) self.assertEqual(f, g) self.assertEqual(hash(f), hash(g))
def test_invalid_expression_variable(self): iris = Table("iris") f = FeatureFunc("1 / petal_length", [("petal_length", iris.domain["petal length"])]) iris[0]["petal length"] = 0 f.mask_exceptions = False self.assertRaises(Exception, f, iris) self.assertRaises(Exception, f, iris[0]) _ = f(iris[1]) f.mask_exceptions = True r = f(iris) self.assertTrue(np.isnan(r[0])) self.assertFalse(np.isnan(r[1])) self.assertTrue(np.isnan(f(iris[0]))) self.assertFalse(np.isnan(f(iris[1])))
def test_missing_variable(self): zoo = Table("zoo") assert zoo.domain.class_var.name == "type" f = FeatureFunc("type[0]", [("type", zoo.domain["type"])]) no_class = Domain(zoo.domain.attributes, None, zoo.domain.metas) data2 = zoo.transform(no_class) r = f(data2) self.assertTrue(np.all(np.isnan(r))) self.assertTrue(np.isnan(f(data2[0])))
def test_reconstruct(self): iris = Table("iris") inst1 = iris[0] val1 = 2 * inst1["sepal width"] + 10 inst2 = iris[100] val2 = 2 * inst2["sepal width"] + 10 f = FeatureFunc("a * sepal_width + c", [("sepal_width", iris.domain["sepal width"])], {"a": 2, "c": 10}) self.assertAlmostEqual(f(inst1), val1) f1 = pickle.loads(pickle.dumps(f)) self.assertAlmostEqual(f1(inst1), val1) fc = copy.copy(f) self.assertEqual(fc(inst2), val2)
def test_repr(self): self.assertEqual(repr(FeatureFunc("a + 1", [("a", 2)])), "FeatureFunc('a + 1', [('a', 2)], {}, None)")
def test_time_str(self): data = Table.from_numpy(Domain([TimeVariable("T", have_date=True)]), [[0], [0]]) f = FeatureFunc("str(T)", [("T", data.domain[0])]) c = f(data) self.assertEqual(c, ["1970-01-01", "1970-01-01"])