From 00453f628b47f7ec02e9e944b081882cae459f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Mon, 5 Dec 2022 10:55:15 +0100 Subject: [PATCH] Add tests to 100% coverage to Python --- Makefile | 3 ++- debug.cpp | 45 ++++++++++++++++++++++++++++++++----- fimdlp/mdlp.py | 37 ------------------------------ fimdlp/tests/FImdlp_test.py | 14 ++++++++++++ 4 files changed, 56 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index d2f1a9a..8301143 100644 --- a/Makefile +++ b/Makefile @@ -10,13 +10,14 @@ clean: ## Clean up if [ -d fimdlp/testcpp/lcoverage ]; then rm -fr fimdlp/testcpp/lcoverage/* ; fi; test: - python -m unittest -v fimdlp.tests + coverage run -m unittest -v fimdlp.tests cd fimdlp/testcpp && ./test coverage: if [ -d fimdlp/testcpp/build/CMakeFiles ]; then rm -fr fimdlp/testcpp/build/CMakeFiles/* ; fi; make test cd fimdlp/testcpp && ./cover + coverage report -m lint: ## Lint and static-check black fimdlp diff --git a/debug.cpp b/debug.cpp index 042092e..3d38136 100644 --- a/debug.cpp +++ b/debug.cpp @@ -1,14 +1,49 @@ std::cout << "+++++++++++++++++++++++" << std::endl; -for (size_t i = 0; i < y.size(); i++) -{ +for (size_t i = 0; i < y.size(); i++) { printf("(%3.1f, %d)\n", X[indices.at(i)], y[indices.at(i)]); } std::cout << "+++++++++++++++++++++++" << std::endl; std::cout << "Information Gain:" << std::endl; auto nc = Metrics::numClasses(y, indices, 0, indices.size()); -for (auto cutPoint = cutIdx.begin(); cutPoint != cutIdx.end(); ++cutPoint) -{ +for (auto cutPoint = cutIdx.begin(); cutPoint != cutIdx.end(); ++cutPoint) { std::cout << *cutPoint << " -> " << Metrics::informationGain(y, indices, 0, indices.size(), *cutPoint, nc) << std::endl; // << Metrics::informationGain(y, 0, y.size(), *cutPoint, Metrics::numClasses(y, 0, y.size())) << std::endl; -} \ No newline at end of file +} + +def test(self) : + print("Calculating cut points in python for first feature") + yz = self.y_.copy() + xz = X[:, 0].copy() + xz = xz[np.argsort(X[:, 0])] + yz = yz[np.argsort(X[:, 0])] + cuts = [] + for i in range(1, len(yz)) : + if yz[i] != yz[i - 1] and xz[i - 1] < xz[i] : + print(f"Cut point: ({xz[i-1]}, {xz[i]}) ({yz[i-1]}, {yz[i]})") + cuts.append((xz[i] + xz[i - 1]) / 2) + print("Cuts calculados en python: ", cuts) + print("-- Cuts calculados en C++ --") + print("Cut points for each feature in Iris dataset:") + for i in range(0, 1) : + # datax = self.X_[np.argsort(self.X_[:, i]), i] + # y_ = self.y_[np.argsort(self.X_[:, i])] + datax = self.X_[:, i] + y_ = self.y_ + self.discretizer_.fit(datax, y_) + Xcutpoints = self.discretizer_.get_cut_points() + print( + f"New ({len(Xcutpoints)}):{self.features_[i]:20s}: " + f"{[i['toValue'] for i in Xcutpoints]}" + ) + X_translated = [ + f"{i['classNumber']} - ({i['start']}, {i['end']}) - " + f"({i['fromValue']}, {i['toValue']})" + for i in Xcutpoints + ] + print(X_translated) + print("*******************************") + print("Disretized values:") + print(self.discretizer_.get_discretized_values()) + print("*******************************") + return X \ No newline at end of file diff --git a/fimdlp/mdlp.py b/fimdlp/mdlp.py index d6d50d2..ed18232 100644 --- a/fimdlp/mdlp.py +++ b/fimdlp/mdlp.py @@ -110,40 +110,3 @@ class FImdlp(TransformerMixin, BaseEstimator): for feature in range(self.n_features_): result.append(self.cut_points_[feature][:-1]) return result - - def test(self): - print("Calculating cut points in python for first feature") - yz = self.y_.copy() - xz = X[:, 0].copy() - xz = xz[np.argsort(X[:, 0])] - yz = yz[np.argsort(X[:, 0])] - cuts = [] - for i in range(1, len(yz)): - if yz[i] != yz[i - 1] and xz[i - 1] < xz[i]: - print(f"Cut point: ({xz[i-1]}, {xz[i]}) ({yz[i-1]}, {yz[i]})") - cuts.append((xz[i] + xz[i - 1]) / 2) - print("Cuts calculados en python: ", cuts) - print("-- Cuts calculados en C++ --") - print("Cut points for each feature in Iris dataset:") - for i in range(0, 1): - # datax = self.X_[np.argsort(self.X_[:, i]), i] - # y_ = self.y_[np.argsort(self.X_[:, i])] - datax = self.X_[:, i] - y_ = self.y_ - self.discretizer_.fit(datax, y_) - Xcutpoints = self.discretizer_.get_cut_points() - print( - f"New ({len(Xcutpoints)}):{self.features_[i]:20s}: " - f"{[i['toValue'] for i in Xcutpoints]}" - ) - X_translated = [ - f"{i['classNumber']} - ({i['start']}, {i['end']}) - " - f"({i['fromValue']}, {i['toValue']})" - for i in Xcutpoints - ] - print(X_translated) - print("*******************************") - print("Disretized values:") - print(self.discretizer_.get_discretized_values()) - print("*******************************") - return X diff --git a/fimdlp/tests/FImdlp_test.py b/fimdlp/tests/FImdlp_test.py index 045a159..08aff11 100644 --- a/fimdlp/tests/FImdlp_test.py +++ b/fimdlp/tests/FImdlp_test.py @@ -31,8 +31,22 @@ class FImdlpTest(unittest.TestCase): [0.75, 1.399999976158142, 1.5], ] self.assertListEqual(expected, clf.get_cut_points()) + self.assertListEqual( + ["feature_0", "feature_1", "feature_2", "feature_3"], clf.features_ + ) + self.assertEqual("class", clf.class_name_) + clf.fit(X, y, features=["a", "b", "c", "d"], class_name="class_name") + self.assertListEqual(["a", "b", "c", "d"], clf.features_) + self.assertEqual("class_name", clf.class_name_) + + def test_fit_Errors(self): + clf = FImdlp() with self.assertRaises(ValueError): clf.fit([[1, 2], [3, 4]], [1, 2, 3]) + with self.assertRaises(ValueError): + clf.fit([[1, 2], [3, 4]], [1, 2], features=["a", "b", "c"]) + with self.assertRaises(ValueError): + clf.fit([[1, 2], [3, 4]], [1, 2], unexpected="class_name") def test_transform(self): clf = FImdlp()