forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistogramIteratorTest.h
302 lines (249 loc) · 11.6 KB
/
HistogramIteratorTest.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidHistogramData/Histogram.h"
#include "MantidHistogramData/HistogramItem.h"
#include "MantidHistogramData/HistogramIterator.h"
#include "MantidHistogramData/LinearGenerator.h"
using namespace Mantid::HistogramData;
class HistogramIteratorTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static HistogramIteratorTest *createSuite() { return new HistogramIteratorTest(); }
static void destroySuite(HistogramIteratorTest *suite) { delete suite; }
void test_construction() {
Histogram hist(Histogram::XMode::BinEdges, Histogram::YMode::Counts);
TS_ASSERT_THROWS_NOTHING(HistogramIterator iter(hist, 0));
}
void test_iterator_begin() {
Histogram hist(Points{1.1, 1.2, 1.4}, Frequencies{2, 3, 4});
auto iter = hist.begin();
TS_ASSERT(iter != hist.end());
TS_ASSERT_EQUALS(iter->frequency(), 2);
}
void test_iterator_end() {
Histogram hist(Points{1.1, 1.2, 1.4}, Frequencies{2, 3, 4});
auto iter = hist.end();
TS_ASSERT(iter != hist.begin());
}
void test_iterator_increment() {
Histogram hist(Points{1.1, 1.2, 1.4}, Frequencies{2, 3, 4});
auto iter = hist.begin();
TS_ASSERT(iter != hist.end());
TS_ASSERT_EQUALS(iter->frequency(), 2);
++iter;
TS_ASSERT(iter != hist.end());
TS_ASSERT_EQUALS(iter->frequency(), 3);
++iter;
TS_ASSERT(iter != hist.end());
TS_ASSERT_EQUALS(iter->frequency(), 4);
++iter;
TS_ASSERT(iter == hist.end());
}
void test_iterator_decrement() {
Histogram hist(Points{1.1, 1.2, 1.4}, Frequencies{2, 3, 4});
auto iter = hist.end();
--iter;
TS_ASSERT_DIFFERS(iter, hist.begin());
TS_ASSERT_EQUALS(iter->frequency(), 4);
--iter;
TS_ASSERT_DIFFERS(iter, hist.begin());
TS_ASSERT_EQUALS(iter->frequency(), 3);
--iter;
TS_ASSERT_EQUALS(iter, hist.begin());
TS_ASSERT_EQUALS(iter->frequency(), 2);
}
void test_iterator_advance() {
Histogram hist(Points{1.1, 1.2, 1.4}, Frequencies{2, 3, 4});
auto iter = hist.begin();
std::advance(iter, 2);
TS_ASSERT_EQUALS(iter->frequency(), 4);
// check past end of valid range
std::advance(iter, 2);
TS_ASSERT_EQUALS(iter, hist.end());
std::advance(iter, -3);
TS_ASSERT_EQUALS(iter->frequency(), 2);
// check before start of valid range
std::advance(iter, -2);
TS_ASSERT_EQUALS(iter, hist.begin());
}
void test_iterator_distance() {
Histogram hist(Points{1.1, 1.2, 1.4}, Frequencies{2, 3, 4});
auto begin = hist.begin();
auto end = hist.end();
TS_ASSERT_DIFFERS(begin, end);
TS_ASSERT_EQUALS(std::distance(begin, end), 3);
++begin;
TS_ASSERT_DIFFERS(begin, end);
TS_ASSERT_EQUALS(std::distance(begin, end), 2);
--begin;
std::advance(begin, std::distance(begin, end) / 2);
TS_ASSERT_DIFFERS(begin, end);
TS_ASSERT_EQUALS(std::distance(begin, end), 2);
}
void test_iterate_over_histogram_x_errors() {
Histogram hist(Points{1.1, 1.2, 1.3}, Counts{5, 4, 6});
hist.setPointStandardDeviations(PointStandardDeviations{0.1, 0.3, 0.5});
const auto dX = hist.dataDx();
TS_ASSERT(std::equal(hist.begin(), hist.end(), dX.begin(),
[](const HistogramItem &item, const double &dx) { return item.centerError() == dx; }));
}
void test_iterate_over_histogram_counts() {
Counts expectedCounts{2, 3, 4};
Histogram hist(Points{1.1, 1.2, 1.4}, expectedCounts);
TS_ASSERT(std::equal(hist.begin(), hist.end(), expectedCounts.begin(),
[](const HistogramItem &item, const double &counts) { return item.counts() == counts; }));
}
void test_iterate_over_histogram_counts_when_histogram_has_frequencies() {
Histogram hist(BinEdges{1.0, 1.1, 1.2, 1.5}, Frequencies{2, 3, 4});
Counts expectedCounts = hist.counts();
TS_ASSERT(std::equal(hist.begin(), hist.end(), expectedCounts.begin(),
[](const HistogramItem &item, const double &counts) { return item.counts() == counts; }));
}
void test_iterate_over_histogram_frequencies() {
Frequencies expectedFrequencies{2, 3, 4};
Histogram hist(Points{1.1, 1.2, 1.4}, expectedFrequencies);
TS_ASSERT(
std::equal(hist.begin(), hist.end(), expectedFrequencies.begin(),
[](const HistogramItem &item, const double &frequency) { return item.frequency() == frequency; }));
}
void test_iterate_over_histogram_frequencies_when_histogram_has_counts() {
Histogram hist(BinEdges{1.1, 1.2, 1.3, 1.5}, Counts{2, 3, 4});
Frequencies expectedFrequencies = hist.frequencies();
TS_ASSERT(
std::equal(hist.begin(), hist.end(), expectedFrequencies.begin(),
[](const HistogramItem &item, const double &frequency) { return item.frequency() == frequency; }));
}
void test_iterate_over_histogram_center_when_histogram_has_bins() {
Histogram hist(BinEdges{1.1, 1.2, 1.3, 1.4}, Counts{2, 3, 4});
Points expectedPoints = hist.points();
TS_ASSERT(std::equal(hist.begin(), hist.end(), expectedPoints.begin(),
[](const HistogramItem &item, const double &point) { return item.center() == point; }));
}
void test_iterate_over_histogram_center_when_histogram_has_points() {
Histogram hist(Points{1.1, 1.2, 1.4}, Counts{2, 3, 4});
Points expectedPoints = hist.points();
TS_ASSERT(std::equal(hist.begin(), hist.end(), expectedPoints.begin(),
[](const HistogramItem &item, const double &point) { return item.center() == point; }));
}
void test_iterate_over_histogram_width_when_histogram_has_bins() {
Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4});
std::vector<double> expectedWidths = {1, 1, 2};
TS_ASSERT(std::equal(hist.begin(), hist.end(), expectedWidths.begin(),
[](const HistogramItem &item, const double &width) { return item.binWidth() == width; }));
}
void test_iterate_over_histogram_width_when_histogram_has_points() {
Histogram hist(Points{1, 3, 5}, Counts{2, 3, 4});
std::vector<double> expectedWidths = {2, 2, 2};
TS_ASSERT(std::equal(hist.begin(), hist.end(), expectedWidths.begin(),
[](const HistogramItem &item, const double &width) { return item.binWidth() == width; }));
}
void test_iterate_over_histogram_count_variances_when_histogram_has_counts() {
Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4});
auto expectedCountVariances = hist.countVariances();
TS_ASSERT(
std::equal(hist.begin(), hist.end(), expectedCountVariances.begin(),
[](const HistogramItem &item, const double &variance) { return item.countVariance() == variance; }));
}
void test_iterate_over_histogram_count_variances_when_histogram_has_frequencies() {
Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4});
auto expectedCountVariances = hist.countVariances();
TS_ASSERT(
std::equal(hist.begin(), hist.end(), expectedCountVariances.begin(),
[](const HistogramItem &item, const double &variance) { return item.countVariance() == variance; }));
}
void test_iterate_over_histogram_count_std_when_histogram_has_counts() {
Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4});
auto expectedCountStd = hist.countStandardDeviations();
TS_ASSERT(std::equal(
hist.begin(), hist.end(), expectedCountStd.begin(),
[](const HistogramItem &item, const double &sigma) { return item.countStandardDeviation() == sigma; }));
}
void test_iterate_over_histogram_count_std_when_histogram_has_frequencies() {
Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4});
auto expectedCountStd = hist.countStandardDeviations();
TS_ASSERT(std::equal(
hist.begin(), hist.end(), expectedCountStd.begin(),
[](const HistogramItem &item, const double &sigma) { return item.countStandardDeviation() == sigma; }));
}
void test_iterate_over_histogram_frequency_variances_when_histogram_has_frequencys() {
Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4});
auto expectedFrequencyVariances = hist.frequencyVariances();
TS_ASSERT(std::equal(
hist.begin(), hist.end(), expectedFrequencyVariances.begin(),
[](const HistogramItem &item, const double &variance) { return item.frequencyVariance() == variance; }));
}
void test_iterate_over_histogram_frequency_variances_when_histogram_has_frequencies() {
Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4});
auto expectedFrequencyVariances = hist.frequencyVariances();
TS_ASSERT(std::equal(
hist.begin(), hist.end(), expectedFrequencyVariances.begin(),
[](const HistogramItem &item, const double &variance) { return item.frequencyVariance() == variance; }));
}
void test_iterate_over_histogram_frequency_std_when_histogram_has_frequencys() {
Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4});
auto expectedFrequencyStd = hist.frequencyStandardDeviations();
TS_ASSERT(std::equal(
hist.begin(), hist.end(), expectedFrequencyStd.begin(),
[](const HistogramItem &item, const double &sigma) { return item.frequencyStandardDeviation() == sigma; }));
}
void test_iterate_over_histogram_frequency_std_when_histogram_has_frequencies() {
Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4});
auto expectedFrequencyStd = hist.frequencyStandardDeviations();
TS_ASSERT(std::equal(
hist.begin(), hist.end(), expectedFrequencyStd.begin(),
[](const HistogramItem &item, const double &sigma) { return item.frequencyStandardDeviation() == sigma; }));
}
};
class HistogramIteratorTestPerformance : public CxxTest::TestSuite {
public:
static HistogramIteratorTestPerformance *createSuite() { return new HistogramIteratorTestPerformance; }
static void destroySuite(HistogramIteratorTestPerformance *suite) { delete suite; }
HistogramIteratorTestPerformance()
: m_hist(BinEdges(histSize, LinearGenerator(0, 1)), Counts(histSize - 1, LinearGenerator(0, 1))) {}
void test_convert_counts_to_frequency_for_each_item() {
double total = 0;
for (size_t i = 0; i < nHists; i++)
for (auto &item : m_hist)
total += item.frequency();
}
void test_convert_counts_to_frequency_once_per_histogram() {
double total = 0;
for (size_t i = 0; i < nHists; i++) {
const auto &frequencies = m_hist.frequencies();
for (auto &frequency : frequencies)
total += frequency;
}
}
void test_convert_counts_to_frequency_for_each_item_sparse() {
double total = 0;
double floor = static_cast<double>(histSize) - 5.0;
for (size_t i = 0; i < nHists; i++) {
for (auto &item : m_hist) {
if (item.counts() > floor)
total += item.frequency();
}
}
}
void test_convert_counts_to_frequency_once_per_histogram_sparse() {
double total = 0;
double floor = static_cast<double>(histSize) - 5.0;
for (size_t i = 0; i < nHists; i++) {
const auto &counts = m_hist.counts();
const auto &frequencies = m_hist.frequencies();
for (size_t j = 0; j < histSize; ++j)
if (counts[j] > floor)
total += frequencies[j];
}
}
private:
const size_t nHists = 1000;
const size_t histSize = 1000000;
Histogram m_hist;
};