Compare commits

70 Commits

Author SHA1 Message Date
321e2a2f28 Add folder to manage 2025-05-13 14:09:25 +02:00
36c72491e7 Add folder to b_best 2025-05-13 13:50:07 +02:00
aa19ab6c21 Option to use BayesNet local or vcpkg in CMakeLists 2025-05-09 19:16:17 +02:00
16b4923851 Complete configuration xlsxwriter is still with the old config 2025-05-09 11:10:27 +02:00
b1965c8ae5 Add vcpkg config files 2025-05-09 10:54:27 +02:00
7d3a2dd713 Remove modules 2025-05-08 17:15:42 +02:00
50fde9521b Update last commit badge in README 2025-04-22 11:16:27 +00:00
cd2f47c58b Merge pull request 'Including XA1DE model' (#5) from XA1DE into main
Reviewed-on: #5
2025-03-20 14:58:37 +00:00
facf6f6ddd Fix GridBase to eliminate uneeded GridData 2025-03-20 15:54:13 +01:00
c9ab88e475 Update models and remove normalize weights in XA1DE 2025-03-17 13:28:35 +01:00
c2a4e3e64e Add XSPnDE n=2 2025-03-13 11:00:21 +01:00
664a6a5aeb Add XBAODE & XSPODE from bayesnet 2025-03-09 19:20:51 +01:00
ae7b89b134 tolerance <- 3 2025-03-08 18:07:56 +01:00
9c1852c6c3 First working version 2025-03-08 14:20:27 +01:00
7a23782b05 Add XSpode submodel 2025-03-07 18:34:16 +01:00
b2002d341c Create Xaode2 and add initializer factor in predict 2025-03-03 12:38:05 +01:00
9a8b960ce8 Remove uneeded commented code 2025-03-03 11:29:57 +01:00
7bc8633ed1 Enhance result 2025-03-03 10:56:20 +01:00
11155463b9 Fix predict_proba_spode 2025-03-02 21:41:21 +01:00
12e69a7f53 Add Prior probability to predict
Fix predict_spode
2025-03-01 20:29:45 +01:00
c127cb670a Fix predict_proba_spode mistake 2025-02-27 20:45:28 +01:00
610c2a6a4a Continue refactoring 2025-02-27 11:37:30 +01:00
2dcd073299 Refactor Xaode 2025-02-27 10:08:27 +01:00
f51d5b5e40 Continue refactoring 2025-02-27 09:57:40 +01:00
4e3043b2d1 Fix XA1DE integration 2025-02-27 09:23:47 +01:00
b055065e59 Fix predict_proba declaration 2025-02-26 21:08:33 +01:00
0d1e4b3c6f Continue refactoring 2025-02-26 21:03:01 +01:00
1a688f90b4 Complete refactor of XA1DE & XBAODE with new ExpClf class 2025-02-26 16:55:04 +01:00
c63baf419f Add log and fix some mistakes in integration 2025-02-25 20:35:13 +01:00
de7cf091be Add open excel file on b_manage termination 2025-02-25 13:41:06 +01:00
475a819a87 Continue integration into trainModel 2025-02-25 11:03:53 +01:00
ce6e192a33 Include BoostAODE trainModel method in XBAODE fit method 2025-02-24 10:27:24 +01:00
5daf7cbd69 Create XBAODE classifier 2025-02-23 19:44:13 +01:00
1b26de1e38 Set use_threads true as default for XA1DE 2025-02-23 18:54:55 +01:00
d3de429f2c Add room for nodes, depth and edges on screen report 2025-02-19 16:05:21 +01:00
f48864a415 Fix back button in manage
Fix sort datasets in b_main when --datasets is used
2025-02-19 13:32:07 +01:00
c1531dba2a Complete XA1DE integration 2025-02-19 11:40:33 +01:00
5556fbab03 Complete integration with memory failure 2025-02-18 22:57:02 +01:00
ac89cefab3 Add conversion methods 2025-02-18 12:07:56 +01:00
14dd8ebb66 First compilation 2025-02-18 11:04:24 +01:00
bd5ba14f04 Begin model inclusion 2025-02-18 10:48:46 +01:00
17728212c1 Ignore case in datasets sorting 2025-02-17 20:01:06 +01:00
86b4558f9d Add 1 char to b_list datasets headers 2025-02-17 19:44:23 +01:00
505edc79ac Fix sample issue 2025-02-04 18:53:23 +01:00
73a4b3d5e5 Add changeModel to b_manage 2025-02-04 17:34:00 +01:00
cbe8f4c79c Fix status length output in b_main 2025-02-01 21:42:56 +01:00
0d08a526fa Add score to b_main output 2025-01-30 17:36:45 +01:00
d0706da887 Fix sort order in bgrid report 2025-01-21 20:38:07 +01:00
07e3cc9599 Fix errors in grid Experiment 2025-01-19 13:51:51 +01:00
2a9652b450 Fix b_main order of datasets if --datasets parameter used 2025-01-18 20:31:58 +01:00
3397d0962f Refactor arguments management for Experimentation 2025-01-18 18:26:34 +01:00
7aaf6d1bf8 Add conditional saveResults to GridExperiment 2025-01-18 13:09:45 +01:00
eb430a84c4 Fix dataset name order in grid experiment 2025-01-17 16:58:39 +01:00
d0e65348e0 Complete b_grid experiment 2025-01-17 13:56:19 +01:00
c1d5dd74e3 Continue with grid experiment 2025-01-17 10:39:56 +01:00
9a9a9fb17a Continue grid Experiment 2025-01-14 22:04:23 +01:00
386faf960e Refactor grid classes and add summary of tasks at the end 2025-01-14 18:53:11 +01:00
28894004c8 Fix time output in b_main 2025-01-08 20:45:08 +01:00
ae41975fb4 Add nominal or index dataset name in tex output 2025-01-08 17:18:32 +01:00
0e475e4488 Sort datasets on input 2025-01-08 11:05:22 +01:00
909cec712c Complete schema validation 2025-01-07 18:24:55 +01:00
4901bb1f32 Add json results format validation 2025-01-07 11:58:18 +01:00
0318dcf8e5 Continue with grid_experiment refactor 2024-12-21 14:18:47 +01:00
1cc19a7b19 Refactor mpi classes 2024-12-20 19:10:17 +01:00
f88944de36 Add grid base class and static class 2024-12-20 18:54:08 +01:00
1a336a094e Refactor gridsearch and begin gridexperiment 2024-12-20 17:36:43 +01:00
8705adf3ee Begin b_grid experiment 2024-12-20 12:51:33 +01:00
017cb8a0dc Fix smoothing problem in gridsearch 2024-12-18 11:17:04 +01:00
e966c880e6 Refactor gridsearch output 2024-12-17 10:49:58 +01:00
70ea32dc9a Update folding library 2024-12-14 20:23:31 +01:00
67 changed files with 3862 additions and 935 deletions

2
.gitignore vendored
View File

@@ -41,3 +41,5 @@ puml/**
*.dot *.dot
diagrams/html/** diagrams/html/**
diagrams/latex/** diagrams/latex/**
.cache
vcpkg_installed

21
.gitmodules vendored
View File

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

View File

@@ -7,15 +7,11 @@ project(Platform
LANGUAGES CXX LANGUAGES CXX
) )
find_package(Torch REQUIRED)
if (POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif ()
# Global CMake variables # Global CMake variables
# ---------------------- # ----------------------
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -26,62 +22,77 @@ set(CMAKE_CXX_FLAGS_DEBUG " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -O
# Options # Options
# ------- # -------
option(ENABLE_CLANG_TIDY "Enable to add clang tidy." OFF)
option(ENABLE_TESTING "Unit testing build" OFF) option(ENABLE_TESTING "Unit testing build" OFF)
option(CODE_COVERAGE "Collect coverage from test library" 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 # MPI
find_package(MPI REQUIRED) find_package(MPI REQUIRED)
message("MPI_CXX_LIBRARIES=${MPI_CXX_LIBRARIES}") message("MPI_CXX_LIBRARIES=${MPI_CXX_LIBRARIES}")
message("MPI_CXX_INCLUDE_DIRS=${MPI_CXX_INCLUDE_DIRS}") message("MPI_CXX_INCLUDE_DIRS=${MPI_CXX_INCLUDE_DIRS}")
# Boost Library # Boost Library
cmake_policy(SET CMP0135 NEW)
cmake_policy(SET CMP0167 NEW) # For FindBoost
set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON) set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.66.0 REQUIRED COMPONENTS python3 numpy3) 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) if(Boost_FOUND)
message("Boost_INCLUDE_DIRS=${Boost_INCLUDE_DIRS}") message("Boost_INCLUDE_DIRS=${Boost_INCLUDE_DIRS}")
message("Boost_LIBRARIES=${Boost_LIBRARIES}")
message("Boost_VERSION=${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS}) include_directories(${Boost_INCLUDE_DIRS})
endif() 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 # 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) 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_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_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=${PyClassifiers}")
message(STATUS "PyClassifiers_INCLUDE_DIRS=${PyClassifiers_INCLUDE_DIRS}") message(STATUS "PyClassifiers_INCLUDE_DIRS=${PyClassifiers_INCLUDE_DIRS}")
message(STATUS "BayesNet=${BayesNet}")
message(STATUS "Bayesnet_INCLUDE_DIRS=${Bayesnet_INCLUDE_DIRS}")
# Subdirectories # Subdirectories
# -------------- # --------------
@@ -96,10 +107,14 @@ file(GLOB Platform_SOURCES CONFIGURE_DEPENDS ${Platform_SOURCE_DIR}/src/*.cpp)
# Testing # Testing
# ------- # -------
if (ENABLE_TESTING) if (ENABLE_TESTING)
enable_testing()
MESSAGE("Testing enabled") MESSAGE("Testing enabled")
if (NOT TARGET Catch2::Catch2) find_package(Catch2 CONFIG REQUIRED)
add_git_submodule("lib/catch2")
endif (NOT TARGET Catch2::Catch2)
include(CTest) include(CTest)
add_subdirectory(tests) add_subdirectory(tests)
endif (ENABLE_TESTING) 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)

View File

@@ -1,10 +1,10 @@
SHELL := /bin/bash SHELL := /bin/bash
.DEFAULT_GOAL := help .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_release = build_Release
f_debug = build_debug f_debug = build_Debug
app_targets = b_best b_list b_main b_manage b_grid app_targets = b_best b_list b_main b_manage b_grid b_results
test_targets = unit_tests_platform test_targets = unit_tests_platform
define ClearTests define ClearTests
@@ -20,14 +20,22 @@ define ClearTests
fi ; fi ;
endef endef
init: ## Initialize the project installing dependencies
@echo ">>> Installing dependencies"
@vcpkg install
@echo ">>> Done";
sub-init: ## Initialize submodules clean: ## Clean the project
@git submodule update --init --recursive @echo ">>> Cleaning the project..."
@if test -f CMakeCache.txt ; then echo "- Deleting CMakeCache.txt"; rm -f CMakeCache.txt; fi
sub-update: ## Initialize submodules @for folder in $(f_release) $(f_debug) vpcpkg_installed install_test ; do \
@git submodule update --remote --merge if test -d "$$folder" ; then \
@git submodule foreach git pull origin master echo "- Deleting $$folder folder" ; \
rm -rf "$$folder"; \
fi; \
done
$(call ClearTests)
@echo ">>> Done";
setup: ## Install dependencies for tests and coverage setup: ## Install dependencies for tests and coverage
@if [ "$(shell uname)" = "Darwin" ]; then \ @if [ "$(shell uname)" = "Darwin" ]; then \
brew install gcovr; \ brew install gcovr; \
@@ -37,6 +45,11 @@ setup: ## Install dependencies for tests and coverage
pip install gcovr; \ pip install gcovr; \
fi fi
dest ?= ${HOME}/bin
main: ## Build only the b_main target
@cmake --build $(f_release) -t b_main --parallel
@cp $(f_release)/src/b_main $(dest)
dest ?= ${HOME}/bin dest ?= ${HOME}/bin
install: ## Copy binary files to bin folder install: ## Copy binary files to bin folder
@echo "Destination folder: $(dest)" @echo "Destination folder: $(dest)"
@@ -55,32 +68,27 @@ 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 cd $(f_debug) && cmake .. --graphviz=dependency.dot && dot -Tpng dependency.dot -o dependency.png
buildd: ## Build the debug targets 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 buildr: ## Build the release targets
cmake --build $(f_release) -t $(app_targets) --parallel @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: ## Create uml class and sequence diagrams
clang-uml -p --add-compile-flag -I /usr/lib/gcc/x86_64-redhat-linux/8/include/ 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..."; @echo ">>> Building Debug Platform...";
@if [ -d ./$(f_debug) ]; then rm -rf ./$(f_debug); fi @if [ -d ./$(f_debug) ]; then rm -rf ./$(f_debug); fi
@mkdir $(f_debug); @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"; @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..."; @echo ">>> Building Release Platform...";
@if [ -d ./$(f_release) ]; then rm -rf ./$(f_release); fi @if [ -d ./$(f_release) ]; then rm -rf ./$(f_release); fi
@mkdir $(f_release); @mkdir $(f_release);
@cmake -S . -B $(f_release) -D CMAKE_BUILD_TYPE=Release @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"; @echo ">>> Done";
opt = "" opt = ""
test: ## Run tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximum Spanning Tree'") to run only that section test: ## Run tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximum Spanning Tree'") to run only that section
@@ -98,8 +106,8 @@ test: ## Run tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximu
fname = iris fname = iris
example: ## Build sample example: ## Build sample
@echo ">>> Building Sample..."; @echo ">>> Building Sample...";
@cmake --build build_debug -t sample @cmake --build $(f_release) -t sample
build_debug/sample/PlatformSample --model BoostAODE --dataset $(fname) --discretize --stratified $(f_release)/sample/PlatformSample --model BoostAODE --dataset $(fname) --discretize --stratified
@echo ">>> Done"; @echo ">>> Done";

View File

@@ -2,7 +2,8 @@
![C++](https://img.shields.io/badge/c++-%2300599C.svg?style=flat&logo=c%2B%2B&logoColor=white) ![C++](https://img.shields.io/badge/c++-%2300599C.svg?style=flat&logo=c%2B%2B&logoColor=white)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](<https://opensource.org/licenses/MIT>) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](<https://opensource.org/licenses/MIT>)
![Gitea Last Commit](https://img.shields.io/gitea/last-commit/rmontanana/platform?gitea_url=https://gitea.rmontanana.es:3000&logo=gitea) [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/rmontanana/Platform)
![Gitea Last Commit](https://img.shields.io/gitea/last-commit/rmontanana/platform?gitea_url=https://gitea.rmontanana.es&logo=gitea)
Platform to run Bayesian Networks and Machine Learning Classifiers experiments. Platform to run Bayesian Networks and Machine Learning Classifiers experiments.
@@ -40,7 +41,7 @@ export MPI_HOME="/usr/lib64/openmpi"
In Mac OS X, install mpich with brew and if cmake doesn't find it, edit mpicxx wrapper to remove the ",-commons,use_dylibs" from final_ldflags In Mac OS X, install mpich with brew and if cmake doesn't find it, edit mpicxx wrapper to remove the ",-commons,use_dylibs" from final_ldflags
```bash ```bash
vi /opt/homebrew/bin/mpicx vi /opt/homebrew/bin/mpicxx
``` ```
### boost library ### boost library

View File

@@ -137,7 +137,7 @@
include(CMakeParseArguments) include(CMakeParseArguments)
option(CODE_COVERAGE_VERBOSE "Verbose information" FALSE) option(CODE_COVERAGE_VERBOSE "Verbose information" TRUE)
# Check prereqs # Check prereqs
find_program( GCOV_PATH gcov ) find_program( GCOV_PATH gcov )
@@ -160,7 +160,11 @@ foreach(LANG ${LANGUAGES})
endif() endif()
elseif(NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "GNU" elseif(NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "GNU"
AND NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "(LLVM)?[Ff]lang") AND NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "(LLVM)?[Ff]lang")
message(FATAL_ERROR "Compiler is not GNU or Flang! Aborting...") if ("${LANG}" MATCHES "CUDA")
message(STATUS "Ignoring CUDA")
else()
message(FATAL_ERROR "Compiler is not GNU or Flang! Aborting...")
endif()
endif() endif()
endforeach() endforeach()

View File

@@ -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 a4329f5f9d

Submodule lib/argparse deleted from cbd9fd8ed6

Submodule lib/catch2 deleted from 0321d2fce3

Submodule lib/folding deleted from 2ac43e32ac

Submodule lib/json deleted from 620034ecec

Submodule lib/mdlp deleted from cfb993f5ec

14
remove_submodules.sh Normal file
View 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

View File

@@ -1,15 +1,11 @@
include_directories( include_directories(
${TORCH_INCLUDE_DIRS}
${Platform_SOURCE_DIR}/src/common ${Platform_SOURCE_DIR}/src/common
${Platform_SOURCE_DIR}/src/main ${Platform_SOURCE_DIR}/src/main
${Python3_INCLUDE_DIRS} ${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 ${CMAKE_BINARY_DIR}/configured_files/include
${PyClassifiers_INCLUDE_DIRS} ${PyClassifiers_INCLUDE_DIRS}
${Bayesnet_INCLUDE_DIRS} ${bayesnet_INCLUDE_DIRS}
) )
add_executable(PlatformSample sample.cpp ${Platform_SOURCE_DIR}/src/main/Models.cpp) 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})

View File

@@ -9,6 +9,7 @@
#include <fimdlp/CPPFImdlp.h> #include <fimdlp/CPPFImdlp.h>
#include <folding.hpp> #include <folding.hpp>
#include <bayesnet/utils/BayesMetrics.h> #include <bayesnet/utils/BayesMetrics.h>
#include <bayesnet/classifiers/SPODE.h>
#include "Models.h" #include "Models.h"
#include "modelRegister.h" #include "modelRegister.h"
#include "config_platform.h" #include "config_platform.h"
@@ -160,82 +161,119 @@ int main(int argc, char** argv)
states[feature] = std::vector<int>(maxes[feature]); states[feature] = std::vector<int>(maxes[feature]);
} }
states[className] = std::vector<int>(maxes[className]); states[className] = std::vector<int>(maxes[className]);
auto clf = platform::Models::instance()->create(model_name); // Output the states
std::cout << std::string(80, '-') << std::endl;
std::cout << "States" << std::endl;
for (auto feature : features) {
std::cout << feature << ": " << states[feature].size() << std::endl;
}
std::cout << std::string(80, '-') << std::endl;
//auto clf = platform::Models::instance()->create("SPODE");
auto clf = bayesnet::SPODE(2);
bayesnet::Smoothing_t smoothing = bayesnet::Smoothing_t::ORIGINAL; bayesnet::Smoothing_t smoothing = bayesnet::Smoothing_t::ORIGINAL;
clf->fit(Xd, y, features, className, states, smoothing); clf.fit(Xd, y, features, className, states, smoothing);
if (dump_cpt) { if (dump_cpt) {
std::cout << "--- CPT Tables ---" << std::endl; std::cout << "--- CPT Tables ---" << std::endl;
clf->dump_cpt(); std::cout << clf.dump_cpt();
} }
auto lines = clf->show(); std::cout << "--- Datos predicción ---" << std::endl;
std::cout << "Orden de variables: " << std::endl;
for (auto feature : features) {
std::cout << feature << ", ";
}
std::cout << std::endl;
std::cout << "X[0]: ";
for (int i = 0; i < Xd.size(); ++i) {
std::cout << Xd[i][0] << ", ";
}
std::cout << std::endl;
std::cout << std::string(80, '-') << std::endl;
auto lines = clf.show();
for (auto line : lines) { for (auto line : lines) {
std::cout << line << std::endl; std::cout << line << std::endl;
} }
std::cout << "--- Topological Order ---" << std::endl; std::cout << "--- Topological Order ---" << std::endl;
auto order = clf->topological_order(); auto order = clf.topological_order();
for (auto name : order) { for (auto name : order) {
std::cout << name << ", "; std::cout << name << ", ";
} }
std::cout << "end." << std::endl; auto predict_proba = clf.predict_proba(Xd);
auto score = clf->score(Xd, y); std::cout << "Instances predict_proba: ";
std::cout << "Score: " << score << std::endl; for (int i = 0; i < predict_proba.size(); i++) {
auto graph = clf->graph(); std::cout << "Instance " << i << ": ";
auto dot_file = model_name + "_" + file_name; for (int j = 0; j < 4; ++j) {
ofstream file(dot_file + ".dot"); std::cout << Xd[j][i] << ", ";
file << graph;
file.close();
std::cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << std::endl;
std::cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << std::endl;
std::string stratified_string = stratified ? " Stratified" : "";
std::cout << nFolds << " Folds" << stratified_string << " Cross validation" << std::endl;
std::cout << "==========================================" << std::endl;
torch::Tensor Xt = torch::zeros({ static_cast<int>(Xd.size()), static_cast<int>(Xd[0].size()) }, torch::kInt32);
torch::Tensor yt = torch::tensor(y, torch::kInt32);
for (int i = 0; i < features.size(); ++i) {
Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32));
}
float total_score = 0, total_score_train = 0, score_train, score_test;
folding::Fold* fold;
double nodes = 0.0;
if (stratified)
fold = new folding::StratifiedKFold(nFolds, y, seed);
else
fold = new folding::KFold(nFolds, y.size(), seed);
for (auto i = 0; i < nFolds; ++i) {
auto [train, test] = fold->getFold(i);
std::cout << "Fold: " << i + 1 << std::endl;
if (tensors) {
auto ttrain = torch::tensor(train, torch::kInt64);
auto ttest = torch::tensor(test, torch::kInt64);
torch::Tensor Xtraint = torch::index_select(Xt, 1, ttrain);
torch::Tensor ytraint = yt.index({ ttrain });
torch::Tensor Xtestt = torch::index_select(Xt, 1, ttest);
torch::Tensor ytestt = yt.index({ ttest });
clf->fit(Xtraint, ytraint, features, className, states, smoothing);
auto temp = clf->predict(Xtraint);
score_train = clf->score(Xtraint, ytraint);
score_test = clf->score(Xtestt, ytestt);
} else {
auto [Xtrain, ytrain] = extract_indices(train, Xd, y);
auto [Xtest, ytest] = extract_indices(test, Xd, y);
clf->fit(Xtrain, ytrain, features, className, states, smoothing);
std::cout << "Nodes: " << clf->getNumberOfNodes() << std::endl;
nodes += clf->getNumberOfNodes();
score_train = clf->score(Xtrain, ytrain);
score_test = clf->score(Xtest, ytest);
} }
if (dump_cpt) { std::cout << ": ";
std::cout << "--- CPT Tables ---" << std::endl; for (auto score : predict_proba[i]) {
clf->dump_cpt(); std::cout << score << ", ";
} }
total_score_train += score_train; std::cout << std::endl;
total_score += score_test;
std::cout << "Score Train: " << score_train << std::endl;
std::cout << "Score Test : " << score_test << std::endl;
std::cout << "-------------------------------------------------------------------------------" << std::endl;
} }
std::cout << "Nodes: " << nodes / nFolds << std::endl; // std::cout << std::endl;
std::cout << "**********************************************************************************" << std::endl; // std::cout << "end." << std::endl;
std::cout << "Average Score Train: " << total_score_train / nFolds << std::endl; // auto score = clf->score(Xd, y);
std::cout << "Average Score Test : " << total_score / nFolds << std::endl;return 0; // std::cout << "Score: " << score << std::endl;
// auto graph = clf->graph();
// auto dot_file = model_name + "_" + file_name;
// ofstream file(dot_file + ".dot");
// file << graph;
// file.close();
// std::cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << std::endl;
// std::cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << std::endl;
// std::string stratified_string = stratified ? " Stratified" : "";
// std::cout << nFolds << " Folds" << stratified_string << " Cross validation" << std::endl;
// std::cout << "==========================================" << std::endl;
// torch::Tensor Xt = torch::zeros({ static_cast<int>(Xd.size()), static_cast<int>(Xd[0].size()) }, torch::kInt32);
// torch::Tensor yt = torch::tensor(y, torch::kInt32);
// for (int i = 0; i < features.size(); ++i) {
// Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32));
// }
// float total_score = 0, total_score_train = 0, score_train, score_test;
// folding::Fold* fold;
// double nodes = 0.0;
// if (stratified)
// fold = new folding::StratifiedKFold(nFolds, y, seed);
// else
// fold = new folding::KFold(nFolds, y.size(), seed);
// for (auto i = 0; i < nFolds; ++i) {
// auto [train, test] = fold->getFold(i);
// std::cout << "Fold: " << i + 1 << std::endl;
// if (tensors) {
// auto ttrain = torch::tensor(train, torch::kInt64);
// auto ttest = torch::tensor(test, torch::kInt64);
// torch::Tensor Xtraint = torch::index_select(Xt, 1, ttrain);
// torch::Tensor ytraint = yt.index({ ttrain });
// torch::Tensor Xtestt = torch::index_select(Xt, 1, ttest);
// torch::Tensor ytestt = yt.index({ ttest });
// clf->fit(Xtraint, ytraint, features, className, states, smoothing);
// auto temp = clf->predict(Xtraint);
// score_train = clf->score(Xtraint, ytraint);
// score_test = clf->score(Xtestt, ytestt);
// } else {
// auto [Xtrain, ytrain] = extract_indices(train, Xd, y);
// auto [Xtest, ytest] = extract_indices(test, Xd, y);
// clf->fit(Xtrain, ytrain, features, className, states, smoothing);
// std::cout << "Nodes: " << clf->getNumberOfNodes() << std::endl;
// nodes += clf->getNumberOfNodes();
// score_train = clf->score(Xtrain, ytrain);
// score_test = clf->score(Xtest, ytest);
// }
// // if (dump_cpt) {
// // std::cout << "--- CPT Tables ---" << std::endl;
// // std::cout << clf->dump_cpt();
// // }
// total_score_train += score_train;
// total_score += score_test;
// std::cout << "Score Train: " << score_train << std::endl;
// std::cout << "Score Test : " << score_test << std::endl;
// std::cout << "-------------------------------------------------------------------------------" << std::endl;
// }
// std::cout << "Nodes: " << nodes / nFolds << std::endl;
// std::cout << "**********************************************************************************" << std::endl;
// std::cout << "Average Score Train: " << total_score_train / nFolds << std::endl;
// std::cout << "Average Score Test : " << total_score / nFolds << std::endl;return 0;
} }

View File

@@ -1,17 +1,10 @@
include_directories( include_directories(
## Libs ## Libs
${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} ${Python3_INCLUDE_DIRS}
${MPI_CXX_INCLUDE_DIRS} ${MPI_CXX_INCLUDE_DIRS}
${TORCH_INCLUDE_DIRS} ${TORCH_INCLUDE_DIRS}
${CMAKE_BINARY_DIR}/configured_files/include ${CMAKE_BINARY_DIR}/configured_files/include
${PyClassifiers_INCLUDE_DIRS} ${PyClassifiers_INCLUDE_DIRS}
${Bayesnet_INCLUDE_DIRS}
## Platform ## Platform
${Platform_SOURCE_DIR}/src ${Platform_SOURCE_DIR}/src
${Platform_SOURCE_DIR}/results ${Platform_SOURCE_DIR}/results
@@ -25,17 +18,23 @@ add_executable(
main/Models.cpp main/Scores.cpp main/Models.cpp main/Scores.cpp
reports/ReportExcel.cpp reports/ReportBase.cpp reports/ExcelFile.cpp reports/ReportExcel.cpp reports/ReportBase.cpp reports/ExcelFile.cpp
results/Result.cpp results/Result.cpp
experimental_clfs/XA1DE.cpp
experimental_clfs/ExpClf.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 # b_grid
set(grid_sources GridSearch.cpp GridData.cpp) set(grid_sources GridSearch.cpp GridData.cpp GridExperiment.cpp GridBase.cpp )
list(TRANSFORM grid_sources PREPEND grid/) list(TRANSFORM grid_sources PREPEND grid/)
add_executable(b_grid commands/b_grid.cpp ${grid_sources} add_executable(b_grid commands/b_grid.cpp ${grid_sources}
common/Datasets.cpp common/Dataset.cpp common/Discretization.cpp common/Datasets.cpp common/Dataset.cpp common/Discretization.cpp
main/HyperParameters.cpp main/Models.cpp main/HyperParameters.cpp main/Models.cpp main/Experiment.cpp main/Scores.cpp main/ArgumentsExperiment.cpp
reports/ReportConsole.cpp reports/ReportBase.cpp
results/Result.cpp
experimental_clfs/XA1DE.cpp
experimental_clfs/ExpClf.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 # b_list
add_executable(b_list commands/b_list.cpp add_executable(b_list commands/b_list.cpp
@@ -43,18 +42,22 @@ add_executable(b_list commands/b_list.cpp
main/Models.cpp main/Scores.cpp main/Models.cpp main/Scores.cpp
reports/ReportExcel.cpp reports/ExcelFile.cpp reports/ReportBase.cpp reports/DatasetsExcel.cpp reports/DatasetsConsole.cpp reports/ReportsPaged.cpp reports/ReportExcel.cpp reports/ExcelFile.cpp reports/ReportBase.cpp reports/DatasetsExcel.cpp reports/DatasetsConsole.cpp reports/ReportsPaged.cpp
results/Result.cpp results/ResultsDatasetExcel.cpp results/ResultsDataset.cpp results/ResultsDatasetConsole.cpp results/Result.cpp results/ResultsDatasetExcel.cpp results/ResultsDataset.cpp results/ResultsDatasetConsole.cpp
experimental_clfs/XA1DE.cpp
experimental_clfs/ExpClf.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 # b_main
set(main_sources Experiment.cpp Models.cpp HyperParameters.cpp Scores.cpp) set(main_sources Experiment.cpp Models.cpp HyperParameters.cpp Scores.cpp ArgumentsExperiment.cpp)
list(TRANSFORM main_sources PREPEND main/) list(TRANSFORM main_sources PREPEND main/)
add_executable(b_main commands/b_main.cpp ${main_sources} add_executable(b_main commands/b_main.cpp ${main_sources}
common/Datasets.cpp common/Dataset.cpp common/Discretization.cpp common/Datasets.cpp common/Dataset.cpp common/Discretization.cpp
reports/ReportConsole.cpp reports/ReportBase.cpp reports/ReportConsole.cpp reports/ReportBase.cpp
results/Result.cpp results/Result.cpp
experimental_clfs/XA1DE.cpp
experimental_clfs/ExpClf.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 # b_manage
set(manage_sources ManageScreen.cpp OptionsMenu.cpp ResultsManager.cpp) set(manage_sources ManageScreen.cpp OptionsMenu.cpp ResultsManager.cpp)
@@ -66,4 +69,7 @@ add_executable(
results/Result.cpp results/ResultsDataset.cpp results/ResultsDatasetConsole.cpp results/Result.cpp results/ResultsDataset.cpp results/ResultsDatasetConsole.cpp
main/Scores.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)

View File

@@ -132,6 +132,7 @@ namespace platform {
for (const auto& dataset_ : table.items()) { for (const auto& dataset_ : table.items()) {
datasets.push_back(dataset_.key()); datasets.push_back(dataset_.key());
} }
std::stable_sort(datasets.begin(), datasets.end());
maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size(); 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); maxDatasetName = std::max(7, maxDatasetName);
return datasets; return datasets;
@@ -214,7 +215,7 @@ namespace platform {
return table; return table;
} }
void BestResults::printTableResults(std::vector<std::string> models, json table, bool tex) void BestResults::printTableResults(std::vector<std::string> models, json table, bool tex, bool index)
{ {
std::stringstream oss; std::stringstream oss;
oss << Colors::GREEN() << "Best results for " << score << " as of " << table.at("dateTable").get<std::string>() << std::endl; oss << Colors::GREEN() << "Best results for " << score << " as of " << table.at("dateTable").get<std::string>() << std::endl;
@@ -224,7 +225,7 @@ namespace platform {
auto bestResultsTex = BestResultsTex(); auto bestResultsTex = BestResultsTex();
auto bestResultsMd = BestResultsMd(); auto bestResultsMd = BestResultsMd();
if (tex) { if (tex) {
bestResultsTex.results_header(models, table.at("dateTable").get<std::string>()); bestResultsTex.results_header(models, table.at("dateTable").get<std::string>(), index);
bestResultsMd.results_header(models, table.at("dateTable").get<std::string>()); bestResultsMd.results_header(models, table.at("dateTable").get<std::string>());
} }
for (const auto& model : models) { for (const auto& model : models) {
@@ -241,7 +242,7 @@ namespace platform {
int nDatasets = table.begin().value().size(); int nDatasets = table.begin().value().size();
auto datasets = getDatasets(table.begin().value()); auto datasets = getDatasets(table.begin().value());
if (tex) { if (tex) {
bestResultsTex.results_body(datasets, table); bestResultsTex.results_body(datasets, table, index);
bestResultsMd.results_body(datasets, table); bestResultsMd.results_body(datasets, table);
} }
for (auto const& dataset_ : datasets) { for (auto const& dataset_ : datasets) {
@@ -325,14 +326,14 @@ namespace platform {
messageOutputFile("Excel", excel_report.getFileName()); messageOutputFile("Excel", excel_report.getFileName());
} }
} }
void BestResults::reportAll(bool excel, bool tex) void BestResults::reportAll(bool excel, bool tex, bool index)
{ {
auto models = getModels(); auto models = getModels();
// Build the table of results // Build the table of results
json table = buildTableResults(models); json table = buildTableResults(models);
std::vector<std::string> datasets = getDatasets(table.begin().value()); std::vector<std::string> datasets = getDatasets(table.begin().value());
// Print the table of results // Print the table of results
printTableResults(models, table, tex); printTableResults(models, table, tex, index);
// Compute the Friedman test // Compute the Friedman test
std::map<std::string, std::map<std::string, float>> ranksModels; std::map<std::string, std::map<std::string, float>> ranksModels;
if (friedman) { if (friedman) {

View File

@@ -13,7 +13,7 @@ namespace platform {
} }
std::string build(); std::string build();
void reportSingle(bool excel); void reportSingle(bool excel);
void reportAll(bool excel, bool tex); void reportAll(bool excel, bool tex, bool index);
void buildAll(); void buildAll();
private: private:
std::vector<std::string> getModels(); std::vector<std::string> getModels();
@@ -21,7 +21,7 @@ namespace platform {
std::vector<std::string> loadResultFiles(); std::vector<std::string> loadResultFiles();
void messageOutputFile(const std::string& title, const std::string& fileName); void messageOutputFile(const std::string& title, const std::string& fileName);
json buildTableResults(std::vector<std::string> models); json buildTableResults(std::vector<std::string> models);
void printTableResults(std::vector<std::string> models, json table, bool tex); void printTableResults(std::vector<std::string> models, json table, bool tex, bool index);
json loadFile(const std::string& fileName); json loadFile(const std::string& fileName);
void listFile(); void listFile();
std::string path; std::string path;

View File

@@ -12,7 +12,7 @@ namespace platform {
exit(1); exit(1);
} }
} }
void BestResultsTex::results_header(const std::vector<std::string>& models, const std::string& date) void BestResultsTex::results_header(const std::vector<std::string>& models, const std::string& date, bool index)
{ {
this->models = models; this->models = models;
auto file_name = Paths::tex() + Paths::tex_output(); auto file_name = Paths::tex() + Paths::tex_output();
@@ -29,7 +29,8 @@ namespace platform {
handler << "\\renewcommand{\\tabcolsep }{0.07cm} " << 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 << "\\caption{Accuracy results(mean $\\pm$ std) for all the algorithms and datasets} " << std::endl;
handler << "\\label{tab:results_accuracy}" << std::endl; handler << "\\label{tab:results_accuracy}" << std::endl;
handler << "\\begin{tabular} {{r" << std::string(models.size(), 'c').c_str() << "}}" << 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; handler << "\\hline " << std::endl;
handler << "" << std::endl; handler << "" << std::endl;
for (const auto& model : models) { for (const auto& model : models) {
@@ -38,13 +39,12 @@ namespace platform {
handler << "\\\\" << std::endl; handler << "\\\\" << std::endl;
handler << "\\hline" << std::endl; handler << "\\hline" << std::endl;
} }
void BestResultsTex::results_body(const std::vector<std::string>& datasets, json& table) void BestResultsTex::results_body(const std::vector<std::string>& datasets, json& table, bool index)
{ {
int i = 0; int i = 0;
for (auto const& dataset : datasets) { for (auto const& dataset : datasets) {
// Find out max value for this dataset // Find out max value for this dataset
double max_value = 0; double max_value = 0;
// Find out the max value for this dataset
for (const auto& model : models) { for (const auto& model : models) {
double value; double value;
try { try {
@@ -57,7 +57,10 @@ namespace platform {
max_value = value; max_value = value;
} }
} }
handler << ++i << " "; if (index)
handler << ++i << " ";
else
handler << dataset << " ";
for (const auto& model : models) { for (const auto& model : models) {
double value = table[model].at(dataset).at(0).get<double>(); double value = table[model].at(dataset).at(0).get<double>();
double std_value = table[model].at(dataset).at(3).get<double>(); double std_value = table[model].at(dataset).at(3).get<double>();

View File

@@ -9,13 +9,14 @@ namespace platform {
using json = nlohmann::ordered_json; using json = nlohmann::ordered_json;
class BestResultsTex { class BestResultsTex {
public: public:
BestResultsTex() = default; BestResultsTex(bool dataset_name = true) : dataset_name(dataset_name) {};
~BestResultsTex() = default; ~BestResultsTex() = default;
void results_header(const std::vector<std::string>& models, const std::string& date); 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); 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 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 holm_test(struct HolmResult& holmResult, const std::string& date);
private: private:
bool dataset_name;
void openTexFile(const std::string& name); void openTexFile(const std::string& name);
std::ofstream handler; std::ofstream handler;
std::vector<std::string> models; std::vector<std::string> models;

View File

@@ -9,14 +9,14 @@
void manageArguments(argparse::ArgumentParser& program) void manageArguments(argparse::ArgumentParser& program)
{ {
program.add_argument("-m", "--model") program.add_argument("-m", "--model").help("Model to use or any").default_value("any");
.help("Model to use or any") program.add_argument("--folder").help("Results folder to use").default_value(platform::Paths::results());
.default_value("any");
program.add_argument("-d", "--dataset").default_value("any").help("Filter results of the selected model) (any for all datasets)"); 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("accuracy").help("Filter results of the score name supplied");
program.add_argument("--friedman").help("Friedman test").default_value(false).implicit_value(true); 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("--excel").help("Output to excel").default_value(false).implicit_value(true);
program.add_argument("--tex").help("Output result table to TeX file").default_value(false).implicit_value(true); program.add_argument("--tex").help("Output results to TeX & Markdown files").default_value(false).implicit_value(true);
program.add_argument("--index").help("In tex output show the index of the dataset instead of the name to save space").default_value(false).implicit_value(true);
program.add_argument("--level").help("significance level").default_value(0.05).scan<'g', double>().action([](const std::string& value) { program.add_argument("--level").help("significance level").default_value(0.05).scan<'g', double>().action([](const std::string& value) {
try { try {
auto k = std::stod(value); auto k = std::stod(value);
@@ -37,17 +37,19 @@ int main(int argc, char** argv)
{ {
argparse::ArgumentParser program("b_best", { platform_project_version.begin(), platform_project_version.end() }); argparse::ArgumentParser program("b_best", { platform_project_version.begin(), platform_project_version.end() });
manageArguments(program); manageArguments(program);
std::string model, dataset, score; std::string model, dataset, score, folder;
bool build, report, friedman, excel, tex; bool build, report, friedman, excel, tex, index;
double level; double level;
try { try {
program.parse_args(argc, argv); program.parse_args(argc, argv);
model = program.get<std::string>("model"); model = program.get<std::string>("model");
folder = program.get<std::string>("folder");
dataset = program.get<std::string>("dataset"); dataset = program.get<std::string>("dataset");
score = program.get<std::string>("score"); score = program.get<std::string>("score");
friedman = program.get<bool>("friedman"); friedman = program.get<bool>("friedman");
excel = program.get<bool>("excel"); excel = program.get<bool>("excel");
tex = program.get<bool>("tex"); tex = program.get<bool>("tex");
index = program.get<bool>("index");
level = program.get<double>("level"); level = program.get<double>("level");
if (model == "" || score == "") { if (model == "" || score == "") {
throw std::runtime_error("Model and score name must be supplied"); throw std::runtime_error("Model and score name must be supplied");
@@ -64,10 +66,10 @@ int main(int argc, char** argv)
exit(1); exit(1);
} }
// Generate report // 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") { if (model == "any") {
results.buildAll(); results.buildAll();
results.reportAll(excel, tex); results.reportAll(excel, tex, index);
} else { } else {
std::string fileName = results.build(); std::string fileName = results.build();
std::cout << Colors::GREEN() << fileName << " created!" << Colors::RESET() << std::endl; std::cout << Colors::GREEN() << fileName << " created!" << Colors::RESET() << std::endl;

View File

@@ -1,16 +1,16 @@
#include <iostream> #include <iostream>
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
#include <map> #include <map>
#include <tuple>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <mpi.h> #include <mpi.h>
#include "main/Models.h" #include "main/Models.h"
#include "main/modelRegister.h" #include "main/ArgumentsExperiment.h"
#include "common/Paths.h" #include "common/Paths.h"
#include "common/Timer.h" #include "common/Timer.hpp"
#include "common/Colors.h" #include "common/Colors.h"
#include "common/DotEnv.h" #include "common/DotEnv.h"
#include "grid/GridSearch.h" #include "grid/GridSearch.h"
#include "grid/GridExperiment.h"
#include "config_platform.h" #include "config_platform.h"
using json = nlohmann::ordered_json; using json = nlohmann::ordered_json;
@@ -31,15 +31,20 @@ void assignModel(argparse::ArgumentParser& parser)
} }
); );
} }
void add_compute_args(argparse::ArgumentParser& program) void add_search_args(argparse::ArgumentParser& program)
{ {
auto env = platform::DotEnv(); auto env = platform::DotEnv();
program.add_argument("--discretize").help("Discretize input datasets").default_value((bool)stoi(env.get("discretize"))).implicit_value(true); program.add_argument("--discretize").help("Discretize input datasets").default_value((bool)stoi(env.get("discretize"))).implicit_value(true);
program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value((bool)stoi(env.get("stratified"))).implicit_value(true); program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value((bool)stoi(env.get("stratified"))).implicit_value(true);
program.add_argument("--quiet").help("Don't display detailed progress").default_value(false).implicit_value(true); program.add_argument("--quiet").help("Don't display detailed progress").default_value(false).implicit_value(true);
program.add_argument("--continue").help("Continue computing from that dataset").default_value(platform::GridSearch::NO_CONTINUE()); program.add_argument("--continue").help("Continue computing from that dataset").default_value(platform::GridSearch::NO_CONTINUE());
program.add_argument("--only").help("Used with continue to compute that dataset only").default_value(false).implicit_value(true); program.add_argument("--only").help("Used with continue to search with that dataset only").default_value(false).implicit_value(true);
program.add_argument("--exclude").default_value("[]").help("Datasets to exclude in json format, e.g. [\"dataset1\", \"dataset2\"]"); program.add_argument("--exclude").default_value("[]").help("Datasets to exclude in json format, e.g. [\"dataset1\", \"dataset2\"]");
auto valid_choices = env.valid_tokens("smooth_strat");
auto& smooth_arg = program.add_argument("--smooth-strat").help("Smooth strategy used in Bayes Network node initialization. Valid values: " + env.valid_values("smooth_strat")).default_value(env.get("smooth_strat"));
for (auto choice : valid_choices) {
smooth_arg.choices(choice);
}
program.add_argument("--nested").help("Set the double/nested cross validation number of folds").default_value(5).scan<'i', int>().action([](const std::string& value) { program.add_argument("--nested").help("Set the double/nested cross validation number of folds").default_value(5).scan<'i', int>().action([](const std::string& value) {
try { try {
auto k = stoi(value); auto k = stoi(value);
@@ -133,7 +138,8 @@ void list_results(json& results, std::string& model)
std::cout << std::string(MAXL, '*') << std::endl; std::cout << std::string(MAXL, '*') << std::endl;
int spaces = 7; int spaces = 7;
int hyperparameters_spaces = 15; int hyperparameters_spaces = 15;
for (const auto& item : results["results"].items()) { nlohmann::json temp = results["results"]; // To show in alphabetical order of the dataset
for (const auto& item : temp.items()) {
auto key = item.key(); auto key = item.key();
auto value = item.value(); auto value = item.value();
if (key.size() > spaces) { if (key.size() > spaces) {
@@ -148,7 +154,7 @@ void list_results(json& results, std::string& model)
std::cout << "=== " << string(spaces, '=') << " " << string(19, '=') << " " << string(8, '=') << " " std::cout << "=== " << string(spaces, '=') << " " << string(19, '=') << " " << string(8, '=') << " "
<< string(8, '=') << " " << string(hyperparameters_spaces, '=') << std::endl; << string(8, '=') << " " << string(hyperparameters_spaces, '=') << std::endl;
int index = 0; int index = 0;
for (const auto& item : results["results"].items()) { for (const auto& item : temp.items()) {
auto color = (index % 2) ? Colors::CYAN() : Colors::BLUE(); auto color = (index % 2) ? Colors::CYAN() : Colors::BLUE();
auto value = item.value(); auto value = item.value();
std::cout << color; std::cout << color;
@@ -181,13 +187,14 @@ void report(argparse::ArgumentParser& program)
list_results(results, config.model); list_results(results, config.model);
} }
} }
void compute(argparse::ArgumentParser& program) void search(argparse::ArgumentParser& program)
{ {
struct platform::ConfigGrid config; struct platform::ConfigGrid config;
config.model = program.get<std::string>("model"); config.model = program.get<std::string>("model");
config.score = program.get<std::string>("score"); config.score = program.get<std::string>("score");
config.discretize = program.get<bool>("discretize"); config.discretize = program.get<bool>("discretize");
config.stratified = program.get<bool>("stratified"); config.stratified = program.get<bool>("stratified");
config.smooth_strategy = program.get<std::string>("smooth-strat");
config.n_folds = program.get<int>("folds"); config.n_folds = program.get<int>("folds");
config.quiet = program.get<bool>("quiet"); config.quiet = program.get<bool>("quiet");
config.only = program.get<bool>("only"); config.only = program.get<bool>("only");
@@ -199,9 +206,6 @@ void compute(argparse::ArgumentParser& program)
} }
auto excluded = program.get<std::string>("exclude"); auto excluded = program.get<std::string>("exclude");
config.excluded = json::parse(excluded); config.excluded = json::parse(excluded);
auto env = platform::DotEnv();
config.platform = env.get("platform");
platform::Paths::createPath(platform::Paths::grid()); platform::Paths::createPath(platform::Paths::grid());
auto grid_search = platform::GridSearch(config); auto grid_search = platform::GridSearch(config);
platform::Timer timer; platform::Timer timer;
@@ -212,16 +216,47 @@ void compute(argparse::ArgumentParser& program)
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_config.rank); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_config.rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_config.n_procs); MPI_Comm_size(MPI_COMM_WORLD, &mpi_config.n_procs);
if (mpi_config.n_procs < 2) { if (mpi_config.n_procs < 2) {
throw std::runtime_error("Cannot use --compute with less than 2 mpi processes, try mpirun -np 2 ..."); throw std::runtime_error("Cannot use --search with less than 2 mpi processes, try mpirun -np 2 ...");
} }
grid_search.go(mpi_config); grid_search.go(mpi_config);
if (mpi_config.rank == mpi_config.manager) { if (mpi_config.rank == mpi_config.manager) {
auto results = grid_search.loadResults(); auto results = grid_search.loadResults();
std::cout << Colors::RESET() << "* Report of the computed hyperparameters" << std::endl;
list_results(results, config.model); list_results(results, config.model);
std::cout << "Process took " << timer.getDurationString() << std::endl; std::cout << "Process took " << timer.getDurationString() << std::endl;
} }
MPI_Finalize(); MPI_Finalize();
} }
void experiment(argparse::ArgumentParser& program)
{
struct platform::ConfigGrid config;
auto arguments = platform::ArgumentsExperiment(program, platform::experiment_t::GRID);
arguments.parse();
auto grid_experiment = platform::GridExperiment(arguments, config);
platform::Timer timer;
timer.start();
struct platform::ConfigMPI mpi_config;
mpi_config.manager = 0; // which process is the manager
MPI_Init(nullptr, nullptr);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_config.rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_config.n_procs);
if (mpi_config.n_procs < 2) {
throw std::runtime_error("Cannot use --experiment with less than 2 mpi processes, try mpirun -np 2 ...");
}
grid_experiment.go(mpi_config);
if (mpi_config.rank == mpi_config.manager) {
auto experiment = grid_experiment.getExperiment();
std::cout << "* Report of the computed hyperparameters" << std::endl;
auto duration = timer.getDuration();
experiment.setDuration(duration);
if (grid_experiment.haveToSaveResults()) {
experiment.saveResult();
}
experiment.report();
std::cout << "Process took " << duration << std::endl;
}
MPI_Finalize();
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// //
@@ -238,15 +273,21 @@ int main(int argc, char** argv)
assignModel(report_command); assignModel(report_command);
report_command.add_description("Report the computed hyperparameters of a model."); report_command.add_description("Report the computed hyperparameters of a model.");
// grid compute subparser // grid search subparser
argparse::ArgumentParser compute_command("compute"); argparse::ArgumentParser search_command("search");
compute_command.add_description("Compute using mpi the hyperparameters of a model."); search_command.add_description("Search using mpi the hyperparameters of a model.");
assignModel(compute_command); assignModel(search_command);
add_compute_args(compute_command); add_search_args(search_command);
// grid experiment subparser
argparse::ArgumentParser experiment_command("experiment");
experiment_command.add_description("Experiment like b_main using mpi.");
auto arguments = platform::ArgumentsExperiment(experiment_command, platform::experiment_t::GRID);
arguments.add_arguments();
program.add_subparser(dump_command); program.add_subparser(dump_command);
program.add_subparser(report_command); program.add_subparser(report_command);
program.add_subparser(compute_command); program.add_subparser(search_command);
program.add_subparser(experiment_command);
// //
// Process options // Process options
@@ -254,7 +295,7 @@ int main(int argc, char** argv)
try { try {
program.parse_args(argc, argv); program.parse_args(argc, argv);
bool found = false; bool found = false;
map<std::string, void(*)(argparse::ArgumentParser&)> commands = { {"dump", &dump}, {"report", &report}, {"compute", &compute} }; map<std::string, void(*)(argparse::ArgumentParser&)> commands = { {"dump", &dump}, {"report", &report}, {"search", &search}, { "experiment",&experiment } };
for (const auto& command : commands) { for (const auto& command : commands) {
if (program.is_subcommand_used(command.first)) { if (program.is_subcommand_used(command.first)) {
std::invoke(command.second, program.at<argparse::ArgumentParser>(command.first)); std::invoke(command.second, program.at<argparse::ArgumentParser>(command.first));
@@ -263,7 +304,7 @@ int main(int argc, char** argv)
} }
} }
if (!found) { if (!found) {
throw std::runtime_error("You must specify one of the following commands: dump, report, compute\n"); throw std::runtime_error("You must specify one of the following commands: dump, experiment, report, search \n");
} }
} }
catch (const exception& err) { catch (const exception& err) {

View File

@@ -1,234 +1,36 @@
#include <iostream>
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
#include <nlohmann/json.hpp>
#include "main/Experiment.h" #include "main/Experiment.h"
#include "common/Datasets.h" #include "main/ArgumentsExperiment.h"
#include "common/DotEnv.h"
#include "common/Paths.h"
#include "main/Models.h"
#include "main/modelRegister.h"
#include "config_platform.h" #include "config_platform.h"
using json = nlohmann::ordered_json; using json = nlohmann::ordered_json;
void manageArguments(argparse::ArgumentParser& program)
{
auto env = platform::DotEnv();
auto datasets = platform::Datasets(false, platform::Paths::datasets());
auto& group = program.add_mutually_exclusive_group(true);
group.add_argument("-d", "--dataset")
.help("Dataset file name: " + datasets.toString())
.default_value("all")
.action([](const std::string& value) {
auto datasets = platform::Datasets(false, platform::Paths::datasets());
static std::vector<std::string> choices_datasets(datasets.getNames());
choices_datasets.push_back("all");
if (find(choices_datasets.begin(), choices_datasets.end(), value) != choices_datasets.end()) {
return value;
}
throw std::runtime_error("Dataset must be one of: " + datasets.toString());
}
);
group.add_argument("--datasets").nargs(1, 50).help("Datasets file names 1..50 separated by spaces").default_value(std::vector<std::string>());
group.add_argument("--datasets-file").default_value("").help("Datasets file name. Mutually exclusive with dataset. This file should contain a list of datasets to test.");
program.add_argument("--hyperparameters").default_value("{}").help("Hyperparameters passed to the model in Experiment");
program.add_argument("--hyper-file").default_value("").help("Hyperparameters file name." \
"Mutually exclusive with hyperparameters. This file should contain hyperparameters for each dataset in json format.");
program.add_argument("--hyper-best").default_value(false).help("Use best results of the model as source of hyperparameters").implicit_value(true);
program.add_argument("-m", "--model")
.help("Model to use: " + platform::Models::instance()->toString())
.action([](const std::string& value) {
static const std::vector<std::string> choices = platform::Models::instance()->getNames();
if (find(choices.begin(), choices.end(), value) != choices.end()) {
return value;
}
throw std::runtime_error("Model must be one of " + platform::Models::instance()->toString());
}
);
program.add_argument("--title").default_value("").help("Experiment title");
program.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 = program.add_argument("--discretize-algo").help("Algorithm to use in discretization. Valid values: " + env.valid_values("discretize_algo")).default_value(env.get("discretize_algo"));
for (auto choice : valid_choices) {
disc_arg.choices(choice);
}
valid_choices = env.valid_tokens("smooth_strat");
auto& smooth_arg = program.add_argument("--smooth-strat").help("Smooth strategy used in Bayes Network node initialization. Valid values: " + env.valid_values("smooth_strat")).default_value(env.get("smooth_strat"));
for (auto choice : valid_choices) {
smooth_arg.choices(choice);
}
auto& score_arg = program.add_argument("-s", "--score").help("Score to use. Valid values: " + env.valid_values("score")).default_value(env.get("score"));
valid_choices = env.valid_tokens("score");
for (auto choice : valid_choices) {
score_arg.choices(choice);
}
program.add_argument("--generate-fold-files").help("generate fold information in datasets_experiment folder").default_value(false).implicit_value(true);
program.add_argument("--graph").help("generate graphviz dot files with the model").default_value(false).implicit_value(true);
program.add_argument("--no-train-score").help("Don't compute train score").default_value(false).implicit_value(true);
program.add_argument("--quiet").help("Don't display detailed progress").default_value(false).implicit_value(true);
program.add_argument("--save").help("Save result (always save if no dataset is supplied)").default_value(false).implicit_value(true);
program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value((bool)stoi(env.get("stratified"))).implicit_value(true);
program.add_argument("-f", "--folds").help("Number of folds").default_value(stoi(env.get("n_folds"))).scan<'i', int>().action([](const std::string& value) {
try {
auto k = stoi(value);
if (k < 2) {
throw std::runtime_error("Number of folds must be greater than 1");
}
return k;
}
catch (const runtime_error& err) {
throw std::runtime_error(err.what());
}
catch (...) {
throw std::runtime_error("Number of folds must be an integer");
}});
auto seed_values = env.getSeeds();
program.add_argument("--seeds").nargs(1, 10).help("Random seeds. Set to -1 to have pseudo random").scan<'i', int>().default_value(seed_values);
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
argparse::ArgumentParser program("b_main", { platform_project_version.begin(), platform_project_version.end() }); argparse::ArgumentParser program("b_main", { platform_project_version.begin(), platform_project_version.end() });
manageArguments(program); auto arguments = platform::ArgumentsExperiment(program, platform::experiment_t::NORMAL);
std::string file_name, model_name, title, hyperparameters_file, datasets_file, discretize_algo, smooth_strat, score; arguments.add_arguments();
json hyperparameters_json; arguments.parse_args(argc, argv);
bool discretize_dataset, stratified, saveResults, quiet, no_train_score, generate_fold_files, graph, hyper_best;
std::vector<int> seeds;
std::vector<std::string> file_names;
std::vector<std::string> filesToTest;
int n_folds;
try {
program.parse_args(argc, argv);
file_name = program.get<std::string>("dataset");
file_names = program.get<std::vector<std::string>>("datasets");
datasets_file = program.get<std::string>("datasets-file");
model_name = program.get<std::string>("model");
discretize_dataset = program.get<bool>("discretize");
discretize_algo = program.get<std::string>("discretize-algo");
smooth_strat = program.get<std::string>("smooth-strat");
stratified = program.get<bool>("stratified");
quiet = program.get<bool>("quiet");
graph = program.get<bool>("graph");
n_folds = program.get<int>("folds");
score = program.get<std::string>("score");
seeds = program.get<std::vector<int>>("seeds");
auto hyperparameters = program.get<std::string>("hyperparameters");
hyperparameters_json = json::parse(hyperparameters);
hyperparameters_file = program.get<std::string>("hyper-file");
no_train_score = program.get<bool>("no-train-score");
hyper_best = program.get<bool>("hyper-best");
generate_fold_files = program.get<bool>("generate-fold-files");
if (hyper_best) {
// Build the best results file_name
hyperparameters_file = platform::Paths::results() + platform::Paths::bestResultsFile(score, model_name);
// ignore this parameter
hyperparameters = "{}";
} else {
if (hyperparameters_file != "" && hyperparameters != "{}") {
throw runtime_error("hyperparameters and hyper_file are mutually exclusive");
}
}
title = program.get<std::string>("title");
if (title == "" && file_name == "all") {
throw runtime_error("title is mandatory if all datasets are to be tested");
}
saveResults = program.get<bool>("save");
}
catch (const exception& err) {
cerr << err.what() << std::endl;
cerr << program;
exit(1);
}
auto datasets = platform::Datasets(false, platform::Paths::datasets());
if (datasets_file != "") {
ifstream catalog(datasets_file);
if (catalog.is_open()) {
std::string line;
while (getline(catalog, line)) {
if (line.empty() || line[0] == '#') {
continue;
}
if (!datasets.isDataset(line)) {
cerr << "Dataset " << line << " not found" << std::endl;
exit(1);
}
filesToTest.push_back(line);
}
catalog.close();
saveResults = true;
if (title == "") {
title = "Test " + to_string(filesToTest.size()) + " datasets (" + datasets_file + ") "\
+ model_name + " " + to_string(n_folds) + " folds";
}
} else {
throw std::invalid_argument("Unable to open catalog file. [" + datasets_file + "]");
}
} else {
if (file_names.size() > 0) {
for (auto file : file_names) {
if (!datasets.isDataset(file)) {
cerr << "Dataset " << file << " not found" << std::endl;
exit(1);
}
}
filesToTest = file_names;
saveResults = true;
if (title == "") {
title = "Test " + to_string(file_names.size()) + " datasets " + model_name + " " + to_string(n_folds) + " folds";
}
} else {
if (file_name != "all") {
if (!datasets.isDataset(file_name)) {
cerr << "Dataset " << file_name << " not found" << std::endl;
exit(1);
}
if (title == "") {
title = "Test " + file_name + " " + model_name + " " + to_string(n_folds) + " folds";
}
filesToTest.push_back(file_name);
} else {
filesToTest = datasets.getNames();
saveResults = true;
}
}
}
platform::HyperParameters test_hyperparams;
if (hyperparameters_file != "") {
test_hyperparams = platform::HyperParameters(datasets.getNames(), hyperparameters_file, hyper_best);
} else {
test_hyperparams = platform::HyperParameters(datasets.getNames(), hyperparameters_json);
}
/* /*
* Begin Processing * Begin Processing
*/ */
auto env = platform::DotEnv(); // Initialize the experiment class with the command line arguments
auto experiment = platform::Experiment(); auto experiment = arguments.initializedExperiment();
experiment.setTitle(title).setLanguage("c++").setLanguageVersion("gcc 14.1.1");
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);
experiment.setHyperparameters(test_hyperparams);
for (auto seed : seeds) {
experiment.addRandomSeed(seed);
}
platform::Timer timer; platform::Timer timer;
timer.start(); timer.start();
experiment.go(filesToTest, quiet, no_train_score, generate_fold_files, graph); experiment.go();
experiment.setDuration(timer.getDuration()); experiment.setDuration(timer.getDuration());
if (!quiet) { if (!arguments.isQuiet()) {
// Classification report if only one dataset is tested // Classification report if only one dataset is tested
experiment.report(filesToTest.size() == 1); experiment.report();
} }
if (saveResults) { if (arguments.haveToSaveResults()) {
experiment.saveResult(); experiment.saveResult();
} }
if (graph) { if (arguments.doGraph()) {
experiment.saveGraph(); experiment.saveGraph();
} }
std::cout << "Done!" << std::endl;
return 0; return 0;
} }

