Skip to content

Commit

Permalink
should be three failing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rboston628 committed Sep 24, 2024
1 parent 356e3be commit d72cb5f
Show file tree
Hide file tree
Showing 23 changed files with 361 additions and 187 deletions.
8 changes: 7 additions & 1 deletion Framework/API/src/NumericAxis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//----------------------------------------------------------------------
#include "MantidAPI/NumericAxis.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/VectorHelper.h"

#include <boost/format.hpp>
Expand All @@ -22,12 +23,17 @@ Mantid::Kernel::Logger g_log("NumericAxis");
class EqualWithinTolerance {
public:
explicit EqualWithinTolerance(double tolerance) : m_tolerance(tolerance){};
/**
* This handles NaNs and infs differently than the FloatingPointComparison operations.
* If this is not necessary, then this entire class may be replaced with
* Mantid::Kernel::withinAbsoluteDifference
*/
bool operator()(double a, double b) {
if (std::isnan(a) && std::isnan(b))
return true;
if (std::isinf(a) && std::isinf(b))
return true;
return std::abs(a - b) <= m_tolerance;
return Mantid::Kernel::withinAbsoluteDifference(a, b, m_tolerance);
}

private:
Expand Down
10 changes: 5 additions & 5 deletions Framework/API/src/WorkspaceOpOverloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/Property.h"

#include <numeric>
Expand Down Expand Up @@ -376,10 +377,10 @@ bool WorkspaceHelpers::matchingBins(const MatrixWorkspace &ws1, const MatrixWork
const double secondWS = std::accumulate(ws2.x(0).begin(), ws2.x(0).end(), 0.);
if (std::abs(firstWS) < 1.0E-7 && std::abs(secondWS) < 1.0E-7) {
for (size_t i = 0; i < ws1.x(0).size(); i++) {
if (std::abs(ws1.x(0)[i] - ws2.x(0)[i]) > 1.0E-7)
if (!Kernel::withinAbsoluteDifference(ws1.x(0)[i], ws2.x(0)[i], 1.0E-7))
return false;
}
} else if (std::abs(firstWS - secondWS) / std::max<double>(std::abs(firstWS), std::abs(secondWS)) > 1.0E-7)
} else if (!Kernel::withinRelativeDifference(firstWS, secondWS, 1.0E-7))
return false;

// If we were only asked to check the first spectrum, return now
Expand Down Expand Up @@ -409,11 +410,10 @@ bool WorkspaceHelpers::matchingBins(const MatrixWorkspace &ws1, const MatrixWork
const double secondWSLoop = std::accumulate(ws2.x(i).begin(), ws2.x(i).end(), 0.);
if (std::abs(firstWSLoop) < 1.0E-7 && std::abs(secondWSLoop) < 1.0E-7) {
for (size_t j = 0; j < ws1.x(i).size(); j++) {
if (std::abs(ws1.x(i)[j] - ws2.x(i)[j]) > 1.0E-7)
if (!Kernel::withinAbsoluteDifference(ws1.x(i)[j], ws2.x(i)[j], 1.0E-7))
return false;
}
} else if (std::abs(firstWSLoop - secondWSLoop) / std::max<double>(std::abs(firstWSLoop), std::abs(secondWSLoop)) >
1.0E-7)
} else if (!Kernel::withinRelativeDifference(firstWSLoop, secondWSLoop, 1.0E-7))
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/Material.h"

#include <stdexcept>
Expand Down Expand Up @@ -128,12 +129,11 @@ void CalculateCarpenterSampleCorrection::exec() {
const Material &sampleMaterial = inputWksp->sample().getMaterial();
if (sampleMaterial.totalScatterXSection() != 0.0) {
g_log.information() << "Using material \"" << sampleMaterial.name() << "\" from workspace\n";
if (std::abs(coeff1 - 2.8) < std::numeric_limits<double>::epsilon())
if (Kernel::equals(coeff1, 2.8))
coeff1 = sampleMaterial.absorbXSection(LAMBDA_REF) / LAMBDA_REF;
if ((std::abs(coeff2 - 0.0721) < std::numeric_limits<double>::epsilon()) &&
(!isEmpty(sampleMaterial.numberDensity())))
if (Kernel::equals(coeff2, 0.0721) && !isEmpty(sampleMaterial.numberDensity()))
coeff2 = sampleMaterial.numberDensity();
if (std::abs(coeff3 - 5.1) < std::numeric_limits<double>::epsilon())
if (Kernel::equals(coeff3, 5.1))
coeff3 = sampleMaterial.totalScatterXSection();
} else // Save input in Sample with wrong atomic number and name
{
Expand Down
58 changes: 24 additions & 34 deletions Framework/Algorithms/src/CompareWorkspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "MantidGeometry/Crystal/IPeak.h"
#include "MantidGeometry/Instrument/ComponentInfo.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/Unit.h"

namespace Mantid::Algorithms {
Expand Down Expand Up @@ -71,23 +72,18 @@ int compareEventLists(Kernel::Logger &logger, const EventList &el1, const EventL
const auto &e1 = events1[i];
const auto &e2 = events2[i];

bool diffpulse = false;
bool difftof = false;
bool diffweight = false;
if (std::abs(e1.pulseTime().totalNanoseconds() - e2.pulseTime().totalNanoseconds()) > tolPulse) {
diffpulse = true;
++numdiffpulse;
}
if (std::abs(e1.tof() - e2.tof()) > tolTof) {
difftof = true;
++numdifftof;
}
bool diffpulse =
!withinAbsoluteDifference(e1.pulseTime().totalNanoseconds(), e2.pulseTime().totalNanoseconds(), tolPulse);
bool difftof = !withinAbsoluteDifference(e1.tof(), e2.tof(), tolTof);
bool diffweight = !withinAbsoluteDifference(e1.weight(), e2.weight(), tolWeight);
if (diffpulse && difftof)
++numdiffboth;
if (std::abs(e1.weight() - e2.weight()) > tolWeight) {
diffweight = true;
++numdiffweight;
}
numdiffboth++;
if (diffpulse)
numdiffpulse++;
if (difftof)
numdifftof++;
if (diffweight)
numdiffweight++;

bool same = (!diffpulse) && (!difftof) && (!diffweight);
if (!same) {
Expand Down Expand Up @@ -663,6 +659,8 @@ bool CompareWorkspaces::checkData(const API::MatrixWorkspace_const_sptr &ws1,
if (checkError && !err)
err = !m_compare(E1[j], E2[j]);
if (err) {
printf("MISMATCH ws (%d, %d) : %le %le | %le %le | %le %le \n", i, j, X1[j], X2[j], Y1[j], Y2[j], E1[j],
E2[j]);
if (logDebug) {
g_log.debug() << "Data mismatch at cell (hist#,bin#): (" << i << "," << j << ")\n";
g_log.debug() << " Dataset #1 (X,Y,E) = (" << X1[j] << "," << Y1[j] << "," << E1[j] << ")\n";
Expand Down Expand Up @@ -1049,6 +1047,7 @@ void CompareWorkspaces::doPeaksComparison(PeaksWorkspace_sptr tws1, PeaksWorkspa
}

const bool isRelErr = getProperty("ToleranceRelErr");
const bool checkAllData = getProperty("CheckAllData");
for (int i = 0; i < tws1->getNumberPeaks(); i++) {
const Peak &peak1 = tws1->getPeak(i);
const Peak &peak2 = tws2->getPeak(i);
Expand Down Expand Up @@ -1121,13 +1120,15 @@ void CompareWorkspaces::doPeaksComparison(PeaksWorkspace_sptr tws1, PeaksWorkspa
!m_compare(v1[2], v2[2]); //
}
if (mismatch) {
printf("MISTMACH col %s - (%d, %d) values %le %le\n", name.c_str(), i, j, s1, s2);
g_log.notice(name);
g_log.notice() << "data mismatch in column name = " << name << "\n"
<< "cell (row#, col#): (" << i << "," << j << ")\n"
<< "value1 = " << s1 << "\n"
<< "value2 = " << s2 << "\n";
recordMismatch("Data mismatch");
return;
if (!checkAllData)
return;
}
}
}
Expand Down Expand Up @@ -1163,6 +1164,7 @@ void CompareWorkspaces::doLeanElasticPeaksComparison(const LeanElasticPeaksWorks

const double tolerance = getProperty("Tolerance");
const bool isRelErr = getProperty("ToleranceRelErr");
const bool checkAllData = getProperty("CheckAllData");
for (int peakIndex = 0; peakIndex < ipws1->getNumberPeaks(); peakIndex++) {
for (size_t j = 0; j < ipws1->columnCount(); j++) {
std::shared_ptr<const API::Column> col = ipws1->getColumn(j);
Expand Down Expand Up @@ -1236,13 +1238,15 @@ void CompareWorkspaces::doLeanElasticPeaksComparison(const LeanElasticPeaksWorks
mismatch = true;
}
if (mismatch) {
printf("MISTMACH (%d, %d) values %le %le\n", peakIndex, j, s1, s2);
g_log.notice(name);
g_log.notice() << "data mismatch in column name = " << name << "\n"
<< "cell (row#, col#): (" << peakIndex << "," << j << ")\n"
<< "value1 = " << s1 << "\n"
<< "value2 = " << s2 << "\n";
recordMismatch("Data mismatch");
return;
if (!checkAllData)
return;
}
}
}
Expand Down Expand Up @@ -1360,8 +1364,7 @@ this error is within the limits requested.
@returns true if absolute difference is within the tolerance; false otherwise
*/
bool CompareWorkspaces::withinAbsoluteTolerance(double const x1, double const x2, double const atol) {
// NOTE !(|x1-x2| > atol) is not the same as |x1-x2| <= atol
return !(std::abs(x1 - x2) > atol);
return Kernel::withinAbsoluteDifference(x1, x2, atol);
}

//------------------------------------------------------------------------------------------------
Expand All @@ -1376,19 +1379,6 @@ this error is within the limits requested.
@returns true if error or false if the relative value is within the limits requested
*/
bool CompareWorkspaces::withinRelativeTolerance(double const x1, double const x2, double const rtol) {
// calculate difference
double const num = std::abs(x1 - x2);
// return early if the values are equal
if (num == 0.0)
return true;
// create the average magnitude for comparison
double const den = 0.5 * (std::abs(x1) + std::abs(x2));
// return early, possibly avoids a multiplication
// NOTE if den<1, then divsion will only make num larger
// NOTE if den<1 but num<=rtol, we cannot conclude anything
if (den <= 1.0 && num > rtol)
return false;
// NOTE !(num > rtol*den) is not the same as (num <= rtol*den)
return !(num > (rtol * den));
return Kernel::withinRelativeDifference(x1, x2, rtol);
}
} // namespace Mantid::Algorithms
10 changes: 6 additions & 4 deletions Framework/Algorithms/src/FindCenterOfMassPosition2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/PhysicalConstants.h"
namespace Mantid::Algorithms {

Expand All @@ -29,10 +30,11 @@ using namespace API;
using namespace Geometry;
using namespace DataObjects;

namespace { // anonymous namespace
// namespace { // anonymous namespace
/// Equal within machine tolerance
bool almost_equals(const double a, const double b) { return fabs(a - b) < std::numeric_limits<double>::min(); }
} // anonymous namespace
// bool almost_equals(double const a, double const b) { return Kernel::equals(a, b); }
// bool almost_equals(const double a, const double b) { return fabs(a - b) < std::numeric_limits<double>::min(); }
// } // anonymous namespace

void FindCenterOfMassPosition2::init() {
const auto wsValidator = std::make_shared<CompositeValidator>();
Expand Down Expand Up @@ -113,7 +115,7 @@ void FindCenterOfMassPosition2::findCenterOfMass(const API::MatrixWorkspace_sptr

// Check to see if we have the same result
// as the previous iteration
if (almost_equals(distanceFromPrevious, distanceFromPreviousPrevious)) {
if (Kernel::equals(distanceFromPrevious, distanceFromPreviousPrevious)) {
totalLocalMinima++;
} else {
totalLocalMinima = 0;
Expand Down
11 changes: 7 additions & 4 deletions Framework/Algorithms/src/FitPeak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void FitOneSinglePeak::setupGuessedFWHM(double usrwidth, int minfwhm, int maxfwh
void FitOneSinglePeak::setFitPeakCriteria(bool usepeakpostol, double peakpostol) {
m_usePeakPositionTolerance = usepeakpostol;
if (usepeakpostol) {
m_peakPositionTolerance = fabs(peakpostol);
m_peakPositionTolerance = std::abs(peakpostol);
if (peakpostol < 1.0E-13)
g_log.warning("Peak position tolerance is very tight. ");
}
Expand Down Expand Up @@ -931,7 +931,7 @@ void FitOneSinglePeak::processNStoreFitResult(double rwp, bool storebkgd) {
double f_centre = m_peakFunc->centre();
if (m_usePeakPositionTolerance) {
// Peak position criteria is on position tolerance
if (fabs(f_centre - m_userPeakCentre) > m_peakPositionTolerance) {
if (std::abs(f_centre - m_userPeakCentre) > m_peakPositionTolerance) {
rwp = DBL_MAX;
failreason = "Peak centre out of tolerance. ";
fitsuccess = false;
Expand Down Expand Up @@ -1407,8 +1407,11 @@ void FitPeak::setupOutput(const std::map<std::string, double> &m_fitErrorPeakFun
// TODO - Need to retrieve useful information from FitOneSinglePeak object
// (think of how)
const auto &vecX = m_dataWS->x(m_wsIndex);
const size_t i_minFitX = getIndex(vecX, m_minFitX);
const size_t i_maxFitX = getIndex(vecX, m_maxFitX);
// NOTE these scoped variables were shadowed, and were not actually being used in the calculations below.
// It is unclear if this represents and error, or was due to misunderstanding of member variables.
// Existing tests expect the current behavior, which does not use these.
// const size_t i_minFitX = getIndex(vecX, m_minFitX);
// const size_t i_maxFitX = getIndex(vecX, m_maxFitX);

// Data workspace
const size_t nspec = 3;
Expand Down
9 changes: 5 additions & 4 deletions Framework/Algorithms/src/He3TubeEfficiency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "MantidKernel/ArrayBoundedValidator.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/FloatingPointComparison.h"

#include <cmath>
#include <stdexcept>
Expand Down Expand Up @@ -216,7 +217,7 @@ double He3TubeEfficiency::calculateExponential(std::size_t spectraIndex, const G
double sinTheta = std::sqrt(1.0 - cosTheta * cosTheta);

const double straight_path = detDiameter - twiceTubeThickness;
if (std::fabs(straight_path - 0.0) < TOL) {
if (Kernel::withinAbsoluteDifference(straight_path, 0.0, TOL)) {
throw std::out_of_range("Twice tube thickness cannot be greater than "
"or equal to the tube diameter");
}
Expand Down Expand Up @@ -245,15 +246,15 @@ void He3TubeEfficiency::getDetectorGeometry(const Geometry::IDetector &det, doub
if (it == m_shapeCache.end()) {
double xDist = distToSurface(Kernel::V3D(DIST_TO_UNIVERSE_EDGE, 0, 0), shape_sptr.get());
double zDist = distToSurface(Kernel::V3D(0, 0, DIST_TO_UNIVERSE_EDGE), shape_sptr.get());
if (std::abs(zDist - xDist) < 1e-8) {
if (Kernel::withinAbsoluteDifference(zDist, xDist, 1e-8)) {
detRadius = zDist / 2.0;
detAxis = Kernel::V3D(0, 1, 0);
// assume radii in z and x and the axis is in the y
PARALLEL_CRITICAL(deteff_shapecachea) { m_shapeCache.insert({shape_sptr.get(), {detRadius, detAxis}}); }
return;
}
double yDist = distToSurface(Kernel::V3D(0, DIST_TO_UNIVERSE_EDGE, 0), shape_sptr.get());
if (std::abs(yDist - zDist) < 1e-8) {
if (Kernel::withinAbsoluteDifference(yDist, zDist, 1e-8)) {
detRadius = yDist / 2.0;
detAxis = Kernel::V3D(1, 0, 0);
// assume that y and z are radii of the cylinder's circular cross-section
Expand All @@ -262,7 +263,7 @@ void He3TubeEfficiency::getDetectorGeometry(const Geometry::IDetector &det, doub
return;
}

if (std::abs(xDist - yDist) < 1e-8) {
if (Kernel::withinAbsoluteDifference(xDist, yDist, 1e-8)) {
detRadius = xDist / 2.0;
detAxis = Kernel::V3D(0, 0, 1);
PARALLEL_CRITICAL(deteff_shapecachec) { m_shapeCache.insert({shape_sptr.get(), {detRadius, detAxis}}); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "MantidAPI/Run.h"
#include "MantidAlgorithms/RunCombinationHelpers/SampleLogsBehaviour.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/Property.h"
#include "MantidKernel/StringTokenizer.h"
#include "MantidKernel/Strings.h"
Expand Down Expand Up @@ -531,7 +532,7 @@ void SampleLogsBehaviour::checkErrorProperty(const MatrixWorkspace &addeeWS, Pro
bool SampleLogsBehaviour::isWithinTolerance(const SampleLogBehaviour &behaviour, const double addeeWSNumericValue,
const double outWSNumericValue) {
if (behaviour.isNumeric && behaviour.tolerance > 0.0) {
return std::abs(addeeWSNumericValue - outWSNumericValue) < behaviour.tolerance;
return Kernel::withinAbsoluteDifference(addeeWSNumericValue, outWSNumericValue, behaviour.tolerance);
}

return false;
Expand Down
5 changes: 3 additions & 2 deletions Framework/Algorithms/src/Stitch1D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "MantidHistogramData/HistogramY.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/MultiThreaded.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/RebinParamsValidator.h"
Expand Down Expand Up @@ -491,10 +492,10 @@ void Stitch1D::exec() {
const double xMin = params.front();
const double xMax = params.back();

if (std::abs(xMin - startOverlap) < 1E-6)
if (Kernel::withinAbsoluteDifference(xMin, startOverlap, 1E-6))
startOverlap = xMin;

if (std::abs(xMax - endOverlap) < 1E-6)
if (Kernel::withinAbsoluteDifference(xMax, endOverlap, 1E-6))
endOverlap = xMax;

if (startOverlap < xMin) {
Expand Down
5 changes: 3 additions & 2 deletions Framework/Algorithms/src/UnwrapMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/FloatingPointComparison.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/UnitFactory.h"

Expand Down Expand Up @@ -204,7 +205,7 @@ const std::vector<int> UnwrapMonitor::unwrapX(std::vector<double> &newX, const i
const double wavelength = m_conversionConstant / velocity;
newX.emplace_back(wavelength);
// Remove the duplicate boundary bin
if (tof == m_Tmax && std::abs(wavelength - tempX_L.front()) < 1.0e-5)
if (tof == m_Tmax && Kernel::withinAbsoluteDifference(wavelength, tempX_L.front(), 1.0e-5))
newX.pop_back();
// Record the bins that fall in this range for copying over the data &
// errors
Expand All @@ -223,7 +224,7 @@ const std::vector<int> UnwrapMonitor::unwrapX(std::vector<double> &newX, const i

// Record the point at which the unwrapped sections are joined, first time
// through only
Property *join = getProperty("JoinWavelength");
Property const *join = getProperty("JoinWavelength");
if (join->isDefault()) {
g_log.information() << "Joining wavelength: " << tempX_L.front() << " Angstrom\n";
setProperty("JoinWavelength", tempX_L.front());
Expand Down
Loading

0 comments on commit d72cb5f

Please sign in to comment.