histogram_fill_test.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Copyright 2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <array>
  7. #include <boost/config.hpp>
  8. #include <boost/core/ignore_unused.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/histogram/accumulators.hpp>
  11. #include <boost/histogram/accumulators/ostream.hpp>
  12. #include <boost/histogram/algorithm/sum.hpp>
  13. #include <boost/histogram/axis/category.hpp>
  14. #include <boost/histogram/axis/integer.hpp>
  15. #include <boost/histogram/axis/ostream.hpp>
  16. #include <boost/histogram/histogram.hpp>
  17. #include <boost/histogram/literals.hpp>
  18. #include <boost/histogram/make_histogram.hpp>
  19. #include <boost/histogram/ostream.hpp>
  20. #include <boost/histogram/storage_adaptor.hpp>
  21. #include <boost/variant2/variant.hpp>
  22. #include <random>
  23. #include <sstream>
  24. #include <stdexcept>
  25. #include <tuple>
  26. #include <utility>
  27. #include <vector>
  28. #include "throw_exception.hpp"
  29. #include "utility_histogram.hpp"
  30. using namespace boost::histogram;
  31. using namespace boost::histogram::algorithm;
  32. using namespace boost::histogram::literals; // to get _c suffix
  33. using boost::variant2::variant;
  34. constexpr auto ndata = 1 << 16; // should be larger than index buffer in fill_n
  35. using in = axis::integer<int, axis::null_type>;
  36. using in0 = axis::integer<int, axis::null_type, axis::option::none_t>;
  37. using ing = axis::integer<double, axis::null_type,
  38. decltype(axis::option::growth | axis::option::underflow |
  39. axis::option::overflow)>;
  40. using cs = axis::category<std::string, axis::null_type>;
  41. using csg = axis::category<std::string, axis::null_type, axis::option::growth_t>;
  42. struct axis2d {
  43. auto size() const { return axis::index_type{2}; }
  44. auto index(const std::tuple<double, double>& xy) const {
  45. const auto x = std::get<0>(xy);
  46. const auto y = std::get<1>(xy);
  47. const auto r = std::sqrt(x * x + y * y);
  48. return std::min(static_cast<axis::index_type>(r), size());
  49. }
  50. friend std::ostream& operator<<(std::ostream& os, const axis2d&) {
  51. os << "axis2d()";
  52. return os;
  53. }
  54. };
  55. template <class Tag>
  56. void run_tests(const std::vector<int>& x, const std::vector<int>& y,
  57. const std::vector<double>& w) {
  58. // 1D simple A
  59. {
  60. auto h = make(Tag(), in{1, 3});
  61. auto h2 = h;
  62. for (auto&& xi : x) h(xi);
  63. // uses 1D specialization
  64. h2.fill(x);
  65. BOOST_TEST_EQ(h, h2);
  66. }
  67. // 1D simple B
  68. {
  69. auto h = make(Tag(), in{1, 3});
  70. auto h2 = h;
  71. for (auto&& xi : x) h(xi);
  72. // uses generic form
  73. const auto vx = {x};
  74. h2.fill(vx);
  75. BOOST_TEST_EQ(h, h2);
  76. }
  77. // 1D simple C
  78. {
  79. auto h = make(Tag(), in{1, 3});
  80. auto h2 = h;
  81. h(1);
  82. for (auto&& xi : x) h(xi);
  83. // uses variant
  84. boost::variant2::variant<int, std::vector<int>, std::string> v[1];
  85. v[0] = 1;
  86. h2.fill(v);
  87. v[0] = x;
  88. h2.fill(v);
  89. BOOST_TEST_EQ(h, h2);
  90. }
  91. // 1D bad arguments
  92. {
  93. auto h = make(Tag(), in{1, 3});
  94. int bad1[2][4];
  95. boost::ignore_unused(bad1);
  96. BOOST_TEST_THROWS(h.fill(bad1), std::invalid_argument);
  97. std::vector<std::array<int, 4>> bad2;
  98. boost::ignore_unused(bad2);
  99. BOOST_TEST_THROWS(h.fill(bad2), std::invalid_argument);
  100. }
  101. // 1D with category axis
  102. {
  103. auto h = make(Tag(), cs{"A", "B"});
  104. auto h2 = h;
  105. const auto s = {"A", "B", "C"};
  106. for (auto&& si : s) h(si);
  107. h2.fill(s);
  108. variant<int, std::string, std::vector<std::string>> v[1];
  109. h("B");
  110. v[0] = "B";
  111. h2.fill(v);
  112. v[0] = std::vector<std::string>(s.begin(), s.end());
  113. for (auto&& si : s) h(si);
  114. h2.fill(v);
  115. BOOST_TEST_EQ(h, h2);
  116. }
  117. // 1D weight
  118. {
  119. auto h = make(Tag(), in{1, 3});
  120. auto h2 = h;
  121. for (auto&& xi : x) h(weight(2), xi);
  122. h2.fill(weight(2), x);
  123. for (unsigned i = 0; i < ndata; ++i) h(weight(w[i]), x[i]);
  124. h2.fill(weight(w), x);
  125. BOOST_TEST_EQ(h, h2);
  126. auto w2 = {1};
  127. boost::ignore_unused(w2);
  128. BOOST_TEST_THROWS(h2.fill(x, weight(w2)), std::invalid_argument);
  129. }
  130. // 2D simple
  131. {
  132. auto h = make(Tag(), in{1, 3}, in0{1, 5});
  133. auto h2 = h;
  134. for (int i = 0; i < ndata; ++i) h(x[i], y[i]);
  135. const auto xy = {x, y};
  136. h2.fill(xy);
  137. BOOST_TEST_EQ(h, h2);
  138. // wrong rank
  139. BOOST_TEST_THROWS(h.fill(x), std::invalid_argument);
  140. // not rectangular
  141. std::array<std::vector<int>, 2> bad = {{std::vector<int>(1), std::vector<int>(2)}};
  142. boost::ignore_unused(bad);
  143. BOOST_TEST_THROWS(h2.fill(bad), std::invalid_argument);
  144. }
  145. // 2D variant and weight
  146. {
  147. auto h = make(Tag(), in{1, 3}, in0{1, 5});
  148. using V = variant<int, std::vector<int>, std::string>;
  149. V xy[2];
  150. {
  151. xy[0] = 3;
  152. xy[1] = y;
  153. auto h1 = h;
  154. auto h2 = h;
  155. for (auto&& vi : y) h1(3, vi);
  156. h2.fill(xy);
  157. BOOST_TEST_EQ(h1, h2);
  158. }
  159. {
  160. xy[0] = x;
  161. xy[1] = 3;
  162. auto h1 = h;
  163. auto h2 = h;
  164. for (auto&& vi : x) h1(vi, 3);
  165. h2.fill(xy);
  166. BOOST_TEST_EQ(h1, h2);
  167. }
  168. {
  169. xy[0] = 3;
  170. xy[1] = y;
  171. auto h1 = h;
  172. auto h2 = h;
  173. for (auto&& vi : y) h1(3, vi, weight(2));
  174. h2.fill(xy, weight(2));
  175. BOOST_TEST_EQ(h1, h2);
  176. }
  177. {
  178. xy[0] = 3;
  179. xy[1] = y;
  180. auto h1 = h;
  181. auto h2 = h;
  182. for (unsigned i = 0; i < ndata; ++i) h1(3, y[i], weight(w[i]));
  183. h2.fill(xy, weight(w));
  184. BOOST_TEST_EQ(sum(h1), sum(h2));
  185. BOOST_TEST_EQ(h1, h2);
  186. }
  187. }
  188. // 1D growing
  189. {
  190. auto h = make(Tag(), ing());
  191. auto h2 = h;
  192. for (const auto& xi : x) h(xi);
  193. h2.fill(x);
  194. BOOST_TEST_EQ(h, h2);
  195. }
  196. // 2D growing A
  197. {
  198. auto h = make(Tag(), in(1, 3), ing());
  199. auto h2 = h;
  200. for (unsigned i = 0; i < ndata; ++i) h(x[i], y[i]);
  201. const auto xy = {x, y};
  202. h2.fill(xy);
  203. BOOST_TEST_EQ(h, h2);
  204. }
  205. // 2D growing B
  206. {
  207. auto h = make(Tag(), ing(), ing());
  208. auto h2 = h;
  209. for (unsigned i = 0; i < ndata; ++i) h(x[i], y[i]);
  210. const auto xy = {x, y};
  211. h2.fill(xy);
  212. BOOST_TEST_EQ(h, h2);
  213. }
  214. // 2D growing with weights A
  215. {
  216. auto h = make(Tag(), in(1, 3), ing());
  217. auto h2 = h;
  218. for (unsigned i = 0; i < ndata; ++i) h(x[i], y[i], weight(w[i]));
  219. const auto xy = {x, y};
  220. h2.fill(xy, weight(w));
  221. BOOST_TEST_EQ(h, h2);
  222. }
  223. // 2D growing with weights B
  224. {
  225. auto h = make(Tag(), ing(), ing());
  226. auto h2 = h;
  227. for (unsigned i = 0; i < ndata; ++i) h(x[i], y[i], weight(2));
  228. const auto xy = {x, y};
  229. h2.fill(xy, weight(2));
  230. BOOST_TEST_EQ(h, h2);
  231. }
  232. // 2D growing and variant
  233. {
  234. auto h = make(Tag(), csg{}, in{1, 2});
  235. auto h2 = h;
  236. h("foo", 1);
  237. h("foo", 2);
  238. using V = variant<std::string, std::vector<std::string>, int, std::vector<int>>;
  239. const auto xy = {V("foo"), V(std::vector<int>{1, 2})};
  240. h2.fill(xy);
  241. BOOST_TEST_EQ(h, h2);
  242. const auto bad = {V(std::vector<std::string>(1, "foo")), V(std::vector<int>{1, 2})};
  243. boost::ignore_unused(bad);
  244. BOOST_TEST_THROWS(h.fill(bad), std::invalid_argument);
  245. }
  246. // 1D profile with samples
  247. {
  248. auto h = make_s(Tag(), profile_storage(), in(1, 3));
  249. auto h2 = h;
  250. for (unsigned i = 0; i < ndata; ++i) h(x[i], sample(w[i]));
  251. for (unsigned i = 0; i < ndata; ++i) h(x[i], sample(w[i]), weight(w[i]));
  252. for (unsigned i = 0; i < ndata; ++i) h(x[i], sample(2), weight(w[i]));
  253. for (unsigned i = 0; i < ndata; ++i) h(x[i], sample(w[i]), weight(2));
  254. h2.fill(x, sample(w));
  255. h2.fill(x, sample(w), weight(w));
  256. h2.fill(x, sample(2), weight(w));
  257. h2.fill(x, sample(w), weight(2));
  258. BOOST_TEST_EQ(h, h2);
  259. }
  260. // 2D weighted profile with samples and weights
  261. {
  262. auto h = make_s(Tag(), weighted_profile_storage(), in(1, 3), in0(1, 3));
  263. auto h2 = h;
  264. for (unsigned i = 0; i < ndata; ++i) h(x[i], 3, sample(w[i]), weight(w[i]));
  265. for (unsigned i = 0; i < ndata; ++i) h(x[i], 3, sample(2), weight(w[i]));
  266. for (unsigned i = 0; i < ndata; ++i) h(x[i], 3, sample(w[i]), weight(2));
  267. using V = variant<int, std::vector<int>>;
  268. std::array<V, 2> xy;
  269. xy[0] = x;
  270. xy[1] = 3;
  271. h2.fill(xy, sample(w), weight(w));
  272. h2.fill(xy, sample(2), weight(w));
  273. h2.fill(xy, sample(w), weight(2));
  274. BOOST_TEST_EQ(h, h2);
  275. }
  276. // axis2d
  277. {
  278. auto h = make(Tag(), axis2d{});
  279. auto h2 = h;
  280. std::vector<std::tuple<double, double>> xy;
  281. xy.reserve(ndata);
  282. for (unsigned i = 0; i < ndata; ++i) xy.emplace_back(x[i], y[i]);
  283. for (auto&& xyi : xy) h(xyi);
  284. h2.fill(xy);
  285. BOOST_TEST_EQ(h, h2);
  286. }
  287. }
  288. int main() {
  289. std::mt19937 gen(1);
  290. std::normal_distribution<> id(0, 2);
  291. std::vector<int> x(ndata), y(ndata);
  292. auto generator = [&] { return static_cast<int>(id(gen)); };
  293. std::generate(x.begin(), x.end(), generator);
  294. std::generate(y.begin(), y.end(), generator);
  295. std::vector<double> w(ndata);
  296. // must be all positive
  297. std::generate(w.begin(), w.end(), [&] { return 0.5 + std::abs(id(gen)); });
  298. run_tests<static_tag>(x, y, w);
  299. run_tests<dynamic_tag>(x, y, w);
  300. return boost::report_errors();
  301. }