Skip to content

Commit

Permalink
Re #4158. Added generic Fit algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 2, 2012
1 parent a02b074 commit 5f54043
Show file tree
Hide file tree
Showing 44 changed files with 1,180 additions and 819 deletions.
10 changes: 5 additions & 5 deletions Code/Mantid/Framework/Algorithms/src/CalMuonDeadTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,17 @@ void CalMuonDeadTime::exec()
const double in_bg1 = 0.0;

API::IAlgorithm_sptr fit;
fit = createSubAlgorithm("FitMW", -1, -1, true);

const int wsindex = static_cast<int>(i);
fit->setProperty("InputWorkspace", wsFitAgainst);
fit->setProperty("WorkspaceIndex", wsindex);
fit = createSubAlgorithm("Fit", -1, -1, true);

std::stringstream ss;
ss << "name=LinearBackground,A0=" << in_bg0 << ",A1=" << in_bg1;
std::string function = ss.str();

fit->setPropertyValue("Function", function);
const int wsindex = static_cast<int>(i);
fit->setProperty("InputWorkspace", wsFitAgainst);
fit->setProperty("WorkspaceIndex", wsindex);

fit->executeAsSubAlg();

std::string fitStatus = fit->getProperty("OutputStatus");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,21 @@ namespace Algorithms
try
{
//set the subalgorithm no to log as this will be run once per spectra
fit_alg = createSubAlgorithm("FitMW",-1,-1,false);
fit_alg = createSubAlgorithm("Fit",-1,-1,false);
} catch (Exception::NotFoundError&)
{
g_log.error("Can't locate Fit algorithm");
throw ;
}
std::ostringstream fun_str;
fun_str << "name=Gaussian,Height="<<peakHeight<<",Sigma=0.01,PeakCentre="<<peakLoc;
fit_alg->setProperty("Function",fun_str.str());
fit_alg->setProperty("InputWorkspace",outputW);
fit_alg->setProperty("WorkspaceIndex",0);
fit_alg->setProperty("StartX",outputW->readX(0)[0]);
fit_alg->setProperty("EndX",outputW->readX(0)[outputW->blocksize()]);
fit_alg->setProperty("MaxIterations",200);
fit_alg->setProperty("Output","fit");
std::ostringstream fun_str;
fun_str << "name=Gaussian,Height="<<peakHeight<<",Sigma=0.01,PeakCentre="<<peakLoc;
fit_alg->setProperty("Function",fun_str.str());
fit_alg->executeAsSubAlg();

