From 9b938e5793f4a3833f901595b82a9441b2443419 Mon Sep 17 00:00:00 2001 From: Federico Sabbatini Date: Wed, 10 Aug 2022 02:13:20 +0200 Subject: [PATCH] fix: rebase on master --- test/psyke/classification/real/test_real.py | 36 ------------ test/psyke/regression/gridex/test_gridex.py | 62 --------------------- test/psyke/regression/iter/test_iter.py | 46 --------------- 3 files changed, 144 deletions(-) delete mode 100644 test/psyke/classification/real/test_real.py delete mode 100644 test/psyke/regression/gridex/test_gridex.py delete mode 100644 test/psyke/regression/iter/test_iter.py diff --git a/test/psyke/classification/real/test_real.py b/test/psyke/classification/real/test_real.py deleted file mode 100644 index c4a95b57..00000000 --- a/test/psyke/classification/real/test_real.py +++ /dev/null @@ -1,36 +0,0 @@ -import unittest -from parameterized import parameterized_class -from tuprolog.solve.prolog import prolog_solver -from psyke import logger -from psyke.utils.dataframe import get_discrete_dataset, get_discrete_features_supervised -from test import get_in_rule, get_not_in_rule -from test.psyke import initialize, data_to_struct - - -@parameterized_class(initialize('real')) -class TestReal(unittest.TestCase): - - def test_extract(self): - logger.info(self.expected_theory) - logger.info(self.extracted_theory) - self.assertTrue(self.expected_theory.equals(self.extracted_theory, False)) - - def test_predict(self): - - discrete_dataset = get_discrete_dataset(self.test_set.iloc[:, :-1], self.discretization) - predictions = self.extractor.predict(discrete_dataset) - solver = prolog_solver(static_kb=self.extracted_theory.assertZ(get_in_rule()).assertZ(get_not_in_rule())) - - substitutions = [solver.solveOnce(data_to_struct(data)) - for _, data in self.test_set.iterrows()] - index = self.test_set.shape[1] - 1 - expected = [str(query.solved_query.get_arg_at(index)) if query.is_yes else None for query in substitutions] - - logger.info(predictions) - logger.info(expected) - - self.assertTrue(predictions == expected) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/psyke/regression/gridex/test_gridex.py b/test/psyke/regression/gridex/test_gridex.py deleted file mode 100644 index 13c4182e..00000000 --- a/test/psyke/regression/gridex/test_gridex.py +++ /dev/null @@ -1,62 +0,0 @@ -from tuprolog.core import Var, Real -from psyke import logger -from parameterized import parameterized_class -from psyke.utils import get_default_precision, get_int_precision -from test import get_in_rule -from test.psyke import initialize, data_to_struct, are_similar, are_equal -from tuprolog.solve.prolog import prolog_solver -import numpy as np -import unittest - - -@parameterized_class(initialize('gridex')) -class TestGridEx(unittest.TestCase): - - def test_extract(self): - logger.info(self.expected_theory) - logger.info(self.extracted_theory) - - if isinstance(self.test_set.iloc[0, -1], str): - self.assertTrue(self.expected_theory.equals(self.extracted_theory, False)) - else: - for exp, ext in zip(self.expected_theory, self.extracted_theory): - for v1, v2 in zip(exp.head.args, ext.head.args): - if isinstance(v1, Var): - self.assertTrue(isinstance(v2, Var)) - self.assertTrue(v1.equals(v2, False)) - elif isinstance(v1, Real): - self.assertTrue(isinstance(v2, Real)) - self.assertTrue(are_similar(v1, v2)) - for t1, t2 in zip(exp.body, ext.body): - are_equal(self, t1, t2) - - def test_predict(self): - predictions = self.extractor.predict(self.test_set.iloc[:, :-1]) - idx = [pred is not None for pred in predictions] - predictions = predictions[idx] - - # Handle both classification and regression. - if not isinstance(predictions[0], str): - predictions = np.array([round(x, get_int_precision()) for x in predictions]) - - solver = prolog_solver(static_kb=self.extracted_theory.assertZ(get_in_rule())) - substitutions = [solver.solveOnce(data_to_struct(data)) for _, data in self.test_set.iterrows()] - index = self.test_set.shape[1] - 1 - expected = np.array( - [query.solved_query.get_arg_at(index) if query.is_yes else '-1' for query in substitutions] - )[idx] - expected = [str(x) for x in expected] if isinstance(predictions[0], str) else \ - [float(x) if isinstance(x, str) else float(x.value) for x in expected] - - ''' - GridEx is not exhaustive so all entry's predictions that are not inside an hypercube are nan. - In python nan == nan is always False so for this test we do not consider them. - ''' - if isinstance(predictions[0], str): - self.assertTrue(all([pred == exp if exp != "-1" else True for (pred, exp) in zip(predictions, expected)])) - else: - self.assertTrue(max(abs(predictions - expected)) < get_default_precision()) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/psyke/regression/iter/test_iter.py b/test/psyke/regression/iter/test_iter.py deleted file mode 100644 index 58524430..00000000 --- a/test/psyke/regression/iter/test_iter.py +++ /dev/null @@ -1,46 +0,0 @@ -from tuprolog.core import Var, Real -from psyke import logger -from parameterized import parameterized_class -from psyke.utils import get_default_precision -from test import get_in_rule -from test.psyke import initialize, data_to_struct, are_similar, are_equal -from tuprolog.solve.prolog import prolog_solver -import numpy as np -import unittest - - -@parameterized_class(initialize('iter')) -class TestIter(unittest.TestCase): - - def test_extract(self): - logger.info(self.expected_theory) - logger.info(self.extracted_theory) - - for exp, ext in zip(self.expected_theory, self.extracted_theory): - for v1, v2 in zip(exp.head.args, ext.head.args): - if isinstance(v1, Var): - self.assertTrue(isinstance(v2, Var)) - self.assertTrue(v1.equals(v2, False)) - elif isinstance(v1, Real): - self.assertTrue(isinstance(v2, Real)) - self.assertTrue(are_similar(v1, v2)) - for t1, t2 in zip(exp.body, ext.body): - are_equal(self, t1, t2) - - def test_predict(self): - predictions = np.array(self.extractor.predict(self.test_set.iloc[:, :-1])) - solver = prolog_solver(static_kb=self.extracted_theory.assertZ(get_in_rule())) - substitutions = [solver.solveOnce(data_to_struct(data)) for _, data in self.test_set.iterrows()] - index = self.test_set.shape[1] - 1 - expected = np.array([float(query.solved_query.get_arg_at(index).value) - if query.is_yes else 0 for query in substitutions]) - ''' - ITER is not exhaustive so all entry's predictions that are not inside an hypercube are nan. - In python nan == nan is always False so for this test we do not consider them. - ''' - idx = [pred is not None for pred in predictions] - self.assertTrue(max(abs(predictions[idx] - expected[idx])) < get_default_precision()) - - -if __name__ == '__main__': - unittest.main()