-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathtest_xhistogram.cpp
116 lines (96 loc) · 3.95 KB
/
test_xhistogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <complex>
#include <limits>
// For some obscure reason xtensor.hpp need to be included first for Windows' Clangcl
#include "xtensor/containers/xtensor.hpp"
//
#include "xtensor/generators/xrandom.hpp"
#include "xtensor/misc/xhistogram.hpp"
#include "test_common_macros.hpp"
namespace xt
{
TEST(xhistogram, histogram)
{
xt::xtensor<double, 1> data = {1., 1., 2., 2.};
{
xt::xtensor<double, 1> count = xt::histogram(data, std::size_t(2));
EXPECT_EQ(count.size(), std::size_t(2));
EXPECT_EQ(count(0), 2.);
EXPECT_EQ(count(1), 2.);
}
{
xt::xtensor<double, 1> count = xt::histogram(
data,
xt::histogram_bin_edges(data, std::size_t(2), xt::histogram_algorithm::uniform)
);
EXPECT_EQ(count.size(), std::size_t(2));
EXPECT_EQ(count(0), 2.);
EXPECT_EQ(count(1), 2.);
}
{
xt::xtensor<double, 1> count = xt::histogram(data, size_t(2), 0.5, 1.5);
EXPECT_EQ(count.size(), std::size_t(2));
EXPECT_EQ(count(0), 0.);
EXPECT_EQ(count(1), 2.);
}
{
// test specifying return type as well
auto count = xt::histogram<float>(data, size_t(2), xt::xtensor<double, 1>({1., 2., 1., 1.}), 0.5, 1.5);
EXPECT_EQ(count.size(), std::size_t(2));
EXPECT_EQ(count(0), 0.f);
EXPECT_EQ(count(1), 3.f);
bool is_same_type = std::is_same<decltype(count)::value_type, float>::value;
EXPECT_TRUE(is_same_type);
}
{
// test input being a container other than xtensor
xt::xarray<double> arr = {1., 1., 2., 2.};
xt::xtensor<double, 1> count = xt::histogram(arr, std::size_t(2));
EXPECT_EQ(count.size(), std::size_t(2));
EXPECT_EQ(count(0), 2.);
EXPECT_EQ(count(1), 2.);
}
}
TEST(xhistogram, bincount)
{
xtensor<int, 1> data = {1, 2, 3, 1, 1, 1, 1, 2, 3, 2, 3, 3, 3, 3};
xtensor<int, 1> weights = xt::ones<int>(data.shape()) * 3;
xtensor<int, 1> expc = {0, 5, 3, 6};
auto bc = bincount(data);
EXPECT_EQ(bc, expc);
auto bc2 = bincount(data, weights);
EXPECT_EQ(bc2, expc * 3);
auto bc3 = bincount(data, 10);
EXPECT_EQ(bc3.size(), std::size_t(10));
EXPECT_EQ(bc3(3), expc(3));
}
TEST(xhistogram, digitize)
{
xt::xtensor<size_t, 1> bin_edges = {0, 10, 20, 30};
xt::xtensor<size_t, 1> data = {1, 12, 2, 21, 11, 10};
xt::xtensor<size_t, 1> res_left = {1, 2, 1, 3, 2, 2};
xt::xtensor<size_t, 1> res_right = {1, 2, 1, 3, 2, 1};
EXPECT_EQ(xt::digitize(data, bin_edges), res_left);
EXPECT_EQ(xt::digitize(data, bin_edges, false), res_left);
EXPECT_EQ(xt::digitize(data, bin_edges, true), res_right);
}
TEST(xhistogram, bin_items_1)
{
xt::xtensor<size_t, 1> a = xt::bin_items(11, xt::xtensor<double, 1>{0.9, 0.0, 0.0, 0.1});
xt::xtensor<size_t, 1> b = {9, 0, 0, 2};
EXPECT_EQ(a, b);
}
TEST(xhistogram, bin_items_2)
{
xt::xtensor<size_t, 1> a = xt::bin_items(11, xt::xtensor<double, 1>{0.25, 0.25, 0.25, 0.25});
xt::xtensor<size_t, 1> b = {3, 3, 3, 2};
EXPECT_EQ(a, b);
}
}