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 
25 inline std::string ModelToString(RSModel* model)
26 {
27  std::ostringstream oss;
28  cereal::JSONOutputArchive oa(oss);
29  oa(CEREAL_POINTER(model));
30  return oss.str();
31 }
32 
41 inline 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 
64 inline 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 
87 template<typename T>
88 std::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
auto replace(Range &&rng, T const &old, T const &value) -> enable_if_t< is_range< Range >::value >
Definition: algorithm.hpp:1022
constexpr auto size(Container const &container) noexcept -> decltype(container.size())
Definition: iterator.hpp:29
auto sort(Range &&rng) -> enable_if_t< is_range< Range >::value >
Definition: algorithm.hpp:1369
std::string ModelToString(RSModel *model)
Convert a model to a string using the text_oarchive of cereal.
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
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...
std::vector< std::vector< T > > ReadData(const std::string &filename)
Load a CSV file into a vector of vector with a templated datatype.
#define CEREAL_POINTER(T)
Cereal does not support the serialization of raw pointer.