Merge pull request 'Integrate libraries with vcpkg' (#6) from vcpkg into main
Reviewed-on: #6
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -42,3 +42,4 @@ puml/**
|
||||
diagrams/html/**
|
||||
diagrams/latex/**
|
||||
.cache
|
||||
vcpkg_installed
|
||||
|
21
.gitmodules
vendored
21
.gitmodules
vendored
@@ -1,21 +0,0 @@
|
||||
[submodule "lib/catch2"]
|
||||
path = lib/catch2
|
||||
url = https://github.com/catchorg/Catch2.git
|
||||
[submodule "lib/argparse"]
|
||||
path = lib/argparse
|
||||
url = https://github.com/p-ranav/argparse
|
||||
[submodule "lib/json"]
|
||||
path = lib/json
|
||||
url = https://github.com/nlohmann/json
|
||||
[submodule "lib/libxlsxwriter"]
|
||||
path = lib/libxlsxwriter
|
||||
url = https://github.com/jmcnamara/libxlsxwriter.git
|
||||
[submodule "lib/folding"]
|
||||
path = lib/folding
|
||||
url = https://github.com/rmontanana/folding
|
||||
[submodule "lib/Files"]
|
||||
path = lib/Files
|
||||
url = https://github.com/rmontanana/ArffFiles
|
||||
[submodule "lib/mdlp"]
|
||||
path = lib/mdlp
|
||||
url = https://github.com/rmontanana/mdlp
|
@@ -7,12 +7,6 @@ project(Platform
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
find_package(Torch REQUIRED)
|
||||
|
||||
if (POLICY CMP0135)
|
||||
cmake_policy(SET CMP0135 NEW)
|
||||
endif ()
|
||||
|
||||
# Global CMake variables
|
||||
# ----------------------
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
@@ -26,62 +20,77 @@ set(CMAKE_CXX_FLAGS_DEBUG " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -O
|
||||
|
||||
# Options
|
||||
# -------
|
||||
option(ENABLE_CLANG_TIDY "Enable to add clang tidy." OFF)
|
||||
option(ENABLE_TESTING "Unit testing build" OFF)
|
||||
option(CODE_COVERAGE "Collect coverage from test library" OFF)
|
||||
|
||||
# CMakes modules
|
||||
# --------------
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
|
||||
|
||||
# MPI
|
||||
find_package(MPI REQUIRED)
|
||||
message("MPI_CXX_LIBRARIES=${MPI_CXX_LIBRARIES}")
|
||||
message("MPI_CXX_INCLUDE_DIRS=${MPI_CXX_INCLUDE_DIRS}")
|
||||
|
||||
# Boost Library
|
||||
cmake_policy(SET CMP0135 NEW)
|
||||
cmake_policy(SET CMP0167 NEW) # For FindBoost
|
||||
set(Boost_USE_STATIC_LIBS OFF)
|
||||
set(Boost_USE_MULTITHREADED ON)
|
||||
set(Boost_USE_STATIC_RUNTIME OFF)
|
||||
|
||||
find_package(Boost 1.66.0 REQUIRED COMPONENTS python3 numpy3)
|
||||
|
||||
# # Python
|
||||
find_package(Python3 REQUIRED COMPONENTS Development)
|
||||
# # target_include_directories(MyTarget SYSTEM PRIVATE ${Python3_INCLUDE_DIRS})
|
||||
# message("Python_LIBRARIES=${Python_LIBRARIES}")
|
||||
|
||||
# # Boost Python
|
||||
# find_package(boost_python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR} CONFIG REQUIRED COMPONENTS python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR})
|
||||
# # target_link_libraries(MyTarget PRIVATE Boost::python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR})
|
||||
|
||||
|
||||
if(Boost_FOUND)
|
||||
message("Boost_INCLUDE_DIRS=${Boost_INCLUDE_DIRS}")
|
||||
message("Boost_LIBRARIES=${Boost_LIBRARIES}")
|
||||
message("Boost_VERSION=${Boost_VERSION}")
|
||||
include_directories(${Boost_INCLUDE_DIRS})
|
||||
endif()
|
||||
|
||||
# Python
|
||||
find_package(Python3 3.11 COMPONENTS Interpreter Development REQUIRED)
|
||||
message("Python3_LIBRARIES=${Python3_LIBRARIES}")
|
||||
|
||||
# CMakes modules
|
||||
# --------------
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
|
||||
include(AddGitSubmodule)
|
||||
|
||||
if (CODE_COVERAGE)
|
||||
enable_testing()
|
||||
include(CodeCoverage)
|
||||
MESSAGE("Code coverage enabled")
|
||||
SET(GCC_COVERAGE_LINK_FLAGS " ${GCC_COVERAGE_LINK_FLAGS} -lgcov --coverage")
|
||||
endif (CODE_COVERAGE)
|
||||
|
||||
if (ENABLE_CLANG_TIDY)
|
||||
include(StaticAnalyzers) # clang-tidy
|
||||
endif (ENABLE_CLANG_TIDY)
|
||||
|
||||
# External libraries - dependencies of Platform
|
||||
# ---------------------------------------------
|
||||
add_git_submodule("lib/argparse")
|
||||
add_git_submodule("lib/mdlp")
|
||||
|
||||
find_library(XLSXWRITER_LIB NAMES libxlsxwriter.dylib libxlsxwriter.so PATHS ${Platform_SOURCE_DIR}/lib/libxlsxwriter/lib)
|
||||
message("XLSXWRITER_LIB=${XLSXWRITER_LIB}")
|
||||
# find_path(XLSXWRITER_INCLUDE_DIR xlsxwriter.h)
|
||||
# find_library(XLSXWRITER_LIBRARY xlsxwriter)
|
||||
# message("XLSXWRITER_INCLUDE_DIR=${XLSXWRITER_INCLUDE_DIR}")
|
||||
# message("XLSXWRITER_LIBRARY=${XLSXWRITER_LIBRARY}")
|
||||
find_package(Torch CONFIG REQUIRED)
|
||||
find_package(fimdlp CONFIG REQUIRED)
|
||||
find_package(folding CONFIG REQUIRED)
|
||||
find_package(argparse CONFIG REQUIRED)
|
||||
find_package(nlohmann_json CONFIG REQUIRED)
|
||||
find_package(Boost REQUIRED COMPONENTS python)
|
||||
find_package(arff-files CONFIG REQUIRED)
|
||||
|
||||
# BayesNet
|
||||
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)
|
||||
add_library(bayesnet::bayesnet UNKNOWN IMPORTED)
|
||||
set_target_properties(bayesnet::bayesnet PROPERTIES
|
||||
IMPORTED_LOCATION ${bayesnet}
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${Bayesnet_INCLUDE_DIRS})
|
||||
message(STATUS "BayesNet=${bayesnet}")
|
||||
message(STATUS "BayesNet_INCLUDE_DIRS=${Bayesnet_INCLUDE_DIRS}")
|
||||
|
||||
# PyClassifiers
|
||||
find_library(PyClassifiers NAMES libPyClassifiers PyClassifiers libPyClassifiers.a PATHS ${Platform_SOURCE_DIR}/../lib/lib REQUIRED)
|
||||
find_path(PyClassifiers_INCLUDE_DIRS REQUIRED NAMES pyclassifiers PATHS ${Platform_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)
|
||||
|
||||
message(STATUS "PyClassifiers=${PyClassifiers}")
|
||||
message(STATUS "PyClassifiers_INCLUDE_DIRS=${PyClassifiers_INCLUDE_DIRS}")
|
||||
message(STATUS "BayesNet=${BayesNet}")
|
||||
message(STATUS "Bayesnet_INCLUDE_DIRS=${Bayesnet_INCLUDE_DIRS}")
|
||||
|
||||
# Subdirectories
|
||||
# --------------
|
||||
@@ -90,16 +99,20 @@ cmake_path(SET TEST_DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests/data")
|
||||
configure_file(src/common/SourceData.h.in "${CMAKE_BINARY_DIR}/configured_files/include/SourceData.h")
|
||||
add_subdirectory(config)
|
||||
add_subdirectory(src)
|
||||
add_subdirectory(sample)
|
||||
# add_subdirectory(sample)
|
||||
file(GLOB Platform_SOURCES CONFIGURE_DEPENDS ${Platform_SOURCE_DIR}/src/*.cpp)
|
||||
|
||||
# Testing
|
||||
# -------
|
||||
if (ENABLE_TESTING)
|
||||
enable_testing()
|
||||
MESSAGE("Testing enabled")
|
||||
if (NOT TARGET Catch2::Catch2)
|
||||
add_git_submodule("lib/catch2")
|
||||
endif (NOT TARGET Catch2::Catch2)
|
||||
find_package(Catch2 CONFIG REQUIRED)
|
||||
include(CTest)
|
||||
add_subdirectory(tests)
|
||||
endif (ENABLE_TESTING)
|
||||
if (CODE_COVERAGE)
|
||||
include(CodeCoverage)
|
||||
MESSAGE("Code coverage enabled")
|
||||
SET(GCC_COVERAGE_LINK_FLAGS " ${GCC_COVERAGE_LINK_FLAGS} -lgcov --coverage")
|
||||
endif (CODE_COVERAGE)
|
||||
|
50
Makefile
50
Makefile
@@ -1,9 +1,9 @@
|
||||
SHELL := /bin/bash
|
||||
.DEFAULT_GOAL := help
|
||||
.PHONY: coverage setup help build test clean debug release submodules buildr buildd install dependency testp testb clang-uml
|
||||
.PHONY: init clean coverage setup help build test clean debug release buildr buildd install dependency testp testb clang-uml example
|
||||
|
||||
f_release = build_release
|
||||
f_debug = build_debug
|
||||
f_release = build_Release
|
||||
f_debug = build_Debug
|
||||
app_targets = b_best b_list b_main b_manage b_grid b_results
|
||||
test_targets = unit_tests_platform
|
||||
|
||||
@@ -20,14 +20,22 @@ define ClearTests
|
||||
fi ;
|
||||
endef
|
||||
|
||||
init: ## Initialize the project installing dependencies
|
||||
@echo ">>> Installing dependencies"
|
||||
@vcpkg install
|
||||
@echo ">>> Done";
|
||||
|
||||
sub-init: ## Initialize submodules
|
||||
@git submodule update --init --recursive
|
||||
|
||||
sub-update: ## Initialize submodules
|
||||
@git submodule update --remote --merge
|
||||
@git submodule foreach git pull origin master
|
||||
|
||||
clean: ## Clean the project
|
||||
@echo ">>> Cleaning the project..."
|
||||
@if test -f CMakeCache.txt ; then echo "- Deleting CMakeCache.txt"; rm -f CMakeCache.txt; fi
|
||||
@for folder in $(f_release) $(f_debug) vpcpkg_installed install_test ; do \
|
||||
if test -d "$$folder" ; then \
|
||||
echo "- Deleting $$folder folder" ; \
|
||||
rm -rf "$$folder"; \
|
||||
fi; \
|
||||
done
|
||||
$(call ClearTests)
|
||||
@echo ">>> Done";
|
||||
setup: ## Install dependencies for tests and coverage
|
||||
@if [ "$(shell uname)" = "Darwin" ]; then \
|
||||
brew install gcovr; \
|
||||
@@ -51,7 +59,9 @@ install: ## Copy binary files to bin folder
|
||||
@echo "*******************************************"
|
||||
@for item in $(app_targets); do \
|
||||
echo ">>> Copying $$item" ; \
|
||||
cp $(f_release)/src/$$item $(dest) ; \
|
||||
cp $(f_release)/src/$$item $(dest) || { \
|
||||
echo "*** Error copying $$item" ; \
|
||||
} ; \
|
||||
done
|
||||
|
||||
dependency: ## Create a dependency graph diagram of the project (build/dependency.png)
|
||||
@@ -60,37 +70,33 @@ dependency: ## Create a dependency graph diagram of the project (build/dependenc
|
||||
cd $(f_debug) && cmake .. --graphviz=dependency.dot && dot -Tpng dependency.dot -o dependency.png
|
||||
|
||||
buildd: ## Build the debug targets
|
||||
@cmake --build $(f_debug) -t $(app_targets) PlatformSample --parallel
|
||||
@cmake --build $(f_debug) -t $(app_targets) PlatformSample --parallel
|
||||
|
||||
buildr: ## Build the release targets
|
||||
@cmake --build $(f_release) -t $(app_targets) --parallel
|
||||
|
||||
clean: ## Clean the tests info
|
||||
@echo ">>> Cleaning Debug Platform tests...";
|
||||
$(call ClearTests)
|
||||
@echo ">>> Done";
|
||||
|
||||
clang-uml: ## Create uml class and sequence diagrams
|
||||
clang-uml -p --add-compile-flag -I /usr/lib/gcc/x86_64-redhat-linux/8/include/
|
||||
|
||||
debug: ## Build a debug version of the project
|
||||
debug: ## Build a debug version of the project with BayesNet from vcpkg
|
||||
@echo ">>> Building Debug Platform...";
|
||||
@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 -D CMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
|
||||
@echo ">>> Done";
|
||||
|
||||
release: ## Build a Release version of the project
|
||||
release: ## Build a Release version of the project with BayesNet from vcpkg
|
||||
@echo ">>> Building Release Platform...";
|
||||
@if [ -d ./$(f_release) ]; then rm -rf ./$(f_release); fi
|
||||
@mkdir $(f_release);
|
||||
@cmake -S . -B $(f_release) -D CMAKE_BUILD_TYPE=Release
|
||||
@echo ">>> Done";
|
||||
@cmake -S . -B $(f_release) -D CMAKE_BUILD_TYPE=Release -D CMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
|
||||
@echo ">>> Done";
|
||||
|
||||
opt = ""
|
||||
test: ## Run tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximum Spanning Tree'") to run only that section
|
||||
@echo ">>> Running Platform tests...";
|
||||
@$(MAKE) clean
|
||||
@$(MAKE) debug
|
||||
@cmake --build $(f_debug) -t $(test_targets) --parallel
|
||||
@for t in $(test_targets); do \
|
||||
if [ -f $(f_debug)/tests/$$t ]; then \
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||

|
||||
[](<https://opensource.org/licenses/MIT>)
|
||||
[](https://deepwiki.com/rmontanana/Platform)
|
||||

|
||||
|
||||
Platform to run Bayesian Networks and Machine Learning Classifiers experiments.
|
||||
|
23
gitmodules
23
gitmodules
@@ -1,23 +0,0 @@
|
||||
[submodule "lib/catch2"]
|
||||
path = lib/catch2
|
||||
main = v2.x
|
||||
update = merge
|
||||
url = https://github.com/catchorg/Catch2.git
|
||||
[submodule "lib/argparse"]
|
||||
path = lib/argparse
|
||||
url = https://github.com/p-ranav/argparse
|
||||
master = master
|
||||
update = merge
|
||||
[submodule "lib/json"]
|
||||
path = lib/json
|
||||
url = https://github.com/nlohmann/json.git
|
||||
master = master
|
||||
update = merge
|
||||
[submodule "lib/libxlsxwriter"]
|
||||
path = lib/libxlsxwriter
|
||||
url = https://github.com/jmcnamara/libxlsxwriter.git
|
||||
main = main
|
||||
update = merge
|
||||
[submodule "lib/folding"]
|
||||
path = lib/folding
|
||||
url = https://github.com/rmontanana/Folding
|
Submodule lib/Files deleted from 18c79f6d48
Submodule lib/argparse deleted from cbd9fd8ed6
Submodule lib/catch2 deleted from 914aeecfe2
Submodule lib/folding deleted from 9652853d69
1
lib/json
1
lib/json
Submodule lib/json deleted from 48e7b4c23b
Submodule lib/libxlsxwriter deleted from 14f13513cb
2009
lib/log/loguru.cpp
2009
lib/log/loguru.cpp
File diff suppressed because it is too large
Load Diff
1475
lib/log/loguru.hpp
1475
lib/log/loguru.hpp
File diff suppressed because it is too large
Load Diff
1
lib/mdlp
1
lib/mdlp
Submodule lib/mdlp deleted from cfb993f5ec
14
remove_submodules.sh
Normal file
14
remove_submodules.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | while read line; do
|
||||
echo "Removing $line"
|
||||
# Deinit the submodule
|
||||
git submodule deinit -f "$line"
|
||||
|
||||
# Remove the submodule from the working tree
|
||||
git rm -f "$line"
|
||||
|
||||
# Remove the submodule from .git/modules
|
||||
rm -rf ".git/modules/$line"
|
||||
done
|
||||
|
||||
# Remove the .gitmodules file
|
||||
git rm -f .gitmodules
|
@@ -1,15 +1,11 @@
|
||||
include_directories(
|
||||
${TORCH_INCLUDE_DIRS}
|
||||
${Platform_SOURCE_DIR}/src/common
|
||||
${Platform_SOURCE_DIR}/src/main
|
||||
${Python3_INCLUDE_DIRS}
|
||||
${Platform_SOURCE_DIR}/lib/Files
|
||||
${Platform_SOURCE_DIR}/lib/mdlp/src
|
||||
${Platform_SOURCE_DIR}/lib/argparse/include
|
||||
${Platform_SOURCE_DIR}/lib/folding
|
||||
${Platform_SOURCE_DIR}/lib/json/include
|
||||
${CMAKE_BINARY_DIR}/configured_files/include
|
||||
${PyClassifiers_INCLUDE_DIRS}
|
||||
${Bayesnet_INCLUDE_DIRS}
|
||||
${bayesnet_INCLUDE_DIRS}
|
||||
)
|
||||
add_executable(PlatformSample sample.cpp ${Platform_SOURCE_DIR}/src/main/Models.cpp)
|
||||
target_link_libraries(PlatformSample "${PyClassifiers}" "${BayesNet}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy)
|
||||
target_link_libraries(PlatformSample "${PyClassifiers}" "${BayesNet}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} ${Boost_LIBRARIES})
|
@@ -1,18 +1,10 @@
|
||||
include_directories(
|
||||
## Libs
|
||||
${Platform_SOURCE_DIR}/lib/log
|
||||
${Platform_SOURCE_DIR}/lib/Files
|
||||
${Platform_SOURCE_DIR}/lib/folding
|
||||
${Platform_SOURCE_DIR}/lib/mdlp/src
|
||||
${Platform_SOURCE_DIR}/lib/argparse/include
|
||||
${Platform_SOURCE_DIR}/lib/json/include
|
||||
${Platform_SOURCE_DIR}/lib/libxlsxwriter/include
|
||||
${Python3_INCLUDE_DIRS}
|
||||
${MPI_CXX_INCLUDE_DIRS}
|
||||
${TORCH_INCLUDE_DIRS}
|
||||
${CMAKE_BINARY_DIR}/configured_files/include
|
||||
${PyClassifiers_INCLUDE_DIRS}
|
||||
${Bayesnet_INCLUDE_DIRS}
|
||||
## Platform
|
||||
${Platform_SOURCE_DIR}/src
|
||||
${Platform_SOURCE_DIR}/results
|
||||
@@ -28,8 +20,10 @@ add_executable(
|
||||
results/Result.cpp
|
||||
experimental_clfs/XA1DE.cpp
|
||||
experimental_clfs/ExpClf.cpp
|
||||
experimental_clfs/DecisionTree.cpp
|
||||
experimental_clfs/AdaBoost.cpp
|
||||
)
|
||||
target_link_libraries(b_best Boost::boost "${PyClassifiers}" "${BayesNet}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy "${XLSXWRITER_LIB}")
|
||||
target_link_libraries(b_best Boost::boost "${PyClassifiers}" bayesnet::bayesnet fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" Boost::python Boost::numpy "${XLSXWRITER_LIB}")
|
||||
|
||||
# b_grid
|
||||
set(grid_sources GridSearch.cpp GridData.cpp GridExperiment.cpp GridBase.cpp )
|
||||
@@ -41,8 +35,10 @@ add_executable(b_grid commands/b_grid.cpp ${grid_sources}
|
||||
results/Result.cpp
|
||||
experimental_clfs/XA1DE.cpp
|
||||
experimental_clfs/ExpClf.cpp
|
||||
experimental_clfs/DecisionTree.cpp
|
||||
experimental_clfs/AdaBoost.cpp
|
||||
)
|
||||
target_link_libraries(b_grid ${MPI_CXX_LIBRARIES} "${PyClassifiers}" "${BayesNet}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy)
|
||||
target_link_libraries(b_grid ${MPI_CXX_LIBRARIES} "${PyClassifiers}" bayesnet::bayesnet fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" Boost::python Boost::numpy)
|
||||
|
||||
# b_list
|
||||
add_executable(b_list commands/b_list.cpp
|
||||
@@ -52,8 +48,10 @@ add_executable(b_list commands/b_list.cpp
|
||||
results/Result.cpp results/ResultsDatasetExcel.cpp results/ResultsDataset.cpp results/ResultsDatasetConsole.cpp
|
||||
experimental_clfs/XA1DE.cpp
|
||||
experimental_clfs/ExpClf.cpp
|
||||
experimental_clfs/DecisionTree.cpp
|
||||
experimental_clfs/AdaBoost.cpp
|
||||
)
|
||||
target_link_libraries(b_list "${PyClassifiers}" "${BayesNet}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy "${XLSXWRITER_LIB}")
|
||||
target_link_libraries(b_list "${PyClassifiers}" bayesnet::bayesnet fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" Boost::python Boost::numpy "${XLSXWRITER_LIB}")
|
||||
|
||||
# b_main
|
||||
set(main_sources Experiment.cpp Models.cpp HyperParameters.cpp Scores.cpp ArgumentsExperiment.cpp)
|
||||
@@ -64,8 +62,11 @@ add_executable(b_main commands/b_main.cpp ${main_sources}
|
||||
results/Result.cpp
|
||||
experimental_clfs/XA1DE.cpp
|
||||
experimental_clfs/ExpClf.cpp
|
||||
experimental_clfs/ExpClf.cpp
|
||||
experimental_clfs/DecisionTree.cpp
|
||||
experimental_clfs/AdaBoost.cpp
|
||||
)
|
||||
target_link_libraries(b_main "${PyClassifiers}" "${BayesNet}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy)
|
||||
target_link_libraries(b_main PRIVATE nlohmann_json::nlohmann_json "${PyClassifiers}" bayesnet::bayesnet fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" Boost::python Boost::numpy)
|
||||
|
||||
# b_manage
|
||||
set(manage_sources ManageScreen.cpp OptionsMenu.cpp ResultsManager.cpp)
|
||||
@@ -77,7 +78,7 @@ add_executable(
|
||||
results/Result.cpp results/ResultsDataset.cpp results/ResultsDatasetConsole.cpp
|
||||
main/Scores.cpp
|
||||
)
|
||||
target_link_libraries(b_manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" fimdlp "${BayesNet}")
|
||||
target_link_libraries(b_manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" fimdlp bayesnet::bayesnet)
|
||||
|
||||
# b_results
|
||||
add_executable(b_results commands/b_results.cpp)
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include "common/Colors.h"
|
||||
#include "common/CLocale.h"
|
||||
#include "common/Paths.h"
|
||||
@@ -123,16 +124,24 @@ namespace platform {
|
||||
}
|
||||
result = std::vector<std::string>(models.begin(), models.end());
|
||||
maxModelName = (*max_element(result.begin(), result.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||
maxModelName = std::max(12, maxModelName);
|
||||
maxModelName = std::max(minLength, maxModelName);
|
||||
return result;
|
||||
}
|
||||
std::string toLower(std::string data)
|
||||
{
|
||||
std::transform(data.begin(), data.end(), data.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
return data;
|
||||
}
|
||||
std::vector<std::string> BestResults::getDatasets(json table)
|
||||
{
|
||||
std::vector<std::string> datasets;
|
||||
for (const auto& dataset_ : table.items()) {
|
||||
datasets.push_back(dataset_.key());
|
||||
}
|
||||
std::stable_sort(datasets.begin(), datasets.end());
|
||||
std::stable_sort(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) {
|
||||
return toLower(a) < toLower(b);
|
||||
});
|
||||
maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||
maxDatasetName = std::max(7, maxDatasetName);
|
||||
return datasets;
|
||||
@@ -222,7 +231,7 @@ namespace platform {
|
||||
std::cout << oss.str();
|
||||
std::cout << std::string(oss.str().size() - 8, '-') << std::endl;
|
||||
std::cout << Colors::GREEN() << " # " << std::setw(maxDatasetName + 1) << std::left << std::string("Dataset");
|
||||
auto bestResultsTex = BestResultsTex();
|
||||
auto bestResultsTex = BestResultsTex(score);
|
||||
auto bestResultsMd = BestResultsMd();
|
||||
if (tex) {
|
||||
bestResultsTex.results_header(models, table.at("dateTable").get<std::string>(), index);
|
||||
@@ -266,12 +275,14 @@ namespace platform {
|
||||
// Print the row with red colors on max values
|
||||
for (const auto& model : models) {
|
||||
std::string efectiveColor = color;
|
||||
double value;
|
||||
double value, std;
|
||||
try {
|
||||
value = table[model].at(dataset_).at(0).get<double>();
|
||||
std = table[model].at(dataset_).at(3).get<double>();
|
||||
}
|
||||
catch (nlohmann::json_abi_v3_11_3::detail::out_of_range err) {
|
||||
value = -1.0;
|
||||
std = -1.0;
|
||||
}
|
||||
if (value == maxValue) {
|
||||
efectiveColor = Colors::RED();
|
||||
@@ -280,7 +291,8 @@ namespace platform {
|
||||
std::cout << Colors::YELLOW() << std::setw(maxModelName) << std::right << "N/A" << " ";
|
||||
} else {
|
||||
totals[model].push_back(value);
|
||||
std::cout << efectiveColor << std::setw(maxModelName) << std::setprecision(maxModelName - 2) << std::fixed << value << " ";
|
||||
std::cout << efectiveColor << std::setw(maxModelName - 6) << std::setprecision(maxModelName - 8) << std::fixed << value;
|
||||
std::cout << efectiveColor << "±" << std::setw(5) << std::setprecision(3) << std::fixed << std << " ";
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
@@ -307,9 +319,9 @@ namespace platform {
|
||||
for (const auto& model : models) {
|
||||
std::string efectiveColor = model == best_model ? Colors::RED() : Colors::GREEN();
|
||||
double value = std::reduce(totals[model].begin(), totals[model].end()) / nDatasets;
|
||||
double std_value = compute_std(totals[model], value);
|
||||
std::cout << efectiveColor << std::right << std::setw(maxModelName) << std::setprecision(maxModelName - 4) << std::fixed << value << " ";
|
||||
|
||||
double std = compute_std(totals[model], value);
|
||||
std::cout << efectiveColor << std::right << std::setw(maxModelName - 6) << std::setprecision(maxModelName - 8) << std::fixed << value;
|
||||
std::cout << efectiveColor << "±" << std::setw(5) << std::setprecision(3) << std::fixed << std << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
@@ -321,9 +333,10 @@ namespace platform {
|
||||
// Build the table of results
|
||||
json table = buildTableResults(models);
|
||||
std::vector<std::string> datasets = getDatasets(table.begin().value());
|
||||
BestResultsExcel excel_report(score, datasets);
|
||||
BestResultsExcel excel_report(path, score, datasets);
|
||||
excel_report.reportSingle(model, path + Paths::bestResultsFile(score, model));
|
||||
messageOutputFile("Excel", excel_report.getFileName());
|
||||
excelFileName = excel_report.getFileName();
|
||||
}
|
||||
}
|
||||
void BestResults::reportAll(bool excel, bool tex, bool index)
|
||||
@@ -337,9 +350,10 @@ namespace platform {
|
||||
// Compute the Friedman test
|
||||
std::map<std::string, std::map<std::string, float>> ranksModels;
|
||||
if (friedman) {
|
||||
Statistics stats(models, datasets, table, significance);
|
||||
Statistics stats(score, models, datasets, table, significance);
|
||||
auto result = stats.friedmanTest();
|
||||
stats.postHocHolmTest(result, tex);
|
||||
stats.postHocTest();
|
||||
stats.postHocTestReport(result, tex);
|
||||
ranksModels = stats.getRanks();
|
||||
}
|
||||
if (tex) {
|
||||
@@ -351,33 +365,21 @@ namespace platform {
|
||||
}
|
||||
}
|
||||
if (excel) {
|
||||
BestResultsExcel excel(score, datasets);
|
||||
BestResultsExcel excel(path, score, datasets);
|
||||
excel.reportAll(models, table, ranksModels, friedman, significance);
|
||||
if (friedman) {
|
||||
int idx = -1;
|
||||
double min = 2000;
|
||||
// Find out the control model
|
||||
auto totals = std::vector<double>(models.size(), 0.0);
|
||||
for (const auto& dataset_ : datasets) {
|
||||
for (int i = 0; i < models.size(); ++i) {
|
||||
totals[i] += ranksModels[dataset_][models[i]];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < models.size(); ++i) {
|
||||
if (totals[i] < min) {
|
||||
min = totals[i];
|
||||
idx = i;
|
||||
}
|
||||
}
|
||||
Statistics stats(score, models, datasets, table, significance);
|
||||
int idx = stats.getControlIdx();
|
||||
model = models.at(idx);
|
||||
excel.reportSingle(model, path + Paths::bestResultsFile(score, model));
|
||||
}
|
||||
messageOutputFile("Excel", excel.getFileName());
|
||||
excelFileName = excel.getFileName();
|
||||
}
|
||||
}
|
||||
void BestResults::messageOutputFile(const std::string& title, const std::string& fileName)
|
||||
{
|
||||
std::cout << Colors::YELLOW() << "** " << std::setw(5) << std::left << title
|
||||
std::cout << Colors::YELLOW() << "** " << std::setw(8) << std::left << title
|
||||
<< " file generated: " << fileName << Colors::RESET() << std::endl;
|
||||
}
|
||||
}
|
@@ -15,6 +15,7 @@ namespace platform {
|
||||
void reportSingle(bool excel);
|
||||
void reportAll(bool excel, bool tex, bool index);
|
||||
void buildAll();
|
||||
std::string getExcelFileName() const { return excelFileName; }
|
||||
private:
|
||||
std::vector<std::string> getModels();
|
||||
std::vector<std::string> getDatasets(json table);
|
||||
@@ -32,6 +33,8 @@ namespace platform {
|
||||
double significance;
|
||||
int maxModelName = 0;
|
||||
int maxDatasetName = 0;
|
||||
int minLength = 13; // Minimum length for scores
|
||||
std::string excelFileName;
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -30,7 +30,7 @@ namespace platform {
|
||||
}
|
||||
return columnName;
|
||||
}
|
||||
BestResultsExcel::BestResultsExcel(const std::string& score, const std::vector<std::string>& datasets) : score(score), datasets(datasets)
|
||||
BestResultsExcel::BestResultsExcel(const std::string& path, const std::string& score, const std::vector<std::string>& datasets) : path(path), score(score), datasets(datasets)
|
||||
{
|
||||
file_name = Paths::bestResultsExcel(score);
|
||||
workbook = workbook_new(getFileName().c_str());
|
||||
@@ -92,7 +92,7 @@ namespace platform {
|
||||
catch (const std::out_of_range& oor) {
|
||||
auto tabName = "table_" + std::to_string(i);
|
||||
auto worksheetNew = workbook_add_worksheet(workbook, tabName.c_str());
|
||||
json data = loadResultData(Paths::results() + fileName);
|
||||
json data = loadResultData(path + fileName);
|
||||
auto report = ReportExcel(data, false, workbook, worksheetNew);
|
||||
report.show();
|
||||
hyperlink = "#table_" + std::to_string(i);
|
||||
@@ -164,13 +164,15 @@ namespace platform {
|
||||
addConditionalFormat("max");
|
||||
footer(false);
|
||||
if (friedman) {
|
||||
// Create Sheet with ranks
|
||||
worksheet = workbook_add_worksheet(workbook, "Ranks");
|
||||
formatColumns();
|
||||
header(true);
|
||||
body(true);
|
||||
addConditionalFormat("min");
|
||||
footer(true);
|
||||
if (score == "accuracy") {
|
||||
// Create Sheet with ranks
|
||||
worksheet = workbook_add_worksheet(workbook, "Ranks");
|
||||
formatColumns();
|
||||
header(true);
|
||||
body(true);
|
||||
addConditionalFormat("min");
|
||||
footer(true);
|
||||
}
|
||||
// Create Sheet with Friedman Test
|
||||
doFriedman();
|
||||
}
|
||||
@@ -241,11 +243,12 @@ namespace platform {
|
||||
}
|
||||
worksheet_merge_range(worksheet, 0, 0, 0, 7, "Friedman Test", styles["headerFirst"]);
|
||||
row = 2;
|
||||
Statistics stats(models, datasets, table, significance, false);
|
||||
Statistics stats(score, models, datasets, table, significance, false); // No output
|
||||
auto result = stats.friedmanTest();
|
||||
stats.postHocHolmTest(result);
|
||||
stats.postHocTest();
|
||||
stats.postHocTestReport(result, false); // No tex output
|
||||
auto friedmanResult = stats.getFriedmanResult();
|
||||
auto holmResult = stats.getHolmResult();
|
||||
auto postHocResults = stats.getPostHocResults();
|
||||
worksheet_merge_range(worksheet, row, 0, row, 7, "Null hypothesis: H0 'There is no significant differences between all the classifiers.'", styles["headerSmall"]);
|
||||
row += 2;
|
||||
writeString(row, 1, "Friedman Q", "bodyHeader");
|
||||
@@ -264,7 +267,7 @@ namespace platform {
|
||||
row += 2;
|
||||
worksheet_merge_range(worksheet, row, 0, row, 7, "Null hypothesis: H0 'There is no significant differences between the control model and the other models.'", styles["headerSmall"]);
|
||||
row += 2;
|
||||
std::string controlModel = "Control Model: " + holmResult.model;
|
||||
std::string controlModel = "Control Model: " + postHocResults.at(0).model;
|
||||
worksheet_merge_range(worksheet, row, 1, row, 7, controlModel.c_str(), styles["bodyHeader_odd"]);
|
||||
row++;
|
||||
writeString(row, 1, "Model", "bodyHeader");
|
||||
@@ -276,7 +279,7 @@ namespace platform {
|
||||
writeString(row, 7, "Reject H0", "bodyHeader");
|
||||
row++;
|
||||
bool first = true;
|
||||
for (const auto& item : holmResult.holmLines) {
|
||||
for (const auto& item : postHocResults) {
|
||||
writeString(row, 1, item.model, "text");
|
||||
if (first) {
|
||||
// Control model info
|
||||
|
@@ -10,7 +10,7 @@ namespace platform {
|
||||
using json = nlohmann::ordered_json;
|
||||
class BestResultsExcel : public ExcelFile {
|
||||
public:
|
||||
BestResultsExcel(const std::string& score, const std::vector<std::string>& datasets);
|
||||
BestResultsExcel(const std::string& path, const std::string& score, const std::vector<std::string>& datasets);
|
||||
~BestResultsExcel();
|
||||
void reportAll(const std::vector<std::string>& models, const json& table, const std::map<std::string, std::map<std::string, float>>& ranks, bool friedman, double significance);
|
||||
void reportSingle(const std::string& model, const std::string& fileName);
|
||||
@@ -22,6 +22,7 @@ namespace platform {
|
||||
void formatColumns();
|
||||
void doFriedman();
|
||||
void addConditionalFormat(std::string formula);
|
||||
std::string path;
|
||||
std::string score;
|
||||
std::vector<std::string> models;
|
||||
std::vector<std::string> datasets;
|
||||
|
@@ -75,7 +75,7 @@ namespace platform {
|
||||
|
||||
handler.close();
|
||||
}
|
||||
void BestResultsMd::holm_test(struct HolmResult& holmResult, const std::string& date)
|
||||
void BestResultsMd::postHoc_test(std::vector<PostHocLine>& postHocResults, const std::string& kind, const std::string& date)
|
||||
{
|
||||
auto file_name = Paths::tex() + Paths::md_post_hoc();
|
||||
openMdFile(file_name);
|
||||
@@ -84,13 +84,15 @@ namespace platform {
|
||||
handler << std::endl;
|
||||
handler << " Post-hoc handler test" << std::endl;
|
||||
handler << "-->" << std::endl;
|
||||
handler << "Post-hoc Holm test: H<sub>0</sub>: There is no significant differences between the control model and the other models." << std::endl << std::endl;
|
||||
handler << "Post-hoc " << kind << " test: H<sub>0</sub>: There is no significant differences between the control model and the other models." << std::endl << std::endl;
|
||||
handler << "| classifier | pvalue | rank | win | tie | loss | H<sub>0</sub> |" << std::endl;
|
||||
handler << "| :-- | --: | --: | --:| --: | --: | :--: |" << std::endl;
|
||||
for (auto const& line : holmResult.holmLines) {
|
||||
bool first = true;
|
||||
for (auto const& line : postHocResults) {
|
||||
auto textStatus = !line.reject ? "**" : " ";
|
||||
if (line.model == holmResult.model) {
|
||||
if (first) {
|
||||
handler << "| " << line.model << " | - | " << std::fixed << std::setprecision(2) << line.rank << " | - | - | - |" << std::endl;
|
||||
first = false;
|
||||
} else {
|
||||
handler << "| " << line.model << " | " << textStatus << std::scientific << std::setprecision(4) << line.pvalue << textStatus << " |";
|
||||
handler << std::fixed << std::setprecision(2) << line.rank << " | " << line.wtl.win << " | " << line.wtl.tie << " | " << line.wtl.loss << " |";
|
||||
|
@@ -14,7 +14,7 @@ namespace platform {
|
||||
void results_header(const std::vector<std::string>& models, const std::string& date);
|
||||
void results_body(const std::vector<std::string>& datasets, json& table);
|
||||
void results_footer(const std::map<std::string, std::vector<double>>& totals, const std::string& best_model);
|
||||
void holm_test(struct HolmResult& holmResult, const std::string& date);
|
||||
void postHoc_test(std::vector<PostHocLine>& postHocResults, const std::string& kind, const std::string& date);
|
||||
private:
|
||||
void openMdFile(const std::string& name);
|
||||
std::ofstream handler;
|
||||
|
@@ -27,8 +27,10 @@ namespace platform {
|
||||
handler << "\\tiny " << std::endl;
|
||||
handler << "\\renewcommand{\\arraystretch }{1.2} " << std::endl;
|
||||
handler << "\\renewcommand{\\tabcolsep }{0.07cm} " << std::endl;
|
||||
handler << "\\caption{Accuracy results(mean $\\pm$ std) for all the algorithms and datasets} " << std::endl;
|
||||
handler << "\\label{tab:results_accuracy}" << std::endl;
|
||||
auto umetric = score;
|
||||
umetric[0] = toupper(umetric[0]);
|
||||
handler << "\\caption{" << umetric << " results(mean $\\pm$ std) for all the algorithms and datasets} " << std::endl;
|
||||
handler << "\\label{tab:results_" << score << "}" << std::endl;
|
||||
std::string header_dataset_name = index ? "r" : "l";
|
||||
handler << "\\begin{tabular} {{" << header_dataset_name << std::string(models.size(), 'c').c_str() << "}}" << std::endl;
|
||||
handler << "\\hline " << std::endl;
|
||||
@@ -87,26 +89,28 @@ namespace platform {
|
||||
handler << "\\end{table}" << std::endl;
|
||||
handler.close();
|
||||
}
|
||||
void BestResultsTex::holm_test(struct HolmResult& holmResult, const std::string& date)
|
||||
void BestResultsTex::postHoc_test(std::vector<PostHocLine>& postHocResults, const std::string& kind, const std::string& date)
|
||||
{
|
||||
auto file_name = Paths::tex() + Paths::tex_post_hoc();
|
||||
openTexFile(file_name);
|
||||
handler << "%% This file has been generated by the platform program" << std::endl;
|
||||
handler << "%% Date: " << date.c_str() << std::endl;
|
||||
handler << "%%" << std::endl;
|
||||
handler << "%% Post-hoc handler test" << std::endl;
|
||||
handler << "%% Post-hoc " << kind << " test" << std::endl;
|
||||
handler << "%%" << std::endl;
|
||||
handler << "\\begin{table}[htbp]" << std::endl;
|
||||
handler << "\\centering" << std::endl;
|
||||
handler << "\\caption{Results of the post-hoc test for the mean accuracy of the algorithms.}\\label{tab:tests}" << std::endl;
|
||||
handler << "\\caption{Results of the post-hoc " << kind << " test for the mean " << score << " of the algorithms.}\\label{ tab:tests }" << std::endl;
|
||||
handler << "\\begin{tabular}{lrrrrr}" << std::endl;
|
||||
handler << "\\hline" << std::endl;
|
||||
handler << "classifier & pvalue & rank & win & tie & loss\\\\" << std::endl;
|
||||
handler << "\\hline" << std::endl;
|
||||
for (auto const& line : holmResult.holmLines) {
|
||||
bool first = true;
|
||||
for (auto const& line : postHocResults) {
|
||||
auto textStatus = !line.reject ? "\\bf " : " ";
|
||||
if (line.model == holmResult.model) {
|
||||
if (first) {
|
||||
handler << line.model << " & - & " << std::fixed << std::setprecision(2) << line.rank << " & - & - & - \\\\" << std::endl;
|
||||
first = false;
|
||||
} else {
|
||||
handler << line.model << " & " << textStatus << std::scientific << std::setprecision(4) << line.pvalue << " & ";
|
||||
handler << std::fixed << std::setprecision(2) << line.rank << " & " << line.wtl.win << " & " << line.wtl.tie << " & " << line.wtl.loss << "\\\\" << std::endl;
|
||||
|
@@ -9,13 +9,14 @@ namespace platform {
|
||||
using json = nlohmann::ordered_json;
|
||||
class BestResultsTex {
|
||||
public:
|
||||
BestResultsTex(bool dataset_name = true) : dataset_name(dataset_name) {};
|
||||
BestResultsTex(const std::string score, bool dataset_name = true) : score{ score }, dataset_name{ dataset_name } {};
|
||||
~BestResultsTex() = default;
|
||||
void results_header(const std::vector<std::string>& models, const std::string& date, bool index);
|
||||
void results_body(const std::vector<std::string>& datasets, json& table, bool index);
|
||||
void results_footer(const std::map<std::string, std::vector<double>>& totals, const std::string& best_model);
|
||||
void holm_test(struct HolmResult& holmResult, const std::string& date);
|
||||
void postHoc_test(std::vector<PostHocLine>& postHocResults, const std::string& kind, const std::string& date);
|
||||
private:
|
||||
std::string score;
|
||||
bool dataset_name;
|
||||
void openTexFile(const std::string& name);
|
||||
std::ofstream handler;
|
||||
|
@@ -7,18 +7,25 @@
|
||||
#include "BestResultsTex.h"
|
||||
#include "BestResultsMd.h"
|
||||
#include "Statistics.h"
|
||||
#include "WilcoxonTest.hpp"
|
||||
|
||||
|
||||
namespace platform {
|
||||
|
||||
Statistics::Statistics(const std::vector<std::string>& models, const std::vector<std::string>& datasets, const json& data, double significance, bool output) :
|
||||
models(models), datasets(datasets), data(data), significance(significance), output(output)
|
||||
Statistics::Statistics(const std::string& score, const std::vector<std::string>& models, const std::vector<std::string>& datasets, const json& data, double significance, bool output) :
|
||||
score(score), models(models), datasets(datasets), data(data), significance(significance), output(output)
|
||||
{
|
||||
if (score == "accuracy") {
|
||||
postHocType = "Holm";
|
||||
hlen = 85;
|
||||
} else {
|
||||
postHocType = "Wilcoxon";
|
||||
hlen = 88;
|
||||
}
|
||||
nModels = models.size();
|
||||
nDatasets = datasets.size();
|
||||
auto temp = ConfigLocale();
|
||||
}
|
||||
|
||||
void Statistics::fit()
|
||||
{
|
||||
if (nModels < 3 || nDatasets < 3) {
|
||||
@@ -27,9 +34,11 @@ namespace platform {
|
||||
throw std::runtime_error("Can't make the Friedman test with less than 3 models and/or less than 3 datasets.");
|
||||
}
|
||||
ranksModels.clear();
|
||||
computeRanks();
|
||||
computeRanks(); // compute greaterAverage and ranks
|
||||
// Set the control model as the one with the lowest average rank
|
||||
controlIdx = distance(ranks.begin(), min_element(ranks.begin(), ranks.end(), [](const auto& l, const auto& r) { return l.second < r.second; }));
|
||||
controlIdx = score == "accuracy" ?
|
||||
distance(ranks.begin(), min_element(ranks.begin(), ranks.end(), [](const auto& l, const auto& r) { return l.second < r.second; }))
|
||||
: greaterAverage; // The model with the greater average score
|
||||
computeWTL();
|
||||
maxModelName = (*std::max_element(models.begin(), models.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||
maxDatasetName = (*std::max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||
@@ -66,11 +75,16 @@ namespace platform {
|
||||
void Statistics::computeRanks()
|
||||
{
|
||||
std::map<std::string, float> ranksLine;
|
||||
std::map<std::string, float> averages;
|
||||
for (const auto& model : models) {
|
||||
averages[model] = 0;
|
||||
}
|
||||
for (const auto& dataset : datasets) {
|
||||
std::vector<std::pair<std::string, double>> ranksOrder;
|
||||
for (const auto& model : models) {
|
||||
double value = data[model].at(dataset).at(0).get<double>();
|
||||
ranksOrder.push_back({ model, value });
|
||||
averages[model] += value;
|
||||
}
|
||||
// Assign the ranks
|
||||
ranksLine = assignRanks(ranksOrder);
|
||||
@@ -88,10 +102,17 @@ namespace platform {
|
||||
for (const auto& rank : ranks) {
|
||||
ranks[rank.first] /= nDatasets;
|
||||
}
|
||||
// Average the scores
|
||||
for (const auto& average : averages) {
|
||||
averages[average.first] /= nDatasets;
|
||||
}
|
||||
// Get the model with the greater average score
|
||||
greaterAverage = distance(averages.begin(), max_element(averages.begin(), averages.end(), [](const auto& l, const auto& r) { return l.second < r.second; }));
|
||||
}
|
||||
void Statistics::computeWTL()
|
||||
{
|
||||
// Compute the WTL matrix
|
||||
const double practical_threshold = 0.0005;
|
||||
// Compute the WTL matrix (Win Tie Loss)
|
||||
for (int i = 0; i < nModels; ++i) {
|
||||
wtl[i] = { 0, 0, 0 };
|
||||
}
|
||||
@@ -104,23 +125,85 @@ namespace platform {
|
||||
continue;
|
||||
}
|
||||
double value = data[models[i]].at(item.key()).at(0).get<double>();
|
||||
if (value < controlValue) {
|
||||
wtl[i].win++;
|
||||
} else if (value == controlValue) {
|
||||
double diff = controlValue - value; // control − comparison
|
||||
if (std::fabs(diff) <= practical_threshold) {
|
||||
wtl[i].tie++;
|
||||
} else if (diff < 0) {
|
||||
wtl[i].win++;
|
||||
} else {
|
||||
wtl[i].loss++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Statistics::postHocHolmTest(bool friedmanResult, bool tex)
|
||||
int Statistics::getControlIdx()
|
||||
{
|
||||
if (!fitted) {
|
||||
fit();
|
||||
}
|
||||
return controlIdx;
|
||||
}
|
||||
void Statistics::postHocTest()
|
||||
{
|
||||
if (score == "accuracy") {
|
||||
postHocHolmTest();
|
||||
} else {
|
||||
postHocWilcoxonTest();
|
||||
}
|
||||
}
|
||||
void Statistics::postHocWilcoxonTest()
|
||||
{
|
||||
if (!fitted) {
|
||||
fit();
|
||||
}
|
||||
// Reference: Wilcoxon, F. (1945). “Individual Comparisons by Ranking Methods”. Biometrics Bulletin, 1(6), 80-83.
|
||||
auto wilcoxon = WilcoxonTest(models, datasets, data, significance);
|
||||
controlIdx = wilcoxon.getControlIdx();
|
||||
postHocResults = wilcoxon.getPostHocResults();
|
||||
setResultsOrder();
|
||||
// Fill the ranks info
|
||||
for (const auto& item : postHocResults) {
|
||||
ranks[item.model] = item.rank;
|
||||
}
|
||||
Holm_Bonferroni();
|
||||
restoreResultsOrder();
|
||||
}
|
||||
void Statistics::Holm_Bonferroni()
|
||||
{
|
||||
// The algorithm need the p-values sorted from the lowest to the highest
|
||||
// Sort the models by p-value
|
||||
std::sort(postHocResults.begin(), postHocResults.end(), [](const PostHocLine& a, const PostHocLine& b) {
|
||||
return a.pvalue < b.pvalue;
|
||||
});
|
||||
// Holm adjustment
|
||||
for (int i = 0; i < postHocResults.size(); ++i) {
|
||||
auto item = postHocResults.at(i);
|
||||
double before = i == 0 ? 0.0 : postHocResults.at(i - 1).pvalue;
|
||||
double p_value = std::min((long double)1.0, item.pvalue * (nModels - i));
|
||||
p_value = std::max(before, p_value);
|
||||
postHocResults[i].pvalue = p_value;
|
||||
}
|
||||
}
|
||||
void Statistics::setResultsOrder()
|
||||
{
|
||||
int c = 0;
|
||||
for (auto& item : postHocResults) {
|
||||
item.idx = c++;
|
||||
}
|
||||
|
||||
}
|
||||
void Statistics::restoreResultsOrder()
|
||||
{
|
||||
// Restore the order of the results
|
||||
std::sort(postHocResults.begin(), postHocResults.end(), [](const PostHocLine& a, const PostHocLine& b) {
|
||||
return a.idx < b.idx;
|
||||
});
|
||||
}
|
||||
void Statistics::postHocHolmTest()
|
||||
{
|
||||
if (!fitted) {
|
||||
fit();
|
||||
}
|
||||
std::stringstream oss;
|
||||
// Reference https://link.springer.com/article/10.1007/s44196-022-00083-8
|
||||
// Post-hoc Holm test
|
||||
// Calculate the p-value for the models paired with the control model
|
||||
@@ -128,80 +211,66 @@ namespace platform {
|
||||
boost::math::normal dist(0.0, 1.0);
|
||||
double diff = sqrt(nModels * (nModels + 1) / (6.0 * nDatasets));
|
||||
for (int i = 0; i < nModels; i++) {
|
||||
PostHocLine line;
|
||||
line.model = models[i];
|
||||
line.rank = ranks.at(models[i]);
|
||||
line.wtl = wtl.at(i);
|
||||
line.reject = false;
|
||||
if (i == controlIdx) {
|
||||
stats[i] = 0.0;
|
||||
postHocResults.push_back(line);
|
||||
continue;
|
||||
}
|
||||
double z = std::abs(ranks.at(models[controlIdx]) - ranks.at(models[i])) / diff;
|
||||
double p_value = (long double)2 * (1 - cdf(dist, z));
|
||||
stats[i] = p_value;
|
||||
line.pvalue = (long double)2 * (1 - cdf(dist, z));
|
||||
line.reject = (line.pvalue < significance);
|
||||
postHocResults.push_back(line);
|
||||
}
|
||||
// Sort the models by p-value
|
||||
std::vector<std::pair<int, double>> statsOrder;
|
||||
for (const auto& stat : stats) {
|
||||
statsOrder.push_back({ stat.first, stat.second });
|
||||
}
|
||||
std::sort(statsOrder.begin(), statsOrder.end(), [](const std::pair<int, double>& a, const std::pair<int, double>& b) {
|
||||
return a.second < b.second;
|
||||
std::sort(postHocResults.begin(), postHocResults.end(), [](const PostHocLine& a, const PostHocLine& b) {
|
||||
return a.rank < b.rank;
|
||||
});
|
||||
setResultsOrder();
|
||||
Holm_Bonferroni();
|
||||
restoreResultsOrder();
|
||||
}
|
||||
|
||||
// Holm adjustment
|
||||
for (int i = 0; i < statsOrder.size(); ++i) {
|
||||
auto item = statsOrder.at(i);
|
||||
double before = i == 0 ? 0.0 : statsOrder.at(i - 1).second;
|
||||
double p_value = std::min((double)1.0, item.second * (nModels - i));
|
||||
p_value = std::max(before, p_value);
|
||||
statsOrder[i] = { item.first, p_value };
|
||||
}
|
||||
holmResult.model = models.at(controlIdx);
|
||||
void Statistics::postHocTestReport(bool friedmanResult, bool tex)
|
||||
{
|
||||
|
||||
std::stringstream oss;
|
||||
auto color = friedmanResult ? Colors::CYAN() : Colors::YELLOW();
|
||||
oss << color;
|
||||
oss << " *************************************************************************************************************" << std::endl;
|
||||
oss << " Post-hoc Holm test: H0: 'There is no significant differences between the control model and the other models.'" << std::endl;
|
||||
oss << " " << std::string(hlen + 25, '*') << std::endl;
|
||||
oss << " Post-hoc " << postHocType << " test: H0: 'There is no significant differences between the control model and the other models.'" << std::endl;
|
||||
oss << " Control model: " << models.at(controlIdx) << std::endl;
|
||||
oss << " " << std::left << std::setw(maxModelName) << std::string("Model") << " p-value rank win tie loss Status" << std::endl;
|
||||
oss << " " << std::string(maxModelName, '=') << " ============ ========= === === ==== =============" << std::endl;
|
||||
// sort ranks from lowest to highest
|
||||
std::vector<std::pair<std::string, float>> ranksOrder;
|
||||
for (const auto& rank : ranks) {
|
||||
ranksOrder.push_back({ rank.first, rank.second });
|
||||
}
|
||||
std::sort(ranksOrder.begin(), ranksOrder.end(), [](const std::pair<std::string, float>& a, const std::pair<std::string, float>& b) {
|
||||
return a.second < b.second;
|
||||
});
|
||||
// Show the control model info.
|
||||
oss << " " << Colors::BLUE() << std::left << std::setw(maxModelName) << ranksOrder.at(0).first << " ";
|
||||
oss << std::setw(12) << " " << std::setprecision(7) << std::fixed << " " << ranksOrder.at(0).second << std::endl;
|
||||
for (const auto& item : ranksOrder) {
|
||||
auto idx = distance(models.begin(), find(models.begin(), models.end(), item.first));
|
||||
double pvalue = 0.0;
|
||||
for (const auto& stat : statsOrder) {
|
||||
if (stat.first == idx) {
|
||||
pvalue = stat.second;
|
||||
}
|
||||
}
|
||||
holmResult.holmLines.push_back({ item.first, pvalue, item.second, wtl.at(idx), pvalue < significance });
|
||||
if (item.first == models.at(controlIdx)) {
|
||||
bool first = true;
|
||||
for (const auto& item : postHocResults) {
|
||||
if (first) {
|
||||
oss << " " << Colors::BLUE() << std::left << std::setw(maxModelName) << item.model << " ";
|
||||
oss << std::setw(12) << " " << std::setprecision(7) << std::fixed << " " << item.rank << std::endl;
|
||||
first = false;
|
||||
continue;
|
||||
}
|
||||
auto pvalue = item.pvalue;
|
||||
auto colorStatus = pvalue > significance ? Colors::GREEN() : Colors::MAGENTA();
|
||||
auto status = pvalue > significance ? Symbols::check_mark : Symbols::cross;
|
||||
auto textStatus = pvalue > significance ? " accepted H0" : " rejected H0";
|
||||
oss << " " << colorStatus << std::left << std::setw(maxModelName) << item.first << " ";
|
||||
oss << std::setprecision(6) << std::scientific << pvalue << std::setprecision(7) << std::fixed << " " << item.second;
|
||||
oss << " " << std::right << std::setw(3) << wtl.at(idx).win << " " << std::setw(3) << wtl.at(idx).tie << " " << std::setw(4) << wtl.at(idx).loss;
|
||||
oss << " " << colorStatus << std::left << std::setw(maxModelName) << item.model << " ";
|
||||
oss << std::setprecision(6) << std::scientific << pvalue << std::setprecision(7) << std::fixed << " " << item.rank;
|
||||
oss << " " << std::right << std::setw(3) << item.wtl.win << " " << std::setw(3) << item.wtl.tie << " " << std::setw(4) << item.wtl.loss;
|
||||
oss << " " << status << textStatus << std::endl;
|
||||
}
|
||||
oss << color << " *************************************************************************************************************" << std::endl;
|
||||
oss << color << " " << std::string(hlen + 25, '*') << std::endl;
|
||||
oss << Colors::RESET();
|
||||
if (output) {
|
||||
std::cout << oss.str();
|
||||
}
|
||||
if (tex) {
|
||||
BestResultsTex bestResultsTex;
|
||||
BestResultsTex bestResultsTex(score);
|
||||
BestResultsMd bestResultsMd;
|
||||
bestResultsTex.holm_test(holmResult, get_date() + " " + get_time());
|
||||
bestResultsMd.holm_test(holmResult, get_date() + " " + get_time());
|
||||
bestResultsTex.postHoc_test(postHocResults, postHocType, get_date() + " " + get_time());
|
||||
bestResultsMd.postHoc_test(postHocResults, postHocType, get_date() + " " + get_time());
|
||||
}
|
||||
}
|
||||
bool Statistics::friedmanTest()
|
||||
@@ -213,7 +282,7 @@ namespace platform {
|
||||
// Friedman test
|
||||
// Calculate the Friedman statistic
|
||||
oss << Colors::BLUE() << std::endl;
|
||||
oss << "***************************************************************************************************************" << std::endl;
|
||||
oss << std::string(hlen, '*') << std::endl;
|
||||
oss << Colors::GREEN() << "Friedman test: H0: 'There is no significant differences between all the classifiers.'" << Colors::BLUE() << std::endl;
|
||||
double degreesOfFreedom = nModels - 1.0;
|
||||
double sumSquared = 0;
|
||||
@@ -238,23 +307,11 @@ namespace platform {
|
||||
oss << Colors::YELLOW() << "The null hypothesis H0 is accepted. Computed p-values will not be significant." << std::endl;
|
||||
result = false;
|
||||
}
|
||||
oss << Colors::BLUE() << "***************************************************************************************************************" << Colors::RESET() << std::endl;
|
||||
oss << Colors::BLUE() << std::string(hlen, '*') << Colors::RESET() << std::endl;
|
||||
if (output) {
|
||||
std::cout << oss.str();
|
||||
}
|
||||
friedmanResult = { friedmanQ, criticalValue, p_value, result };
|
||||
return result;
|
||||
}
|
||||
FriedmanResult& Statistics::getFriedmanResult()
|
||||
{
|
||||
return friedmanResult;
|
||||
}
|
||||
HolmResult& Statistics::getHolmResult()
|
||||
{
|
||||
return holmResult;
|
||||
}
|
||||
std::map<std::string, std::map<std::string, float>>& Statistics::getRanks()
|
||||
{
|
||||
return ranksModels;
|
||||
}
|
||||
} // namespace platform
|
||||
|
@@ -9,9 +9,9 @@ namespace platform {
|
||||
using json = nlohmann::ordered_json;
|
||||
|
||||
struct WTL {
|
||||
int win;
|
||||
int tie;
|
||||
int loss;
|
||||
uint win;
|
||||
uint tie;
|
||||
uint loss;
|
||||
};
|
||||
struct FriedmanResult {
|
||||
double statistic;
|
||||
@@ -19,29 +19,36 @@ namespace platform {
|
||||
long double pvalue;
|
||||
bool reject;
|
||||
};
|
||||
struct HolmLine {
|
||||
struct PostHocLine {
|
||||
uint idx; //index of the main order
|
||||
std::string model;
|
||||
long double pvalue;
|
||||
double rank;
|
||||
WTL wtl;
|
||||
bool reject;
|
||||
};
|
||||
struct HolmResult {
|
||||
std::string model;
|
||||
std::vector<HolmLine> holmLines;
|
||||
};
|
||||
|
||||
class Statistics {
|
||||
public:
|
||||
Statistics(const std::vector<std::string>& models, const std::vector<std::string>& datasets, const json& data, double significance = 0.05, bool output = true);
|
||||
Statistics(const std::string& score, const std::vector<std::string>& models, const std::vector<std::string>& datasets, const json& data, double significance = 0.05, bool output = true);
|
||||
bool friedmanTest();
|
||||
void postHocHolmTest(bool friedmanResult, bool tex=false);
|
||||
FriedmanResult& getFriedmanResult();
|
||||
HolmResult& getHolmResult();
|
||||
std::map<std::string, std::map<std::string, float>>& getRanks();
|
||||
void postHocTest();
|
||||
void postHocTestReport(bool friedmanResult, bool tex);
|
||||
int getControlIdx();
|
||||
FriedmanResult& getFriedmanResult() { return friedmanResult; }
|
||||
std::vector<PostHocLine>& getPostHocResults() { return postHocResults; }
|
||||
std::map<std::string, std::map<std::string, float>>& getRanks() { return ranksModels; } // ranks of the models per dataset
|
||||
private:
|
||||
void fit();
|
||||
void postHocHolmTest();
|
||||
void postHocWilcoxonTest();
|
||||
void computeRanks();
|
||||
void computeWTL();
|
||||
void Holm_Bonferroni();
|
||||
void setResultsOrder(); // Set the order of the results based on the statistic analysis needed
|
||||
void restoreResultsOrder(); // Restore the order of the results after the Holm-Bonferroni adjustment
|
||||
const std::string& score;
|
||||
std::string postHocType;
|
||||
const std::vector<std::string>& models;
|
||||
const std::vector<std::string>& datasets;
|
||||
const json& data;
|
||||
@@ -51,12 +58,14 @@ namespace platform {
|
||||
int nModels = 0;
|
||||
int nDatasets = 0;
|
||||
int controlIdx = 0;
|
||||
int greaterAverage = -1; // The model with the greater average score
|
||||
std::map<int, WTL> wtl;
|
||||
std::map<std::string, float> ranks;
|
||||
int maxModelName = 0;
|
||||
int maxDatasetName = 0;
|
||||
int hlen; // length of the line
|
||||
FriedmanResult friedmanResult;
|
||||
HolmResult holmResult;
|
||||
std::vector<PostHocLine> postHocResults;
|
||||
std::map<std::string, std::map<std::string, float>> ranksModels;
|
||||
};
|
||||
}
|
||||
|
245
src/best/WilcoxonTest.hpp
Normal file
245
src/best/WilcoxonTest.hpp
Normal file
@@ -0,0 +1,245 @@
|
||||
#ifndef BEST_WILCOXON_TEST_HPP
|
||||
#define BEST_WILCOXON_TEST_HPP
|
||||
// WilcoxonTest.hpp
|
||||
// Stand‑alone class for paired Wilcoxon signed‑rank post‑hoc analysis
|
||||
// ------------------------------------------------------------------
|
||||
// * Constructor takes the *already‑loaded* nlohmann::json object plus the
|
||||
// vectors of model and dataset names.
|
||||
// * Internally selects a control model (highest average AUC) and builds all
|
||||
// statistics (ranks, W/T/L counts, Wilcoxon p‑values).
|
||||
// * Public API:
|
||||
// int getControlIdx() const;
|
||||
// PostHocResult getPostHocResult() const;
|
||||
//
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <limits>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "Statistics.h"
|
||||
|
||||
namespace platform {
|
||||
class WilcoxonTest {
|
||||
public:
|
||||
WilcoxonTest(const std::vector<std::string>& models, const std::vector<std::string>& datasets,
|
||||
const json& data, double alpha = 0.05) : models_(models), datasets_(datasets), data_(data), alpha_(alpha)
|
||||
{
|
||||
buildAUCTable(); // extracts all AUCs into a dense matrix
|
||||
computeAverageAUCs(); // per‑model mean (→ control selection)
|
||||
computeAverageRanks(); // Friedman‑style ranks per model
|
||||
selectControlModel(); // sets control_idx_
|
||||
buildPostHocResult(); // fills postHocResult_
|
||||
}
|
||||
|
||||
int getControlIdx() const noexcept { return control_idx_; }
|
||||
const std::vector<PostHocLine>& getPostHocResults() const noexcept { return postHocResults_; }
|
||||
|
||||
private:
|
||||
//-------------------------------------------------- helper structs ----
|
||||
// When a value is missing we keep NaN so that ordinary arithmetic still
|
||||
// works (NaN simply propagates and we can test with std::isnan).
|
||||
using Matrix = std::vector<std::vector<double>>; // [model][dataset]
|
||||
|
||||
//------------------------------------------------- implementation ----
|
||||
void buildAUCTable()
|
||||
{
|
||||
const std::size_t M = models_.size();
|
||||
const std::size_t D = datasets_.size();
|
||||
auc_.assign(M, std::vector<double>(D, std::numeric_limits<double>::quiet_NaN()));
|
||||
|
||||
for (std::size_t i = 0; i < M; ++i) {
|
||||
const auto& model = models_[i];
|
||||
for (std::size_t j = 0; j < D; ++j) {
|
||||
const auto& ds = datasets_[j];
|
||||
try {
|
||||
auc_[i][j] = data_.at(model).at(ds).at(0).get<double>();
|
||||
}
|
||||
catch (...) {
|
||||
// leave as NaN when value missing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void computeAverageAUCs()
|
||||
{
|
||||
const std::size_t M = models_.size();
|
||||
avg_auc_.resize(M, std::numeric_limits<double>::quiet_NaN());
|
||||
|
||||
for (std::size_t i = 0; i < M; ++i) {
|
||||
double sum = 0.0;
|
||||
std::size_t cnt = 0;
|
||||
for (double v : auc_[i]) {
|
||||
if (!std::isnan(v)) { sum += v; ++cnt; }
|
||||
}
|
||||
avg_auc_[i] = cnt ? sum / cnt : std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
}
|
||||
|
||||
// Average rank across datasets (1 = best).
|
||||
void computeAverageRanks()
|
||||
{
|
||||
const std::size_t M = models_.size();
|
||||
const std::size_t D = datasets_.size();
|
||||
rank_sum_.assign(M, 0.0);
|
||||
rank_cnt_.assign(M, 0);
|
||||
|
||||
const double EPS = 1e-10;
|
||||
|
||||
for (std::size_t j = 0; j < D; ++j) {
|
||||
// Collect present values for this dataset
|
||||
std::vector<std::pair<double, std::size_t>> vals; // (auc, model_idx)
|
||||
vals.reserve(M);
|
||||
for (std::size_t i = 0; i < M; ++i) {
|
||||
if (!std::isnan(auc_[i][j]))
|
||||
vals.emplace_back(auc_[i][j], i);
|
||||
}
|
||||
if (vals.empty()) continue; // no info for this dataset
|
||||
|
||||
// Sort descending (higher AUC better)
|
||||
std::sort(vals.begin(), vals.end(), [](auto a, auto b) {
|
||||
return a.first > b.first;
|
||||
});
|
||||
|
||||
// Assign ranks with average for ties
|
||||
std::size_t k = 0;
|
||||
while (k < vals.size()) {
|
||||
std::size_t l = k + 1;
|
||||
while (l < vals.size() && std::fabs(vals[l].first - vals[k].first) < EPS) ++l;
|
||||
const double avg_rank = (k + 1 + l) * 0.5; // average of ranks (1‑based)
|
||||
for (std::size_t m = k; m < l; ++m) {
|
||||
const auto idx = vals[m].second;
|
||||
rank_sum_[idx] += avg_rank;
|
||||
++rank_cnt_[idx];
|
||||
}
|
||||
k = l;
|
||||
}
|
||||
}
|
||||
|
||||
// Final average
|
||||
avg_rank_.resize(M, std::numeric_limits<double>::quiet_NaN());
|
||||
for (std::size_t i = 0; i < M; ++i) {
|
||||
avg_rank_[i] = rank_cnt_[i] ? rank_sum_[i] / rank_cnt_[i]
|
||||
: std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
}
|
||||
|
||||
void selectControlModel()
|
||||
{
|
||||
// pick model with highest average AUC (ties → first)
|
||||
control_idx_ = 0;
|
||||
for (std::size_t i = 1; i < avg_auc_.size(); ++i) {
|
||||
if (avg_auc_[i] > avg_auc_[control_idx_]) control_idx_ = static_cast<int>(i);
|
||||
}
|
||||
}
|
||||
|
||||
void buildPostHocResult()
|
||||
{
|
||||
const std::size_t M = models_.size();
|
||||
const std::size_t D = datasets_.size();
|
||||
const std::string& control_name = models_[control_idx_];
|
||||
|
||||
const double practical_threshold = 0.0005; // same heuristic as original code
|
||||
|
||||
for (std::size_t i = 0; i < M; ++i) {
|
||||
PostHocLine line;
|
||||
line.model = models_[i];
|
||||
line.rank = avg_auc_[i];
|
||||
|
||||
WTL wtl = { 0, 0, 0 }; // win, tie, loss
|
||||
std::vector<double> differences;
|
||||
differences.reserve(D);
|
||||
|
||||
for (std::size_t j = 0; j < D; ++j) {
|
||||
double auc_control = auc_[control_idx_][j];
|
||||
double auc_other = auc_[i][j];
|
||||
if (std::isnan(auc_control) || std::isnan(auc_other)) continue;
|
||||
|
||||
double diff = auc_control - auc_other; // control − comparison
|
||||
if (std::fabs(diff) <= practical_threshold) {
|
||||
++wtl.tie;
|
||||
} else if (diff < 0) {
|
||||
++wtl.win; // comparison wins
|
||||
} else {
|
||||
++wtl.loss; // control wins
|
||||
}
|
||||
differences.push_back(diff);
|
||||
}
|
||||
|
||||
line.wtl = wtl;
|
||||
line.pvalue = differences.empty() ? 1.0L : static_cast<long double>(wilcoxonSignedRankTest(differences));
|
||||
line.reject = (line.pvalue < alpha_);
|
||||
|
||||
postHocResults_.push_back(std::move(line));
|
||||
}
|
||||
// Sort results by rank (descending)
|
||||
std::sort(postHocResults_.begin(), postHocResults_.end(), [](const PostHocLine& a, const PostHocLine& b) {
|
||||
return a.rank > b.rank;
|
||||
});
|
||||
}
|
||||
|
||||
// ------------------------------------------------ Wilcoxon (private) --
|
||||
static double wilcoxonSignedRankTest(const std::vector<double>& diffs)
|
||||
{
|
||||
if (diffs.empty()) return 1.0;
|
||||
|
||||
// Build |diff| + sign vector (exclude zeros)
|
||||
struct Node { double absval; int sign; };
|
||||
std::vector<Node> v;
|
||||
v.reserve(diffs.size());
|
||||
for (double d : diffs) {
|
||||
if (d != 0.0) v.push_back({ std::fabs(d), d > 0 ? 1 : -1 });
|
||||
}
|
||||
if (v.empty()) return 1.0;
|
||||
|
||||
// Sort by absolute value
|
||||
std::sort(v.begin(), v.end(), [](const Node& a, const Node& b) { return a.absval < b.absval; });
|
||||
|
||||
const double EPS = 1e-10;
|
||||
const std::size_t n = v.size();
|
||||
std::vector<double> ranks(n, 0.0);
|
||||
|
||||
std::size_t i = 0;
|
||||
while (i < n) {
|
||||
std::size_t j = i + 1;
|
||||
while (j < n && std::fabs(v[j].absval - v[i].absval) < EPS) ++j;
|
||||
double avg_rank = (i + 1 + j) * 0.5; // 1‑based ranks
|
||||
for (std::size_t k = i; k < j; ++k) ranks[k] = avg_rank;
|
||||
i = j;
|
||||
}
|
||||
|
||||
double w_plus = 0.0, w_minus = 0.0;
|
||||
for (std::size_t k = 0; k < n; ++k) {
|
||||
if (v[k].sign > 0) w_plus += ranks[k];
|
||||
else w_minus += ranks[k];
|
||||
}
|
||||
double w = std::min(w_plus, w_minus);
|
||||
double mean_w = n * (n + 1) / 4.0;
|
||||
double sd_w = std::sqrt(n * (n + 1) * (2 * n + 1) / 24.0);
|
||||
if (sd_w == 0.0) return 1.0; // degenerate (all diffs identical)
|
||||
|
||||
double z = (w - mean_w) / sd_w;
|
||||
double p_two = std::erfc(std::fabs(z) / std::sqrt(2.0)); // 2‑sided tail
|
||||
return p_two;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------- data ----
|
||||
std::vector<std::string> models_;
|
||||
std::vector<std::string> datasets_;
|
||||
json data_;
|
||||
double alpha_;
|
||||
|
||||
Matrix auc_; // [model][dataset]
|
||||
std::vector<double> avg_auc_; // mean AUC per model
|
||||
std::vector<double> avg_rank_; // mean rank per model
|
||||
std::vector<double> rank_sum_; // helper for ranks
|
||||
std::vector<int> rank_cnt_; // datasets counted per model
|
||||
|
||||
int control_idx_ = -1;
|
||||
std::vector<PostHocLine> postHocResults_;
|
||||
};
|
||||
|
||||
} // namespace platform
|
||||
#endif // BEST_WILCOXON_TEST_HPP
|
@@ -4,16 +4,18 @@
|
||||
#include "main/modelRegister.h"
|
||||
#include "common/Paths.h"
|
||||
#include "common/Colors.h"
|
||||
#include "common/Utils.h"
|
||||
#include "best/BestResults.h"
|
||||
#include "common/DotEnv.h"
|
||||
#include "config_platform.h"
|
||||
|
||||
void manageArguments(argparse::ArgumentParser& program)
|
||||
{
|
||||
program.add_argument("-m", "--model")
|
||||
.help("Model to use or any")
|
||||
.default_value("any");
|
||||
auto env = platform::DotEnv();
|
||||
program.add_argument("-m", "--model").help("Model to use or any").default_value("any");
|
||||
program.add_argument("--folder").help("Results folder to use").default_value(platform::Paths::results());
|
||||
program.add_argument("-d", "--dataset").default_value("any").help("Filter results of the selected model) (any for all datasets)");
|
||||
program.add_argument("-s", "--score").default_value("accuracy").help("Filter results of the score name supplied");
|
||||
program.add_argument("-s", "--score").default_value(env.get("score")).help("Filter results of the score name supplied");
|
||||
program.add_argument("--friedman").help("Friedman test").default_value(false).implicit_value(true);
|
||||
program.add_argument("--excel").help("Output to excel").default_value(false).implicit_value(true);
|
||||
program.add_argument("--tex").help("Output results to TeX & Markdown files").default_value(false).implicit_value(true);
|
||||
@@ -38,12 +40,16 @@ int main(int argc, char** argv)
|
||||
{
|
||||
argparse::ArgumentParser program("b_best", { platform_project_version.begin(), platform_project_version.end() });
|
||||
manageArguments(program);
|
||||
std::string model, dataset, score;
|
||||
std::string model, dataset, score, folder;
|
||||
bool build, report, friedman, excel, tex, index;
|
||||
double level;
|
||||
try {
|
||||
program.parse_args(argc, argv);
|
||||
model = program.get<std::string>("model");
|
||||
folder = program.get<std::string>("folder");
|
||||
if (folder.back() != '/') {
|
||||
folder += '/';
|
||||
}
|
||||
dataset = program.get<std::string>("dataset");
|
||||
score = program.get<std::string>("score");
|
||||
friedman = program.get<bool>("friedman");
|
||||
@@ -66,7 +72,7 @@ int main(int argc, char** argv)
|
||||
exit(1);
|
||||
}
|
||||
// Generate report
|
||||
auto results = platform::BestResults(platform::Paths::results(), score, model, dataset, friedman, level);
|
||||
auto results = platform::BestResults(folder, score, model, dataset, friedman, level);
|
||||
if (model == "any") {
|
||||
results.buildAll();
|
||||
results.reportAll(excel, tex, index);
|
||||
@@ -75,6 +81,11 @@ int main(int argc, char** argv)
|
||||
std::cout << Colors::GREEN() << fileName << " created!" << Colors::RESET() << std::endl;
|
||||
results.reportSingle(excel);
|
||||
}
|
||||
if (excel) {
|
||||
auto fileName = results.getExcelFileName();
|
||||
std::cout << "Opening " << fileName << std::endl;
|
||||
platform::openFile(fileName);
|
||||
}
|
||||
std::cout << Colors::RESET();
|
||||
return 0;
|
||||
}
|
||||
|
@@ -232,6 +232,7 @@ void experiment(argparse::ArgumentParser& program)
|
||||
struct platform::ConfigGrid config;
|
||||
auto arguments = platform::ArgumentsExperiment(program, platform::experiment_t::GRID);
|
||||
arguments.parse();
|
||||
auto path_results = arguments.getPathResults();
|
||||
auto grid_experiment = platform::GridExperiment(arguments, config);
|
||||
platform::Timer timer;
|
||||
timer.start();
|
||||
@@ -250,7 +251,7 @@ void experiment(argparse::ArgumentParser& program)
|
||||
auto duration = timer.getDuration();
|
||||
experiment.setDuration(duration);
|
||||
if (grid_experiment.haveToSaveResults()) {
|
||||
experiment.saveResult();
|
||||
experiment.saveResult(path_results);
|
||||
}
|
||||
experiment.report();
|
||||
std::cout << "Process took " << duration << std::endl;
|
||||
|
@@ -8,6 +8,7 @@
|
||||
#include "common/Paths.h"
|
||||
#include "common/Colors.h"
|
||||
#include "common/Datasets.h"
|
||||
#include "common/Utils.h"
|
||||
#include "reports/DatasetsExcel.h"
|
||||
#include "reports/DatasetsConsole.h"
|
||||
#include "results/ResultsDatasetConsole.h"
|
||||
@@ -24,9 +25,13 @@ void list_datasets(argparse::ArgumentParser& program)
|
||||
std::cout << report.getOutput();
|
||||
if (excel) {
|
||||
auto data = report.getData();
|
||||
auto report = platform::DatasetsExcel();
|
||||
report.report(data);
|
||||
std::cout << std::endl << Colors::GREEN() << "Output saved in " << report.getFileName() << std::endl;
|
||||
auto ereport = new platform::DatasetsExcel();
|
||||
ereport->report(data);
|
||||
std::cout << std::endl << Colors::GREEN() << "Output saved in " << ereport->getFileName() << std::endl;
|
||||
auto fileName = ereport->getExcelFileName();
|
||||
delete ereport;
|
||||
std::cout << "Opening " << fileName << std::endl;
|
||||
platform::openFile(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,9 +47,13 @@ void list_results(argparse::ArgumentParser& program)
|
||||
std::cout << report.getOutput();
|
||||
if (excel) {
|
||||
auto data = report.getData();
|
||||
auto report = platform::ResultsDatasetExcel();
|
||||
report.report(data);
|
||||
std::cout << std::endl << Colors::GREEN() << "Output saved in " << report.getFileName() << std::endl;
|
||||
auto ereport = new platform::ResultsDatasetExcel();
|
||||
ereport->report(data);
|
||||
std::cout << std::endl << Colors::GREEN() << "Output saved in " << ereport->getFileName() << std::endl;
|
||||
auto fileName = ereport->getExcelFileName();
|
||||
delete ereport;
|
||||
std::cout << "Opening " << fileName << std::endl;
|
||||
platform::openFile(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -18,6 +18,7 @@ int main(int argc, char** argv)
|
||||
*/
|
||||
// Initialize the experiment class with the command line arguments
|
||||
auto experiment = arguments.initializedExperiment();
|
||||
auto path_results = arguments.getPathResults();
|
||||
platform::Timer timer;
|
||||
timer.start();
|
||||
experiment.go();
|
||||
@@ -27,7 +28,7 @@ int main(int argc, char** argv)
|
||||
experiment.report();
|
||||
}
|
||||
if (arguments.haveToSaveResults()) {
|
||||
experiment.saveResult();
|
||||
experiment.saveResult(path_results);
|
||||
}
|
||||
if (arguments.doGraph()) {
|
||||
experiment.saveGraph();
|
||||
|
@@ -1,7 +1,8 @@
|
||||
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <sys/ioctl.h>
|
||||
#include <utility>
|
||||
#include <unistd.h>
|
||||
#include "common/Paths.h"
|
||||
#include <argparse/argparse.hpp>
|
||||
#include "manage/ManageScreen.h"
|
||||
#include <signal.h>
|
||||
@@ -13,6 +14,7 @@ void manageArguments(argparse::ArgumentParser& program, int argc, char** argv)
|
||||
{
|
||||
program.add_argument("-m", "--model").default_value("any").help("Filter results of the selected model)");
|
||||
program.add_argument("-s", "--score").default_value("any").help("Filter results of the score name supplied");
|
||||
program.add_argument("--folder").help("Results folder to use").default_value(platform::Paths::results());
|
||||
program.add_argument("--platform").default_value("any").help("Filter results of the selected platform");
|
||||
program.add_argument("--complete").help("Show only results with all datasets").default_value(false).implicit_value(true);
|
||||
program.add_argument("--partial").help("Show only partial results").default_value(false).implicit_value(true);
|
||||
@@ -51,71 +53,17 @@ void handleResize(int sig)
|
||||
manager->updateSize(rows, cols);
|
||||
}
|
||||
|
||||
void openFile(const std::string& fileName)
|
||||
{
|
||||
// #ifdef __APPLE__
|
||||
// // macOS uses the "open" command
|
||||
// std::string command = "open";
|
||||
// #elif defined(__linux__)
|
||||
// // Linux typically uses "xdg-open"
|
||||
// std::string command = "xdg-open";
|
||||
// #else
|
||||
// // For other OSes, do nothing or handle differently
|
||||
// std::cerr << "Unsupported platform." << std::endl;
|
||||
// return;
|
||||
// #endif
|
||||
// execlp(command.c_str(), command.c_str(), fileName.c_str(), NULL);
|
||||
#ifdef __APPLE__
|
||||
const char* tool = "/usr/bin/open";
|
||||
#elif defined(__linux__)
|
||||
const char* tool = "/usr/bin/xdg-open";
|
||||
#else
|
||||
std::cerr << "Unsupported platform." << std::endl;
|
||||
return;
|
||||
#endif
|
||||
|
||||
// We'll build an argv array for execve:
|
||||
std::vector<char*> argv;
|
||||
argv.push_back(const_cast<char*>(tool)); // argv[0]
|
||||
argv.push_back(const_cast<char*>(fileName.c_str())); // argv[1]
|
||||
argv.push_back(nullptr);
|
||||
|
||||
// Make a new environment array, skipping BASH_FUNC_ variables
|
||||
std::vector<std::string> filteredEnv;
|
||||
for (char** env = environ; *env != nullptr; ++env) {
|
||||
// *env is a string like "NAME=VALUE"
|
||||
// We want to skip those starting with "BASH_FUNC_"
|
||||
if (strncmp(*env, "BASH_FUNC_", 10) == 0) {
|
||||
// skip it
|
||||
continue;
|
||||
}
|
||||
filteredEnv.push_back(*env);
|
||||
}
|
||||
|
||||
// Convert filteredEnv into a char* array
|
||||
std::vector<char*> envp;
|
||||
for (auto& var : filteredEnv) {
|
||||
envp.push_back(const_cast<char*>(var.c_str()));
|
||||
}
|
||||
envp.push_back(nullptr);
|
||||
|
||||
// Now call execve with the cleaned environment
|
||||
// NOTE: You may need a full path to the tool if it's not in PATH, or use which() logic
|
||||
// For now, let's assume "open" or "xdg-open" is found in the default PATH:
|
||||
execve(tool, argv.data(), envp.data());
|
||||
|
||||
// If we reach here, execve failed
|
||||
perror("execve failed");
|
||||
// This would terminate your current process if it's not in a child
|
||||
// Usually you'd do something like:
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto program = argparse::ArgumentParser("b_manage", { platform_project_version.begin(), platform_project_version.end() });
|
||||
manageArguments(program, argc, argv);
|
||||
std::string model = program.get<std::string>("model");
|
||||
std::string path = program.get<std::string>("folder");
|
||||
if (path.back() != '/') {
|
||||
path += '/';
|
||||
}
|
||||
std::string score = program.get<std::string>("score");
|
||||
std::string platform = program.get<std::string>("platform");
|
||||
bool complete = program.get<bool>("complete");
|
||||
@@ -125,13 +73,13 @@ int main(int argc, char** argv)
|
||||
partial = false;
|
||||
signal(SIGWINCH, handleResize);
|
||||
auto [rows, cols] = numRowsCols();
|
||||
manager = new platform::ManageScreen(rows, cols, model, score, platform, complete, partial, compare);
|
||||
manager = new platform::ManageScreen(path, rows, cols, model, score, platform, complete, partial, compare);
|
||||
manager->doMenu();
|
||||
auto fileName = manager->getExcelFileName();
|
||||
delete manager;
|
||||
if (!fileName.empty()) {
|
||||
std::cout << "Opening " << fileName << std::endl;
|
||||
openFile(fileName);
|
||||
platform::openFile(fileName);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#include <ArffFiles.hpp>
|
||||
#include <ArffFiles/ArffFiles.hpp>
|
||||
#include <fstream>
|
||||
#include "Dataset.h"
|
||||
namespace platform {
|
||||
|
@@ -49,6 +49,7 @@ namespace platform {
|
||||
return "BestResults_" + score + ".xlsx";
|
||||
}
|
||||
static std::string excelResults() { return "some_results.xlsx"; }
|
||||
static std::string excelDatasets() { return "datasets.xlsx"; }
|
||||
static std::string grid_input(const std::string& model)
|
||||
{
|
||||
return grid() + "grid_" + model + "_input.json";
|
||||
@@ -73,6 +74,7 @@ namespace platform {
|
||||
{
|
||||
return "post_hoc.md";
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -1,5 +1,7 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -66,5 +68,64 @@ namespace platform {
|
||||
oss << std::put_time(timeinfo, "%H:%M:%S");
|
||||
return oss.str();
|
||||
}
|
||||
static void openFile(const std::string& fileName)
|
||||
{
|
||||
// #ifdef __APPLE__
|
||||
// // macOS uses the "open" command
|
||||
// std::string command = "open";
|
||||
// #elif defined(__linux__)
|
||||
// // Linux typically uses "xdg-open"
|
||||
// std::string command = "xdg-open";
|
||||
// #else
|
||||
// // For other OSes, do nothing or handle differently
|
||||
// std::cerr << "Unsupported platform." << std::endl;
|
||||
// return;
|
||||
// #endif
|
||||
// execlp(command.c_str(), command.c_str(), fileName.c_str(), NULL);
|
||||
#ifdef __APPLE__
|
||||
const char* tool = "/usr/bin/open";
|
||||
#elif defined(__linux__)
|
||||
const char* tool = "/usr/bin/xdg-open";
|
||||
#else
|
||||
std::cerr << "Unsupported platform." << std::endl;
|
||||
return;
|
||||
#endif
|
||||
|
||||
// We'll build an argv array for execve:
|
||||
std::vector<char*> argv;
|
||||
argv.push_back(const_cast<char*>(tool)); // argv[0]
|
||||
argv.push_back(const_cast<char*>(fileName.c_str())); // argv[1]
|
||||
argv.push_back(nullptr);
|
||||
|
||||
// Make a new environment array, skipping BASH_FUNC_ variables
|
||||
std::vector<std::string> filteredEnv;
|
||||
for (char** env = environ; *env != nullptr; ++env) {
|
||||
// *env is a string like "NAME=VALUE"
|
||||
// We want to skip those starting with "BASH_FUNC_"
|
||||
if (strncmp(*env, "BASH_FUNC_", 10) == 0) {
|
||||
// skip it
|
||||
continue;
|
||||
}
|
||||
filteredEnv.push_back(*env);
|
||||
}
|
||||
|
||||
// Convert filteredEnv into a char* array
|
||||
std::vector<char*> envp;
|
||||
for (auto& var : filteredEnv) {
|
||||
envp.push_back(const_cast<char*>(var.c_str()));
|
||||
}
|
||||
envp.push_back(nullptr);
|
||||
|
||||
// Now call execve with the cleaned environment
|
||||
// NOTE: You may need a full path to the tool if it's not in PATH, or use which() logic
|
||||
// For now, let's assume "open" or "xdg-open" is found in the default PATH:
|
||||
execve(tool, argv.data(), envp.data());
|
||||
|
||||
// If we reach here, execve failed
|
||||
perror("execve failed");
|
||||
// This would terminate your current process if it's not in a child
|
||||
// Usually you'd do something like:
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
#endif
|
492
src/experimental_clfs/AdaBoost.cpp
Normal file
492
src/experimental_clfs/AdaBoost.cpp
Normal file
@@ -0,0 +1,492 @@
|
||||
// ***************************************************************
|
||||
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
||||
// SPDX-FileType: SOURCE
|
||||
// SPDX-License-Identifier: MIT
|
||||
// ***************************************************************
|
||||
|
||||
#include "AdaBoost.h"
|
||||
#include "DecisionTree.h"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include "TensorUtils.hpp"
|
||||
|
||||
// Conditional debug macro for performance-critical sections
|
||||
#define DEBUG_LOG(condition, ...) \
|
||||
do { \
|
||||
if (__builtin_expect((condition), 0)) { \
|
||||
std::cout << __VA_ARGS__ << std::endl; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
namespace bayesnet {
|
||||
|
||||
AdaBoost::AdaBoost(int n_estimators, int max_depth)
|
||||
: Ensemble(true), n_estimators(n_estimators), base_max_depth(max_depth), n(0), n_classes(0)
|
||||
{
|
||||
validHyperparameters = { "n_estimators", "base_max_depth" };
|
||||
}
|
||||
|
||||
// Versión optimizada de buildModel - Reemplazar en AdaBoost.cpp:
|
||||
|
||||
void AdaBoost::buildModel(const torch::Tensor& weights)
|
||||
{
|
||||
// Initialize variables
|
||||
models.clear();
|
||||
alphas.clear();
|
||||
training_errors.clear();
|
||||
|
||||
// Initialize n (number of features) and n_classes
|
||||
n = dataset.size(0) - 1; // Exclude the label row
|
||||
n_classes = states[className].size();
|
||||
|
||||
// Initialize sample weights uniformly
|
||||
int n_samples = dataset.size(1);
|
||||
sample_weights = torch::ones({ n_samples }) / n_samples;
|
||||
|
||||
// If initial weights are provided, incorporate them
|
||||
if (weights.defined() && weights.numel() > 0) {
|
||||
if (weights.size(0) != n_samples) {
|
||||
throw std::runtime_error("weights must have the same length as number of samples");
|
||||
}
|
||||
sample_weights = weights.clone();
|
||||
normalizeWeights();
|
||||
}
|
||||
|
||||
// Conditional debug information (only when debug is enabled)
|
||||
DEBUG_LOG(debug, "Starting AdaBoost training with " << n_estimators << " estimators\n"
|
||||
<< "Number of classes: " << n_classes << "\n"
|
||||
<< "Number of features: " << n << "\n"
|
||||
<< "Number of samples: " << n_samples);
|
||||
|
||||
// Pre-compute random guess error threshold
|
||||
const double random_guess_error = 1.0 - (1.0 / static_cast<double>(n_classes));
|
||||
|
||||
// Main AdaBoost training loop (SAMME algorithm)
|
||||
for (int iter = 0; iter < n_estimators; ++iter) {
|
||||
// Train base estimator with current sample weights
|
||||
auto estimator = trainBaseEstimator(sample_weights);
|
||||
|
||||
// Calculate weighted error
|
||||
double weighted_error = calculateWeightedError(estimator.get(), sample_weights);
|
||||
training_errors.push_back(weighted_error);
|
||||
|
||||
// According to SAMME, we need error < random_guess_error
|
||||
if (weighted_error >= random_guess_error) {
|
||||
DEBUG_LOG(debug, "Error >= random guess (" << random_guess_error << "), stopping");
|
||||
// If only one estimator and it's worse than random, keep it with zero weight
|
||||
if (models.empty()) {
|
||||
models.push_back(std::move(estimator));
|
||||
alphas.push_back(0.0);
|
||||
}
|
||||
break; // Stop boosting
|
||||
}
|
||||
|
||||
// Check for perfect classification BEFORE calculating alpha
|
||||
if (weighted_error <= 1e-10) {
|
||||
DEBUG_LOG(debug, "Perfect classification achieved (error=" << weighted_error << ")");
|
||||
|
||||
// For perfect classification, use a large but finite alpha
|
||||
double alpha = 10.0 + std::log(static_cast<double>(n_classes - 1));
|
||||
|
||||
// Store the estimator and its weight
|
||||
models.push_back(std::move(estimator));
|
||||
alphas.push_back(alpha);
|
||||
|
||||
DEBUG_LOG(debug, "Iteration " << iter << ":\n"
|
||||
<< " Weighted error: " << weighted_error << "\n"
|
||||
<< " Alpha (finite): " << alpha << "\n"
|
||||
<< " Random guess error: " << random_guess_error);
|
||||
|
||||
break; // Stop training as we have a perfect classifier
|
||||
}
|
||||
|
||||
// Calculate alpha (estimator weight) using SAMME formula
|
||||
// alpha = log((1 - err) / err) + log(K - 1)
|
||||
// Clamp weighted_error to avoid division by zero and infinite alpha
|
||||
double clamped_error = std::max(1e-15, std::min(1.0 - 1e-15, weighted_error));
|
||||
double alpha = std::log((1.0 - clamped_error) / clamped_error) +
|
||||
std::log(static_cast<double>(n_classes - 1));
|
||||
|
||||
// Clamp alpha to reasonable bounds to avoid numerical issues
|
||||
alpha = std::max(-10.0, std::min(10.0, alpha));
|
||||
|
||||
// Store the estimator and its weight
|
||||
models.push_back(std::move(estimator));
|
||||
alphas.push_back(alpha);
|
||||
|
||||
// Update sample weights (only if this is not the last iteration)
|
||||
if (iter < n_estimators - 1) {
|
||||
updateSampleWeights(models.back().get(), alpha);
|
||||
normalizeWeights();
|
||||
}
|
||||
|
||||
DEBUG_LOG(debug, "Iteration " << iter << ":\n"
|
||||
<< " Weighted error: " << weighted_error << "\n"
|
||||
<< " Alpha: " << alpha << "\n"
|
||||
<< " Random guess error: " << random_guess_error);
|
||||
}
|
||||
|
||||
// Set the number of models actually trained
|
||||
n_models = models.size();
|
||||
DEBUG_LOG(debug, "AdaBoost training completed with " << n_models << " models");
|
||||
}
|
||||
|
||||
void AdaBoost::trainModel(const torch::Tensor& weights, const Smoothing_t smoothing)
|
||||
{
|
||||
// Call buildModel which does the actual training
|
||||
buildModel(weights);
|
||||
fitted = true;
|
||||
}
|
||||
|
||||
std::unique_ptr<Classifier> AdaBoost::trainBaseEstimator(const torch::Tensor& weights)
|
||||
{
|
||||
// Create a decision tree with specified max depth
|
||||
auto tree = std::make_unique<DecisionTree>(base_max_depth);
|
||||
|
||||
// Ensure weights are properly normalized
|
||||
auto normalized_weights = weights / weights.sum();
|
||||
|
||||
// Fit the tree with the current sample weights
|
||||
tree->fit(dataset, features, className, states, normalized_weights, Smoothing_t::NONE);
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
double AdaBoost::calculateWeightedError(Classifier* estimator, const torch::Tensor& weights)
|
||||
{
|
||||
// Get features and labels from dataset (avoid repeated indexing)
|
||||
auto X = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), torch::indexing::Slice() });
|
||||
auto y_true = dataset.index({ -1, torch::indexing::Slice() });
|
||||
|
||||
// Get predictions from the estimator
|
||||
auto y_pred = estimator->predict(X);
|
||||
|
||||
// Vectorized error calculation using PyTorch operations
|
||||
auto incorrect = (y_pred != y_true).to(torch::kDouble);
|
||||
|
||||
// Direct dot product for weighted error (more efficient than sum)
|
||||
double weighted_error = torch::dot(incorrect, weights).item<double>();
|
||||
|
||||
// Clamp to valid range in one operation
|
||||
return std::clamp(weighted_error, 1e-15, 1.0 - 1e-15);
|
||||
}
|
||||
|
||||
void AdaBoost::updateSampleWeights(Classifier* estimator, double alpha)
|
||||
{
|
||||
// Get predictions from the estimator (reuse from calculateWeightedError if possible)
|
||||
auto X = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), torch::indexing::Slice() });
|
||||
auto y_true = dataset.index({ -1, torch::indexing::Slice() });
|
||||
auto y_pred = estimator->predict(X);
|
||||
|
||||
// Vectorized weight update using PyTorch operations
|
||||
auto incorrect = (y_pred != y_true).to(torch::kDouble);
|
||||
|
||||
// Single vectorized operation instead of element-wise multiplication
|
||||
sample_weights *= torch::exp(alpha * incorrect);
|
||||
|
||||
// Vectorized clamping for numerical stability
|
||||
sample_weights = torch::clamp(sample_weights, 1e-15, 1e15);
|
||||
}
|
||||
|
||||
void AdaBoost::normalizeWeights()
|
||||
{
|
||||
// Single-pass normalization using PyTorch operations
|
||||
double sum_weights = torch::sum(sample_weights).item<double>();
|
||||
|
||||
if (__builtin_expect(sum_weights <= 0, 0)) {
|
||||
// Reset to uniform if all weights are zero/negative (rare case)
|
||||
sample_weights = torch::ones_like(sample_weights) / sample_weights.size(0);
|
||||
} else {
|
||||
// Vectorized normalization
|
||||
sample_weights /= sum_weights;
|
||||
|
||||
// Vectorized minimum weight enforcement
|
||||
sample_weights = torch::clamp_min(sample_weights, 1e-15);
|
||||
|
||||
// Renormalize after clamping (if any weights were clamped)
|
||||
double new_sum = torch::sum(sample_weights).item<double>();
|
||||
if (new_sum != 1.0) {
|
||||
sample_weights /= new_sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> AdaBoost::graph(const std::string& title) const
|
||||
{
|
||||
// Create a graph representation of the AdaBoost ensemble
|
||||
std::vector<std::string> graph_lines;
|
||||
|
||||
// Header
|
||||
graph_lines.push_back("digraph AdaBoost {");
|
||||
graph_lines.push_back(" rankdir=TB;");
|
||||
graph_lines.push_back(" node [shape=box];");
|
||||
|
||||
if (!title.empty()) {
|
||||
graph_lines.push_back(" label=\"" + title + "\";");
|
||||
graph_lines.push_back(" labelloc=t;");
|
||||
}
|
||||
|
||||
// Add input node
|
||||
graph_lines.push_back(" Input [shape=ellipse, label=\"Input Features\"];");
|
||||
|
||||
// Add base estimators
|
||||
for (size_t i = 0; i < models.size(); ++i) {
|
||||
std::stringstream ss;
|
||||
ss << " Estimator" << i << " [label=\"Base Estimator " << i + 1
|
||||
<< "\\nα = " << std::fixed << std::setprecision(3) << alphas[i] << "\"];";
|
||||
graph_lines.push_back(ss.str());
|
||||
|
||||
// Connect input to estimator
|
||||
ss.str("");
|
||||
ss << " Input -> Estimator" << i << ";";
|
||||
graph_lines.push_back(ss.str());
|
||||
}
|
||||
|
||||
// Add combination node
|
||||
graph_lines.push_back(" Combination [shape=diamond, label=\"Weighted Vote\"];");
|
||||
|
||||
// Connect estimators to combination
|
||||
for (size_t i = 0; i < models.size(); ++i) {
|
||||
std::stringstream ss;
|
||||
ss << " Estimator" << i << " -> Combination;";
|
||||
graph_lines.push_back(ss.str());
|
||||
}
|
||||
|
||||
// Add output node
|
||||
graph_lines.push_back(" Output [shape=ellipse, label=\"Final Prediction\"];");
|
||||
graph_lines.push_back(" Combination -> Output;");
|
||||
|
||||
// Close graph
|
||||
graph_lines.push_back("}");
|
||||
|
||||
return graph_lines;
|
||||
}
|
||||
|
||||
void AdaBoost::checkValues() const
|
||||
{
|
||||
if (n_estimators <= 0) {
|
||||
throw std::invalid_argument("n_estimators must be positive");
|
||||
}
|
||||
if (base_max_depth <= 0) {
|
||||
throw std::invalid_argument("base_max_depth must be positive");
|
||||
}
|
||||
}
|
||||
|
||||
void AdaBoost::setHyperparameters(const nlohmann::json& hyperparameters_)
|
||||
{
|
||||
auto hyperparameters = hyperparameters_;
|
||||
// Set hyperparameters from JSON
|
||||
auto it = hyperparameters.find("n_estimators");
|
||||
if (it != hyperparameters.end()) {
|
||||
n_estimators = it->get<int>();
|
||||
hyperparameters.erase("n_estimators");
|
||||
}
|
||||
|
||||
it = hyperparameters.find("base_max_depth");
|
||||
if (it != hyperparameters.end()) {
|
||||
base_max_depth = it->get<int>();
|
||||
hyperparameters.erase("base_max_depth");
|
||||
}
|
||||
checkValues();
|
||||
Ensemble::setHyperparameters(hyperparameters);
|
||||
}
|
||||
|
||||
int AdaBoost::predictSample(const torch::Tensor& x) const
|
||||
{
|
||||
// Early validation (keep essential checks only)
|
||||
if (!fitted || models.empty()) {
|
||||
throw std::runtime_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
|
||||
// Pre-allocate and reuse memory
|
||||
static thread_local std::vector<double> class_votes_cache;
|
||||
if (class_votes_cache.size() != static_cast<size_t>(n_classes)) {
|
||||
class_votes_cache.resize(n_classes);
|
||||
}
|
||||
std::fill(class_votes_cache.begin(), class_votes_cache.end(), 0.0);
|
||||
|
||||
// Optimized voting loop - avoid exception handling in hot path
|
||||
for (size_t i = 0; i < models.size(); ++i) {
|
||||
double alpha = alphas[i];
|
||||
if (alpha <= 0 || !std::isfinite(alpha)) continue;
|
||||
|
||||
// Direct cast and call - avoid virtual dispatch overhead
|
||||
int predicted_class = static_cast<DecisionTree*>(models[i].get())->predictSample(x);
|
||||
|
||||
// Bounds check with branch prediction hint
|
||||
if (__builtin_expect(predicted_class >= 0 && predicted_class < n_classes, 1)) {
|
||||
class_votes_cache[predicted_class] += alpha;
|
||||
}
|
||||
}
|
||||
|
||||
// Fast argmax using iterators
|
||||
return std::distance(class_votes_cache.begin(),
|
||||
std::max_element(class_votes_cache.begin(), class_votes_cache.end()));
|
||||
}
|
||||
|
||||
torch::Tensor AdaBoost::predictProbaSample(const torch::Tensor& x) const
|
||||
{
|
||||
// Early validation
|
||||
if (!fitted || models.empty()) {
|
||||
throw std::runtime_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
|
||||
// Use stack allocation for small arrays (typical case: n_classes <= 32)
|
||||
constexpr int STACK_THRESHOLD = 32;
|
||||
double stack_votes[STACK_THRESHOLD];
|
||||
std::vector<double> heap_votes;
|
||||
double* class_votes;
|
||||
|
||||
if (n_classes <= STACK_THRESHOLD) {
|
||||
class_votes = stack_votes;
|
||||
std::fill_n(class_votes, n_classes, 0.0);
|
||||
} else {
|
||||
heap_votes.resize(n_classes, 0.0);
|
||||
class_votes = heap_votes.data();
|
||||
}
|
||||
|
||||
double total_votes = 0.0;
|
||||
|
||||
// Optimized voting loop
|
||||
for (size_t i = 0; i < models.size(); ++i) {
|
||||
double alpha = alphas[i];
|
||||
if (alpha <= 0 || !std::isfinite(alpha)) continue;
|
||||
|
||||
int predicted_class = static_cast<DecisionTree*>(models[i].get())->predictSample(x);
|
||||
|
||||
if (__builtin_expect(predicted_class >= 0 && predicted_class < n_classes, 1)) {
|
||||
class_votes[predicted_class] += alpha;
|
||||
total_votes += alpha;
|
||||
}
|
||||
}
|
||||
|
||||
// Direct tensor creation with pre-computed size
|
||||
torch::Tensor class_probs = torch::empty({ n_classes }, torch::TensorOptions().dtype(torch::kFloat32));
|
||||
auto probs_accessor = class_probs.accessor<float, 1>();
|
||||
|
||||
if (__builtin_expect(total_votes > 0.0, 1)) {
|
||||
// Vectorized probability calculation
|
||||
const double inv_total = 1.0 / total_votes;
|
||||
for (int j = 0; j < n_classes; ++j) {
|
||||
probs_accessor[j] = static_cast<float>(class_votes[j] * inv_total);
|
||||
}
|
||||
} else {
|
||||
// Uniform distribution fallback
|
||||
const float uniform_prob = 1.0f / n_classes;
|
||||
for (int j = 0; j < n_classes; ++j) {
|
||||
probs_accessor[j] = uniform_prob;
|
||||
}
|
||||
}
|
||||
|
||||
return class_probs;
|
||||
}
|
||||
|
||||
torch::Tensor AdaBoost::predict_proba(torch::Tensor& X)
|
||||
{
|
||||
if (!fitted || models.empty()) {
|
||||
throw std::runtime_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
|
||||
// Input validation
|
||||
if (X.size(0) != n) {
|
||||
throw std::runtime_error("Input has wrong number of features. Expected " +
|
||||
std::to_string(n) + " but got " + std::to_string(X.size(0)));
|
||||
}
|
||||
|
||||
const int n_samples = X.size(1);
|
||||
|
||||
// Pre-allocate output tensor with correct layout
|
||||
torch::Tensor probabilities = torch::empty({ n_samples, n_classes },
|
||||
torch::TensorOptions().dtype(torch::kFloat32));
|
||||
|
||||
// Convert to contiguous memory if needed (optimization for memory access)
|
||||
if (!X.is_contiguous()) {
|
||||
X = X.contiguous();
|
||||
}
|
||||
|
||||
// Batch processing with memory-efficient sample extraction
|
||||
for (int i = 0; i < n_samples; ++i) {
|
||||
// Extract sample without unnecessary copies
|
||||
auto sample = X.select(1, i);
|
||||
|
||||
// Direct assignment to pre-allocated tensor
|
||||
probabilities[i] = predictProbaSample(sample);
|
||||
}
|
||||
|
||||
return probabilities;
|
||||
}
|
||||
|
||||
std::vector<std::vector<double>> AdaBoost::predict_proba(std::vector<std::vector<int>>& X)
|
||||
{
|
||||
const size_t n_samples = X[0].size();
|
||||
|
||||
// Pre-allocate result with exact size
|
||||
std::vector<std::vector<double>> result;
|
||||
result.reserve(n_samples);
|
||||
|
||||
// Avoid repeated allocations
|
||||
for (size_t i = 0; i < n_samples; ++i) {
|
||||
result.emplace_back(n_classes, 0.0);
|
||||
}
|
||||
|
||||
// Convert to tensor only once (batch conversion is more efficient)
|
||||
torch::Tensor X_tensor = platform::TensorUtils::to_matrix(X);
|
||||
torch::Tensor proba_tensor = predict_proba(X_tensor);
|
||||
|
||||
// Optimized tensor-to-vector conversion
|
||||
auto proba_accessor = proba_tensor.accessor<float, 2>();
|
||||
for (size_t i = 0; i < n_samples; ++i) {
|
||||
for (int j = 0; j < n_classes; ++j) {
|
||||
result[i][j] = static_cast<double>(proba_accessor[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
torch::Tensor AdaBoost::predict(torch::Tensor& X)
|
||||
{
|
||||
if (!fitted || models.empty()) {
|
||||
throw std::runtime_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
|
||||
if (X.size(0) != n) {
|
||||
throw std::runtime_error("Input has wrong number of features. Expected " +
|
||||
std::to_string(n) + " but got " + std::to_string(X.size(0)));
|
||||
}
|
||||
|
||||
const int n_samples = X.size(1);
|
||||
|
||||
// Pre-allocate with correct dtype
|
||||
torch::Tensor predictions = torch::empty({ n_samples }, torch::TensorOptions().dtype(torch::kInt32));
|
||||
auto pred_accessor = predictions.accessor<int32_t, 1>();
|
||||
|
||||
// Ensure contiguous memory layout
|
||||
if (!X.is_contiguous()) {
|
||||
X = X.contiguous();
|
||||
}
|
||||
|
||||
// Optimized prediction loop
|
||||
for (int i = 0; i < n_samples; ++i) {
|
||||
auto sample = X.select(1, i);
|
||||
pred_accessor[i] = predictSample(sample);
|
||||
}
|
||||
|
||||
return predictions;
|
||||
}
|
||||
|
||||
std::vector<int> AdaBoost::predict(std::vector<std::vector<int>>& X)
|
||||
{
|
||||
// Single tensor conversion for batch processing
|
||||
torch::Tensor X_tensor = platform::TensorUtils::to_matrix(X);
|
||||
torch::Tensor predictions_tensor = predict(X_tensor);
|
||||
|
||||
// Optimized tensor-to-vector conversion
|
||||
std::vector<int> result = platform::TensorUtils::to_vector<int>(predictions_tensor);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace bayesnet
|
81
src/experimental_clfs/AdaBoost.h
Normal file
81
src/experimental_clfs/AdaBoost.h
Normal file
@@ -0,0 +1,81 @@
|
||||
// ***************************************************************
|
||||
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
||||
// SPDX-FileType: SOURCE
|
||||
// SPDX-License-Identifier: MIT
|
||||
// ***************************************************************
|
||||
|
||||
#ifndef ADABOOST_H
|
||||
#define ADABOOST_H
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "bayesnet/ensembles/Ensemble.h"
|
||||
|
||||
namespace bayesnet {
|
||||
class AdaBoost : public Ensemble {
|
||||
public:
|
||||
explicit AdaBoost(int n_estimators = 100, int max_depth = 1);
|
||||
virtual ~AdaBoost() = default;
|
||||
|
||||
// Override base class methods
|
||||
std::vector<std::string> graph(const std::string& title = "") const override;
|
||||
|
||||
// AdaBoost specific methods
|
||||
void setNEstimators(int n_estimators) { this->n_estimators = n_estimators; checkValues(); }
|
||||
int getNEstimators() const { return n_estimators; }
|
||||
void setBaseMaxDepth(int depth) { this->base_max_depth = depth; checkValues(); }
|
||||
int getBaseMaxDepth() const { return base_max_depth; }
|
||||
|
||||
// Get the weight of each base estimator
|
||||
std::vector<double> getEstimatorWeights() const { return alphas; }
|
||||
|
||||
// Get training errors for each iteration
|
||||
std::vector<double> getTrainingErrors() const { return training_errors; }
|
||||
|
||||
// Override setHyperparameters from BaseClassifier
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||
|
||||
torch::Tensor predict(torch::Tensor& X) override;
|
||||
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
||||
torch::Tensor predict_proba(torch::Tensor& X) override;
|
||||
std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X);
|
||||
void setDebug(bool debug) { this->debug = debug; }
|
||||
|
||||
protected:
|
||||
void buildModel(const torch::Tensor& weights) override;
|
||||
void trainModel(const torch::Tensor& weights, const Smoothing_t smoothing) override;
|
||||
|
||||
private:
|
||||
int n_estimators;
|
||||
int base_max_depth; // Max depth for base decision trees
|
||||
std::vector<double> alphas; // Weight of each base estimator
|
||||
std::vector<double> training_errors; // Training error at each iteration
|
||||
torch::Tensor sample_weights; // Current sample weights
|
||||
int n_classes; // Number of classes in the target variable
|
||||
int n; // Number of features
|
||||
|
||||
// Train a single base estimator
|
||||
std::unique_ptr<Classifier> trainBaseEstimator(const torch::Tensor& weights);
|
||||
|
||||
// Calculate weighted error
|
||||
double calculateWeightedError(Classifier* estimator, const torch::Tensor& weights);
|
||||
|
||||
// Update sample weights based on predictions
|
||||
void updateSampleWeights(Classifier* estimator, double alpha);
|
||||
|
||||
// Normalize weights to sum to 1
|
||||
void normalizeWeights();
|
||||
|
||||
// Check if hyperparameters values are valid
|
||||
void checkValues() const;
|
||||
|
||||
// Make predictions for a single sample
|
||||
int predictSample(const torch::Tensor& x) const;
|
||||
|
||||
// Make probabilistic predictions for a single sample
|
||||
torch::Tensor predictProbaSample(const torch::Tensor& x) const;
|
||||
bool debug = false; // Enable debug mode for debug output
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ADABOOST_H
|
495
src/experimental_clfs/DecisionTree.cpp
Normal file
495
src/experimental_clfs/DecisionTree.cpp
Normal file
@@ -0,0 +1,495 @@
|
||||
// ***************************************************************
|
||||
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
||||
// SPDX-FileType: SOURCE
|
||||
// SPDX-License-Identifier: MIT
|
||||
// ***************************************************************
|
||||
|
||||
#include "DecisionTree.h"
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
#include "TensorUtils.hpp"
|
||||
|
||||
namespace bayesnet {
|
||||
|
||||
DecisionTree::DecisionTree(int max_depth, int min_samples_split, int min_samples_leaf)
|
||||
: Classifier(Network()), max_depth(max_depth),
|
||||
min_samples_split(min_samples_split), min_samples_leaf(min_samples_leaf)
|
||||
{
|
||||
validHyperparameters = { "max_depth", "min_samples_split", "min_samples_leaf" };
|
||||
}
|
||||
|
||||
void DecisionTree::setHyperparameters(const nlohmann::json& hyperparameters_)
|
||||
{
|
||||
auto hyperparameters = hyperparameters_;
|
||||
// Set hyperparameters from JSON
|
||||
auto it = hyperparameters.find("max_depth");
|
||||
if (it != hyperparameters.end()) {
|
||||
max_depth = it->get<int>();
|
||||
hyperparameters.erase("max_depth"); // Remove 'order' if present
|
||||
}
|
||||
|
||||
it = hyperparameters.find("min_samples_split");
|
||||
if (it != hyperparameters.end()) {
|
||||
min_samples_split = it->get<int>();
|
||||
hyperparameters.erase("min_samples_split"); // Remove 'min_samples_split' if present
|
||||
}
|
||||
|
||||
it = hyperparameters.find("min_samples_leaf");
|
||||
if (it != hyperparameters.end()) {
|
||||
min_samples_leaf = it->get<int>();
|
||||
hyperparameters.erase("min_samples_leaf"); // Remove 'min_samples_leaf' if present
|
||||
}
|
||||
Classifier::setHyperparameters(hyperparameters);
|
||||
checkValues();
|
||||
}
|
||||
void DecisionTree::checkValues()
|
||||
{
|
||||
if (max_depth <= 0) {
|
||||
throw std::invalid_argument("max_depth must be positive");
|
||||
}
|
||||
if (min_samples_leaf <= 0) {
|
||||
throw std::invalid_argument("min_samples_leaf must be positive");
|
||||
}
|
||||
if (min_samples_split <= 0) {
|
||||
throw std::invalid_argument("min_samples_split must be positive");
|
||||
}
|
||||
}
|
||||
void DecisionTree::buildModel(const torch::Tensor& weights)
|
||||
{
|
||||
// Extract features (X) and labels (y) from dataset
|
||||
auto X = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), torch::indexing::Slice() }).t();
|
||||
auto y = dataset.index({ -1, torch::indexing::Slice() });
|
||||
|
||||
if (X.size(0) != y.size(0)) {
|
||||
throw std::runtime_error("X and y must have the same number of samples");
|
||||
}
|
||||
|
||||
n_classes = states[className].size();
|
||||
|
||||
// Use provided weights or uniform weights
|
||||
torch::Tensor sample_weights;
|
||||
if (weights.defined() && weights.numel() > 0) {
|
||||
if (weights.size(0) != X.size(0)) {
|
||||
throw std::runtime_error("weights must have the same length as number of samples");
|
||||
}
|
||||
sample_weights = weights;
|
||||
} else {
|
||||
sample_weights = torch::ones({ X.size(0) }) / X.size(0);
|
||||
}
|
||||
|
||||
// Normalize weights
|
||||
sample_weights = sample_weights / sample_weights.sum();
|
||||
|
||||
// Build the tree
|
||||
root = buildTree(X, y, sample_weights, 0);
|
||||
|
||||
// Mark as fitted
|
||||
fitted = true;
|
||||
}
|
||||
bool DecisionTree::validateTensors(const torch::Tensor& X, const torch::Tensor& y,
|
||||
const torch::Tensor& sample_weights) const
|
||||
{
|
||||
if (X.size(0) != y.size(0) || X.size(0) != sample_weights.size(0)) {
|
||||
return false;
|
||||
}
|
||||
if (X.size(0) == 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<TreeNode> DecisionTree::buildTree(
|
||||
const torch::Tensor& X,
|
||||
const torch::Tensor& y,
|
||||
const torch::Tensor& sample_weights,
|
||||
int current_depth)
|
||||
{
|
||||
auto node = std::make_unique<TreeNode>();
|
||||
int n_samples = y.size(0);
|
||||
|
||||
// Check stopping criteria
|
||||
auto unique = at::_unique(y);
|
||||
bool should_stop = (current_depth >= max_depth) ||
|
||||
(n_samples < min_samples_split) ||
|
||||
(std::get<0>(unique).size(0) == 1); // All samples same class
|
||||
|
||||
if (should_stop || n_samples <= min_samples_leaf) {
|
||||
// Create leaf node
|
||||
node->is_leaf = true;
|
||||
|
||||
// Calculate class probabilities
|
||||
node->class_probabilities = torch::zeros({ n_classes });
|
||||
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
int class_idx = y[i].item<int>();
|
||||
node->class_probabilities[class_idx] += sample_weights[i].item<float>();
|
||||
}
|
||||
|
||||
// Normalize probabilities
|
||||
node->class_probabilities /= node->class_probabilities.sum();
|
||||
|
||||
// Set predicted class as the one with highest probability
|
||||
node->predicted_class = torch::argmax(node->class_probabilities).item<int>();
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
// Find best split
|
||||
SplitInfo best_split = findBestSplit(X, y, sample_weights);
|
||||
|
||||
// If no valid split found, create leaf
|
||||
if (best_split.feature_index == -1 || best_split.impurity_decrease <= 0) {
|
||||
node->is_leaf = true;
|
||||
|
||||
// Calculate class probabilities
|
||||
node->class_probabilities = torch::zeros({ n_classes });
|
||||
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
int class_idx = y[i].item<int>();
|
||||
node->class_probabilities[class_idx] += sample_weights[i].item<float>();
|
||||
}
|
||||
|
||||
node->class_probabilities /= node->class_probabilities.sum();
|
||||
node->predicted_class = torch::argmax(node->class_probabilities).item<int>();
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
// Create internal node
|
||||
node->is_leaf = false;
|
||||
node->split_feature = best_split.feature_index;
|
||||
node->split_value = best_split.split_value;
|
||||
|
||||
// Split data
|
||||
auto left_X = X.index({ best_split.left_mask });
|
||||
auto left_y = y.index({ best_split.left_mask });
|
||||
auto left_weights = sample_weights.index({ best_split.left_mask });
|
||||
|
||||
auto right_X = X.index({ best_split.right_mask });
|
||||
auto right_y = y.index({ best_split.right_mask });
|
||||
auto right_weights = sample_weights.index({ best_split.right_mask });
|
||||
|
||||
// Recursively build subtrees
|
||||
if (left_X.size(0) >= min_samples_leaf) {
|
||||
node->left = buildTree(left_X, left_y, left_weights, current_depth + 1);
|
||||
} else {
|
||||
// Force leaf if not enough samples
|
||||
node->left = std::make_unique<TreeNode>();
|
||||
node->left->is_leaf = true;
|
||||
auto mode = std::get<0>(torch::mode(left_y));
|
||||
node->left->predicted_class = mode.item<int>();
|
||||
node->left->class_probabilities = torch::zeros({ n_classes });
|
||||
node->left->class_probabilities[node->left->predicted_class] = 1.0;
|
||||
}
|
||||
|
||||
if (right_X.size(0) >= min_samples_leaf) {
|
||||
node->right = buildTree(right_X, right_y, right_weights, current_depth + 1);
|
||||
} else {
|
||||
// Force leaf if not enough samples
|
||||
node->right = std::make_unique<TreeNode>();
|
||||
node->right->is_leaf = true;
|
||||
auto mode = std::get<0>(torch::mode(right_y));
|
||||
node->right->predicted_class = mode.item<int>();
|
||||
node->right->class_probabilities = torch::zeros({ n_classes });
|
||||
node->right->class_probabilities[node->right->predicted_class] = 1.0;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
DecisionTree::SplitInfo DecisionTree::findBestSplit(
|
||||
const torch::Tensor& X,
|
||||
const torch::Tensor& y,
|
||||
const torch::Tensor& sample_weights)
|
||||
{
|
||||
|
||||
SplitInfo best_split;
|
||||
best_split.feature_index = -1;
|
||||
best_split.split_value = -1;
|
||||
best_split.impurity_decrease = -std::numeric_limits<double>::infinity();
|
||||
|
||||
int n_features = X.size(1);
|
||||
int n_samples = X.size(0);
|
||||
|
||||
// Calculate impurity of current node
|
||||
double current_impurity = calculateGiniImpurity(y, sample_weights);
|
||||
double total_weight = sample_weights.sum().item<double>();
|
||||
|
||||
// Try each feature
|
||||
for (int feat_idx = 0; feat_idx < n_features; feat_idx++) {
|
||||
auto feature_values = X.index({ torch::indexing::Slice(), feat_idx });
|
||||
auto unique_values = std::get<0>(torch::unique_consecutive(std::get<0>(torch::sort(feature_values))));
|
||||
|
||||
// Try each unique value as split point
|
||||
for (int i = 0; i < unique_values.size(0); i++) {
|
||||
int split_val = unique_values[i].item<int>();
|
||||
|
||||
// Create masks for left and right splits
|
||||
auto left_mask = feature_values == split_val;
|
||||
auto right_mask = ~left_mask;
|
||||
|
||||
int left_count = left_mask.sum().item<int>();
|
||||
int right_count = right_mask.sum().item<int>();
|
||||
|
||||
// Skip if split doesn't satisfy minimum samples requirement
|
||||
if (left_count < min_samples_leaf || right_count < min_samples_leaf) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate weighted impurities
|
||||
auto left_y = y.index({ left_mask });
|
||||
auto left_weights = sample_weights.index({ left_mask });
|
||||
double left_weight = left_weights.sum().item<double>();
|
||||
double left_impurity = calculateGiniImpurity(left_y, left_weights);
|
||||
|
||||
auto right_y = y.index({ right_mask });
|
||||
auto right_weights = sample_weights.index({ right_mask });
|
||||
double right_weight = right_weights.sum().item<double>();
|
||||
double right_impurity = calculateGiniImpurity(right_y, right_weights);
|
||||
|
||||
// Calculate impurity decrease
|
||||
double impurity_decrease = current_impurity -
|
||||
(left_weight / total_weight * left_impurity +
|
||||
right_weight / total_weight * right_impurity);
|
||||
|
||||
// Update best split if this is better
|
||||
if (impurity_decrease > best_split.impurity_decrease) {
|
||||
best_split.feature_index = feat_idx;
|
||||
best_split.split_value = split_val;
|
||||
best_split.impurity_decrease = impurity_decrease;
|
||||
best_split.left_mask = left_mask;
|
||||
best_split.right_mask = right_mask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best_split;
|
||||
}
|
||||
|
||||
double DecisionTree::calculateGiniImpurity(
|
||||
const torch::Tensor& y,
|
||||
const torch::Tensor& sample_weights)
|
||||
{
|
||||
if (y.size(0) == 0 || sample_weights.size(0) == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if (y.size(0) != sample_weights.size(0)) {
|
||||
throw std::runtime_error("y and sample_weights must have same size");
|
||||
}
|
||||
|
||||
torch::Tensor class_weights = torch::zeros({ n_classes });
|
||||
|
||||
// Calculate weighted class counts
|
||||
for (int i = 0; i < y.size(0); i++) {
|
||||
int class_idx = y[i].item<int>();
|
||||
|
||||
if (class_idx < 0 || class_idx >= n_classes) {
|
||||
throw std::runtime_error("Invalid class index: " + std::to_string(class_idx));
|
||||
}
|
||||
|
||||
class_weights[class_idx] += sample_weights[i].item<float>();
|
||||
}
|
||||
|
||||
// Normalize
|
||||
double total_weight = class_weights.sum().item<double>();
|
||||
if (total_weight == 0) return 0.0;
|
||||
|
||||
class_weights /= total_weight;
|
||||
|
||||
// Calculate Gini impurity: 1 - sum(p_i^2)
|
||||
double gini = 1.0;
|
||||
for (int i = 0; i < n_classes; i++) {
|
||||
double p = class_weights[i].item<double>();
|
||||
gini -= p * p;
|
||||
}
|
||||
|
||||
return gini;
|
||||
}
|
||||
|
||||
|
||||
torch::Tensor DecisionTree::predict(torch::Tensor& X)
|
||||
{
|
||||
if (!fitted) {
|
||||
throw std::runtime_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
|
||||
int n_samples = X.size(1);
|
||||
torch::Tensor predictions = torch::zeros({ n_samples }, torch::kInt32);
|
||||
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
auto sample = X.index({ torch::indexing::Slice(), i }).ravel();
|
||||
predictions[i] = predictSample(sample);
|
||||
}
|
||||
|
||||
return predictions;
|
||||
}
|
||||
|
||||
std::vector<int> DecisionTree::predict(std::vector<std::vector<int>>& X)
|
||||
{
|
||||
// Convert to tensor
|
||||
long n = X.size();
|
||||
long m = X.at(0).size();
|
||||
torch::Tensor X_tensor = platform::TensorUtils::to_matrix(X);
|
||||
auto predictions = predict(X_tensor);
|
||||
std::vector<int> result = platform::TensorUtils::to_vector<int>(predictions);
|
||||
return result;
|
||||
}
|
||||
|
||||
torch::Tensor DecisionTree::predict_proba(torch::Tensor& X)
|
||||
{
|
||||
if (!fitted) {
|
||||
throw std::runtime_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
|
||||
int n_samples = X.size(1);
|
||||
torch::Tensor probabilities = torch::zeros({ n_samples, n_classes });
|
||||
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
auto sample = X.index({ torch::indexing::Slice(), i }).ravel();
|
||||
probabilities[i] = predictProbaSample(sample);
|
||||
}
|
||||
|
||||
return probabilities;
|
||||
}
|
||||
|
||||
std::vector<std::vector<double>> DecisionTree::predict_proba(std::vector<std::vector<int>>& X)
|
||||
{
|
||||
auto n_samples = X.at(0).size();
|
||||
// Convert to tensor
|
||||
torch::Tensor X_tensor = platform::TensorUtils::to_matrix(X);
|
||||
auto proba_tensor = predict_proba(X_tensor);
|
||||
std::vector<std::vector<double>> result(n_samples, std::vector<double>(n_classes, 0.0));
|
||||
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
for (int j = 0; j < n_classes; j++) {
|
||||
result[i][j] = proba_tensor[i][j].item<double>();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int DecisionTree::predictSample(const torch::Tensor& x) const
|
||||
{
|
||||
if (!fitted) {
|
||||
throw std::runtime_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
|
||||
if (x.size(0) != n) { // n debería ser el número de características
|
||||
throw std::runtime_error("Input sample has wrong number of features");
|
||||
}
|
||||
|
||||
const TreeNode* leaf = traverseTree(x, root.get());
|
||||
return leaf->predicted_class;
|
||||
}
|
||||
torch::Tensor DecisionTree::predictProbaSample(const torch::Tensor& x) const
|
||||
{
|
||||
const TreeNode* leaf = traverseTree(x, root.get());
|
||||
return leaf->class_probabilities.clone();
|
||||
}
|
||||
|
||||
|
||||
const TreeNode* DecisionTree::traverseTree(const torch::Tensor& x, const TreeNode* node) const
|
||||
{
|
||||
if (!node) {
|
||||
throw std::runtime_error("Null node encountered during tree traversal");
|
||||
}
|
||||
|
||||
if (node->is_leaf) {
|
||||
return node;
|
||||
}
|
||||
|
||||
if (node->split_feature < 0 || node->split_feature >= x.size(0)) {
|
||||
throw std::runtime_error("Invalid split_feature index: " + std::to_string(node->split_feature));
|
||||
}
|
||||
|
||||
int feature_value = x[node->split_feature].item<int>();
|
||||
|
||||
if (feature_value == node->split_value) {
|
||||
if (!node->left) {
|
||||
throw std::runtime_error("Missing left child in tree");
|
||||
}
|
||||
return traverseTree(x, node->left.get());
|
||||
} else {
|
||||
if (!node->right) {
|
||||
throw std::runtime_error("Missing right child in tree");
|
||||
}
|
||||
return traverseTree(x, node->right.get());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> DecisionTree::graph(const std::string& title) const
|
||||
{
|
||||
std::vector<std::string> lines;
|
||||
lines.push_back("digraph DecisionTree {");
|
||||
lines.push_back(" rankdir=TB;");
|
||||
lines.push_back(" node [shape=box, style=\"filled, rounded\", fontname=\"helvetica\"];");
|
||||
lines.push_back(" edge [fontname=\"helvetica\"];");
|
||||
|
||||
if (!title.empty()) {
|
||||
lines.push_back(" label=\"" + title + "\";");
|
||||
lines.push_back(" labelloc=t;");
|
||||
}
|
||||
|
||||
if (root) {
|
||||
int node_id = 0;
|
||||
treeToGraph(root.get(), lines, node_id);
|
||||
}
|
||||
|
||||
lines.push_back("}");
|
||||
return lines;
|
||||
}
|
||||
|
||||
void DecisionTree::treeToGraph(
|
||||
const TreeNode* node,
|
||||
std::vector<std::string>& lines,
|
||||
int& node_id,
|
||||
int parent_id,
|
||||
const std::string& edge_label) const
|
||||
{
|
||||
|
||||
int current_id = node_id++;
|
||||
std::stringstream ss;
|
||||
|
||||
if (node->is_leaf) {
|
||||
// Leaf node
|
||||
ss << " node" << current_id << " [label=\"Class: " << node->predicted_class;
|
||||
ss << "\\nProb: " << std::fixed << std::setprecision(3)
|
||||
<< node->class_probabilities[node->predicted_class].item<float>();
|
||||
ss << "\", fillcolor=\"lightblue\"];";
|
||||
lines.push_back(ss.str());
|
||||
} else {
|
||||
// Internal node
|
||||
ss << " node" << current_id << " [label=\"" << features[node->split_feature];
|
||||
ss << " = " << node->split_value << "?\", fillcolor=\"lightgreen\"];";
|
||||
lines.push_back(ss.str());
|
||||
}
|
||||
|
||||
// Add edge from parent
|
||||
if (parent_id >= 0) {
|
||||
ss.str("");
|
||||
ss << " node" << parent_id << " -> node" << current_id;
|
||||
if (!edge_label.empty()) {
|
||||
ss << " [label=\"" << edge_label << "\"];";
|
||||
} else {
|
||||
ss << ";";
|
||||
}
|
||||
lines.push_back(ss.str());
|
||||
}
|
||||
|
||||
// Recurse on children
|
||||
if (!node->is_leaf) {
|
||||
if (node->left) {
|
||||
treeToGraph(node->left.get(), lines, node_id, current_id, "Yes");
|
||||
}
|
||||
if (node->right) {
|
||||
treeToGraph(node->right.get(), lines, node_id, current_id, "No");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bayesnet
|
134
src/experimental_clfs/DecisionTree.h
Normal file
134
src/experimental_clfs/DecisionTree.h
Normal file
@@ -0,0 +1,134 @@
|
||||
// ***************************************************************
|
||||
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
||||
// SPDX-FileType: SOURCE
|
||||
// SPDX-License-Identifier: MIT
|
||||
// ***************************************************************
|
||||
|
||||
#ifndef DECISION_TREE_H
|
||||
#define DECISION_TREE_H
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <torch/torch.h>
|
||||
#include "bayesnet/classifiers/Classifier.h"
|
||||
|
||||
namespace bayesnet {
|
||||
|
||||
// Forward declaration
|
||||
struct TreeNode;
|
||||
|
||||
class DecisionTree : public Classifier {
|
||||
public:
|
||||
explicit DecisionTree(int max_depth = 3, int min_samples_split = 2, int min_samples_leaf = 1);
|
||||
virtual ~DecisionTree() = default;
|
||||
|
||||
// Override graph method to show tree structure
|
||||
std::vector<std::string> graph(const std::string& title = "") const override;
|
||||
|
||||
// Setters for hyperparameters
|
||||
void setMaxDepth(int depth) { max_depth = depth; checkValues(); }
|
||||
void setMinSamplesSplit(int samples) { min_samples_split = samples; checkValues(); }
|
||||
void setMinSamplesLeaf(int samples) { min_samples_leaf = samples; checkValues(); }
|
||||
int getMaxDepth() const { return max_depth; }
|
||||
int getMinSamplesSplit() const { return min_samples_split; }
|
||||
int getMinSamplesLeaf() const { return min_samples_leaf; }
|
||||
|
||||
// Override setHyperparameters
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||
|
||||
torch::Tensor predict(torch::Tensor& X) override;
|
||||
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
||||
torch::Tensor predict_proba(torch::Tensor& X) override;
|
||||
std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X);
|
||||
|
||||
// Make predictions for a single sample
|
||||
int predictSample(const torch::Tensor& x) const;
|
||||
|
||||
// Make probabilistic predictions for a single sample
|
||||
torch::Tensor predictProbaSample(const torch::Tensor& x) const;
|
||||
|
||||
protected:
|
||||
void buildModel(const torch::Tensor& weights) override;
|
||||
void trainModel(const torch::Tensor& weights, const Smoothing_t smoothing) override
|
||||
{
|
||||
// Decision trees do not require training in the traditional sense
|
||||
// as they are built from the data directly.
|
||||
// This method can be used to set weights or other parameters if needed.
|
||||
}
|
||||
private:
|
||||
void checkValues();
|
||||
bool validateTensors(const torch::Tensor& X, const torch::Tensor& y, const torch::Tensor& sample_weights) const;
|
||||
// Tree hyperparameters
|
||||
int max_depth;
|
||||
int min_samples_split;
|
||||
int min_samples_leaf;
|
||||
int n_classes; // Number of classes in the target variable
|
||||
|
||||
// Root of the decision tree
|
||||
std::unique_ptr<TreeNode> root;
|
||||
|
||||
// Build tree recursively
|
||||
std::unique_ptr<TreeNode> buildTree(
|
||||
const torch::Tensor& X,
|
||||
const torch::Tensor& y,
|
||||
const torch::Tensor& sample_weights,
|
||||
int current_depth
|
||||
);
|
||||
|
||||
// Find best split for a node
|
||||
struct SplitInfo {
|
||||
int feature_index;
|
||||
int split_value;
|
||||
double impurity_decrease;
|
||||
torch::Tensor left_mask;
|
||||
torch::Tensor right_mask;
|
||||
};
|
||||
|
||||
SplitInfo findBestSplit(
|
||||
const torch::Tensor& X,
|
||||
const torch::Tensor& y,
|
||||
const torch::Tensor& sample_weights
|
||||
);
|
||||
|
||||
// Calculate weighted Gini impurity for multi-class
|
||||
double calculateGiniImpurity(
|
||||
const torch::Tensor& y,
|
||||
const torch::Tensor& sample_weights
|
||||
);
|
||||
|
||||
|
||||
|
||||
// Traverse tree to find leaf node
|
||||
const TreeNode* traverseTree(const torch::Tensor& x, const TreeNode* node) const;
|
||||
|
||||
// Convert tree to graph representation
|
||||
void treeToGraph(
|
||||
const TreeNode* node,
|
||||
std::vector<std::string>& lines,
|
||||
int& node_id,
|
||||
int parent_id = -1,
|
||||
const std::string& edge_label = ""
|
||||
) const;
|
||||
};
|
||||
|
||||
// Tree node structure
|
||||
struct TreeNode {
|
||||
bool is_leaf;
|
||||
|
||||
// For internal nodes
|
||||
int split_feature;
|
||||
int split_value;
|
||||
std::unique_ptr<TreeNode> left;
|
||||
std::unique_ptr<TreeNode> right;
|
||||
|
||||
// For leaf nodes
|
||||
int predicted_class;
|
||||
torch::Tensor class_probabilities; // Probability for each class
|
||||
|
||||
TreeNode() : is_leaf(false), split_feature(-1), split_value(-1), predicted_class(-1) {}
|
||||
};
|
||||
|
||||
} // namespace bayesnet
|
||||
|
||||
#endif // DECISION_TREE_H
|
@@ -43,6 +43,7 @@ namespace platform {
|
||||
void add_active_parents(const std::vector<int>& active_parents);
|
||||
void add_active_parent(int parent);
|
||||
void remove_last_parent();
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters_) override {};
|
||||
protected:
|
||||
bool debug = false;
|
||||
Xaode aode_;
|
||||
|
142
src/experimental_clfs/README.md
Normal file
142
src/experimental_clfs/README.md
Normal file
@@ -0,0 +1,142 @@
|
||||
# AdaBoost and DecisionTree Classifier Implementation
|
||||
|
||||
This implementation provides both a Decision Tree classifier and a multi-class AdaBoost classifier based on the SAMME (Stagewise Additive Modeling using a Multi-class Exponential loss) algorithm described in the paper "Multi-class AdaBoost" by Zhu et al. Implemented in C++ using <https://claude.ai>
|
||||
|
||||
## Components
|
||||
|
||||
### 1. DecisionTree Classifier
|
||||
|
||||
A classic decision tree implementation that:
|
||||
|
||||
- Supports multi-class classification
|
||||
- Handles weighted samples (essential for boosting)
|
||||
- Uses Gini impurity as the splitting criterion
|
||||
- Works with discrete/categorical features
|
||||
- Provides both class predictions and probability estimates
|
||||
|
||||
#### Key Features
|
||||
|
||||
- **Max Depth Control**: Limit tree depth to create weak learners
|
||||
- **Minimum Samples**: Control minimum samples for splitting and leaf nodes
|
||||
- **Weighted Training**: Properly handles sample weights for boosting
|
||||
- **Visualization**: Generates DOT format graphs of the tree structure
|
||||
|
||||
#### Hyperparameters
|
||||
|
||||
- `max_depth`: Maximum depth of the tree (default: 3)
|
||||
- `min_samples_split`: Minimum samples required to split a node (default: 2)
|
||||
- `min_samples_leaf`: Minimum samples required in a leaf node (default: 1)
|
||||
|
||||
### 2. AdaBoost Classifier
|
||||
|
||||
A multi-class AdaBoost implementation using DecisionTree as base estimators:
|
||||
|
||||
- **SAMME Algorithm**: Implements the multi-class extension of AdaBoost
|
||||
- **Automatic Stumps**: Uses decision stumps (max_depth=1) by default
|
||||
- **Early Stopping**: Stops if base classifier performs worse than random
|
||||
- **Ensemble Visualization**: Shows the weighted combination of base estimators
|
||||
|
||||
#### Key Features
|
||||
|
||||
- **Multi-class Support**: Natural extension to K classes
|
||||
- **Base Estimator Control**: Configure depth of base decision trees
|
||||
- **Training Monitoring**: Track training errors and estimator weights
|
||||
- **Probability Estimates**: Provides class probability predictions
|
||||
|
||||
#### Hyperparameters
|
||||
|
||||
- `n_estimators`: Number of base estimators to train (default: 50)
|
||||
- `base_max_depth`: Maximum depth for base decision trees (default: 1)
|
||||
|
||||
## Algorithm Details
|
||||
|
||||
The SAMME algorithm differs from binary AdaBoost in the calculation of the estimator weight (alpha):
|
||||
|
||||
```
|
||||
α = log((1 - err) / err) + log(K - 1)
|
||||
```
|
||||
|
||||
where `K` is the number of classes. This formula ensures that:
|
||||
|
||||
- When K = 2, it reduces to standard AdaBoost
|
||||
- For K > 2, base classifiers only need to be better than random guessing (1/K) rather than 50%
|
||||
|
||||
## Usage Example
|
||||
|
||||
```cpp
|
||||
// Create AdaBoost with decision stumps
|
||||
AdaBoost ada(100, 1); // 100 estimators, max_depth=1
|
||||
|
||||
// Train
|
||||
ada.fit(X_train, y_train, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
// Predict
|
||||
auto predictions = ada.predict(X_test);
|
||||
auto probabilities = ada.predict_proba(X_test);
|
||||
|
||||
// Evaluate
|
||||
float accuracy = ada.score(X_test, y_test);
|
||||
|
||||
// Get ensemble information
|
||||
auto weights = ada.getEstimatorWeights();
|
||||
auto errors = ada.getTrainingErrors();
|
||||
```
|
||||
|
||||
## Implementation Structure
|
||||
|
||||
```
|
||||
AdaBoost (inherits from Ensemble)
|
||||
└── Uses multiple DecisionTree instances as base estimators
|
||||
└── DecisionTree (inherits from Classifier)
|
||||
└── Implements weighted Gini impurity splitting
|
||||
```
|
||||
|
||||
## Visualization
|
||||
|
||||
Both classifiers support graph visualization:
|
||||
|
||||
- **DecisionTree**: Shows the tree structure with split conditions
|
||||
- **AdaBoost**: Shows the ensemble of weighted base estimators
|
||||
|
||||
Generate visualizations using:
|
||||
|
||||
```cpp
|
||||
auto graph = classifier.graph("Title");
|
||||
```
|
||||
|
||||
## Data Format
|
||||
|
||||
Both classifiers expect discrete/categorical data:
|
||||
|
||||
- **Features**: Integer values representing categories (stored in `torch::Tensor` or `std::vector<std::vector<int>>`)
|
||||
- **Labels**: Integer values representing class indices (0, 1, ..., K-1)
|
||||
- **States**: Map defining possible values for each feature and the class variable
|
||||
- **Sample Weights**: Optional weights for each training sample (important for boosting)
|
||||
|
||||
Example data setup:
|
||||
|
||||
```cpp
|
||||
// Features matrix (n_features x n_samples)
|
||||
torch::Tensor X = torch::tensor({{0, 1, 2}, {1, 0, 1}}); // 2 features, 3 samples
|
||||
|
||||
// Labels vector
|
||||
torch::Tensor y = torch::tensor({0, 1, 0}); // 3 samples
|
||||
|
||||
// States definition
|
||||
std::map<std::string, std::vector<int>> states;
|
||||
states["feature1"] = {0, 1, 2}; // Feature 1 can take values 0, 1, or 2
|
||||
states["feature2"] = {0, 1}; // Feature 2 can take values 0 or 1
|
||||
states["class"] = {0, 1}; // Binary classification
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- The implementation handles discrete/categorical features as indicated by the int-based data structures
|
||||
- Sample weights are properly propagated through the tree building process
|
||||
- The DecisionTree implementation uses equality testing for splits (suitable for categorical data)
|
||||
- Both classifiers support the standard fit/predict interface from the base framework
|
||||
|
||||
## References
|
||||
|
||||
- Zhu, J., Zou, H., Rosset, S., & Hastie, T. (2009). Multi-class AdaBoost. Statistics and its interface, 2(3), 349-360.
|
||||
- Breiman, L., Friedman, J., Olshen, R., & Stone, C. (1984). Classification and Regression Trees. Wadsworth, Belmont, CA.
|
@@ -45,7 +45,53 @@ namespace platform {
|
||||
|
||||
return data;
|
||||
}
|
||||
static torch::Tensor to_matrix(const std::vector<std::vector<int>>& data)
|
||||
{
|
||||
if (data.empty()) return torch::empty({ 0, 0 }, torch::kInt64);
|
||||
size_t rows = data.size();
|
||||
size_t cols = data[0].size();
|
||||
torch::Tensor tensor = torch::empty({ static_cast<long>(rows), static_cast<long>(cols) }, torch::kInt64);
|
||||
for (size_t i = 0; i < rows; ++i) {
|
||||
for (size_t j = 0; j < cols; ++j) {
|
||||
tensor.index_put_({ static_cast<long>(i), static_cast<long>(j) }, data[i][j]);
|
||||
}
|
||||
}
|
||||
return tensor;
|
||||
}
|
||||
};
|
||||
static void dumpVector(const std::vector<std::vector<int>>& vec, const std::string& name)
|
||||
{
|
||||
std::cout << name << ": " << std::endl;
|
||||
for (const auto& row : vec) {
|
||||
std::cout << "[";
|
||||
for (const auto& val : row) {
|
||||
std::cout << val << " ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
static void dumpTensor(const torch::Tensor& tensor, const std::string& name)
|
||||
{
|
||||
std::cout << name << ": " << std::endl;
|
||||
for (auto i = 0; i < tensor.size(0); i++) {
|
||||
std::cout << "[";
|
||||
for (auto j = 0; j < tensor.size(1); j++) {
|
||||
std::cout << tensor[i][j].item<int>() << " ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
static void dumpTensorV(const torch::Tensor& tensor, const std::string& name)
|
||||
{
|
||||
std::cout << name << ": " << std::endl;
|
||||
std::cout << "[";
|
||||
for (int i = 0; i < tensor.size(0); i++) {
|
||||
std::cout << tensor[i].item<int>() << " ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TENSORUTILS_HPP
|
@@ -13,6 +13,7 @@ namespace platform {
|
||||
auto env = platform::DotEnv();
|
||||
auto datasets = platform::Datasets(false, platform::Paths::datasets());
|
||||
auto& group = arguments.add_mutually_exclusive_group(true);
|
||||
|
||||
group.add_argument("-d", "--dataset")
|
||||
.help("Dataset file name: " + datasets.toString())
|
||||
.default_value("all")
|
||||
@@ -43,6 +44,7 @@ namespace platform {
|
||||
}
|
||||
);
|
||||
arguments.add_argument("--title").default_value("").help("Experiment title");
|
||||
arguments.add_argument("--folder").help("Results folder to use").default_value(platform::Paths::results());
|
||||
arguments.add_argument("--discretize").help("Discretize input dataset").default_value((bool)stoi(env.get("discretize"))).implicit_value(true);
|
||||
auto valid_choices = env.valid_tokens("discretize_algo");
|
||||
auto& disc_arg = arguments.add_argument("--discretize-algo").help("Algorithm to use in discretization. Valid values: " + env.valid_values("discretize_algo")).default_value(env.get("discretize_algo"));
|
||||
@@ -103,6 +105,10 @@ namespace platform {
|
||||
file_name = arguments.get<std::string>("dataset");
|
||||
file_names = arguments.get<std::vector<std::string>>("datasets");
|
||||
datasets_file = arguments.get<std::string>("datasets-file");
|
||||
path_results = arguments.get<std::string>("folder");
|
||||
if (path_results.back() != '/') {
|
||||
path_results += '/';
|
||||
}
|
||||
model_name = arguments.get<std::string>("model");
|
||||
discretize_dataset = arguments.get<bool>("discretize");
|
||||
discretize_algo = arguments.get<std::string>("discretize-algo");
|
||||
@@ -119,7 +125,7 @@ namespace platform {
|
||||
hyper_best = arguments.get<bool>("hyper-best");
|
||||
if (hyper_best) {
|
||||
// Build the best results file_name
|
||||
hyperparameters_file = platform::Paths::results() + platform::Paths::bestResultsFile(score, model_name);
|
||||
hyperparameters_file = path_results + platform::Paths::bestResultsFile(score, model_name);
|
||||
// ignore this parameter
|
||||
hyperparameters = "{}";
|
||||
} else {
|
||||
@@ -209,10 +215,36 @@ namespace platform {
|
||||
test_hyperparams = platform::HyperParameters(datasets.getNames(), hyperparameters_json);
|
||||
}
|
||||
}
|
||||
std::string getGppVersion()
|
||||
{
|
||||
std::string result;
|
||||
std::array<char, 128> buffer;
|
||||
|
||||
// Run g++ --version and capture the output
|
||||
using pclose_t = int(*)(FILE*);
|
||||
std::unique_ptr<FILE, pclose_t> pipe(popen("g++ --version", "r"), pclose);
|
||||
|
||||
if (!pipe) {
|
||||
return "Error executing g++ --version command";
|
||||
}
|
||||
|
||||
// Read the first line of output (which contains the version info)
|
||||
if (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
|
||||
result = buffer.data();
|
||||
// Remove trailing newline if present
|
||||
if (!result.empty() && result[result.length() - 1] == '\n') {
|
||||
result.erase(result.length() - 1);
|
||||
}
|
||||
} else {
|
||||
return "No output from g++ --version command";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Experiment& ArgumentsExperiment::initializedExperiment()
|
||||
{
|
||||
auto env = platform::DotEnv();
|
||||
experiment.setTitle(title).setLanguage("c++").setLanguageVersion("gcc 14.1.1");
|
||||
experiment.setTitle(title).setLanguage("c++").setLanguageVersion(getGppVersion());
|
||||
experiment.setDiscretizationAlgorithm(discretize_algo).setSmoothSrategy(smooth_strat);
|
||||
experiment.setDiscretized(discretize_dataset).setModel(model_name).setPlatform(env.get("platform"));
|
||||
experiment.setStratified(stratified).setNFolds(n_folds).setScoreName(score);
|
||||
|
@@ -22,11 +22,13 @@ namespace platform {
|
||||
bool isQuiet() const { return quiet; }
|
||||
bool haveToSaveResults() const { return saveResults; }
|
||||
bool doGraph() const { return graph; }
|
||||
std::string getPathResults() const { return path_results; }
|
||||
private:
|
||||
Experiment experiment;
|
||||
experiment_t type;
|
||||
argparse::ArgumentParser& arguments;
|
||||
std::string file_name, model_name, title, hyperparameters_file, datasets_file, discretize_algo, smooth_strat, score;
|
||||
std::string file_name, model_name, title, hyperparameters_file, datasets_file, discretize_algo, smooth_strat;
|
||||
std::string score, path_results;
|
||||
json hyperparameters_json;
|
||||
bool discretize_dataset, stratified, saveResults, quiet, no_train_score, generate_fold_files, graph, hyper_best;
|
||||
std::vector<int> seeds;
|
||||
|
@@ -7,12 +7,12 @@
|
||||
namespace platform {
|
||||
using json = nlohmann::ordered_json;
|
||||
|
||||
void Experiment::saveResult()
|
||||
void Experiment::saveResult(const std::string& path)
|
||||
{
|
||||
result.setSchemaVersion("1.0");
|
||||
result.check();
|
||||
result.save();
|
||||
std::cout << "Result saved in " << Paths::results() << result.getFilename() << std::endl;
|
||||
result.save(path);
|
||||
std::cout << "Result saved in " << path << result.getFilename() << std::endl;
|
||||
}
|
||||
void Experiment::report()
|
||||
{
|
||||
@@ -245,8 +245,6 @@ namespace platform {
|
||||
// Train model
|
||||
//
|
||||
clf->fit(X_train, y_train, features, className, states, smooth_type);
|
||||
if (!quiet)
|
||||
showProgress(nfold + 1, getColor(clf->getStatus()), "b");
|
||||
auto clf_notes = clf->getNotes();
|
||||
std::transform(clf_notes.begin(), clf_notes.end(), std::back_inserter(notes), [nfold](const std::string& note)
|
||||
{ return "Fold " + std::to_string(nfold) + ": " + note; });
|
||||
@@ -259,10 +257,13 @@ namespace platform {
|
||||
// Score train
|
||||
//
|
||||
if (!no_train_score) {
|
||||
if (!quiet)
|
||||
showProgress(nfold + 1, getColor(clf->getStatus()), "b");
|
||||
auto y_proba_train = clf->predict_proba(X_train);
|
||||
Scores scores(y_train, y_proba_train, num_classes, labels);
|
||||
score_train_value = score == score_t::ACCURACY ? scores.accuracy() : scores.auc();
|
||||
confusion_matrices_train.push_back(scores.get_confusion_matrix_json(true));
|
||||
if (discretized)
|
||||
confusion_matrices_train.push_back(scores.get_confusion_matrix_json(true));
|
||||
}
|
||||
//
|
||||
// Test model
|
||||
@@ -277,7 +278,8 @@ namespace platform {
|
||||
test_time[item] = test_timer.getDuration();
|
||||
score_train[item] = score_train_value;
|
||||
score_test[item] = score_test_value;
|
||||
confusion_matrices.push_back(scores.get_confusion_matrix_json(true));
|
||||
if (discretized)
|
||||
confusion_matrices.push_back(scores.get_confusion_matrix_json(true));
|
||||
if (!quiet)
|
||||
std::cout << "\b\b\b, " << flush;
|
||||
//
|
||||
|
@@ -45,7 +45,7 @@ namespace platform {
|
||||
std::vector<int> getRandomSeeds() const { return randomSeeds; }
|
||||
void cross_validation(const std::string& fileName);
|
||||
void go();
|
||||
void saveResult();
|
||||
void saveResult(const std::string& path);
|
||||
void show();
|
||||
void saveGraph();
|
||||
void report();
|
||||
|
@@ -23,8 +23,11 @@
|
||||
#include <pyclassifiers/ODTE.h>
|
||||
#include <pyclassifiers/SVC.h>
|
||||
#include <pyclassifiers/XGBoost.h>
|
||||
#include <pyclassifiers/AdaBoostPy.h>
|
||||
#include <pyclassifiers/RandomForest.h>
|
||||
#include "../experimental_clfs/XA1DE.h"
|
||||
#include "../experimental_clfs/AdaBoost.h"
|
||||
#include "../experimental_clfs/DecisionTree.h"
|
||||
|
||||
namespace platform {
|
||||
class Models {
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#include <utility>
|
||||
#include "RocAuc.h"
|
||||
namespace platform {
|
||||
|
||||
|
||||
double RocAuc::compute(const torch::Tensor& y_proba, const torch::Tensor& labels)
|
||||
{
|
||||
size_t nClasses = y_proba.size(1);
|
||||
@@ -48,6 +48,7 @@ namespace platform {
|
||||
double tp = 0, fp = 0;
|
||||
double totalPos = std::count(y_test.begin(), y_test.end(), classIdx);
|
||||
double totalNeg = nSamples - totalPos;
|
||||
if (totalPos == 0 || totalNeg == 0) return 0.5; // neutral AUC
|
||||
|
||||
for (const auto& [score, label] : scoresAndLabels) {
|
||||
if (label == 1) {
|
||||
|
@@ -35,6 +35,12 @@ namespace platform {
|
||||
[](void) -> bayesnet::BaseClassifier* { return new pywrap::RandomForest();});
|
||||
static Registrar registrarXGB("XGBoost",
|
||||
[](void) -> bayesnet::BaseClassifier* { return new pywrap::XGBoost();});
|
||||
static Registrar registrarAdaPy("AdaBoostPy",
|
||||
[](void) -> bayesnet::BaseClassifier* { return new pywrap::AdaBoostPy();});
|
||||
static Registrar registrarAda("AdaBoost",
|
||||
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AdaBoost();});
|
||||
static Registrar registrarDT("DecisionTree",
|
||||
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::DecisionTree();});
|
||||
static Registrar registrarXSPODE("XSPODE",
|
||||
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::XSpode(0);});
|
||||
static Registrar registrarXSP2DE("XSP2DE",
|
||||
@@ -44,6 +50,6 @@ namespace platform {
|
||||
static Registrar registrarXBA2DE("XBA2DE",
|
||||
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::XBA2DE();});
|
||||
static Registrar registrarXA1DE("XA1DE",
|
||||
[](void) -> bayesnet::BaseClassifier* { return new XA1DE();});
|
||||
[](void) -> bayesnet::BaseClassifier* { return new XA1DE();});
|
||||
}
|
||||
#endif
|
||||
|
@@ -18,8 +18,8 @@ namespace platform {
|
||||
const std::string STATUS_OK = "Ok.";
|
||||
const std::string STATUS_COLOR = Colors::GREEN();
|
||||
|
||||
ManageScreen::ManageScreen(int rows, int cols, const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial, bool compare) :
|
||||
rows{ rows }, cols{ cols }, complete{ complete }, partial{ partial }, compare{ compare }, didExcel(false), results(ResultsManager(model, score, platform, complete, partial))
|
||||
ManageScreen::ManageScreen(const std::string path_, int rows, int cols, const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial, bool compare) :
|
||||
path{ path_ }, rows{ rows }, cols{ cols }, complete{ complete }, partial{ partial }, compare{ compare }, didExcel(false), results(ResultsManager(path_, model, score, platform, complete, partial))
|
||||
{
|
||||
results.load();
|
||||
openExcel = false;
|
||||
@@ -329,11 +329,11 @@ namespace platform {
|
||||
return;
|
||||
}
|
||||
// Remove the old result file
|
||||
std::string oldFile = Paths::results() + results.at(index).getFilename();
|
||||
std::string oldFile = path + results.at(index).getFilename();
|
||||
std::filesystem::remove(oldFile);
|
||||
// Actually change the model
|
||||
results.at(index).setModel(newModel);
|
||||
results.at(index).save();
|
||||
results.at(index).save(path);
|
||||
int newModelSize = static_cast<int>(newModel.size());
|
||||
if (newModelSize > maxModel) {
|
||||
maxModel = newModelSize;
|
||||
@@ -583,7 +583,7 @@ namespace platform {
|
||||
getline(std::cin, newTitle);
|
||||
if (!newTitle.empty()) {
|
||||
results.at(index).setTitle(newTitle);
|
||||
results.at(index).save();
|
||||
results.at(index).save(path);
|
||||
list("Title changed to " + newTitle, Colors::GREEN());
|
||||
break;
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ namespace platform {
|
||||
};
|
||||
class ManageScreen {
|
||||
public:
|
||||
ManageScreen(int rows, int cols, const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial, bool compare);
|
||||
ManageScreen(const std::string path, int rows, int cols, const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial, bool compare);
|
||||
~ManageScreen() = default;
|
||||
void doMenu();
|
||||
void updateSize(int rows, int cols);
|
||||
@@ -59,7 +59,7 @@ namespace platform {
|
||||
std::vector<Paginator> paginator;
|
||||
ResultsManager results;
|
||||
lxw_workbook* workbook;
|
||||
std::string excelFileName;
|
||||
std::string path, excelFileName;
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -1,10 +1,9 @@
|
||||
#include <algorithm>
|
||||
#include "common/Paths.h"
|
||||
#include "ResultsManager.h"
|
||||
|
||||
namespace platform {
|
||||
ResultsManager::ResultsManager(const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial) :
|
||||
path(Paths::results()), model(model), scoreName(score), platform(platform), complete(complete), partial(partial), maxModel(0), maxTitle(0)
|
||||
ResultsManager::ResultsManager(const std::string& path_, const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial) :
|
||||
path(path_), model(model), scoreName(score), platform(platform), complete(complete), partial(partial), maxModel(0), maxTitle(0)
|
||||
{
|
||||
}
|
||||
void ResultsManager::load()
|
||||
|
@@ -18,7 +18,7 @@ namespace platform {
|
||||
};
|
||||
class ResultsManager {
|
||||
public:
|
||||
ResultsManager(const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial);
|
||||
ResultsManager(const std::string& path_, const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial);
|
||||
void load(); // Loads the list of results
|
||||
void sortResults(SortField field, SortType type); // Sorts the list of results
|
||||
void sortDate(SortType type);
|
||||
|
@@ -26,6 +26,7 @@ namespace platform {
|
||||
auto datasets = platform::Datasets(false, platform::Paths::datasets());
|
||||
std::stringstream sheader;
|
||||
auto datasets_names = datasets.getNames();
|
||||
std::cout << Colors::GREEN() << "Datasets available in the platform: " << datasets_names.size() << std::endl;
|
||||
int maxName = std::max(size_t(7), (*max_element(datasets_names.begin(), datasets_names.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size());
|
||||
std::vector<std::string> header_labels = { " #", "Dataset", "Sampl.", "Feat.", "#Num.", "Cls", "Balance" };
|
||||
std::vector<int> header_lengths = { 3, maxName, 6, 6, 6, 3, DatasetsConsole::BALANCE_LENGTH };
|
||||
@@ -61,9 +62,13 @@ namespace platform {
|
||||
line << setw(header_lengths[5]) << right << nClasses << " ";
|
||||
std::string sep = "";
|
||||
oss.str("");
|
||||
for (auto number : dataset.getClassesCounts()) {
|
||||
oss << sep << std::setprecision(2) << fixed << (float)number / nSamples * 100.0 << "% (" << number << ")";
|
||||
sep = " / ";
|
||||
if (nSamples == 0) {
|
||||
oss << "No samples";
|
||||
} else {
|
||||
for (auto number : dataset.getClassesCounts()) {
|
||||
oss << sep << std::setprecision(2) << fixed << (float)number / nSamples * 100.0 << "% (" << number << ")";
|
||||
sep = " / ";
|
||||
}
|
||||
}
|
||||
split_lines(maxName, line.str(), oss.str());
|
||||
// Store data for Excel report
|
||||
|
@@ -1,8 +1,9 @@
|
||||
#include "common/Paths.h"
|
||||
#include "DatasetsExcel.h"
|
||||
namespace platform {
|
||||
DatasetsExcel::DatasetsExcel()
|
||||
{
|
||||
file_name = "datasets.xlsx";
|
||||
file_name = Paths::excelDatasets();
|
||||
workbook = workbook_new(getFileName().c_str());
|
||||
createFormats();
|
||||
setProperties("Datasets");
|
||||
|
@@ -11,6 +11,7 @@ namespace platform {
|
||||
DatasetsExcel();
|
||||
~DatasetsExcel();
|
||||
void report(json& data);
|
||||
std::string getExcelFileName() { return getFileName(); }
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -69,9 +69,9 @@ namespace platform {
|
||||
platform::JsonValidator validator(platform::SchemaV1_0::schema);
|
||||
return validator.validate(data);
|
||||
}
|
||||
void Result::save()
|
||||
void Result::save(const std::string& path)
|
||||
{
|
||||
std::ofstream file(Paths::results() + getFilename());
|
||||
std::ofstream file(path + getFilename());
|
||||
file << data;
|
||||
file.close();
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ namespace platform {
|
||||
public:
|
||||
Result();
|
||||
Result& load(const std::string& path, const std::string& filename);
|
||||
void save();
|
||||
void save(const std::string& path);
|
||||
std::vector<std::string> check();
|
||||
// Getters
|
||||
json getJson();
|
||||
|
@@ -1,8 +1,9 @@
|
||||
#include "common/Paths.h"
|
||||
#include "ResultsDatasetExcel.h"
|
||||
namespace platform {
|
||||
ResultsDatasetExcel::ResultsDatasetExcel()
|
||||
{
|
||||
file_name = "some_results.xlsx";
|
||||
file_name = Paths::excelResults();
|
||||
workbook = workbook_new(getFileName().c_str());
|
||||
createFormats();
|
||||
setProperties("Results");
|
||||
|
@@ -12,6 +12,7 @@ namespace platform {
|
||||
ResultsDatasetExcel();
|
||||
~ResultsDatasetExcel();
|
||||
void report(json& data);
|
||||
std::string getExcelFileName() { return getFileName(); }
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -12,11 +12,13 @@ if(ENABLE_TESTING)
|
||||
${Bayesnet_INCLUDE_DIRS}
|
||||
)
|
||||
set(TEST_SOURCES_PLATFORM
|
||||
TestUtils.cpp TestPlatform.cpp TestResult.cpp TestScores.cpp
|
||||
TestUtils.cpp TestPlatform.cpp TestResult.cpp TestScores.cpp TestDecisionTree.cpp TestAdaBoost.cpp
|
||||
${Platform_SOURCE_DIR}/src/common/Datasets.cpp ${Platform_SOURCE_DIR}/src/common/Dataset.cpp ${Platform_SOURCE_DIR}/src/common/Discretization.cpp
|
||||
${Platform_SOURCE_DIR}/src/main/Scores.cpp
|
||||
${Platform_SOURCE_DIR}/src/main/Scores.cpp
|
||||
${Platform_SOURCE_DIR}/src/experimental_clfs/DecisionTree.cpp
|
||||
${Platform_SOURCE_DIR}/src/experimental_clfs/AdaBoost.cpp
|
||||
)
|
||||
add_executable(${TEST_PLATFORM} ${TEST_SOURCES_PLATFORM})
|
||||
target_link_libraries(${TEST_PLATFORM} PUBLIC "${TORCH_LIBRARIES}" mdlp Catch2::Catch2WithMain BayesNet)
|
||||
target_link_libraries(${TEST_PLATFORM} PUBLIC "${TORCH_LIBRARIES}" fimdlp Catch2::Catch2WithMain bayesnet)
|
||||
add_test(NAME ${TEST_PLATFORM} COMMAND ${TEST_PLATFORM})
|
||||
endif(ENABLE_TESTING)
|
||||
|
547
tests/TestAdaBoost.cpp
Normal file
547
tests/TestAdaBoost.cpp
Normal file
@@ -0,0 +1,547 @@
|
||||
// ***************************************************************
|
||||
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
||||
// SPDX-FileType: SOURCE
|
||||
// SPDX-License-Identifier: MIT
|
||||
// ***************************************************************
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/matchers/catch_matchers_string.hpp>
|
||||
#include <catch2/matchers/catch_matchers_vector.hpp>
|
||||
#include <torch/torch.h>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include "experimental_clfs/AdaBoost.h"
|
||||
#include "experimental_clfs/DecisionTree.h"
|
||||
#include "experimental_clfs/TensorUtils.hpp"
|
||||
#include "TestUtils.h"
|
||||
|
||||
using namespace bayesnet;
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
static const bool DEBUG = false;
|
||||
|
||||
TEST_CASE("AdaBoost Construction", "[AdaBoost]")
|
||||
{
|
||||
SECTION("Default constructor")
|
||||
{
|
||||
REQUIRE_NOTHROW(AdaBoost());
|
||||
}
|
||||
|
||||
SECTION("Constructor with parameters")
|
||||
{
|
||||
REQUIRE_NOTHROW(AdaBoost(100, 2));
|
||||
}
|
||||
|
||||
SECTION("Constructor parameter access")
|
||||
{
|
||||
AdaBoost ada(75, 3);
|
||||
REQUIRE(ada.getNEstimators() == 75);
|
||||
REQUIRE(ada.getBaseMaxDepth() == 3);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("AdaBoost Hyperparameter Setting", "[AdaBoost]")
|
||||
{
|
||||
AdaBoost ada;
|
||||
|
||||
SECTION("Set individual hyperparameters")
|
||||
{
|
||||
REQUIRE_NOTHROW(ada.setNEstimators(100));
|
||||
REQUIRE_NOTHROW(ada.setBaseMaxDepth(5));
|
||||
|
||||
REQUIRE(ada.getNEstimators() == 100);
|
||||
REQUIRE(ada.getBaseMaxDepth() == 5);
|
||||
}
|
||||
|
||||
SECTION("Set hyperparameters via JSON")
|
||||
{
|
||||
nlohmann::json params;
|
||||
params["n_estimators"] = 80;
|
||||
params["base_max_depth"] = 4;
|
||||
|
||||
REQUIRE_NOTHROW(ada.setHyperparameters(params));
|
||||
}
|
||||
|
||||
SECTION("Invalid hyperparameters should throw")
|
||||
{
|
||||
nlohmann::json params;
|
||||
|
||||
// Negative n_estimators
|
||||
params["n_estimators"] = -1;
|
||||
REQUIRE_THROWS_AS(ada.setHyperparameters(params), std::invalid_argument);
|
||||
|
||||
// Zero n_estimators
|
||||
params["n_estimators"] = 0;
|
||||
REQUIRE_THROWS_AS(ada.setHyperparameters(params), std::invalid_argument);
|
||||
|
||||
// Negative base_max_depth
|
||||
params["n_estimators"] = 50;
|
||||
params["base_max_depth"] = -1;
|
||||
REQUIRE_THROWS_AS(ada.setHyperparameters(params), std::invalid_argument);
|
||||
|
||||
// Zero base_max_depth
|
||||
params["base_max_depth"] = 0;
|
||||
REQUIRE_THROWS_AS(ada.setHyperparameters(params), std::invalid_argument);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("AdaBoost Basic Functionality", "[AdaBoost]")
|
||||
{
|
||||
// Create a simple dataset
|
||||
int n_samples = 20;
|
||||
int n_features = 2;
|
||||
|
||||
std::vector<std::vector<int>> X(n_features, std::vector<int>(n_samples));
|
||||
std::vector<int> y(n_samples);
|
||||
|
||||
// Simple pattern: class depends on first feature
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
X[0][i] = i < 10 ? 0 : 1;
|
||||
X[1][i] = i % 2;
|
||||
y[i] = X[0][i]; // Class equals first feature
|
||||
}
|
||||
|
||||
std::vector<std::string> features = { "f1", "f2" };
|
||||
std::string className = "class";
|
||||
std::map<std::string, std::vector<int>> states;
|
||||
states["f1"] = { 0, 1 };
|
||||
states["f2"] = { 0, 1 };
|
||||
states["class"] = { 0, 1 };
|
||||
|
||||
SECTION("Training with vector interface")
|
||||
{
|
||||
AdaBoost ada(10, 3); // 10 estimators, max_depth = 3
|
||||
REQUIRE_NOTHROW(ada.fit(X, y, features, className, states, Smoothing_t::NONE));
|
||||
|
||||
// Check that we have the expected number of models
|
||||
auto weights = ada.getEstimatorWeights();
|
||||
REQUIRE(weights.size() <= 10); // Should be <= n_estimators
|
||||
REQUIRE(weights.size() > 0); // Should have at least one model
|
||||
|
||||
// Check training errors
|
||||
auto errors = ada.getTrainingErrors();
|
||||
REQUIRE(errors.size() == weights.size());
|
||||
|
||||
// All training errors should be less than 0.5 for this simple dataset
|
||||
for (double error : errors) {
|
||||
REQUIRE(error < 0.5);
|
||||
REQUIRE(error >= 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Prediction before fitting")
|
||||
{
|
||||
AdaBoost ada;
|
||||
REQUIRE_THROWS_WITH(ada.predict(X),
|
||||
ContainsSubstring("not been fitted"));
|
||||
REQUIRE_THROWS_WITH(ada.predict_proba(X),
|
||||
ContainsSubstring("not been fitted"));
|
||||
}
|
||||
|
||||
SECTION("Prediction with vector interface")
|
||||
{
|
||||
AdaBoost ada(10, 3);
|
||||
ada.setDebug(DEBUG); // Enable debug to investigate
|
||||
ada.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
auto predictions = ada.predict(X);
|
||||
REQUIRE(predictions.size() == static_cast<size_t>(n_samples));
|
||||
// Check accuracy
|
||||
int correct = 0;
|
||||
for (size_t i = 0; i < predictions.size(); i++) {
|
||||
if (predictions[i] == y[i]) correct++;
|
||||
}
|
||||
double accuracy = static_cast<double>(correct) / n_samples;
|
||||
REQUIRE(accuracy > 0.99); // Should achieve good accuracy on this simple dataset
|
||||
auto accuracy_computed = ada.score(X, y);
|
||||
REQUIRE(accuracy_computed == Catch::Approx(accuracy).epsilon(1e-6));
|
||||
}
|
||||
|
||||
SECTION("Probability predictions with vector interface")
|
||||
{
|
||||
AdaBoost ada(10, 3);
|
||||
ada.setDebug(DEBUG); // ENABLE DEBUG HERE TOO
|
||||
ada.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
auto proba = ada.predict_proba(X);
|
||||
REQUIRE(proba.size() == static_cast<size_t>(n_samples));
|
||||
REQUIRE(proba[0].size() == 2); // Two classes
|
||||
|
||||
// Check probabilities sum to 1 and are valid
|
||||
auto predictions = ada.predict(X);
|
||||
int correct = 0;
|
||||
for (size_t i = 0; i < proba.size(); i++) {
|
||||
auto p = proba[i];
|
||||
auto pred = predictions[i];
|
||||
REQUIRE(p.size() == 2);
|
||||
REQUIRE(p[0] >= 0.0);
|
||||
REQUIRE(p[1] >= 0.0);
|
||||
double sum = p[0] + p[1];
|
||||
REQUIRE(sum == Catch::Approx(1.0).epsilon(1e-6));
|
||||
// compute the predicted class based on probabilities
|
||||
auto predicted_class = (p[0] > p[1]) ? 0 : 1;
|
||||
// compute accuracy based on predictions
|
||||
if (predicted_class == y[i]) {
|
||||
correct++;
|
||||
}
|
||||
|
||||
INFO("Probability test - Sample " << i << ": pred=" << pred << ", probs=[" << p[0] << "," << p[1] << "], expected_from_probs=" << predicted_class);
|
||||
|
||||
// Handle ties
|
||||
if (std::abs(p[0] - p[1]) < 1e-10) {
|
||||
INFO("Tie detected in probabilities");
|
||||
// Either prediction is valid in case of tie
|
||||
} else {
|
||||
// Check that predict_proba matches the expected predict value
|
||||
REQUIRE(pred == predicted_class);
|
||||
}
|
||||
}
|
||||
double accuracy = static_cast<double>(correct) / n_samples;
|
||||
REQUIRE(accuracy > 0.99); // Should achieve good accuracy on this simple dataset
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("AdaBoost Tensor Interface", "[AdaBoost]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
|
||||
SECTION("Training with tensor format")
|
||||
{
|
||||
AdaBoost ada(20, 3);
|
||||
|
||||
INFO("Dataset shape: " << raw.dataset.sizes());
|
||||
INFO("Features: " << raw.featurest.size());
|
||||
INFO("Samples: " << raw.nSamples);
|
||||
|
||||
// AdaBoost expects dataset in format: features x samples, with labels as last row
|
||||
REQUIRE_NOTHROW(ada.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, Smoothing_t::NONE));
|
||||
|
||||
// Test prediction with tensor
|
||||
auto predictions = ada.predict(raw.Xt);
|
||||
REQUIRE(predictions.size(0) == raw.yt.size(0));
|
||||
|
||||
// Calculate accuracy
|
||||
auto correct = torch::sum(predictions == raw.yt).item<int>();
|
||||
double accuracy = static_cast<double>(correct) / raw.yt.size(0);
|
||||
auto accuracy_computed = ada.score(raw.Xt, raw.yt);
|
||||
REQUIRE(accuracy_computed == Catch::Approx(accuracy).epsilon(1e-6));
|
||||
REQUIRE(accuracy > 0.97); // Should achieve good accuracy on Iris
|
||||
|
||||
// Test probability predictions with tensor
|
||||
auto proba = ada.predict_proba(raw.Xt);
|
||||
REQUIRE(proba.size(0) == raw.yt.size(0));
|
||||
REQUIRE(proba.size(1) == 3); // Three classes in Iris
|
||||
|
||||
// Check probabilities sum to 1
|
||||
auto prob_sums = torch::sum(proba, 1);
|
||||
for (int i = 0; i < prob_sums.size(0); i++) {
|
||||
REQUIRE(prob_sums[i].item<double>() == Catch::Approx(1.0).epsilon(1e-6));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("AdaBoost SAMME Algorithm Validation", "[AdaBoost]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
|
||||
SECTION("Prediction consistency with probabilities")
|
||||
{
|
||||
AdaBoost ada(15, 3);
|
||||
ada.setDebug(DEBUG); // Enable debug for ALL instances
|
||||
ada.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, Smoothing_t::NONE);
|
||||
|
||||
auto predictions = ada.predict(raw.Xt);
|
||||
auto probabilities = ada.predict_proba(raw.Xt);
|
||||
|
||||
REQUIRE(predictions.size(0) == probabilities.size(0));
|
||||
REQUIRE(probabilities.size(1) == 3); // Three classes in Iris
|
||||
|
||||
// For each sample, predicted class should correspond to highest probability
|
||||
for (int i = 0; i < predictions.size(0); i++) {
|
||||
int predicted_class = predictions[i].item<int>();
|
||||
auto probs = probabilities[i];
|
||||
|
||||
// Find class with highest probability
|
||||
auto max_prob_idx = torch::argmax(probs).item<int>();
|
||||
|
||||
// Predicted class should match class with highest probability
|
||||
REQUIRE(predicted_class == max_prob_idx);
|
||||
|
||||
// Probabilities should sum to 1
|
||||
double sum_probs = torch::sum(probs).item<double>();
|
||||
REQUIRE(sum_probs == Catch::Approx(1.0).epsilon(1e-6));
|
||||
|
||||
// All probabilities should be non-negative
|
||||
for (int j = 0; j < 3; j++) {
|
||||
REQUIRE(probs[j].item<double>() >= 0.0);
|
||||
REQUIRE(probs[j].item<double>() <= 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Weighted voting verification")
|
||||
{
|
||||
// Simple dataset where we can verify the weighted voting
|
||||
std::vector<std::vector<int>> X = { {0,0,1,1}, {0,1,0,1} };
|
||||
std::vector<int> y = { 0, 1, 1, 0 };
|
||||
std::vector<std::string> features = { "f1", "f2" };
|
||||
std::string className = "class";
|
||||
std::map<std::string, std::vector<int>> states;
|
||||
states["f1"] = { 0, 1 };
|
||||
states["f2"] = { 0, 1 };
|
||||
states["class"] = { 0, 1 };
|
||||
|
||||
AdaBoost ada(5, 2);
|
||||
ada.setDebug(DEBUG); // Enable debug for detailed logging
|
||||
ada.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
INFO("=== Final test verification ===");
|
||||
auto predictions = ada.predict(X);
|
||||
auto probabilities = ada.predict_proba(X);
|
||||
auto alphas = ada.getEstimatorWeights();
|
||||
|
||||
INFO("Training info:");
|
||||
for (size_t i = 0; i < alphas.size(); i++) {
|
||||
INFO(" Model " << i << ": alpha=" << alphas[i]);
|
||||
}
|
||||
|
||||
REQUIRE(predictions.size() == 4);
|
||||
REQUIRE(probabilities.size() == 4);
|
||||
REQUIRE(probabilities[0].size() == 2); // Two classes
|
||||
REQUIRE(alphas.size() > 0);
|
||||
|
||||
// Verify that estimator weights are reasonable
|
||||
for (double alpha : alphas) {
|
||||
REQUIRE(alpha >= 0.0); // Alphas should be non-negative
|
||||
}
|
||||
|
||||
// Verify prediction-probability consistency with detailed logging
|
||||
for (size_t i = 0; i < predictions.size(); i++) {
|
||||
int pred = predictions[i];
|
||||
auto probs = probabilities[i];
|
||||
|
||||
INFO("Final check - Sample " << i << ": predicted=" << pred << ", probabilities=[" << probs[0] << "," << probs[1] << "]");
|
||||
|
||||
// Handle the case where probabilities are exactly equal (tie)
|
||||
if (std::abs(probs[0] - probs[1]) < 1e-10) {
|
||||
INFO("Tie detected in probabilities - either prediction is valid");
|
||||
REQUIRE((pred == 0 || pred == 1));
|
||||
} else {
|
||||
// Normal case - prediction should match max probability
|
||||
int expected_pred = (probs[0] > probs[1]) ? 0 : 1;
|
||||
INFO("Expected prediction based on probs: " << expected_pred);
|
||||
REQUIRE(pred == expected_pred);
|
||||
}
|
||||
|
||||
REQUIRE(probs[0] + probs[1] == Catch::Approx(1.0).epsilon(1e-6));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Empty models edge case")
|
||||
{
|
||||
AdaBoost ada(1, 1);
|
||||
ada.setDebug(DEBUG); // Enable debug for ALL instances
|
||||
|
||||
// Try to predict before fitting
|
||||
std::vector<std::vector<int>> X = { {0}, {1} };
|
||||
REQUIRE_THROWS_WITH(ada.predict(X), ContainsSubstring("not been fitted"));
|
||||
REQUIRE_THROWS_WITH(ada.predict_proba(X), ContainsSubstring("not been fitted"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("AdaBoost Debug - Simple Dataset Analysis", "[AdaBoost][debug]")
|
||||
{
|
||||
// Create the exact same simple dataset that was failing
|
||||
int n_samples = 20;
|
||||
int n_features = 2;
|
||||
|
||||
std::vector<std::vector<int>> X(n_features, std::vector<int>(n_samples));
|
||||
std::vector<int> y(n_samples);
|
||||
|
||||
// Simple pattern: class depends on first feature
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
X[0][i] = i < 10 ? 0 : 1;
|
||||
X[1][i] = i % 2;
|
||||
y[i] = X[0][i]; // Class equals first feature
|
||||
}
|
||||
|
||||
std::vector<std::string> features = { "f1", "f2" };
|
||||
std::string className = "class";
|
||||
std::map<std::string, std::vector<int>> states;
|
||||
states["f1"] = { 0, 1 };
|
||||
states["f2"] = { 0, 1 };
|
||||
states["class"] = { 0, 1 };
|
||||
|
||||
SECTION("Debug training process")
|
||||
{
|
||||
AdaBoost ada(5, 3); // Few estimators for debugging
|
||||
ada.setDebug(DEBUG);
|
||||
|
||||
// This should work perfectly on this simple dataset
|
||||
REQUIRE_NOTHROW(ada.fit(X, y, features, className, states, Smoothing_t::NONE));
|
||||
|
||||
// Get training details
|
||||
auto weights = ada.getEstimatorWeights();
|
||||
auto errors = ada.getTrainingErrors();
|
||||
|
||||
INFO("Number of models trained: " << weights.size());
|
||||
INFO("Training errors: ");
|
||||
for (size_t i = 0; i < errors.size(); i++) {
|
||||
INFO(" Model " << i << ": error=" << errors[i] << ", weight=" << weights[i]);
|
||||
}
|
||||
|
||||
// Should have at least one model
|
||||
REQUIRE(weights.size() > 0);
|
||||
REQUIRE(errors.size() == weights.size());
|
||||
|
||||
// All training errors should be reasonable for this simple dataset
|
||||
for (double error : errors) {
|
||||
REQUIRE(error >= 0.0);
|
||||
REQUIRE(error < 0.5); // Should be better than random
|
||||
}
|
||||
|
||||
// Test predictions
|
||||
auto predictions = ada.predict(X);
|
||||
REQUIRE(predictions.size() == static_cast<size_t>(n_samples));
|
||||
|
||||
// Calculate accuracy
|
||||
int correct = 0;
|
||||
for (size_t i = 0; i < predictions.size(); i++) {
|
||||
if (predictions[i] == y[i]) correct++;
|
||||
INFO("Sample " << i << ": predicted=" << predictions[i] << ", actual=" << y[i]);
|
||||
}
|
||||
double accuracy = static_cast<double>(correct) / n_samples;
|
||||
INFO("Accuracy: " << accuracy);
|
||||
|
||||
// Should achieve high accuracy on this perfectly separable dataset
|
||||
REQUIRE(accuracy >= 0.9); // Lower threshold for debugging
|
||||
|
||||
// Test probability predictions
|
||||
auto proba = ada.predict_proba(X);
|
||||
REQUIRE(proba.size() == static_cast<size_t>(n_samples));
|
||||
|
||||
// Verify probabilities are valid
|
||||
for (size_t i = 0; i < proba.size(); i++) {
|
||||
auto p = proba[i];
|
||||
REQUIRE(p.size() == 2);
|
||||
REQUIRE(p[0] >= 0.0);
|
||||
REQUIRE(p[1] >= 0.0);
|
||||
double sum = p[0] + p[1];
|
||||
REQUIRE(sum == Catch::Approx(1.0).epsilon(1e-6));
|
||||
|
||||
// Predicted class should match highest probability
|
||||
int pred_class = predictions[i];
|
||||
|
||||
// Handle ties
|
||||
if (std::abs(p[0] - p[1]) < 1e-10) {
|
||||
INFO("Tie detected - probabilities are equal");
|
||||
REQUIRE((pred_class == 0 || pred_class == 1));
|
||||
} else {
|
||||
REQUIRE(pred_class == (p[0] > p[1] ? 0 : 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Compare with single DecisionTree")
|
||||
{
|
||||
// Test that AdaBoost performs at least as well as a single tree
|
||||
DecisionTree single_tree(3, 2, 1);
|
||||
single_tree.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
auto tree_predictions = single_tree.predict(X);
|
||||
|
||||
int tree_correct = 0;
|
||||
for (size_t i = 0; i < tree_predictions.size(); i++) {
|
||||
if (tree_predictions[i] == y[i]) tree_correct++;
|
||||
}
|
||||
double tree_accuracy = static_cast<double>(tree_correct) / n_samples;
|
||||
|
||||
AdaBoost ada(5, 3);
|
||||
ada.setDebug(DEBUG);
|
||||
ada.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
auto ada_predictions = ada.predict(X);
|
||||
|
||||
int ada_correct = 0;
|
||||
for (size_t i = 0; i < ada_predictions.size(); i++) {
|
||||
if (ada_predictions[i] == y[i]) ada_correct++;
|
||||
}
|
||||
double ada_accuracy = static_cast<double>(ada_correct) / n_samples;
|
||||
|
||||
INFO("DecisionTree accuracy: " << tree_accuracy);
|
||||
INFO("AdaBoost accuracy: " << ada_accuracy);
|
||||
|
||||
// AdaBoost should perform at least as well as single tree
|
||||
// (allowing small tolerance for numerical differences)
|
||||
REQUIRE(ada_accuracy >= tree_accuracy - 0.1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("AdaBoost Predict-Proba Consistency Fix", "[AdaBoost][consistency]")
|
||||
{
|
||||
// Simple binary classification dataset
|
||||
std::vector<std::vector<int>> X = { {0,0,1,1}, {0,1,0,1} };
|
||||
std::vector<int> y = { 0, 0, 1, 1 };
|
||||
std::vector<std::string> features = { "f1", "f2" };
|
||||
std::string className = "class";
|
||||
std::map<std::string, std::vector<int>> states;
|
||||
states["f1"] = { 0, 1 };
|
||||
states["f2"] = { 0, 1 };
|
||||
states["class"] = { 0, 1 };
|
||||
|
||||
SECTION("Binary classification consistency")
|
||||
{
|
||||
AdaBoost ada(3, 2);
|
||||
ada.setDebug(DEBUG); // Enable debug output
|
||||
ada.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
INFO("=== Debugging predict vs predict_proba consistency ===");
|
||||
|
||||
// Get training info
|
||||
auto alphas = ada.getEstimatorWeights();
|
||||
auto errors = ada.getTrainingErrors();
|
||||
|
||||
INFO("Training completed:");
|
||||
INFO(" Number of models: " << alphas.size());
|
||||
for (size_t i = 0; i < alphas.size(); i++) {
|
||||
INFO(" Model " << i << ": alpha=" << alphas[i] << ", error=" << errors[i]);
|
||||
}
|
||||
|
||||
auto predictions = ada.predict(X);
|
||||
auto probabilities = ada.predict_proba(X);
|
||||
|
||||
// Verify consistency for each sample
|
||||
for (size_t i = 0; i < predictions.size(); i++) {
|
||||
int predicted_class = predictions[i];
|
||||
auto probs = probabilities[i];
|
||||
|
||||
INFO("Sample " << i << ":");
|
||||
INFO(" Features: [" << X[0][i] << ", " << X[1][i] << "]");
|
||||
INFO(" True class: " << y[i]);
|
||||
INFO(" Predicted class: " << predicted_class);
|
||||
INFO(" Probabilities: [" << probs[0] << ", " << probs[1] << "]");
|
||||
|
||||
// The predicted class should be the one with highest probability
|
||||
int max_prob_class = (probs[0] > probs[1]) ? 0 : 1;
|
||||
INFO(" Max prob class: " << max_prob_class);
|
||||
|
||||
// Handle tie case (when probabilities are equal)
|
||||
if (std::abs(probs[0] - probs[1]) < 1e-10) {
|
||||
INFO(" Tie detected - probabilities are equal");
|
||||
// In case of tie, either prediction is valid
|
||||
REQUIRE((predicted_class == 0 || predicted_class == 1));
|
||||
} else {
|
||||
REQUIRE(predicted_class == max_prob_class);
|
||||
}
|
||||
|
||||
// Probabilities should sum to 1
|
||||
double sum_probs = probs[0] + probs[1];
|
||||
REQUIRE(sum_probs == Catch::Approx(1.0).epsilon(1e-6));
|
||||
|
||||
// All probabilities should be valid
|
||||
REQUIRE(probs[0] >= 0.0);
|
||||
REQUIRE(probs[1] >= 0.0);
|
||||
REQUIRE(probs[0] <= 1.0);
|
||||
REQUIRE(probs[1] <= 1.0);
|
||||
}
|
||||
}
|
||||
}
|
311
tests/TestDecisionTree.cpp
Normal file
311
tests/TestDecisionTree.cpp
Normal file
@@ -0,0 +1,311 @@
|
||||
// ***************************************************************
|
||||
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
||||
// SPDX-FileType: SOURCE
|
||||
// SPDX-License-Identifier: MIT
|
||||
// ***************************************************************
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/matchers/catch_matchers_string.hpp>
|
||||
#include <catch2/matchers/catch_matchers_vector.hpp>
|
||||
#include <torch/torch.h>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include "experimental_clfs/DecisionTree.h"
|
||||
#include "TestUtils.h"
|
||||
|
||||
using namespace bayesnet;
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("DecisionTree Construction", "[DecisionTree]")
|
||||
{
|
||||
SECTION("Default constructor")
|
||||
{
|
||||
REQUIRE_NOTHROW(DecisionTree());
|
||||
}
|
||||
|
||||
SECTION("Constructor with parameters")
|
||||
{
|
||||
REQUIRE_NOTHROW(DecisionTree(5, 10, 3));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DecisionTree Hyperparameter Setting", "[DecisionTree]")
|
||||
{
|
||||
DecisionTree dt;
|
||||
|
||||
SECTION("Set individual hyperparameters")
|
||||
{
|
||||
REQUIRE_NOTHROW(dt.setMaxDepth(10));
|
||||
REQUIRE_NOTHROW(dt.setMinSamplesSplit(5));
|
||||
REQUIRE_NOTHROW(dt.setMinSamplesLeaf(2));
|
||||
REQUIRE(dt.getMaxDepth() == 10);
|
||||
REQUIRE(dt.getMinSamplesSplit() == 5);
|
||||
REQUIRE(dt.getMinSamplesLeaf() == 2);
|
||||
}
|
||||
|
||||
SECTION("Set hyperparameters via JSON")
|
||||
{
|
||||
nlohmann::json params;
|
||||
params["max_depth"] = 7;
|
||||
params["min_samples_split"] = 4;
|
||||
params["min_samples_leaf"] = 2;
|
||||
|
||||
REQUIRE_NOTHROW(dt.setHyperparameters(params));
|
||||
REQUIRE(dt.getMaxDepth() == 7);
|
||||
REQUIRE(dt.getMinSamplesSplit() == 4);
|
||||
REQUIRE(dt.getMinSamplesLeaf() == 2);
|
||||
}
|
||||
|
||||
SECTION("Invalid hyperparameters should throw")
|
||||
{
|
||||
nlohmann::json params;
|
||||
|
||||
// Negative max_depth
|
||||
params["max_depth"] = -1;
|
||||
REQUIRE_THROWS_AS(dt.setHyperparameters(params), std::invalid_argument);
|
||||
|
||||
// Zero min_samples_split
|
||||
params["max_depth"] = 5;
|
||||
params["min_samples_split"] = 0;
|
||||
REQUIRE_THROWS_AS(dt.setHyperparameters(params), std::invalid_argument);
|
||||
|
||||
// Negative min_samples_leaf
|
||||
params["min_samples_split"] = 2;
|
||||
params["min_samples_leaf"] = -5;
|
||||
REQUIRE_THROWS_AS(dt.setHyperparameters(params), std::invalid_argument);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DecisionTree Basic Functionality", "[DecisionTree]")
|
||||
{
|
||||
// Create a simple dataset
|
||||
int n_samples = 20;
|
||||
int n_features = 2;
|
||||
|
||||
std::vector<std::vector<int>> X(n_features, std::vector<int>(n_samples));
|
||||
std::vector<int> y(n_samples);
|
||||
|
||||
// Simple pattern: class depends on first feature
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
X[0][i] = i < 10 ? 0 : 1;
|
||||
X[1][i] = i % 2;
|
||||
y[i] = X[0][i]; // Class equals first feature
|
||||
}
|
||||
|
||||
std::vector<std::string> features = { "f1", "f2" };
|
||||
std::string className = "class";
|
||||
std::map<std::string, std::vector<int>> states;
|
||||
states["f1"] = { 0, 1 };
|
||||
states["f2"] = { 0, 1 };
|
||||
states["class"] = { 0, 1 };
|
||||
|
||||
SECTION("Training with vector interface")
|
||||
{
|
||||
DecisionTree dt(3, 2, 1);
|
||||
REQUIRE_NOTHROW(dt.fit(X, y, features, className, states, Smoothing_t::NONE));
|
||||
|
||||
auto predictions = dt.predict(X);
|
||||
REQUIRE(predictions.size() == static_cast<size_t>(n_samples));
|
||||
|
||||
// Should achieve perfect accuracy on this simple dataset
|
||||
int correct = 0;
|
||||
for (size_t i = 0; i < predictions.size(); i++) {
|
||||
if (predictions[i] == y[i]) correct++;
|
||||
}
|
||||
REQUIRE(correct == n_samples);
|
||||
}
|
||||
|
||||
SECTION("Prediction before fitting")
|
||||
{
|
||||
DecisionTree dt;
|
||||
REQUIRE_THROWS_WITH(dt.predict(X),
|
||||
ContainsSubstring("Classifier has not been fitted"));
|
||||
}
|
||||
|
||||
SECTION("Probability predictions")
|
||||
{
|
||||
DecisionTree dt(3, 2, 1);
|
||||
dt.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
auto proba = dt.predict_proba(X);
|
||||
REQUIRE(proba.size() == static_cast<size_t>(n_samples));
|
||||
REQUIRE(proba[0].size() == 2); // Two classes
|
||||
|
||||
// Check probabilities sum to 1 and probabilities are valid
|
||||
auto predictions = dt.predict(X);
|
||||
for (size_t i = 0; i < proba.size(); i++) {
|
||||
auto p = proba[i];
|
||||
auto pred = predictions[i];
|
||||
REQUIRE(p.size() == 2);
|
||||
REQUIRE(p[0] >= 0.0);
|
||||
REQUIRE(p[1] >= 0.0);
|
||||
double sum = p[0] + p[1];
|
||||
//Check that prodict_proba matches the expected predict value
|
||||
REQUIRE(pred == (p[0] > p[1] ? 0 : 1));
|
||||
REQUIRE(sum == Catch::Approx(1.0).epsilon(1e-6));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DecisionTree on Iris Dataset", "[DecisionTree][iris]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
|
||||
SECTION("Training with dataset format")
|
||||
{
|
||||
DecisionTree dt(5, 2, 1);
|
||||
|
||||
INFO("Dataset shape: " << raw.dataset.sizes());
|
||||
INFO("Features: " << raw.featurest.size());
|
||||
INFO("Samples: " << raw.nSamples);
|
||||
|
||||
// DecisionTree expects dataset in format: features x samples, with labels as last row
|
||||
REQUIRE_NOTHROW(dt.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, Smoothing_t::NONE));
|
||||
|
||||
// Test prediction
|
||||
auto predictions = dt.predict(raw.Xt);
|
||||
REQUIRE(predictions.size(0) == raw.yt.size(0));
|
||||
|
||||
// Calculate accuracy
|
||||
auto correct = torch::sum(predictions == raw.yt).item<int>();
|
||||
double accuracy = static_cast<double>(correct) / raw.yt.size(0);
|
||||
double acurracy_computed = dt.score(raw.Xt, raw.yt);
|
||||
REQUIRE(accuracy > 0.97); // Reasonable accuracy for Iris
|
||||
REQUIRE(acurracy_computed == Catch::Approx(accuracy).epsilon(1e-6));
|
||||
}
|
||||
|
||||
SECTION("Training with vector interface")
|
||||
{
|
||||
DecisionTree dt(5, 2, 1);
|
||||
|
||||
REQUIRE_NOTHROW(dt.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv, Smoothing_t::NONE));
|
||||
|
||||
// std::cout << "Tree structure:\n";
|
||||
// auto graph_lines = dt.graph("Iris Decision Tree");
|
||||
// for (const auto& line : graph_lines) {
|
||||
// std::cout << line << "\n";
|
||||
// }
|
||||
auto predictions = dt.predict(raw.Xv);
|
||||
REQUIRE(predictions.size() == raw.yv.size());
|
||||
}
|
||||
|
||||
SECTION("Different tree depths")
|
||||
{
|
||||
std::vector<int> depths = { 1, 3, 5 };
|
||||
|
||||
for (int depth : depths) {
|
||||
DecisionTree dt(depth, 2, 1);
|
||||
dt.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, Smoothing_t::NONE);
|
||||
|
||||
auto predictions = dt.predict(raw.Xt);
|
||||
REQUIRE(predictions.size(0) == raw.yt.size(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DecisionTree Edge Cases", "[DecisionTree]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
|
||||
SECTION("Very shallow tree")
|
||||
{
|
||||
DecisionTree dt(1, 2, 1); // depth = 1
|
||||
dt.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, Smoothing_t::NONE);
|
||||
|
||||
auto predictions = dt.predict(raw.Xt);
|
||||
REQUIRE(predictions.size(0) == raw.yt.size(0));
|
||||
|
||||
// With depth 1, should have at most 2 unique predictions
|
||||
auto unique_vals = at::_unique(predictions);
|
||||
REQUIRE(std::get<0>(unique_vals).size(0) <= 2);
|
||||
}
|
||||
|
||||
SECTION("High min_samples_split")
|
||||
{
|
||||
DecisionTree dt(10, 50, 1);
|
||||
dt.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, Smoothing_t::NONE);
|
||||
|
||||
auto predictions = dt.predict(raw.Xt);
|
||||
REQUIRE(predictions.size(0) == raw.yt.size(0));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DecisionTree Graph Visualization", "[DecisionTree]")
|
||||
{
|
||||
// Simple dataset
|
||||
std::vector<std::vector<int>> X = { {0,0,0,1}, {0,1,1,1} }; // XOR pattern
|
||||
std::vector<int> y = { 0, 1, 1, 0 }; // XOR pattern
|
||||
std::vector<std::string> features = { "x1", "x2" };
|
||||
std::string className = "xor";
|
||||
std::map<std::string, std::vector<int>> states;
|
||||
states["x1"] = { 0, 1 };
|
||||
states["x2"] = { 0, 1 };
|
||||
states["xor"] = { 0, 1 };
|
||||
|
||||
SECTION("Graph generation")
|
||||
{
|
||||
DecisionTree dt(2, 1, 1);
|
||||
dt.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
auto graph_lines = dt.graph();
|
||||
|
||||
REQUIRE(graph_lines.size() > 2);
|
||||
REQUIRE(graph_lines.front() == "digraph DecisionTree {");
|
||||
REQUIRE(graph_lines.back() == "}");
|
||||
|
||||
// Should contain node definitions
|
||||
bool has_nodes = false;
|
||||
for (const auto& line : graph_lines) {
|
||||
if (line.find("node") != std::string::npos) {
|
||||
has_nodes = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
REQUIRE(has_nodes);
|
||||
}
|
||||
|
||||
SECTION("Graph with title")
|
||||
{
|
||||
DecisionTree dt(2, 1, 1);
|
||||
dt.fit(X, y, features, className, states, Smoothing_t::NONE);
|
||||
|
||||
auto graph_lines = dt.graph("XOR Tree");
|
||||
|
||||
bool has_title = false;
|
||||
for (const auto& line : graph_lines) {
|
||||
if (line.find("label=\"XOR Tree\"") != std::string::npos) {
|
||||
has_title = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
REQUIRE(has_title);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DecisionTree with Weights", "[DecisionTree]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
|
||||
SECTION("Uniform weights")
|
||||
{
|
||||
DecisionTree dt(5, 2, 1);
|
||||
dt.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, raw.weights, Smoothing_t::NONE);
|
||||
|
||||
auto predictions = dt.predict(raw.Xt);
|
||||
REQUIRE(predictions.size(0) == raw.yt.size(0));
|
||||
}
|
||||
|
||||
SECTION("Non-uniform weights")
|
||||
{
|
||||
auto weights = torch::ones({ raw.nSamples });
|
||||
weights.index({ torch::indexing::Slice(0, 50) }) *= 2.0; // Emphasize first class
|
||||
weights = weights / weights.sum();
|
||||
|
||||
DecisionTree dt(5, 2, 1);
|
||||
dt.fit(raw.dataset, raw.featurest, raw.classNamet, raw.statest, weights, Smoothing_t::NONE);
|
||||
|
||||
auto predictions = dt.predict(raw.Xt);
|
||||
REQUIRE(predictions.size(0) == raw.yt.size(0));
|
||||
}
|
||||
}
|
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include "TestUtils.h"
|
||||
#include "folding.hpp"
|
||||
#include <ArffFiles.hpp>
|
||||
#include <ArffFiles/ArffFiles.hpp>
|
||||
#include <bayesnet/classifiers/TAN.h>
|
||||
#include "config_platform.h"
|
||||
|
||||
@@ -20,17 +20,17 @@ TEST_CASE("Test Platform version", "[Platform]")
|
||||
TEST_CASE("Test Folding library version", "[Folding]")
|
||||
{
|
||||
std::string version = folding::KFold(5, 100).version();
|
||||
REQUIRE(version == "1.1.0");
|
||||
REQUIRE(version == "1.1.1");
|
||||
}
|
||||
TEST_CASE("Test BayesNet version", "[BayesNet]")
|
||||
{
|
||||
std::string version = bayesnet::TAN().getVersion();
|
||||
REQUIRE(version == "1.0.6");
|
||||
REQUIRE(version == "1.1.2");
|
||||
}
|
||||
TEST_CASE("Test mdlp version", "[mdlp]")
|
||||
{
|
||||
std::string version = mdlp::CPPFImdlp::version();
|
||||
REQUIRE(version == "2.0.0");
|
||||
REQUIRE(version == "2.0.1");
|
||||
}
|
||||
TEST_CASE("Test Arff version", "[Arff]")
|
||||
{
|
||||
|
@@ -14,38 +14,40 @@
|
||||
using json = nlohmann::ordered_json;
|
||||
auto epsilon = 1e-4;
|
||||
|
||||
void make_test_bin(int TP, int TN, int FP, int FN, std::vector<int>& y_test, std::vector<int>& y_pred)
|
||||
void make_test_bin(int TP, int TN, int FP, int FN, std::vector<int>& y_test, torch::Tensor& y_pred)
|
||||
{
|
||||
// TP
|
||||
std::vector<std::array<double, 2>> probs;
|
||||
// TP: true positive (label 1, predicted 1)
|
||||
for (int i = 0; i < TP; i++) {
|
||||
y_test.push_back(1);
|
||||
y_pred.push_back(1);
|
||||
probs.push_back({ 0.0, 1.0 }); // P(class 0)=0, P(class 1)=1
|
||||
}
|
||||
// TN
|
||||
// TN: true negative (label 0, predicted 0)
|
||||
for (int i = 0; i < TN; i++) {
|
||||
y_test.push_back(0);
|
||||
y_pred.push_back(0);
|
||||
probs.push_back({ 1.0, 0.0 }); // P(class 0)=1, P(class 1)=0
|
||||
}
|
||||
// FP
|
||||
// FP: false positive (label 0, predicted 1)
|
||||
for (int i = 0; i < FP; i++) {
|
||||
y_test.push_back(0);
|
||||
y_pred.push_back(1);
|
||||
probs.push_back({ 0.0, 1.0 }); // P(class 0)=0, P(class 1)=1
|
||||
}
|
||||
// FN
|
||||
// FN: false negative (label 1, predicted 0)
|
||||
for (int i = 0; i < FN; i++) {
|
||||
y_test.push_back(1);
|
||||
y_pred.push_back(0);
|
||||
probs.push_back({ 1.0, 0.0 }); // P(class 0)=1, P(class 1)=0
|
||||
}
|
||||
// Convert to torch::Tensor of double, shape [N,2]
|
||||
y_pred = torch::from_blob(probs.data(), { (long)probs.size(), 2 }, torch::kFloat64).clone();
|
||||
}
|
||||
|
||||
TEST_CASE("Scores binary", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test;
|
||||
std::vector<int> y_pred;
|
||||
torch::Tensor y_pred;
|
||||
make_test_bin(197, 210, 52, 41, y_test, y_pred);
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 2);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 2);
|
||||
REQUIRE(scores.accuracy() == Catch::Approx(0.814).epsilon(epsilon));
|
||||
REQUIRE(scores.f1_score(0) == Catch::Approx(0.818713));
|
||||
REQUIRE(scores.f1_score(1) == Catch::Approx(0.809035));
|
||||
@@ -64,10 +66,23 @@ TEST_CASE("Scores binary", "[Scores]")
|
||||
TEST_CASE("Scores multiclass", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test = { 0, 2, 2, 2, 2, 0, 1, 2, 0, 2 };
|
||||
std::vector<int> y_pred = { 0, 1, 2, 2, 1, 1, 1, 0, 0, 2 };
|
||||
// Refactor y_pred to a tensor of shape [10, 3] with probabilities
|
||||
std::vector<std::array<double, 3>> probs = {
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 } // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
};
|
||||
torch::Tensor y_pred = torch::from_blob(probs.data(), { (long)probs.size(), 3 }, torch::kFloat64).clone();
|
||||
// Convert y_test to a tensor
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 3);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 3);
|
||||
REQUIRE(scores.accuracy() == Catch::Approx(0.6).epsilon(epsilon));
|
||||
REQUIRE(scores.f1_score(0) == Catch::Approx(0.666667));
|
||||
REQUIRE(scores.f1_score(1) == Catch::Approx(0.4));
|
||||
@@ -84,10 +99,21 @@ TEST_CASE("Scores multiclass", "[Scores]")
|
||||
TEST_CASE("Test Confusion Matrix Values", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test = { 0, 2, 2, 2, 2, 0, 1, 2, 0, 2 };
|
||||
std::vector<int> y_pred = { 0, 1, 2, 2, 1, 1, 1, 0, 0, 2 };
|
||||
std::vector<std::array<double, 3>> probs = {
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 } // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
};
|
||||
torch::Tensor y_pred = torch::from_blob(probs.data(), { (long)probs.size(), 3 }, torch::kFloat64).clone();
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 3);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 3);
|
||||
auto confusion_matrix = scores.get_confusion_matrix();
|
||||
REQUIRE(confusion_matrix[0][0].item<int>() == 2);
|
||||
REQUIRE(confusion_matrix[0][1].item<int>() == 1);
|
||||
@@ -102,11 +128,22 @@ TEST_CASE("Test Confusion Matrix Values", "[Scores]")
|
||||
TEST_CASE("Confusion Matrix JSON", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test = { 0, 2, 2, 2, 2, 0, 1, 2, 0, 2 };
|
||||
std::vector<int> y_pred = { 0, 1, 2, 2, 1, 1, 1, 0, 0, 2 };
|
||||
std::vector<std::array<double, 3>> probs = {
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 } // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
};
|
||||
torch::Tensor y_pred = torch::from_blob(probs.data(), { (long)probs.size(), 3 }, torch::kFloat64).clone();
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
std::vector<std::string> labels = { "Aeroplane", "Boat", "Car" };
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 3, labels);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 3, labels);
|
||||
auto res_json_int = scores.get_confusion_matrix_json();
|
||||
REQUIRE(res_json_int[0][0] == 2);
|
||||
REQUIRE(res_json_int[0][1] == 1);
|
||||
@@ -131,11 +168,22 @@ TEST_CASE("Confusion Matrix JSON", "[Scores]")
|
||||
TEST_CASE("Classification Report", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test = { 0, 2, 2, 2, 2, 0, 1, 2, 0, 2 };
|
||||
std::vector<int> y_pred = { 0, 1, 2, 2, 1, 1, 1, 0, 0, 2 };
|
||||
std::vector<std::array<double, 3>> probs = {
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 } // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
};
|
||||
torch::Tensor y_pred = torch::from_blob(probs.data(), { (long)probs.size(), 3 }, torch::kFloat64).clone();
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
std::vector<std::string> labels = { "Aeroplane", "Boat", "Car" };
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 3, labels);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 3, labels);
|
||||
auto report = scores.classification_report(Colors::BLUE(), "train");
|
||||
auto json_matrix = scores.get_confusion_matrix_json(true);
|
||||
platform::Scores scores2(json_matrix);
|
||||
@@ -144,11 +192,22 @@ TEST_CASE("Classification Report", "[Scores]")
|
||||
TEST_CASE("JSON constructor", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test = { 0, 2, 2, 2, 2, 0, 1, 2, 0, 2 };
|
||||
std::vector<int> y_pred = { 0, 1, 2, 2, 1, 1, 1, 0, 0, 2 };
|
||||
std::vector<std::array<double, 3>> probs = {
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 } // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
};
|
||||
torch::Tensor y_pred = torch::from_blob(probs.data(), { (long)probs.size(), 3 }, torch::kFloat64).clone();
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
std::vector<std::string> labels = { "Car", "Boat", "Aeroplane" };
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 3, labels);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 3, labels);
|
||||
auto res_json_int = scores.get_confusion_matrix_json();
|
||||
platform::Scores scores2(res_json_int);
|
||||
REQUIRE(scores.accuracy() == scores2.accuracy());
|
||||
@@ -173,17 +232,14 @@ TEST_CASE("JSON constructor", "[Scores]")
|
||||
TEST_CASE("Aggregate", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test;
|
||||
std::vector<int> y_pred;
|
||||
torch::Tensor y_pred;
|
||||
make_test_bin(197, 210, 52, 41, y_test, y_pred);
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 2);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 2);
|
||||
y_test.clear();
|
||||
y_pred.clear();
|
||||
make_test_bin(227, 187, 39, 47, y_test, y_pred);
|
||||
auto y_test_tensor2 = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor2 = torch::tensor(y_pred, torch::kInt32);
|
||||
platform::Scores scores2(y_test_tensor2, y_pred_tensor2, 2);
|
||||
platform::Scores scores2(y_test_tensor2, y_pred, 2);
|
||||
scores.aggregate(scores2);
|
||||
REQUIRE(scores.accuracy() == Catch::Approx(0.821).epsilon(epsilon));
|
||||
REQUIRE(scores.f1_score(0) == Catch::Approx(0.8160329));
|
||||
@@ -195,11 +251,9 @@ TEST_CASE("Aggregate", "[Scores]")
|
||||
REQUIRE(scores.f1_weighted() == Catch::Approx(0.8209856));
|
||||
REQUIRE(scores.f1_macro() == Catch::Approx(0.8208694));
|
||||
y_test.clear();
|
||||
y_pred.clear();
|
||||
make_test_bin(197 + 227, 210 + 187, 52 + 39, 41 + 47, y_test, y_pred);
|
||||
y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
platform::Scores scores3(y_test_tensor, y_pred_tensor, 2);
|
||||
platform::Scores scores3(y_test_tensor, y_pred, 2);
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
REQUIRE(scores3.f1_score(i) == scores.f1_score(i));
|
||||
REQUIRE(scores3.precision(i) == scores.precision(i));
|
||||
@@ -212,11 +266,22 @@ TEST_CASE("Aggregate", "[Scores]")
|
||||
TEST_CASE("Order of keys", "[Scores]")
|
||||
{
|
||||
std::vector<int> y_test = { 0, 2, 2, 2, 2, 0, 1, 2, 0, 2 };
|
||||
std::vector<int> y_pred = { 0, 1, 2, 2, 1, 1, 1, 0, 0, 2 };
|
||||
std::vector<std::array<double, 3>> probs = {
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 0.0, 1.0 }, // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 0.0, 1.0, 0.0 }, // P(class 0)=0, P(class 1)=1, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 1.0, 0.0, 0.0 }, // P(class 0)=1, P(class 1)=0, P(class 2)=0
|
||||
{ 0.0, 0.0, 1.0 } // P(class 0)=0, P(class 1)=0, P(class 2)=1
|
||||
};
|
||||
torch::Tensor y_pred = torch::from_blob(probs.data(), { (long)probs.size(), 3 }, torch::kFloat64).clone();
|
||||
auto y_test_tensor = torch::tensor(y_test, torch::kInt32);
|
||||
auto y_pred_tensor = torch::tensor(y_pred, torch::kInt32);
|
||||
std::vector<std::string> labels = { "Car", "Boat", "Aeroplane" };
|
||||
platform::Scores scores(y_test_tensor, y_pred_tensor, 3, labels);
|
||||
platform::Scores scores(y_test_tensor, y_pred, 3, labels);
|
||||
auto res_json_int = scores.get_confusion_matrix_json(true);
|
||||
// Make a temp file and store the json
|
||||
std::string filename = "temp.json";
|
||||
|
@@ -5,7 +5,7 @@
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
#include <ArffFiles.hpp>
|
||||
#include <ArffFiles/ArffFiles.hpp>
|
||||
#include <fimdlp/CPPFImdlp.h>
|
||||
|
||||
bool file_exists(const std::string& name);
|
||||
|
21
vcpkg-configuration.json
Normal file
21
vcpkg-configuration.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"default-registry": {
|
||||
"kind": "git",
|
||||
"baseline": "760bfd0c8d7c89ec640aec4df89418b7c2745605",
|
||||
"repository": "https://github.com/microsoft/vcpkg"
|
||||
},
|
||||
"registries": [
|
||||
{
|
||||
"kind": "git",
|
||||
"repository": "https://github.com/rmontanana/vcpkg-stash",
|
||||
"baseline": "1ea69243c0e8b0de77c9d1dd6e1d7593ae7f3627",
|
||||
"packages": [
|
||||
"arff-files",
|
||||
"bayesnet",
|
||||
"fimdlp",
|
||||
"folding",
|
||||
"libtorch-bin"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
43
vcpkg.json
Normal file
43
vcpkg.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "platform",
|
||||
"version-string": "1.1.0",
|
||||
"dependencies": [
|
||||
"arff-files",
|
||||
"nlohmann-json",
|
||||
"fimdlp",
|
||||
"libtorch-bin",
|
||||
"folding",
|
||||
"catch2",
|
||||
"argparse"
|
||||
],
|
||||
"overrides": [
|
||||
{
|
||||
"name": "arff-files",
|
||||
"version": "1.1.0"
|
||||
},
|
||||
{
|
||||
"name": "fimdlp",
|
||||
"version": "2.0.1"
|
||||
},
|
||||
{
|
||||
"name": "libtorch-bin",
|
||||
"version": "2.7.0"
|
||||
},
|
||||
{
|
||||
"name": "folding",
|
||||
"version": "1.1.1"
|
||||
},
|
||||
{
|
||||
"name": "argparse",
|
||||
"version": "3.2"
|
||||
},
|
||||
{
|
||||
"name": "catch2",
|
||||
"version": "3.8.1"
|
||||
},
|
||||
{
|
||||
"name": "nlohmann-json",
|
||||
"version": "3.11.3"
|
||||
}
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user