Skip to content

Commit

Permalink
Tidy up the fitting tests. Refs #5736
Browse files Browse the repository at this point in the history
Removes a lot of copy-and-pasted code and also ensures that we
should no longer see random unit test failures.
  • Loading branch information
martyngigg committed Aug 10, 2012
1 parent dc23aff commit b62568c
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 1,873 deletions.
6 changes: 5 additions & 1 deletion Code/Mantid/Framework/Algorithms/test/FlatBackgroundTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "MantidAlgorithms/FlatBackground.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidCurveFitting/Linear.h"
#include "MantidKernel/MersenneTwister.h"
#include <boost/lexical_cast.hpp>
#include <cmath>

Expand All @@ -25,11 +26,14 @@ class FlatBackgroundTest : public CxxTest::TestSuite
bg = 100.0;
Mantid::DataObjects::Workspace2D_sptr WS(new Mantid::DataObjects::Workspace2D);
WS->initialize(1,NUMBINS+1,NUMBINS);
const size_t seed(12345);
const double lower(-1.0), upper(1.0);
MersenneTwister randGen(seed, lower, upper);

for (int i = 0; i < NUMBINS; ++i)
{
WS->dataX(0)[i] = i;
WS->dataY(0)[i] = bg+(static_cast<double>(std::rand()-RAND_MAX/2)/static_cast<double>(RAND_MAX/2));
WS->dataY(0)[i] = bg + randGen.nextValue();
WS->dataE(0)[i] = 0.05*WS->dataY(0)[i];
}
WS->dataX(0)[NUMBINS] = NUMBINS;
Expand Down
6 changes: 4 additions & 2 deletions Code/Mantid/Framework/Crystal/test/IntegratePeakCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/MersenneTwister.h"
#include "MantidAPI/IPeak.h"
#include <math.h>
#include <cstdlib>
Expand All @@ -56,6 +57,7 @@ class IntegratePeakCheck: public CxxTest::TestSuite
{
Mantid::API::FrameworkManager::Instance();
usePoisson = false;
m_randGen.setSeed(1234);
}
/*
int* ArryofIDs = new int[500];
Expand Down Expand Up @@ -483,11 +485,11 @@ ISAWIntensityError 59.4822 69.8998 78.9547 87.073 78.9547

private:
bool usePoisson;

Mantid::Kernel::MersenneTwister m_randGen;
double Poisson( double mean)
{
double T = exp(-mean);
double P = rand()/(double)RAND_MAX;
double P = m_randGen.nextValue();
int N=0;
double S =T;
// std::cout<< "Poisson "<< mean<<","<<P<<","<<T;
Expand Down
4 changes: 3 additions & 1 deletion Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ target_link_libraries ( CurveFitting ${MANTIDLIBS} ${GSL_LIBRARIES} )

if ( CXXTEST_FOUND )
include_directories ( ../DataHandling/inc ../TestHelpers/inc )
cxxtest_add_test ( CurveFittingTest ${TEST_FILES} )
set ( TESTHELPER_SRCS ../TestHelpers/src/WorkspaceCreationHelper.cpp
../TestHelpers/src/ComponentCreationHelper.cpp ) # Used by cxx_test_add_test
cxxtest_add_test ( CurveFittingTest ${TEST_FILES} )
target_link_libraries( CurveFittingTest CurveFitting DataHandling DataObjects ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES})
add_dependencies ( FrameworkTests CurveFittingTest )
# Add to the 'FrameworkTests' group in VS
Expand Down
127 changes: 30 additions & 97 deletions Code/Mantid/Framework/CurveFitting/test/CompositeFunctionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
#include "MantidAPI/FunctionDomain1D.h"
#include "MantidAPI/FunctionValues.h"

using namespace Mantid;
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::CurveFitting;
Expand Down Expand Up @@ -139,6 +140,18 @@ DECLARE_FUNCTION(CurveFittingGauss);

class CompositeFunctionTest : public CxxTest::TestSuite
{
private:

struct TestFunction
{
double operator()(double x,int)
{
double x1 = x-4;
double x2 = x-6;
return 1. + 0.1*x + std::exp(-0.5*(x1*x1)*2)+2*std::exp(-0.5*(x2*x2)*3);
}
};

public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
Expand All @@ -147,7 +160,7 @@ class CompositeFunctionTest : public CxxTest::TestSuite

CompositeFunctionTest()
{
Kernel::ConfigService::Instance().setString("curvefitting.peakRadius","100");
Mantid::Kernel::ConfigService::Instance().setString("curvefitting.peakRadius","100");
FrameworkManager::Instance();
}

Expand Down Expand Up @@ -184,9 +197,9 @@ class CompositeFunctionTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(mfun->getParameter(6),1.1);
TS_ASSERT_EQUALS(mfun->getParameter(7),1.0);

WS_type ws = mkWS(1,0,10,0.1);
addNoise(ws,0.1);
storeWS("mfun",ws);
auto ws = WorkspaceCreationHelper::Create2DWorkspaceFromFunction(TestFunction(), 1, 0.0, 10.0, 0.1);
WorkspaceCreationHelper::addNoise(ws,0.1);
WorkspaceCreationHelper::storeWS("mfun",ws);

IFunction_sptr out;

Expand All @@ -199,7 +212,7 @@ class CompositeFunctionTest : public CxxTest::TestSuite
alg.setProperty("CreateOutput",true);
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
WS_type outWS = getWS("mfun_Workspace");
auto outWS = WorkspaceCreationHelper::getWS<MatrixWorkspace>("mfun_Workspace");

const Mantid::MantidVec& Y00 = ws->readY(0);
const Mantid::MantidVec& Y0 = outWS->readY(0);
Expand Down Expand Up @@ -239,7 +252,7 @@ class CompositeFunctionTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(out->parameterName(7),"f2.s");
TS_ASSERT_DELTA(out->getParameter(7),2.8530,0.3);

TWS_type outParams = getTWS("mfun_Parameters");
auto outParams = WorkspaceCreationHelper::getWS<TableWorkspace>("mfun_Parameters");
TS_ASSERT(outParams);

TS_ASSERT_EQUALS(outParams->rowCount(),9);
Expand Down Expand Up @@ -277,9 +290,9 @@ class CompositeFunctionTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(row.String(0),"f2.s");
TS_ASSERT_DELTA(row.Double(1),3.0,0.2);

removeWS("mfun");
removeWS("mfun_0_Workspace");
removeWS("mfun_0_Parameters");
WorkspaceCreationHelper::removeWS("mfun");
WorkspaceCreationHelper::removeWS("mfun_0_Workspace");
WorkspaceCreationHelper::removeWS("mfun_0_Parameters");

}

Expand All @@ -291,8 +304,8 @@ class CompositeFunctionTest : public CxxTest::TestSuite
x[i] = 0.1 * double(i);
y[i] = 3.3 * x[i] + 4.4;
}
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(x));
API::FunctionValues_sptr values(new API::FunctionValues(*domain));
FunctionDomain1D_sptr domain(new FunctionDomain1DVector(x));
FunctionValues_sptr values(new FunctionValues(*domain));
values->setFitData(y);
values->setFitWeights(1.0);

Expand Down Expand Up @@ -330,8 +343,8 @@ class CompositeFunctionTest : public CxxTest::TestSuite
x[i] = t;
y[i] = 0.1 * t * t + 3.3 * t + 4.4;
}
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(x));
API::FunctionValues_sptr values(new API::FunctionValues(*domain));
FunctionDomain1D_sptr domain(new FunctionDomain1DVector(x));
FunctionValues_sptr values(new FunctionValues(*domain));
values->setFitData(y);
values->setFitWeights(1.0);

Expand All @@ -349,13 +362,6 @@ class CompositeFunctionTest : public CxxTest::TestSuite
mfun->addFunction(fun1);
mfun->addFunction(fun2);

//CurveFitting::GSLJacobian J(mfun, values->size());
//mfun->functionDeriv(*domain,J);
//for(size_t i = 0; i < values->size(); ++i)
//{
// std::cerr << (*domain)[i] << " " << J.get(i,0) << ' ' << J.get(i,1) << ' ' << J.get(i,2) << std::endl;
//}

boost::shared_ptr<CostFuncLeastSquares> costFun(new CostFuncLeastSquares);
costFun->setFittingFunction(mfun,domain,values);

Expand All @@ -371,16 +377,16 @@ class CompositeFunctionTest : public CxxTest::TestSuite

void test_with_LM()
{
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector( 0.0, 10.0, 10));
API::FunctionValues mockData(*domain);
FunctionDomain1D_sptr domain(new FunctionDomain1DVector( 0.0, 10.0, 10));
FunctionValues mockData(*domain);
UserFunction dataMaker;
dataMaker.setAttributeValue("Formula","a*x+b+c*x^2");
dataMaker.setParameter("a",3.3);
dataMaker.setParameter("b",4.4);
dataMaker.setParameter("c",0.1);
dataMaker.function(*domain,mockData);

API::FunctionValues_sptr values(new API::FunctionValues(*domain));
FunctionValues_sptr values(new FunctionValues(*domain));
values->setFitDataFromCalculated(mockData);
values->setFitWeights(1.0);

Expand Down Expand Up @@ -411,79 +417,6 @@ class CompositeFunctionTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(s.getError(),"success");
}

private:
WS_type mkWS(int nSpec,double x0,double x1,double dx,bool isHist=false)
{
int nX = int((x1 - x0)/dx) + 1;
int nY = nX - (isHist?1:0);
if (nY <= 0)
throw std::invalid_argument("Cannot create an empty workspace");

Mantid::DataObjects::Workspace2D_sptr ws = boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>
(WorkspaceFactory::Instance().create("Workspace2D",nSpec,nX,nY));

double x;

for(int iSpec=0;iSpec<nSpec;iSpec++)
{
Mantid::MantidVec& X = ws->dataX(iSpec);
Mantid::MantidVec& Y = ws->dataY(iSpec);
Mantid::MantidVec& E = ws->dataE(iSpec);
for(int i=0;i<nY;i++)
{
x = x0 + dx*i;
X[i] = x;
double x1 = x-4;
double x2 = x-6;
Y[i] = 1. + 0.1*x + exp(-0.5*(x1*x1)*2)+2*exp(-0.5*(x2*x2)*3);
E[i] = 1;
}
if (isHist)
X.back() = X[nY-1] + dx;
}
return ws;
}

void storeWS(const std::string& name,WS_type ws)
{
AnalysisDataService::Instance().add(name,ws);
}

void removeWS(const std::string& name)
{
AnalysisDataService::Instance().remove(name);
}

WS_type getWS(const std::string& name)
{
return AnalysisDataService::Instance().retrieveWS<Mantid::DataObjects::Workspace2D>(name);
}

TWS_type getTWS(const std::string& name)
{
return AnalysisDataService::Instance().retrieveWS<Mantid::DataObjects::TableWorkspace>(name);
}

void addNoise(WS_type ws,double noise)
{
for(size_t iSpec=0;iSpec<ws->getNumberHistograms();iSpec++)
{
Mantid::MantidVec& Y = ws->dataY(iSpec);
Mantid::MantidVec& E = ws->dataE(iSpec);
for(size_t i=0;i<Y.size();i++)
{
Y[i] += noise*(-.5 + double(rand())/RAND_MAX);
E[i] += noise;
}
}
}

void interrupt()
{
int iii;
std::cerr<<"Enter a number:";
std::cin>>iii;
}
};

#endif /*CURVEFITTING_COMPOSITEFUNCTIONTEST_H_*/
80 changes: 0 additions & 80 deletions Code/Mantid/Framework/CurveFitting/test/ConvolutionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,6 @@ class ConvolutionTest : public CxxTest::TestSuite

}

