From ca833a34f5835cb085c1ead9c9dd4f48edc435c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Tue, 10 Oct 2023 18:16:43 +0200 Subject: [PATCH] try openssl sha256 --- CMakeLists.txt | 2 +- src/BayesNet/BoostAODE.cc | 58 ++++++++++++++++++++++++++++++------- src/BayesNet/BoostAODE.h | 2 +- src/BayesNet/CMakeLists.txt | 2 +- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 294f0bf..37c674d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,7 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") # CMakes modules # -------------- set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH}) - +find_package(OpenSSL REQUIRED) include(AddGitSubmodule) if (CODE_COVERAGE) enable_testing() diff --git a/src/BayesNet/BoostAODE.cc b/src/BayesNet/BoostAODE.cc index aeae235..d3e8901 100644 --- a/src/BayesNet/BoostAODE.cc +++ b/src/BayesNet/BoostAODE.cc @@ -1,10 +1,12 @@ -#include "BoostAODE.h" #include +#include +#include +#include "BoostAODE.h" #include "BayesMetrics.h" #include "Colors.h" #include "Folding.h" -#include #include "Paths.h" +#include namespace bayesnet { BoostAODE::BoostAODE() : Ensemble() {} @@ -13,6 +15,8 @@ namespace bayesnet { // Models shall be built in trainModel // Prepare the validation dataset auto y_ = dataset.index({ -1, "..." }); + int nSamples = dataset.size(1); + int nFeatures = dataset.size(0) - 1; if (convergence) { // Prepare train & validation sets from train data auto fold = platform::StratifiedKFold(5, y_, 271); @@ -38,7 +42,7 @@ namespace bayesnet { y_train = y_; } if (cfs != "") { - initializeModels(); + initializeModels(nSamples, nFeatures); } } void BoostAODE::setHyperparameters(nlohmann::json& hyperparameters) @@ -62,18 +66,52 @@ namespace bayesnet { cfs = hyperparameters["cfs"]; } } - void BoostAODE::initializeModels() + string sha256(const string& input) { - ifstream file(cfs + ".json"); + EVP_MD_CTX* mdctx; + const EVP_MD* md; + unsigned char hash[EVP_MAX_MD_SIZE]; + unsigned int hash_len; + + OpenSSL_add_all_digests(); + md = EVP_get_digestbyname("sha256"); + mdctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(mdctx, md, nullptr); + EVP_DigestUpdate(mdctx, input.c_str(), input.size()); + EVP_DigestFinal_ex(mdctx, hash, &hash_len); + EVP_MD_CTX_free(mdctx); + stringstream oss; + for (unsigned int i = 0; i < hash_len; i++) { + oss << hex << (int)hash[i]; + } + return oss.str(); + } + + void BoostAODE::initializeModels(int nSamples, int nFeatures) + { + // Read the CFS features + string output = "[", prefix = ""; + bool first = true; + for (const auto& feature : features) { + output += prefix + feature; + if (first) { + prefix = ", "; + first = false; + } + } + output += "]"; + // std::size_t str_hash = std::hash{}(output); + string str_hash = sha256(output); + stringstream oss; + oss << "cfs/" << str_hash << ".json"; + string name = oss.str(); + ifstream file(name); if (file.is_open()) { - nlohmann::json data; - file >> data; + nlohmann::json features = nlohmann::json::parse(file); file.close(); - auto model = "iris"; // has to come in when building object - auto features = data[model]; cout << "features: " << features.dump() << endl; } else { - throw runtime_error("File " + cfs + ".json not found"); + throw runtime_error("File " + name + " not found"); } } void BoostAODE::trainModel(const torch::Tensor& weights) diff --git a/src/BayesNet/BoostAODE.h b/src/BayesNet/BoostAODE.h index f3fa5bd..3464a7d 100644 --- a/src/BayesNet/BoostAODE.h +++ b/src/BayesNet/BoostAODE.h @@ -15,7 +15,7 @@ namespace bayesnet { private: torch::Tensor dataset_; torch::Tensor X_train, y_train, X_test, y_test; - void initializeModels(); + void initializeModels(int nSamples, int nFeatures); // Hyperparameters bool repeatSparent = false; // if true, a feature can be selected more than once int maxModels = 0; diff --git a/src/BayesNet/CMakeLists.txt b/src/BayesNet/CMakeLists.txt index 2a120f3..6ca1238 100644 --- a/src/BayesNet/CMakeLists.txt +++ b/src/BayesNet/CMakeLists.txt @@ -6,4 +6,4 @@ include_directories(${BayesNet_SOURCE_DIR}/src/Platform) add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANLd.cc KDBLd.cc SPODELd.cc AODELd.cc BoostAODE.cc Mst.cc Proposal.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc) -target_link_libraries(BayesNet mdlp "${TORCH_LIBRARIES}") \ No newline at end of file +target_link_libraries(BayesNet mdlp "${TORCH_LIBRARIES}" OpenSSL::Crypto) \ No newline at end of file