From cb3659b22503e02036a42df0f33227d60ff534f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Wed, 3 Jul 2024 23:43:08 +0200 Subject: [PATCH] Add coypright header to sources Fix coverage report Add coverage badge to README --- BinDisc.cpp | 6 +++ BinDisc.h | 6 +++ CMakeLists.txt | 11 +++++- CPPFImdlp.cpp | 6 +++ CPPFImdlp.h | 6 +++ Discretizer.cpp | 11 +++--- Discretizer.h | 7 +++- Makefile | 25 +++++++++++-- Metrics.cpp | 6 +++ Metrics.h | 6 +++ README.md | 10 ++--- sample/sample.cpp | 6 +++ tests/ArffFiles.cpp | 6 +++ tests/ArffFiles.h | 6 +++ tests/BinDisc_unittest.cpp | 6 +++ tests/Discretizer_unittest.cpp | 45 +++++++++++++++++++++-- tests/Experiments.hpp | 6 +++ tests/FImdlp_unittest.cpp | 6 +++ tests/Metrics_unittest.cpp | 6 +++ tests/Testing/Temporary/CTestCostData.txt | 1 - tests/Testing/Temporary/LastTest.log | 3 -- tests/test | 15 -------- tests/tests_do.py | 6 +++ update_coverage.py | 38 +++++++++++++++++++ 24 files changed, 213 insertions(+), 37 deletions(-) delete mode 100644 tests/Testing/Temporary/CTestCostData.txt delete mode 100644 tests/Testing/Temporary/LastTest.log delete mode 100755 tests/test create mode 100644 update_coverage.py diff --git a/BinDisc.cpp b/BinDisc.cpp index 8b3028c..e5f32f2 100644 --- a/BinDisc.cpp +++ b/BinDisc.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include #include #include "BinDisc.h" diff --git a/BinDisc.h b/BinDisc.h index eaa7ddf..55a35ce 100644 --- a/BinDisc.h +++ b/BinDisc.h @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #ifndef BINDISC_H #define BINDISC_H diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f35cce..6bbdb1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,15 @@ include_directories(${TORCH_INCLUDE_DIRS}) add_library(mdlp CPPFImdlp.cpp Metrics.cpp BinDisc.cpp Discretizer.cpp) target_link_libraries(mdlp "${TORCH_LIBRARIES}") add_subdirectory(sample) +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-elide-constructors") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") +if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-default-inline") +endif() if (ENABLE_TESTING) + MESSAGE("Debug mode") + enable_testing() + set(CODE_COVERAGE ON) + SET(GCC_COVERAGE_LINK_FLAGS " ${GCC_COVERAGE_LINK_FLAGS} -lgcov --coverage") add_subdirectory(tests) -endif(ENABLE_TESTING) +endif(ENABLE_TESTING) \ No newline at end of file diff --git a/CPPFImdlp.cpp b/CPPFImdlp.cpp index 30c9bbb..8c953b1 100644 --- a/CPPFImdlp.cpp +++ b/CPPFImdlp.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include #include #include diff --git a/CPPFImdlp.h b/CPPFImdlp.h index b832423..45fa65c 100644 --- a/CPPFImdlp.h +++ b/CPPFImdlp.h @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #ifndef CPPFIMDLP_H #define CPPFIMDLP_H diff --git a/Discretizer.cpp b/Discretizer.cpp index 1f5615c..975a781 100644 --- a/Discretizer.cpp +++ b/Discretizer.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include "Discretizer.h" namespace mdlp { @@ -14,11 +20,6 @@ namespace mdlp { for (const precision_t& item : data) { auto pos = bound(first, last, item); int number = pos - first; - /* - OJO - */ - if (number < 0) - throw std::runtime_error("number is less than 0 in discretizer::transform"); discretizedData.push_back(number); } return discretizedData; diff --git a/Discretizer.h b/Discretizer.h index 423781e..edfd7e8 100644 --- a/Discretizer.h +++ b/Discretizer.h @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #ifndef DISCRETIZER_H #define DISCRETIZER_H @@ -24,7 +30,6 @@ namespace mdlp { torch::Tensor fit_transform_t(torch::Tensor& X_, torch::Tensor& y_); static inline std::string version() { return "1.2.3"; }; protected: - void normalize_cutpoints(); labels_t discretizedData = labels_t(); cutPoints_t cutPoints; // At least two cutpoints must be provided, the first and the last will be ignored in transform bound_dir_t direction; // used in transform diff --git a/Makefile b/Makefile index eb45104..c0e145b 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,32 @@ SHELL := /bin/bash .DEFAULT_GOAL := build .PHONY: build test +lcov := lcov build: @if [ -d build_release ]; then rm -fr build_release; fi @mkdir build_release @cmake -B build_release -S . -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTING=OFF - @cmake --build build_release + @cmake --build build_release -j 8 test: - @echo "Testing..." - @cd tests && ./test + @if [ -d build_debug ]; then rm -fr build_debug; fi + @mkdir build_debug + @cmake -B build_debug -S . -DCMAKE_BUILD_TYPE=Debug -DENABLE_TESTING=ON + @cmake --build build_debug -j 8 + @cd build_debug/tests && ctest --output-on-failure -j 8 + @cd build_debug/tests && $(lcov) --capture --directory ../ --demangle-cpp --ignore-errors source,source --ignore-errors mismatch --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info '/usr/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'lib/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'libtorch/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'tests/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'gtest/*' --output-file coverage.info >/dev/null 2>&1; + @genhtml build_debug/tests/coverage.info --demangle-cpp --output-directory build_debug/tests/coverage --title "Discretizer mdlp Coverage Report" -s -k -f --legend + @echo "* Coverage report is generated at build_debug/tests/coverage/index.html" + @which python || (echo ">>> Please install python"; exit 1) + @if [ ! -f build_debug/tests/coverage.info ]; then \ + echo ">>> No coverage.info file found!"; \ + exit 1; \ + fi + @echo ">>> Updating coverage badge..." + @env python update_coverage.py build_debug/tests \ No newline at end of file diff --git a/Metrics.cpp b/Metrics.cpp index f3405e9..b76fbcc 100644 --- a/Metrics.cpp +++ b/Metrics.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include "Metrics.h" #include #include diff --git a/Metrics.h b/Metrics.h index 4f8151a..eea4d4b 100644 --- a/Metrics.h +++ b/Metrics.h @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #ifndef CCMETRICS_H #define CCMETRICS_H diff --git a/README.md b/README.md index a42900f..9bd53f9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ [![Build](https://github.com/rmontanana/mdlp/actions/workflows/build.yml/badge.svg)](https://github.com/rmontanana/mdlp/actions/workflows/build.yml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=rmontanana_mdlp&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=rmontanana_mdlp) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=rmontanana_mdlp&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=rmontanana_mdlp) +[![Coverage Badge](https://img.shields.io/badge/Coverage-100,0%25-green)](html/index.html) # logo mdlp @@ -31,15 +32,14 @@ Other features: To run the sample, just execute the following commands: ```bash -cmake -B build -S . -cmake --build build -build/sample/sample -f iris -m 2 -build/sample/sample -h +make build +build_release/sample/sample -f iris -m 2 +build_release/sample/sample -h ``` ## Test -To run the tests and see coverage (llvm & gcovr have to be installed), execute the following commands: +To run the tests and see coverage (llvm with lcov and genhtml have to be installed), execute the following commands: ```bash make test diff --git a/sample/sample.cpp b/sample/sample.cpp index 654a0cc..b5604ff 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include #include #include diff --git a/tests/ArffFiles.cpp b/tests/ArffFiles.cpp index a95e244..3b88117 100644 --- a/tests/ArffFiles.cpp +++ b/tests/ArffFiles.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include "ArffFiles.h" #include #include diff --git a/tests/ArffFiles.h b/tests/ArffFiles.h index f36d9d3..985dce0 100644 --- a/tests/ArffFiles.h +++ b/tests/ArffFiles.h @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #ifndef ARFFFILES_H #define ARFFFILES_H diff --git a/tests/BinDisc_unittest.cpp b/tests/BinDisc_unittest.cpp index 2008036..db0e051 100644 --- a/tests/BinDisc_unittest.cpp +++ b/tests/BinDisc_unittest.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include #include #include diff --git a/tests/Discretizer_unittest.cpp b/tests/Discretizer_unittest.cpp index 98da775..d8a7f7f 100644 --- a/tests/Discretizer_unittest.cpp +++ b/tests/Discretizer_unittest.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include #include #include @@ -20,7 +26,7 @@ namespace mdlp { return "../../tests/datasets/"; } const std::string data_path = set_data_path(); - + const labels_t iris_quantile = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3, 3, 3, 1, 3, 1, 2, 0, 3, 1, 0, 2, 2, 2, 1, 3, 1, 2, 2, 1, 2, 2, 2, 2, 3, 3, 3, 3, 2, 1, 1, 1, 2, 2, 1, 2, 3, 2, 1, 1, 1, 2, 2, 0, 1, 1, 1, 2, 1, 1, 2, 2, 3, 2, 3, 3, 0, 3, 3, 3, 3, 3, 3, 1, 2, 3, 3, 3, 3, 2, 3, 1, 3, 2, 3, 3, 2, 2, 3, 3, 3, 3, 3, 2, 2, 3, 2, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 2, 2 }; TEST(Discretizer, Version) { Discretizer* disc = new BinDisc(4, strategy_t::UNIFORM); @@ -51,9 +57,42 @@ namespace mdlp { auto y = labels_t(); disc->fit(X[0], y); auto Xt = disc->transform(X[0]); - labels_t expected = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3, 3, 3, 1, 3, 1, 2, 0, 3, 1, 0, 2, 2, 2, 1, 3, 1, 2, 2, 1, 2, 2, 2, 2, 3, 3, 3, 3, 2, 1, 1, 1, 2, 2, 1, 2, 3, 2, 1, 1, 1, 2, 2, 0, 1, 1, 1, 2, 1, 1, 2, 2, 3, 2, 3, 3, 0, 3, 3, 3, 3, 3, 3, 1, 2, 3, 3, 3, 3, 2, 3, 1, 3, 2, 3, 3, 2, 2, 3, 3, 3, 3, 3, 2, 2, 3, 2, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 2, 2 }; delete disc; - EXPECT_EQ(expected, Xt); + EXPECT_EQ(iris_quantile, Xt); + } + + TEST(Discretizer, BinIrisQuantileTorch) + { + ArffFiles file; + Discretizer* disc = new BinDisc(4, strategy_t::QUANTILE); + file.load(data_path + "iris.arff", true); + auto X = file.getX(); + auto y = file.getY(); + auto X_torch = torch::tensor(X[0], torch::kFloat32); + auto yt = torch::tensor(y, torch::kInt32); + disc->fit_t(X_torch, yt); + torch::Tensor Xt = disc->transform_t(X_torch); + delete disc; + EXPECT_EQ(iris_quantile.size(), Xt.size(0)); + for (int i = 0; i < iris_quantile.size(); ++i) { + EXPECT_EQ(iris_quantile.at(i), Xt[i].item()); + } + } + TEST(Discretizer, BinIrisQuantileTorchFit_transform) + { + ArffFiles file; + Discretizer* disc = new BinDisc(4, strategy_t::QUANTILE); + file.load(data_path + "iris.arff", true); + auto X = file.getX(); + auto y = file.getY(); + auto X_torch = torch::tensor(X[0], torch::kFloat32); + auto yt = torch::tensor(y, torch::kInt32); + torch::Tensor Xt = disc->fit_transform_t(X_torch, yt); + delete disc; + EXPECT_EQ(iris_quantile.size(), Xt.size(0)); + for (int i = 0; i < iris_quantile.size(); ++i) { + EXPECT_EQ(iris_quantile.at(i), Xt[i].item()); + } } TEST(Discretizer, FImdlpIris) diff --git a/tests/Experiments.hpp b/tests/Experiments.hpp index ba9d948..d122529 100644 --- a/tests/Experiments.hpp +++ b/tests/Experiments.hpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #ifndef EXPERIMENTS_HPP #define EXPERIMENTS_HPP #include diff --git a/tests/FImdlp_unittest.cpp b/tests/FImdlp_unittest.cpp index d68b983..38fe830 100644 --- a/tests/FImdlp_unittest.cpp +++ b/tests/FImdlp_unittest.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include "gtest/gtest.h" #include "../Metrics.h" #include "../CPPFImdlp.h" diff --git a/tests/Metrics_unittest.cpp b/tests/Metrics_unittest.cpp index 83f5cb8..990321b 100644 --- a/tests/Metrics_unittest.cpp +++ b/tests/Metrics_unittest.cpp @@ -1,3 +1,9 @@ +// **************************************************************** +// SPDX - FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX - FileType: SOURCE +// SPDX - License - Identifier: MIT +// **************************************************************** + #include "gtest/gtest.h" #include "../Metrics.h" diff --git a/tests/Testing/Temporary/CTestCostData.txt b/tests/Testing/Temporary/CTestCostData.txt deleted file mode 100644 index ed97d53..0000000 --- a/tests/Testing/Temporary/CTestCostData.txt +++ /dev/null @@ -1 +0,0 @@ ---- diff --git a/tests/Testing/Temporary/LastTest.log b/tests/Testing/Temporary/LastTest.log deleted file mode 100644 index 63c81d7..0000000 --- a/tests/Testing/Temporary/LastTest.log +++ /dev/null @@ -1,3 +0,0 @@ -Start testing: Jul 03 18:09 CEST ----------------------------------------------------------- -End testing: Jul 03 18:09 CEST diff --git a/tests/test b/tests/test deleted file mode 100755 index eba31ef..0000000 --- a/tests/test +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash -if [ -d build ] && [ "$1" != "run" ]; then - rm -fr build -fi -if [ -d gcovr-report ] ; then - rm -fr gcovr-report -fi -cmake -S . -B build -Wno-dev -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="--coverage" -DCMAKE_C_FLAGS="--coverage" -cmake --build build -cd build -ctest --output-on-failure -j 8 -cd .. -mkdir gcovr-report -cd .. -gcovr --gcov-filter "CPPFImdlp.cpp" --gcov-filter "Metrics.cpp" --gcov-filter "BinDisc.cpp" --gcov-filter "Discretizer.cpp" --txt --sonarqube=tests/gcovr-report/coverage.xml --exclude-noncode-lines diff --git a/tests/tests_do.py b/tests/tests_do.py index 46bb52c..357d073 100644 --- a/tests/tests_do.py +++ b/tests/tests_do.py @@ -1,3 +1,9 @@ +# *************************************************************** +# SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +# SPDX-FileType: SOURCE +# SPDX-License-Identifier: MIT +# *************************************************************** + import json from sklearn.preprocessing import KBinsDiscretizer diff --git a/update_coverage.py b/update_coverage.py new file mode 100644 index 0000000..1a5a213 --- /dev/null +++ b/update_coverage.py @@ -0,0 +1,38 @@ +# *************************************************************** +# SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +# SPDX-FileType: SOURCE +# SPDX-License-Identifier: MIT +# *************************************************************** + +import subprocess +import sys + +readme_file = "README.md" +print("Updating coverage...") +# Generate badge line +output = subprocess.check_output( + "lcov --summary " + sys.argv[1] + "/coverage.info", + shell=True, +) +value = output.decode("utf-8").strip() +percentage = 0 +for line in value.splitlines(): + if "lines" in line: + percentage = float(line.split(":")[1].split("%")[0]) + break +print(f"Coverage: {percentage}%") +if percentage < 90: + print("⛔Coverage is less than 90%. I won't update the badge.") + sys.exit(1) +percentage_label = str(percentage).replace(".", ",") +coverage_line = f"[![Coverage Badge](https://img.shields.io/badge/Coverage-{percentage_label}%25-green)](html/index.html)" +# Update README.md +with open(readme_file, "r") as f: + lines = f.readlines() +with open(readme_file, "w") as f: + for line in lines: + if "img.shields.io/badge/Coverage" in line: + f.write(coverage_line + "\n") + else: + f.write(line) +print(f"✅Coverage updated with value: {percentage}")