View File

@@ -2,6 +2,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <utility> #include <utility>
#include <unistd.h> #include <unistd.h>
#include "common/Paths.h"
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
#include "manage/ManageScreen.h" #include "manage/ManageScreen.h"
#include <signal.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("-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("-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("--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("--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); program.add_argument("--partial").help("Show only partial results").default_value(false).implicit_value(true);
@@ -51,11 +53,72 @@ void handleResize(int sig)
manager->updateSize(rows, cols); 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) int main(int argc, char** argv)
{ {
auto program = argparse::ArgumentParser("b_manage", { platform_project_version.begin(), platform_project_version.end() }); auto program = argparse::ArgumentParser("b_manage", { platform_project_version.begin(), platform_project_version.end() });
manageArguments(program, argc, argv); manageArguments(program, argc, argv);
std::string model = program.get<std::string>("model"); std::string model = program.get<std::string>("model");
std::string path = program.get<std::string>("folder");
std::string score = program.get<std::string>("score"); std::string score = program.get<std::string>("score");
std::string platform = program.get<std::string>("platform"); std::string platform = program.get<std::string>("platform");
bool complete = program.get<bool>("complete"); bool complete = program.get<bool>("complete");
@@ -65,8 +128,13 @@ int main(int argc, char** argv)
partial = false; partial = false;
signal(SIGWINCH, handleResize); signal(SIGWINCH, handleResize);
auto [rows, cols] = numRowsCols(); 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(); manager->doMenu();
auto fileName = manager->getExcelFileName();
delete manager; delete manager;
if (!fileName.empty()) {
std::cout << "Opening " << fileName << std::endl;
openFile(fileName);
}
return 0; return 0;
} }

