diff --git a/Makefile b/Makefile index 47d0419..d2f1a9a 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ clean: ## Clean up if [ -d fimdlp/testcpp/lcoverage ]; then rm -fr fimdlp/testcpp/lcoverage/* ; fi; test: + python -m unittest -v fimdlp.tests cd fimdlp/testcpp && ./test coverage: diff --git a/fimdlp/mdlp.py b/fimdlp/mdlp.py index a0603b1..d6d50d2 100644 --- a/fimdlp/mdlp.py +++ b/fimdlp/mdlp.py @@ -90,10 +90,6 @@ class FImdlp(TransformerMixin, BaseEstimator): # Input validation X = check_array(X) - if (X != self.X_).any(): - raise ValueError( - "X values are not the same as the ones used to fit the model." - ) # Check that the input is of the same shape as the one passed # during fit. @@ -109,6 +105,12 @@ class FImdlp(TransformerMixin, BaseEstimator): ) return result + def get_cut_points(self): + result = [] + 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() diff --git a/fimdlp/tests/FImdlp_test.py b/fimdlp/tests/FImdlp_test.py new file mode 100644 index 0000000..045a159 --- /dev/null +++ b/fimdlp/tests/FImdlp_test.py @@ -0,0 +1,65 @@ +import unittest +import sklearn +from sklearn.datasets import load_iris +import numpy as np +from ..mdlp import FImdlp + + +class FImdlpTest(unittest.TestCase): + def test_init(self): + clf = FImdlp() + self.assertTrue(clf.proposal) + clf = FImdlp(proposal=False) + self.assertFalse(clf.proposal) + + def test_fit(self): + clf = FImdlp() + clf.fit([[1, 2], [3, 4]], [1, 2]) + self.assertEqual(clf.n_features_, 2) + self.assertListEqual(clf.X_.tolist(), [[1, 2], [3, 4]]) + self.assertListEqual(clf.y_.tolist(), [1, 2]) + self.assertListEqual([[], []], clf.get_cut_points()) + X, y = load_iris(return_X_y=True) + clf.fit(X, y) + self.assertEqual(clf.n_features_, 4) + self.assertTrue(np.array_equal(X, clf.X_)) + self.assertTrue(np.array_equal(y, clf.y_)) + expected = [ + [4.900000095367432, 5.0, 5.099999904632568, 5.400000095367432], + [2.6999998092651367, 2.9000000953674316], + [2.3499999046325684, 4.5], + [0.75, 1.399999976158142, 1.5], + ] + self.assertListEqual(expected, clf.get_cut_points()) + with self.assertRaises(ValueError): + clf.fit([[1, 2], [3, 4]], [1, 2, 3]) + + def test_transform(self): + clf = FImdlp() + clf.fit([[1, 2], [3, 4]], [1, 2]) + self.assertEqual( + clf.transform([[1, 2], [3, 4]]).tolist(), [[0, 0], [0, 0]] + ) + X, y = load_iris(return_X_y=True) + clf.fit(X, y) + self.assertEqual(clf.n_features_, 4) + self.assertTrue(np.array_equal(X, clf.X_)) + self.assertTrue(np.array_equal(y, clf.y_)) + self.assertListEqual( + clf.transform(X).tolist(), clf.fit(X, y).transform(X).tolist() + ) + expected = [ + [4, 0, 1, 1], + [4, 2, 2, 2], + [4, 0, 1, 1], + [1, 0, 1, 1], + [4, 1, 1, 1], + [4, 2, 1, 1], + [4, 1, 1, 1], + ] + self.assertTrue(np.array_equal(clf.transform(X[90:97]), expected)) + with self.assertRaises(ValueError): + clf.transform([[1, 2, 3], [4, 5, 6]]) + with self.assertRaises(sklearn.exceptions.NotFittedError): + clf = FImdlp() + clf.transform([[1, 2], [3, 4]]) diff --git a/fimdlp/tests/__init__.py b/fimdlp/tests/__init__.py new file mode 100644 index 0000000..cac328b --- /dev/null +++ b/fimdlp/tests/__init__.py @@ -0,0 +1,3 @@ +from .FImdlp_test import FImdlpTest + +all = ["FImdlpTest"]