From e3f0280221b9c62a59b371786f3a72ffd6464f8e Mon Sep 17 00:00:00 2001 From: Roman Tolchenov Date: Thu, 21 May 2015 15:45:36 +0100 Subject: [PATCH] Re #11617. Added property for chi^2 / DOF. --- .../CurveFitting/src/CalculateChiSquared.cpp | 45 ++++++++++--------- .../test/CalculateChiSquaredTest.h | 26 ++++------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/Code/Mantid/Framework/CurveFitting/src/CalculateChiSquared.cpp b/Code/Mantid/Framework/CurveFitting/src/CalculateChiSquared.cpp index c0514e92577e..a2bf562c3958 100644 --- a/Code/Mantid/Framework/CurveFitting/src/CalculateChiSquared.cpp +++ b/Code/Mantid/Framework/CurveFitting/src/CalculateChiSquared.cpp @@ -37,10 +37,11 @@ const std::string CalculateChiSquared::summary() const { //---------------------------------------------------------------------------------------------- /// Initialize the algorithm's properties. void CalculateChiSquared::initConcrete() { - declareProperty("DivideByDOF", false, "Flag to divide the chi^2 by the " - "number of degrees of freedom (NofData " - "- nOfParams)."); - declareProperty("ChiSquared", 0.0, "Output value of chi squared."); + declareProperty("ChiSquared", 0.0, "Output value of chi squared.", Direction::Output); + declareProperty("ChiSquaredDividedByDOF", 0.0, + "Output value of chi squared divided by the " + "number of degrees of freedom (NofData " + "- nOfParams).", Direction::Output); } //---------------------------------------------------------------------------------------------- @@ -70,25 +71,29 @@ void CalculateChiSquared::execConcrete() { } g_log.debug() << "Chi squared " << chiSquared << std::endl; + // Store the result. + setProperty("ChiSquared", chiSquared); + // Divide by the DOF - bool divideByDOF = getProperty("DivideByDOF"); - if (divideByDOF) { - // Get the number of free fitting parameters - size_t nParams = 0; - for(size_t i = 0; i < m_function->nParams(); ++i) { - if ( !m_function->isFixed(i) ) nParams += 1; - } - double dof = static_cast(domain->size()) - static_cast(nParams); - g_log.debug() << "DOF " << dof << std::endl; - if (dof <= 0.0) { - dof = 1.0; - g_log.warning() << "DOF has a non-positive value, changing to 1,0." << std::endl; - } - chiSquared /= dof; - g_log.debug() << "Chi squared / DOF " << chiSquared << std::endl; + // Get the number of free fitting parameters + size_t nParams = 0; + for (size_t i = 0; i < m_function->nParams(); ++i) { + if (!m_function->isFixed(i)) + nParams += 1; + } + double dof = + static_cast(domain->size()) - static_cast(nParams); + g_log.debug() << "DOF " << dof << std::endl; + if (dof <= 0.0) { + dof = 1.0; + g_log.warning() << "DOF has a non-positive value, changing to 1,0." + << std::endl; } + chiSquared /= dof; + g_log.debug() << "Chi squared / DOF " << chiSquared << std::endl; + // Store the result. - setProperty("ChiSquared", chiSquared); + setProperty("ChiSquaredDividedByDOF", chiSquared); } } // namespace CurveFitting diff --git a/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h b/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h index 3723ceabfb89..327d88c66887 100644 --- a/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h +++ b/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h @@ -133,7 +133,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { Tester tester; tester.set1DFunction(); tester.set1DSpectrumValues(); - tester.setDivideByDOF(); tester.runAlgorithm(); tester.check1DSpectrum(); TS_ASSERT_DELTA(tester.chiSquared, 236.5, 1.0); @@ -143,7 +142,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { Tester tester(1); tester.set1DFunction(); tester.set1DSpectrumValues(); - tester.setDivideByDOF(); tester.runAlgorithm(); tester.check1DSpectrum(); TS_ASSERT_DELTA(tester.chiSquared, 184.0, 1.0); @@ -153,7 +151,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { Tester tester(3,3); tester.set1DFunction(); tester.set1DSpectrumValues(); - tester.setDivideByDOF(); tester.runAlgorithm(); tester.check1DSpectrum(); TS_ASSERT_DELTA(tester.chiSquared, 5069.0, 1.0); @@ -163,7 +160,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { Tester tester(3,2); tester.set1DFunction(); tester.set1DSpectrumValues(); - tester.setDivideByDOF(); tester.runAlgorithm(); tester.check1DSpectrum(); TS_ASSERT_DELTA(tester.chiSquared, 7151.0, 1.0); @@ -187,7 +183,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { double StartX; double EndX; bool ignoreInvalidData; - bool divideByDOF; void makeXValues() { size_t dlt = isHisto ? 1 : 0; @@ -227,12 +222,13 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { public: // algorithm output double chiSquared; + double chiSquaredDividedByDOF; bool isExecuted; Tester(size_t np = 3, size_t nd = 10, bool histo = true) : nParams(np), nData(nd), isHisto(histo), workspaceIndex(0), xMin(-10), xMax(10), StartX(EMPTY_DBL()), EndX(EMPTY_DBL()), - ignoreInvalidData(false), divideByDOF(false), chiSquared(-1), isExecuted(false) { + ignoreInvalidData(false), chiSquared(-1), chiSquaredDividedByDOF(-1), isExecuted(false) { makeXValues(); } @@ -243,7 +239,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { TS_ASSERT_THROWS_NOTHING(alg.setProperty("Function", function)); TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", workspace)); TS_ASSERT_THROWS_NOTHING(alg.setProperty("IgnoreInvalidData", ignoreInvalidData)); - TS_ASSERT_THROWS_NOTHING(alg.setProperty("DivideByDOF", divideByDOF)); if (dynamic_cast(function.get())) { TS_ASSERT_THROWS_NOTHING(alg.setProperty("WorkspaceIndex", workspaceIndex)); TS_ASSERT_THROWS_NOTHING(alg.setProperty("StartX", StartX)); @@ -251,8 +246,10 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { } TS_ASSERT_THROWS_NOTHING(alg.execute()); isExecuted = alg.isExecuted(); - if (isExecuted) + if (isExecuted) { chiSquared = alg.getProperty("ChiSquared"); + chiSquaredDividedByDOF = alg.getProperty("ChiSquaredDividedByDOF"); + } } void setXRange_All() { @@ -278,10 +275,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { ignoreInvalidData = true; } - void setDivideByDOF() { - divideByDOF = true; - } - void set1DFunction() { const std::string fun = "name=UserFunction,Formula=a+b*x+c*x^2,a=1,b=1,c=1"; @@ -347,13 +340,12 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { sum2 += tmp * tmp; } } - if (divideByDOF) { - double dof = double(nData) - double(nParams); - if (dof <= 0.0) dof = 1.0; - sum2 /= dof; - } TS_ASSERT_DIFFERS(sum2, 0); TS_ASSERT_DELTA(sum2, chiSquared, 1e-10); + double dof = double(nData) - double(nParams); + if (dof <= 0.0) dof = 1.0; + sum2 /= dof; + TS_ASSERT_DELTA(sum2, chiSquaredDividedByDOF, 1e-10); } void checkFailed() {