From da8d018ec48afd714fb38c6ac8ae8d3e67ef9a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Thu, 5 Oct 2023 11:45:00 +0200 Subject: [PATCH] Refactor Makefile --- Makefile | 46 ++++++++++++++++++++------------------- lib/libxlsxwriter | 2 +- tests/TestBayesMetrics.cc | 26 +++++++--------------- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 85870e4..3b4d7b6 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SHELL := /bin/bash .DEFAULT_GOAL := help -.PHONY: coverage setup help build test +.PHONY: coverage setup help build test clean debug release setup: ## Install dependencies for tests and coverage @if [ "$(shell uname)" = "Darwin" ]; then \ @@ -28,45 +28,47 @@ build: ## Build the main and BayesNetSample cmake --build build -t b_main -t BayesNetSample -t b_manage -t b_list -t b_best -j 32 clean: ## Clean the debug info - @echo ">>> Cleaning Debug BayesNet ..."; - find . -name "*.gcda" -print0 | xargs -0 rm + @echo ">>> Cleaning Debug BayesNet..."; + $(call ClearTests) @echo ">>> Done"; clang-uml: ## Create uml class and sequence diagrams clang-uml -p --add-compile-flag -I /usr/lib/gcc/x86_64-redhat-linux/8/include/ debug: ## Build a debug version of the project - @echo ">>> Building Debug BayesNet ..."; + @echo ">>> Building Debug BayesNet..."; @if [ -d ./build ]; then rm -rf ./build; fi @mkdir build; - cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON; \ - cmake --build build -t b_main -t BayesNetSample -t b_manage -t b_list -t b_best -t unit_tests -j 32; + @cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON; @echo ">>> Done"; release: ## Build a Release version of the project - @echo ">>> Building Release BayesNet ..."; + @echo ">>> Building Release BayesNet..."; @if [ -d ./build ]; then rm -rf ./build; fi @mkdir build; - cmake -S . -B build -D CMAKE_BUILD_TYPE=Release; \ - cmake --build build -t b_main -t BayesNetSample -t b_manage -t b_list -t b_best -j 32; + @cmake -S . -B build -D CMAKE_BUILD_TYPE=Release; @echo ">>> Done"; test: ## Run tests - @echo "* Running tests..."; - find . -name "*.gcda" -print0 | xargs -0 rm - @cd build; \ - cmake --build . --target unit_tests ; - @cd build/tests; \ - ./unit_tests; + @echo ">>> Running tests..."; + $(MAKE) clean + @cmake --build build --target unit_tests ; + @if [ -f build/tests/unit_tests ]; then cd build/tests ; ./unit_tests ; fi ; + @echo ">>> Done"; coverage: ## Run tests and generate coverage report (build/index.html) - @echo "*Building tests..."; - find . -name "*.gcda" -print0 | xargs -0 rm - @cd build; \ - cmake --build . --target unit_tests ; - @cd build/tests; \ - ./unit_tests; - gcovr ; + @echo ">>> Building tests with coverage..."; + $(MAKE) test + @gcovr + @echo ">>> Done"; + +define ClearTests = + $(eval nfiles=$(find . -name "*.gcda" -print)) + @if [ -f build/tests/unit_tests ]; then rm -f build/tests/unit_tests ; fi ; + @if test "${nfiles}" != "" ; then \ + find . -name "*.gcda" -print0 | xargs -0 rm 2>/dev/null ;\ + fi ; +endef help: ## Show help message @IFS=$$'\n' ; \ diff --git a/lib/libxlsxwriter b/lib/libxlsxwriter index 44e72c5..29355a0 160000 --- a/lib/libxlsxwriter +++ b/lib/libxlsxwriter @@ -1 +1 @@ -Subproject commit 44e72c5862f9d549453a4ff6e8ceab0da19705e5 +Subproject commit 29355a0887475488c7cc470ad43cc867fcfa92e2 diff --git a/tests/TestBayesMetrics.cc b/tests/TestBayesMetrics.cc index f7adf9e..26eea82 100644 --- a/tests/TestBayesMetrics.cc +++ b/tests/TestBayesMetrics.cc @@ -9,7 +9,7 @@ using namespace std; TEST_CASE("Metrics Test", "[Metrics]") { string file_name = GENERATE("glass", "iris", "ecoli", "diabetes"); - map>> results = { + map>> resultsKBest = { {"glass", {7, { 3, 2, 0, 1, 6, 7, 5 }}}, {"iris", {3, { 1, 0, 2 }} }, {"ecoli", {6, { 2, 3, 1, 0, 4, 5 }}}, @@ -31,26 +31,16 @@ TEST_CASE("Metrics Test", "[Metrics]") { bayesnet::Metrics metrics(XDisc, featuresDisc, classNameDisc, classNumStates); torch::Tensor weights = torch::full({ nSamples }, 1.0 / nSamples, torch::kDouble); - vector kBest = metrics.SelectKBestWeighted(weights, true, results.at(file_name).first); - REQUIRE(kBest.size() == results.at(file_name).first); - REQUIRE(kBest == results.at(file_name).second); + vector kBest = metrics.SelectKBestWeighted(weights, true, resultsKBest.at(file_name).first); + REQUIRE(kBest.size() == resultsKBest.at(file_name).first); + REQUIRE(kBest == resultsKBest.at(file_name).second); } SECTION("Test mutualInformation") { - // torch::Tensor samples = torch::rand({ 10, 5 }); - // vector features = { "feature1", "feature2", "feature3", "feature4", "feature5" }; - // string className = "class1"; - // int classNumStates = 2; - - // bayesnet::Metrics obj(samples, features, className, classNumStates); - - // torch::Tensor firstFeature = samples.select(1, 0); - // torch::Tensor secondFeature = samples.select(1, 1); - // torch::Tensor weights = torch::ones({ 10 }); - - // double mi = obj.mutualInformation(firstFeature, secondFeature, weights); - - // REQUIRE(mi >= 0); + bayesnet::Metrics metrics(XDisc, featuresDisc, classNameDisc, classNumStates); + torch::Tensor weights = torch::full({ nSamples }, 1.0 / nSamples, torch::kDouble); + auto result = metrics.mutualInformation(dataset.index({ 1, "..." }), dataset.index({ 2, "..." }), weights); + REQUIRE(result == 0.87); } } \ No newline at end of file