Remove submodules to change its location

This commit is contained in:
2024-05-11 17:42:36 +02:00
parent 38bc00bb05
commit 030ed6bc1c
14 changed files with 38 additions and 171 deletions

6
.gitmodules vendored
View File

@@ -1,6 +0,0 @@
[submodule "lib/catch2"]
path = lib/catch2
url = https://github.com/catchorg/Catch2.git
[submodule "lib/mdlp"]
path = lib/mdlp
url = https://github.com/rmontanana/mdlp

View File

@@ -10,8 +10,16 @@
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"cStandard": "c17",
"cppStandard": "c++17",
"compileCommands": "${workspaceFolder}/cmake-build-release/compile_commands.json"
"cppStandard": "c++17"
},
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"cStandard": "c17",
"cppStandard": "c++17"
}
],
"version": 4

4
.vscode/launch.json vendored
View File

@@ -5,9 +5,9 @@
"type": "lldb",
"request": "launch",
"name": "test",
"program": "${workspaceFolder}/build_debug/tests/unit_tests_folding",
"program": "${workspaceFolder}/build_Debug/tests/unit_tests_folding",
"args": [],
"cwd": "${workspaceFolder}/build_debug/tests",
"cwd": "${workspaceFolder}/build_Debug/tests",
},
]
}

View File

@@ -5,6 +5,18 @@ 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.0] 2024-05-11
### Fixed
- Fixed the issue in stratified K-fold when the number of samples of a class is less than the number of folds. Now the algorithm will split the samples evenly among the folds.
### Added
- Refactor stratified build method to remove uneeded structures and optimize loops.
- Refactor the code to improve the readability and maintainability of the code,changing the order of the private, public and protected methods.
- More tests to enhance the robustness of the code.
## [1.0.1] 2024-04-03
### Added

View File

@@ -7,10 +7,6 @@ project(Folding
LANGUAGES CXX
)
if (CODE_COVERAGE AND NOT ENABLE_TESTING)
MESSAGE(FATAL_ERROR "Code coverage requires testing enabled")
endif (CODE_COVERAGE AND NOT ENABLE_TESTING)
find_package(Torch REQUIRED)
if (POLICY CMP0135)
@@ -25,6 +21,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
# Options
# -------
option(ENABLE_TESTING "Unit testing build" OFF)
@@ -40,12 +37,11 @@ add_subdirectory(config)
# Testing
# -------
if (ENABLE_TESTING)
MESSAGE("Testing enabled")
add_git_submodule("lib/catch2")
add_git_submodule("lib/Files")
add_git_submodule("lib/mdlp")
add_git_submodule("tests/lib/catch2")
add_git_submodule("tests/lib/Files")
add_git_submodule("tests/lib/mdlp")
include(CTest)
add_subdirectory(tests)
endif (ENABLE_TESTING)

View File

@@ -2,7 +2,7 @@ SHELL := /bin/bash
.DEFAULT_GOAL := help
.PHONY: help build test clean
f_debug = build_debug
f_debug = build_Debug
test_targets = unit_tests_folding
n_procs = -j 16
@@ -28,7 +28,7 @@ build: ## Build a debug version of the project
@echo ">>> Building Debug Folding...";
@if [ -d ./$(f_debug) ]; then rm -rf ./$(f_debug); fi
@mkdir $(f_debug);
@cmake -S . -B $(f_debug) -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON
@cmake -S . -B $(f_debug) -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON
@echo ">>> Done";
opt = ""

View File

