Añade AdaBoost and tests

This commit is contained in:
2025-06-15 12:03:32 +02:00
parent 1678a17fc4
commit 2a99dce23b
9 changed files with 62 additions and 20 deletions

View File

@@ -68,8 +68,8 @@ endif (ENABLE_CLANG_TIDY)
# --------------------------------------------------
# find_library(BayesNet NAMES libBayesNet BayesNet libBayesNet.a PATHS ${PyClassifiers_SOURCE_DIR}/../lib/lib REQUIRED)
# find_path(Bayesnet_INCLUDE_DIRS REQUIRED NAMES bayesnet PATHS ${PyClassifiers_SOURCE_DIR}/../lib/include)
find_library(bayesnet NAMES libbayesnet bayesnet libbayesnet.a PATHS ${Platform_SOURCE_DIR}/../lib/lib REQUIRED)
find_path(Bayesnet_INCLUDE_DIRS REQUIRED NAMES bayesnet PATHS ${Platform_SOURCE_DIR}/../lib/include)
find_library(bayesnet NAMES libbayesnet bayesnet libbayesnet.a PATHS ../lib REQUIRED)
find_path(Bayesnet_INCLUDE_DIRS REQUIRED NAMES bayesnet PATHS ../lib/include)
message(STATUS "BayesNet=${bayesnet}")
message(STATUS "Bayesnet_INCLUDE_DIRS=${Bayesnet_INCLUDE_DIRS}")
@@ -82,9 +82,8 @@ add_subdirectory(pyclfs)
# -------
if (ENABLE_TESTING)
MESSAGE("Testing enabled")
add_git_submodule(lib/catch2)
add_git_submodule(lib/mdlp)
add_subdirectory(lib/Files)
find_package(Catch2 CONFIG REQUIRED)
find_package(arff-files CONFIG REQUIRED)
include(CTest)
add_subdirectory(tests)
endif (ENABLE_TESTING)
@@ -96,4 +95,4 @@ install(TARGETS PyClassifiers
LIBRARY DESTINATION lib
CONFIGURATIONS Release)
install(DIRECTORY pyclfs/ DESTINATION include/pyclassifiers FILES_MATCHING CONFIGURATIONS Release PATTERN "*.h" PATTERN "*.hpp")
install(FILES ${Bayesnet_INCLUDE_DIRS}/bayesnet/config.h DESTINATION include/pyclassifiers CONFIGURATIONS Release)
install(FILES ${Bayesnet_INCLUDE_DIRS}/bayesnet/config.h DESTINATION include/pyclassifiers CONFIGURATIONS Release)

View File

@@ -52,7 +52,7 @@ debug: ## Build a debug version of the project
@echo ">>> Building Debug PyClassifiers...";
@if [ -d ./$(f_debug) ]; then rm -rf ./$(f_debug); fi
@mkdir $(f_debug);
@cmake -S . -B $(f_debug) -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON
@cmake -S . -B $(f_debug) -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
@echo ">>> Done";
release: ## Build a Release version of the project

20
pyclfs/AdaBoost.cc Normal file
View File

@@ -0,0 +1,20 @@
#include "AdaBoost.h"
namespace pywrap {
AdaBoost::AdaBoost() : PyClassifier("sklearn.ensemble", "AdaBoostClassifier", true)
{
validHyperparameters = { "n_estimators", "n_jobs", "random_state" };
}
int AdaBoost::getNumberOfEdges() const
{
return callMethodSumOfItems("get_n_leaves");
}
int AdaBoost::getNumberOfStates() const
{
return callMethodSumOfItems("get_depth");
}
int AdaBoost::getNumberOfNodes() const
{
return callMethodSumOfItems("node_count");
}
} /* namespace pywrap */

15
pyclfs/AdaBoost.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef ADABOOST_H
#define ADABOOST_H
#include "PyClassifier.h"
namespace pywrap {
class AdaBoost : public PyClassifier {
public:
AdaBoost();
~AdaBoost() = default;
int getNumberOfEdges() const override;
int getNumberOfStates() const override;
int getNumberOfNodes() const override;
};
} /* namespace pywrap */
#endif /* ADABOOST_H */

View File

@@ -4,5 +4,5 @@ include_directories(
${PyClassifiers_SOURCE_DIR}/lib/json/include
${Bayesnet_INCLUDE_DIRS}
)
add_library(PyClassifiers ODTE.cc STree.cc SVC.cc RandomForest.cc XGBoost.cc PyClassifier.cc PyWrap.cc)
add_library(PyClassifiers ODTE.cc STree.cc SVC.cc RandomForest.cc XGBoost.cc AdaBoost.cc PyClassifier.cc PyWrap.cc)
target_link_libraries(PyClassifiers nlohmann_json::nlohmann_json ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::boost Boost::python Boost::numpy)

View File