102
src/commands/b_results.cpp Normal file
View File

@@ -0,0 +1,102 @@
#include <iostream>
#include <filesystem>
#include <fstream>
#include <vector>
#include <argparse/argparse.hpp>
#include <nlohmann/json.hpp>
#include "common/Paths.h"
#include "results/JsonValidator.h"
#include "results/SchemaV1_0.h"
#include "config_platform.h"
using json = nlohmann::json;
namespace fs = std::filesystem;
void header(const std::string& message, int length, const std::string& symbol)
{
std::cout << std::string(length + 11, symbol[0]) << std::endl;
std::cout << symbol << " " << std::setw(length + 7) << std::left << message << " " << symbol << std::endl;
std::cout << std::string(length + 11, symbol[0]) << std::endl;
}
int main(int argc, char* argv[])
{
argparse::ArgumentParser program("b_results", { platform_project_version.begin(), platform_project_version.end() });
program.add_description("Check the results files and optionally fixes them.");
program.add_argument("--fix").help("Fix any errors in results").default_value(false).implicit_value(true);
program.add_argument("--file").help("check only this results file").default_value("");
std::string nameSuffix = "results_";
std::string schemaVersion = "1.0";
bool fix_it = false;
std::string selected_file;
try {
program.parse_args(argc, argv);
fix_it = program.get<bool>("fix");
selected_file = program.get<std::string>("file");
}
catch (const std::exception& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
exit(1);
}
//
// Determine the files to process
//
std::vector<std::string> result_files;
int max_length = 0;
if (selected_file != "") {
if (!selected_file.starts_with(platform::Paths::results())) {
selected_file = platform::Paths::results() + selected_file;
}
// Only check the selected file
result_files.push_back(selected_file);
max_length = selected_file.length();
} else {
// Load the result files and find the longest file name
for (const auto& entry : fs::directory_iterator(platform::Paths::results())) {
if (entry.is_regular_file() && entry.path().filename().string().starts_with(nameSuffix) && entry.path().filename().string().ends_with(".json")) {
std::string fileName = entry.path().string();
if (fileName.length() > max_length) {
max_length = fileName.length();
}
result_files.push_back(fileName);
}
}
}
//
// Process the results files
//
if (result_files.empty()) {
std::cerr << "Error: No result files found." << std::endl;
return 1;
}
std::string header_message = "Processing " + std::to_string(result_files.size()) + " result files.";
header(header_message, max_length, "*");
platform::JsonValidator validator(platform::SchemaV1_0::schema);
int n_errors = 0;
std::vector<std::string> files_with_errors;
for (const auto& file_name : result_files) {
std::vector<std::string> errors = validator.validate_file(file_name);
if (!errors.empty()) {
n_errors++;
std::cout << std::setw(max_length) << std::left << file_name << ": " << errors.size() << " Errors:" << std::endl;
for (const auto& error : errors) {
std::cout << " - " << error << std::endl;
}
if (fix_it) {
validator.fix_it(file_name);
std::cout << " -> File fixed." << std::endl;
}
files_with_errors.push_back(file_name);
}
}
if (n_errors == 0) {
header("All files are valid.", max_length, "*");
} else {
std::string $verb = (fix_it) ? "had" : "have";
std::string msg = std::to_string(n_errors) + " files " + $verb + " errors.";
header(msg, max_length, "*");
for (const auto& file_name : files_with_errors) {
std::cout << "- " << file_name << std::endl;
}
}
return 0;
}

View File

@@ -1,4 +1,4 @@
#include <ArffFiles.hpp> #include <ArffFiles/ArffFiles.hpp>
#include <fstream> #include <fstream>
#include "Dataset.h" #include "Dataset.h"
namespace platform { namespace platform {

View File

@@ -1,4 +1,5 @@
#include <fstream> #include <fstream>
#include<algorithm>
#include "Datasets.h" #include "Datasets.h"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@@ -24,10 +25,20 @@ namespace platform {
throw std::invalid_argument("Unable to open catalog file. [" + path + "all.txt" + "]"); throw std::invalid_argument("Unable to open catalog file. [" + path + "all.txt" + "]");
} }
std::string line; std::string line;
std::vector<std::string> sorted_lines;
while (getline(catalog, line)) { while (getline(catalog, line)) {
if (line.empty() || line[0] == '#') { if (line.empty() || line[0] == '#') {
continue; continue;
} }
sorted_lines.push_back(line);
}
sort(sorted_lines.begin(), sorted_lines.end(), [](const auto& lhs, const auto& rhs) {
const auto result = mismatch(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), [](const auto& lhs, const auto& rhs) {return tolower(lhs) == tolower(rhs);});
return result.second != rhs.cend() && (result.first == lhs.cend() || tolower(*result.first) < tolower(*result.second));
});
for (const auto& line : sorted_lines) {
std::vector<std::string> tokens = split(line, ';'); std::vector<std::string> tokens = split(line, ';');
std::string name = tokens[0]; std::string name = tokens[0];
std::string className; std::string className;
@@ -70,6 +81,11 @@ namespace platform {
{ {
std::vector<std::string> result; std::vector<std::string> result;
transform(datasets.begin(), datasets.end(), back_inserter(result), [](const auto& d) { return d.first; }); transform(datasets.begin(), datasets.end(), back_inserter(result), [](const auto& d) { return d.first; });
sort(result.begin(), result.end(), [](const auto& lhs, const auto& rhs) {
const auto result = mismatch(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), [](const auto& lhs, const auto& rhs) {return tolower(lhs) == tolower(rhs);});
return result.second != rhs.cend() && (result.first == lhs.cend() || tolower(*result.first) < tolower(*result.second));
});
return result; return result;
} }
bool Datasets::isDataset(const std::string& name) const bool Datasets::isDataset(const std::string& name) const

View File

@@ -0,0 +1,53 @@
#ifndef COUNTING_SEMAPHORE_H
#define COUNTING_SEMAPHORE_H
#include <mutex>
#include <condition_variable>
#include <algorithm>
#include <thread>
#include <mutex>
#include <condition_variable>
class CountingSemaphore {
public:
static CountingSemaphore& getInstance()
{
static CountingSemaphore instance;
return instance;
}
// Delete copy constructor and assignment operator
CountingSemaphore(const CountingSemaphore&) = delete;
CountingSemaphore& operator=(const CountingSemaphore&) = delete;
void acquire()
{
std::unique_lock<std::mutex> lock(mtx_);
cv_.wait(lock, [this]() { return count_ > 0; });
--count_;
}
void release()
{
std::lock_guard<std::mutex> lock(mtx_);
++count_;
if (count_ <= max_count_) {
cv_.notify_one();
}
}
uint getCount() const
{
return count_;
}
uint getMaxCount() const
{
return max_count_;
}
private:
CountingSemaphore()
: max_count_(std::max(1u, static_cast<uint>(0.95 * std::thread::hardware_concurrency()))),
count_(max_count_)
{
}
std::mutex mtx_;
std::condition_variable cv_;
const uint max_count_;
uint count_;
};
#endif

View File

@@ -0,0 +1,182 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#include "ExpClf.h"
#include "TensorUtils.hpp"
namespace platform {
ExpClf::ExpClf() : semaphore_{ CountingSemaphore::getInstance() }, Boost(false)
{
validHyperparameters = {};
}
//
// Parents
//
void ExpClf::add_active_parents(const std::vector<int>& active_parents)
{
for (const auto& parent : active_parents)
aode_.add_active_parent(parent);
}
void ExpClf::add_active_parent(int parent)
{
aode_.add_active_parent(parent);
}
void ExpClf::remove_last_parent()
{
aode_.remove_last_parent();
}
//
// Predict
//
std::vector<int> ExpClf::predict_spode(std::vector<std::vector<int>>& test_data, int parent)
{
int test_size = test_data[0].size();
int sample_size = test_data.size();
auto predictions = std::vector<int>(test_size);
int chunk_size = std::min(150, int(test_size / semaphore_.getMaxCount()) + 1);
std::vector<std::thread> threads;
auto worker = [&](const std::vector<std::vector<int>>& samples, int begin, int chunk, int sample_size, std::vector<int>& predictions) {
std::string threadName = "(V)PWorker-" + std::to_string(begin) + "-" + std::to_string(chunk);
#if defined(__linux__)
pthread_setname_np(pthread_self(), threadName.c_str());
#else
pthread_setname_np(threadName.c_str());
#endif
std::vector<int> instance(sample_size);
for (int sample = begin; sample < begin + chunk; ++sample) {
for (int feature = 0; feature < sample_size; ++feature) {
instance[feature] = samples[feature][sample];
}
predictions[sample] = aode_.predict_spode(instance, parent);
}
semaphore_.release();
};
for (int begin = 0; begin < test_size; begin += chunk_size) {
int chunk = std::min(chunk_size, test_size - begin);
semaphore_.acquire();
threads.emplace_back(worker, test_data, begin, chunk, sample_size, std::ref(predictions));
}
for (auto& thread : threads) {
thread.join();
}
return predictions;
}
torch::Tensor ExpClf::predict(torch::Tensor& X)
{
auto X_ = TensorUtils::to_matrix(X);
torch::Tensor y = torch::tensor(predict(X_));
return y;
}
torch::Tensor ExpClf::predict_proba(torch::Tensor& X)
{
auto X_ = TensorUtils::to_matrix(X);
auto probabilities = predict_proba(X_);
auto n_samples = X.size(1);
int n_classes = probabilities[0].size();
auto y = torch::zeros({ n_samples, n_classes });
for (int i = 0; i < n_samples; i++) {
for (int j = 0; j < n_classes; j++) {
y[i][j] = probabilities[i][j];
}
}
return y;
}
float ExpClf::score(torch::Tensor& X, torch::Tensor& y)
{
auto X_ = TensorUtils::to_matrix(X);
auto y_ = TensorUtils::to_vector<int>(y);
return score(X_, y_);
}
std::vector<std::vector<double>> ExpClf::predict_proba(const std::vector<std::vector<int>>& test_data)
{
int test_size = test_data[0].size();
int sample_size = test_data.size();
auto probabilities = std::vector<std::vector<double>>(test_size, std::vector<double>(aode_.statesClass()));
int chunk_size = std::min(150, int(test_size / semaphore_.getMaxCount()) + 1);
std::vector<std::thread> threads;
auto worker = [&](const std::vector<std::vector<int>>& samples, int begin, int chunk, int sample_size, std::vector<std::vector<double>>& predictions) {
std::string threadName = "(V)PWorker-" + std::to_string(begin) + "-" + std::to_string(chunk);
#if defined(__linux__)
pthread_setname_np(pthread_self(), threadName.c_str());
#else
pthread_setname_np(threadName.c_str());
#endif
std::vector<int> instance(sample_size);
for (int sample = begin; sample < begin + chunk; ++sample) {
for (int feature = 0; feature < sample_size; ++feature) {
instance[feature] = samples[feature][sample];
}
predictions[sample] = aode_.predict_proba(instance);
}
semaphore_.release();
};
for (int begin = 0; begin < test_size; begin += chunk_size) {
int chunk = std::min(chunk_size, test_size - begin);
semaphore_.acquire();
threads.emplace_back(worker, test_data, begin, chunk, sample_size, std::ref(probabilities));
}
for (auto& thread : threads) {
thread.join();
}
return probabilities;
}
std::vector<int> ExpClf::predict(std::vector<std::vector<int>>& test_data)
{
if (!fitted) {
throw std::logic_error(CLASSIFIER_NOT_FITTED);
}
auto probabilities = predict_proba(test_data);
std::vector<int> predictions(probabilities.size(), 0);
for (size_t i = 0; i < probabilities.size(); i++) {
predictions[i] = std::distance(probabilities[i].begin(), std::max_element(probabilities[i].begin(), probabilities[i].end()));
}
return predictions;
}
float ExpClf::score(std::vector<std::vector<int>>& test_data, std::vector<int>& labels)
{
Timer timer;
timer.start();
std::vector<int> predictions = predict(test_data);
int correct = 0;
for (size_t i = 0; i < predictions.size(); i++) {
if (predictions[i] == labels[i]) {
correct++;
}
}
if (debug) {
std::cout << "* Time to predict: " << timer.getDurationString() << std::endl;
}
return static_cast<float>(correct) / predictions.size();
}
//
// statistics
//
int ExpClf::getNumberOfNodes() const
{
return aode_.getNumberOfNodes();
}
int ExpClf::getNumberOfEdges() const
{
return aode_.getNumberOfEdges();
}
int ExpClf::getNumberOfStates() const
{
return aode_.getNumberOfStates();
}
int ExpClf::getClassNumStates() const
{
return aode_.statesClass();
}
}

View File

@@ -0,0 +1,67 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#ifndef EXPCLF_H
#define EXPCLF_H
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <limits>
#include <bayesnet/ensembles/Boost.h>
#include <bayesnet/network/Smoothing.h>
#include "common/Timer.hpp"
#include "CountingSemaphore.hpp"
#include "Xaode.hpp"
namespace platform {
class ExpClf : public bayesnet::Boost {
public:
ExpClf();
virtual ~ExpClf() = default;
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
torch::Tensor predict(torch::Tensor& X) override;
torch::Tensor predict_proba(torch::Tensor& X) override;
std::vector<int> predict_spode(std::vector<std::vector<int>>& test_data, int parent);
std::vector<std::vector<double>> predict_proba(const std::vector<std::vector<int>>& X);
float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
float score(torch::Tensor& X, torch::Tensor& y) override;
int getNumberOfNodes() const override;
int getNumberOfEdges() const override;
int getNumberOfStates() const override;
int getClassNumStates() const override;
std::vector<std::string> show() const override { return {}; }
std::vector<std::string> topological_order() override { return {}; }
std::string dump_cpt() const override { return ""; }
void setDebug(bool debug) { this->debug = debug; }
bayesnet::status_t getStatus() const override { return status; }
std::vector<std::string> getNotes() const override { return notes; }
std::vector<std::string> graph(const std::string& title = "") const override { return {}; }
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_;
torch::Tensor weights_;
const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
inline void normalize_weights(int num_instances)
{
double sum = weights_.sum().item<double>();
if (sum == 0) {
weights_ = torch::full({ num_instances }, 1.0);
} else {
for (int i = 0; i < weights_.size(0); ++i) {
weights_[i] = weights_[i].item<double>() * num_instances / sum;
}
}
}
private:
CountingSemaphore& semaphore_;
};
}
#endif // EXPCLF_H

View File

@@ -0,0 +1,158 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#include "ExpEnsemble.h"
#include "TensorUtils.hpp"
namespace platform {
ExpEnsemble::ExpEnsemble() : semaphore_{ CountingSemaphore::getInstance() }, Boost(false)
{
validHyperparameters = {};
}
//
// Parents
//
void ExpEnsemble::add_model(std::unique_ptr<XSpode> model)
{
models.push_back(std::move(model));
n_models++;
}
void ExpEnsemble::remove_last_model()
{
models.pop_back();
n_models--;
}
//
// Predict
//
torch::Tensor ExpEnsemble::predict(torch::Tensor& X)
{
auto X_ = TensorUtils::to_matrix(X);
torch::Tensor y = torch::tensor(predict(X_));
return y;
}
torch::Tensor ExpEnsemble::predict_proba(torch::Tensor& X)
{
auto X_ = TensorUtils::to_matrix(X);
auto probabilities = predict_proba(X_);
auto n_samples = X.size(1);
int n_classes = probabilities[0].size();
auto y = torch::zeros({ n_samples, n_classes });
for (int i = 0; i < n_samples; i++) {
for (int j = 0; j < n_classes; j++) {
y[i][j] = probabilities[i][j];
}
}
return y;
}
float ExpEnsemble::score(torch::Tensor& X, torch::Tensor& y)
{
auto X_ = TensorUtils::to_matrix(X);
auto y_ = TensorUtils::to_vector<int>(y);
return score(X_, y_);
}
std::vector<std::vector<double>> ExpEnsemble::predict_proba(const std::vector<std::vector<int>>& test_data)
{
int test_size = test_data[0].size();
int sample_size = test_data.size();
auto probabilities = std::vector<std::vector<double>>(test_size, std::vector<double>(getClassNumStates()));
int chunk_size = std::min(150, int(test_size / semaphore_.getMaxCount()) + 1);
std::vector<std::thread> threads;
auto worker = [&](const std::vector<std::vector<int>>& samples, int begin, int chunk, int sample_size, std::vector<std::vector<double>>& predictions) {
std::string threadName = "(V)PWorker-" + std::to_string(begin) + "-" + std::to_string(chunk);
#if defined(__linux__)
pthread_setname_np(pthread_self(), threadName.c_str());
#else
pthread_setname_np(threadName.c_str());
#endif
std::vector<int> instance(sample_size);
for (int sample = begin; sample < begin + chunk; ++sample) {
for (int feature = 0; feature < sample_size; ++feature) {
instance[feature] = samples[feature][sample];
}
// predictions[sample] = aode_.predict_proba(instance);
}
semaphore_.release();
};
for (int begin = 0; begin < test_size; begin += chunk_size) {
int chunk = std::min(chunk_size, test_size - begin);
semaphore_.acquire();
threads.emplace_back(worker, test_data, begin, chunk, sample_size, std::ref(probabilities));
}
for (auto& thread : threads) {
thread.join();
}
return probabilities;
}
std::vector<int> ExpEnsemble::predict(std::vector<std::vector<int>>& test_data)
{
if (!fitted) {
throw std::logic_error(CLASSIFIER_NOT_FITTED);
}
auto probabilities = predict_proba(test_data);
std::vector<int> predictions(probabilities.size(), 0);
for (size_t i = 0; i < probabilities.size(); i++) {
predictions[i] = std::distance(probabilities[i].begin(), std::max_element(probabilities[i].begin(), probabilities[i].end()));
}
return predictions;
}
float ExpEnsemble::score(std::vector<std::vector<int>>& test_data, std::vector<int>& labels)
{
Timer timer;
timer.start();
std::vector<int> predictions = predict(test_data);
int correct = 0;
for (size_t i = 0; i < predictions.size(); i++) {
if (predictions[i] == labels[i]) {
correct++;
}
}
if (debug) {
std::cout << "* Time to predict: " << timer.getDurationString() << std::endl;
}
return static_cast<float>(correct) / predictions.size();
}
//
// statistics
//
int ExpEnsemble::getNumberOfNodes() const
{
if (models_.empty()) {
return 0;
}
return n_models * (models_.at(0)->getNFeatures() + 1);
}
int ExpEnsemble::getNumberOfEdges() const
{
if (models_.empty()) {
return 0;
}
return n_models * (2 * models_.at(0)->getNFeatures() - 1);
}
int ExpEnsemble::getNumberOfStates() const
{
if (models_.empty()) {
return 0;
}
auto states = models_.at(0)->getStates();
int nFeatures = models_.at(0)->getNFeatures();
return std::accumulate(states.begin(), states.end(), 0) * nFeatures * n_models;
}
int ExpEnsemble::getClassNumStates() const
{
if (models_.empty()) {
return 0;
}
return models_.at(0)->statesClass();
}
}

View File

@@ -0,0 +1,66 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#ifndef EXPENSEMBLE_H
#define EXPENSEMBLE_H
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <limits>
#include <bayesnet/ensembles/Boost.h>
#include <bayesnet/network/Smoothing.h>
#include "common/Timer.hpp"
#include "CountingSemaphore.hpp"
#include "XSpode.hpp"
namespace platform {
class ExpEnsemble : public bayesnet::Boost {
public:
ExpEnsemble();
virtual ~ExpEnsemble() = default;
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
torch::Tensor predict(torch::Tensor& X) override;
torch::Tensor predict_proba(torch::Tensor& X) override;
std::vector<int> predict_spode(std::vector<std::vector<int>>& test_data, int parent);
std::vector<std::vector<double>> predict_proba(const std::vector<std::vector<int>>& X);
float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
float score(torch::Tensor& X, torch::Tensor& y) override;
int getNumberOfNodes() const override;
int getNumberOfEdges() const override;
int getNumberOfStates() const override;
int getClassNumStates() const override;
std::vector<std::string> show() const override { return {}; }
std::vector<std::string> topological_order() override { return {}; }
std::string dump_cpt() const override { return ""; }
void setDebug(bool debug) { this->debug = debug; }
bayesnet::status_t getStatus() const override { return status; }
std::vector<std::string> getNotes() const override { return notes; }
std::vector<std::string> graph(const std::string& title = "") const override { return {}; }
protected:
void add_model(std::unique_ptr<XSpode> model);
void remove_last_model();
bool debug = false;
std::vector <std::unique_ptr<XSpode>> models_;
torch::Tensor weights_;
std::vector<double> significanceModels_;
const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
inline void normalize_weights(int num_instances)
{
double sum = weights_.sum().item<double>();
if (sum == 0) {
weights_ = torch::full({ num_instances }, 1.0);
} else {
for (int i = 0; i < weights_.size(0); ++i) {
weights_[i] = weights_[i].item<double>() * num_instances / sum;
}
}
}
private:
CountingSemaphore& semaphore_;
};
}
#endif // EXPENSEMBLE_H

View File

@@ -0,0 +1,51 @@
#ifndef TENSORUTILS_HPP
#define TENSORUTILS_HPP
#include <torch/torch.h>
#include <vector>
namespace platform {
class TensorUtils {
public:
static std::vector<std::vector<int>> to_matrix(const torch::Tensor& X)
{
// Ensure tensor is contiguous in memory
auto X_contig = X.contiguous();
// Access tensor data pointer directly
auto data_ptr = X_contig.data_ptr<int>();
// IF you are using int64_t as the data type, use the following line
//auto data_ptr = X_contig.data_ptr<int64_t>();
//std::vector<std::vector<int64_t>> data(X.size(0), std::vector<int64_t>(X.size(1)));
// Prepare output container
std::vector<std::vector<int>> data(X.size(0), std::vector<int>(X.size(1)));
// Fill the 2D vector in a single loop using pointer arithmetic
int rows = X.size(0);
int cols = X.size(1);
for (int i = 0; i < rows; ++i) {
std::copy(data_ptr + i * cols, data_ptr + (i + 1) * cols, data[i].begin());
}
return data;
}
template <typename T>
static std::vector<T> to_vector(const torch::Tensor& y)
{
// Ensure the tensor is contiguous in memory
auto y_contig = y.contiguous();
// Access data pointer
auto data_ptr = y_contig.data_ptr<T>();
// Prepare output container
std::vector<T> data(y.size(0));
// Copy data efficiently
std::copy(data_ptr, data_ptr + y.size(0), data.begin());
return data;
}
};
}
#endif // TENSORUTILS_HPP

View File

@@ -0,0 +1,20 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#include "XA1DE.h"
#include "TensorUtils.hpp"
namespace platform {
void XA1DE::trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing)
{
auto X = TensorUtils::to_matrix(dataset.slice(0, 0, dataset.size(0) - 1));
auto y = TensorUtils::to_vector<int>(dataset.index({ -1, "..." }));
int num_instances = X[0].size();
weights_ = torch::full({ num_instances }, 1.0);
//normalize_weights(num_instances);
aode_.fit(X, y, features, className, states, weights_, true, smoothing);
}
}

