Compare commits
29 Commits
alphablock
...
17728212c1
Author | SHA1 | Date | |
---|---|---|---|
17728212c1
|
|||
86b4558f9d
|
|||
505edc79ac
|
|||
73a4b3d5e5
|
|||
cbe8f4c79c
|
|||
0d08a526fa
|
|||
d0706da887
|
|||
07e3cc9599
|
|||
2a9652b450
|
|||
3397d0962f
|
|||
7aaf6d1bf8
|
|||
eb430a84c4
|
|||
d0e65348e0
|
|||
c1d5dd74e3
|
|||
9a9a9fb17a
|
|||
386faf960e
|
|||
28894004c8
|
|||
ae41975fb4
|
|||
0e475e4488
|
|||
909cec712c
|
|||
4901bb1f32
|
|||
0318dcf8e5
|
|||
1cc19a7b19
|
|||
f88944de36
|
|||
1a336a094e
|
|||
8705adf3ee
|
|||
017cb8a0dc
|
|||
e966c880e6
|
|||
70ea32dc9a
|
@@ -15,7 +15,7 @@ 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)
|
||||||
@@ -90,7 +90,7 @@ cmake_path(SET TEST_DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests/data")
|
|||||||
configure_file(src/common/SourceData.h.in "${CMAKE_BINARY_DIR}/configured_files/include/SourceData.h")
|
configure_file(src/common/SourceData.h.in "${CMAKE_BINARY_DIR}/configured_files/include/SourceData.h")
|
||||||
add_subdirectory(config)
|
add_subdirectory(config)
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
# add_subdirectory(sample)
|
add_subdirectory(sample)
|
||||||
file(GLOB Platform_SOURCES CONFIGURE_DEPENDS ${Platform_SOURCE_DIR}/src/*.cpp)
|
file(GLOB Platform_SOURCES CONFIGURE_DEPENDS ${Platform_SOURCE_DIR}/src/*.cpp)
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
|
6
Makefile
6
Makefile
@@ -4,7 +4,7 @@ SHELL := /bin/bash
|
|||||||
|
|
||||||
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
|
||||||
@@ -55,10 +55,10 @@ 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
|
clean: ## Clean the tests info
|
||||||
@echo ">>> Cleaning Debug Platform tests...";
|
@echo ">>> Cleaning Debug Platform tests...";
|
||||||
|
@@ -40,7 +40,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
|
||||||
|
@@ -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()
|
||||||
|
|
||||||
|
Submodule lib/Files updated: a4329f5f9d...18c79f6d48
Submodule lib/catch2 updated: 0321d2fce3...914aeecfe2
Submodule lib/folding updated: 2ac43e32ac...9652853d69
2
lib/json
2
lib/json
Submodule lib/json updated: 620034ecec...48e7b4c23b
Submodule lib/libxlsxwriter updated: 8206bda64a...14f13513cb
@@ -226,7 +226,7 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
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();
|
||||||
}
|
}
|
||||||
total_score_train += score_train;
|
total_score_train += score_train;
|
||||||
total_score += score_test;
|
total_score += score_test;
|
||||||
|
@@ -29,11 +29,13 @@ add_executable(
|
|||||||
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}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} 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
|
||||||
)
|
)
|
||||||
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}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy)
|
||||||
|
|
||||||
@@ -47,7 +49,7 @@ add_executable(b_list commands/b_list.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}" fimdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} 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
|
||||||
@@ -67,3 +69,6 @@ add_executable(
|
|||||||
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}")
|
||||||
|
|
||||||
|
# b_results
|
||||||
|
add_executable(b_results commands/b_results.cpp)
|
||||||
|
@@ -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) {
|
||||||
|
@@ -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;
|
||||||
|
@@ -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>();
|
||||||
|
@@ -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;
|
||||||
|
@@ -16,7 +16,8 @@ void manageArguments(argparse::ArgumentParser& program)
|
|||||||
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);
|
||||||
@@ -38,7 +39,7 @@ 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;
|
||||||
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);
|
||||||
@@ -48,6 +49,7 @@ int main(int argc, char** argv)
|
|||||||
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");
|
||||||
@@ -67,7 +69,7 @@ int main(int argc, char** argv)
|
|||||||
auto results = platform::BestResults(platform::Paths::results(), score, model, dataset, friedman, level);
|
auto results = platform::BestResults(platform::Paths::results(), 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;
|
||||||
|
@@ -6,11 +6,13 @@
|
|||||||
#include <mpi.h>
|
#include <mpi.h>
|
||||||
#include "main/Models.h"
|
#include "main/Models.h"
|
||||||
#include "main/modelRegister.h"
|
#include "main/modelRegister.h"
|
||||||
|
#include "main/ArgumentsExperiment.h"
|
||||||
#include "common/Paths.h"
|
#include "common/Paths.h"
|
||||||
#include "common/Timer.h"
|
#include "common/Timer.h"
|
||||||
#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 +33,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 +140,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 +156,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 +189,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 +208,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 +218,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 +275,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 +297,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 +306,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) {
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
|
102
src/commands/b_results.cpp
Normal file
102
src/commands/b_results.cpp
Normal 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;
|
||||||
|
}
|
@@ -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
|
||||||
|
315
src/grid/GridBase.cpp
Normal file
315
src/grid/GridBase.cpp
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
#include <random>
|
||||||
|
#include <cstddef>
|
||||||
|
#include "common/DotEnv.h"
|
||||||
|
#include "common/Paths.h"
|
||||||
|
#include "common/DotEnv.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 grid = GridData(Paths::grid_input(config.model));
|
||||||
|
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_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
43
src/grid/GridBase.h
Normal file
43
src/grid/GridBase.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#ifndef GRIDBASE_H
|
||||||
|
#define GRIDBASE_H
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <mpi.h>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include "common/Datasets.h"
|
||||||
|
#include "common/Timer.h"
|
||||||
|
#include "common/Colors.h"
|
||||||
|
#include "main/HyperParameters.h"
|
||||||
|
#include "GridData.h"
|
||||||
|
#include "GridConfig.h"
|
||||||
|
#include "bayesnet/network/Network.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
55
src/grid/GridConfig.h
Normal 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.h"
|
||||||
|
#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
196
src/grid/GridExperiment.cpp
Normal 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 */
|
42
src/grid/GridExperiment.h
Normal file
42
src/grid/GridExperiment.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef GRIDEXPERIMENT_H
|
||||||
|
#define GRIDEXPERIMENT_H
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <mpi.h>
|
||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include "common/Datasets.h"
|
||||||
|
#include "common/DotEnv.h"
|
||||||
|
#include "main/Experiment.h"
|
||||||
|
#include "main/HyperParameters.h"
|
||||||
|
#include "main/ArgumentsExperiment.h"
|
||||||
|
#include "GridData.h"
|
||||||
|
#include "GridBase.h"
|
||||||
|
#include "bayesnet/network/Network.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
|
@@ -4,18 +4,11 @@
|
|||||||
#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 "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);
|
||||||
}
|
}
|
||||||
|
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 */
|
} /* namespace platform */
|
@@ -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.h"
|
||||||
#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
|
225
src/main/ArgumentsExperiment.cpp
Normal file
225
src/main/ArgumentsExperiment.cpp
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
#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;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
39
src/main/ArgumentsExperiment.h
Normal file
39
src/main/ArgumentsExperiment.h
Normal 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
|
@@ -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,18 @@ 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) {
|
// Sort files to test to have a consistent order even if --datasets is used
|
||||||
|
std::stable_sort(filesToTest.begin(), filesToTest.end());
|
||||||
|
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 +117,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 +160,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
|
||||||
@@ -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
|
||||||
//
|
//
|
||||||
|
@@ -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 };
|
||||||
|
@@ -257,8 +257,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 +312,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 +400,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},
|
||||||
@@ -497,6 +527,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 +576,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 +583,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());
|
||||||
|
@@ -27,6 +27,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();
|
||||||
|
@@ -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()) {
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
@@ -250,7 +251,7 @@ namespace platform {
|
|||||||
if (train_data) {
|
if (train_data) {
|
||||||
oss << color_line << std::left << std::setw(maxLine) << output_train[i]
|
oss << color_line << std::left << std::setw(maxLine) << output_train[i]
|
||||||
<< suffix << Colors::BLUE() << " | " << color_line << std::left << std::setw(maxLine)
|
<< suffix << Colors::BLUE() << " | " << color_line << std::left << std::setw(maxLine)
|
||||||
<< output_test[i] << std::endl;
|
<< output_test[i] << std::endl;
|
||||||
} else {
|
} else {
|
||||||
oss << color_line << output_test[i] << std::endl;
|
oss << color_line << output_test[i] << std::endl;
|
||||||
}
|
}
|
||||||
|
137
src/results/JsonValidator.h
Normal file
137
src/results/JsonValidator.h
Normal 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
|
@@ -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());
|
||||||
|
@@ -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
103
src/results/SchemaV1_0.h
Normal 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
|
Reference in New Issue
Block a user