@@ -1,143 +0,0 @@
#pragma once
#include <torch/torch.h>
#include <algorithm>
#include <map>
#include <random>
#include <vector>
namespace folding {
const std::string FOLDING_VERSION = "1.1.0";
class Fold {
public:
inline Fold(int k, int m, int seed = -1) : k(k), m(m), seed(seed)
{
std::random_device rd;
random_seed = std::mt19937(seed == -1 ? rd() : seed);
std::srand(seed == -1 ? time(0) : seed);
}
virtual std::pair<std::vector<int>, std::vector<int>> getFold(int nFold) = 0;
virtual ~Fold() = default;
std::string version() { return FOLDING_VERSION; }
int getNumberOfFolds() { return k; }
protected:
int k;
int m;
int seed;
std::mt19937 random_seed;
};
class KFold : public Fold {
public:
inline KFold(int k, int m, int seed = -1) : Fold(k, m, seed), indices(std::vector<int>(m))
{
std::iota(begin(indices), end(indices), 0); // fill with 0, 1, ..., n - 1
std::shuffle(indices.begin(), indices.end(), random_seed);
}
inline std::pair<std::vector<int>, std::vector<int>> getFold(int nFold) override
{
if (nFold >= k || nFold < 0) {
throw std::out_of_range("nFold (" + std::to_string(nFold) + ") must be less than k (" + std::to_string(k) + ")");
}
int nTest = m / k;
auto train = std::vector<int>();
auto test = std::vector<int>();
for (int i = 0; i < m; i++) {
if (i >= nTest * nFold && i < nTest * (nFold + 1)) {
test.push_back(indices[i]);
} else {
train.push_back(indices[i]);
}
}
return { train, test };
}
private:
std::vector<int> indices;
};
class StratifiedKFold : public Fold {
public:
inline StratifiedKFold(int k, const std::vector<int>& y, int seed = -1) : Fold(k, y.size(), seed)
{
m = y.size();
this->y = y;
build();
}
inline StratifiedKFold(int k, torch::Tensor& y, int seed = -1) : Fold(k, y.numel(), seed)
{
m = y.numel();
this->y = std::vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + m);
build();
}
inline std::pair<std::vector<int>, std::vector<int>> getFold(int nFold) override
{
if (nFold >= k || nFold < 0) {
throw std::out_of_range("nFold (" + std::to_string(nFold) + ") must be less than k (" + std::to_string(k) + ")");
}
std::vector<int> test_indices = stratified_indices[nFold];
std::vector<int> train_indices;
for (int i = 0; i < k; ++i) {
if (i == nFold) continue;
train_indices.insert(train_indices.end(), stratified_indices[i].begin(), stratified_indices[i].end());
}
return { train_indices, test_indices };
}
inline bool isFaulty() { return faulty; }
private:
std::vector<int> y;
std::vector<std::vector<int>> stratified_indices;
bool faulty = false; // Only true if the number of samples of any class is less than the number of folds.
void build()
{
stratified_indices = std::vector<std::vector<int>>(k);
// Compute class counts and indices
auto class_indices = std::map<int, std::vector<int>>();
std::vector<int> class_counts(*max_element(y.begin(), y.end()) + 1, 0);
for (auto i = 0; i < m; ++i) {
class_counts[y[i]]++;
class_indices[y[i]].push_back(i);
}
// Assign indices to folds
for (auto [label, indices] : class_indices) {
shuffle(indices.begin(), indices.end(), random_seed);
int num_samples = indices.size();
int samples_per_fold = num_samples / k;
int remainder_samples_to_take = num_samples % k;
if (samples_per_fold == 0) {
std::cerr << "Warning! The number of samples in class " << label << " (" << num_samples
<< ") is less than the number of folds (" << k << ")." << std::endl;
faulty = true;
}
int start = 0;
// auto chosen2 = std::vector<int>(k);
// if (remainder_samples_to_take > 0) {
// iota(chosen2.begin(), chosen2.end(), 0);
// shuffle(chosen2.begin(), chosen2.end(), random_seed);
// }
if (samples_per_fold != 0) {
for (auto fold = 0; fold < k; ++fold) {
// auto it = next(indices.begin() + start, samples_per_fold);
// move(indices.begin() + start, it, back_inserter(stratified_indices[fold]));
auto it = next(class_indices[label].begin(), samples_per_fold);
move(class_indices[label].begin(), it, back_inserter(stratified_indices[fold]));
start += samples_per_fold;
class_indices[label].erase(class_indices[label].begin(), it);
}
}
auto chosen = std::vector<bool>(k, false);
while (remainder_samples_to_take > 0) {
int fold = (rand() % static_cast<int>(k));
if (chosen.at(fold)) {
continue;
}
chosen[fold] = true;
// auto it = next(indices.begin() + start, 1);
auto it = next(indices.begin(), 1);
stratified_indices[fold].push_back(class_indices[label][0]);
start++;
class_indices[label].erase(class_indices[label].begin(), it);
remainder_samples_to_take--;
}
}
}
};
}

View File

@@ -104,10 +104,12 @@ namespace folding {
faulty = true;
}
int start = 0;
for (auto fold = 0; fold < k; ++fold) {
auto it = next(class_indices[label].begin() + start, num_samples_to_take);
move(indices.begin() + start, it, back_inserter(stratified_indices[fold]));
start += num_samples_to_take;
if (num_samples_to_take > 0) {
for (auto fold = 0; fold < k; ++fold) {
auto it = next(class_indices[label].begin() + start, num_samples_to_take);
move(indices.begin() + start, it, back_inserter(stratified_indices[fold]));
start += num_samples_to_take;
}
}
if (remainder_samples_to_take > 0) {
auto chosen = std::vector<int>(k);

Submodule lib/catch2 deleted from 863c662c0e

Submodule lib/mdlp deleted from 5708dc3de9

View File

@@ -1,8 +1,8 @@
if(ENABLE_TESTING)
include_directories(
${Folding_SOURCE_DIR}
${Folding_SOURCE_DIR}/lib/Files
${Folding_SOURCE_DIR}/lib/mdlp
lib/Files
lib/mdlp
${CMAKE_BINARY_DIR}/configured_files/include
)
set(TEST_FOLDING "unit_tests_folding")