View File

@@ -0,0 +1,26 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#ifndef XA1DE_H
#define XA1DE_H
#include "Xaode.hpp"
#include "ExpClf.h"
#include <bayesnet/network/Smoothing.h>
namespace platform {
class XA1DE : public ExpClf {
public:
XA1DE() = default;
virtual ~XA1DE() override = default;
std::string getVersion() override { return version; };
protected:
void buildModel(const torch::Tensor& weights) override {};
void trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) override;
private:
std::string version = "1.0.0";
};
}
#endif // XA1DE_H

View File

@@ -0,0 +1,183 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#include <random>
#include <set>
#include <functional>
#include <limits.h>
#include <tuple>
#include "XBAODE.h"
#include "XSpode.hpp"
#include "TensorUtils.hpp"
#include <loguru.hpp>
namespace platform {
XBAODE::XBAODE()
{
validHyperparameters = { "alpha_block", "order", "convergence", "convergence_best", "bisection", "threshold", "maxTolerance",
"predict_voting", "select_features" };
}
void XBAODE::add_model(std::unique_ptr<XSpode> model)
{
models.push_back(std::move(model));
n_models++;
}
void XBAODE::remove_last_model()
{
models.pop_back();
n_models--;
}
void XBAODE::trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing)
{
fitted = true;
X_train_ = TensorUtils::to_matrix(X_train);
y_train_ = TensorUtils::to_vector<int>(y_train);
X_test_ = TensorUtils::to_matrix(X_test);
y_test_ = TensorUtils::to_vector<int>(y_test);
maxTolerance = 3;
//
// Logging setup
//
// loguru::set_thread_name("XBAODE");
// loguru::g_stderr_verbosity = loguru::Verbosity_OFF;
// loguru::add_file("XBAODE.log", loguru::Truncate, loguru::Verbosity_MAX);
// Algorithm based on the adaboost algorithm for classification
// as explained in Ensemble methods (Zhi-Hua Zhou, 2012)
double alpha_t = 0;
weights_ = torch::full({ m }, 1.0 / static_cast<double>(m), torch::kFloat64); // m initialized in Classifier.cc
significanceModels.resize(n, 0.0); // n initialized in Classifier.cc
bool finished = false;
std::vector<int> featuresUsed;
n_models = 0;
std::unique_ptr<XSpode> model;
if (selectFeatures) {
featuresUsed = featureSelection(weights_);
for (const auto& parent : featuresUsed) {
model = std::unique_ptr<XSpode>(new XSpode(parent));
model->fit(X_train_, y_train_, weights_, smoothing);
std::cout << model->getNFeatures() << std::endl;
add_model(std::move(model));
}
notes.push_back("Used features in initialization: " + std::to_string(featuresUsed.size()) + " of " + std::to_string(features.size()) + " with " + select_features_algorithm);
auto ypred = ExpEnsemble::predict(X_train);
std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
// Update significance of the models
for (const auto& parent : featuresUsed) {
significanceModels_[parent] = alpha_t;
}
n_models = featuresUsed.size();
// VLOG_SCOPE_F(1, "SelectFeatures. alpha_t: %f n_models: %d", alpha_t, n_models);
if (finished) {
return;
}
}
int numItemsPack = 0; // The counter of the models inserted in the current pack
// Variables to control the accuracy finish condition
double priorAccuracy = 0.0;
double improvement = 1.0;
double convergence_threshold = 1e-4;
int tolerance = 0; // number of times the accuracy is lower than the convergence_threshold
// Step 0: Set the finish condition
// epsilon sub t > 0.5 => inverse the weights policy
// validation error is not decreasing
// run out of features
bool ascending = order_algorithm == bayesnet::Orders.ASC;
std::mt19937 g{ 173 };
while (!finished) {
// Step 1: Build ranking with mutual information
auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n); // Get all the features sorted
if (order_algorithm == bayesnet::Orders.RAND) {
std::shuffle(featureSelection.begin(), featureSelection.end(), g);
}
// Remove used features
featureSelection.erase(remove_if(featureSelection.begin(), featureSelection.end(), [&](auto x)
{ return std::find(featuresUsed.begin(), featuresUsed.end(), x) != featuresUsed.end();}),
featureSelection.end()
);
int k = bisection ? pow(2, tolerance) : 1;
int counter = 0; // The model counter of the current pack
// VLOG_SCOPE_F(1, "counter=%d k=%d featureSelection.size: %zu", counter, k, featureSelection.size());
while (counter++ < k && featureSelection.size() > 0) {
auto feature = featureSelection[0];
featureSelection.erase(featureSelection.begin());
model = std::unique_ptr<XSpode>(new XSpode(feature));
model->fit(X_train_, y_train_, weights_, smoothing);
std::vector<int> ypred;
if (alpha_block) {
//
// Compute the prediction with the current ensemble + model
//
// Add the model to the ensemble
significanceModels[feature] = 1.0;
add_model(std::move(model));
// Compute the prediction
ypred = ExpEnsemble::predict(X_train_);
// Remove the model from the ensemble
significanceModels[feature] = 0.0;
model = std::move(models_.back());
remove_last_model();
} else {
ypred = model->predict(X_train_);
}
// Step 3.1: Compute the classifier amout of say
auto ypred_t = torch::tensor(ypred);
std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred_t, weights_);
// Step 3.4: Store classifier and its accuracy to weigh its future vote
numItemsPack++;
featuresUsed.push_back(feature);
add_model(std::move(model));
significanceModels[feature] = alpha_t;
// VLOG_SCOPE_F(2, "finished: %d numItemsPack: %d n_models: %d featuresUsed: %zu", finished, numItemsPack, n_models, featuresUsed.size());
} // End of the pack
if (convergence && !finished) {
auto y_val_predict = ExpEnsemble::predict(X_test);
double accuracy = (y_val_predict == y_test).sum().item<double>() / (double)y_test.size(0);
if (priorAccuracy == 0) {
priorAccuracy = accuracy;
} else {
improvement = accuracy - priorAccuracy;
}
if (improvement < convergence_threshold) {
// VLOG_SCOPE_F(3, " (improvement<threshold) tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy);
tolerance++;
} else {
// VLOG_SCOPE_F(3, "* (improvement>=threshold) Reset. tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy);
tolerance = 0; // Reset the counter if the model performs better
numItemsPack = 0;
}
if (convergence_best) {
// Keep the best accuracy until now as the prior accuracy
priorAccuracy = std::max(accuracy, priorAccuracy);
} else {
// Keep the last accuray obtained as the prior accuracy
priorAccuracy = accuracy;
}
}
// VLOG_SCOPE_F(1, "tolerance: %d featuresUsed.size: %zu features.size: %zu", tolerance, featuresUsed.size(), features.size());
finished = finished || tolerance > maxTolerance || featuresUsed.size() == features.size();
}
if (tolerance > maxTolerance) {
if (numItemsPack < n_models) {
notes.push_back("Convergence threshold reached & " + std::to_string(numItemsPack) + " models eliminated");
// VLOG_SCOPE_F(4, "Convergence threshold reached & %d models eliminated of %d", numItemsPack, n_models);
for (int i = featuresUsed.size() - 1; i >= featuresUsed.size() - numItemsPack; --i) {
remove_last_model();
significanceModels[featuresUsed[i]] = 0.0;
}
// VLOG_SCOPE_F(4, "*Convergence threshold %d models left & %d features used.", n_models, featuresUsed.size());
} else {
notes.push_back("Convergence threshold reached & 0 models eliminated");
// VLOG_SCOPE_F(4, "Convergence threshold reached & 0 models eliminated n_models=%d numItemsPack=%d", n_models, numItemsPack);
}
}
if (featuresUsed.size() != features.size()) {
notes.push_back("Used features in train: " + std::to_string(featuresUsed.size()) + " of " + std::to_string(features.size()));
status = bayesnet::WARNING;
}
notes.push_back("Number of models: " + std::to_string(n_models));
return;
}
}

View File

@@ -0,0 +1,35 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#ifndef XBAODE_H
#define XBAODE_H
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits>
#include "common/Timer.hpp"
#include "ExpEnsemble.h"
namespace platform {
class XBAODE : public Boost {
// Hay que hacer un vector de modelos entrenados y hacer un predict ensemble con todos ellos
// Probar XA1DE con smooth original y laplace y comprobar diferencias si se pasan pesos a 1 o a 1/m
public:
XBAODE();
std::string getVersion() override { return version; };
protected:
void trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) override;
private:
void add_model(std::unique_ptr<XSpode> model);
void remove_last_model();
std::vector<std::vector<int>> X_train_, X_test_;
std::vector<int> y_train_, y_test_;
std::string version = "0.9.7";
};
}
#endif // XBAODE_H

View File

@@ -0,0 +1,436 @@
#ifndef XSPODE_H
#define XSPODE_H
#include <vector>
#include <map>
#include <stdexcept>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <limits>
#include <sstream>
#include <iostream>
#include <torch/torch.h>
#include <bayesnet/network/Smoothing.h>
#include <bayesnet/classifiers/Classifier.h>
#include "CountingSemaphore.hpp"
namespace platform {
class XSpode : public bayesnet::Classifier {
public:
// --------------------------------------
// Constructor
//
// Supply which feature index is the single super-parent (“spIndex”).
// --------------------------------------
explicit XSpode(int spIndex)
: superParent_{ spIndex },
nFeatures_{ 0 },
statesClass_{ 0 },
fitted_{ false },
alpha_{ 1.0 },
initializer_{ 1.0 },
semaphore_{ CountingSemaphore::getInstance() } : bayesnet::Classifier(bayesnet::Network())
{
}
// --------------------------------------
// fit
// --------------------------------------
//
// Trains the SPODE given data:
// X: X[f][n] is the f-th feature value for instance n
// y: y[n] is the class value for instance n
// states: a map or array that tells how many distinct states each feature and the class can take
//
// For example, states_.back() is the number of class states,
// and states_[f] is the number of distinct values for feature f.
//
// We only store conditional probabilities for:
// p(x_sp| c) (the super-parent feature)
// p(x_child| c, x_sp) for all child ≠ sp
//
// The “weights” can be a vector of per-instance weights; if not used, pass them as 1.0.
// --------------------------------------
void fit(const std::vector<std::vector<int>>& X,
const std::vector<int>& y,
const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing)
{
int numInstances = static_cast<int>(y.size());
nFeatures_ = static_cast<int>(X.size());
// Derive the number of states for each feature and for the class.
// (This is just one approach; adapt to match your environment.)
// Here, we assume the user also gave us the total #states per feature in e.g. statesMap.
// We'll simply reconstruct the integer states_ array. The last entry is statesClass_.
states_.resize(nFeatures_);
for (int f = 0; f < nFeatures_; f++) {
// Suppose you look up in “statesMap” by the feature name, or read directly from X.
// We'll assume states_[f] = max value in X[f] + 1.
auto maxIt = std::max_element(X[f].begin(), X[f].end());
states_[f] = (*maxIt) + 1;
}
// For the class: states_.back() = max(y)+1
statesClass_ = (*std::max_element(y.begin(), y.end())) + 1;
// Initialize counts
classCounts_.resize(statesClass_, 0.0);
// p(x_sp = spVal | c)
// We'll store these counts in spFeatureCounts_[spVal * statesClass_ + c].
spFeatureCounts_.resize(states_[superParent_] * statesClass_, 0.0);
// For each child ≠ sp, we store p(childVal| c, spVal) in a separate block of childCounts_.
// childCounts_ will be sized as sum_{child≠sp} (states_[child] * statesClass_ * states_[sp]).
// We also need an offset for each child to index into childCounts_.
childOffsets_.resize(nFeatures_, -1);
int totalSize = 0;
for (int f = 0; f < nFeatures_; f++) {
if (f == superParent_) continue; // skip sp
childOffsets_[f] = totalSize;
// block size for this child's counts: states_[f] * statesClass_ * states_[superParent_]
totalSize += (states_[f] * statesClass_ * states_[superParent_]);
}
childCounts_.resize(totalSize, 0.0);
// Accumulate raw counts
for (int n = 0; n < numInstances; n++) {
std::vector<int> instance(nFeatures_ + 1);
for (int f = 0; f < nFeatures_; f++) {
instance[f] = X[f][n];
}
instance[nFeatures_] = y[n];
addSample(instance, weights[n].item<double>());
}
switch (smoothing) {
case bayesnet::Smoothing_t::ORIGINAL:
alpha_ = 1.0 / numInstances;
break;
case bayesnet::Smoothing_t::LAPLACE:
alpha_ = 1.0;
break;
default:
alpha_ = 0.0; // No smoothing
}
initializer_ = initializer_ = std::numeric_limits<double>::max() / (nFeatures_ * nFeatures_);
// Convert raw counts to probabilities
computeProbabilities();
fitted_ = true;
}
// --------------------------------------
// addSample (only valid in COUNTS mode)
// --------------------------------------
//
// instance has size nFeatures_ + 1, with the class at the end.
// We add 1 to the appropriate counters for each (c, superParentVal, childVal).
//
void addSample(const std::vector<int>& instance, double weight)
{
if (weight <= 0.0) return;
int c = instance.back();
// (A) increment classCounts
classCounts_[c] += weight;
// (B) increment super-parent counts => p(x_sp | c)
int spVal = instance[superParent_];
spFeatureCounts_[spVal * statesClass_ + c] += weight;
// (C) increment child counts => p(childVal | c, x_sp)
for (int f = 0; f < nFeatures_; f++) {
if (f == superParent_) continue;
int childVal = instance[f];
int offset = childOffsets_[f];
// Compute index in childCounts_.
// Layout: [ offset + (spVal * states_[f] + childVal) * statesClass_ + c ]
int blockSize = states_[f] * statesClass_;
int idx = offset + spVal * blockSize + childVal * statesClass_ + c;
childCounts_[idx] += weight;
}
}
// --------------------------------------
// computeProbabilities
// --------------------------------------
//
// Once all samples are added in COUNTS mode, call this to:
// p(c)
// p(x_sp = spVal | c)
// p(x_child = v | c, x_sp = s_sp)
//
// We store them in the corresponding *Probs_ arrays for inference.
// --------------------------------------
void computeProbabilities()
{
double totalCount = std::accumulate(classCounts_.begin(), classCounts_.end(), 0.0);
// p(c) => classPriors_
classPriors_.resize(statesClass_, 0.0);
if (totalCount <= 0.0) {
// fallback => uniform
double unif = 1.0 / static_cast<double>(statesClass_);
for (int c = 0; c < statesClass_; c++) {
classPriors_[c] = unif;
}
} else {
for (int c = 0; c < statesClass_; c++) {
classPriors_[c] = (classCounts_[c] + alpha_)
/ (totalCount + alpha_ * statesClass_);
}
}
// p(x_sp | c)
spFeatureProbs_.resize(spFeatureCounts_.size());
// denominator for spVal * statesClass_ + c is just classCounts_[c] + alpha_ * (#states of sp)
int spCard = states_[superParent_];
for (int spVal = 0; spVal < spCard; spVal++) {
for (int c = 0; c < statesClass_; c++) {
double denom = classCounts_[c] + alpha_ * spCard;
double num = spFeatureCounts_[spVal * statesClass_ + c] + alpha_;
spFeatureProbs_[spVal * statesClass_ + c] = (denom <= 0.0 ? 0.0 : num / denom);
}
}
// p(x_child | c, x_sp)
childProbs_.resize(childCounts_.size());
for (int f = 0; f < nFeatures_; f++) {
if (f == superParent_) continue;
int offset = childOffsets_[f];
int childCard = states_[f];
// For each spVal, c, childVal in childCounts_:
for (int spVal = 0; spVal < spCard; spVal++) {
for (int childVal = 0; childVal < childCard; childVal++) {
for (int c = 0; c < statesClass_; c++) {
int idx = offset + spVal * (childCard * statesClass_)
+ childVal * statesClass_
+ c;
double num = childCounts_[idx] + alpha_;
// denominator = spFeatureCounts_[spVal * statesClass_ + c] + alpha_ * (#states of child)
double denom = spFeatureCounts_[spVal * statesClass_ + c]
+ alpha_ * childCard;
childProbs_[idx] = (denom <= 0.0 ? 0.0 : num / denom);
}
}
}
}
}
// --------------------------------------
// predict_proba
// --------------------------------------
//
// For a single instance x of dimension nFeatures_:
// P(c | x) ∝ p(c) × p(x_sp | c) × ∏(child ≠ sp) p(x_child | c, x_sp).
//
// Then we normalize the result.
// --------------------------------------
std::vector<double> predict_proba(const std::vector<int>& instance) const
{
std::vector<double> probs(statesClass_, 0.0);
// Multiply p(c) × p(x_sp | c)
int spVal = instance[superParent_];
for (int c = 0; c < statesClass_; c++) {
double pc = classPriors_[c];
double pSpC = spFeatureProbs_[spVal * statesClass_ + c];
probs[c] = pc * pSpC * initializer_;
}
// Multiply by each childs probability p(x_child | c, x_sp)
for (int feature = 0; feature < nFeatures_; feature++) {
if (feature == superParent_) continue; // skip sp
int sf = instance[feature];
int offset = childOffsets_[feature];
int childCard = states_[feature]; // not used directly, but for clarity
// Index into childProbs_ = offset + spVal*(childCard*statesClass_) + childVal*statesClass_ + c
int base = offset + spVal * (childCard * statesClass_) + sf * statesClass_;
for (int c = 0; c < statesClass_; c++) {
probs[c] *= childProbs_[base + c];
}
}
// Normalize
normalize(probs);
return probs;
}
std::vector<std::vector<double>> predict_proba(const std::vector<std::vector<int>>& test_data)
{
int test_size = test_data[0].size();
int sample_size = test_data.size();
auto probabilities = std::vector<std::vector<double>>(test_size, std::vector<double>(statesClass_));
int chunk_size = std::min(150, int(test_size / semaphore_.getMaxCount()) + 1);
std::vector<std::thread> threads;
auto worker = [&](const std::vector<std::vector<int>>& samples, int begin, int chunk, int sample_size, std::vector<std::vector<double>>& predictions) {
std::string threadName = "(V)PWorker-" + std::to_string(begin) + "-" + std::to_string(chunk);
#if defined(__linux__)
pthread_setname_np(pthread_self(), threadName.c_str());
#else
pthread_setname_np(threadName.c_str());
#endif
std::vector<int> instance(sample_size);
for (int sample = begin; sample < begin + chunk; ++sample) {
for (int feature = 0; feature < sample_size; ++feature) {
instance[feature] = samples[feature][sample];
}
predictions[sample] = predict_proba(instance);
}
semaphore_.release();
};
for (int begin = 0; begin < test_size; begin += chunk_size) {
int chunk = std::min(chunk_size, test_size - begin);
semaphore_.acquire();
threads.emplace_back(worker, test_data, begin, chunk, sample_size, std::ref(probabilities));
}
for (auto& thread : threads) {
thread.join();
}
return probabilities;
}
// --------------------------------------
// predict
// --------------------------------------
//
// Return the class argmax( P(c|x) ).
// --------------------------------------
int predict(const std::vector<int>& instance) const
{
auto p = predict_proba(instance);
return static_cast<int>(std::distance(p.begin(),
std::max_element(p.begin(), p.end())));
}
std::vector<int> predict(std::vector<std::vector<int>>& test_data)
{
if (!fitted_) {
throw std::logic_error(CLASSIFIER_NOT_FITTED);
}
auto probabilities = predict_proba(test_data);
std::vector<int> predictions(probabilities.size(), 0);
for (size_t i = 0; i < probabilities.size(); i++) {
predictions[i] = std::distance(probabilities[i].begin(), std::max_element(probabilities[i].begin(), probabilities[i].end()));
}
return predictions;
}
// --------------------------------------
// Utility: normalize
// --------------------------------------
void normalize(std::vector<double>& v) const
{
double sum = 0.0;
for (auto val : v) { sum += val; }
if (sum <= 0.0) {
return;
}
for (auto& val : v) {
val /= sum;
}
}
// --------------------------------------
// debug printing, if desired
// --------------------------------------
std::string to_string() const
{
std::ostringstream oss;
oss << "---- SPODE Model ----\n"
<< "nFeatures_ = " << nFeatures_ << "\n"
<< "superParent_ = " << superParent_ << "\n"
<< "statesClass_ = " << statesClass_ << "\n"
<< "\n";
oss << "States: [";
for (int s : states_) oss << s << " ";
oss << "]\n";
oss << "classCounts_: [";
for (double c : classCounts_) oss << c << " ";
oss << "]\n";
oss << "classPriors_: [";
for (double c : classPriors_) oss << c << " ";
oss << "]\n";
oss << "spFeatureCounts_: size = " << spFeatureCounts_.size() << "\n[";
for (double c : spFeatureCounts_) oss << c << " ";
oss << "]\n";
oss << "spFeatureProbs_: size = " << spFeatureProbs_.size() << "\n[";
for (double c : spFeatureProbs_) oss << c << " ";
oss << "]\n";
oss << "childCounts_: size = " << childCounts_.size() << "\n[";
for (double cc : childCounts_) oss << cc << " ";
oss << "]\n";
oss << "childProbs_: size = " << childProbs_.size() << "\n[";
for (double cp : childProbs_) oss << cp << " ";
oss << "]\n";
oss << "childOffsets_: [";
for (int co : childOffsets_) oss << co << " ";
oss << "]\n";
oss << "---------------------\n";
return oss.str();
}
int statesClass() const { return statesClass_; }
int getNFeatures() const { return nFeatures_; }
int getNumberOfStates() const
{
return std::accumulate(states_.begin(), states_.end(), 0) * nFeatures_;
}
int getNumberOfEdges() const
{
return nFeatures_ * (2 * nFeatures_ - 1);
}
std::vector<int>& getStates() { return states_; }
private:
// --------------------------------------
// MEMBERS
// --------------------------------------
int superParent_; // which feature is the single super-parent
int nFeatures_;
int statesClass_;
bool fitted_ = false;
std::vector<int> states_; // [states_feat0, ..., states_feat(N-1)] (class not included in this array)
const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
// Class counts
std::vector<double> classCounts_; // [c], accumulative
std::vector<double> classPriors_; // [c], after normalization
// For p(x_sp = spVal | c)
std::vector<double> spFeatureCounts_; // [spVal * statesClass_ + c]
std::vector<double> spFeatureProbs_; // same shape, after normalization
// For p(x_child = childVal | x_sp = spVal, c)
// childCounts_ is big enough to hold all child features except sp:
// For each child f, we store childOffsets_[f] as the start index, then
// childVal, spVal, c => the data.
std::vector<double> childCounts_;
std::vector<double> childProbs_;
std::vector<int> childOffsets_;
double alpha_ = 1.0;
double initializer_; // for numerical stability
CountingSemaphore& semaphore_;
};
} // namespace platform
#endif // XSPODE_H

View File