if (debug) std::cout << tim << " to Fit" << std::endl;
Expand Down
17 changes: 4 additions & 13 deletions Code/Mantid/Framework/Algorithms/src/FindPeaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,15 +925,12 @@ void FindPeaks::fitPeakOneStep(const API::MatrixWorkspace_sptr &input, const int
try
{
// Fitting the candidate peaks to a Gaussian
fit = createSubAlgorithm("FitMW", -1, -1, true);
fit = createSubAlgorithm("Fit", -1, -1, true);
} catch (Exception::NotFoundError &)
{
g_log.error("The StripPeaks algorithm requires the CurveFitting library");
throw;
}
fit->setProperty("InputWorkspace", input);
fit->setProperty("WorkspaceIndex", spectrum);
fit->setProperty("MaxIterations", 50);

// b) Guess sigma
const double in_sigma = (i0 + width < X.size()) ? X[i0 + width] - X[i0] : 0.;
Expand Down Expand Up @@ -1020,15 +1017,12 @@ void FindPeaks::fitPeakHighBackground(const API::MatrixWorkspace_sptr &input, co
try
{
// Fitting the candidate peaks to a Gaussian
fit = createSubAlgorithm("FitMW", -1, -1, true);
fit = createSubAlgorithm("Fit", -1, -1, true);
} catch (Exception::NotFoundError &)
{
g_log.error("The StripPeaks algorithm requires the CurveFitting library");
throw;
}
fit->setProperty("InputWorkspace", bkgdWS);
fit->setProperty("WorkspaceIndex", 0);
fit->setProperty("MaxIterations", 50);

// c) Set initial fitting parameters
IFitFunction_sptr backgroundFunction = this->createFunction(0., 0., 0., in_bg0, in_bg1, in_bg2, false);
Expand Down Expand Up @@ -1089,7 +1083,7 @@ void FindPeaks::fitPeakHighBackground(const API::MatrixWorkspace_sptr &input, co
try
{
// Fitting the candidate peaks to a Gaussian
gfit = createSubAlgorithm("FitMW", -1, -1, true);
gfit = createSubAlgorithm("Fit", -1, -1, true);
} catch (Exception::NotFoundError &)
{
g_log.error("The FindPeaks algorithm requires the CurveFitting library");
Expand Down Expand Up @@ -1136,15 +1130,12 @@ void FindPeaks::fitPeakHighBackground(const API::MatrixWorkspace_sptr &input, co
try
{
// Fitting the candidate peaks to a Gaussian
lastfit = createSubAlgorithm("FitMW", -1, -1, true);
lastfit = createSubAlgorithm("Fit", -1, -1, true);
} catch (Exception::NotFoundError &)
{
g_log.error("The StripPeaks algorithm requires the CurveFitting library");
throw;
}
lastfit->setProperty("InputWorkspace", input);
lastfit->setProperty("WorkspaceIndex", spectrum);
lastfit->setProperty("MaxIterations", 50);

// c) Set initial fitting parameters
IFitFunction_sptr peakAndBackgroundFunction = this->createFunction(bestparams[2], bestparams[0], bestparams[1],
Expand Down
5 changes: 4 additions & 1 deletion Code/Mantid/Framework/Algorithms/src/GetDetectorOffsets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,15 @@ namespace Mantid
try
{
//set the subalgorithm no to log as this will be run once per spectra
fit_alg = createSubAlgorithm("FitMW",-1,-1,false);
fit_alg = createSubAlgorithm("Fit",-1,-1,false);
} catch (Exception::NotFoundError&)
{
g_log.error("Can't locate Fit algorithm");
throw ;
}
std::string fun_str = createFunctionString(peakHeight, peakLoc);
fit_alg->setPropertyValue("Function",fun_str);

fit_alg->setProperty("InputWorkspace",inputW);
fit_alg->setProperty<int>("WorkspaceIndex",static_cast<int>(s)); // TODO what is the right thing to do here?
fit_alg->setProperty("StartX",Xmin);
Expand Down
5 changes: 1 addition & 4 deletions Code/Mantid/Framework/Algorithms/src/MuonRemoveExpDecay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ double MuonRemoveExpDecay::calNormalisationConst(API::MatrixWorkspace_sptr ws, i
double retVal = 1.0;

API::IAlgorithm_sptr fit;
fit = createSubAlgorithm("FitMW", -1, -1, true);

fit->setProperty("InputWorkspace", ws);
fit->setProperty("WorkspaceIndex", wsIndex);
fit = createSubAlgorithm("Fit", -1, -1, true);

std::stringstream ss;
ss << "name=LinearBackground,A0=" << ws->readY(wsIndex)[0] << ",A1=" << 0.0 << ",ties=(A1=0.0)";
Expand Down
14 changes: 7 additions & 7 deletions Code/Mantid/Framework/Crystal/src/PeakIntegration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,12 @@ namespace Mantid
IAlgorithm_sptr fit_alg;
try
{
fit_alg = createSubAlgorithm("FitMW", -1, -1, false);
fit_alg = createSubAlgorithm("Fit", -1, -1, false);
} catch (Exception::NotFoundError&)
{
g_log.error("Can't locate Fit algorithm");
throw ;
}
fit_alg->setProperty("InputWorkspace", outputW);
fit_alg->setProperty("WorkspaceIndex", i);
fit_alg->setProperty("StartX", X[TOFmin]);
fit_alg->setProperty("EndX", X[TOFmax]);
fit_alg->setProperty("MaxIterations", 5000);
//fit_alg->setProperty("Output", "fit");
//Initialize Ikeda-Carpender function variables
double Alpha0 = 1.6;
double Alpha1 = 1.5;
Expand All @@ -301,6 +295,12 @@ namespace Mantid
tie_str << "Alpha0="<<Alpha0<<",Alpha1="<<Alpha1<<",Beta0="<<Beta0<<",Kappa="<<Kappa;
fit_alg->setProperty("Ties", tie_str.str());
}
fit_alg->setProperty("InputWorkspace", outputW);
fit_alg->setProperty("WorkspaceIndex", i);
fit_alg->setProperty("StartX", X[TOFmin]);
fit_alg->setProperty("EndX", X[TOFmax]);
fit_alg->setProperty("MaxIterations", 5000);
//fit_alg->setProperty("Output", "fit");
fit_alg->executeAsSubAlg();
//MatrixWorkspace_sptr ws = fit_alg->getProperty("OutputWorkspace");

Expand Down
12 changes: 4 additions & 8 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ set ( SRC_FILES
src/ExpDecayOsc.cpp
src/FlatBackground.cpp
src/FRConjugateGradientMinimizer.cpp
# src/Fit.cpp
src/Fit.cpp
src/Fit1D.cpp
src/FitMD.cpp
src/FitMW.cpp
Expand All @@ -29,9 +29,8 @@ set ( SRC_FILES
src/MuonFInteraction.cpp
src/Gaussian1D.cpp
src/GaussianLinearBG1D.cpp
# src/GenericFit.cpp
src/GSLFunctions.cpp
src/IFit.cpp
src/IDomainCreator.cpp
src/IFuncMinimizer.cpp
src/IkedaCarpenterPV.cpp
src/LevenbergMarquardtMinimizer.cpp
Expand All @@ -41,7 +40,6 @@ set ( SRC_FILES
src/LogNormal.cpp
src/Lorentzian.cpp
src/Lorentzian1D.cpp
# src/MultiBG.cpp
src/PRConjugateGradientMinimizer.cpp
src/PlotPeakByLogValue.cpp
src/ProductFunction.cpp
Expand Down Expand Up @@ -84,7 +82,7 @@ set ( INC_FILES
inc/MantidCurveFitting/ExpDecayOsc.h
inc/MantidCurveFitting/FlatBackground.h
inc/MantidCurveFitting/FRConjugateGradientMinimizer.h
# inc/MantidCurveFitting/Fit.h
inc/MantidCurveFitting/Fit.h
inc/MantidCurveFitting/Fit1D.h
inc/MantidCurveFitting/FitMD.h
inc/MantidCurveFitting/FitMW.h
Expand All @@ -100,7 +98,7 @@ set ( INC_FILES
inc/MantidCurveFitting/Gaussian1D.h
inc/MantidCurveFitting/GaussianLinearBG1D.h
inc/MantidCurveFitting/GenericFit.h
inc/MantidCurveFitting/IFit.h
inc/MantidCurveFitting/IDomainCreator.h
inc/MantidCurveFitting/IFuncMinimizer.h
inc/MantidCurveFitting/IkedaCarpenterPV.h
inc/MantidCurveFitting/LevenbergMarquardtMinimizer.h
Expand All @@ -111,7 +109,6 @@ set ( INC_FILES
inc/MantidCurveFitting/Lorentzian.h
inc/MantidCurveFitting/Lorentzian1D.h
inc/MantidCurveFitting/MuonFInteraction.h
# inc/MantidCurveFitting/MultiBG.h
inc/MantidCurveFitting/PRConjugateGradientMinimizer.h
inc/MantidCurveFitting/PlotPeakByLogValue.h
inc/MantidCurveFitting/ProductFunction.h
Expand Down Expand Up @@ -169,7 +166,6 @@ set ( TEST_FILES
test/Lorentzian1DTest.h
test/LorentzianTest.h
test/MultiDomainFunctionTest.h
# test/MultiBGTest.h
test/PlotPeakByLogValueTest.h
test/PRConjugateGradientTest.h
test/ProductFunctionTest.h
Expand Down
90 changes: 90 additions & 0 deletions Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/Fit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef MANTID_CURVEFITTING_FIT_H_
#define MANTID_CURVEFITTING_FIT_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/Workspace.h"
#include "MantidCurveFitting/IDomainCreator.h"

#include <boost/scoped_ptr.hpp>

namespace Mantid
{

namespace API
{
class FunctionDomain;
class FunctionValues;
class Workspace;
}

namespace CurveFitting
{
/**
New algorithm for fitting functions. The name is temporary.
@author Roman Tolchenov, Tessella plc
@date 06/12/2011
Copyright &copy; 2007-8 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
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://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport Fit : public API::Algorithm
{
public:
/// Default constructor
Fit() : API::Algorithm(),m_isFunctionSet(false),m_isWorkspaceSet(false) {};
/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const { return "Fit";}
/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return (1);}
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "Optimization";}

protected:
/// Sets documentation strings for this algorithm
virtual void initDocs();
// Overridden Algorithm methods
void init();
void exec();
/// Override this method to perform a custom action right after a property was set.
/// The argument is the property name. Default - do nothing.
virtual void afterPropertySet(const std::string& propName);

/// Pointer to the fitting function
API::IFunction_sptr m_function;
///// Pointer
//API::Workspace_const_sptr m_workspace;
/// Pointer to a domain creator
boost::scoped_ptr<IDomainCreator> m_domainCreator;
friend class IDomainCreator;
bool m_isFunctionSet;
bool m_isWorkspaceSet;

};


} // namespace CurveFitting
} // namespace Mantid

#endif /*MANTID_CURVEFITTING_FIT_H_*/
25 changes: 8 additions & 17 deletions Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/FitMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/IFit.h"
#include "MantidCurveFitting/IDomainCreator.h"

namespace Mantid
{
Expand Down Expand Up @@ -45,28 +45,19 @@ namespace Mantid
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport FitMD : public IFit
class DLLExport FitMD : public IDomainCreator
{
public:
/// Default constructor
FitMD() : IFit() {};
/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const { return "FitMD";}
/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return (1);}
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "Optimization";}

protected:
/// Sets documentation strings for this algorithm
virtual void initDocs();

/// declare properties that specify the dataset within the workspace to fit to.
virtual void declareDatasetProperties(){}
/// Create a domain from the input workspace
virtual void createDomain(boost::shared_ptr<API::FunctionDomain>&, boost::shared_ptr<API::FunctionValues>&);
protected:
/// Constructor
FitMD(API::Algorithm* fit):IDomainCreator(fit){}
/// A friend that can create instances of this class
friend class Fit;

/// The input MareixWorkspace
/// The input IMDWorkspace
boost::shared_ptr<API::IMDWorkspace> m_IMDWorkspace;
};

Expand Down

0 comments on commit 5f54043

Please sign in to comment.