Compare commits
8 Commits
v1.1.0
...
8807cd513c
Author | SHA1 | Date | |
---|---|---|---|
8807cd513c
|
|||
91df2f5e02
|
|||
72053e195a
|
|||
1973ab6fb6
|
|||
9652853d69 | |||
0406322c62
|
|||
d1335f9f8a
|
|||
deba2a9011
|
26
.gitignore
vendored
26
.gitignore
vendored
@@ -37,4 +37,28 @@ build_*/**
|
|||||||
cmake-build*/**
|
cmake-build*/**
|
||||||
.idea
|
.idea
|
||||||
puml/**
|
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
6
.gitmodules
vendored
@@ -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
|
|
@@ -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/),
|
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.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
|
## [1.1.0] 2024-05-11
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
project(Folding
|
project(Folding
|
||||||
VERSION 1.1.0
|
|
||||||
DESCRIPTION "Folding utility for BayesNet library"
|
DESCRIPTION "Folding utility for BayesNet library"
|
||||||
HOMEPAGE_URL "https://github.com/rmontanana/folding"
|
HOMEPAGE_URL "https://github.com/rmontanana/folding"
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
find_package(Torch REQUIRED)
|
find_package(Torch REQUIRED)
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
|
||||||
|
|
||||||
if (POLICY CMP0135)
|
if (POLICY CMP0135)
|
||||||
cmake_policy(SET CMP0135 NEW)
|
cmake_policy(SET CMP0135 NEW)
|
||||||
@@ -33,15 +33,14 @@ include(AddGitSubmodule)
|
|||||||
|
|
||||||
# Subdirectories
|
# Subdirectories
|
||||||
# --------------
|
# --------------
|
||||||
add_subdirectory(config)
|
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
# -------
|
# -------
|
||||||
if (ENABLE_TESTING)
|
if (ENABLE_TESTING)
|
||||||
MESSAGE("Testing enabled")
|
MESSAGE("Testing enabled")
|
||||||
add_git_submodule("tests/lib/Catch2")
|
find_package(Catch2 REQUIRED)
|
||||||
add_git_submodule("tests/lib/Files")
|
find_package(arff-files REQUIRED)
|
||||||
add_git_submodule("tests/lib/mdlp")
|
find_package(fimdlp REQUIRED)
|
||||||
include(CTest)
|
include(CTest)
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
endif (ENABLE_TESTING)
|
endif (ENABLE_TESTING)
|
||||||
|
9
CMakeUserPresets.json
Normal file
9
CMakeUserPresets.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"version": 4,
|
||||||
|
"vendor": {
|
||||||
|
"conan": {}
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"build_Debug/CMakePresets.json"
|
||||||
|
]
|
||||||
|
}
|
3
Makefile
3
Makefile
@@ -28,7 +28,8 @@ 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
|
@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";
|
@echo ">>> Done";
|
||||||
|
|
||||||
opt = ""
|
opt = ""
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
# <img src="logo.png" alt="logo" width="50"/> Folding
|
# <img src="logo.png" alt="logo" width="50"/> Folding
|
||||||
|
|
||||||
|
|
||||||

|

|
||||||
[](<https://opensource.org/licenses/MIT>)
|
[](<https://opensource.org/licenses/MIT>)
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
K-Fold and stratified K-Fold header-only library for BayesNet classifiers & platform
|
K-Fold and stratified K-Fold header-only library for BayesNet classifiers & platform
|
||||||
|
|
||||||
|
49
conanfile.py
Normal file
49
conanfile.py
Normal 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 = []
|
@@ -1,4 +0,0 @@
|
|||||||
configure_file(
|
|
||||||
"config.h.in"
|
|
||||||
"${CMAKE_BINARY_DIR}/configured_files/include/folding_config.h" ESCAPE_QUOTES
|
|
||||||
)
|
|
@@ -1,14 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
#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/";
|
|
12
folding.hpp
12
folding.hpp
@@ -11,7 +11,7 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
namespace folding {
|
namespace folding {
|
||||||
const std::string FOLDING_VERSION = "1.1.0";
|
const std::string FOLDING_VERSION = "1.1.1";
|
||||||
class Fold {
|
class Fold {
|
||||||
public:
|
public:
|
||||||
inline Fold(int k, int n, int seed = -1) : k(k), n(n), seed(seed)
|
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 {
|
class StratifiedKFold : public Fold {
|
||||||
public:
|
public:
|
||||||
inline StratifiedKFold(int k, const std::vector<int>& y, int seed = -1) : Fold(k, y.size(), seed)
|
inline StratifiedKFold(int k, const std::vector<int>& y, int seed = -1, bool quiet = true) : Fold(k, y.size(), seed)
|
||||||
{
|
{
|
||||||
this->y = y;
|
this->y = y;
|
||||||
n = y.size();
|
n = y.size();
|
||||||
|
this->quiet = quiet;
|
||||||
build();
|
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();
|
n = y.numel();
|
||||||
this->y = std::vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + n);
|
this->y = std::vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + n);
|
||||||
|
this->quiet = quiet;
|
||||||
build();
|
build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,6 +92,7 @@ namespace folding {
|
|||||||
std::vector<int> y;
|
std::vector<int> y;
|
||||||
std::vector<std::vector<int>> stratified_indices;
|
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.
|
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()
|
void build()
|
||||||
{
|
{
|
||||||
stratified_indices = std::vector<std::vector<int>>(k);
|
stratified_indices = std::vector<std::vector<int>>(k);
|
||||||
@@ -105,7 +108,8 @@ namespace folding {
|
|||||||
int num_samples_to_take = num_samples / k;
|
int num_samples_to_take = num_samples / k;
|
||||||
int remainder_samples_to_take = num_samples % k;
|
int remainder_samples_to_take = num_samples % k;
|
||||||
if (num_samples_to_take == 0) {
|
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;
|
<< ") is less than the number of folds (" << k << ")." << std::endl;
|
||||||
faulty = true;
|
faulty = true;
|
||||||
}
|
}
|
||||||
|
@@ -1,12 +1,15 @@
|
|||||||
if(ENABLE_TESTING)
|
if(ENABLE_TESTING)
|
||||||
include_directories(
|
include_directories(
|
||||||
${Folding_SOURCE_DIR}
|
${Folding_SOURCE_DIR}
|
||||||
lib/Files
|
|
||||||
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")
|
||||||
add_executable(${TEST_FOLDING} TestFolding.cc TestUtils.cc)
|
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}
|
||||||
|
arff-files::arff-files
|
||||||
|
fimdlp::fimdlp
|
||||||
|
Catch2::Catch2WithMain
|
||||||
|
)
|
||||||
add_test(NAME ${TEST_FOLDING} COMMAND ${TEST_FOLDING})
|
add_test(NAME ${TEST_FOLDING} COMMAND ${TEST_FOLDING})
|
||||||
endif(ENABLE_TESTING)
|
endif(ENABLE_TESTING)
|
||||||
|
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
TEST_CASE("Version Test", "[Folding]")
|
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<int>(100);
|
auto data = std::vector<int>(100);
|
||||||
folding::StratifiedKFold stratified_kfold(5, data, 17);
|
folding::StratifiedKFold stratified_kfold(5, data, 17);
|
||||||
REQUIRE(stratified_kfold.version() == actual_version);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
@@ -8,7 +8,6 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include "ArffFiles.h"
|
#include "ArffFiles.h"
|
||||||
#include "CPPFImdlp.h"
|
#include "CPPFImdlp.h"
|
||||||
#include "folding_config.h"
|
|
||||||
|
|
||||||
bool file_exists(const std::string& name);
|
bool file_exists(const std::string& name);
|
||||||
std::pair<vector<mdlp::labels_t>, map<std::string, int>> discretize(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y, std::vector<string> features);
|
std::pair<vector<mdlp::labels_t>, map<std::string, int>> discretize(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y, std::vector<string> features);
|
||||||
@@ -45,11 +44,11 @@ class Paths {
|
|||||||
public:
|
public:
|
||||||
static std::string datasets()
|
static std::string datasets()
|
||||||
{
|
{
|
||||||
return { folding_data_path.begin(), folding_data_path.end() };
|
return "../../tests/data/";
|
||||||
}
|
}
|
||||||
static std::string csv()
|
static std::string csv()
|
||||||
{
|
{
|
||||||
return { folding_csv_path.begin(), folding_csv_path.end() };
|
return "../../tests/csv/";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
class CSVFiles {
|
class CSVFiles {
|
||||||
|
Submodule tests/lib/Catch2 deleted from 4e8d92bf02
@@ -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;
|
|
||||||
}
|
|
@@ -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
|
|
@@ -1 +0,0 @@
|
|||||||
add_library(ArffFiles ArffFiles.cc)
|
|
Submodule tests/lib/mdlp deleted from 236d1b2f8b
Reference in New Issue
Block a user