Add Python tests to module

This commit is contained in:
2022-12-05 10:44:36 +01:00
parent cabfdd57ea
commit 54edd95e5b
4 changed files with 75 additions and 4 deletions

View File

@@ -10,6 +10,7 @@ clean: ## Clean up
if [ -d fimdlp/testcpp/lcoverage ]; then rm -fr fimdlp/testcpp/lcoverage/* ; fi; if [ -d fimdlp/testcpp/lcoverage ]; then rm -fr fimdlp/testcpp/lcoverage/* ; fi;
test: test:
python -m unittest -v fimdlp.tests
cd fimdlp/testcpp && ./test cd fimdlp/testcpp && ./test
coverage: coverage:

View File

@@ -90,10 +90,6 @@ class FImdlp(TransformerMixin, BaseEstimator):
# Input validation # Input validation
X = check_array(X) 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 # Check that the input is of the same shape as the one passed
# during fit. # during fit.
@@ -109,6 +105,12 @@ class FImdlp(TransformerMixin, BaseEstimator):
) )
return result 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): def test(self):
print("Calculating cut points in python for first feature") print("Calculating cut points in python for first feature")
yz = self.y_.copy() yz = self.y_.copy()

View File

@@ -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]])

3
fimdlp/tests/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
from .FImdlp_test import FImdlpTest
all = ["FImdlpTest"]