@@ -0,0 +1,478 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2025 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
// Based on the Geoff. I. Webb A1DE java algorithm
// https://weka.sourceforge.io/packageMetaData/AnDE/Latest.html
#ifndef XAODE_H
#define XAODE_H
#include <vector>
#include <map>
#include <stdexcept>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <limits>
#include <sstream>
#include <torch/torch.h>
#include <bayesnet/network/Smoothing.h>
namespace platform {
class Xaode {
public:
// -------------------------------------------------------
// The Xaode can be EMPTY (just created), in COUNTS mode (accumulating raw counts)
// or PROBS mode (storing conditional probabilities).
enum class MatrixState {
EMPTY,
COUNTS,
PROBS
};
std::vector<double> significance_models_;
Xaode() : nFeatures_{ 0 }, statesClass_{ 0 }, matrixState_{ MatrixState::EMPTY } {}
// -------------------------------------------------------
// fit
// -------------------------------------------------------
//
// Classifiers interface
// all parameter decide if the model is initialized with all the parents active or none of them
//
// states.size() = nFeatures + 1,
// where states.back() = number of class states.
//
// We'll store:
// 1) p(x_i=si | c) in classFeatureProbs_
// 2) p(x_j=sj | c, x_i=si) in data_, with i<j => i is "superparent," j is "child."
//
// Internally, in COUNTS mode, data_ accumulates raw counts, then
// computeProbabilities(...) normalizes them into conditionals.
void fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const bool all_parents, const bayesnet::Smoothing_t smoothing)
{
int num_instances = X[0].size();
nFeatures_ = X.size();
significance_models_.resize(nFeatures_, (all_parents ? 1.0 : 0.0));
for (int i = 0; i < nFeatures_; i++) {
if (all_parents) active_parents.push_back(i);
states_.push_back(*max_element(X[i].begin(), X[i].end()) + 1);
}
states_.push_back(*max_element(y.begin(), y.end()) + 1);
//
statesClass_ = states_.back();
classCounts_.resize(statesClass_, 0.0);
classPriors_.resize(statesClass_, 0.0);
//
// Initialize data structures
//
active_parents.resize(nFeatures_);
int totalStates = std::accumulate(states_.begin(), states_.end(), 0) - statesClass_;
// For p(x_i=si | c), we store them in a 1D array classFeatureProbs_ after we compute.
// We'll need the offsets for each feature i in featureClassOffset_.
featureClassOffset_.resize(nFeatures_);
// We'll store p(x_child=sj | c, x_sp=si) for each pair (i<j).
// So data_(i, si, j, sj, c) indexes into a big 1D array with an offset.
// For p(x_i=si | c), we store them in a 1D array classFeatureProbs_ after we compute.
// We'll need the offsets for each feature i in featureClassOffset_.
featureClassOffset_.resize(nFeatures_);
pairOffset_.resize(totalStates);
int feature_offset = 0;
int runningOffset = 0;
int feature = 0, index = 0;
for (int i = 0; i < nFeatures_; ++i) {
featureClassOffset_[i] = feature_offset;
feature_offset += states_[i];
for (int j = 0; j < states_[i]; ++j) {
pairOffset_[feature++] = index;
index += runningOffset;
}
runningOffset += states_[i];
}
int totalSize = index * statesClass_;
data_.resize(totalSize);
dataOpp_.resize(totalSize);
classFeatureCounts_.resize(feature_offset * statesClass_);
classFeatureProbs_.resize(feature_offset * statesClass_);
matrixState_ = MatrixState::COUNTS;
//
// Add samples
//
std::vector<int> instance(nFeatures_ + 1);
for (int n_instance = 0; n_instance < num_instances; n_instance++) {
for (int feature = 0; feature < nFeatures_; feature++) {
instance[feature] = X[feature][n_instance];
}
instance[nFeatures_] = y[n_instance];
addSample(instance, weights[n_instance].item<double>());
}
switch (smoothing) {
case bayesnet::Smoothing_t::ORIGINAL:
alpha_ = 1.0 / num_instances;
break;
case bayesnet::Smoothing_t::LAPLACE:
alpha_ = 1.0;
break;
default:
alpha_ = 0.0; // No smoothing
}
initializer_ = std::numeric_limits<double>::max() / (nFeatures_ * nFeatures_);
computeProbabilities();
}
std::string to_string() const
{
std::ostringstream ostream;
ostream << "-------- Xaode.status --------" << std::endl
<< "- nFeatures = " << nFeatures_ << std::endl
<< "- statesClass = " << statesClass_ << std::endl
<< "- matrixState = " << (matrixState_ == MatrixState::COUNTS ? "COUNTS" : "PROBS") << std::endl;
ostream << "- states: size: " << states_.size() << std::endl;
for (int s : states_) ostream << s << " "; ostream << std::endl;
ostream << "- classCounts: size: " << classCounts_.size() << std::endl;
for (double cc : classCounts_) ostream << cc << " "; ostream << std::endl;
ostream << "- classPriors: size: " << classPriors_.size() << std::endl;
for (double cp : classPriors_) ostream << cp << " "; ostream << std::endl;
ostream << "- classFeatureCounts: size: " << classFeatureCounts_.size() << std::endl;
for (double cfc : classFeatureCounts_) ostream << cfc << " "; ostream << std::endl;
ostream << "- classFeatureProbs: size: " << classFeatureProbs_.size() << std::endl;
for (double cfp : classFeatureProbs_) ostream << cfp << " "; ostream << std::endl;
ostream << "- featureClassOffset: size: " << featureClassOffset_.size() << std::endl;
for (int f : featureClassOffset_) ostream << f << " "; ostream << std::endl;
ostream << "- pairOffset_: size: " << pairOffset_.size() << std::endl;
for (int p : pairOffset_) ostream << p << " "; ostream << std::endl;
ostream << "- data: size: " << data_.size() << std::endl;
for (double d : data_) ostream << d << " "; ostream << std::endl;
ostream << "- dataOpp: size: " << dataOpp_.size() << std::endl;
for (double d : dataOpp_) ostream << d << " "; ostream << std::endl;
ostream << "--------------------------------" << std::endl;
std::string output = ostream.str();
return output;
}
// -------------------------------------------------------
// addSample (only in COUNTS mode)
// -------------------------------------------------------
//
// instance should have the class at the end.
//
void addSample(const std::vector<int>& instance, double weight)
{
//
// (A) increment classCounts_
// (B) increment featureclass counts => for p(x_i|c)
// (C) increment pair (superparent= i, child= j) counts => data_
//
int c = instance.back();
if (weight <= 0.0) {
return;
}
// (A) increment classCounts_
classCounts_[c] += weight;
// (B,C)
// We'll store raw counts now and turn them into p(child| c, superparent) later.
int idx, fcIndex, sp, sc, i_offset;
for (int parent = 0; parent < nFeatures_; ++parent) {
sp = instance[parent];
// (B) increment featureclass counts => for p(x_i|c)
fcIndex = (featureClassOffset_[parent] + sp) * statesClass_ + c;
classFeatureCounts_[fcIndex] += weight;
// (C) increment pair (superparent= i, child= j) counts => data_
i_offset = pairOffset_[featureClassOffset_[parent] + sp];
for (int child = 0; child < parent; ++child) {
sc = instance[child];
idx = (i_offset + featureClassOffset_[child] + sc) * statesClass_ + c;
data_[idx] += weight;
}
}
}
// -------------------------------------------------------
// computeProbabilities
// -------------------------------------------------------
//
// Once all samples are added in COUNTS mode, call this to:
// 1) compute p(c) => classPriors_
// 2) compute p(x_i=si | c) => classFeatureProbs_
// 3) compute p(x_j=sj | c, x_i=si) => data_ (for i<j) dataOpp_ (for i>j)
//
void computeProbabilities()
{
if (matrixState_ != MatrixState::COUNTS) {
throw std::logic_error("computeProbabilities: must be in COUNTS mode.");
}
double totalCount = std::accumulate(classCounts_.begin(), classCounts_.end(), 0.0);
// (1) p(c)
if (totalCount <= 0.0) {
// fallback => uniform
double unif = 1.0 / statesClass_;
for (int c = 0; c < statesClass_; ++c) {
classPriors_[c] = unif;
}
} else {
for (int c = 0; c < statesClass_; ++c) {
classPriors_[c] = (classCounts_[c] + alpha_) / (totalCount + alpha_ * statesClass_);
}
}
// (2) p(x_i=si | c) => classFeatureProbs_
int idx, sf;
double denom;
for (int feature = 0; feature < nFeatures_; ++feature) {
sf = states_[feature];
for (int c = 0; c < statesClass_; ++c) {
denom = classCounts_[c] + alpha_ * sf;
for (int sf_value = 0; sf_value < sf; ++sf_value) {
idx = (featureClassOffset_[feature] + sf_value) * statesClass_ + c;
classFeatureProbs_[idx] = (classFeatureCounts_[idx] + alpha_) / denom;
}
}
}
// getCountFromTable(int classVal, int pIndex, int childIndex)
// (3) p(x_c=sc | c, x_p=sp) => data_(parent,sp,child,sc,c)
// (3) p(x_p=sp | c, x_c=sc) => dataOpp_(child,sc,parent,sp,c)
// C(x_c, x_p, c) + alpha_
// P(x_p | x_c, c) = -----------------------------------
// C(x_c, c) + alpha_
double pcc_count, pc_count, cc_count;
double conditionalProb, oppositeCondProb;
int part1, part2, p1, part2_class, p1_class;
for (int parent = 1; parent < nFeatures_; ++parent) {
for (int sp = 0; sp < states_[parent]; ++sp) {
p1 = featureClassOffset_[parent] + sp;
part1 = pairOffset_[p1];
p1_class = p1 * statesClass_;
for (int child = 0; child < parent; ++child) {
for (int sc = 0; sc < states_[child]; ++sc) {
part2 = featureClassOffset_[child] + sc;
part2_class = part2 * statesClass_;
for (int c = 0; c < statesClass_; c++) {
idx = (part1 + part2) * statesClass_ + c;
// Parent, Child, Class Count
pcc_count = data_[idx];
// Parent, Class count
pc_count = classFeatureCounts_[p1_class + c];
// Child, Class count
cc_count = classFeatureCounts_[part2_class + c];
// p(x_c=sc | c, x_p=sp)
conditionalProb = (pcc_count + alpha_) / (pc_count + alpha_ * states_[child]);
data_[idx] = conditionalProb;
// p(x_p=sp | c, x_c=sc)
oppositeCondProb = (pcc_count + alpha_) / (cc_count + alpha_ * states_[parent]);
dataOpp_[idx] = oppositeCondProb;
}
}
}
}
}
matrixState_ = MatrixState::PROBS;
}
// -------------------------------------------------------
// predict_proba_spode
// -------------------------------------------------------
//
// Single-superparent approach:
// P(c | x) ∝ p(c) * p(x_sp| c) * ∏_{i≠sp} p(x_i | c, x_sp)
//
// 'instance' should have size == nFeatures_ (no class).
// sp in [0..nFeatures_).
// We multiply p(c) * p(x_sp| c) * p(x_i| c, x_sp).
// Then normalize the distribution.
//
std::vector<double> predict_proba_spode(const std::vector<int>& instance, int parent)
{
// accumulates posterior probabilities for each class
auto probs = std::vector<double>(statesClass_);
auto spodeProbs = std::vector<double>(statesClass_, 0.0);
if (std::find(active_parents.begin(), active_parents.end(), parent) == active_parents.end()) {
return spodeProbs;
}
// Initialize the probabilities with the feature|class probabilities x class priors
int localOffset;
int sp = instance[parent];
localOffset = (featureClassOffset_[parent] + sp) * statesClass_;
for (int c = 0; c < statesClass_; ++c) {
spodeProbs[c] = classFeatureProbs_[localOffset + c] * classPriors_[c] * initializer_;
}
int idx, base, sc, parent_offset;
for (int child = 0; child < nFeatures_; ++child) {
if (child == parent) {
continue;
}
sc = instance[child];
if (child > parent) {
parent_offset = pairOffset_[featureClassOffset_[child] + sc];
base = (parent_offset + featureClassOffset_[parent] + sp) * statesClass_;
} else {
parent_offset = pairOffset_[featureClassOffset_[parent] + sp];
base = (parent_offset + featureClassOffset_[child] + sc) * statesClass_;
}
for (int c = 0; c < statesClass_; ++c) {
/*
* The probability P(xc|xp,c) is stored in dataOpp_, and
* the probability P(xp|xc,c) is stored in data_
*/
idx = base + c;
double factor = child > parent ? dataOpp_[idx] : data_[idx];
// double factor = data_[idx];
spodeProbs[c] *= factor;
}
}
// Normalize the probabilities
normalize(spodeProbs);
return spodeProbs;
}
int predict_spode(const std::vector<int>& instance, int parent)
{
auto probs = predict_proba_spode(instance, parent);
return (int)std::distance(probs.begin(), std::max_element(probs.begin(), probs.end()));
}
// -------------------------------------------------------
// predict_proba
// -------------------------------------------------------
//
// P(c | x) ∝ p(c) * ∏_{i} p(x_i | c) * ∏_{i<j} p(x_j | c, x_i) * p(x_i | c, x_j)
//
// 'instance' should have size == nFeatures_ (no class).
// We multiply p(c) * p(x_i| c) * p(x_j| c, x_i) for all i, j.
// Then normalize the distribution.
//
std::vector<double> predict_proba(const std::vector<int>& instance)
{
// accumulates posterior probabilities for each class
auto probs = std::vector<double>(statesClass_);
auto spodeProbs = std::vector<std::vector<double>>(nFeatures_, std::vector<double>(statesClass_));
// Initialize the probabilities with the feature|class probabilities
int localOffset;
for (int feature = 0; feature < nFeatures_; ++feature) {
// if feature is not in the active_parents, skip it
if (std::find(active_parents.begin(), active_parents.end(), feature) == active_parents.end()) {
continue;
}
localOffset = (featureClassOffset_[feature] + instance[feature]) * statesClass_;
for (int c = 0; c < statesClass_; ++c) {
spodeProbs[feature][c] = classFeatureProbs_[localOffset + c] * classPriors_[c] * initializer_;
}
}
int idx, base, sp, sc, parent_offset;
for (int parent = 1; parent < nFeatures_; ++parent) {
// if parent is not in the active_parents, skip it
if (std::find(active_parents.begin(), active_parents.end(), parent) == active_parents.end()) {
continue;
}
sp = instance[parent];
parent_offset = pairOffset_[featureClassOffset_[parent] + sp];
for (int child = 0; child < parent; ++child) {
sc = instance[child];
if (child > parent) {
parent_offset = pairOffset_[featureClassOffset_[child] + sc];
base = (parent_offset + featureClassOffset_[parent] + sp) * statesClass_;
} else {
parent_offset = pairOffset_[featureClassOffset_[parent] + sp];
base = (parent_offset + featureClassOffset_[child] + sc) * statesClass_;
}
for (int c = 0; c < statesClass_; ++c) {
/*
* The probability P(xc|xp,c) is stored in dataOpp_, and
* the probability P(xp|xc,c) is stored in data_
*/
idx = base + c;
double factor_child = child > parent ? data_[idx] : dataOpp_[idx];
double factor_parent = child > parent ? dataOpp_[idx] : data_[idx];
spodeProbs[child][c] *= factor_child;
spodeProbs[parent][c] *= factor_parent;
}
}
}
/* add all the probabilities for each class */
for (int c = 0; c < statesClass_; ++c) {
for (int i = 0; i < nFeatures_; ++i) {
probs[c] += spodeProbs[i][c] * significance_models_[i];
}
}
// Normalize the probabilities
normalize(probs);
return probs;
}
void normalize(std::vector<double>& probs) const
{
double sum = std::accumulate(probs.begin(), probs.end(), 0.0);
if (std::isnan(sum)) {
throw std::runtime_error("Can't normalize array. Sum is NaN.");
}
if (sum == 0) {
return;
}
for (int i = 0; i < (int)probs.size(); i++) {
probs[i] /= sum;
}
}
// Returns current mode: INIT, COUNTS or PROBS
MatrixState state() const
{
return matrixState_;
}
int statesClass() const
{
return statesClass_;
}
int nFeatures() const
{
return nFeatures_;
}
int getNumberOfStates() const
{
return std::accumulate(states_.begin(), states_.end(), 0) * nFeatures_;
}
int getNumberOfEdges() const
{
return nFeatures_ * (2 * nFeatures_ - 1);
}
int getNumberOfNodes() const
{
return (nFeatures_ + 1) * nFeatures_;
}
void add_active_parent(int active_parent)
{
active_parents.push_back(active_parent);
}
void remove_last_parent()
{
active_parents.pop_back();
}
private:
// -----------
// MEMBER DATA
// -----------
std::vector<int> states_; // [states_feat0, ..., states_feat(n-1), statesClass_]
int nFeatures_;
int statesClass_;
// data_ means p(child=sj | c, superparent= si) after normalization.
// But in COUNTS mode, it accumulates raw counts.
std::vector<int> pairOffset_;
// data_ stores p(child=sj | c, superparent=si) for each pair (i<j).
std::vector<double> data_;
// dataOpp_ stores p(superparent=si | c, child=sj) for each pair (i<j).
std::vector<double> dataOpp_;
// classCounts_[c]
std::vector<double> classCounts_;
std::vector<double> classPriors_; // => p(c)
// For p(x_i=si| c), we store counts in classFeatureCounts_ => offset by featureClassOffset_[i]
std::vector<int> featureClassOffset_;
std::vector<double> classFeatureCounts_;
std::vector<double> classFeatureProbs_; // => p(x_i=si | c) after normalization
MatrixState matrixState_;
double alpha_ = 1.0; // Laplace smoothing
double initializer_ = 1.0;
std::vector<int> active_parents;
};
}
#endif // XAODE_H

314
src/grid/GridBase.cpp Normal file
View File

@@ -0,0 +1,314 @@
#include <random>
#include <cstddef>
#include "common/DotEnv.h"
#include "common/Paths.h"
#include "common/Colors.h"
#include "GridBase.h"
namespace platform {
GridBase::GridBase(struct ConfigGrid& config)
{
this->config = config;
auto env = platform::DotEnv();
this->config.platform = env.get("platform");
}
void GridBase::validate_config()
{
if (config.smooth_strategy == "ORIGINAL")
smooth_type = bayesnet::Smoothing_t::ORIGINAL;
else if (config.smooth_strategy == "LAPLACE")
smooth_type = bayesnet::Smoothing_t::LAPLACE;
else if (config.smooth_strategy == "CESTNIK")
smooth_type = bayesnet::Smoothing_t::CESTNIK;
else {
std::cerr << "GridBase: Unknown smoothing strategy: " << config.smooth_strategy << std::endl;
exit(1);
}
}
std::string GridBase::get_color_rank(int rank)
{
auto colors = { Colors::WHITE(), Colors::RED(), Colors::GREEN(), Colors::BLUE(), Colors::MAGENTA(), Colors::CYAN(), Colors::YELLOW(), Colors::BLACK() };
std::string id = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
auto idx = rank % id.size();
return *(colors.begin() + rank % colors.size()) + id[idx];
}
void GridBase::shuffle_and_progress_bar(json& tasks)
{
// Shuffle the array so heavy datasets are eas ier spread across the workers
std::mt19937 g{ 271 }; // Use fixed seed to obtain the same shuffle
std::shuffle(tasks.begin(), tasks.end(), g);
std::cout << "* Number of tasks: " << tasks.size() << std::endl;
std::cout << separator << std::flush;
for (int i = 0; i < tasks.size(); ++i) {
if ((i + 1) % 10 == 0)
std::cout << separator;
else
std::cout << (i + 1) % 10;
}
std::cout << separator << std::endl << separator << std::flush;
}
json GridBase::build_tasks(Datasets& datasets)
{
/*
* Each task is a json object with the following structure:
* {
* "dataset": "dataset_name",
* "idx_dataset": idx_dataset, // used to identify the dataset in the results
* // this index is relative to the list of used datasets in the actual run not to the whole datasets list
* "seed": # of seed to use,
* "fold": # of fold to process
* }
* This way a task consists in process all combinations of hyperparameters for a dataset, seed and fold
*/
auto tasks = json::array();
auto all_datasets = datasets.getNames();
auto datasets_names = filterDatasets(datasets);
for (int idx_dataset = 0; idx_dataset < datasets_names.size(); ++idx_dataset) {
auto dataset = datasets_names[idx_dataset];
for (const auto& seed : config.seeds) {
for (int n_fold = 0; n_fold < config.n_folds; n_fold++) {
json task = {
{ "dataset", dataset },
{ "idx_dataset", idx_dataset},
{ "seed", seed },
{ "fold", n_fold},
};
tasks.push_back(task);
}
}
}
shuffle_and_progress_bar(tasks);
return tasks;
}
void GridBase::summary(json& all_results, json& tasks, struct ConfigMPI& config_mpi)
{
// Report the tasks done by each worker, showing dataset number, seed, fold and time spent
// The format I want to show is:
// worker, dataset, seed, fold, time
// with headers
std::cout << Colors::RESET() << "* Summary of tasks done by each worker" << std::endl;
json worker_tasks = json::array();
for (int i = 0; i < config_mpi.n_procs; ++i) {
worker_tasks.push_back(json::array());
}
int max_dataset = 7;
for (const auto& [key, results] : all_results.items()) {
auto dataset = key;
if (dataset.size() > max_dataset)
max_dataset = dataset.size();
for (const auto& result : results) {
int n_task = result["task"].get<int>();
json task = tasks[n_task];
auto seed = task["seed"].get<int>();
auto fold = task["fold"].get<int>();
auto time = result["time"].get<double>();
auto worker = result["process"].get<int>();
json line = {
{ "dataset", dataset },
{ "seed", seed },
{ "fold", fold },
{ "time", time }
};
worker_tasks[worker].push_back(line);
}
}
std::cout << Colors::MAGENTA() << " W " << setw(max_dataset) << std::left << "Dataset";
std::cout << " Seed Fold Time" << std::endl;
std::cout << "=== " << std::string(max_dataset, '=') << " ==== ==== " << std::string(15, '=') << std::endl;
for (int worker = 0; worker < config_mpi.n_procs; ++worker) {
auto color = (worker % 2) ? Colors::CYAN() : Colors::BLUE();
std::cout << color << std::right << setw(3) << worker << " ";
if (worker == config_mpi.manager) {
std::cout << "Manager" << std::endl;
continue;
}
if (worker_tasks[worker].empty()) {
std::cout << "No tasks" << std::endl;
continue;
}
bool first = true;
double total = 0.0;
int num_tasks = 0;
for (const auto& task : worker_tasks[worker]) {
num_tasks++;
if (!first)
std::cout << std::string(4, ' ');
else
first = false;
std::cout << std::left << setw(max_dataset) << task["dataset"].get<std::string>();
std::cout << " " << setw(4) << std::right << task["seed"].get<int>();
std::cout << " " << setw(4) << task["fold"].get<int>();
std::cout << " " << setw(15) << std::setprecision(7) << std::fixed << task["time"].get<double>() << std::endl;
total += task["time"].get<double>();
}
if (num_tasks > 1) {
std::cout << Colors::MAGENTA() << " ";
std::cout << setw(max_dataset) << "Total (" << setw(2) << std::right << num_tasks << ")" << std::string(7, '.');
std::cout << " " << setw(15) << std::setprecision(7) << std::fixed << total << std::endl;
}
}
}
void GridBase::go(struct ConfigMPI& config_mpi)
{
/*
* Each task is a json object with the data needed by the process
*
* The overall process consists in these steps:
* 0. Validate config, create the MPI result type & tasks
* 0.1 Create the MPI result type
* 0.2 Manager creates the tasks
* 1. Manager will broadcast the tasks to all the processes
* 1.1 Broadcast the number of tasks
* 1.2 Broadcast the length of the following string
* 1.2 Broadcast the tasks as a char* string
* 2a. Producer delivers the tasks to the consumers
* 2a.1 Producer will loop to send all the tasks to the consumers and receive the results
* 2a.2 Producer will send the end message to all the consumers
* 2b. Consumers process the tasks and send the results to the producer
* 2b.1 Consumers announce to the producer that they are ready to receive a task
* 2b.2 Consumers receive the task from the producer and process it
* 2b.3 Consumers send the result to the producer
* 3. Manager compile results for each dataset
* 3.1 Loop thru all the results obtained from each outer fold (task) and select the best
* 3.2 Save the results
* 3.3 Summary of jobs done
*/
//
// 0.1 Create the MPI result type
//
validate_config();
Task_Result result;
int tasks_size;
MPI_Datatype MPI_Result;
MPI_Datatype type[11] = { MPI_UNSIGNED, MPI_UNSIGNED, MPI_INT, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_INT, MPI_INT };
int blocklen[11] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
MPI_Aint disp[11];
disp[0] = offsetof(Task_Result, idx_dataset);
disp[1] = offsetof(Task_Result, idx_combination);
disp[2] = offsetof(Task_Result, n_fold);
disp[3] = offsetof(Task_Result, score);
disp[4] = offsetof(Task_Result, time);
disp[5] = offsetof(Task_Result, time_train);
disp[6] = offsetof(Task_Result, nodes);
disp[7] = offsetof(Task_Result, leaves);
disp[8] = offsetof(Task_Result, depth);
disp[9] = offsetof(Task_Result, process);
disp[10] = offsetof(Task_Result, task);
MPI_Type_create_struct(11, blocklen, disp, type, &MPI_Result);
MPI_Type_commit(&MPI_Result);
//
// 0.2 Manager creates the tasks
//
char* msg;
json tasks;
auto env = platform::DotEnv();
auto datasets = Datasets(config.discretize, Paths::datasets(), env.get("discretize_algo"));
if (config_mpi.rank == config_mpi.manager) {
timer.start();
tasks = build_tasks(datasets);
auto tasks_str = tasks.dump();
tasks_size = tasks_str.size();
msg = new char[tasks_size + 1];
strcpy(msg, tasks_str.c_str());
}
//
// 1. Manager will broadcast the tasks to all the processes
//
MPI_Bcast(&tasks_size, 1, MPI_INT, config_mpi.manager, MPI_COMM_WORLD);
if (config_mpi.rank != config_mpi.manager) {
msg = new char[tasks_size + 1];
}
MPI_Bcast(msg, tasks_size + 1, MPI_CHAR, config_mpi.manager, MPI_COMM_WORLD);
tasks = json::parse(msg);
delete[] msg;
if (config_mpi.rank == config_mpi.manager) {
//
// 2a. Producer delivers the tasks to the consumers
//
auto datasets_names = filterDatasets(datasets);
json all_results = producer(datasets_names, tasks, config_mpi, MPI_Result);
std::cout << separator << std::endl;
//
// 3. Manager compile results for each dataset
//
auto results = initializeResults();
compile_results(results, all_results, config.model);
//
// 3.2 Save the results
//
save(results);
//
// 3.3 Summary of jobs done
//
if (!config.quiet)
summary(all_results, tasks, config_mpi);
} else {
//
// 2b. Consumers process the tasks and send the results to the producer
//
consumer(datasets, tasks, config, config_mpi, MPI_Result);
}
}
json GridBase::producer(std::vector<std::string>& names, json& tasks, struct ConfigMPI& config_mpi, MPI_Datatype& MPI_Result)
{
Task_Result result;
json results;
int num_tasks = tasks.size();
//
// 2a.1 Producer will loop to send all the tasks to the consumers and receive the results
//
for (int i = 0; i < num_tasks; ++i) {
MPI_Status status;
MPI_Recv(&result, 1, MPI_Result, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == TAG_RESULT) {
//Store result
store_result(names, result, results);
}
MPI_Send(&i, 1, MPI_INT, status.MPI_SOURCE, TAG_TASK, MPI_COMM_WORLD);
}
//
// 2a.2 Producer will send the end message to all the consumers
//
for (int i = 0; i < config_mpi.n_procs - 1; ++i) {
MPI_Status status;
MPI_Recv(&result, 1, MPI_Result, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == TAG_RESULT) {
//Store result
store_result(names, result, results);
}
MPI_Send(&i, 1, MPI_INT, status.MPI_SOURCE, TAG_END, MPI_COMM_WORLD);
}
return results;
}
void GridBase::consumer(Datasets& datasets, json& tasks, struct ConfigGrid& config, struct ConfigMPI& config_mpi, MPI_Datatype& MPI_Result)
{
Task_Result result;
//
// 2b.1 Consumers announce to the producer that they are ready to receive a task
//
MPI_Send(&result, 1, MPI_Result, config_mpi.manager, TAG_QUERY, MPI_COMM_WORLD);
int task;
while (true) {
MPI_Status status;
//
// 2b.2 Consumers receive the task from the producer and process it
//
MPI_Recv(&task, 1, MPI_INT, config_mpi.manager, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == TAG_END) {
break;
}
consumer_go(config, config_mpi, tasks, task, datasets, &result);
//
// 2b.3 Consumers send the result to the producer
//
MPI_Send(&result, 1, MPI_Result, config_mpi.manager, TAG_RESULT, MPI_COMM_WORLD);
}
}
}

39
src/grid/GridBase.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef GRIDBASE_H
#define GRIDBASE_H
#include <string>
#include <mpi.h>
#include <nlohmann/json.hpp>
#include "common/Datasets.h"
#include "common/Timer.hpp"
#include "main/HyperParameters.h"
#include "GridConfig.h"
namespace platform {
using json = nlohmann::ordered_json;
class GridBase {
public:
explicit GridBase(struct ConfigGrid& config);
~GridBase() = default;
void go(struct ConfigMPI& config_mpi);
void validate_config();
protected:
json build_tasks(Datasets& datasets);
virtual void save(json& results) = 0;
virtual std::vector<std::string> filterDatasets(Datasets& datasets) const = 0;
virtual json initializeResults() = 0;
virtual void compile_results(json& results, json& all_results, std::string& model) = 0;
virtual json store_result(std::vector<std::string>& names, Task_Result& result, json& results) = 0;
virtual void consumer_go(struct ConfigGrid& config, struct ConfigMPI& config_mpi, json& tasks, int n_task, Datasets& datasets, Task_Result* result) = 0;
void shuffle_and_progress_bar(json& tasks);
json producer(std::vector<std::string>& names, json& tasks, struct ConfigMPI& config_mpi, MPI_Datatype& MPI_Result);
void consumer(Datasets& datasets, json& tasks, struct ConfigGrid& config, struct ConfigMPI& config_mpi, MPI_Datatype& MPI_Result);
std::string get_color_rank(int rank);
void summary(json& all_results, json& tasks, struct ConfigMPI& config_mpi);
struct ConfigGrid config;
Timer timer; // used to measure the time of the whole process
const std::string separator = "|";
bayesnet::Smoothing_t smooth_type{ bayesnet::Smoothing_t::NONE };
};
} /* namespace platform */
#endif

55
src/grid/GridConfig.h Normal file
View File

