Mantid
Loading...
Searching...
No Matches
RebinParamsValidator.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10#include <cmath>
11
12namespace Mantid::Kernel {
13RebinParamsValidator::RebinParamsValidator(bool allowEmpty, bool allowRange)
14 : m_allowEmpty(allowEmpty), m_allowRange(allowRange) {}
15
16IValidator_sptr RebinParamsValidator::clone() const { return std::make_shared<RebinParamsValidator>(*this); }
17
22std::string RebinParamsValidator::checkValidity(const std::vector<double> &value) const {
23 // Rebin Parameters allow three forms of input:
24 // 1) a single value, the bin width, in which case the bin boundaries will be calculated from the input workspace
25 // 2) a pair of values (a range), the lower and upper limits of the binning
26 // 3) a list of values in the form x1,dx1,x2,dx2,...,xn where the x's are bin boundaries and the dx's are bin widths
27
28 std::string error = "";
29 size_t numParams = value.size();
30 switch (numParams) {
31 case 0: {
32 if (!m_allowEmpty) { // unless allowed in the constructor
33 error = "Enter values for this property";
34 }
35 } break;
36 case 1: {
37 if (value[0] == 0.0) {
38 error = "Cannot have a zero bin width";
39 }
40 } break;
41 case 2: {
42 if (m_allowRange) { // if we allow ranges, must be in order
43 if (value[0] >= value[1]) {
44 error = "When giving a range the second value must be larger than the first";
45 }
46 } else { // if we don't allow ranges, then this is wrong
47 error = "The number of bin boundary parameters provided must be odd";
48 }
49 } break;
50 // three or more parameters must be in the form x1,dx1,x2,dx2,...,xn
51 default: {
52 if (value.size() % 2 == 0) { // even
53 error = "The number of bin boundary parameters provided must be odd";
54 } else { // odd
55 std::size_t numBins = 0; // defensively init to 0
56 try {
58 } catch (std::runtime_error &e) {
59 error = e.what();
60 break;
61 }
62 std::size_t binSpaceInBytes = numBins * sizeof(double);
63 error = MemoryStats().checkAvailableMemory(binSpaceInBytes);
64 } // end else odd
65 } // end default case
66 }
67
68 // return string, which contains any error messages
69 return error;
70}
71
72} // namespace Mantid::Kernel
double value
The value of the point.
Definition FitMW.cpp:51
double error
This class is responsible for memory statistics.
Definition Memory.h:28
std::string checkAvailableMemory(std::size_t const requestedMemoryBytes) const
Check if there is enough space in memory to hold the requested amount of memory.
Definition Memory.cpp:571
IValidator_sptr clone() const override
std::string checkValidity(const std::vector< double > &value) const override
Check on the inputed bin boundaries and widths.
RebinParamsValidator(bool allowEmpty=false, bool allowRange=false)
std::size_t MANTID_KERNEL_DLL estimateNumberOfBins(std::vector< double > const &params, double const power=-1)
Returns a size_t with the estimated number of bins that would be needed for a rebinning operation.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition IValidator.h:26