Compare commits
11 Commits
v1.0.0
...
acfc14c5c3
Author | SHA1 | Date | |
---|---|---|---|
acfc14c5c3
|
|||
ca4c8b716d
|
|||
63711decc0
|
|||
18c79f6d48
|
|||
a4329f5f9d
|
|||
eff7a33f96
|
|||
a5316928d4 | |||
cf32b9ae58
|
|||
9e1ef5bce2
|
|||
00068c05ed
|
|||
dbefa02d9c
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
||||
[submodule "tests/lib/catch2"]
|
||||
path = tests/lib/catch2
|
||||
url = https://github.com/catchorg/Catch2.git
|
124
ArffFiles.hpp
124
ArffFiles.hpp
@@ -7,10 +7,10 @@
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <cctype> // std::isdigit
|
||||
#include <algorithm> // std::all_of
|
||||
#include <algorithm> // std::all_of std::transform
|
||||
|
||||
class ArffFiles {
|
||||
const std::string VERSION = "1.0.0";
|
||||
const std::string VERSION = "1.1.0";
|
||||
public:
|
||||
ArffFiles() = default;
|
||||
void load(const std::string& fileName, bool classLast = true)
|
||||
@@ -28,8 +28,9 @@ public:
|
||||
attributes.erase(attributes.begin());
|
||||
labelIndex = 0;
|
||||
}
|
||||
preprocessDataset(labelIndex);
|
||||
generateDataset(labelIndex);
|
||||
};
|
||||
}
|
||||
void load(const std::string& fileName, const std::string& name)
|
||||
{
|
||||
int labelIndex;
|
||||
@@ -48,27 +49,67 @@ public:
|
||||
if (!found) {
|
||||
throw std::invalid_argument("Class name not found");
|
||||
}
|
||||
preprocessDataset(labelIndex);
|
||||
generateDataset(labelIndex);
|
||||
};
|
||||
std::vector<std::string> getLines() const { return lines; };
|
||||
unsigned long int getSize() const { return lines.size(); };
|
||||
std::string getClassName() const { return className; };
|
||||
std::string getClassType() const { return classType; };
|
||||
std::vector<std::string> getLabels() const { return labels; }
|
||||
}
|
||||
std::vector<std::string> getLines() const { return lines; }
|
||||
unsigned long int getSize() const { return lines.size(); }
|
||||
std::string getClassName() const { return className; }
|
||||
std::string getClassType() const { return classType; }
|
||||
std::map<std::string, std::vector<std::string>> getStates() const { return states; }
|
||||
std::vector<std::string> getLabels() const { return states.at(className); }
|
||||
static std::string 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<std::vector<float>>& getX() { return X; };
|
||||
}
|
||||
std::vector<std::vector<float>>& getX() { return X; }
|
||||
std::vector<int>& getY() { return y; }
|
||||
std::map<std::string, bool> getNumericAttributes() const { return numeric_features; }
|
||||
std::vector<std::pair<std::string, std::string>> getAttributes() const { return attributes; };
|
||||
std::vector<int> factorize(const std::vector<std::string>& labels_t)
|
||||
std::vector<std::string> split(const std::string& text, char delimiter)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
std::stringstream ss(text);
|
||||
std::string token;
|
||||
while (std::getline(ss, token, delimiter)) {
|
||||
result.push_back(trim(token));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::string version() const { return VERSION; }
|
||||
protected:
|
||||
std::vector<std::string> lines;
|
||||
std::map<std::string, bool> numeric_features;
|
||||
std::vector<std::pair<std::string, std::string>> attributes;
|
||||
std::string className;
|
||||
std::string classType;
|
||||
std::vector<std::vector<float>> X;
|
||||
std::vector<std::vector<std::string>> Xs;
|
||||
std::vector<int> y;
|
||||
std::map<std::string, std::vector<std::string>> states;
|
||||
private:
|
||||
void preprocessDataset(int labelIndex)
|
||||
{
|
||||
//
|
||||
// Learn the numeric features
|
||||
//
|
||||
numeric_features.clear();
|
||||
for (const auto& attribute : attributes) {
|
||||
auto feature = attribute.first;
|
||||
if (feature == className)
|
||||
continue;
|
||||
auto values = attribute.second;
|
||||
std::transform(values.begin(), values.end(), values.begin(), ::toupper);
|
||||
numeric_features[feature] = values == "REAL" || values == "INTEGER" || values == "NUMERIC";
|
||||
}
|
||||
}
|
||||
std::vector<int> factorize(const std::string feature, const std::vector<std::string>& labels_t)
|
||||
{
|
||||
std::vector<int> yy;
|
||||
labels.clear();
|
||||
states.at(feature).clear();
|
||||
yy.reserve(labels_t.size());
|
||||
std::map<std::string, int> labelMap;
|
||||
int i = 0;
|
||||
@@ -77,54 +118,46 @@ public:
|
||||
labelMap[label] = i++;
|
||||
bool allDigits = std::all_of(label.begin(), label.end(), ::isdigit);
|
||||
if (allDigits)
|
||||
labels.push_back("Class " + label);
|
||||
states[feature].push_back("Class " + label);
|
||||
else
|
||||
labels.push_back(label);
|
||||
states[feature].push_back(label);
|
||||
}
|
||||
yy.push_back(labelMap[label]);
|
||||
}
|
||||
return yy;
|
||||
};
|
||||
std::string version() const { return VERSION; };
|
||||
protected:
|
||||
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;
|
||||
std::vector<std::string> labels;
|
||||
private:
|
||||
}
|
||||
void generateDataset(int labelIndex)
|
||||
{
|
||||
X = std::vector<std::vector<float>>(attributes.size(), std::vector<float>(lines.size()));
|
||||
Xs = std::vector<std::vector<std::string>>(attributes.size(), std::vector<std::string>(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, ',')) {
|
||||
auto tokens = split(lines[i], ',');
|
||||
for (const auto& token : tokens) {
|
||||
if (pos++ == labelIndex) {
|
||||
yy[i] = value;
|
||||
yy[i] = token;
|
||||
} else {
|
||||
if (value == "?") {
|
||||
X[xIndex++][i] = -1;
|
||||
removeLines.push_back(i);
|
||||
} else
|
||||
X[xIndex++][i] = stof(value);
|
||||
if (numeric_features[attributes[xIndex].first]) {
|
||||
X[xIndex][i] = stof(token);
|
||||
} else {
|
||||
Xs[xIndex][i] = token;
|
||||
}
|
||||
xIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto i : removeLines) {
|
||||
yy.erase(yy.begin() + i);
|
||||
for (auto& x : X) {
|
||||
x.erase(x.begin() + i);
|
||||
for (size_t i = 0; i < attributes.size(); i++) {
|
||||
if (!numeric_features[attributes[i].first]) {
|
||||
auto data = factorize(attributes[i].first, Xs[i]);
|
||||
std::transform(data.begin(), data.end(), X[i].begin(), [](int x) { return float(x);});
|
||||
}
|
||||
}
|
||||
y = factorize(yy);
|
||||
};
|
||||
y = factorize(className, yy);
|
||||
}
|
||||
void loadCommon(std::string fileName)
|
||||
{
|
||||
std::ifstream file(fileName);
|
||||
@@ -152,12 +185,19 @@ private:
|
||||
if (line[0] == '@') {
|
||||
continue;
|
||||
}
|
||||
if (line.find("?", 0) != std::string::npos) {
|
||||
// ignore lines with missing values
|
||||
continue;
|
||||
}
|
||||
lines.push_back(line);
|
||||
}
|
||||
file.close();
|
||||
for (const auto& attribute : attributes) {
|
||||
states[attribute.first] = std::vector<std::string>();
|
||||
}
|
||||
if (attributes.empty())
|
||||
throw std::invalid_argument("No attributes found");
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
28
CHANGELOG.md
28
CHANGELOG.md
@@ -5,6 +5,34 @@ 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.2.0] 2025-06-27 Refactoring and Improvements
|
||||
|
||||
### Added
|
||||
|
||||
- Refactored code to improve readability and maintainability
|
||||
- Improved error handling with exceptions
|
||||
- Claude TECHNICAL_REPORT.md for detailed analysis
|
||||
- Claude CLAUDE.md for AI engine usage
|
||||
- Actions to build and upload the conan package to Cimmeria
|
||||
|
||||
|
||||
|
||||
## [1.1.0] 2024-07-24 String Values in Features
|
||||
|
||||
### Added
|
||||
|
||||
- Allow string values in features
|
||||
- Library logo
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed bug in numeric attributes states
|
||||
|
||||
### Removed
|
||||
|
||||
- Catch2 git submodule
|
||||
- iostream include
|
||||
|
||||
## [1.0.0] 2024-05-21 Initial Release
|
||||
|
||||
### Added
|
||||
|
83
CLAUDE.md
Normal file
83
CLAUDE.md
Normal file
@@ -0,0 +1,83 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
ArffFiles is a header-only C++ library for reading ARFF (Attribute-Relation File Format) files and converting them into STL vectors. The library handles both numeric and categorical features, automatically factorizing categorical attributes.
|
||||
|
||||
## Build System
|
||||
|
||||
This project uses CMake with Conan for package management:
|
||||
- **CMake**: Primary build system (requires CMake 3.20+)
|
||||
- **Conan**: Package management for dependencies
|
||||
- **Makefile**: Convenience wrapper for common tasks
|
||||
|
||||
## Common Development Commands
|
||||
|
||||
### Building and Testing
|
||||
```bash
|
||||
# Build and run tests (recommended)
|
||||
make build && make test
|
||||
|
||||
# Alternative manual build process
|
||||
mkdir build_debug
|
||||
cmake -S . -B build_debug -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON
|
||||
cmake --build build_debug -t unit_tests_arffFiles -j 16
|
||||
cd build_debug/tests && ./unit_tests_arffFiles
|
||||
```
|
||||
|
||||
### Testing Options
|
||||
```bash
|
||||
# Run tests with verbose output
|
||||
make test opt="-s"
|
||||
|
||||
# Clean test artifacts
|
||||
make clean
|
||||
```
|
||||
|
||||
### Code Coverage
|
||||
Code coverage is enabled when building with `-D CODE_COVERAGE=ON` and `-D ENABLE_TESTING=ON`. Coverage reports are generated during test runs.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Core Components
|
||||
|
||||
**Single Header Library**: `ArffFiles.hpp` contains the complete implementation.
|
||||
|
||||
**Main Class**: `ArffFiles`
|
||||
- Header-only design for easy integration
|
||||
- Handles ARFF file parsing and data conversion
|
||||
- Automatically determines numeric vs categorical features
|
||||
- Supports flexible class attribute positioning
|
||||
|
||||
### Key Methods
|
||||
- `load(fileName, classLast=true)`: Load with class attribute at end/beginning
|
||||
- `load(fileName, className)`: Load with specific named class attribute
|
||||
- `getX()`: Returns feature vectors as `std::vector<std::vector<float>>`
|
||||
- `getY()`: Returns labels as `std::vector<int>`
|
||||
- `getNumericAttributes()`: Returns feature type mapping
|
||||
|
||||
### Data Processing Pipeline
|
||||
1. **File Parsing**: Reads ARFF format, extracts attributes and data
|
||||
2. **Feature Detection**: Automatically identifies numeric vs categorical attributes
|
||||
3. **Preprocessing**: Handles missing values (lines with '?' are skipped)
|
||||
4. **Factorization**: Converts categorical features to numeric codes
|
||||
5. **Dataset Generation**: Creates final X (features) and y (labels) vectors
|
||||
|
||||
### Dependencies
|
||||
- **Catch2**: Testing framework (fetched via CMake FetchContent)
|
||||
- **Standard Library**: Uses STL containers (vector, map, string)
|
||||
- **C++17**: Minimum required standard
|
||||
|
||||
### Test Structure
|
||||
- Tests located in `tests/` directory
|
||||
- Sample ARFF files in `tests/data/`
|
||||
- Single test executable: `unit_tests_arffFiles`
|
||||
- Uses Catch2 v3.3.2 for test framework
|
||||
|
||||
### Conan Integration
|
||||
The project includes a `conanfile.py` that:
|
||||
- Automatically extracts version from CMakeLists.txt
|
||||
- Packages as a header-only library
|
||||
- Exports only the main header file
|
@@ -41,7 +41,12 @@ add_subdirectory(config)
|
||||
# -------
|
||||
if (ENABLE_TESTING)
|
||||
MESSAGE("Testing enabled")
|
||||
add_git_submodule("tests/lib/catch2")
|
||||
Include(FetchContent)
|
||||
FetchContent_Declare(Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
GIT_TAG v3.3.2
|
||||
)
|
||||
FetchContent_MakeAvailable(Catch2)
|
||||
include(CTest)
|
||||
add_subdirectory(tests)
|
||||
endif (ENABLE_TESTING)
|
||||
|
12
Makefile
12
Makefile
@@ -1,6 +1,6 @@
|
||||
SHELL := /bin/bash
|
||||
.DEFAULT_GOAL := help
|
||||
.PHONY: help build test clean
|
||||
.PHONY: help build test clean conan-build conan-upload
|
||||
|
||||
f_debug = build_debug
|
||||
test_targets = unit_tests_arffFiles
|
||||
@@ -44,6 +44,16 @@ test: ## Run tests (opt="-s") to verbose output the tests
|
||||
done
|
||||
@echo ">>> Done";
|
||||
|
||||
conan-build: ## Build Conan package locally
|
||||
@echo ">>> Building Conan package...";
|
||||
@conan create . --profile default
|
||||
@echo ">>> Done";
|
||||
|
||||
conan-upload: ## Upload package to Cimmeria JFrog Artifactory
|
||||
@echo ">>> Uploading to Cimmeria JFrog Artifactory...";
|
||||
@conan upload arff-files --all -r Cimmeria --confirm
|
||||
@echo ">>> Done";
|
||||
|
||||
help: ## Show help message
|
||||
@IFS=$$'\n' ; \
|
||||
help_lines=(`fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##/:/'`); \
|
||||
|
209
README.md
209
README.md
@@ -1,14 +1,211 @@
|
||||
# ArffFiles
|
||||
# <img src="logo.png" alt="logo" width="50"/> ArffFiles
|
||||
|
||||

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

|
||||

|
||||

|
||||

|
||||
|
||||
Header-only library to read Arff Files and return STL vectors with the data read.
|
||||
A modern C++17 header-only library to read **ARFF (Attribute-Relation File Format)** files and convert them into STL vectors for machine learning and data analysis applications.
|
||||
|
||||
### Tests
|
||||
## Features
|
||||
|
||||
- 🔧 **Header-only**: Simply include `ArffFiles.hpp` - no compilation required
|
||||
- 🚀 **Modern C++17**: Clean, efficient implementation using modern C++ standards
|
||||
- 🔄 **Automatic Type Detection**: Distinguishes between numeric and categorical attributes
|
||||
- 📊 **Flexible Class Positioning**: Support for class attributes at any position
|
||||
- 🎯 **STL Integration**: Returns standard `std::vector` containers for seamless integration
|
||||
- 🧹 **Data Cleaning**: Automatically handles missing values (lines with '?' are skipped)
|
||||
- 🏷️ **Label Encoding**: Automatic factorization of categorical features into numeric codes
|
||||
|
||||
## Requirements
|
||||
|
||||
- **C++17** compatible compiler
|
||||
- **Standard Library**: Uses STL containers (no external dependencies)
|
||||
|
||||
## Installation
|
||||
|
||||
### Using Conan
|
||||
|
||||
```bash
|
||||
make build && make test
|
||||
# Add the package to your conanfile.txt
|
||||
[requires]
|
||||
arff-files/1.0.1
|
||||
|
||||
# Or install directly
|
||||
conan install arff-files/1.0.1@
|
||||
```
|
||||
|
||||
### Manual Installation
|
||||
|
||||
Simply download `ArffFiles.hpp` and include it in your project:
|
||||
|
||||
```cpp
|
||||
#include "ArffFiles.hpp"
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
```cpp
|
||||
#include "ArffFiles.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
ArffFiles arff;
|
||||
|
||||
// Load ARFF file (class attribute at the end by default)
|
||||
arff.load("dataset.arff");
|
||||
|
||||
// Get feature matrix and labels
|
||||
auto& X = arff.getX(); // std::vector<std::vector<float>>
|
||||
auto& y = arff.getY(); // std::vector<int>
|
||||
|
||||
std::cout << "Dataset size: " << arff.getSize() << " samples" << std::endl;
|
||||
std::cout << "Features: " << X.size() << std::endl;
|
||||
std::cout << "Classes: " << arff.getLabels().size() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### Loading Data
|
||||
|
||||
```cpp
|
||||
// Load with class attribute at the end (default)
|
||||
arff.load("dataset.arff");
|
||||
|
||||
// Load with class attribute at the beginning
|
||||
arff.load("dataset.arff", false);
|
||||
|
||||
// Load with specific named class attribute
|
||||
arff.load("dataset.arff", "class_name");
|
||||
```
|
||||
|
||||
### Accessing Data
|
||||
|
||||
```cpp
|
||||
// Get feature matrix (each inner vector is a feature, not a sample)
|
||||
std::vector<std::vector<float>>& X = arff.getX();
|
||||
|
||||
// Get labels (encoded as integers)
|
||||
std::vector<int>& y = arff.getY();
|
||||
|
||||
// Get dataset information
|
||||
std::string className = arff.getClassName();
|
||||
std::vector<std::string> labels = arff.getLabels();
|
||||
unsigned long size = arff.getSize();
|
||||
|
||||
// Get attribute information
|
||||
auto attributes = arff.getAttributes(); // std::vector<std::pair<std::string, std::string>>
|
||||
auto numericFeatures = arff.getNumericAttributes(); // std::map<std::string, bool>
|
||||
```
|
||||
|
||||
### Utility Methods
|
||||
|
||||
```cpp
|
||||
// Get library version
|
||||
std::string version = arff.version();
|
||||
|
||||
// Access raw lines (after preprocessing)
|
||||
std::vector<std::string> lines = arff.getLines();
|
||||
|
||||
// Get label states mapping
|
||||
auto states = arff.getStates(); // std::map<std::string, std::vector<std::string>>
|
||||
```
|
||||
|
||||
## Data Processing Pipeline
|
||||
|
||||
1. **File Parsing**: Reads ARFF format, extracts `@attribute` declarations and data
|
||||
2. **Missing Value Handling**: Skips lines containing `?` (missing values)
|
||||
3. **Feature Type Detection**: Automatically identifies `REAL`, `INTEGER`, `NUMERIC` vs categorical
|
||||
4. **Label Positioning**: Handles class attributes at any position in the data
|
||||
5. **Factorization**: Converts categorical features and labels to numeric codes
|
||||
6. **Data Organization**: Creates feature matrix `X` and label vector `y`
|
||||
|
||||
## Example: Complete Workflow
|
||||
|
||||
```cpp
|
||||
#include "ArffFiles.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
try {
|
||||
ArffFiles arff;
|
||||
arff.load("iris.arff");
|
||||
|
||||
// Display dataset information
|
||||
std::cout << "Dataset: " << arff.getClassName() << std::endl;
|
||||
std::cout << "Samples: " << arff.getSize() << std::endl;
|
||||
std::cout << "Features: " << arff.getX().size() << std::endl;
|
||||
|
||||
// Show class labels
|
||||
auto labels = arff.getLabels();
|
||||
std::cout << "Classes: ";
|
||||
for (const auto& label : labels) {
|
||||
std::cout << label << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Show which features are numeric
|
||||
auto numericFeatures = arff.getNumericAttributes();
|
||||
for (const auto& [feature, isNumeric] : numericFeatures) {
|
||||
std::cout << feature << ": " << (isNumeric ? "numeric" : "categorical") << std::endl;
|
||||
}
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## Supported ARFF Features
|
||||
|
||||
- ✅ Numeric attributes (`@attribute feature REAL/INTEGER/NUMERIC`)
|
||||
- ✅ Categorical attributes (`@attribute feature {value1,value2,...}`)
|
||||
- ✅ Comments (lines starting with `%`)
|
||||
- ✅ Missing values (automatic skipping of lines with `?`)
|
||||
- ✅ Flexible class attribute positioning
|
||||
- ✅ Case-insensitive attribute declarations
|
||||
|
||||
## Error Handling
|
||||
|
||||
The library throws `std::invalid_argument` exceptions for:
|
||||
- Unable to open file
|
||||
- No attributes found in file
|
||||
- Specified class name not found
|
||||
|
||||
## Development
|
||||
|
||||
### Building and Testing
|
||||
|
||||
```bash
|
||||
# Build and run tests
|
||||
make build && make test
|
||||
|
||||
# Run tests with verbose output
|
||||
make test opt="-s"
|
||||
|
||||
# Clean test artifacts
|
||||
make clean
|
||||
```
|
||||
|
||||
### Using CMake Directly
|
||||
|
||||
```bash
|
||||
mkdir build_debug
|
||||
cmake -S . -B build_debug -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON
|
||||
cmake --build build_debug -t unit_tests_arffFiles
|
||||
cd build_debug/tests && ./unit_tests_arffFiles
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please feel free to submit a Pull Request.
|
||||
|
242
TECHNICAL_REPORT.md
Normal file
242
TECHNICAL_REPORT.md
Normal file
@@ -0,0 +1,242 @@
|
||||
# ArffFiles Library - Technical Analysis Report
|
||||
|
||||
**Generated**: 2025-06-27
|
||||
**Version Analyzed**: 1.1.0
|
||||
**Library Type**: Header-only C++17 ARFF File Parser
|
||||
|
||||
## Executive Summary
|
||||
|
||||
The ArffFiles library is a functional header-only C++17 implementation for parsing ARFF (Attribute-Relation File Format) files. While it successfully accomplishes its core purpose, several significant weaknesses in design, performance, and robustness have been identified that could impact production use.
|
||||
|
||||
**Overall Assessment**: ⚠️ **MODERATE RISK** - Functional but requires improvements for production use.
|
||||
|
||||
---
|
||||
|
||||
## 🟢 Strengths
|
||||
|
||||
### 1. **Architectural Design**
|
||||
- ✅ **Header-only**: Easy integration, no compilation dependencies
|
||||
- ✅ **Modern C++17**: Uses appropriate standard library features
|
||||
- ✅ **Clear separation**: Public/protected/private access levels well-defined
|
||||
- ✅ **STL Integration**: Returns standard containers for seamless integration
|
||||
|
||||
### 2. **Functionality**
|
||||
- ✅ **Flexible class positioning**: Supports class attributes at any position
|
||||
- ✅ **Automatic type detection**: Distinguishes numeric vs categorical attributes
|
||||
- ✅ **Missing value handling**: Skips lines with '?' characters
|
||||
- ✅ **Label encoding**: Automatic factorization of categorical features
|
||||
- ✅ **Case-insensitive parsing**: Handles @ATTRIBUTE/@attribute variations
|
||||
|
||||
### 3. **API Usability**
|
||||
- ✅ **Multiple load methods**: Three different loading strategies
|
||||
- ✅ **Comprehensive getters**: Good access to internal data structures
|
||||
- ✅ **Utility functions**: Includes trim() and split() helpers
|
||||
|
||||
### 4. **Testing Coverage**
|
||||
- ✅ **Real datasets**: Tests with iris, glass, adult, and Japanese vowels datasets
|
||||
- ✅ **Edge cases**: Tests different class positioning scenarios
|
||||
- ✅ **Data validation**: Verifies parsing accuracy with expected values
|
||||
|
||||
---
|
||||
|
||||
## 🔴 Critical Weaknesses
|
||||
|
||||
### 1. **Memory Management & Performance Issues**
|
||||
|
||||
#### **Inefficient Data Layout** (HIGH SEVERITY)
|
||||
```cpp
|
||||
// Line 131: Inefficient memory allocation
|
||||
X = std::vector<std::vector<float>>(attributes.size(), std::vector<float>(lines.size()));
|
||||
```
|
||||
- **Problem**: Feature-major layout instead of sample-major
|
||||
- **Impact**: Poor cache locality, inefficient for ML algorithms
|
||||
- **Memory overhead**: Double allocation for `X` and `Xs` vectors
|
||||
- **Performance**: Suboptimal for large datasets
|
||||
|
||||
#### **Redundant Memory Usage** (MEDIUM SEVERITY)
|
||||
```cpp
|
||||
std::vector<std::vector<float>> X; // Line 89
|
||||
std::vector<std::vector<std::string>> Xs; // Line 90
|
||||
```
|
||||
- **Problem**: Maintains both numeric and string representations
|
||||
- **Impact**: 2x memory usage for categorical features
|
||||
- **Memory waste**: `Xs` could be deallocated after factorization
|
||||
|
||||
#### **No Memory Pre-allocation** (MEDIUM SEVERITY)
|
||||
- **Problem**: Multiple vector resizing during parsing
|
||||
- **Impact**: Memory fragmentation and performance degradation
|
||||
|
||||
### 2. **Error Handling & Robustness**
|
||||
|
||||
#### **Unsafe Type Conversions** (HIGH SEVERITY)
|
||||
```cpp
|
||||
// Line 145: No exception handling
|
||||
X[xIndex][i] = stof(token);
|
||||
```
|
||||
- **Problem**: `stof()` can throw `std::invalid_argument` or `std::out_of_range`
|
||||
- **Impact**: Program termination on malformed numeric data
|
||||
- **Missing validation**: No checks for valid numeric format
|
||||
|
||||
#### **Insufficient Input Validation** (HIGH SEVERITY)
|
||||
```cpp
|
||||
// Line 39: Unsafe comparison without bounds checking
|
||||
for (int i = 0; i < attributes.size(); ++i)
|
||||
```
|
||||
- **Problem**: No validation of file structure integrity
|
||||
- **Missing checks**:
|
||||
- Empty attribute names
|
||||
- Duplicate attribute names
|
||||
- Malformed attribute declarations
|
||||
- Inconsistent number of tokens per line
|
||||
|
||||
#### **Resource Management** (MEDIUM SEVERITY)
|
||||
```cpp
|
||||
// Line 163-194: No RAII for file handling
|
||||
std::ifstream file(fileName);
|
||||
// ... processing ...
|
||||
file.close(); // Manual close
|
||||
```
|
||||
- **Problem**: Manual file closing (though acceptable here)
|
||||
- **Potential issue**: No exception safety guarantee
|
||||
|
||||
### 3. **Algorithm & Design Issues**
|
||||
|
||||
#### **Inefficient String Processing** (MEDIUM SEVERITY)
|
||||
```cpp
|
||||
// Line 176-182: Inefficient attribute parsing
|
||||
std::stringstream ss(line);
|
||||
ss >> keyword >> attribute;
|
||||
type = "";
|
||||
while (ss >> type_w)
|
||||
type += type_w + " "; // String concatenation in loop
|
||||
```
|
||||
- **Problem**: Repeated string concatenation is O(n²)
|
||||
- **Impact**: Performance degradation on large files
|
||||
- **Solution needed**: Use string reserve or stringstream
|
||||
|
||||
#### **Suboptimal Lookup Performance** (LOW SEVERITY)
|
||||
```cpp
|
||||
// Line 144: Map lookup in hot path
|
||||
if (numeric_features[attributes[xIndex].first])
|
||||
```
|
||||
- **Problem**: Hash map lookup for every data point
|
||||
- **Impact**: Unnecessary overhead during dataset generation
|
||||
|
||||
### 4. **API Design Limitations**
|
||||
|
||||
#### **Return by Value Issues** (MEDIUM SEVERITY)
|
||||
```cpp
|
||||
// Line 55-60: Expensive copies
|
||||
std::vector<std::string> getLines() const { return lines; }
|
||||
std::map<std::string, std::vector<std::string>> getStates() const { return states; }
|
||||
```
|
||||
- **Problem**: Large object copies instead of const references
|
||||
- **Impact**: Unnecessary memory allocation and copying
|
||||
- **Performance**: O(n) copy cost for large datasets
|
||||
|
||||
#### **Non-const Correctness** (MEDIUM SEVERITY)
|
||||
```cpp
|
||||
// Line 68-69: Mutable references without const alternatives
|
||||
std::vector<std::vector<float>>& getX() { return X; }
|
||||
std::vector<int>& getY() { return y; }
|
||||
```
|
||||
- **Problem**: No const versions for read-only access
|
||||
- **Impact**: API design inconsistency, potential accidental modification
|
||||
|
||||
#### **Type Inconsistency** (LOW SEVERITY)
|
||||
```cpp
|
||||
// Line 56: Mixed return types
|
||||
unsigned long int getSize() const { return lines.size(); }
|
||||
```
|
||||
- **Problem**: Should use `size_t` or `std::size_t`
|
||||
- **Impact**: Type conversion warnings on some platforms
|
||||
|
||||
### 5. **Thread Safety**
|
||||
|
||||
#### **Not Thread-Safe** (MEDIUM SEVERITY)
|
||||
- **Problem**: No synchronization mechanisms
|
||||
- **Impact**: Unsafe for concurrent access
|
||||
- **Missing**: Thread-safe accessors or documentation warning
|
||||
|
||||
### 6. **Security Considerations**
|
||||
|
||||
#### **Path Traversal Vulnerability** (LOW SEVERITY)
|
||||
```cpp
|
||||
// Line 161: No path validation
|
||||
void loadCommon(std::string fileName)
|
||||
```
|
||||
- **Problem**: No validation of file path
|
||||
- **Impact**: Potential directory traversal if user input not sanitized
|
||||
- **Mitigation**: Application-level validation needed
|
||||
|
||||
#### **Resource Exhaustion** (MEDIUM SEVERITY)
|
||||
- **Problem**: No limits on file size or memory usage
|
||||
- **Impact**: Potential DoS with extremely large files
|
||||
- **Missing**: File size validation and memory limits
|
||||
|
||||
### 7. **ARFF Format Compliance**
|
||||
|
||||
#### **Limited Format Support** (MEDIUM SEVERITY)
|
||||
- **Missing features**:
|
||||
- Date attributes (`@attribute date "yyyy-MM-dd HH:mm:ss"`)
|
||||
- String attributes (`@attribute text string`)
|
||||
- Relational attributes (nested ARFF)
|
||||
- Sparse data format (`{0 X, 3 Y, 5 Z}`)
|
||||
|
||||
#### **Parsing Edge Cases** (LOW SEVERITY)
|
||||
```cpp
|
||||
// Line 188: Simplistic missing value detection
|
||||
if (line.find("?", 0) != std::string::npos)
|
||||
```
|
||||
- **Problem**: Doesn't handle quoted '?' characters
|
||||
- **Impact**: May incorrectly skip valid data containing '?' in strings
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Recommended Improvements
|
||||
|
||||
### High Priority
|
||||
1. **Add exception handling** around `stof()` calls
|
||||
2. **Implement proper input validation** for malformed data
|
||||
3. **Fix memory layout** to sample-major organization
|
||||
4. **Add const-correct API methods**
|
||||
5. **Optimize string concatenation** in parsing
|
||||
|
||||
### Medium Priority
|
||||
1. **Implement RAII** patterns consistently
|
||||
2. **Add memory usage limits** and validation
|
||||
3. **Provide const reference getters** for large objects
|
||||
4. **Document thread safety** requirements
|
||||
5. **Add comprehensive error reporting**
|
||||
|
||||
### Low Priority
|
||||
1. **Extend ARFF format support** (dates, strings, sparse)
|
||||
2. **Optimize lookup performance** with cached indices
|
||||
3. **Add file path validation**
|
||||
4. **Implement move semantics** for performance
|
||||
|
||||
---
|
||||
|
||||
## 📊 Performance Metrics (Estimated)
|
||||
|
||||
| Dataset Size | Memory Overhead | Performance Impact |
|
||||
|--------------|-----------------|-------------------|
|
||||
| Small (< 1MB) | ~200% | Negligible |
|
||||
| Medium (10MB) | ~300% | Moderate |
|
||||
| Large (100MB+) | ~400% | Significant |
|
||||
|
||||
**Note**: Overhead includes duplicate storage and inefficient layout.
|
||||
|
||||
---
|
||||
|
||||
## 🎯 Conclusion
|
||||
|
||||
The ArffFiles library successfully implements core ARFF parsing functionality but suffers from several design and implementation issues that limit its suitability for production environments. The most critical concerns are:
|
||||
|
||||
1. **Lack of robust error handling** leading to potential crashes
|
||||
2. **Inefficient memory usage** limiting scalability
|
||||
3. **Performance issues** with large datasets
|
||||
|
||||
While functional for small to medium datasets in controlled environments, significant refactoring would be required for production use with large datasets or untrusted input.
|
||||
|
||||
**Recommendation**: Consider this library suitable for prototyping and small-scale applications, but plan for refactoring before production deployment.
|
@@ -137,7 +137,7 @@
|
||||
|
||||
include(CMakeParseArguments)
|
||||
|
||||
option(CODE_COVERAGE_VERBOSE "Verbose information" FALSE)
|
||||
option(CODE_COVERAGE_VERBOSE "Verbose information" TRUE)
|
||||
|
||||
# Check prereqs
|
||||
find_program( GCOV_PATH gcov )
|
||||
@@ -160,7 +160,11 @@ foreach(LANG ${LANGUAGES})
|
||||
endif()
|
||||
elseif(NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "GNU"
|
||||
AND NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "(LLVM)?[Ff]lang")
|
||||
message(FATAL_ERROR "Compiler is not GNU or Flang! Aborting...")
|
||||
if ("${LANG}" MATCHES "CUDA")
|
||||
message(STATUS "Ignoring CUDA")
|
||||
else()
|
||||
message(FATAL_ERROR "Compiler is not GNU or Flang! Aborting...")
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
|
43
conanfile.py
Normal file
43
conanfile.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import re
|
||||
from conan import ConanFile
|
||||
from conan.tools.files import copy
|
||||
|
||||
|
||||
class ArffFilesConan(ConanFile):
|
||||
name = "arff-files"
|
||||
version = "X.X.X"
|
||||
description = (
|
||||
"Header-only library to read ARFF (Attribute-Relation File Format) files and return STL vectors with the data read."
|
||||
)
|
||||
url = "https://github.com/rmontanana/ArffFiles"
|
||||
license = "MIT"
|
||||
homepage = "https://github.com/rmontanana/ArffFiles"
|
||||
topics = ("arff", "data-processing", "file-parsing", "header-only", "cpp17")
|
||||
no_copy_source = True
|
||||
exports_sources = "ArffFiles.hpp", "LICENSE", "README.md"
|
||||
package_type = "header-library"
|
||||
|
||||
def init(self):
|
||||
# Read the CMakeLists.txt file to get the version
|
||||
with open("CMakeLists.txt", "r") as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
if "VERSION" in line:
|
||||
# Extract the version number using regex
|
||||
match = re.search(r"VERSION\s+(\d+\.\d+\.\d+)", line)
|
||||
if match:
|
||||
self.version = match.group(1)
|
||||
|
||||
def package(self):
|
||||
# Copy header file to include directory
|
||||
copy(self, "*.hpp", src=self.source_folder, dst=self.package_folder, keep_path=False)
|
||||
# Copy license and readme for package documentation
|
||||
copy(self, "LICENSE", src=self.source_folder, dst=self.package_folder, keep_path=False)
|
||||
copy(self, "README.md", src=self.source_folder, dst=self.package_folder, keep_path=False)
|
||||
|
||||
def package_info(self):
|
||||
# Header-only library configuration
|
||||
self.cpp_info.bindirs = []
|
||||
self.cpp_info.libdirs = []
|
||||
# Set include directory (header will be in package root)
|
||||
self.cpp_info.includedirs = ["."]
|
@@ -18,7 +18,7 @@ public:
|
||||
TEST_CASE("Version Test", "[ArffFiles]")
|
||||
{
|
||||
ArffFiles arff;
|
||||
REQUIRE(arff.version() == "1.0.0");
|
||||
REQUIRE(arff.version() == "1.1.0");
|
||||
}
|
||||
TEST_CASE("Load Test", "[ArffFiles]")
|
||||
{
|
||||
@@ -65,14 +65,14 @@ TEST_CASE("Load Test", "[ArffFiles]")
|
||||
TEST_CASE("Load with class name", "[ArffFiles]")
|
||||
{
|
||||
ArffFiles arff;
|
||||
arff.load(Paths::datasets("glass"), "Type");
|
||||
arff.load(Paths::datasets("glass"), std::string("Type"));
|
||||
REQUIRE(arff.getClassName() == "Type");
|
||||
REQUIRE(arff.getClassType() == "{ 'build wind float', 'build wind non-float', 'vehic wind float', 'vehic wind non-float', containers, tableware, headlamps}");
|
||||
REQUIRE(arff.getLabels().size() == 6);
|
||||
REQUIRE(arff.getLabels()[0] == "'build wind float'");
|
||||
REQUIRE(arff.getLabels()[1] == "'vehic wind float'");
|
||||
REQUIRE(arff.getLabels()[0] == "build wind float");
|
||||
REQUIRE(arff.getLabels()[1] == "vehic wind float");
|
||||
REQUIRE(arff.getLabels()[2] == "tableware");
|
||||
REQUIRE(arff.getLabels()[3] == "'build wind non-float'");
|
||||
REQUIRE(arff.getLabels()[3] == "build wind non-float");
|
||||
REQUIRE(arff.getLabels()[4] == "headlamps");
|
||||
REQUIRE(arff.getLabels()[5] == "containers");
|
||||
REQUIRE(arff.getSize() == 214);
|
||||
@@ -108,12 +108,41 @@ TEST_CASE("Load with class name as first attribute", "[ArffFiles]")
|
||||
{1.86094, 1.89165, 1.93921, 1.71752},
|
||||
{-0.207383, -0.193249, -0.239664, -0.218572} }
|
||||
};
|
||||
auto X = arff.getX();
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j)
|
||||
REQUIRE(arff.getX()[i][j] == Catch::Approx(expected[i][j]));
|
||||
REQUIRE(X[i][j] == Catch::Approx(expected[i][j]));
|
||||
}
|
||||
auto expected_y = std::vector<int>{ 0, 0, 0, 0 };
|
||||
for (int i = 120; i < 124; ++i)
|
||||
REQUIRE(arff.getY()[i] == expected_y[i - 120]);
|
||||
}
|
||||
TEST_CASE("Adult dataset", "[ArffFiles]")
|
||||
{
|
||||
ArffFiles arff;
|
||||
arff.load(Paths::datasets("adult"), std::string("class"));
|
||||
REQUIRE(arff.getClassName() == "class");
|
||||
REQUIRE(arff.getClassType() == "{ >50K, <=50K }");
|
||||
REQUIRE(arff.getLabels().size() == 2);
|
||||
REQUIRE(arff.getLabels()[0] == "<=50K");
|
||||
REQUIRE(arff.getLabels()[1] == ">50K");
|
||||
REQUIRE(arff.getSize() == 45222);
|
||||
REQUIRE(arff.getLines().size() == 45222);
|
||||
REQUIRE(arff.getLines()[0] == "25, Private, 226802, 11th, 7, Never-married, Machine-op-inspct, Own-child, Black, Male, 0, 0, 40, United-States, <=50K");
|
||||
auto X = arff.getX();
|
||||
REQUIRE(X[0][0] == 25);
|
||||
REQUIRE(X[1][0] == 0);
|
||||
REQUIRE(X[2][0] == 226802);
|
||||
REQUIRE(X[3][0] == 0);
|
||||
REQUIRE(X[4][0] == 7);
|
||||
REQUIRE(X[5][0] == 0);
|
||||
REQUIRE(X[6][0] == 0);
|
||||
REQUIRE(X[7][0] == 0);
|
||||
REQUIRE(X[8][0] == 0);
|
||||
REQUIRE(X[9][0] == 0);
|
||||
REQUIRE(X[10][0] == 0);
|
||||
REQUIRE(X[11][0] == 0);
|
||||
REQUIRE(X[12][0] == 40);
|
||||
REQUIRE(X[13][0] == 0);
|
||||
}
|
||||
|
||||
|
48861
tests/data/adult.arff
Normal file
48861
tests/data/adult.arff
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user