@@ -0,0 +1,55 @@
#ifndef GRIDCONFIG_H
#define GRIDCONFIG_H
#include <string>
#include <map>
#include <mpi.h>
#include <nlohmann/json.hpp>
#include "common/Datasets.h"
#include "common/Timer.hpp"
#include "main/HyperParameters.h"
#include "GridData.h"
#include "GridConfig.h"
#include "bayesnet/network/Network.h"
namespace platform {
using json = nlohmann::ordered_json;
struct ConfigGrid {
std::string model;
std::string score;
std::string continue_from;
std::string platform;
std::string smooth_strategy;
bool quiet;
bool only; // used with continue_from to only compute that dataset
bool discretize;
bool stratified;
int nested;
int n_folds;
json excluded;
std::vector<int> seeds;
};
struct ConfigMPI {
int rank;
int n_procs;
int manager;
};
typedef struct {
uint idx_dataset;
uint idx_combination;
int n_fold;
double score; // Experiment: Score test, no score train in this case
double time; // Experiment: Time test
double time_train;
double nodes; // Experiment specific
double leaves; // Experiment specific
double depth; // Experiment specific
int process;
int task;
} Task_Result;
const int TAG_QUERY = 1;
const int TAG_RESULT = 2;
const int TAG_TASK = 3;
const int TAG_END = 4;
} /* namespace platform */
#endif

196
src/grid/GridExperiment.cpp Normal file
View File

@@ -0,0 +1,196 @@
#include <iostream>
#include <cstddef>
#include <torch/torch.h>
#include <folding.hpp>
#include "main/Models.h"
#include "common/Paths.h"
#include "common/Utils.h"
#include "GridExperiment.h"
namespace platform {
// GridExperiment::GridExperiment(argparse::ArgumentParser& program, struct ConfigGrid& config) : arguments(program), GridBase(config)
GridExperiment::GridExperiment(ArgumentsExperiment& program, struct ConfigGrid& config) : arguments(program), GridBase(config)
{
experiment = arguments.initializedExperiment();
filesToTest = arguments.getFilesToTest();
saveResults = arguments.haveToSaveResults();
this->config.model = experiment.getModel();
this->config.score = experiment.getScore();
this->config.discretize = experiment.isDiscretized();
this->config.stratified = experiment.isStratified();
this->config.smooth_strategy = experiment.getSmoothStrategy();
this->config.n_folds = experiment.getNFolds();
this->config.seeds = experiment.getRandomSeeds();
this->config.quiet = experiment.isQuiet();
}
json GridExperiment::getResults()
{
return computed_results;
}
std::vector<std::string> GridExperiment::filterDatasets(Datasets& datasets) const
{
return filesToTest;
}
json GridExperiment::initializeResults()
{
json results;
return results;
}
void GridExperiment::save(json& results)
{
}
void GridExperiment::compile_results(json& results, json& all_results, std::string& model)
{
auto datasets = Datasets(false, Paths::datasets());
nlohmann::json temp = all_results; // To restore the order of the data by dataset name
all_results = temp;
for (const auto& result_item : all_results.items()) {
// each result has the results of all the outer folds as each one were a different task
auto dataset_name = result_item.key();
auto data = result_item.value();
auto result = json::object();
int data_size = data.size();
auto score = torch::zeros({ data_size }, torch::kFloat64);
auto score_train = torch::zeros({ data_size }, torch::kFloat64);
auto time_test = torch::zeros({ data_size }, torch::kFloat64);
auto time_train = torch::zeros({ data_size }, torch::kFloat64);
auto nodes = torch::zeros({ data_size }, torch::kFloat64);
auto leaves = torch::zeros({ data_size }, torch::kFloat64);
auto depth = torch::zeros({ data_size }, torch::kFloat64);
auto& dataset = datasets.getDataset(dataset_name);
dataset.load();
//
// Prepare Result
//
auto partial_result = PartialResult();
partial_result.setSamples(dataset.getNSamples()).setFeatures(dataset.getNFeatures()).setClasses(dataset.getNClasses());
partial_result.setHyperparameters(experiment.getHyperParameters().get(dataset_name));
for (int fold = 0; fold < data_size; ++fold) {
partial_result.addScoreTest(data[fold]["score"]);
partial_result.addScoreTrain(0.0);
partial_result.addTimeTest(data[fold]["time"]);
partial_result.addTimeTrain(data[fold]["time_train"]);
score[fold] = data[fold]["score"].get<double>();
time_test[fold] = data[fold]["time"].get<double>();
time_train[fold] = data[fold]["time_train"].get<double>();
nodes[fold] = data[fold]["nodes"].get<double>();
leaves[fold] = data[fold]["leaves"].get<double>();
depth[fold] = data[fold]["depth"].get<double>();
}
partial_result.setGraph(std::vector<std::string>());
partial_result.setScoreTest(torch::mean(score).item<double>()).setScoreTrain(0.0);
partial_result.setScoreTestStd(torch::std(score).item<double>()).setScoreTrainStd(0.0);
partial_result.setTrainTime(torch::mean(time_train).item<double>()).setTestTime(torch::mean(time_test).item<double>());
partial_result.setTrainTimeStd(torch::std(time_train).item<double>()).setTestTimeStd(torch::std(time_test).item<double>());
partial_result.setNodes(torch::mean(nodes).item<double>()).setLeaves(torch::mean(leaves).item<double>()).setDepth(torch::mean(depth).item<double>());
partial_result.setDataset(dataset_name).setNotes(std::vector<std::string>());
partial_result.setConfusionMatrices(json::array());
experiment.addResult(partial_result);
}
auto clf = Models::instance()->create(experiment.getModel());
experiment.setModelVersion(clf->getVersion());
computed_results = results;
}
json GridExperiment::store_result(std::vector<std::string>& names, Task_Result& result, json& results)
{
json json_result = {
{ "score", result.score },
{ "combination", result.idx_combination },
{ "fold", result.n_fold },
{ "time", result.time },
{ "time_train", result.time_train },
{ "dataset", result.idx_dataset },
{ "nodes", result.nodes },
{ "leaves", result.leaves },
{ "depth", result.depth },
{ "process", result.process },
{ "task", result.task }
};
auto name = names[result.idx_dataset];
if (!results.contains(name)) {
results[name] = json::array();
}
results[name].push_back(json_result);
return results;
}
void GridExperiment::consumer_go(struct ConfigGrid& config, struct ConfigMPI& config_mpi, json& tasks, int n_task, Datasets& datasets, Task_Result* result)
{
//
// initialize
//
Timer train_timer, test_timer;
json task = tasks[n_task];
auto model = config.model;
auto dataset_name = task["dataset"].get<std::string>();
auto idx_dataset = task["idx_dataset"].get<int>();
auto seed = task["seed"].get<int>();
auto n_fold = task["fold"].get<int>();
bool stratified = config.stratified;
bayesnet::Smoothing_t smooth;
if (config.smooth_strategy == "ORIGINAL")
smooth = bayesnet::Smoothing_t::ORIGINAL;
else if (config.smooth_strategy == "LAPLACE")
smooth = bayesnet::Smoothing_t::LAPLACE;
else if (config.smooth_strategy == "CESTNIK")
smooth = bayesnet::Smoothing_t::CESTNIK;
//
// Generate the hyperparameters combinations
//
auto& dataset = datasets.getDataset(dataset_name);
dataset.load();
auto [X, y] = dataset.getTensors();
auto features = dataset.getFeatures();
auto className = dataset.getClassName();
//
// Start working on task
//
folding::Fold* fold;
if (stratified)
fold = new folding::StratifiedKFold(config.n_folds, y, seed);
else
fold = new folding::KFold(config.n_folds, y.size(0), seed);
train_timer.start();
auto [train, test] = fold->getFold(n_fold);
auto [X_train, X_test, y_train, y_test] = dataset.getTrainTestTensors(train, test);
auto states = dataset.getStates(); // Get the states of the features Once they are discretized
//
// Build Classifier with selected hyperparameters
//
auto clf = Models::instance()->create(config.model);
auto valid = clf->getValidHyperparameters();
auto hyperparameters = experiment.getHyperParameters();
hyperparameters.check(valid, dataset_name);
clf->setHyperparameters(hyperparameters.get(dataset_name));
//
// Train model
//
clf->fit(X_train, y_train, features, className, states, smooth);
auto train_time = train_timer.getDuration();
//
// Test model
//
test_timer.start();
double score = clf->score(X_test, y_test);
delete fold;
auto test_time = test_timer.getDuration();
//
// Return the result
//
result->idx_dataset = task["idx_dataset"].get<int>();
result->idx_combination = 0;
result->score = score;
result->n_fold = n_fold;
result->time = test_time;
result->time_train = train_time;
result->nodes = clf->getNumberOfNodes();
result->leaves = clf->getNumberOfEdges();
result->depth = clf->getNumberOfStates();
result->process = config_mpi.rank;
result->task = n_task;
//
// Update progress bar
//
std::cout << get_color_rank(config_mpi.rank) << std::flush;
}
} /* namespace platform */

38
src/grid/GridExperiment.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef GRIDEXPERIMENT_H
#define GRIDEXPERIMENT_H
#include <string>
#include <mpi.h>
#include <argparse/argparse.hpp>
#include <nlohmann/json.hpp>
#include "common/Datasets.h"
#include "main/Experiment.h"
#include "main/HyperParameters.h"
#include "main/ArgumentsExperiment.h"
#include "GridBase.h"
namespace platform {
using json = nlohmann::ordered_json;
class GridExperiment : public GridBase {
public:
explicit GridExperiment(ArgumentsExperiment& program, struct ConfigGrid& config);
~GridExperiment() = default;
json getResults();
Experiment& getExperiment() { return experiment; }
size_t numFiles() const { return filesToTest.size(); }
bool haveToSaveResults() const { return saveResults; }
private:
ArgumentsExperiment& arguments;
Experiment experiment;
json computed_results;
bool saveResults = false;
std::vector<std::string> filesToTest;
void save(json& results);
json initializeResults();
std::vector<std::string> filterDatasets(Datasets& datasets) const;
void compile_results(json& results, json& all_results, std::string& model);
json store_result(std::vector<std::string>& names, Task_Result& result, json& results);
void consumer_go(struct ConfigGrid& config, struct ConfigMPI& config_mpi, json& tasks, int n_task, Datasets& datasets, Task_Result* result);
};
} /* namespace platform */
#endif

View File

@@ -1,21 +1,14 @@
#include <iostream> #include <iostream>
#include <cstddef>
#include <torch/torch.h> #include <torch/torch.h>
#include <folding.hpp> #include <folding.hpp>
#include "main/Models.h" #include "main/Models.h"
#include "common/Paths.h" #include "common/Paths.h"
#include "common/Colors.h"
#include "common/Utils.h" #include "common/Utils.h"
#include "common/Colors.h"
#include "GridSearch.h" #include "GridSearch.h"
namespace platform { namespace platform {
GridSearch::GridSearch(struct ConfigGrid& config) : GridBase(config)
std::string get_color_rank(int rank)
{
auto colors = { Colors::WHITE(), Colors::RED(), Colors::GREEN(), Colors::BLUE(), Colors::MAGENTA(), Colors::CYAN() };
return *(colors.begin() + rank % colors.size());
}
GridSearch::GridSearch(struct ConfigGrid& config) : config(config)
{ {
} }
json GridSearch::loadResults() json GridSearch::loadResults()
@@ -59,333 +52,13 @@ namespace platform {
} }
return datasets_names; return datasets_names;
} }
json GridSearch::build_tasks_mpi(int rank)
{
auto tasks = json::array();
auto grid = GridData(Paths::grid_input(config.model));
auto datasets = Datasets(false, Paths::datasets());
auto all_datasets = datasets.getNames();
auto datasets_names = filterDatasets(datasets);
for (int idx_dataset = 0; idx_dataset < datasets_names.size(); ++idx_dataset) {
auto dataset = datasets_names[idx_dataset];
for (const auto& seed : config.seeds) {
auto combinations = grid.getGrid(dataset);
for (int n_fold = 0; n_fold < config.n_folds; n_fold++) {
json task = {
{ "dataset", dataset },
{ "idx_dataset", idx_dataset},
{ "seed", seed },
{ "fold", n_fold},
};
tasks.push_back(task);
}
}
}
// Shuffle the array so heavy datasets are spread across the workers
std::mt19937 g{ 271 }; // Use fixed seed to obtain the same shuffle
std::shuffle(tasks.begin(), tasks.end(), g);
std::cout << get_color_rank(rank) << "* Number of tasks: " << tasks.size() << std::endl;
std::cout << separator;
for (int i = 0; i < tasks.size(); ++i) {
std::cout << (i + 1) % 10;
}
std::cout << separator << std::endl << separator << std::flush;
return tasks;
}
void process_task_mpi_consumer(struct ConfigGrid& config, struct ConfigMPI& config_mpi, json& tasks, int n_task, Datasets& datasets, Task_Result* result)
{
// initialize
Timer timer;
timer.start();
json task = tasks[n_task];
auto model = config.model;
auto grid = GridData(Paths::grid_input(model));
auto dataset_name = task["dataset"].get<std::string>();
auto idx_dataset = task["idx_dataset"].get<int>();
auto seed = task["seed"].get<int>();
auto n_fold = task["fold"].get<int>();
bool stratified = config.stratified;
// Generate the hyperparamters combinations
auto& dataset = datasets.getDataset(dataset_name);
auto combinations = grid.getGrid(dataset_name);
dataset.load();
auto [X, y] = dataset.getTensors();
auto features = dataset.getFeatures();
auto className = dataset.getClassName();
//
// Start working on task
//
folding::Fold* fold;
if (stratified)
fold = new folding::StratifiedKFold(config.n_folds, y, seed);
else
fold = new folding::KFold(config.n_folds, y.size(0), seed);
auto [train, test] = fold->getFold(n_fold);
auto [X_train, X_test, y_train, y_test] = dataset.getTrainTestTensors(train, test);
auto states = dataset.getStates(); // Get the states of the features Once they are discretized
double best_fold_score = 0.0;
int best_idx_combination = -1;
bayesnet::Smoothing_t smoothing = bayesnet::Smoothing_t::NONE;
json best_fold_hyper;
for (int idx_combination = 0; idx_combination < combinations.size(); ++idx_combination) {
auto hyperparam_line = combinations[idx_combination];
auto hyperparameters = platform::HyperParameters(datasets.getNames(), hyperparam_line);
folding::Fold* nested_fold;
if (config.stratified)
nested_fold = new folding::StratifiedKFold(config.nested, y_train, seed);
else
nested_fold = new folding::KFold(config.nested, y_train.size(0), seed);
double score = 0.0;
for (int n_nested_fold = 0; n_nested_fold < config.nested; n_nested_fold++) {
// Nested level fold
auto [train_nested, test_nested] = nested_fold->getFold(n_nested_fold);
auto train_nested_t = torch::tensor(train_nested);
auto test_nested_t = torch::tensor(test_nested);
auto X_nested_train = X_train.index({ "...", train_nested_t });
auto y_nested_train = y_train.index({ train_nested_t });
auto X_nested_test = X_train.index({ "...", test_nested_t });
auto y_nested_test = y_train.index({ test_nested_t });
// Build Classifier with selected hyperparameters
auto clf = Models::instance()->create(config.model);
auto valid = clf->getValidHyperparameters();
hyperparameters.check(valid, dataset_name);
clf->setHyperparameters(hyperparameters.get(dataset_name));
// Train model
clf->fit(X_nested_train, y_nested_train, features, className, states, smoothing);
// Test model
score += clf->score(X_nested_test, y_nested_test);
}
delete nested_fold;
score /= config.nested;
if (score > best_fold_score) {
best_fold_score = score;
best_idx_combination = idx_combination;
best_fold_hyper = hyperparam_line;
}
}
delete fold;
// Build Classifier with the best hyperparameters to obtain the best score
auto hyperparameters = platform::HyperParameters(datasets.getNames(), best_fold_hyper);
auto clf = Models::instance()->create(config.model);
auto valid = clf->getValidHyperparameters();
hyperparameters.check(valid, dataset_name);
clf->setHyperparameters(best_fold_hyper);
clf->fit(X_train, y_train, features, className, states, smoothing);
best_fold_score = clf->score(X_test, y_test);
// Return the result
result->idx_dataset = task["idx_dataset"].get<int>();
result->idx_combination = best_idx_combination;
result->score = best_fold_score;
result->n_fold = n_fold;
result->time = timer.getDuration();
// Update progress bar
std::cout << get_color_rank(config_mpi.rank) << "*" << std::flush;
}
json store_result(std::vector<std::string>& names, Task_Result& result, json& results)
{
json json_result = {
{ "score", result.score },
{ "combination", result.idx_combination },
{ "fold", result.n_fold },
{ "time", result.time },
{ "dataset", result.idx_dataset }
};
auto name = names[result.idx_dataset];
if (!results.contains(name)) {
results[name] = json::array();
}
results[name].push_back(json_result);
return results;
}
json producer(std::vector<std::string>& names, json& tasks, struct ConfigMPI& config_mpi, MPI_Datatype& MPI_Result)
{
Task_Result result;
json results;
int num_tasks = tasks.size();
//
// 2a.1 Producer will loop to send all the tasks to the consumers and receive the results
//
for (int i = 0; i < num_tasks; ++i) {
MPI_Status status;
MPI_Recv(&result, 1, MPI_Result, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == TAG_RESULT) {
//Store result
store_result(names, result, results);
}
MPI_Send(&i, 1, MPI_INT, status.MPI_SOURCE, TAG_TASK, MPI_COMM_WORLD);
}
//
// 2a.2 Producer will send the end message to all the consumers
//
for (int i = 0; i < config_mpi.n_procs - 1; ++i) {
MPI_Status status;
MPI_Recv(&result, 1, MPI_Result, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == TAG_RESULT) {
//Store result
store_result(names, result, results);
}
MPI_Send(&i, 1, MPI_INT, status.MPI_SOURCE, TAG_END, MPI_COMM_WORLD);
}
return results;
}
void select_best_results_folds(json& results, json& all_results, std::string& model)
{
Timer timer;
auto grid = GridData(Paths::grid_input(model));
//
// Select the best result of the computed outer folds
//
for (const auto& result : all_results.items()) {
// each result has the results of all the outer folds as each one were a different task
double best_score = 0.0;
json best;
for (const auto& result_fold : result.value()) {
double score = result_fold["score"].get<double>();
if (score > best_score) {
best_score = score;
best = result_fold;
}
}
auto dataset = result.key();
auto combinations = grid.getGrid(dataset);
json json_best = {
{ "score", best_score },
{ "hyperparameters", combinations[best["combination"].get<int>()] },
{ "date", get_date() + " " + get_time() },
{ "grid", grid.getInputGrid(dataset) },
{ "duration", timer.translate2String(best["time"].get<double>()) }
};
results[dataset] = json_best;
}
}
void consumer(Datasets& datasets, json& tasks, struct ConfigGrid& config, struct ConfigMPI& config_mpi, MPI_Datatype& MPI_Result)
{
Task_Result result;
//
// 2b.1 Consumers announce to the producer that they are ready to receive a task
//
MPI_Send(&result, 1, MPI_Result, config_mpi.manager, TAG_QUERY, MPI_COMM_WORLD);
int task;
while (true) {
MPI_Status status;
//
// 2b.2 Consumers receive the task from the producer and process it
//
MPI_Recv(&task, 1, MPI_INT, config_mpi.manager, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == TAG_END) {
break;
}
process_task_mpi_consumer(config, config_mpi, tasks, task, datasets, &result);
//
// 2b.3 Consumers send the result to the producer
//
MPI_Send(&result, 1, MPI_Result, config_mpi.manager, TAG_RESULT, MPI_COMM_WORLD);
}
}
void GridSearch::go(struct ConfigMPI& config_mpi)
{
/*
* Each task is a json object with the following structure:
* {
* "dataset": "dataset_name",
* "idx_dataset": idx_dataset, // used to identify the dataset in the results
* // this index is relative to the used datasets in the actual run not to the whole datasets
* "seed": # of seed to use,
* "Fold": # of fold to process
* }
*
* The overall process consists in these steps:
* 0. Create the MPI result type & tasks
* 0.1 Create the MPI result type
* 0.2 Manager creates the tasks
* 1. Manager will broadcast the tasks to all the processes
* 1.1 Broadcast the number of tasks
* 1.2 Broadcast the length of the following string
* 1.2 Broadcast the tasks as a char* string
* 2a. Producer delivers the tasks to the consumers
* 2a.1 Producer will loop to send all the tasks to the consumers and receive the results
* 2a.2 Producer will send the end message to all the consumers
* 2b. Consumers process the tasks and send the results to the producer
* 2b.1 Consumers announce to the producer that they are ready to receive a task
* 2b.2 Consumers receive the task from the producer and process it
* 2b.3 Consumers send the result to the producer
* 3. Manager select the bests sccores for each dataset
* 3.1 Loop thru all the results obtained from each outer fold (task) and select the best
* 3.2 Save the results
*/
//
// 0.1 Create the MPI result type
//
Task_Result result;
int tasks_size;
MPI_Datatype MPI_Result;
MPI_Datatype type[5] = { MPI_UNSIGNED, MPI_UNSIGNED, MPI_INT, MPI_DOUBLE, MPI_DOUBLE };
int blocklen[5] = { 1, 1, 1, 1, 1 };
MPI_Aint disp[5];
disp[0] = offsetof(Task_Result, idx_dataset);
disp[1] = offsetof(Task_Result, idx_combination);
disp[2] = offsetof(Task_Result, n_fold);
disp[3] = offsetof(Task_Result, score);
disp[4] = offsetof(Task_Result, time);
MPI_Type_create_struct(5, blocklen, disp, type, &MPI_Result);
MPI_Type_commit(&MPI_Result);
//
// 0.2 Manager creates the tasks
//
char* msg;
json tasks;
if (config_mpi.rank == config_mpi.manager) {
timer.start();
tasks = build_tasks_mpi(config_mpi.rank);
auto tasks_str = tasks.dump();
tasks_size = tasks_str.size();
msg = new char[tasks_size + 1];
strcpy(msg, tasks_str.c_str());
}
//
// 1. Manager will broadcast the tasks to all the processes
//
MPI_Bcast(&tasks_size, 1, MPI_INT, config_mpi.manager, MPI_COMM_WORLD);
if (config_mpi.rank != config_mpi.manager) {
msg = new char[tasks_size + 1];
}
MPI_Bcast(msg, tasks_size + 1, MPI_CHAR, config_mpi.manager, MPI_COMM_WORLD);
tasks = json::parse(msg);
delete[] msg;
auto env = platform::DotEnv();
auto datasets = Datasets(config.discretize, Paths::datasets(), env.get("discretize_algo"));
if (config_mpi.rank == config_mpi.manager) {
//
// 2a. Producer delivers the tasks to the consumers
//
auto datasets_names = filterDatasets(datasets);
json all_results = producer(datasets_names, tasks, config_mpi, MPI_Result);
std::cout << get_color_rank(config_mpi.rank) << separator << std::endl;
//
// 3. Manager select the bests sccores for each dataset
//
auto results = initializeResults();
select_best_results_folds(results, all_results, config.model);
//
// 3.2 Save the results
//
save(results);
} else {
//
// 2b. Consumers process the tasks and send the results to the producer
//
consumer(datasets, tasks, config, config_mpi, MPI_Result);
}
}
json GridSearch::initializeResults() json GridSearch::initializeResults()
{ {
// Load previous results if continue is set // Load previous results if continue is set
json results; json results;
if (config.continue_from != NO_CONTINUE()) { if (config.continue_from != NO_CONTINUE()) {
if (!config.quiet) if (!config.quiet)
std::cout << "* Loading previous results" << std::endl; std::cout << Colors::RESET() << "* Loading previous results" << std::endl;
try { try {
std::ifstream file(Paths::grid_output(config.model)); std::ifstream file(Paths::grid_output(config.model));
if (file.is_open()) { if (file.is_open()) {
@@ -420,4 +93,167 @@ namespace platform {
}; };
file << output.dump(4); file << output.dump(4);
} }
} /* namespace platform */ void GridSearch::compile_results(json& results, json& all_results, std::string& model)
{
Timer timer;
auto grid = GridData(Paths::grid_input(model));
//
// Select the best result of the computed outer folds
//
for (const auto& result : all_results.items()) {
// each result has the results of all the outer folds as each one were a different task
double best_score = 0.0;
json best;
for (const auto& result_fold : result.value()) {
double score = result_fold["score"].get<double>();
if (score > best_score) {
best_score = score;
best = result_fold;
}
}
auto dataset = result.key();
auto combinations = grid.getGrid(dataset);
json json_best = {
{ "score", best_score },
{ "hyperparameters", combinations[best["combination"].get<int>()] },
{ "date", get_date() + " " + get_time() },
{ "grid", grid.getInputGrid(dataset) },
{ "duration", timer.translate2String(best["time"].get<double>()) }
};
results[dataset] = json_best;
}
}
json GridSearch::store_result(std::vector<std::string>& names, Task_Result& result, json& results)
{
json json_result = {
{ "score", result.score },
{ "combination", result.idx_combination },
{ "fold", result.n_fold },
{ "time", result.time },
{ "dataset", result.idx_dataset },
{ "process", result.process },
{ "task", result.task }
};
auto name = names[result.idx_dataset];
if (!results.contains(name)) {
results[name] = json::array();
}
results[name].push_back(json_result);
return results;
}
void GridSearch::consumer_go(struct ConfigGrid& config, struct ConfigMPI& config_mpi, json& tasks, int n_task, Datasets& datasets, Task_Result* result)
{
//
// initialize
//
Timer timer;
timer.start();
json task = tasks[n_task];
auto model = config.model;
auto grid = GridData(Paths::grid_input(model));
auto dataset_name = task["dataset"].get<std::string>();
auto idx_dataset = task["idx_dataset"].get<int>();
auto seed = task["seed"].get<int>();
auto n_fold = task["fold"].get<int>();
bool stratified = config.stratified;
bayesnet::Smoothing_t smooth;
if (config.smooth_strategy == "ORIGINAL")
smooth = bayesnet::Smoothing_t::ORIGINAL;
else if (config.smooth_strategy == "LAPLACE")
smooth = bayesnet::Smoothing_t::LAPLACE;
else if (config.smooth_strategy == "CESTNIK")
smooth = bayesnet::Smoothing_t::CESTNIK;
//
// Generate the hyperparameters combinations
//
auto& dataset = datasets.getDataset(dataset_name);
auto combinations = grid.getGrid(dataset_name);
dataset.load();
auto [X, y] = dataset.getTensors();
auto features = dataset.getFeatures();
auto className = dataset.getClassName();
//
// Start working on task
//
folding::Fold* fold;
if (stratified)
fold = new folding::StratifiedKFold(config.n_folds, y, seed);
else
fold = new folding::KFold(config.n_folds, y.size(0), seed);
auto [train, test] = fold->getFold(n_fold);
auto [X_train, X_test, y_train, y_test] = dataset.getTrainTestTensors(train, test);
auto states = dataset.getStates(); // Get the states of the features Once they are discretized
float best_fold_score = 0.0;
int best_idx_combination = -1;
json best_fold_hyper;
for (int idx_combination = 0; idx_combination < combinations.size(); ++idx_combination) {
auto hyperparam_line = combinations[idx_combination];
auto hyperparameters = platform::HyperParameters(datasets.getNames(), hyperparam_line);
folding::Fold* nested_fold;
if (config.stratified)
nested_fold = new folding::StratifiedKFold(config.nested, y_train, seed);
else
nested_fold = new folding::KFold(config.nested, y_train.size(0), seed);
double score = 0.0;
for (int n_nested_fold = 0; n_nested_fold < config.nested; n_nested_fold++) {
//
// Nested level fold
//
auto [train_nested, test_nested] = nested_fold->getFold(n_nested_fold);
auto train_nested_t = torch::tensor(train_nested);
auto test_nested_t = torch::tensor(test_nested);
auto X_nested_train = X_train.index({ "...", train_nested_t });
auto y_nested_train = y_train.index({ train_nested_t });
auto X_nested_test = X_train.index({ "...", test_nested_t });
auto y_nested_test = y_train.index({ test_nested_t });
//
// Build Classifier with selected hyperparameters
//
auto clf = Models::instance()->create(config.model);
auto valid = clf->getValidHyperparameters();
hyperparameters.check(valid, dataset_name);
clf->setHyperparameters(hyperparameters.get(dataset_name));
//
// Train model
//
clf->fit(X_nested_train, y_nested_train, features, className, states, smooth);
//
// Test model
//
score += clf->score(X_nested_test, y_nested_test);
}
delete nested_fold;
score /= config.nested;
if (score > best_fold_score) {
best_fold_score = score;
best_idx_combination = idx_combination;
best_fold_hyper = hyperparam_line;
}
}
delete fold;
//
// Build Classifier with the best hyperparameters to obtain the best score
//
auto hyperparameters = platform::HyperParameters(datasets.getNames(), best_fold_hyper);
auto clf = Models::instance()->create(config.model);
auto valid = clf->getValidHyperparameters();
hyperparameters.check(valid, dataset_name);
clf->setHyperparameters(best_fold_hyper);
clf->fit(X_train, y_train, features, className, states, smooth);
best_fold_score = clf->score(X_test, y_test);
//
// Return the result
//
result->idx_dataset = task["idx_dataset"].get<int>();
result->idx_combination = best_idx_combination;
result->score = best_fold_score;
result->n_fold = n_fold;
result->time = timer.getDuration();
result->process = config_mpi.rank;
result->task = n_task;
//
// Update progress bar
//
std::cout << get_color_rank(config_mpi.rank) << std::flush;
}
} /* namespace platform */

View File

@@ -4,47 +4,20 @@
#include <map> #include <map>
#include <mpi.h> #include <mpi.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <folding.hpp>
#include "common/Datasets.h" #include "common/Datasets.h"
#include "common/Timer.h" #include "common/Timer.hpp"
#include "main/HyperParameters.h" #include "main/HyperParameters.h"
#include "GridData.h" #include "GridData.h"
#include "GridBase.h"
#include "bayesnet/network/Network.h"
namespace platform { namespace platform {
using json = nlohmann::ordered_json; using json = nlohmann::ordered_json;
struct ConfigGrid { class GridSearch : public GridBase {
std::string model;
std::string score;
std::string continue_from;
std::string platform;
bool quiet;
bool only; // used with continue_from to only compute that dataset
bool discretize;
bool stratified;
int nested;
int n_folds;
json excluded;
std::vector<int> seeds;
};
struct ConfigMPI {
int rank;
int n_procs;
int manager;
};
typedef struct {
uint idx_dataset;
uint idx_combination;
int n_fold;
double score;
double time;
} Task_Result;
const int TAG_QUERY = 1;
const int TAG_RESULT = 2;
const int TAG_TASK = 3;
const int TAG_END = 4;
class GridSearch {
public: public:
explicit GridSearch(struct ConfigGrid& config); explicit GridSearch(struct ConfigGrid& config);
void go(struct ConfigMPI& config_mpi);
~GridSearch() = default; ~GridSearch() = default;
json loadResults(); json loadResults();
static inline std::string NO_CONTINUE() { return "NO_CONTINUE"; } static inline std::string NO_CONTINUE() { return "NO_CONTINUE"; }
@@ -52,10 +25,9 @@ namespace platform {
void save(json& results); void save(json& results);
json initializeResults(); json initializeResults();
std::vector<std::string> filterDatasets(Datasets& datasets) const; std::vector<std::string> filterDatasets(Datasets& datasets) const;
struct ConfigGrid config; void compile_results(json& results, json& all_results, std::string& model);
json build_tasks_mpi(int rank); json store_result(std::vector<std::string>& names, Task_Result& result, json& results);
Timer timer; // used to measure the time of the whole process void consumer_go(struct ConfigGrid& config, struct ConfigMPI& config_mpi, json& tasks, int n_task, Datasets& datasets, Task_Result* result);
const std::string separator = "|";
}; };
} /* namespace platform */ } /* namespace platform */
#endif #endif

View File

@@ -0,0 +1,230 @@
#include "common/Datasets.h"
#include "common/DotEnv.h"
#include "common/Paths.h"
#include "main/Models.h"
#include "main/modelRegister.h"
#include "ArgumentsExperiment.h"
namespace platform {
ArgumentsExperiment::ArgumentsExperiment(argparse::ArgumentParser& program, experiment_t type) : arguments{ program }, type{ type }
{
}
void ArgumentsExperiment::add_arguments()
{
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")
.action([](const std::string& value) {
auto datasets = platform::Datasets(false, platform::Paths::datasets());
static std::vector<std::string> choices_datasets(datasets.getNames());
choices_datasets.push_back("all");
if (find(choices_datasets.begin(), choices_datasets.end(), value) != choices_datasets.end()) {
return value;
}
throw std::runtime_error("Dataset must be one of: " + datasets.toString());
}
);
group.add_argument("--datasets").nargs(1, 50).help("Datasets file names 1..50 separated by spaces").default_value(std::vector<std::string>());
group.add_argument("--datasets-file").default_value("").help("Datasets file name. Mutually exclusive with dataset. This file should contain a list of datasets to test.");
arguments.add_argument("--hyperparameters").default_value("{}").help("Hyperparameters passed to the model in Experiment");
arguments.add_argument("--hyper-file").default_value("").help("Hyperparameters file name." \
"Mutually exclusive with hyperparameters. This file should contain hyperparameters for each dataset in json format.");
arguments.add_argument("--hyper-best").default_value(false).help("Use best results of the model as source of hyperparameters").implicit_value(true);
arguments.add_argument("-m", "--model")
.help("Model to use: " + platform::Models::instance()->toString())
.action([](const std::string& value) {
static const std::vector<std::string> choices = platform::Models::instance()->getNames();
if (find(choices.begin(), choices.end(), value) != choices.end()) {
return value;
}
throw std::runtime_error("Model must be one of " + platform::Models::instance()->toString());
}
);
arguments.add_argument("--title").default_value("").help("Experiment title");
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"));
for (auto choice : valid_choices) {
disc_arg.choices(choice);
}
valid_choices = env.valid_tokens("smooth_strat");
auto& smooth_arg = arguments.add_argument("--smooth-strat").help("Smooth strategy used in Bayes Network node initialization. Valid values: " + env.valid_values("smooth_strat")).default_value(env.get("smooth_strat"));
for (auto choice : valid_choices) {
smooth_arg.choices(choice);
}
auto& score_arg = arguments.add_argument("-s", "--score").help("Score to use. Valid values: " + env.valid_values("score")).default_value(env.get("score"));
valid_choices = env.valid_tokens("score");
for (auto choice : valid_choices) {
score_arg.choices(choice);
}
arguments.add_argument("--no-train-score").help("Don't compute train score").default_value(false).implicit_value(true);
arguments.add_argument("--quiet").help("Don't display detailed progress").default_value(false).implicit_value(true);
arguments.add_argument("--save").help("Save result (always save even if a dataset is supplied)").default_value(false).implicit_value(true);
arguments.add_argument("--stratified").help("If Stratified KFold is to be done").default_value((bool)stoi(env.get("stratified"))).implicit_value(true);
arguments.add_argument("-f", "--folds").help("Number of folds").default_value(stoi(env.get("n_folds"))).scan<'i', int>().action([](const std::string& value) {
try {
auto k = stoi(value);
if (k < 2) {
throw std::runtime_error("Number of folds must be greater than 1");
}
return k;
}
catch (const runtime_error& err) {
throw std::runtime_error(err.what());
}
catch (...) {
throw std::runtime_error("Number of folds must be an integer");
}});
auto seed_values = env.getSeeds();
arguments.add_argument("--seeds").nargs(1, 10).help("Random seeds. Set to -1 to have pseudo random").scan<'i', int>().default_value(seed_values);
if (type == experiment_t::NORMAL) {
arguments.add_argument("--generate-fold-files").help("generate fold information in datasets_experiment folder").default_value(false).implicit_value(true);
arguments.add_argument("--graph").help("generate graphviz dot files with the model").default_value(false).implicit_value(true);
}
}
void ArgumentsExperiment::parse_args(int argc, char** argv)
{
try {
arguments.parse_args(argc, argv);
}
catch (const exception& err) {
cerr << err.what() << std::endl;
cerr << arguments;
exit(1);
}
parse();
}
void ArgumentsExperiment::parse()
{
try {
file_name = arguments.get<std::string>("dataset");
file_names = arguments.get<std::vector<std::string>>("datasets");
datasets_file = arguments.get<std::string>("datasets-file");
model_name = arguments.get<std::string>("model");
discretize_dataset = arguments.get<bool>("discretize");
discretize_algo = arguments.get<std::string>("discretize-algo");
smooth_strat = arguments.get<std::string>("smooth-strat");
stratified = arguments.get<bool>("stratified");
quiet = arguments.get<bool>("quiet");
n_folds = arguments.get<int>("folds");
score = arguments.get<std::string>("score");
seeds = arguments.get<std::vector<int>>("seeds");
auto hyperparameters = arguments.get<std::string>("hyperparameters");
hyperparameters_json = json::parse(hyperparameters);
hyperparameters_file = arguments.get<std::string>("hyper-file");
no_train_score = arguments.get<bool>("no-train-score");
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);
// ignore this parameter
hyperparameters = "{}";
} else {
if (hyperparameters_file != "" && hyperparameters != "{}") {
throw runtime_error("hyperparameters and hyper_file are mutually exclusive");
}
}
title = arguments.get<std::string>("title");
if (title == "" && file_name == "all") {
throw runtime_error("title is mandatory if all datasets are to be tested");
}
saveResults = arguments.get<bool>("save");
if (type == experiment_t::NORMAL) {
graph = arguments.get<bool>("graph");
generate_fold_files = arguments.get<bool>("generate-fold-files");
} else {
graph = false;
generate_fold_files = false;
}
}
catch (const exception& err) {
cerr << err.what() << std::endl;
cerr << arguments;
exit(1);
}
auto datasets = platform::Datasets(false, platform::Paths::datasets());
if (datasets_file != "") {
ifstream catalog(datasets_file);
if (catalog.is_open()) {
std::string line;
while (getline(catalog, line)) {
if (line.empty() || line[0] == '#') {
continue;
}
if (!datasets.isDataset(line)) {
cerr << "Dataset " << line << " not found" << std::endl;
exit(1);
}
filesToTest.push_back(line);
}
catalog.close();
saveResults = true;
if (title == "") {
title = "Test " + to_string(filesToTest.size()) + " datasets (" + datasets_file + ") "\
+ model_name + " " + to_string(n_folds) + " folds";
}
} else {
throw std::invalid_argument("Unable to open catalog file. [" + datasets_file + "]");
}
} else {
if (file_names.size() > 0) {
for (auto file : file_names) {
if (!datasets.isDataset(file)) {
cerr << "Dataset " << file << " not found" << std::endl;
exit(1);
}
}
filesToTest = file_names;
sort(filesToTest.begin(), filesToTest.end(), [](const auto& lhs, const auto& rhs) {
const auto result = mismatch(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), [](const auto& lhs, const auto& rhs) {return tolower(lhs) == tolower(rhs);});
return result.second != rhs.cend() && (result.first == lhs.cend() || tolower(*result.first) < tolower(*result.second));
});
saveResults = true;
if (title == "") {
title = "Test " + to_string(file_names.size()) + " datasets " + model_name + " " + to_string(n_folds) + " folds";
}
} else {
if (file_name != "all") {
if (!datasets.isDataset(file_name)) {
cerr << "Dataset " << file_name << " not found" << std::endl;
exit(1);
}
if (title == "") {
title = "Test " + file_name + " " + model_name + " " + to_string(n_folds) + " folds";
}
filesToTest.push_back(file_name);
} else {
filesToTest = datasets.getNames();
saveResults = true;
}
}
}
if (hyperparameters_file != "") {
test_hyperparams = platform::HyperParameters(datasets.getNames(), hyperparameters_file, hyper_best);
} else {
test_hyperparams = platform::HyperParameters(datasets.getNames(), hyperparameters_json);
}
}
Experiment& ArgumentsExperiment::initializedExperiment()
{
auto env = platform::DotEnv();
experiment.setTitle(title).setLanguage("c++").setLanguageVersion("gcc 14.1.1");
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);
experiment.setHyperparameters(test_hyperparams);
for (auto seed : seeds) {
experiment.addRandomSeed(seed);
}
experiment.setFilesToTest(filesToTest);
experiment.setQuiet(quiet);
experiment.setNoTrainScore(no_train_score);
experiment.setGenerateFoldFiles(generate_fold_files);
experiment.setGraph(graph);
return experiment;
}
}

