Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #11617. Added CalculateChiSquared algorithm.
- Loading branch information
1 parent
3ad34a0
commit bb3b073
Showing
9 changed files
with
281 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/CalculateChiSquared.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Code/Mantid/Framework/CurveFitting/src/CalculateChiSquared.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
44
Code/Mantid/docs/source/algorithms/CalculateChiSquared-v1.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:: | ||
|