From b0952d38df39862d0302fe8599757131b14219d2 Mon Sep 17 00:00:00 2001 From: Sergey Lisitsyn Date: Tue, 14 May 2024 18:16:17 +0100 Subject: [PATCH] Use simpler naming for logging singleton --- include/tapkee/embed.hpp | 5 ++-- .../tapkee/external/barnes_hut_sne/tsne.hpp | 7 ++--- include/tapkee/neighbors/neighbors.hpp | 8 ++--- .../tapkee/routines/eigendecomposition.hpp | 5 ++-- .../generalized_eigendecomposition.hpp | 5 ++-- include/tapkee/utils/logging.hpp | 14 ++++----- include/tapkee/utils/time.hpp | 2 +- src/cli/main.cpp | 30 +++++++++---------- 8 files changed, 36 insertions(+), 40 deletions(-) diff --git a/include/tapkee/embed.hpp b/include/tapkee/embed.hpp index e268689..8fc6ce3 100644 --- a/include/tapkee/embed.hpp +++ b/include/tapkee/embed.hpp @@ -104,7 +104,7 @@ TapkeeOutput embed(RandomAccessIterator begin, RandomAccessIterator end, KernelC parameters.check(); parameters.merge(tapkee_internal::defaults); parameters.visit([] (const stichwort::Parameter& p) { - tapkee::LoggingSingleton::instance().message_debug(fmt::format("Parameter {} = [{}]", p.name(), p.repr())); + tapkee::Logging::instance().message_debug(fmt::format("Parameter {} = [{}]", p.name(), p.repr())); }); DimensionReductionMethod selected_method = parameters[method]; @@ -114,8 +114,7 @@ TapkeeOutput embed(RandomAccessIterator begin, RandomAccessIterator end, KernelC tapkee_internal::Context context(progress_function_ptr, cancel_function_ptr); - LoggingSingleton::instance().message_info( - fmt::format("Using the {} method.", get_method_name(selected_method))); + Logging::instance().message_info(fmt::format("Using the {} method.", get_method_name(selected_method))); output = tapkee_internal::initialize(begin, end, kernel_callback, distance_callback, features_callback, parameters, context) diff --git a/include/tapkee/external/barnes_hut_sne/tsne.hpp b/include/tapkee/external/barnes_hut_sne/tsne.hpp index 7507f44..6cfae3b 100644 --- a/include/tapkee/external/barnes_hut_sne/tsne.hpp +++ b/include/tapkee/external/barnes_hut_sne/tsne.hpp @@ -63,9 +63,9 @@ class TSNE // Determine whether we are using an exact algorithm bool exact = (theta == .0) ? true : false; if (exact) - tapkee::LoggingSingleton::instance().message_info("Using exact t-SNE algorithm"); + tapkee::Logging::instance().message_info("Using exact t-SNE algorithm"); else - tapkee::LoggingSingleton::instance().message_info("Using Barnes-Hut-SNE algorithm"); + tapkee::Logging::instance().message_info("Using Barnes-Hut-SNE algorithm"); // Set learning parameters int max_iter = 1000, stop_lying_iter = 250, mom_switch_iter = 250; @@ -188,8 +188,7 @@ class TSNE C = evaluateError(P.data(), Y, N); else C = evaluateError(row_P, col_P, val_P, Y, N, theta); // doing approximate computation here! - tapkee::LoggingSingleton::instance().message_info( - fmt::format("Iteration {}: error is {}", iter, C)); + tapkee::Logging::instance().message_info(fmt::format("Iteration {}: error is {}", iter, C)); } } // Clean up memory diff --git a/include/tapkee/neighbors/neighbors.hpp b/include/tapkee/neighbors/neighbors.hpp index 180b047..b9da946 100644 --- a/include/tapkee/neighbors/neighbors.hpp +++ b/include/tapkee/neighbors/neighbors.hpp @@ -176,11 +176,11 @@ Neighbors find_neighbors(NeighborsMethod method, const RandomAccessIterator& beg { if (k > static_cast(end - begin - 1)) { - LoggingSingleton::instance().message_warning("Number of neighbors is greater than number of objects to embed. " + Logging::instance().message_warning("Number of neighbors is greater than number of objects to embed. " "Using greatest possible number of neighbors."); k = static_cast(end - begin - 1); } - LoggingSingleton::instance().message_info("Using the " + get_neighbors_method_name(method) + + Logging::instance().message_info("Using the " + get_neighbors_method_name(method) + " neighbors computation method."); Neighbors neighbors; @@ -199,7 +199,7 @@ Neighbors find_neighbors(NeighborsMethod method, const RandomAccessIterator& beg "is not connected. Recomputing with a " "larger number of neighbors {}.", k, 2 * k); - LoggingSingleton::instance().message_warning(message); + Logging::instance().message_warning(message); neighbors = find_neighbors(method, begin, end, callback, 2 * k, check_connectivity); } else @@ -207,7 +207,7 @@ Neighbors find_neighbors(NeighborsMethod method, const RandomAccessIterator& beg const std::string message = fmt::format("The neighborhood graph with {} neighbors " "is connected.", k); - LoggingSingleton::instance().message_info(message); + Logging::instance().message_info(message); } return neighbors; } diff --git a/include/tapkee/routines/eigendecomposition.hpp b/include/tapkee/routines/eigendecomposition.hpp index 18ac8d2..fcd781d 100644 --- a/include/tapkee/routines/eigendecomposition.hpp +++ b/include/tapkee/routines/eigendecomposition.hpp @@ -37,7 +37,7 @@ EigendecompositionResult eigendecomposition_impl_arpack(const MatrixType& wm, In if (arpack.info() == Eigen::Success) { std::string message = fmt::format("Took {} iterations.", arpack.getNbrIterations()); - LoggingSingleton::instance().message_info(message); + Logging::instance().message_info(message); DenseMatrix selected_eigenvectors = arpack.eigenvectors().rightCols(target_dimension); return EigendecompositionResult(selected_eigenvectors, arpack.eigenvalues().tail(target_dimension)); } @@ -329,8 +329,7 @@ EigendecompositionResult eigendecomposition(const EigenMethod& method, const Com const EigendecompositionStrategy& eigen_strategy, const MatrixType& m, IndexType target_dimension) { - LoggingSingleton::instance().message_info( - fmt::format("Using the {} eigendecomposition method.", get_eigen_method_name(method))); + Logging::instance().message_info(fmt::format("Using the {} eigendecomposition method.", get_eigen_method_name(method))); #ifdef TAPKEE_WITH_ARPACK if (method.is(Arpack)) return eigendecomposition_impl().arpack(m, strategy, eigen_strategy, target_dimension); diff --git a/include/tapkee/routines/generalized_eigendecomposition.hpp b/include/tapkee/routines/generalized_eigendecomposition.hpp index 9240552..1b29b85 100644 --- a/include/tapkee/routines/generalized_eigendecomposition.hpp +++ b/include/tapkee/routines/generalized_eigendecomposition.hpp @@ -30,7 +30,7 @@ EigendecompositionResult generalized_eigendecomposition_impl_arpack(const LMatri if (arpack.info() == Eigen::Success) { std::string message = fmt::format("Took {} iterations.", arpack.getNbrIterations()); - LoggingSingleton::instance().message_info(message); + Logging::instance().message_info(message); DenseMatrix selected_eigenvectors = (arpack.eigenvectors()).rightCols(target_dimension); return EigendecompositionResult(selected_eigenvectors, arpack.eigenvalues().tail(target_dimension)); } @@ -169,8 +169,7 @@ EigendecompositionResult generalized_eigendecomposition(const EigenMethod& metho const LMatrixType& lhs, const RMatrixType& rhs, IndexType target_dimension) { - LoggingSingleton::instance().message_info( - fmt::format("Using the {} eigendecomposition method.", get_eigen_method_name(method))); + Logging::instance().message_info(fmt::format("Using the {} eigendecomposition method.", get_eigen_method_name(method))); #ifdef TAPKEE_WITH_ARPACK if (method.is(Arpack)) return generalized_eigendecomposition_impl().arpack(lhs, rhs, strategy, diff --git a/include/tapkee/utils/logging.hpp b/include/tapkee/utils/logging.hpp index 07f8dec..201a9f9 100644 --- a/include/tapkee/utils/logging.hpp +++ b/include/tapkee/utils/logging.hpp @@ -82,19 +82,19 @@ class DefaultLoggerImplementation : public LoggerImplementation //! Main logging singleton used by the library. Can use provided //! @ref LoggerImplementation if necessary. By default uses //! @ref DefaultLoggerImplementation. -class LoggingSingleton +class Logging { private: - LoggingSingleton() + Logging() : impl(new DefaultLoggerImplementation), LEVEL_ENABLED_FIELD_INITIALIZER(info, false), LEVEL_ENABLED_FIELD_INITIALIZER(warning, true), LEVEL_ENABLED_FIELD_INITIALIZER(debug, false), LEVEL_ENABLED_FIELD_INITIALIZER(error, true), LEVEL_ENABLED_FIELD_INITIALIZER(benchmark, false){}; - ~LoggingSingleton() + ~Logging() { delete impl; } - LoggingSingleton(const LoggingSingleton& ls); - void operator=(const LoggingSingleton& ls); + Logging(const Logging& ls) = delete; + void operator=(const Logging& ls) = delete; LoggerImplementation* impl; @@ -106,9 +106,9 @@ class LoggingSingleton public: //! @return instance of the singleton - static LoggingSingleton& instance() + static Logging& instance() { - static LoggingSingleton s; + static Logging s; return s; } diff --git a/include/tapkee/utils/time.hpp b/include/tapkee/utils/time.hpp index 9e70b34..c2ed070 100644 --- a/include/tapkee/utils/time.hpp +++ b/include/tapkee/utils/time.hpp @@ -37,7 +37,7 @@ struct timed_context { std::string message = fmt::format("{} took {} seconds.", operation_name, double(CLOCK_GET - start_clock) / CLOCK_DIVISOR); - LoggingSingleton::instance().message_benchmark(message); + Logging::instance().message_benchmark(message); } }; } // namespace tapkee_internal diff --git a/src/cli/main.cpp b/src/cli/main.cpp index b22919d..62fc928 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -279,18 +279,18 @@ int run(int argc, const char **argv) } if (opt.count(VERBOSE_KEYWORD)) { - tapkee::LoggingSingleton::instance().enable_info(); + tapkee::Logging::instance().enable_info(); } if (opt.count(DEBUG_KEYWORD)) { - tapkee::LoggingSingleton::instance().enable_debug(); - tapkee::LoggingSingleton::instance().message_info("Debug messages enabled"); + tapkee::Logging::instance().enable_debug(); + tapkee::Logging::instance().message_info("Debug messages enabled"); } if (opt.count(BENCHMARK_KEYWORD)) { - tapkee::LoggingSingleton::instance().enable_benchmark(); - tapkee::LoggingSingleton::instance().message_info("Benchmarking enabled"); + tapkee::Logging::instance().enable_benchmark(); + tapkee::Logging::instance().message_info("Benchmarking enabled"); } tapkee::DimensionReductionMethod tapkee_method = tapkee::PassThru; @@ -302,7 +302,7 @@ int run(int argc, const char **argv) } catch (const std::exception &) { - tapkee::LoggingSingleton::instance().message_error(string("Unknown method ") + method); + tapkee::Logging::instance().message_error(string("Unknown method ") + method); return 1; } } @@ -316,7 +316,7 @@ int run(int argc, const char **argv) } catch (const std::exception &) { - tapkee::LoggingSingleton::instance().message_error(string("Unknown neighbors method ") + method); + tapkee::Logging::instance().message_error(string("Unknown neighbors method ") + method); return 1; } } @@ -329,7 +329,7 @@ int run(int argc, const char **argv) } catch (const std::exception &) { - tapkee::LoggingSingleton::instance().message_error(string("Unknown eigendecomposition method ") + method); + tapkee::Logging::instance().message_error(string("Unknown eigendecomposition method ") + method); return 1; } } @@ -342,7 +342,7 @@ int run(int argc, const char **argv) } catch (const std::exception &) { - tapkee::LoggingSingleton::instance().message_error(string("Unknown computation strategy ") + method); + tapkee::Logging::instance().message_error(string("Unknown computation strategy ") + method); return 1; } } @@ -350,7 +350,7 @@ int run(int argc, const char **argv) int target_dim = opt[TARGET_DIMENSION_KEYWORD].as(); if (target_dim < 0) { - tapkee::LoggingSingleton::instance().message_error( + tapkee::Logging::instance().message_error( "Negative target dimensionality is not possible in current circumstances. " "Please visit other universe"); return 1; @@ -359,20 +359,20 @@ int run(int argc, const char **argv) int k = opt[NUM_NEIGHBORS_KEYWORD].as(); if (k < 3) { - tapkee::LoggingSingleton::instance().message_error( + tapkee::Logging::instance().message_error( "The provided number of neighbors is too small, consider at least 3."); return 1; } double width = opt[GAUSSIAN_WIDTH_KEYWORD].as(); if (width < 0.0) { - tapkee::LoggingSingleton::instance().message_error("Width of the gaussian kernel is negative."); + tapkee::Logging::instance().message_error("Width of the gaussian kernel is negative."); return 1; } int timesteps = opt[TIMESTEPS_KEYWORD].as(); if (timesteps < 0) { - tapkee::LoggingSingleton::instance().message_error("Number of timesteps is negative."); + tapkee::Logging::instance().message_error("Number of timesteps is negative."); return 1; } double eigenshift = opt[EIGENSHIFT_KEYWORD].as(); @@ -414,7 +414,7 @@ int run(int argc, const char **argv) std::stringstream ss; ss << "Data contains " << input_data.cols() << " feature vectors with dimension of " << input_data.rows(); - tapkee::LoggingSingleton::instance().message_info(ss.str()); + tapkee::Logging::instance().message_info(ss.str()); tapkee::TapkeeOutput output; @@ -481,7 +481,7 @@ int run(int argc, const char **argv) dynamic_cast(output.projection.implementation.get()); if (!matrix_projection) { - tapkee::LoggingSingleton::instance().message_error("Projection function unavailable"); + tapkee::Logging::instance().message_error("Projection function unavailable"); return 1; } write_matrix(&matrix_projection->proj_mat, ofs_matrix, delimiter[0]);