mlpack 3.4.2
range_search_utils.hpp
Go to the documentation of this file.
1
12#ifndef MLPACK_TESTS_MAIN_TESTS_RANGE_SEARCH_TEST_UTILS_HPP
13#define MLPACK_TESTS_MAIN_TESTS_RANGE_SEARCH_TEST_UTILS_HPP
14
16#include <mlpack/core.hpp>
18#include "../catch.hpp"
19
25inline std::string ModelToString(RSModel* model)
26{
27 std::ostringstream oss;
28 boost::archive::text_oarchive oa(oss);
29 oa << model;
30 return oss.str();
31}
32
41inline void CheckMatrices(std::vector<std::vector<double>>& vec1,
42 std::vector<std::vector<double>>& vec2,
43 const double tolerance = 1e-3)
44{
45 REQUIRE(vec1.size() == vec2.size());
46 for (size_t i = 0; i < vec1.size(); ++i)
47 {
48 REQUIRE(vec1[i].size() == vec2[i].size());
49 std::sort(vec1[i].begin(), vec1[i].end());
50 std::sort(vec2[i].begin(), vec2[i].end());
51 for (size_t j = 0 ; j < vec1[i].size(); ++j)
52 {
53 REQUIRE(vec1[i][j] == Approx(vec2[i][j]).epsilon(tolerance));
54 }
55 }
56}
57
64inline void CheckMatrices(std::vector<std::vector<size_t>>& vec1,
65 std::vector<std::vector<size_t>>& vec2)
66{
67 REQUIRE(vec1.size() == vec2.size());
68 for (size_t i = 0; i < vec1.size(); ++i)
69 {
70 REQUIRE(vec1[i].size() == vec2[i].size());
71 std::sort(vec1[i].begin(), vec1[i].end());
72 std::sort(vec2[i].begin(), vec2[i].end());
73 for (size_t j = 0; j < vec1[i].size(); ++j)
74 {
75 REQUIRE(vec1[i][j] == vec2[i][j]);
76 }
77 }
78}
79
87template<typename T>
88std::vector<std::vector<T>> ReadData(const std::string& filename)
89{
90 std::ifstream ifs(filename);
91 std::vector<std::vector<T>> table;
92 std::string line;
93 while (std::getline(ifs, line))
94 {
95 std::vector<T> numbers;
96 T n;
97 std::replace(line.begin(), line.end(), ',', ' ');
98 std::istringstream stm(line);
99 while (stm >> n)
100 numbers.push_back(n);
101 table.push_back(numbers);
102 }
103
104 return table;
105}
106
107#endif
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
std::string ModelToString(RSModel *model)
Convert a model to a string using the text_oarchive of boost::serialization.
std::vector< std::vector< T > > ReadData(const std::string &filename)
Load a CSV file into a vector of vector with a templated datatype.
void CheckMatrices(std::vector< std::vector< double > > &vec1, std::vector< std::vector< double > > &vec2, const double tolerance=1e-3)
Check that 2 matrices of type vector<vector<double>> are close to equal, using the given tolerance.