void testFit()
{
Fit fit;
WS_type ws = mkWS(ConvolutionExp(),1,10,24,0.13);
}


void testForCategories()
{
Expand All @@ -358,80 +352,6 @@ class ConvolutionTest : public CxxTest::TestSuite
TS_ASSERT( categories[0] == "General" );
}

private:

template<class Funct>
WS_type mkWS(Funct f,int nSpec,double x0,double x1,double dx,bool isHist=false)
{
int nX = int((x1 - x0)/dx) + 1;
int nY = nX - (isHist?1:0);
if (nY <= 0)
throw std::invalid_argument("Cannot create an empty workspace");

Mantid::DataObjects::Workspace2D_sptr ws = boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>
(WorkspaceFactory::Instance().create("Workspace2D",nSpec,nX,nY));

double x;

for(int iSpec=0;iSpec<nSpec;iSpec++)
{
Mantid::MantidVec& X = ws->dataX(iSpec);
Mantid::MantidVec& Y = ws->dataY(iSpec);
Mantid::MantidVec& E = ws->dataE(iSpec);
for(int i=0;i<nY;i++)
{
x = x0 + dx*i;
X[i] = x;
Y[i] = f(x);
E[i] = 1;
}
if (isHist)
X.back() = X[nY-1] + dx;
}
return ws;
}

void storeWS(const std::string& name,WS_type ws)
{
AnalysisDataService::Instance().add(name,ws);
}

void removeWS(const std::string& name)
{
AnalysisDataService::Instance().remove(name);
}

WS_type getWS(const std::string& name)
{
return AnalysisDataService::Instance().retrieveWS<Mantid::DataObjects::Workspace2D>(name);
}

TWS_type getTWS(const std::string& name)
{
return AnalysisDataService::Instance().retrieveWS<Mantid::DataObjects::TableWorkspace>(name);
}

void addNoise(WS_type ws,double noise)
{
for(size_t iSpec=0;iSpec<ws->getNumberHistograms();iSpec++)
{
Mantid::MantidVec& Y = ws->dataY(iSpec);
Mantid::MantidVec& E = ws->dataE(iSpec);
for(size_t i=0;i<Y.size();i++)
{
Y[i] += noise*(-.5 + double(rand())/RAND_MAX);
E[i] += noise;
}
}
}

void press_return()
{
std::cerr<<"Press Return";
std::string str;
getline(std::cin,str);
}

};

#endif /*CONVOLUTIONTEST_H_*/

0 comments on commit b62568c

Please sign in to comment.