diff --git a/CHANGELOG.md b/CHANGELOG.md index c899645..7df00ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.1] 2024-12-13 + +- Added a new parameter `quiet` to enable/disable the warning messages in the Stratified K-Fold partitioning. Default value `true`. + ## [1.1.0] 2024-05-11 ### Fixed diff --git a/CMakeLists.txt b/CMakeLists.txt index cba1f1e..fa57206 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,6 @@ cmake_minimum_required(VERSION 3.20) project(Folding - VERSION 1.1.0 DESCRIPTION "Folding utility for BayesNet library" HOMEPAGE_URL "https://github.com/rmontanana/folding" LANGUAGES CXX @@ -33,7 +32,6 @@ include(AddGitSubmodule) # Subdirectories # -------------- -add_subdirectory(config) # Testing # ------- diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt deleted file mode 100644 index 09fd480..0000000 --- a/config/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -configure_file( - "config.h.in" - "${CMAKE_BINARY_DIR}/configured_files/include/folding_config.h" ESCAPE_QUOTES -) diff --git a/config/config.h.in b/config/config.h.in deleted file mode 100644 index 26937cf..0000000 --- a/config/config.h.in +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -#define PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR @ -#define PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR @ -#define PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH @ - -static constexpr std::string_view folding_project_name = "@PROJECT_NAME@"; -static constexpr std::string_view folding_project_version = "@PROJECT_VERSION@"; -static constexpr std::string_view folding_project_description = "@PROJECT_DESCRIPTION@"; -static constexpr std::string_view folding_data_path = "@Folding_SOURCE_DIR@/tests/data/"; -static constexpr std::string_view folding_csv_path = "@Folding_SOURCE_DIR@/tests/csv/"; diff --git a/folding.hpp b/folding.hpp index b0b40b2..91a381b 100644 --- a/folding.hpp +++ b/folding.hpp @@ -11,7 +11,7 @@ #include #include namespace folding { - const std::string FOLDING_VERSION = "1.1.0"; + const std::string FOLDING_VERSION = "1.1.1"; class Fold { public: inline Fold(int k, int n, int seed = -1) : k(k), n(n), seed(seed) @@ -59,16 +59,18 @@ namespace folding { }; class StratifiedKFold : public Fold { public: - inline StratifiedKFold(int k, const std::vector& y, int seed = -1) : Fold(k, y.size(), seed) + inline StratifiedKFold(int k, const std::vector& y, int seed = -1, bool quiet = true) : Fold(k, y.size(), seed) { this->y = y; n = y.size(); + this->quiet = quiet; build(); } - inline StratifiedKFold(int k, torch::Tensor& y, int seed = -1) : Fold(k, y.numel(), seed) + inline StratifiedKFold(int k, torch::Tensor& y, int seed = -1, bool quiet = true) : Fold(k, y.numel(), seed) { n = y.numel(); this->y = std::vector(y.data_ptr(), y.data_ptr() + n); + this->quiet = quiet; build(); } @@ -90,6 +92,7 @@ namespace folding { std::vector y; std::vector> stratified_indices; bool faulty = false; // Only true if the number of samples of any class is less than the number of folds. + bool quiet = true; // Enable or disable warning messages void build() { stratified_indices = std::vector>(k); @@ -105,7 +108,8 @@ namespace folding { int num_samples_to_take = num_samples / k; int remainder_samples_to_take = num_samples % k; if (num_samples_to_take == 0) { - std::cerr << "Warning! The number of samples in class " << label << " (" << num_samples + if (!quiet) + std::cerr << "Warning! The number of samples in class " << label << " (" << num_samples << ") is less than the number of folds (" << k << ")." << std::endl; faulty = true; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7a72841..25736c2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,12 +1,12 @@ if(ENABLE_TESTING) include_directories( ${Folding_SOURCE_DIR} - lib/Files - lib/mdlp ${CMAKE_BINARY_DIR}/configured_files/include + lib/Files + lib/mdlp/src ) set(TEST_FOLDING "unit_tests_folding") add_executable(${TEST_FOLDING} TestFolding.cc TestUtils.cc) - target_link_libraries(${TEST_FOLDING} PUBLIC "${TORCH_LIBRARIES}" ArffFiles mdlp Catch2::Catch2WithMain) + target_link_libraries(${TEST_FOLDING} PUBLIC "${TORCH_LIBRARIES}" ArffFiles fimdlp Catch2::Catch2WithMain) add_test(NAME ${TEST_FOLDING} COMMAND ${TEST_FOLDING}) endif(ENABLE_TESTING) diff --git a/tests/TestFolding.cc b/tests/TestFolding.cc index 9a0053d..6ab8159 100644 --- a/tests/TestFolding.cc +++ b/tests/TestFolding.cc @@ -12,7 +12,7 @@ TEST_CASE("Version Test", "[Folding]") { - std::string actual_version = { folding_project_version.begin(), folding_project_version.end() }; + std::string actual_version = "1.1.1"; auto data = std::vector(100); folding::StratifiedKFold stratified_kfold(5, data, 17); REQUIRE(stratified_kfold.version() == actual_version); @@ -186,4 +186,38 @@ TEST_CASE("StratifiedKFold Test", "[Folding]") } } } +} +TEST_CASE("Stratified KFold quiet parameter", "[Folding]") +{ + auto raw = RawDatasets("glass", true); + std::string expected = "Warning! The number of samples in class 2 (9) is less than the number of folds (10).\n"; + + SECTION("With vectors") + { + // Redirect cerr to a stringstream + std::streambuf* originalCerrBuffer = std::cerr.rdbuf(); + std::stringstream capturedOutput; + std::cerr.rdbuf(capturedOutput.rdbuf()); + // StratifiedKFold with quiet parameter set to false + folding::StratifiedKFold stratified_kfold(10, raw.yv, 17, false); + // Restore the original cerr buffer + std::cerr.rdbuf(originalCerrBuffer); + // Check the captured output + REQUIRE(capturedOutput.str() == expected); + REQUIRE(stratified_kfold.isFaulty()); + } + SECTION("With tensors") + { + // Redirect cerr to a stringstream + std::streambuf* originalCerrBuffer = std::cerr.rdbuf(); + std::stringstream capturedOutput; + std::cerr.rdbuf(capturedOutput.rdbuf()); + // StratifiedKFold with quiet parameter set to false + folding::StratifiedKFold stratified_kfold(10, raw.yt, 17, false); + // Restore the original cerr buffer + std::cerr.rdbuf(originalCerrBuffer); + // Check the captured output + REQUIRE(capturedOutput.str() == expected); + REQUIRE(stratified_kfold.isFaulty()); + } } \ No newline at end of file diff --git a/tests/TestUtils.h b/tests/TestUtils.h index 83b2e22..8ef1e39 100644 --- a/tests/TestUtils.h +++ b/tests/TestUtils.h @@ -8,7 +8,6 @@ #include #include "ArffFiles.h" #include "CPPFImdlp.h" -#include "folding_config.h" bool file_exists(const std::string& name); std::pair, map> discretize(std::vector& X, mdlp::labels_t& y, std::vector features); @@ -45,11 +44,11 @@ class Paths { public: static std::string datasets() { - return { folding_data_path.begin(), folding_data_path.end() }; + return "../../tests/data/"; } static std::string csv() { - return { folding_csv_path.begin(), folding_csv_path.end() }; + return "../../tests/csv/"; } }; class CSVFiles { diff --git a/tests/lib/Catch2 b/tests/lib/Catch2 index 4e8d92b..0321d2f 160000 --- a/tests/lib/Catch2 +++ b/tests/lib/Catch2 @@ -1 +1 @@ -Subproject commit 4e8d92bf02f7d1c8006a0e7a5ecabd8e62d98502 +Subproject commit 0321d2fce328b5e2ad106a8230ff20e0d5bf5501 diff --git a/tests/lib/mdlp b/tests/lib/mdlp index 236d1b2..cfb993f 160000 --- a/tests/lib/mdlp +++ b/tests/lib/mdlp @@ -1 +1 @@ -Subproject commit 236d1b2f8be185039493fe7fce04a83e02ed72e5 +Subproject commit cfb993f5ec1aabed527f524fdd4db06c6d839868