Remove submodules to change its location
This commit is contained in:
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -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
|
|
12
.vscode/c_cpp_properties.json
vendored
12
.vscode/c_cpp_properties.json
vendored
@@ -10,8 +10,16 @@
|
|||||||
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
|
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
|
||||||
],
|
],
|
||||||
"cStandard": "c17",
|
"cStandard": "c17",
|
||||||
"cppStandard": "c++17",
|
"cppStandard": "c++17"
|
||||||
"compileCommands": "${workspaceFolder}/cmake-build-release/compile_commands.json"
|
},
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"cStandard": "c17",
|
||||||
|
"cppStandard": "c++17"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"version": 4
|
"version": 4
|
||||||
|
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@@ -5,9 +5,9 @@
|
|||||||
"type": "lldb",
|
"type": "lldb",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"program": "${workspaceFolder}/build_debug/tests/unit_tests_folding",
|
"program": "${workspaceFolder}/build_Debug/tests/unit_tests_folding",
|
||||||
"args": [],
|
"args": [],
|
||||||
"cwd": "${workspaceFolder}/build_debug/tests",
|
"cwd": "${workspaceFolder}/build_Debug/tests",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
12
CHANGELOG.md
12
CHANGELOG.md
@@ -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/),
|
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).
|
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
|
## [1.0.1] 2024-04-03
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@@ -7,10 +7,6 @@ project(Folding
|
|||||||
LANGUAGES CXX
|
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)
|
find_package(Torch REQUIRED)
|
||||||
|
|
||||||
if (POLICY CMP0135)
|
if (POLICY CMP0135)
|
||||||
@@ -25,6 +21,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
|
||||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||||
|
|
||||||
# Options
|
# Options
|
||||||
# -------
|
# -------
|
||||||
option(ENABLE_TESTING "Unit testing build" OFF)
|
option(ENABLE_TESTING "Unit testing build" OFF)
|
||||||
@@ -40,12 +37,11 @@ add_subdirectory(config)
|
|||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
# -------
|
# -------
|
||||||
|
|
||||||
if (ENABLE_TESTING)
|
if (ENABLE_TESTING)
|
||||||
MESSAGE("Testing enabled")
|
MESSAGE("Testing enabled")
|
||||||
add_git_submodule("lib/catch2")
|
add_git_submodule("tests/lib/catch2")
|
||||||
add_git_submodule("lib/Files")
|
add_git_submodule("tests/lib/Files")
|
||||||
add_git_submodule("lib/mdlp")
|
add_git_submodule("tests/lib/mdlp")
|
||||||
include(CTest)
|
include(CTest)
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
endif (ENABLE_TESTING)
|
endif (ENABLE_TESTING)
|
||||||
|
4
Makefile
4
Makefile
@@ -2,7 +2,7 @@ SHELL := /bin/bash
|
|||||||
.DEFAULT_GOAL := help
|
.DEFAULT_GOAL := help
|
||||||
.PHONY: help build test clean
|
.PHONY: help build test clean
|
||||||
|
|
||||||
f_debug = build_debug
|
f_debug = build_Debug
|
||||||
test_targets = unit_tests_folding
|
test_targets = unit_tests_folding
|
||||||
n_procs = -j 16
|
n_procs = -j 16
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ build: ## Build a debug version of the project
|
|||||||
@echo ">>> Building Debug Folding...";
|
@echo ">>> Building Debug Folding...";
|
||||||
@if [ -d ./$(f_debug) ]; then rm -rf ./$(f_debug); fi
|
@if [ -d ./$(f_debug) ]; then rm -rf ./$(f_debug); fi
|
||||||
@mkdir $(f_debug);
|
@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";
|
@echo ">>> Done";
|
||||||
|
|
||||||
opt = ""
|
opt = ""
|
||||||
|
143
folding copy.hpp
143
folding copy.hpp
@@ -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--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
10
folding.hpp
10
folding.hpp
@@ -104,10 +104,12 @@ namespace folding {
|
|||||||
faulty = true;
|
faulty = true;
|
||||||
}
|
}
|
||||||
int start = 0;
|
int start = 0;
|
||||||
for (auto fold = 0; fold < k; ++fold) {
|
if (num_samples_to_take > 0) {
|
||||||
auto it = next(class_indices[label].begin() + start, num_samples_to_take);
|
for (auto fold = 0; fold < k; ++fold) {
|
||||||
move(indices.begin() + start, it, back_inserter(stratified_indices[fold]));
|
auto it = next(class_indices[label].begin() + start, num_samples_to_take);
|
||||||
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) {
|
if (remainder_samples_to_take > 0) {
|
||||||
auto chosen = std::vector<int>(k);
|
auto chosen = std::vector<int>(k);
|
||||||
|
Submodule lib/catch2 deleted from 863c662c0e
1
lib/mdlp
1
lib/mdlp
Submodule lib/mdlp deleted from 5708dc3de9
@@ -1,8 +1,8 @@
|
|||||||
if(ENABLE_TESTING)
|
if(ENABLE_TESTING)
|
||||||
include_directories(
|
include_directories(
|
||||||
${Folding_SOURCE_DIR}
|
${Folding_SOURCE_DIR}
|
||||||
${Folding_SOURCE_DIR}/lib/Files
|
lib/Files
|
||||||
${Folding_SOURCE_DIR}/lib/mdlp
|
lib/mdlp
|
||||||
${CMAKE_BINARY_DIR}/configured_files/include
|
${CMAKE_BINARY_DIR}/configured_files/include
|
||||||
)
|
)
|
||||||
set(TEST_FOLDING "unit_tests_folding")
|
set(TEST_FOLDING "unit_tests_folding")
|
||||||
|
Reference in New Issue
Block a user