View File

@@ -0,0 +1,39 @@
#ifndef ARGUMENTSEXPERIMENT_H
#define ARGUMENTSEXPERIMENT_H
#include <string>
#include <iostream>
#include <vector>
#include <argparse/argparse.hpp>
#include <nlohmann/json.hpp>
#include "Experiment.h"
namespace platform {
using json = nlohmann::ordered_json;
enum class experiment_t { NORMAL, GRID };
class ArgumentsExperiment {
public:
ArgumentsExperiment(argparse::ArgumentParser& program, experiment_t type);
~ArgumentsExperiment() = default;
std::vector<std::string> getFilesToTest() const { return filesToTest; }
void add_arguments();
void parse_args(int argc, char** argv);
void parse();
Experiment& initializedExperiment();
bool isQuiet() const { return quiet; }
bool haveToSaveResults() const { return saveResults; }
bool doGraph() const { return graph; }
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;
json hyperparameters_json;
bool discretize_dataset, stratified, saveResults, quiet, no_train_score, generate_fold_files, graph, hyper_best;
std::vector<int> seeds;
std::vector<std::string> file_names;
std::vector<std::string> filesToTest;
platform::HyperParameters test_hyperparams;
int n_folds;
};
}
#endif

View File

@@ -9,14 +9,16 @@ namespace platform {
void Experiment::saveResult() void Experiment::saveResult()
{ {
result.setSchemaVersion("1.0");
result.check();
result.save(); result.save();
std::cout << "Result saved in " << Paths::results() << result.getFilename() << std::endl; std::cout << "Result saved in " << Paths::results() << result.getFilename() << std::endl;
} }
void Experiment::report(bool classification_report) void Experiment::report()
{ {
ReportConsole report(result.getJson()); ReportConsole report(result.getJson());
report.show(); report.show();
if (classification_report) { if (filesToTest.size() == 1) {
std::cout << report.showClassificationReport(Colors::BLUE()); std::cout << report.showClassificationReport(Colors::BLUE());
} }
} }
@@ -41,9 +43,25 @@ namespace platform {
} }
} }
} }
void Experiment::go(std::vector<std::string> filesToProcess, bool quiet, bool no_train_score, bool generate_fold_files, bool graph) Experiment& Experiment::setSmoothSrategy(const std::string& smooth_strategy)
{ {
for (auto fileName : filesToProcess) { this->smooth_strategy = smooth_strategy;
this->result.setSmoothStrategy(smooth_strategy);
if (smooth_strategy == "ORIGINAL")
smooth_type = bayesnet::Smoothing_t::ORIGINAL;
else if (smooth_strategy == "LAPLACE")
smooth_type = bayesnet::Smoothing_t::LAPLACE;
else if (smooth_strategy == "CESTNIK")
smooth_type = bayesnet::Smoothing_t::CESTNIK;
else {
std::cerr << "Experiment: Unknown smoothing strategy: " << smooth_strategy << std::endl;
exit(1);
}
return *this;
}
void Experiment::go()
{
for (auto fileName : filesToTest) {
if (fileName.size() > max_name) if (fileName.size() > max_name)
max_name = fileName.size(); max_name = fileName.size();
} }
@@ -58,14 +76,16 @@ namespace platform {
std::cout << " ( " << Colors::GREEN() << "b" << Colors::RESET() << " ) Scoring train dataset" << std::endl; std::cout << " ( " << Colors::GREEN() << "b" << Colors::RESET() << " ) Scoring train dataset" << std::endl;
std::cout << " ( " << Colors::GREEN() << "c" << Colors::RESET() << " ) Scoring test dataset" << std::endl << std::endl; std::cout << " ( " << Colors::GREEN() << "c" << Colors::RESET() << " ) Scoring test dataset" << std::endl << std::endl;
std::cout << Colors::YELLOW() << "Note: fold number in this color means fitting had issues such as not using all features in BoostAODE classifier" << std::endl << std::endl; std::cout << Colors::YELLOW() << "Note: fold number in this color means fitting had issues such as not using all features in BoostAODE classifier" << std::endl << std::endl;
std::cout << Colors::GREEN() << left << " # " << setw(max_name) << "Dataset" << " #Samp #Feat Seed Status" << string(3 * nfolds - 2, ' ') << " Time" << std::endl; int nc = 4 + 3 * nfolds + (nfolds >= 10 ? nfolds - 10 + 1 : 0);
std::cout << " --- " << string(max_name, '-') << " ----- ----- ---- " << string(4 + 3 * nfolds, '-') << " ----------" << Colors::RESET() << std::endl; std::cout << Colors::GREEN() << left << " # " << setw(max_name) << "Dataset" << " #Samp #Feat Seed Status" << string(nc - 6, ' ') << setw(11) << " Time" << " Score" << std::endl;
std::cout << " --- " << string(max_name, '-') << " ----- ----- ---- " << string(nc, '-') << " ----------" << " ---------";
std::cout << Colors::RESET() << std::endl;
} }
int num = 0; int num = 0;
for (auto fileName : filesToProcess) { for (auto fileName : filesToTest) {
if (!quiet) if (!quiet)
std::cout << " " << setw(3) << right << num++ << " " << setw(max_name) << left << fileName << right << flush; std::cout << " " << setw(3) << right << num++ << " " << setw(max_name) << left << fileName << right << flush;
cross_validation(fileName, quiet, no_train_score, generate_fold_files, graph); cross_validation(fileName);
if (!quiet) if (!quiet)
std::cout << std::endl; std::cout << std::endl;
} }
@@ -95,7 +115,8 @@ namespace platform {
} }
void showProgress(int fold, const std::string& color, const std::string& phase) void showProgress(int fold, const std::string& color, const std::string& phase)
{ {
std::string prefix = phase == "-" ? "" : "\b\b\b\b"; int nc = fold >= 10 ? 5 : 4;
std::string prefix = phase == "-" ? "" : std::string(nc, '\b');
std::cout << prefix << color << fold << Colors::RESET() << "(" << color << phase << Colors::RESET() << ")" << flush; std::cout << prefix << color << fold << Colors::RESET() << "(" << color << phase << Colors::RESET() << ")" << flush;
} }
@@ -137,7 +158,7 @@ namespace platform {
file << output.dump(4); file << output.dump(4);
file.close(); file.close();
} }
void Experiment::cross_validation(const std::string& fileName, bool quiet, bool no_train_score, bool generate_fold_files, bool graph) void Experiment::cross_validation(const std::string& fileName)
{ {
// //
// Load dataset and prepare data // Load dataset and prepare data
@@ -241,7 +262,8 @@ namespace platform {
auto y_proba_train = clf->predict_proba(X_train); auto y_proba_train = clf->predict_proba(X_train);
Scores scores(y_train, y_proba_train, num_classes, labels); Scores scores(y_train, y_proba_train, num_classes, labels);
score_train_value = score == score_t::ACCURACY ? scores.accuracy() : scores.auc(); 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 // Test model
@@ -256,7 +278,8 @@ namespace platform {
test_time[item] = test_timer.getDuration(); test_time[item] = test_timer.getDuration();
score_train[item] = score_train_value; score_train[item] = score_train_value;
score_test[item] = score_test_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) if (!quiet)
std::cout << "\b\b\b, " << flush; std::cout << "\b\b\b, " << flush;
// //
@@ -277,10 +300,13 @@ namespace platform {
} }
if (!quiet) { if (!quiet) {
seed_timer.stop(); seed_timer.stop();
std::cout << "end. [" << seed_timer.getDurationString() << "]" << std::endl; std::cout << "end. " << std::setw(10) << std::right << seed_timer.getDurationString();
} }
delete fold; delete fold;
} }
// Show Results
if (!quiet)
std::cout << " " << setw(9) << right << std::fixed << std::setprecision(7) << torch::mean(score_test).item<double>();
// //
// Store result totals in Result // Store result totals in Result
// //

View File

