Skip to content

Commit

Permalink
Re #11617. Added CalculateChiSquared algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed May 11, 2015
1 parent 3ad34a0 commit bb3b073
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Expand Up @@ -12,6 +12,7 @@ set ( SRC_FILES
src/BivariateNormal.cpp
src/Bk2BkExpConvPV.cpp
src/BoundaryConstraint.cpp
src/CalculateChiSquared.cpp
src/CalculateGammaBackground.cpp
src/CalculateMSVesuvio.cpp
src/Chebyshev.cpp
Expand Down Expand Up @@ -127,6 +128,7 @@ set ( INC_FILES
inc/MantidCurveFitting/BivariateNormal.h
inc/MantidCurveFitting/Bk2BkExpConvPV.h
inc/MantidCurveFitting/BoundaryConstraint.h
inc/MantidCurveFitting/CalculateChiSquared.h
inc/MantidCurveFitting/CalculateGammaBackground.h
inc/MantidCurveFitting/CalculateMSVesuvio.h
inc/MantidCurveFitting/Chebyshev.h
Expand Down Expand Up @@ -242,6 +244,7 @@ set ( TEST_FILES
BivariateNormalTest.h
Bk2BkExpConvPVTest.h
BoundaryConstraintTest.h
CalculateChiSquaredTest.h
CalculateGammaBackgroundTest.h
CalculateMSVesuvioTest.h
ChebyshevTest.h
Expand Down
@@ -0,0 +1,52 @@
#ifndef MANTID_CURVEFITTING_CALCULATECHISQUARED_H_
#define MANTID_CURVEFITTING_CALCULATECHISQUARED_H_

#include "MantidKernel/System.h"
#include "MantidCurveFitting/IFittingAlgorithm.h"

namespace Mantid {
namespace CurveFitting {

/**
Calculate chi squared for a function and a data set in a workspace.
Copyright © 2015 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport CalculateChiSquared : public IFittingAlgorithm {
public:
CalculateChiSquared();
virtual ~CalculateChiSquared();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string summary() const;

private:
void initConcrete();
void execConcrete();
};

} // namespace CurveFitting
} // namespace Mantid

#endif /* MANTID_CURVEFITTING_CALCULATECHISQUARED_H_ */
Expand Up @@ -110,7 +110,7 @@ class DLLExport Fit : public IFittingAlgorithm {

protected:
void initConcrete();
void exec();
void execConcrete();
void copyMinimizerOutput(const API::IFuncMinimizer &minimizer);
};

Expand Down
Expand Up @@ -56,8 +56,12 @@ class DLLExport IFittingAlgorithm : public API::Algorithm {

private:
void init();
void exec();
/// Child classes declare their properties here.
virtual void initConcrete() = 0;
/// Child classes implement the algorithm logic here.
virtual void execConcrete() = 0;

virtual void afterPropertySet(const std::string &propName);
void addWorkspace(const std::string &workspaceNameProperty,
bool addProperties = true);
Expand Down
50 changes: 50 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/CalculateChiSquared.cpp
@@ -0,0 +1,50 @@
#include "MantidCurveFitting/CalculateChiSquared.h"

namespace Mantid {
namespace CurveFitting {

using namespace Mantid::Kernel;
using namespace Mantid::API;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CalculateChiSquared)

//----------------------------------------------------------------------------------------------
/** Constructor
*/
CalculateChiSquared::CalculateChiSquared() {}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
CalculateChiSquared::~CalculateChiSquared() {}

//----------------------------------------------------------------------------------------------

/// Algorithms name for identification. @see Algorithm::name
const std::string CalculateChiSquared::name() const {
return "CalculateChiSquared";
}

/// Algorithm's version for identification. @see Algorithm::version
int CalculateChiSquared::version() const { return 1; }

/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string CalculateChiSquared::summary() const {
return "Calculate chi squared for a function and a data set in a workspace.";
}

//----------------------------------------------------------------------------------------------
/// Initialize the algorithm's properties.
void CalculateChiSquared::initConcrete() {
declareProperty("ChiSquared", 0.0, "Output value of chi squared.");
}

//----------------------------------------------------------------------------------------------
/// Execute the algorithm.
void CalculateChiSquared::execConcrete() {

}

} // namespace CurveFitting
} // namespace Mantid
7 changes: 1 addition & 6 deletions Code/Mantid/Framework/CurveFitting/src/Fit.cpp
Expand Up @@ -118,12 +118,7 @@ void Fit::copyMinimizerOutput(const API::IFuncMinimizer &minimizer) {
*
* @throw runtime_error Thrown if algorithm cannot execute
*/
void Fit::exec() {
// this is to make it work with AlgorithmProxy
if (!m_domainCreator) {
setFunction();
addWorkspaces();
}
void Fit::execConcrete() {

std::string ties = getPropertyValue("Ties");
if (!ties.empty()) {
Expand Down
12 changes: 12 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/IFittingAlgorithm.cpp
Expand Up @@ -269,6 +269,18 @@ void IFittingAlgorithm::addWorkspaces() {
}
}
}
//----------------------------------------------------------------------------------------------
/// Execute the algorithm.
void IFittingAlgorithm::exec() {

// This is to make it work with AlgorithmProxy
if (!m_domainCreator) {
setFunction();
addWorkspaces();
}
// Execute the concrete algorithm.
this->execConcrete();
}

} // namespace CurveFitting
} // namespace Mantid
114 changes: 114 additions & 0 deletions Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h
@@ -0,0 +1,114 @@
#ifndef MANTID_CURVEFITTING_CALCULATECHISQUAREDTEST_H_
#define MANTID_CURVEFITTING_CALCULATECHISQUAREDTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidCurveFitting/CalculateChiSquared.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidKernel/EmptyValues.h"

using Mantid::CurveFitting::CalculateChiSquared;
using namespace Mantid;
using namespace Mantid::API;

class CalculateChiSquaredTest : 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 CalculateChiSquaredTest *createSuite() { return new CalculateChiSquaredTest(); }
static void destroySuite( CalculateChiSquaredTest *suite ) { delete suite; }


void test_Init()
{
CalculateChiSquared alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}

void test_exec()
{
Tester tester;
tester.set1DFunction();
tester.set1DSpectrumEmpty();
tester.runAlgorithm();
}

private:

class Tester{

// input parameters
size_t nParams;
size_t nData;
bool isHisto;
double xMin;
double xMax;
std::vector<double> xBins;
std::vector<double> xValues;

// values for algorithm input properties
IFunction_sptr function;
Workspace_sptr workspace;
int workspaceIndex;
double StartX;
double EndX;

// algorithm output
double chiSquared;

void makeXValues(){
size_t dlt = isHisto ? 1 : 0;
xBins.resize(nData + dlt);
double dx = (xMax - xMin) / double(xBins.size() - 1);
for(size_t i = 0; i < xBins.size(); ++i)
{
xBins[i] = xMin + i * dx;
}
if (isHisto){
xValues.resize(nData);
std::transform(xBins.begin(),xBins.end()-1, xValues.begin(), std::bind2nd(std::plus<double>(),dx/2));
} else {
xValues = xBins;
}
}

public:
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()) {}

void runAlgorithm(){

CalculateChiSquared alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setProperty("Function", function) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", workspace) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

chiSquared = alg.getProperty("ChiSquared");
}

void set1DFunction(){
const std::string fun = "name=UserFunction,Formula=a+b*x+c*x^2,a=1,b=1,c=1";
function = FunctionFactory::Instance().createInitialized(fun);
TS_ASSERT_EQUALS(function->nParams(), nParams);
}

void set1DSpectrumEmpty(){
makeXValues();
const size_t nSpec = 1;
auto space = WorkspaceFactory::Instance().create("Workspace2D", nSpec, nData+1, nData);
space->dataX(0).assign(xBins.begin(),xBins.end());
workspace = space;
}

};

};


#endif /* MANTID_CURVEFITTING_CALCULATECHISQUAREDTEST_H_ */
44 changes: 44 additions & 0 deletions Code/Mantid/docs/source/algorithms/CalculateChiSquared-v1.rst
@@ -0,0 +1,44 @@

.. algorithm::

.. summary::

.. alias::

.. properties::

Description
-----------

TODO: Enter a full rst-markup description of your algorithm here.


Usage
-----
.. Try not to use files in your examples,
but if you cannot avoid it then the (small) files must be added to
autotestdata\UsageData and the following tag unindented
.. include:: ../usagedata-note.txt
**Example - CalculateChiSquared**

.. testcode:: CalculateChiSquaredExample

# Create a host workspace
ws = CreateWorkspace(DataX=range(0,3), DataY=(0,2))
or
ws = CreateSampleWorkspace()

wsOut = CalculateChiSquared()

# Print the result
print "The output workspace has %i spectra" % wsOut.getNumberHistograms()

Output:

.. testoutput:: CalculateChiSquaredExample

The output workspace has ?? spectra

.. categories::

0 comments on commit bb3b073

Please sign in to comment.