From 43c0250187b7b6f0e69c4872e8f2b526e31264a0 Mon Sep 17 00:00:00 2001
From: Jakhes <dean.schmitz@schmitzbauer.de>
Date: Thu, 17 Nov 2022 02:11:46 +0100
Subject: [PATCH] Finishing hoeffding_tree tests.

---
 src/methods/hoeffding_tree/hoeffding_tree.cpp | 12 ++++++
 .../hoeffding_tree/hoeffding_tree_test.pl     | 37 ++++++++++---------
 2 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/src/methods/hoeffding_tree/hoeffding_tree.cpp b/src/methods/hoeffding_tree/hoeffding_tree.cpp
index cbe023a..d9ca00e 100644
--- a/src/methods/hoeffding_tree/hoeffding_tree.cpp
+++ b/src/methods/hoeffding_tree/hoeffding_tree.cpp
@@ -45,6 +45,12 @@ void initAndBuildModel(char const *treeType,
 {
     // convert the Prolog arrays to arma::mat
     mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum);
+    // check if labels fit the data
+    if (data.n_cols != labelsArrSize)
+    {
+        raisePrologSystemExeption("The number of data points does not match the number of labels!");
+        return;
+    }
 
     // convert the Prolog arrays to arma::rowvec
     Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize);
@@ -144,6 +150,12 @@ void train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum,
 
     // convert the Prolog arrays to arma::mat
     mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum);
+    // check if labels fit the data
+    if (data.n_cols != labelsArrSize)
+    {
+        raisePrologSystemExeption("The number of data points does not match the number of labels!");
+        return;
+    }
     
     // convert the Prolog arrays to arma::rowvec
     Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize);
diff --git a/src/methods/hoeffding_tree/hoeffding_tree_test.pl b/src/methods/hoeffding_tree/hoeffding_tree_test.pl
index e03e532..280b7cb 100644
--- a/src/methods/hoeffding_tree/hoeffding_tree_test.pl
+++ b/src/methods/hoeffding_tree/hoeffding_tree_test.pl
@@ -47,18 +47,18 @@ test(hoeffding_Init_Negative_ObservationsBeforeBinning, fail) :-
         hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,1,0,1], 2, 0, 0.95, 5000, 100, 100, 10, -100).
 
 
-test(hoeffding_Init_With_Wrong_Label_Dims1, [error(_,system_error('Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!'))]) :-
+test(hoeffding_Init_With_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :-
         hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,1], 2, 0, 0.95, 5000, 100, 100, 10, 100).
 
-%% If the label vector is to long it seems to cause no problems
-test(hoeffding_Init_With_Wrong_Label_Dims2) :-
+test(hoeffding_Init_With_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :-
         hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,1,0,1,0,0,1], 2, 0, 0.95, 5000, 100, 100, 10, 100).
 
-%% The same when the label values are out of range
+%% doesnt cause an error
 test(hoeffding_Init_With_Wrong_Label_Value) :-
         hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,-1,0,-1], 2, 0, 0.95, 5000, 100, 100, 10, 100).
 
-test(hoeffding_Init_With_Too_Many_Label_Value, [error(_,system_error('The values of the Label have to start at 0 and be >= 0 and < the given numClass!'))]) :-
+%% doesnt cause an error
+test(hoeffding_Init_With_Too_Many_Label_Value) :-
         hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [1,1,0,2], 2, 0, 0.95, 5000, 100, 100, 10, 100).
         
 
@@ -105,10 +105,11 @@ test(hoeffding_Init_InfoBinary_CSV_Input) :-
 :- begin_tests(hoeffding_tree_classify).      
 
 %% Failure Tests
-                                            
-test(hoeffding_Classify_Different_Dims_To_Train, [error(_,system_error('Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!'))]) :-
+
+%% seems to be able to classify diffrent Dimensions than trained
+test(hoeffding_Classify_Different_Dims_To_Train) :-
         reset_Model,
-        hoeffding_tree_classify([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, PredicList, ProbsList),
+        hoeffding_tree_classify([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 2, PredicList, ProbsList),
         print('\nPredictions: '),
         print(PredicList),
         print('\nProbabilities: '),
@@ -167,23 +168,23 @@ test(hoeffding_Classify_InfoBinary) :-
 
 %% Failure Tests
                                             
-test(hoeffding_Train_With_Wrong_Label_Dims1, [error(_,system_error('Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!'))]) :-
+test(hoeffding_Train_With_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :-
         reset_Model,
-        hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,1,0,0,0], 0).
+        hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,1,0,0], 0).
 
-%% If the label vector is to long it seems to cause no problems
-test(hoeffding_Train_With_Wrong_Label_Dims2) :-
+test(hoeffding_Train_With_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :-
         reset_Model,
         hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,1,0,1,0,0,1], 0).
 
-%% The same when the label values are out of range
+
+%% all 3 test dont cause an error
 test(hoeffding_Train_With_Wrong_Label_Value) :-
         reset_Model,
-        hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,-1,0,-1], 0).
+        hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,-1,0,-1,0], 0).
 
-test(hoeffding_Train_With_Too_Many_Label_Value, [error(_,system_error('The values of the Label have to start at 0 and be >= 0 and < the given numClass!'))]) :-
+test(hoeffding_Train_With_Too_Many_Label_Value) :-
         reset_Model,
-        hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [1,1,0,2], 0).
+        hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [1,1,0,2,3], 0).
         
 test(hoeffding_Train_Bad_Data_Dims) :-
         reset_Model,
@@ -192,7 +193,7 @@ test(hoeffding_Train_Bad_Data_Dims) :-
 
 %% Successful Tests
 
-test(testDescription3) :-
+test(hoeffding_Train_Normal_Use) :-
         reset_Model,
         hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,0,1,1,0], 1),
         hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,0,1,1,0], 0).
@@ -200,5 +201,5 @@ test(testDescription3) :-
 :- end_tests(hoeffding_tree_train).
 
 run_hoeffding_tree_tests :-
-        run_tests(hoeffding_tree_train).
+        run_tests.
 
-- 
GitLab