@@ -25,21 +25,7 @@ namespace platform {
{ {
this->discretization_algo = discretization_algo; this->result.setDiscretizationAlgorithm(discretization_algo); return *this; this->discretization_algo = discretization_algo; this->result.setDiscretizationAlgorithm(discretization_algo); return *this;
} }
Experiment& setSmoothSrategy(const std::string& smooth_strategy) Experiment& setSmoothSrategy(const std::string& smooth_strategy);
{
this->smooth_strategy = smooth_strategy; this->result.setSmoothStrategy(smooth_strategy);
if (smooth_strategy == "ORIGINAL")
smooth_type = bayesnet::Smoothing_t::ORIGINAL;
else if (smooth_strategy == "LAPLACE")
smooth_type = bayesnet::Smoothing_t::LAPLACE;
else if (smooth_strategy == "CESTNIK")
smooth_type = bayesnet::Smoothing_t::CESTNIK;
else {
std::cerr << "Experiment: Unknown smoothing strategy: " << smooth_strategy << std::endl;
exit(1);
}
return *this;
}
Experiment& setLanguageVersion(const std::string& language_version) { this->result.setLanguageVersion(language_version); return *this; } Experiment& setLanguageVersion(const std::string& language_version) { this->result.setLanguageVersion(language_version); return *this; }
Experiment& setDiscretized(bool discretized) { this->discretized = discretized; result.setDiscretized(discretized); return *this; } Experiment& setDiscretized(bool discretized) { this->discretized = discretized; result.setDiscretized(discretized); return *this; }
Experiment& setStratified(bool stratified) { this->stratified = stratified; result.setStratified(stratified); return *this; } Experiment& setStratified(bool stratified) { this->stratified = stratified; result.setStratified(stratified); return *this; }
@@ -48,18 +34,33 @@ namespace platform {
Experiment& addRandomSeed(int randomSeed) { randomSeeds.push_back(randomSeed); result.addSeed(randomSeed); return *this; } Experiment& addRandomSeed(int randomSeed) { randomSeeds.push_back(randomSeed); result.addSeed(randomSeed); return *this; }
Experiment& setDuration(float duration) { this->result.setDuration(duration); return *this; } Experiment& setDuration(float duration) { this->result.setDuration(duration); return *this; }
Experiment& setHyperparameters(const HyperParameters& hyperparameters_) { this->hyperparameters = hyperparameters_; return *this; } Experiment& setHyperparameters(const HyperParameters& hyperparameters_) { this->hyperparameters = hyperparameters_; return *this; }
void cross_validation(const std::string& fileName, bool quiet, bool no_train_score, bool generate_fold_files, bool graph); HyperParameters& getHyperParameters() { return hyperparameters; }
void go(std::vector<std::string> filesToProcess, bool quiet, bool no_train_score, bool generate_fold_files, bool graph); std::string getModel() const { return result.getModel(); }
std::string getScore() const { return result.getScoreName(); }
bool isDiscretized() const { return discretized; }
bool isStratified() const { return stratified; }
bool isQuiet() const { return quiet; }
std::string getSmoothStrategy() const { return smooth_strategy; }
int getNFolds() const { return nfolds; }
std::vector<int> getRandomSeeds() const { return randomSeeds; }
void cross_validation(const std::string& fileName);
void go();
void saveResult(); void saveResult();
void show(); void show();
void saveGraph(); void saveGraph();
void report(bool classification_report = false); void report();
void setFilesToTest(const std::vector<std::string>& filesToTest) { this->filesToTest = filesToTest; }
void setQuiet(bool quiet) { this->quiet = quiet; }
void setNoTrainScore(bool no_train_score) { this->no_train_score = no_train_score; }
void setGenerateFoldFiles(bool generate_fold_files) { this->generate_fold_files = generate_fold_files; }
void setGraph(bool graph) { this->graph = graph; }
private: private:
score_t parse_score() const; score_t parse_score() const;
Result result; Result result;
bool discretized{ false }, stratified{ false }; bool discretized{ false }, stratified{ false }, generate_fold_files{ false }, graph{ false }, quiet{ false }, no_train_score{ false };
std::vector<PartialResult> results; std::vector<PartialResult> results;
std::vector<int> randomSeeds; std::vector<int> randomSeeds;
std::vector<std::string> filesToTest;
std::string discretization_algo; std::string discretization_algo;
std::string smooth_strategy; std::string smooth_strategy;
bayesnet::Smoothing_t smooth_type{ bayesnet::Smoothing_t::NONE }; bayesnet::Smoothing_t smooth_type{ bayesnet::Smoothing_t::NONE };

View File

@@ -5,11 +5,15 @@
#include <bayesnet/ensembles/AODE.h> #include <bayesnet/ensembles/AODE.h>
#include <bayesnet/ensembles/A2DE.h> #include <bayesnet/ensembles/A2DE.h>
#include <bayesnet/ensembles/AODELd.h> #include <bayesnet/ensembles/AODELd.h>
#include <bayesnet/ensembles/XBAODE.h>
#include <bayesnet/ensembles/XBA2DE.h>
#include <bayesnet/ensembles/BoostAODE.h> #include <bayesnet/ensembles/BoostAODE.h>
#include <bayesnet/ensembles/BoostA2DE.h> #include <bayesnet/ensembles/BoostA2DE.h>
#include <bayesnet/classifiers/TAN.h> #include <bayesnet/classifiers/TAN.h>
#include <bayesnet/classifiers/KDB.h> #include <bayesnet/classifiers/KDB.h>
#include <bayesnet/classifiers/SPODE.h> #include <bayesnet/classifiers/SPODE.h>
#include <bayesnet/classifiers/XSPODE.h>
#include <bayesnet/classifiers/XSP2DE.h>
#include <bayesnet/classifiers/SPnDE.h> #include <bayesnet/classifiers/SPnDE.h>
#include <bayesnet/classifiers/TANLd.h> #include <bayesnet/classifiers/TANLd.h>
#include <bayesnet/classifiers/KDBLd.h> #include <bayesnet/classifiers/KDBLd.h>
@@ -20,6 +24,8 @@
#include <pyclassifiers/SVC.h> #include <pyclassifiers/SVC.h>
#include <pyclassifiers/XGBoost.h> #include <pyclassifiers/XGBoost.h>
#include <pyclassifiers/RandomForest.h> #include <pyclassifiers/RandomForest.h>
#include "../experimental_clfs/XA1DE.h"
namespace platform { namespace platform {
class Models { class Models {
public: public:
@@ -42,4 +48,4 @@ namespace platform {
Registrar(const std::string& className, function<bayesnet::BaseClassifier* (void)> classFactoryFunction); Registrar(const std::string& className, function<bayesnet::BaseClassifier* (void)> classFactoryFunction);
}; };
} }
#endif #endif

View File

@@ -1,39 +1,49 @@
#ifndef MODELREGISTER_H #ifndef MODELREGISTER_H
#define MODELREGISTER_H #define MODELREGISTER_H
namespace platform {
static platform::Registrar registrarT("TAN", static Registrar registrarT("TAN",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::TAN();}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::TAN();});
static platform::Registrar registrarTLD("TANLd", static Registrar registrarTLD("TANLd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::TANLd();}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::TANLd();});
static platform::Registrar registrarS("SPODE", static Registrar registrarS("SPODE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODE(2);}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODE(2);});
static platform::Registrar registrarSn("SPnDE", static Registrar registrarSn("SPnDE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPnDE({ 0, 1 });}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPnDE({ 0, 1 });});
static platform::Registrar registrarSLD("SPODELd", static Registrar registrarSLD("SPODELd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODELd(2);}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODELd(2);});
static platform::Registrar registrarK("KDB", static Registrar registrarK("KDB",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::KDB(2);}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::KDB(2);});
static platform::Registrar registrarKLD("KDBLd", static Registrar registrarKLD("KDBLd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::KDBLd(2);}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::KDBLd(2);});
static platform::Registrar registrarA("AODE", static Registrar registrarA("AODE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODE();}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODE();});
static platform::Registrar registrarA2("A2DE", static Registrar registrarA2("A2DE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::A2DE();}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::A2DE();});
static platform::Registrar registrarALD("AODELd", static Registrar registrarALD("AODELd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODELd();}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODELd();});
static platform::Registrar registrarBA("BoostAODE", static Registrar registrarBA("BoostAODE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::BoostAODE();}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::BoostAODE();});
static platform::Registrar registrarBA2("BoostA2DE", static Registrar registrarBA2("BoostA2DE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::BoostA2DE();}); [](void) -> bayesnet::BaseClassifier* { return new bayesnet::BoostA2DE();});
static platform::Registrar registrarSt("STree", static Registrar registrarSt("STree",
[](void) -> bayesnet::BaseClassifier* { return new pywrap::STree();}); [](void) -> bayesnet::BaseClassifier* { return new pywrap::STree();});
static platform::Registrar registrarOdte("Odte", static Registrar registrarOdte("Odte",
[](void) -> bayesnet::BaseClassifier* { return new pywrap::ODTE();}); [](void) -> bayesnet::BaseClassifier* { return new pywrap::ODTE();});
static platform::Registrar registrarSvc("SVC", static Registrar registrarSvc("SVC",
[](void) -> bayesnet::BaseClassifier* { return new pywrap::SVC();}); [](void) -> bayesnet::BaseClassifier* { return new pywrap::SVC();});
static platform::Registrar registrarRaF("RandomForest", static Registrar registrarRaF("RandomForest",
[](void) -> bayesnet::BaseClassifier* { return new pywrap::RandomForest();}); [](void) -> bayesnet::BaseClassifier* { return new pywrap::RandomForest();});
static platform::Registrar registrarXGB("XGBoost", static Registrar registrarXGB("XGBoost",
[](void) -> bayesnet::BaseClassifier* { return new pywrap::XGBoost();}); [](void) -> bayesnet::BaseClassifier* { return new pywrap::XGBoost();});
static Registrar registrarXSPODE("XSPODE",
#endif [](void) -> bayesnet::BaseClassifier* { return new bayesnet::XSpode(0);});
static Registrar registrarXSP2DE("XSP2DE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::XSp2de(0, 1);});
static Registrar registrarXBAODE("XBAODE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::XBAODE();});
static Registrar registrarXBA2DE("XBA2DE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::XBA2DE();});
static Registrar registrarXA1DE("XA1DE",
[](void) -> bayesnet::BaseClassifier* { return new XA1DE();});
}
#endif

View File

@@ -18,8 +18,8 @@ namespace platform {
const std::string STATUS_OK = "Ok."; const std::string STATUS_OK = "Ok.";
const std::string STATUS_COLOR = Colors::GREEN(); 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) : 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) :
rows{ rows }, cols{ cols }, complete{ complete }, partial{ partial }, compare{ compare }, didExcel(false), results(ResultsManager(model, score, platform, complete, partial)) rows{ rows }, cols{ cols }, complete{ complete }, partial{ partial }, compare{ compare }, didExcel(false), results(ResultsManager(path, model, score, platform, complete, partial))
{ {
results.load(); results.load();
openExcel = false; openExcel = false;
@@ -82,10 +82,12 @@ namespace platform {
workbook_close(workbook); workbook_close(workbook);
} }
if (didExcel) { if (didExcel) {
std::cout << Colors::MAGENTA() << "Excel file created: " << Paths::excel() + Paths::excelResults() << std::endl; excelFileName = Paths::excel() + Paths::excelResults();
std::cout << Colors::MAGENTA() << "Excel file created: " << excelFileName << std::endl;
} }
std::cout << Colors::RESET() << "Done!" << std::endl; std::cout << Colors::RESET() << "Done!" << std::endl;
} }
std::string ManageScreen::getVersions() std::string ManageScreen::getVersions()
{ {
std::string kfold_version = folding::KFold(5, 100).version(); std::string kfold_version = folding::KFold(5, 100).version();
@@ -257,8 +259,9 @@ namespace platform {
auto [index_from, index_to] = paginator[static_cast<int>(output_type)].getOffset(); auto [index_from, index_to] = paginator[static_cast<int>(output_type)].getOffset();
for (int i = index_from; i <= index_to; i++) { for (int i = index_from; i <= index_to; i++) {
auto color = (i % 2) ? Colors::BLUE() : Colors::CYAN(); auto color = (i % 2) ? Colors::BLUE() : Colors::CYAN();
std::cout << color << std::setw(3) << std::fixed << std::right << i << " "; auto color_status = results.at(i).check().size() == 0 ? color : Colors::RED();
std::cout << results.at(i).to_string(maxModel, maxTitle) << std::endl; std::cout << color_status << std::setw(3) << std::fixed << std::right << i << " ";
std::cout << color << results.at(i).to_string(maxModel, maxTitle) << std::endl;
} }
// //
// Status Area // Status Area
@@ -311,6 +314,34 @@ namespace platform {
return "Reporting " + results.at(index).getFilename(); return "Reporting " + results.at(index).getFilename();
} }
} }
void ManageScreen::changeModel(const int index)
{
std::cout << "Old model: " << results.at(index).getModel() << std::endl;
std::cout << "New model: ";
std::string newModel;
getline(std::cin, newModel);
if (newModel.empty()) {
list("Model not changed", Colors::YELLOW());
return;
}
if (newModel == results.at(index).getModel()) {
list("Model already set to " + newModel, Colors::RED());
return;
}
// Remove the old result file
std::string oldFile = Paths::results() + results.at(index).getFilename();
std::filesystem::remove(oldFile);
// Actually change the model
results.at(index).setModel(newModel);
results.at(index).save();
int newModelSize = static_cast<int>(newModel.size());
if (newModelSize > maxModel) {
maxModel = newModelSize;
header_lengths[2] = maxModel;
updateSize(rows, cols);
}
list("Model changed to " + newModel, Colors::GREEN());
}
std::pair<std::string, std::string> ManageScreen::sortList() std::pair<std::string, std::string> ManageScreen::sortList()
{ {
std::vector<std::tuple<std::string, char, bool>> sortOptions = { std::vector<std::tuple<std::string, char, bool>> sortOptions = {
@@ -371,6 +402,7 @@ namespace platform {
{"list", 'l', false}, {"list", 'l', false},
{"Delete", 'D', true}, {"Delete", 'D', true},
{"datasets", 'd', false}, {"datasets", 'd', false},
{"change model", 'm', true},
{"hide", 'h', true}, {"hide", 'h', true},
{"sort", 's', false}, {"sort", 's', false},
{"report", 'r', true}, {"report", 'r', true},
@@ -457,20 +489,19 @@ namespace platform {
index_A = index; index_A = index;
list("A set to " + std::to_string(index), Colors::GREEN()); list("A set to " + std::to_string(index), Colors::GREEN());
break; break;
case 'B': // set_b or back to list case 'B': // set_b
if (output_type == OutputType::EXPERIMENTS) { if (index == index_A) {
if (index == index_A) { list("A and B cannot be the same!", Colors::RED());
list("A and B cannot be the same!", Colors::RED()); break;
break;
}
index_B = index;
list("B set to " + std::to_string(index), Colors::GREEN());
} else {
// back to show the report
output_type = OutputType::RESULT;
paginator[static_cast<int>(OutputType::DETAIL)].setPage(1);
list(STATUS_OK, STATUS_COLOR);
} }
index_B = index;
list("B set to " + std::to_string(index), Colors::GREEN());
break;
case 'b': // back to list
// back to show the report
output_type = OutputType::RESULT;
paginator[static_cast<int>(OutputType::DETAIL)].setPage(1);
list(STATUS_OK, STATUS_COLOR);
break; break;
case 'c': case 'c':
if (index_A == -1 || index_B == -1) { if (index_A == -1 || index_B == -1) {
@@ -497,6 +528,9 @@ namespace platform {
paginator[static_cast<int>(OutputType::EXPERIMENTS)].setTotal(results.size()); paginator[static_cast<int>(OutputType::EXPERIMENTS)].setTotal(results.size());
list(filename + " deleted!", Colors::RED()); list(filename + " deleted!", Colors::RED());
break; break;
case 'm':
changeModel(index);
break;
case 'h': case 'h':
{ {
std::string status_message; std::string status_message;
@@ -543,7 +577,6 @@ namespace platform {
break; break;
case 't': case 't':
{ {
std::string status_message;
std::cout << "Title: " << results.at(index).getTitle() << std::endl; std::cout << "Title: " << results.at(index).getTitle() << std::endl;
std::cout << "New title: "; std::cout << "New title: ";
std::string newTitle; std::string newTitle;
@@ -551,8 +584,7 @@ namespace platform {
if (!newTitle.empty()) { if (!newTitle.empty()) {
results.at(index).setTitle(newTitle); results.at(index).setTitle(newTitle);
results.at(index).save(); results.at(index).save();
status_message = "Title changed to " + newTitle; list("Title changed to " + newTitle, Colors::GREEN());
list(status_message, Colors::GREEN());
break; break;
} }
list("No title change!", Colors::YELLOW()); list("No title change!", Colors::YELLOW());

View File

@@ -15,10 +15,11 @@ namespace platform {
}; };
class ManageScreen { class ManageScreen {
public: 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; ~ManageScreen() = default;
void doMenu(); void doMenu();
void updateSize(int rows, int cols); void updateSize(int rows, int cols);
std::string getExcelFileName() const { return excelFileName; }
private: private:
void list(const std::string& status, const std::string& color); void list(const std::string& status, const std::string& color);
void list_experiments(const std::string& status, const std::string& color); void list_experiments(const std::string& status, const std::string& color);
@@ -27,6 +28,7 @@ namespace platform {
void list_datasets(const std::string& status, const std::string& color); void list_datasets(const std::string& status, const std::string& color);
bool confirmAction(const std::string& intent, const std::string& fileName) const; bool confirmAction(const std::string& intent, const std::string& fileName) const;
std::string report(const int index, const bool excelReport); std::string report(const int index, const bool excelReport);
void changeModel(const int index);
std::string report_compared(); std::string report_compared();
std::pair<std::string, std::string> sortList(); std::pair<std::string, std::string> sortList();
std::string getVersions(); std::string getVersions();
@@ -57,6 +59,7 @@ namespace platform {
std::vector<Paginator> paginator; std::vector<Paginator> paginator;
ResultsManager results; ResultsManager results;
lxw_workbook* workbook; lxw_workbook* workbook;
std::string excelFileName;
}; };
} }
#endif #endif

View File

@@ -1,10 +1,9 @@
#include <algorithm> #include <algorithm>
#include "common/Paths.h"
#include "ResultsManager.h" #include "ResultsManager.h"
namespace platform { namespace platform {
ResultsManager::ResultsManager(const std::string& model, const std::string& score, const std::string& platform, bool complete, bool partial) : ResultsManager::ResultsManager(const std::string& path_, 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) path(path_), model(model), scoreName(score), platform(platform), complete(complete), partial(partial), maxModel(0), maxTitle(0)
{ {
} }
void ResultsManager::load() void ResultsManager::load()

View File

@@ -18,7 +18,7 @@ namespace platform {
}; };
class ResultsManager { class ResultsManager {
public: 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 load(); // Loads the list of results
void sortResults(SortField field, SortType type); // Sorts the list of results void sortResults(SortField field, SortType type); // Sorts the list of results
void sortDate(SortType type); void sortDate(SortType type);

View File

@@ -28,7 +28,7 @@ namespace platform {
auto datasets_names = datasets.getNames(); auto datasets_names = datasets.getNames();
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()); 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<std::string> header_labels = { " #", "Dataset", "Sampl.", "Feat.", "#Num.", "Cls", "Balance" };
std::vector<int> header_lengths = { 3, maxName, 6, 5, 5, 3, DatasetsConsole::BALANCE_LENGTH }; std::vector<int> header_lengths = { 3, maxName, 6, 6, 6, 3, DatasetsConsole::BALANCE_LENGTH };
sheader << Colors::GREEN(); sheader << Colors::GREEN();
for (int i = 0; i < header_labels.size(); i++) { for (int i = 0; i < header_labels.size(); i++) {
sheader << setw(header_lengths[i]) << left << header_labels[i] << " "; sheader << setw(header_lengths[i]) << left << header_labels[i] << " ";
@@ -51,14 +51,14 @@ namespace platform {
auto& dataset = datasets.getDataset(dataset_name); auto& dataset = datasets.getDataset(dataset_name);
dataset.load(); dataset.load();
auto nSamples = dataset.getNSamples(); auto nSamples = dataset.getNSamples();
line << setw(6) << right << nSamples << " "; line << setw(header_lengths[2]) << right << nSamples << " ";
auto nFeatures = dataset.getFeatures().size(); auto nFeatures = dataset.getFeatures().size();
line << setw(5) << right << nFeatures << " "; line << setw(header_lengths[3]) << right << nFeatures << " ";
auto numericFeatures = dataset.getNumericFeatures(); auto numericFeatures = dataset.getNumericFeatures();
auto num = std::count(numericFeatures.begin(), numericFeatures.end(), true); auto num = std::count(numericFeatures.begin(), numericFeatures.end(), true);
line << setw(5) << right << num << " "; line << setw(header_lengths[4]) << right << num << " ";
auto nClasses = dataset.getNClasses(); auto nClasses = dataset.getNClasses();
line << setw(3) << right << nClasses << " "; line << setw(header_lengths[5]) << right << nClasses << " ";
std::string sep = ""; std::string sep = "";
oss.str(""); oss.str("");
for (auto number : dataset.getClassesCounts()) { for (auto number : dataset.getClassesCounts()) {

View File

@@ -2,7 +2,7 @@
#include <locale> #include <locale>
#include "best/BestScore.h" #include "best/BestScore.h"
#include "common/CLocale.h" #include "common/CLocale.h"
#include "common/Timer.h" #include "common/Timer.hpp"
#include "ReportConsole.h" #include "ReportConsole.h"
#include "main/Scores.h" #include "main/Scores.h"
@@ -49,7 +49,8 @@ namespace platform {
oss << "Execution took " << timer.translate2String(data["duration"].get<float>()) oss << "Execution took " << timer.translate2String(data["duration"].get<float>())
<< " on " << data["platform"].get<std::string>() << " Language: " << data["language"].get<std::string>(); << " on " << data["platform"].get<std::string>() << " Language: " << data["language"].get<std::string>();
sheader << headerLine(oss.str()); sheader << headerLine(oss.str());
sheader << headerLine("Score is " + data["score_name"].get<std::string>()); std::string schema_version = data.find("schema_version") != data.end() ? data["schema_version"].get<std::string>() : "-";
sheader << headerLine("Score is " + data["score_name"].get<std::string>() + " Schema version: " + schema_version);
sheader << std::string(MAXL, '*') << std::endl; sheader << std::string(MAXL, '*') << std::endl;
sheader << std::endl; sheader << std::endl;
} }
@@ -83,7 +84,7 @@ namespace platform {
} }
std::vector<std::string> header_labels = { " #", "Dataset", "Sampl.", "Feat.", "Cls", nodes_label, leaves_label, depth_label, "Score", "Time", "Hyperparameters" }; std::vector<std::string> header_labels = { " #", "Dataset", "Sampl.", "Feat.", "Cls", nodes_label, leaves_label, depth_label, "Score", "Time", "Hyperparameters" };
sheader << Colors::GREEN(); sheader << Colors::GREEN();
std::vector<int> header_lengths = { 3, maxDataset, 6, 5, 3, 9, 9, 9, 15, 20, maxHyper }; std::vector<int> header_lengths = { 3, maxDataset, 6, 6, 3, 13, 13, 13, 15, 20, maxHyper };
for (int i = 0; i < header_labels.size(); i++) { for (int i = 0; i < header_labels.size(); i++) {
sheader << std::setw(header_lengths[i]) << std::left << header_labels[i] << " "; sheader << std::setw(header_lengths[i]) << std::left << header_labels[i] << " ";
} }
@@ -107,11 +108,11 @@ namespace platform {
line << std::setw(3) << std::right << index++ << " "; line << std::setw(3) << std::right << index++ << " ";
line << std::setw(maxDataset) << std::left << r["dataset"].get<std::string>() << " "; line << std::setw(maxDataset) << std::left << r["dataset"].get<std::string>() << " ";
line << std::setw(6) << std::right << r["samples"].get<int>() << " "; line << std::setw(6) << std::right << r["samples"].get<int>() << " ";
line << std::setw(5) << std::right << r["features"].get<int>() << " "; line << std::setw(6) << std::right << r["features"].get<int>() << " ";
line << std::setw(3) << std::right << r["classes"].get<int>() << " "; line << std::setw(3) << std::right << r["classes"].get<int>() << " ";
line << std::setw(9) << std::setprecision(2) << std::fixed << r["nodes"].get<float>() << " "; line << std::setw(13) << std::setprecision(2) << std::fixed << r["nodes"].get<float>() << " ";
line << std::setw(9) << std::setprecision(2) << std::fixed << r["leaves"].get<float>() << " "; line << std::setw(13) << std::setprecision(2) << std::fixed << r["leaves"].get<float>() << " ";
line << std::setw(9) << std::setprecision(2) << std::fixed << r["depth"].get<float>() << " "; line << std::setw(13) << std::setprecision(2) << std::fixed << r["depth"].get<float>() << " ";
line << std::setw(8) << std::right << std::setprecision(6) << std::fixed << r["score"].get<double>() << "±" << std::setw(6) << std::setprecision(4) << std::fixed << r["score_std"].get<double>(); line << std::setw(8) << std::right << std::setprecision(6) << std::fixed << r["score"].get<double>() << "±" << std::setw(6) << std::setprecision(4) << std::fixed << r["score_std"].get<double>();
const std::string status = compareResult(r["dataset"].get<std::string>(), r["score"].get<double>()); const std::string status = compareResult(r["dataset"].get<std::string>(), r["score"].get<double>());
line << status; line << status;
@@ -223,7 +224,7 @@ namespace platform {
std::string ReportConsole::buildClassificationReport(json& result, std::string color) std::string ReportConsole::buildClassificationReport(json& result, std::string color)
{ {
std::stringstream oss; std::stringstream oss;
if (result.find("confusion_matrices") == result.end()) if (result.find("confusion_matrices") == result.end() || result["confusion_matrices"].size() == 0)
return ""; return "";
bool second_header = false; bool second_header = false;
int lines_header = 0; int lines_header = 0;

137
src/results/JsonValidator.h Normal file
View File

@@ -0,0 +1,137 @@
#ifndef JSONVALIDATOR_H
#define JSONVALIDATOR_H
#include <fstream>
#include <vector>
#include <regex>
#include <nlohmann/json.hpp>
namespace platform {
using json = nlohmann::ordered_json;
class JsonValidator {
public:
JsonValidator(const json& schema) : schema(schema) {}
std::vector<std::string> validate_file(const std::string& fileName)
{
auto data = load_json_file(fileName);
return validate(data);
}
std::vector<std::string> validate(const json& data)
{
std::vector<std::string> errors;
// Validate the top-level object
validateObject("", schema, data, errors);
return errors;
}
json load_json_file(const std::string& fileName)
{
std::ifstream file(fileName);
if (!file.is_open()) {
throw std::runtime_error("Error: Unable to open file " + fileName);
}
json data;
file >> data;
file.close();
return data;
}
void fix_it(const std::string& fileName)
{
// Load JSON file
auto data = load_json_file(fileName);
// Fix fields
for (const auto& [key, value] : schema["properties"].items()) {
if (!data.contains(key)) {
// Set default value if specified in the schema
if (value.contains("default")) {
data[key] = value["default"];
} else if (value["type"] == "array") {
data[key] = json::array();
} else if (value["type"] == "object") {
data[key] = json::object();
} else {
data[key] = nullptr;
}
}
// Fix const fields to match the schema value
if (value.contains("const")) {
data[key] = value["const"];
}
}
// Save fixed JSON
std::ofstream outFile(fileName);
if (!outFile.is_open()) {
std::cerr << "Error: Unable to open file for writing." << std::endl;
return;
}
outFile << data.dump(4);
outFile.close();
}
private:
json schema;
void validateObject(const std::string& path, const json& schema, const json& data, std::vector<std::string>& errors)
{
if (schema.contains("required")) {
for (const auto& requiredField : schema["required"]) {
if (!data.contains(requiredField)) {
std::string fullPath = path.empty() ? requiredField.get<std::string>() : path + "." + requiredField.get<std::string>();
errors.push_back("Missing required field: " + fullPath);
}
}
}
if (schema.contains("properties")) {
for (const auto& [key, value] : schema["properties"].items()) {
if (data.contains(key)) {
std::string fullPath = path.empty() ? key : path + "." + key;
validateField(fullPath, value, data[key], errors); // Pass data[key] for nested validation
} else if (value.contains("required")) {
errors.push_back("Missing required field: " + (path.empty() ? key : path + "." + key));
}
}
}
}
void validateField(const std::string& field, const json& value, const json& data, std::vector<std::string>& errors)
{
if (value.contains("type")) {
const std::string& type = value["type"];
if (type == "array") {
if (!data.is_array()) {
errors.push_back("Field '" + field + "' should be an array.");
return;
}
if (value.contains("items")) {
for (size_t i = 0; i < data.size(); ++i) {
validateObject(field + "[" + std::to_string(i) + "]", value["items"], data[i], errors);
}
}
} else if (type == "object") {
if (!data.is_object()) {
errors.push_back("Field '" + field + "' should be an object.");
return;
}
validateObject(field, value, data, errors);
} else if (type == "string" && !data.is_string()) {
errors.push_back("Field '" + field + "' should be a string.");
} else if (type == "number" && !data.is_number()) {
errors.push_back("Field '" + field + "' should be a number.");
} else if (type == "integer" && !data.is_number_integer()) {
errors.push_back("Field '" + field + "' should be an integer.");
} else if (type == "boolean" && !data.is_boolean()) {
errors.push_back("Field '" + field + "' should be a boolean.");
}
}
if (value.contains("const")) {
const auto& expectedValue = value["const"];
if (data != expectedValue) {
errors.push_back("Field '" + field + "' has an invalid value. Expected: " +
expectedValue.dump() + ", Found: " + data.dump());
}
}
}
};
}
#endif

View File

@@ -8,6 +8,8 @@
#include "common/Paths.h" #include "common/Paths.h"
#include "common/Symbols.h" #include "common/Symbols.h"
#include "Result.h" #include "Result.h"
#include "JsonValidator.h"
#include "SchemaV1_0.h"
namespace platform { namespace platform {
std::string get_actual_date() std::string get_actual_date()
@@ -62,7 +64,11 @@ namespace platform {
{ {
return data; return data;
} }
std::vector<std::string> Result::check()
{
platform::JsonValidator validator(platform::SchemaV1_0::schema);
return validator.validate(data);
}
void Result::save() void Result::save()
{ {
std::ofstream file(Paths::results() + getFilename()); std::ofstream file(Paths::results() + getFilename());

View File

@@ -4,7 +4,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "common/Timer.h" #include "common/Timer.hpp"
#include "main/HyperParameters.h" #include "main/HyperParameters.h"
#include "main/PartialResult.h" #include "main/PartialResult.h"
@@ -16,6 +16,7 @@ namespace platform {
Result(); Result();
Result& load(const std::string& path, const std::string& filename); Result& load(const std::string& path, const std::string& filename);
void save(); void save();
std::vector<std::string> check();
// Getters // Getters
json getJson(); json getJson();
std::string to_string(int maxModel, int maxTitle) const; std::string to_string(int maxModel, int maxTitle) const;
@@ -28,7 +29,7 @@ namespace platform {
std::string getModel() const { return data["model"].get<std::string>(); }; std::string getModel() const { return data["model"].get<std::string>(); };
std::string getPlatform() const { return data["platform"].get<std::string>(); }; std::string getPlatform() const { return data["platform"].get<std::string>(); };
std::string getScoreName() const { return data["score_name"].get<std::string>(); }; std::string getScoreName() const { return data["score_name"].get<std::string>(); };
void setSchemaVersion(const std::string& version) { data["schema_version"] = version; };
bool isComplete() const { return complete; }; bool isComplete() const { return complete; };
json getData() const { return data; } json getData() const { return data; }
// Setters // Setters

103
src/results/SchemaV1_0.h Normal file
View File

@@ -0,0 +1,103 @@
#ifndef SCHEMAV1_0_H
#define SCHEMAV1_0_H
#include <nlohmann/json.hpp>
namespace platform {
using json = nlohmann::ordered_json;
class SchemaV1_0 {
public:
// Define JSON schema
const static json schema;
};
const json SchemaV1_0::schema = {
{"$schema", "http://json-schema.org/draft-07/schema#"},
{"type", "object"},
{"properties", {
{"schema_version", {
{"type", "string"},
{"pattern", "^\\d+\\.\\d+$"},
{"default", "1.0"},
{"const", "1.0"} // Fixed schema version for this schema
}},
{"date", {{"type", "string"}, {"format", "date"}}},
{"time", {{"type", "string"}, {"pattern", "^\\d{2}:\\d{2}:\\d{2}$"}}},
{"title", {{"type", "string"}}},
{"language", {{"type", "string"}}},
{"language_version", {{"type", "string"}}},
{"discretized", {{"type", "boolean"}, {"default", false}}},
{"model", {{"type", "string"}}},
{"platform", {{"type", "string"}}},
{"stratified", {{"type", "boolean"}, {"default", false}}},
{"folds", {{"type", "integer"}, {"default", 0}}},
{"score_name", {{"type", "string"}}},
{"version", {{"type", "string"}}},
{"duration", {{"type", "number"}, {"default", 0}}},
{"results", {
{"type", "array"},
{"items", {
{"type", "object"},
{"properties", {
{"scores_train", {{"type", "array"}, {"items", {{"type", "number"}}}}},
{"scores_test", {{"type", "array"}, {"items", {{"type", "number"}}}}},
{"times_train", {{"type", "array"}, {"items", {{"type", "number"}}}}},
{"times_test", {{"type", "array"}, {"items", {{"type", "number"}}}}},
{"notes", {{"type", "array"}, {"items", {{"type", "string"}}}}},
{"train_time", {{"type", "number"}, {"default", 0}}},
{"train_time_std", {{"type", "number"}, {"default", 0}}},
{"test_time", {{"type", "number"}, {"default", 0}}},
{"test_time_std", {{"type", "number"}, {"default", 0}}},
{"samples", {{"type", "integer"}, {"default", 0}}},
{"features", {{"type", "integer"}, {"default", 0}}},
{"classes", {{"type", "integer"}, {"default", 0}}},
{"hyperparameters", {
{"type", "object"},
{"additionalProperties", {
{"oneOf", {
{{"type", "number"}}, // Field can be a number
{{"type", "string"}} // Field can also be a string
}}
}}
}},
{"score", {{"type", "number"}, {"default", 0}}},
{"score_train", {{"type", "number"}, {"default", 0}}},
{"score_std", {{"type", "number"}, {"default", 0}}},
{"score_train_std", {{"type", "number"}, {"default", 0}}},
{"time", {{"type", "number"}, {"default", 0}}},
{"time_std", {{"type", "number"}, {"default", 0}}},
{"nodes", {{"type", "number"}, {"default", 0}}},
{"leaves", {{"type", "number"}, {"default", 0}}},
{"depth", {{"type", "number"}, {"default", 0}}},
{"dataset", {{"type", "string"}}},
{"confusion_matrices", {
{"type", "array"},
{"items", {
{"type", "object"},
{"patternProperties", {
{".*", {
{"type", "array"},
{"items", {{"type", "integer"}}}
}}
}},
{"additionalProperties", false}
}}
}}
}},
{"required", {
"scores_train", "scores_test", "times_train", "times_test",
"train_time", "train_time_std", "test_time", "test_time_std",
"samples", "features", "classes", "hyperparameters", "score", "score_train",
"score_std", "score_train_std", "time", "time_std", "nodes", "leaves",
"depth", "dataset"
}}
}}
}}
}},
{"required", {
"schema_version", "date", "time", "title", "language", "language_version",
"discretized", "model", "platform", "stratified", "folds", "score_name",
"version", "duration", "results"
}}
};
}
#endif

21
vcpkg-configuration.json Normal file
View 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"
]
}
]
}

38
vcpkg.json Normal file
View File

@@ -0,0 +1,38 @@
{
"name": "platform",
"version-string": "1.1.0",
"dependencies": [
"arff-files",
"nlohmann-json",
"fimdlp",
"libtorch-bin",
"folding",
"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": "argpase",
"version": "3.2"
},
{
"name": "nlohmann-json",
"version": "3.11.3"
}
]
}