Add tests to 100% coverage to Python

This commit is contained in:
2022-12-05 10:55:15 +01:00
parent 54edd95e5b
commit 00453f628b
4 changed files with 56 additions and 43 deletions

View File

@@ -10,13 +10,14 @@ 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 coverage run -m unittest -v fimdlp.tests
cd fimdlp/testcpp && ./test cd fimdlp/testcpp && ./test
coverage: coverage:
if [ -d fimdlp/testcpp/build/CMakeFiles ]; then rm -fr fimdlp/testcpp/build/CMakeFiles/* ; fi; if [ -d fimdlp/testcpp/build/CMakeFiles ]; then rm -fr fimdlp/testcpp/build/CMakeFiles/* ; fi;
make test make test
cd fimdlp/testcpp && ./cover cd fimdlp/testcpp && ./cover
coverage report -m
lint: ## Lint and static-check lint: ## Lint and static-check
black fimdlp black fimdlp

View File

@@ -1,14 +1,49 @@
std::cout << "+++++++++++++++++++++++" << std::endl; 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)]); printf("(%3.1f, %d)\n", X[indices.at(i)], y[indices.at(i)]);
} }
std::cout << "+++++++++++++++++++++++" << std::endl; std::cout << "+++++++++++++++++++++++" << std::endl;
std::cout << "Information Gain:" << std::endl; std::cout << "Information Gain:" << std::endl;
auto nc = Metrics::numClasses(y, indices, 0, indices.size()); 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; 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; // << Metrics::informationGain(y, 0, y.size(), *cutPoint, Metrics::numClasses(y, 0, y.size())) << std::endl;
} }
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

View File

@@ -110,40 +110,3 @@ class FImdlp(TransformerMixin, BaseEstimator):
for feature in range(self.n_features_): for feature in range(self.n_features_):
result.append(self.cut_points_[feature][:-1]) result.append(self.cut_points_[feature][:-1])
return result 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

View File

@@ -31,8 +31,22 @@ class FImdlpTest(unittest.TestCase):
[0.75, 1.399999976158142, 1.5], [0.75, 1.399999976158142, 1.5],
] ]
self.assertListEqual(expected, clf.get_cut_points()) 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): with self.assertRaises(ValueError):
clf.fit([[1, 2], [3, 4]], [1, 2, 3]) 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): def test_transform(self):
clf = FImdlp() clf = FImdlp()