@@ -2,9 +2,6 @@ if(ENABLE_TESTING)
set(TEST_PYCLASSIFIERS "unit_tests_pyclassifiers")
include_directories(
${PyClassifiers_SOURCE_DIR}
${PyClassifiers_SOURCE_DIR}/lib/Files
${PyClassifiers_SOURCE_DIR}/lib/mdlp/src
${PyClassifiers_SOURCE_DIR}/lib/json/include
${Python3_INCLUDE_DIRS}
${TORCH_INCLUDE_DIRS}
${CMAKE_BINARY_DIR}/configured_files/include
@@ -13,5 +10,5 @@ if(ENABLE_TESTING)
file(GLOB_RECURSE PyClassifiers_SOURCES "${PyClassifiers_SOURCE_DIR}/pyclfs/*.cc")
set(TEST_SOURCES_PYCLASSIFIERS TestPythonClassifiers.cc TestUtils.cc ${PyClassifiers_SOURCES})
add_executable(${TEST_PYCLASSIFIERS} ${TEST_SOURCES_PYCLASSIFIERS})
target_link_libraries(${TEST_PYCLASSIFIERS} PUBLIC "${TORCH_LIBRARIES}" ${Python3_LIBRARIES} ${LIBTORCH_PYTHON} Boost::boost Boost::python Boost::numpy ArffFiles fimdlp Catch2::Catch2WithMain)
target_link_libraries(${TEST_PYCLASSIFIERS} PUBLIC "${TORCH_LIBRARIES}" ${Python3_LIBRARIES} ${LIBTORCH_PYTHON} Boost::boost Boost::python Boost::numpy fimdlp Catch2::Catch2WithMain)
endif(ENABLE_TESTING)

View File

@@ -10,14 +10,16 @@
#include "pyclfs/SVC.h"
#include "pyclfs/RandomForest.h"
#include "pyclfs/XGBoost.h"
#include "pyclfs/AdaBoost.h"
#include "pyclfs/ODTE.h"
#include "TestUtils.h"
#include <iostream>
TEST_CASE("Test Python Classifiers score", "[PyClassifiers]")
{
map <pair<std::string, std::string>, float> scores = {
// Diabetes
{{"diabetes", "STree"}, 0.81641}, {{"diabetes", "ODTE"}, 0.854166687}, {{"diabetes", "SVC"}, 0.76823}, {{"diabetes", "RandomForest"}, 1.0},
{{"diabetes", "STree"}, 0.81641}, {{"diabetes", "ODTE"}, 0.856770813f}, {{"diabetes", "SVC"}, 0.76823}, {{"diabetes", "RandomForest"}, 1.0},
// Ecoli
{{"ecoli", "STree"}, 0.8125}, {{"ecoli", "ODTE"}, 0.875}, {{"ecoli", "SVC"}, 0.89583}, {{"ecoli", "RandomForest"}, 1.0},
// Glass
@@ -35,8 +37,8 @@ TEST_CASE("Test Python Classifiers score", "[PyClassifiers]")
map<std::string, std::string> versions = {
{"ODTE", "1.0.0-1"},
{"STree", "1.4.0"},
{"SVC", "1.5.1"},
{"RandomForest", "1.5.1"}
{"SVC", "1.5.2"},
{"RandomForest", "1.5.2"}
};
auto clf = models[name];
@@ -58,6 +60,15 @@ TEST_CASE("Test Python Classifiers score", "[PyClassifiers]")
REQUIRE(clf->getVersion() == versions[name]);
}
}
TEST_CASE("AdaBoostClassifier", "[PyClassifiers]")
{
auto raw = RawDatasets("iris", false);
auto clf = pywrap::AdaBoost();
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
clf.setHyperparameters(nlohmann::json::parse("{ \"n_estimators\": 100 }"));
auto score = clf.score(raw.Xt, raw.yt);
REQUIRE(score == Catch::Approx(0.9599999f).epsilon(raw.epsilon));
}
TEST_CASE("Classifiers features", "[PyClassifiers]")
{
auto raw = RawDatasets("iris", false);

View File

@@ -5,8 +5,8 @@
#include <vector>
#include <map>
#include <tuple>
#include "ArffFiles.h"
#include "CPPFImdlp.h"
#include "ArffFiles/ArffFiles.hpp"
#include "fimdlp/CPPFImdlp.h"
bool file_exists(const std::string& name);
std::pair<vector<mdlp::labels_t>, map<std::string, int>> discretize(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y, std::vector<string> features);

View File

@@ -8,7 +8,7 @@
"libtorch-bin",
"folding",
"argparse",
"libxlsxwriter"
"catch2"
],
"overrides": [
{
@@ -36,8 +36,8 @@
"version": "3.2"
},
{
"name": "libxlsxwriter",
"version": "1.2.2"
"name": "catch2",
"version": "3.8.1"
},
{
"name": "nlohmann-json",