4 Commits

13 changed files with 98 additions and 231 deletions

26
.gitignore vendored
View File

@@ -37,4 +37,28 @@ build_*/**
cmake-build*/**
.idea
puml/**
.vscode/settings.json
.vscode/settings.json
# CMake generated files
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
compile_commands.json
Makefile
CTestTestfile.cmake
DartConfiguration.tcl
Testing/
CMakePresets.json
# Conan generated files
conan_toolchain.cmake
conan*.sh
deactivate_*.sh
cmakedeps*.cmake
conandeps*.cmake
*-Target-*.cmake
*-debug-*.cmake
*Config*.cmake
*Targets.cmake
Find*.cmake
module-*.cmake

6
.gitmodules vendored
View File

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

View File

@@ -7,6 +7,7 @@ project(Folding
)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
if (POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
@@ -37,9 +38,9 @@ include(AddGitSubmodule)
# -------
if (ENABLE_TESTING)
MESSAGE("Testing enabled")
add_git_submodule("tests/lib/Catch2")
add_git_submodule("tests/lib/Files")
add_git_submodule("tests/lib/mdlp")
find_package(Catch2 REQUIRED)
find_package(arff-files REQUIRED)
find_package(fimdlp REQUIRED)
include(CTest)
add_subdirectory(tests)
endif (ENABLE_TESTING)

9
CMakeUserPresets.json Normal file
View File

@@ -0,0 +1,9 @@
{
"version": 4,
"vendor": {
"conan": {}
},
"include": [
"build_Debug/CMakePresets.json"
]
}

View File

@@ -28,7 +28,8 @@ 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
@conan install . --output-folder=$(f_debug) --build=missing --profile:build=default --profile:host=default
@cmake -S . -B $(f_debug) -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CMAKE_TOOLCHAIN_FILE=$(f_debug)/conan_toolchain.cmake
@echo ">>> Done";
opt = ""

View File

@@ -1,9 +1,10 @@
# <img src="logo.png" alt="logo" width="50"/> Folding
![C++](https://img.shields.io/badge/c++-%2300599C.svg?style=flat&logo=c%2B%2B&logoColor=white)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](<https://opensource.org/licenses/MIT>)
![Gitea Release](https://img.shields.io/gitea/v/release/rmontanana/folding?gitea_url=https://gitea.rmontanana.es:3000)
![Gitea Last Commit](https://img.shields.io/gitea/last-commit/rmontanana/folding?gitea_url=https://gitea.rmontanana.es:3000&logo=gitea)
![Gitea Release](https://img.shields.io/gitea/v/release/rmontanana/folding?gitea_url=https://gitea.rmontanana.es)
![Gitea Last Commit](https://img.shields.io/gitea/last-commit/rmontanana/folding?gitea_url=https://gitea.rmontanana.es&logo=gitea)
K-Fold and stratified K-Fold header-only library for BayesNet classifiers & platform

49
conanfile.py Normal file
View File

@@ -0,0 +1,49 @@
import re
from conan import ConanFile
from conan.tools.files import copy
from conan.tools.cmake import CMakeToolchain
class FoldingConan(ConanFile):
name = "folding"
version = "X.X.X"
description = "K-Fold and stratified K-Fold header-only library"
url = "https://github.com/rmontanana/folding"
license = "MIT"
homepage = "https://github.com/rmontanana/ArffFiles"
topics = ("kfold", "stratified folding")
no_copy_source = True
exports_sources = "folding.hpp"
package_type = "header-library"
# build_type = "Debug"
def requirements(self):
# Build dependency
self.requires("libtorch/2.7.0")
def build_requirements(self):
# Test dependencies
self.test_requires("catch2/3.8.1")
self.test_requires("arff-files/1.2.0")
self.test_requires("fimdlp/2.0.1")
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
def init(self):
# Read the CMakeLists.txt file to get the version
with open("folding.hpp", "r") as f:
content = f.read()
match = re.search(
r'const std::string FOLDING_VERSION = "([^"]+)";', content
)
if match:
self.version = match.group(1)
def package(self):
copy(self, "*.hpp", self.source_folder, self.package_folder)
def package_info(self):
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []

View File

@@ -2,11 +2,14 @@ if(ENABLE_TESTING)
include_directories(
${Folding_SOURCE_DIR}
${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 fimdlp Catch2::Catch2WithMain)
target_link_libraries(${TEST_FOLDING} PUBLIC
${TORCH_LIBRARIES}
arff-files::arff-files
fimdlp::fimdlp
Catch2::Catch2WithMain
)
add_test(NAME ${TEST_FOLDING} COMMAND ${TEST_FOLDING})
endif(ENABLE_TESTING)

Submodule tests/lib/Catch2 deleted from 0321d2fce3

View File

@@ -1,174 +0,0 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#include "ArffFiles.h"
#include <fstream>
#include <sstream>
#include <map>
#include <iostream>
ArffFiles::ArffFiles() = default;
std::vector<std::string> ArffFiles::getLines() const
{
return lines;
}
unsigned long int ArffFiles::getSize() const
{
return lines.size();
}
std::vector<std::pair<std::string, std::string>> ArffFiles::getAttributes() const
{
return attributes;
}
std::string ArffFiles::getClassName() const
{
return className;
}
std::string ArffFiles::getClassType() const
{
return classType;
}
std::vector<std::vector<float>>& ArffFiles::getX()
{
return X;
}
std::vector<int>& ArffFiles::getY()
{
return y;
}
void ArffFiles::loadCommon(std::string fileName)
{
std::ifstream file(fileName);
if (!file.is_open()) {
throw std::invalid_argument("Unable to open file");
}
std::string line;
std::string keyword;
std::string attribute;
std::string type;
std::string type_w;
while (getline(file, line)) {
if (line.empty() || line[0] == '%' || line == "\r" || line == " ") {
continue;
}
if (line.find("@attribute") != std::string::npos || line.find("@ATTRIBUTE") != std::string::npos) {
std::stringstream ss(line);
ss >> keyword >> attribute;
type = "";
while (ss >> type_w)
type += type_w + " ";
attributes.emplace_back(trim(attribute), trim(type));
continue;
}
if (line[0] == '@') {
continue;
}
lines.push_back(line);
}
file.close();
if (attributes.empty())
throw std::invalid_argument("No attributes found");
}
void ArffFiles::load(const std::string& fileName, bool classLast)
{
int labelIndex;
loadCommon(fileName);
if (classLast) {
className = std::get<0>(attributes.back());
classType = std::get<1>(attributes.back());
attributes.pop_back();
labelIndex = static_cast<int>(attributes.size());
} else {
className = std::get<0>(attributes.front());
classType = std::get<1>(attributes.front());
attributes.erase(attributes.begin());
labelIndex = 0;
}
generateDataset(labelIndex);
}
void ArffFiles::load(const std::string& fileName, const std::string& name)
{
int labelIndex;
loadCommon(fileName);
bool found = false;
for (int i = 0; i < attributes.size(); ++i) {
if (attributes[i].first == name) {
className = std::get<0>(attributes[i]);
classType = std::get<1>(attributes[i]);
attributes.erase(attributes.begin() + i);
labelIndex = i;
found = true;
break;
}
}
if (!found) {
throw std::invalid_argument("Class name not found");
}
generateDataset(labelIndex);
}
void ArffFiles::generateDataset(int labelIndex)
{
X = std::vector<std::vector<float>>(attributes.size(), std::vector<float>(lines.size()));
auto yy = std::vector<std::string>(lines.size(), "");
auto removeLines = std::vector<int>(); // Lines with missing values
for (size_t i = 0; i < lines.size(); i++) {
std::stringstream ss(lines[i]);
std::string value;
int pos = 0;
int xIndex = 0;
while (getline(ss, value, ',')) {
if (pos++ == labelIndex) {
yy[i] = value;
} else {
if (value == "?") {
X[xIndex++][i] = -1;
removeLines.push_back(i);
} else
X[xIndex++][i] = stof(value);
}
}
}
for (auto i : removeLines) {
yy.erase(yy.begin() + i);
for (auto& x : X) {
x.erase(x.begin() + i);
}
}
y = factorize(yy);
}
std::string ArffFiles::trim(const std::string& source)
{
std::string s(source);
s.erase(0, s.find_first_not_of(" '\n\r\t"));
s.erase(s.find_last_not_of(" '\n\r\t") + 1);
return s;
}
std::vector<int> ArffFiles::factorize(const std::vector<std::string>& labels_t)
{
std::vector<int> yy;
yy.reserve(labels_t.size());
std::map<std::string, int> labelMap;
int i = 0;
for (const std::string& label : labels_t) {
if (labelMap.find(label) == labelMap.end()) {
labelMap[label] = i++;
}
yy.push_back(labelMap[label]);
}
return yy;
}

View File

@@ -1,38 +0,0 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#ifndef ARFFFILES_H
#define ARFFFILES_H
#include <string>
#include <vector>
class ArffFiles {
public:
ArffFiles();
void load(const std::string&, bool = true);
void load(const std::string&, const std::string&);
std::vector<std::string> getLines() const;
unsigned long int getSize() const;
std::string getClassName() const;
std::string getClassType() const;
static std::string trim(const std::string&);
std::vector<std::vector<float>>& getX();
std::vector<int>& getY();
std::vector<std::pair<std::string, std::string>> getAttributes() const;
static std::vector<int> factorize(const std::vector<std::string>& labels_t);
private:
std::vector<std::string> lines;
std::vector<std::pair<std::string, std::string>> attributes;
std::string className;
std::string classType;
std::vector<std::vector<float>> X;
std::vector<int> y;
void generateDataset(int);
void loadCommon(std::string);
};
#endif

View File

@@ -1 +0,0 @@
add_library(ArffFiles ArffFiles.cc)

Submodule tests/lib/mdlp deleted from cfb993f5ec