Refactor library structure and add sample

This commit is contained in:
Ricardo Montañana Gómez 2024-02-27 13:06:13 +01:00
parent f10d0daf2e
commit 903b143338
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
49 changed files with 104 additions and 8 deletions

View File

@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Change _ascending_ hyperparameter to _order_ with these possible values _{"asc", "desc", "rand"}_, Default is _"desc"_.
- Add the _predict_single_ hyperparameter to control if only the last model created is used to predict in boost training or the whole ensemble (all the models built so far). Default is true.
- sample app to show how to use the library (make sample)
### Changed
- Change the library structure adding folders for each group of classes (classifiers, ensembles, etc).
## [1.0.3]

View File

@ -65,10 +65,9 @@ add_git_submodule("lib/json")
# --------------
add_subdirectory(config)
add_subdirectory(lib/Files)
add_subdirectory(sample)
add_subdirectory(src)
file(GLOB BayesNet_SOURCES CONFIGURE_DEPENDS ${BayesNet_SOURCE_DIR}/src/*.cc)
# Testing
# -------
if (ENABLE_TESTING)

View File

@ -1,11 +1,11 @@
SHELL := /bin/bash
.DEFAULT_GOAL := help
.PHONY: coverage setup help buildr buildd test clean debug release
.PHONY: coverage setup help buildr buildd test clean debug release sample
f_release = build_release
f_debug = build_debug
app_targets = BayesNet
test_targets = unit_tests_bayesnet
test_targets = unit_tests_bayesnet
n_procs = -j 16
define ClearTests
@ -59,6 +59,12 @@ release: ## Build a Release version of the project
@if [ -d ./$(f_release) ]; then rm -rf ./$(f_release); fi
@mkdir $(f_release);
@cmake -S . -B $(f_release) -D CMAKE_BUILD_TYPE=Release
@echo ">>> Done";
sample: ## Build sample
@echo ">>> Building Sample...";
cmake --build $(f_release) -t bayesnet_sample $(n_procs)
$(f_release)/sample/bayesnet_sample tests/data/iris.arff
@echo ">>> Done";
opt = ""

14
sample/CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
include_directories(
${BayesNet_SOURCE_DIR}/src
${BayesNet_SOURCE_DIR}/src/classifiers
${BayesNet_SOURCE_DIR}/src/ensembles
${BayesNet_SOURCE_DIR}/src/bayesian_network
${BayesNet_SOURCE_DIR}/src/utils
${BayesNet_SOURCE_DIR}/src/feature_selection
${BayesNet_SOURCE_DIR}/lib/Files
${BayesNet_SOURCE_DIR}/lib/mdlp
${BayesNet_SOURCE_DIR}/lib/json/include
${CMAKE_BINARY_DIR}/configured_files/include
)
add_executable(bayesnet_sample sample.cc)
target_link_libraries(bayesnet_sample ArffFiles BayesNet)

62
sample/sample.cc Normal file
View File

@ -0,0 +1,62 @@
#include "ArffFiles.h"
#include "CPPFImdlp.h"
#include "BoostAODE.h"
std::vector<mdlp::labels_t> discretizeDataset(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y)
{
std::vector<mdlp::labels_t> Xd;
auto fimdlp = mdlp::CPPFImdlp();
for (int i = 0; i < X.size(); i++) {
fimdlp.fit(X[i], y);
mdlp::labels_t& xd = fimdlp.transform(X[i]);
Xd.push_back(xd);
}
return Xd;
}
tuple<torch::Tensor, torch::Tensor, std::vector<std::string>, std::string, map<std::string, std::vector<int>>> loadDataset(const std::string& name, bool class_last)
{
auto handler = ArffFiles();
handler.load(name, class_last);
// Get Dataset X, y
std::vector<mdlp::samples_t>& X = handler.getX();
mdlp::labels_t& y = handler.getY();
// Get className & Features
auto className = handler.getClassName();
std::vector<std::string> features;
auto attributes = handler.getAttributes();
transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; });
torch::Tensor Xd;
auto states = map<std::string, std::vector<int>>();
auto Xr = discretizeDataset(X, y);
Xd = torch::zeros({ static_cast<int>(Xr.size()), static_cast<int>(Xr[0].size()) }, torch::kInt32);
for (int i = 0; i < features.size(); ++i) {
states[features[i]] = std::vector<int>(*max_element(Xr[i].begin(), Xr[i].end()) + 1);
auto item = states.at(features[i]);
iota(begin(item), end(item), 0);
Xd.index_put_({ i, "..." }, torch::tensor(Xr[i], torch::kInt32));
}
states[className] = std::vector<int>(*max_element(y.begin(), y.end()) + 1);
iota(begin(states.at(className)), end(states.at(className)), 0);
return { Xd, torch::tensor(y, torch::kInt32), features, className, states };
}
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <file_name>" << std::endl;
return 1;
}
std::string file_name = argv[1];
torch::Tensor X, y;
std::vector<std::string> features;
std::string className;
map<std::string, std::vector<int>> states;
auto clf = bayesnet::BoostAODE(false); // false for not using voting in predict
std::cout << "Library version: " << clf.getVersion() << std::endl;
tie(X, y, features, className, states) = loadDataset(file_name, true);
clf.fit(X, y, features, className, states);
auto score = clf.score(X, y);
std::cout << "File: " << file_name << " score: " << score << std::endl;
return 0;
}

View File

@ -4,10 +4,15 @@ include_directories(
${BayesNet_SOURCE_DIR}/lib/folding
${BayesNet_SOURCE_DIR}/lib/json/include
${BayesNet_SOURCE_DIR}/src
${BayesNet_SOURCE_DIR}/src/feature_selection
${BayesNet_SOURCE_DIR}/src/bayesian_network
${BayesNet_SOURCE_DIR}/src/classifiers
${BayesNet_SOURCE_DIR}/src/ensembles
${BayesNet_SOURCE_DIR}/src/utils
${CMAKE_BINARY_DIR}/configured_files/include
)
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 CFS.cc FCBF.cc IWSS.cc FeatureSelect.cc )
file(GLOB_RECURSE Sources "*.cc")
add_library(BayesNet ${Sources})
target_link_libraries(BayesNet mdlp "${TORCH_LIBRARIES}")

View File

@ -2,13 +2,18 @@ if(ENABLE_TESTING)
set(TEST_BAYESNET "unit_tests_bayesnet")
include_directories(
${BayesNet_SOURCE_DIR}/src
${BayesNet_SOURCE_DIR}/src/Platform
${BayesNet_SOURCE_DIR}/src/feature_selection
${BayesNet_SOURCE_DIR}/src/bayesian_network
${BayesNet_SOURCE_DIR}/src/classifiers
${BayesNet_SOURCE_DIR}/src/utils
${BayesNet_SOURCE_DIR}/src/ensembles
${BayesNet_SOURCE_DIR}/lib/Files
${BayesNet_SOURCE_DIR}/lib/mdlp
${BayesNet_SOURCE_DIR}/lib/folding
${BayesNet_SOURCE_DIR}/lib/json/include
${CMAKE_BINARY_DIR}/configured_files/include
)
file(GLOB_RECURSE BayesNet_SOURCES "${BayesNet_SOURCE_DIR}/src/*.cc")
set(TEST_SOURCES_BAYESNET TestBayesModels.cc TestBayesNetwork.cc TestBayesMetrics.cc TestUtils.cc ${BayesNet_SOURCES})
add_executable(${TEST_BAYESNET} ${TEST_SOURCES_BAYESNET})
target_link_libraries(${TEST_BAYESNET} PUBLIC "${TORCH_LIBRARIES}" ArffFiles mdlp Catch2::Catch2WithMain )