diff --git a/Framework/HistogramData/CMakeLists.txt b/Framework/HistogramData/CMakeLists.txt index ef6ede0afa8f88fe8df6dbb36b23e2e25ea191c4..0f7b182951b67af901f467236f6a92d6bb761398 100644 --- a/Framework/HistogramData/CMakeLists.txt +++ b/Framework/HistogramData/CMakeLists.txt @@ -9,7 +9,6 @@ set ( SRC_FILES src/FrequencyVariances.cpp src/Histogram.cpp src/HistogramBuilder.cpp - src/HistogramIterator.cpp src/HistogramItem.cpp src/HistogramMath.cpp src/Interpolate.cpp diff --git a/Framework/HistogramData/inc/MantidHistogramData/HistogramItem.h b/Framework/HistogramData/inc/MantidHistogramData/HistogramItem.h index 1d60d97d625c8362e82d7fc63c576ce67c16dfa6..c34fd2cdf19e0a2423411881b19daccfd9d2c496 100644 --- a/Framework/HistogramData/inc/MantidHistogramData/HistogramItem.h +++ b/Framework/HistogramData/inc/MantidHistogramData/HistogramItem.h @@ -4,6 +4,7 @@ #include "MantidHistogramData/BinEdges.h" #include "MantidHistogramData/DllConfig.h" #include "MantidHistogramData/Points.h" + #include <utility> namespace Mantid { @@ -52,27 +53,48 @@ class Histogram; class MANTID_HISTOGRAMDATA_DLL HistogramItem { public: - HistogramItem(const Histogram &histogram, const size_t index); - - double counts() const; - double countVariance() const; - double countStandardDeviation() const; - double frequency() const; - double frequencyVariance() const; - double frequencyStandardDeviation() const; - double center() const; - double width() const; - const BinEdges binEdges() const; - const Points point() const; + double counts() const; + double countVariance() const; + double countStandardDeviation() const; + double frequency() const; + double frequencyVariance() const; + double frequencyStandardDeviation() const; + double width() const; + double center() const; + + void incrementIndex(); + void advance(int64_t delta); + + inline void decrementIndex() { + if (m_index > 0) { + --m_index; + } + } + + inline size_t getIndex() const { + return m_index; + } + + inline void setIndex(const size_t index) { + m_index = index; + } private: + friend class HistogramIterator; + /// Private constructor, can only be created by HistogramIterator + HistogramItem(const Histogram &histogram, const size_t index); + /// Get a refernce to the histogram const Histogram &histogramRef() const; - // Deleted copy & assignment operators as a HistogramItem is not copyable - HistogramItem(const HistogramItem &) = delete; + /// Check if is points or bins + bool xModeIsPoints() const; + /// Check if is counts or frequencies + bool yModeIsCounts() const; + + // Deleted assignment operator as a HistogramItem is not copyable HistogramItem operator=(const HistogramItem &) = delete; const Histogram &m_histogram; - const size_t m_index; + size_t m_index; }; } // namespace HistogramData diff --git a/Framework/HistogramData/inc/MantidHistogramData/HistogramIterator.h b/Framework/HistogramData/inc/MantidHistogramData/HistogramIterator.h index dd72f16bc5cf11b6910fe741b9cefd4fc7faa2f3..a3f1718ab335b25a5be70aa1d24ea424d7b0befd 100644 --- a/Framework/HistogramData/inc/MantidHistogramData/HistogramIterator.h +++ b/Framework/HistogramData/inc/MantidHistogramData/HistogramIterator.h @@ -4,7 +4,6 @@ #include "MantidHistogramData/DllConfig.h" #include "MantidHistogramData/HistogramItem.h" -#include "MantidKernel/make_unique.h" #include <boost/iterator/iterator_facade.hpp> #include <memory> @@ -12,6 +11,8 @@ namespace Mantid { namespace HistogramData { +class Histogram; + /** HistogramIterator HistogramIterator implements an the iterator interface for HistogramData. @@ -43,27 +44,34 @@ namespace HistogramData { Code Documentation is available at: <http://doxygen.mantidproject.org> */ class MANTID_HISTOGRAMDATA_DLL HistogramIterator - : public boost::iterator_facade<HistogramIterator, HistogramItem, + : public boost::iterator_facade<HistogramIterator, const HistogramItem&, boost::bidirectional_traversal_tag> { public: HistogramIterator(const Histogram &histogram, const size_t index = 0) - : m_histogram(histogram), m_index(index), - m_currentItem(Kernel::make_unique<HistogramItem>(histogram, index)){}; + : m_item(histogram, index) {}; private: friend class boost::iterator_core_access; - void increment(); - bool equal(const HistogramIterator &other) const; - HistogramItem &dereference() const; - void decrement(); - void advance(int64_t delta); - uint64_t distance_to(const HistogramIterator &other) const; + inline void increment() { m_item.incrementIndex(); } + + inline bool equal(const HistogramIterator &other) const { + return m_item.getIndex() == other.m_item.getIndex(); + } + + inline const HistogramItem &dereference() const { return m_item; } + + inline void decrement() { m_item.incrementIndex(); } + + inline void advance(int64_t delta) { m_item.advance(delta); } + + inline uint64_t distance_to(const HistogramIterator &other) const { + return static_cast<uint64_t>(other.m_item.getIndex()) - + static_cast<uint64_t>(m_item.getIndex()); + } - const Histogram &m_histogram; - size_t m_index; - std::unique_ptr<HistogramItem> m_currentItem; + HistogramItem m_item; }; } // namespace HistogramData diff --git a/Framework/HistogramData/src/HistogramItem.cpp b/Framework/HistogramData/src/HistogramItem.cpp index af61b8d01567ccf0ad118a44a2b7178076e74fba..fd4e8a2d5cc3bce013a51f6dfd590ec06082fe10 100644 --- a/Framework/HistogramData/src/HistogramItem.cpp +++ b/Framework/HistogramData/src/HistogramItem.cpp @@ -11,96 +11,118 @@ using Mantid::HistogramData::Points; HistogramItem::HistogramItem(const Histogram &histogram, const size_t index) : m_histogram(histogram), m_index(index) {} +double HistogramItem::center() const { + const auto &x = histogramRef().x(); + if (xModeIsPoints()) { + return x[m_index]; + } else { + return (x[m_index+1] + x[m_index]) / 2.0; + } +} + +double HistogramItem::width() const { + const auto &x = histogramRef().x(); + if (xModeIsPoints()) { + auto numPoints = histogramRef().size(); + double lower = 0; + double upper = 0; + if(m_index == 0) { + // first point + upper = 0.5 * (x[m_index + 1] + x[m_index]); + lower = x[0] - (upper - x[0]); + } else if (m_index == numPoints - 1) { + // last point + lower = 0.5 * (x[m_index] + x[m_index - 1]); + upper = x[numPoints - 1] + (x[numPoints - 1] - lower); + } else { + // everything inbetween + lower = 0.5 * (x[m_index] + x[m_index - 1]); + upper = 0.5 * (x[m_index + 1] + x[m_index]); + } + return upper - lower; + } else { + return x[m_index + 1] - x[m_index]; + } +} + +bool HistogramItem::xModeIsPoints() const { + return Histogram::XMode::Points == histogramRef().xMode(); +} + +bool HistogramItem::yModeIsCounts() const { + return Histogram::YMode::Counts == histogramRef().yMode(); +} + double HistogramItem::counts() const { - const auto &yMode = histogramRef().yMode(); - if (yMode == Histogram::YMode::Counts) { - return histogramRef().counts()[m_index]; - } else { - const Frequencies frequency{histogramRef().frequencies()[m_index]}; - return Counts(frequency, binEdges())[0]; - } + const auto &y = histogramRef().y(); + if (yModeIsCounts()) { + return y[m_index]; + } else { + return y[m_index] / width(); + } } double HistogramItem::countVariance() const { - return histogramRef().countVariances()[m_index]; + const auto &e = histogramRef().e(); + if (yModeIsCounts()) { + return e[m_index] * e[m_index]; + } else { + const auto binWidth = width(); + return e[m_index] * e[m_index] * binWidth * binWidth; + } } double HistogramItem::countStandardDeviation() const { - return histogramRef().countStandardDeviations()[m_index]; + const auto &e = histogramRef().e(); + if (yModeIsCounts()) { + return e[m_index]; + } else { + const auto binWidth = width(); + return e[m_index] * binWidth; + } } double HistogramItem::frequency() const { - const auto &yMode = histogramRef().yMode(); - if (yMode == Histogram::YMode::Frequencies) { - return histogramRef().frequencies()[m_index]; - } else { - const Counts counts{histogramRef().counts()[m_index]}; - return Frequencies(counts, binEdges())[0]; - } + const auto &y = histogramRef().y(); + if (yModeIsCounts()) { + return y[m_index] * width(); + } else { + return y[m_index]; + } } double HistogramItem::frequencyVariance() const { - return histogramRef().frequencyVariances()[m_index]; + const auto &e = histogramRef().e(); + if (!yModeIsCounts()) { + return e[m_index] * e[m_index]; + } else { + const auto binWidth = width(); + return (e[m_index] * e[m_index]) / (binWidth * binWidth); + } } double HistogramItem::frequencyStandardDeviation() const { - return histogramRef().frequencyStandardDeviations()[m_index]; + const auto &e = histogramRef().e(); + if (!yModeIsCounts()) { + return e[m_index]; + } else { + const auto binWidth = width(); + return e[m_index] / binWidth; + } } -double HistogramItem::center() const { return point()[0]; } - -double HistogramItem::width() const { - const auto &edges = binEdges(); - const auto &lower = edges[0]; - const auto &upper = edges[1]; - return upper - lower; -} +const Histogram &HistogramItem::histogramRef() const { return m_histogram; } -const BinEdges HistogramItem::binEdges() const { - const auto &xMode = histogramRef().xMode(); - - if (xMode == Histogram::XMode::BinEdges) { - const auto &edges = histogramRef().binEdges(); - const auto &lower = edges[m_index]; - const auto &upper = edges[m_index + 1]; - return BinEdges{lower, upper}; - } else { - const auto &points = histogramRef().points(); - // Guess the bin edges from the points - if (points.size() == 0) { - // We have no points, just return empty bin edges - return BinEdges{}; - } else if (points.size() == 1) { - // We have a single point, let BinEdges guess the edges - return BinEdges(points); - } else { - // We have 2 or more points. Attempt to use up to 4 points - // to interpolate the correct bin edges. - size_t upperBound = std::min(m_index + 2, points.size()); - size_t lowerBound = (m_index < 2) ? 0 : m_index - 2; - - std::vector<double> pts; - for (size_t i = lowerBound; i < upperBound; i++) { - pts.push_back(points[i]); - } - - auto index = m_index - lowerBound; - BinEdges edges(Points{pts}); - return BinEdges{edges[index], edges[index + 1]}; - } - } +void HistogramItem::advance(int64_t delta) { + m_index = delta < 0 ? std::max(static_cast<uint64_t>(0), + static_cast<uint64_t>(m_index) + delta) + : std::min(histogramRef().size(), + m_index + static_cast<size_t>(delta)); } -const Points HistogramItem::point() const { - const auto &xMode = histogramRef().xMode(); - if (xMode == Histogram::XMode::Points) { - return Points{histogramRef().points()[m_index]}; - } else { - const auto &lower = histogramRef().binEdges()[m_index]; - const auto &upper = histogramRef().binEdges()[m_index + 1]; - const BinEdges bins{lower, upper}; - return Points(bins); +void HistogramItem::incrementIndex() { + if (m_index < histogramRef().size()) { + ++m_index; } } -const Histogram &HistogramItem::histogramRef() const { return m_histogram; } diff --git a/Framework/HistogramData/src/HistogramIterator.cpp b/Framework/HistogramData/src/HistogramIterator.cpp deleted file mode 100644 index edfd268de33e1430efa74a7ccaf4105e8ba0769e..0000000000000000000000000000000000000000 --- a/Framework/HistogramData/src/HistogramIterator.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "MantidHistogramData/HistogramIterator.h" -#include "MantidHistogramData/Histogram.h" -#include "MantidHistogramData/HistogramItem.h" -#include "MantidKernel/make_unique.h" - -using Mantid::HistogramData::Histogram; -using Mantid::HistogramData::HistogramItem; -using Mantid::HistogramData::HistogramIterator; -using Mantid::Kernel::make_unique; - -void HistogramIterator::increment() { - if (m_index < m_histogram.size()) { - ++m_index; - m_currentItem = make_unique<HistogramItem>(m_histogram, m_index); - } -} - -bool HistogramIterator::equal(const HistogramIterator &other) const { - return (&m_histogram == &other.m_histogram) && (m_index == other.m_index); -} - -HistogramItem &HistogramIterator::dereference() const { return *m_currentItem; } - -void HistogramIterator::decrement() { - if (m_index > 0) { - --m_index; - m_currentItem = make_unique<HistogramItem>(m_histogram, m_index); - } -} - -void HistogramIterator::advance(int64_t delta) { - m_index = delta < 0 ? std::max(static_cast<uint64_t>(0), - static_cast<uint64_t>(m_index) + delta) - : std::min(m_histogram.size(), - m_index + static_cast<size_t>(delta)); - m_currentItem = make_unique<HistogramItem>(m_histogram, m_index); -} - -uint64_t HistogramIterator::distance_to(const HistogramIterator &other) const { - return static_cast<uint64_t>(other.m_index) - static_cast<uint64_t>(m_index); -} diff --git a/Framework/HistogramData/test/HistogramItemTest.h b/Framework/HistogramData/test/HistogramItemTest.h index f02c4f8485e28e73b0aa3bba7a16f057735ebd65..eebb2028cf3ae2cc62b54329b53a02921ad94eb5 100644 --- a/Framework/HistogramData/test/HistogramItemTest.h +++ b/Framework/HistogramData/test/HistogramItemTest.h @@ -15,6 +15,7 @@ using Mantid::HistogramData::BinEdges; class HistogramItemTest : public CxxTest::TestSuite { public: + friend class HistogramItem; // This pair of boilerplate methods prevent the suite being created statically // This means the constructor isn't called when running other tests static HistogramItemTest *createSuite() { return new HistogramItemTest(); } @@ -23,138 +24,8 @@ public: static constexpr double tolerance = 1e-6; void test_construction() { - Histogram hist(Histogram::XMode::BinEdges, Histogram::YMode::Counts); - TS_ASSERT_THROWS_NOTHING(HistogramItem item(hist, 0)); } - void test_get_counts_from_histogram_with_counts() { - Histogram hist(Points{0.1, 0.2, 0.4}, Counts{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_EQUALS(item.counts(), 2.0) - } - - void test_get_counts_from_histogram_with_frequencies() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.counts(), 0.3, tolerance) - } - - void test_get_countVariance_from_histogram_with_counts() { - Histogram hist(Points{0.1, 0.2, 0.4}, Counts{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.countVariance(), 2.0, tolerance) - } - - void test_get_countVariance_from_histogram_with_frequencies() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.countVariance(), 0.045, 1e-12) - } - - void test_get_countStandardDeviation_from_histogram_with_counts() { - Histogram hist(Points{0.1, 0.2, 0.4}, Counts{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.countStandardDeviation(), std::sqrt(2.0), tolerance) - } - - void test_get_countStandardDeviation_from_histogram_with_frequencies() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.countStandardDeviation(), std::sqrt(0.045), 1e-12) - } - - void test_get_frequency_from_histogram_with_counts() { - Histogram hist(Points{0.1, 0.2, 0.4}, Counts{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.frequency(), 13.3333333517, tolerance) - } - - void test_get_frequency_from_histogram_with_frequencies() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.frequency(), 2.0, 1e-12) - } - - void test_get_frequencyVariance_from_histogram_with_counts() { - Histogram hist(Points{0.1, 0.2, 0.4}, Counts{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.frequencyVariance(), 88.8888888, tolerance) - } - - void test_get_frequencyVariance_from_histogram_with_frequencies() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.frequencyVariance(), 2.0, 1e-12) - } - - void test_get_frequencyStandardDeviation_from_histogram_with_counts() { - Histogram hist(Points{0.1, 0.2, 0.4}, Counts{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.frequencyStandardDeviation(), std::sqrt(88.8888888), - tolerance) - } - - void test_get_frequencyStandardDeviation_from_histogram_with_frequencies() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.frequencyStandardDeviation(), std::sqrt(2.0), 1e-12) - } - - void test_get_center_from_histogram_with_bins() { - Histogram hist(BinEdges{0.1, 0.2, 0.4, 0.5}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.center(), 0.3, tolerance) - } - - void test_get_center_from_histogram_with_points() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_EQUALS(item.center(), 0.2) - } - - void test_get_width_from_histogram_with_bins() { - Histogram hist(BinEdges{0.1, 0.2, 0.4, 0.5}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_EQUALS(item.width(), 0.2) - } - - void test_get_width_from_histogram_with_points() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - TS_ASSERT_DELTA(item.width(), 0.15, tolerance) - } - - void test_get_binEdges_from_histogram_with_bins() { - Histogram hist(BinEdges{0.1, 0.2, 0.4, 0.5}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - compare(item.binEdges(), BinEdges{0.2, 0.4}); - } - - void test_get_binEdges_from_histogram_with_points() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - compare(item.binEdges(), BinEdges{0.15, 0.3}); - } - - void test_get_point_from_histogram_with_bins() { - Histogram hist(BinEdges{0.1, 0.2, 0.4, 0.5}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - compare(item.point(), Points{0.3}); - } - - void test_get_point_from_histogram_with_points() { - Histogram hist(Points{0.1, 0.2, 0.4}, Frequencies{1, 2, 4}); - HistogramItem item(hist, 1); - compare(item.point(), Points{0.2}); - } - - template <typename T> void compare(const T &lhs, const T &rhs) { - TS_ASSERT_EQUALS(lhs.size(), rhs.size()) - - for (size_t i = 0; i < lhs.size(); ++i) { - TS_ASSERT_DELTA(lhs[i], rhs[i], tolerance) - } - } }; #endif /* MANTID_HISTOGRAMDATA_HISTOGRAMITEMTEST_H_ */ diff --git a/Framework/HistogramData/test/HistogramIteratorTest.h b/Framework/HistogramData/test/HistogramIteratorTest.h index 4099088d7fdb7f56a03cf4a8f8a5a6b10e00121f..4b637af50da935161d0392d69dc100d1e90ba35f 100644 --- a/Framework/HistogramData/test/HistogramIteratorTest.h +++ b/Framework/HistogramData/test/HistogramIteratorTest.h @@ -7,11 +7,18 @@ #include "MantidHistogramData/HistogramItem.h" #include "MantidHistogramData/HistogramIterator.h" +#include <iostream> using Mantid::HistogramData::Histogram; using Mantid::HistogramData::HistogramItem; using Mantid::HistogramData::HistogramIterator; using Mantid::HistogramData::Points; using Mantid::HistogramData::Counts; +using Mantid::HistogramData::Frequencies; +using Mantid::HistogramData::BinEdges; +using Mantid::HistogramData::CountVariances; +using Mantid::HistogramData::CountStandardDeviations; +using Mantid::HistogramData::FrequencyVariances; +using Mantid::HistogramData::FrequencyStandardDeviations; class HistogramIteratorTest : public CxxTest::TestSuite { public: @@ -26,6 +33,274 @@ public: Histogram hist(Histogram::XMode::BinEdges, Histogram::YMode::Counts); TS_ASSERT_THROWS_NOTHING(HistogramIterator iter(hist)); } + + void test_iterate_over_empty_histogram() { + Histogram hist(Histogram::XMode::BinEdges, Histogram::YMode::Counts); + /* assert(hist.size() == 0); */ + + double total = 0; + /* for (const auto &item : hist) { */ + /* total += item.counts(); */ + /* } */ + + TS_ASSERT_EQUALS(total, 0); + } + + void test_iterate_over_histogram_counts() { + // Arrange + Counts expectedCounts{2, 3, 4}; + Histogram hist(Points{1, 2, 3}, expectedCounts); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedCounts.begin(), + [](const HistogramItem &item, const double &counts) { + return item.counts() == counts; + }); + + // Assert + TSM_ASSERT("Counts did not match", result); + } + + void test_iterate_over_histogram_counts_when_histogram_has_frequencies() { + // Arrange + Histogram hist(Points{1, 2, 3}, Frequencies{2, 3, 4}); + Counts expectedCounts = hist.counts(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedCounts.begin(), + [](const HistogramItem &item, const double &counts) { + return item.counts() == counts; + }); + + // Assert + TSM_ASSERT("Counts did not match", result); + } + + void test_iterate_over_histogram_frequencies() { + // Arrange + Frequencies expectedFrequencies{2, 3, 4}; + Histogram hist(Points{1, 2, 3}, expectedFrequencies); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedFrequencies.begin(), + [](const HistogramItem &item, const double &frequency) { + return item.frequency() == frequency; + }); + + // Assert + TSM_ASSERT("Frequencies did not match", result); + } + + void test_iterate_over_histogram_frequencies_when_histogram_has_counts() { + // Arrange + Histogram hist(Points{1, 2, 3}, Counts{2, 3, 4}); + Frequencies expectedFrequencies = hist.frequencies(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedFrequencies.begin(), + [](const HistogramItem &item, const double &frequency) { + return item.frequency() == frequency; + }); + + // Assert + TSM_ASSERT("Frequencies did not match", result); + } + + void test_iterate_over_histogram_center_when_histogram_has_bins() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 4}, Counts{2, 3, 4}); + Points expectedPoints = hist.points(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedPoints.begin(), + [](const HistogramItem &item, const double &point) { + return item.center() == point; + }); + + // Assert + TSM_ASSERT("Bin centers did not match", result); + } + + void test_iterate_over_histogram_center_when_histogram_has_points() { + // Arrange + Histogram hist(Points{1, 2, 3}, Counts{2, 3, 4}); + Points expectedPoints = hist.points(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedPoints.begin(), + [](const HistogramItem &item, const double &point) { + return item.center() == point; + }); + + // Assert + TSM_ASSERT("Bin centers did not match", result); + } + + void test_iterate_over_histogram_width_when_histogram_has_bins() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4}); + std::vector<double> expectedWidths = {1, 1, 2}; + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedWidths.begin(), + [](const HistogramItem &item, const double &width) { + return item.width() == width; + }); + + // Assert + TSM_ASSERT("Bin widths did not match", result); + } + + void test_iterate_over_histogram_width_when_histogram_has_points() { + // Arrange + Histogram hist(Points{1, 3, 5}, Counts{2, 3, 4}); + std::vector<double> expectedWidths = {2, 2, 2}; + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedWidths.begin(), + [](const HistogramItem &item, const double &width) { + return item.width() == width; + }); + + // Assert + TSM_ASSERT("Bin widths did not match", result); + } + + void test_iterate_over_histogram_count_variances_when_histogram_has_counts() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4}, CountVariances{3, 2, 1}); + auto expectedCountVariances = hist.countVariances(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedCountVariances.begin(), + [](const HistogramItem &item, const double &variance) { + return item.countVariance() == variance; + }); + + // Assert + TSM_ASSERT("Count variances did not match", result); + } + + void test_iterate_over_histogram_count_variances_when_histogram_has_frequencies() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4}, FrequencyVariances{3, 2, 1}); + auto expectedCountVariances = hist.countVariances(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedCountVariances.begin(), + [](const HistogramItem &item, const double &variance) { + return item.countVariance() == variance; + }); + + // Assert + TSM_ASSERT("Count variances did not match", result); + } + + void test_iterate_over_histogram_count_std_when_histogram_has_counts() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4}, CountVariances{3, 2, 1}); + auto expectedCountStd = hist.countStandardDeviations(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedCountStd.begin(), + [](const HistogramItem &item, const double &sigma) { + return item.countStandardDeviation() == sigma; + }); + + // Assert + TSM_ASSERT("Count standard deviations did not match", result); + } + + void test_iterate_over_histogram_count_std_when_histogram_has_frequencies() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4}, FrequencyVariances{3, 2, 1}); + auto expectedCountStd = hist.countStandardDeviations(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedCountStd.begin(), + [](const HistogramItem &item, const double &sigma) { + return item.countStandardDeviation() == sigma; + }); + + // Assert + TSM_ASSERT("Count standard deviations did not match", result); + } + + void test_iterate_over_histogram_frequency_variances_when_histogram_has_frequencys() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4}, CountVariances{3, 2, 1}); + auto expectedFrequencyVariances = hist.frequencyVariances(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedFrequencyVariances.begin(), + [](const HistogramItem &item, const double &variance) { + return item.frequencyVariance() == variance; + }); + + // Assert + TSM_ASSERT("Frequency variances did not match", result); + } + + void test_iterate_over_histogram_frequency_variances_when_histogram_has_frequencies() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4}, FrequencyVariances{3, 2, 1}); + auto expectedFrequencyVariances = hist.frequencyVariances(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedFrequencyVariances.begin(), + [](const HistogramItem &item, const double &variance) { + return item.frequencyVariance() == variance; + }); + + // Assert + TSM_ASSERT("Frequency variances did not match", result); + } + + void test_iterate_over_histogram_frequency_std_when_histogram_has_frequencys() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Counts{2, 3, 4}, CountVariances{3, 2, 1}); + auto expectedFrequencyStd = hist.frequencyStandardDeviations(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedFrequencyStd.begin(), + [](const HistogramItem &item, const double &sigma) { + return item.frequencyStandardDeviation() == sigma; + }); + + // Assert + TSM_ASSERT("Frequency standard deviations did not match", result); + } + + void test_iterate_over_histogram_frequency_std_when_histogram_has_frequencies() { + // Arrange + Histogram hist(BinEdges{1, 2, 3, 5}, Frequencies{2, 3, 4}, FrequencyVariances{3, 2, 1}); + auto expectedFrequencyStd = hist.frequencyStandardDeviations(); + + // Act + auto result = + std::equal(hist.begin(), hist.end(), expectedFrequencyStd.begin(), + [](const HistogramItem &item, const double &sigma) { + return item.frequencyStandardDeviation() == sigma; + }); + + // Assert + TSM_ASSERT("Frequency standard deviations did not match", result); + } }; #endif /* MANTID_HISTOGRAMDATA_HISTOGRAMITERATORTEST_H_ */ diff --git a/Framework/HistogramData/test/HistogramTest.h b/Framework/HistogramData/test/HistogramTest.h index cb45138d9e6861d65164d3f06ded40f0c9857d18..9931d0dd51561658c73782cbb0542faa0d1d45a9 100644 --- a/Framework/HistogramData/test/HistogramTest.h +++ b/Framework/HistogramData/test/HistogramTest.h @@ -1109,7 +1109,7 @@ public: TS_ASSERT(!h.sharedDx()); } - void test_that_can_iterate_hsitogram() { + void test_that_can_iterate_histogram() { Histogram hist(Points{0.1, 0.2, 0.4}, Counts{1, 2, 4}); double total = 0; for (const auto &item : hist) { @@ -1153,7 +1153,7 @@ public: void test_iterate() { double total = 0; for (size_t i = 0; i < nHists / 10; ++i) { - for (auto &item : hists[i]) { + for (const auto &item : hists[i]) { total += item.counts(); } }