Skip to content

Commit

Permalink
Merge remote branch 'origin/feature/7789_fitpeak_to_findpeaks_new'
Browse files Browse the repository at this point in the history
  • Loading branch information
VickieLynch committed May 9, 2014
2 parents 4dfef94 + 088fe90 commit 14526ef
Show file tree
Hide file tree
Showing 21 changed files with 1,177 additions and 1,955 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Expand Up @@ -10,4 +10,4 @@
# Allowing gSoap generated files to be committed.
/Code/Mantid/Framework/ICat/inc/MantidICat/ICat3/GSoapGenerated/ICat3H.h hooks.MaxObjectKiB131025=2048
/Code/Mantid/Framework/ICat/src/ICat3/GSoapGenerated/ICat3C.cpp hooks.MaxObjectKiB131025=5120
/Code/Mantid/Framework/ICat/src/ICat4/GSoapGenerated/ICat4C.cpp hooks.MaxObjectKiB140423=2048
/Code/Mantid/Framework/ICat/src/ICat4/GSoapGenerated/ICat4C.cpp hooks.MaxObjectKiB131025=2048
2 changes: 1 addition & 1 deletion .githooks/pre-commit
Expand Up @@ -121,7 +121,7 @@ mode_looks_exe() {
*.exe) return 0 ;;
*.com) return 0 ;;
esac
git cat-file blob "$2" | head -1 | grep "^#\!\s*/" > /dev/null
git cat-file blob "$2" | head -1 | grep "^#\!/" > /dev/null
}
mode_not_exe () {
echo "The file '$file' has looks executable but does not have an executable mode."
Expand Down
15 changes: 12 additions & 3 deletions Code/Mantid/Framework/API/inc/MantidAPI/TableRow.h
Expand Up @@ -77,7 +77,10 @@ class MANTID_API_DLL TableRow
{
if (m_col >= m_columns.size())
{
throw std::range_error("Column index out of range.");
std::stringstream errss;
errss << "Column index " << m_col << " is out of range " << m_columns.size()
<< " of operator << ";
throw std::range_error(errss.str());
}
Column_sptr c = m_columns[m_col];
if (!c->isType<T>())
Expand Down Expand Up @@ -106,7 +109,10 @@ class MANTID_API_DLL TableRow
{
if (m_col >= m_columns.size())
{
throw std::range_error("Column index out of range.");
std::stringstream errss;
errss << "Column index " << m_col << " is out of range " << m_columns.size()
<< " of operator >> ";
throw std::range_error(errss.str());
}
Column_sptr c = m_columns[m_col];
if (!c->isType<T>())
Expand All @@ -130,7 +136,10 @@ class MANTID_API_DLL TableRow
{
if (col >= m_columns.size())
{
throw std::range_error("Column index out of range.");
std::stringstream errss;
errss << "Column index " << m_col << " is out of range " << m_columns.size()
<< " of method cell(). ";
throw std::range_error(errss.str());
}
m_col = col;
Column_sptr c = m_columns[m_col];
Expand Down
12 changes: 9 additions & 3 deletions Code/Mantid/Framework/API/src/ParamFunction.cpp
Expand Up @@ -119,7 +119,8 @@ void ParamFunction::setParameter(const std::string& name, const double& value, b
if (it == m_parameterNames.end())
{
std::ostringstream msg;
msg << "ParamFunction (set) parameter ("<<ucName<<") does not exist.";
msg << "ParamFunction tries to set value to non-exist parameter ("<<ucName<<") "
<< "of function " << this->name();
msg << "\nAllowed parameters: ";
for (size_t ist = 0; ist < m_parameterNames.size(); ++ist)
{
Expand All @@ -144,7 +145,9 @@ void ParamFunction::setParameterDescription(const std::string& name, const std::
if (it == m_parameterNames.end())
{
std::ostringstream msg;
msg << "ParamFunction parameter ("<<ucName<<") does not exist.";
msg << "ParamFunction tries to set description to non-exist parameter ("<<ucName<<"). ";
msg << "\nAllowed parameters: ";
for (size_t ist = 0; ist < m_parameterNames.size(); ++ist) msg << m_parameterNames[ist] << ", ";
throw std::invalid_argument(msg.str());
}
setParameterDescription(static_cast<int>(it - m_parameterNames.begin()),description);
Expand All @@ -165,7 +168,10 @@ double ParamFunction::getParameter(const std::string& name)const
if (it == m_parameterNames.end())
{
std::ostringstream msg;
msg << "ParamFunction parameter ("<<ucName<<") does not exist.";
msg << "ParamFunction tries to get value of non-existing parameter ("<<ucName<<") "
<< "to function " << this->name();
msg << "\nAllowed parameters: ";
for (size_t ist = 0; ist < m_parameterNames.size(); ++ist) msg << m_parameterNames[ist] << ", ";
throw std::invalid_argument(msg.str());
}

Expand Down
139 changes: 52 additions & 87 deletions Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/FindPeaks.h
Expand Up @@ -4,9 +4,11 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFunction.h"
// #include "MantidAPI/IFunction.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/IBackgroundFunction.h"
#include "MantidDataObjects/TableWorkspace.h"

namespace Mantid
Expand Down Expand Up @@ -53,31 +55,6 @@ namespace Algorithms
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/

class PeakFittingRecord
{
public:
/// Constructor
PeakFittingRecord();
/// Destructor
~PeakFittingRecord();
/// Set parameters
void set(double chi2, const std::map<std::string, double>& peakparammap, const std::map<std::string, double>& bkgdparammap);
/// Get chi-square
double getChiSquare() { return m_goodness; }
/// Get peak parameters
std::map<std::string, double> getPeakParameters() { return m_peakParameterMap;}
/// Get background parameters
std::map<std::string, double> getBackgroundParameters() { return m_bkgdParameterMap; }

private:
/// chi-square
double m_goodness;
/// parameter value
std::map<std::string, double> m_peakParameterMap;
/// parameter value
std::map<std::string, double> m_bkgdParameterMap;
};

class DLLExport FindPeaks : public API::Algorithm
{
public:
Expand Down Expand Up @@ -116,86 +93,70 @@ class DLLExport FindPeaks : public API::Algorithm
long long computePhi(const int& w) const;

/// Fit peak confined in a given window (x-min, x-max)
void fitPeakGivenWindow(const API::MatrixWorkspace_sptr &input, const int spectrum, const double centre, const double xmin, const double xmax);
void fitPeakInWindow(const API::MatrixWorkspace_sptr &input, const int spectrum, const double centre, const double xmin, const double xmax);

/// Fit peak by given/guessed FWHM
void fitPeakGuessWindow(const API::MatrixWorkspace_sptr &input, const int spectrum, const double center_guess, const int FWHM_guess);
void fitPeakGivenFWHM(const API::MatrixWorkspace_sptr &input, const int spectrum, const double center_guess, const int fitWidth,
const bool hasleftpeak, const double leftpeakcentre, const bool hasrightpeak, const double rightpeakcentre);

/// Fit peak
void fitPeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const int i_min, const int i_max, const int i_centre);
/// Fit peak: this is a basic peak fit function as a root function for all different type of user input
void fitSinglePeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const int i_min, const int i_max, const int i_centre);

void fitPeakHighBackground(const API::MatrixWorkspace_sptr &input, const size_t spectrum, int i_centre, int i_min, int i_max,
double &in_bg0, double &in_bg1, double &in_bg2, int i_peakmin, int i_peakmax);

void fitPeakOneStep(const API::MatrixWorkspace_sptr &input, const int spectrum, const int& i0, const int& i2, const int& i4,
const double& in_bg0, const double& in_bg1, const double& in_bg2);
const double& in_bg0, const double& in_bg1, const double& in_bg2);

void addInfoRow(const size_t spectrum, const std::vector<double> &params, const std::vector<double> &paramsRaw, const double mincost, bool error);
/// Add a new row in output TableWorkspace containing information of the fitted peak+background
void addInfoRow(const size_t spectrum, const API::IPeakFunction_const_sptr& peakfunction,
const API::IBackgroundFunction_sptr& bkgdfunction,
const bool isoutputraw,
const double mincost);

/// Add the fit record (failure) to output workspace
void addNonFitRecord(const size_t spectrum);

void updateFitResults(API::IAlgorithm_sptr fitAlg, std::vector<double> &bestEffparams, std::vector<double> &bestRawparams, double &mincost, const double expPeakPos, const double expPeakHeight);

API::IFunction_sptr createFunction(const double height, const double centre, const double sigma, const double a0, const double a1, const double a2, const bool withPeak = true);
int getBackgroundOrder();
/// Create a background function
API::IFunction_sptr createBackgroundFunction(const double a0, const double a1, const double a2);
/// Create peak and background functions
void createFunctions();

/// Fit background functions
bool fitBackground(const MantidVec& X, const MantidVec& Y, const MantidVec& E,
size_t ileft, size_t iright, size_t imin, size_t imax,
double &chi2);
/// Find peak background
bool findPeakBackground(const API::MatrixWorkspace_sptr& input, int spectrum, size_t i_min, size_t i_max,
std::vector<double>& vecBkgdParamValues, std::vector<double>& vecpeakrange);

/// Fit a single peak with background fixed
double fitPeakBackgroundFunction(API::IFunction_sptr peakbkgdfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex, double startx, double endx, std::string constraint, double &init_rwp);

/// Get function parameters from a function to a map
std::map<std::string, double> getFunctionParameters(API::IFunction_sptr func);

/// Set parameters to a peak function
void setParameters(API::IFunction_sptr peak, double height, double centre, double sigma, double centre_lowerbound, double centre_upperbound);

/// Fit peak with multiple iterations
PeakFittingRecord multiFitPeakBackground(API::MatrixWorkspace_sptr purepeakws, size_t purepeakindex,
API::MatrixWorkspace_sptr dataws, size_t datawsindex,
API::IPeakFunction_sptr peak,
double in_centre, double in_height, std::vector<double> in_fwhms,
double peakleftboundary, double peakrightboundary, double user_centre);

/// Set parameters values to a peak function
void setFunctionParameterValue(API::IFunction_sptr function, std::map<std::string, double> parvalues);

/// Set boundary/contraint on peak's centre
std::string makePeakCentreConstraint(API::IFunction_sptr peak, double peakleftboundary, double peakrightboundary, bool composite);

/// Use begin and end data points in a given region to estimate background
/// Estimate background of a given range
void estimateBackground(const MantidVec& X, const MantidVec& Y, const size_t i_min, const size_t i_max,
double& out_bg0, double& out_bg1, double& out_bg2);
std::vector<double>& vecbkgdparvalues);

/// Use FindPeakBackground to estimate peak background and peak range
bool findPeakBackground(const API::MatrixWorkspace_sptr& input, const int spectrum, const double xmin,
const double xmax, const std::string bkgdtype,
double& bg0, double& bg1,double& bg2, int& ipeakmin, int& ipeakmax);
/// Estimate peak range based on background peak parameter
void estimatePeakRange(const MantidVec& vecX, size_t i_centre,
size_t i_min, size_t i_max, const double& leftfwhm, const double& rightfwhm,
std::vector<double>& vecpeakrang);

/// Estimate peak parameters
std::string estimatePeakParameters(const MantidVec& vecX, const MantidVec& vecY,
size_t i_min, size_t i_max, double& centre, double& height, double& fwhm);
size_t i_min, size_t i_max, const std::vector<double> &vecbkgdparvalues,
size_t &iobscentre,
double& height, double& fwhm, double& leftfwhm,
double& rightfwhm);

/// Calulate a function with given data range, and its goodness of fit, Rwp.
double calculateFunctionRwp(API::IFunction_sptr function, API::MatrixWorkspace_sptr dataws,
size_t wsindex, double startx, double endx);
/// Generate a table workspace for output peak parameters
void generateOutputPeakParameterTable();

/// Compare 2 fit results and record the better one
void processFitResult(PeakFittingRecord& r1, PeakFittingRecord& r2, API::IPeakFunction_sptr peak, API::IFunction_sptr bkgdfunc, size_t spectrum,
size_t imin, size_t imax, double windowsize);
std::vector<double> getStartingPeakValues();
std::vector<double> getStartingBkgdValues();

/// Get best result from a set of fitting result
int getBestResult(std::vector<double> vecRwp);
/// Fit peak by calling 'FitPeak'
double callFitPeak(const API::MatrixWorkspace_sptr& dataws, int wsindex,
const API::IPeakFunction_sptr peakfunction,
const API::IBackgroundFunction_sptr backgroundfunction,
const std::vector<double>& vec_fitwindow,
const std::vector<double>& vec_peakrange, int minGuessedFWHM, int maxGuessFWHM,
int guessedFWHMStep);

void addFittedFunction(API::IFunction_sptr fitfunction, size_t ileft, size_t iright);

/// Check the GSL fit status message to determine whether the fit is successful or not
bool isFitSuccessful(std::string fitstatus);
std::vector<std::string> m_peakParameterNames;
std::vector<std::string> m_bkgdParameterNames;
size_t m_bkgdOrder;

/// The number of smoothing iterations. Set to 5, the optimum value according to Mariscotti.
static const int g_z = 5;
Expand All @@ -208,12 +169,12 @@ class DLLExport FindPeaks : public API::Algorithm
//Properties saved in the algo.
API::MatrixWorkspace_sptr m_dataWS; ///<workspace to check for peaks
int m_inputPeakFWHM; ///<holder for the requested peak FWHM
int index; ///<list of workspace indicies to check
int m_wsIndex; ///<list of workspace indicies to check
bool singleSpectrum; ///<flag for if only a single spectrum is present
bool m_highBackground; ///<flag for find relatively weak peak in high background
bool m_rawPeaksTable; ///<flag for whether the output is the raw peak parameters or effective (centre, width, height)
std::size_t m_numTableParams; //<Number of parameters in the output table workspace
bool m_searchPeakPos; ///<flag to search for peak in the window
// bool m_searchPeakPos; ///<flag to search for peak in the window
std::string m_peakFuncType; //< The name of the peak function to fit
std::string m_backgroundType; //< The type of background to fit

Expand All @@ -222,8 +183,8 @@ class DLLExport FindPeaks : public API::Algorithm
std::vector<double> m_vecFitWindows;

// Functions for reused
API::IFunction_sptr m_backgroundFunction;
API::IFunction_sptr m_peakAndBackgroundFunction;
API::IBackgroundFunction_sptr m_backgroundFunction;
API::IPeakFunction_sptr m_peakFunction;

int m_minGuessedPeakWidth;
int m_maxGuessedPeakWidth;
Expand All @@ -240,10 +201,14 @@ class DLLExport FindPeaks : public API::Algorithm
std::vector<size_t> m_peakRightIndexes;

std::string m_minimizer;
std::string m_costFunction;

/// Minimum peak height
double m_minHeight;

/// Start values
bool m_useObsCentre;

};

} // namespace Algorithms
Expand Down
17 changes: 12 additions & 5 deletions Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/FitPeak.h
Expand Up @@ -47,11 +47,13 @@ namespace Algorithms

void setFitPeakCriteria(bool usepeakpostol, double peakpostol);

std::string getDebugMessage();

/// Fit peak and background together
bool simpleFit();

/// Fit peak first considering high background
bool highBkgdFit();
void highBkgdFit();

/// Get fitting error
void getFitError(std::map<std::string, double>& peakerrormap, std::map<std::string, double>& bkgderrormap);
Expand Down Expand Up @@ -95,7 +97,7 @@ namespace Algorithms
bool hasSetupToFitPeak(std::string &errmsg);

/// Estimate the peak height from a set of data containing pure peaks
double estimatePeakHeight(API::IPeakFunction_sptr peakfunc, API::MatrixWorkspace_sptr dataws,
double estimatePeakHeight(API::IPeakFunction_const_sptr peakfunc, API::MatrixWorkspace_sptr dataws,
size_t wsindex, size_t ixmin, size_t ixmax);

/// Check a peak function whether it is valid comparing to user specified criteria
Expand Down Expand Up @@ -191,9 +193,6 @@ namespace Algorithms
/// Cost function
std::string m_costFunction;

/// Goodness of fit
double m_finalFitGoodness;

std::vector<double> m_vecFWHM;

/// Peak position tolerance
Expand All @@ -208,6 +207,11 @@ namespace Algorithms
/// Final goodness value (Rwp/Chi-square)
double m_finalGoodnessValue;

///
size_t m_numFitCalls;

/// String stream
std::stringstream m_sstream;
};


Expand Down Expand Up @@ -410,6 +414,9 @@ namespace Algorithms
// std::map<std::string, double> m_fitErrorPeakFunc;
// std::map<std::string, double> m_fitErrorBkgdFunc;

/// Option on output
bool m_lightWeightOutput;

};


Expand Down
Expand Up @@ -91,7 +91,7 @@ class DLLExport GetDetOffsetsMultiPeaks: public API::Algorithm
int fitSpectra(const int64_t wi, API::MatrixWorkspace_sptr inputW, const std::vector<double> &m_peakPositions,
const std::vector<double> &m_fitWindows, size_t &nparams, double &minD, double &maxD,
std::vector<double>&peakPosToFit, std::vector<double> &peakPosFitted, std::vector<double> &chisq,
int &i_highestpeak);
std::vector<double> &peakHeights, int &i_highestpeak);

/// Add peak fitting and offset calculation information to information table workspaces per spectrum
void addInfoToReportWS(int wi, FitPeakOffsetResult offsetresult, const std::vector<double> &tofitpeakpositions,
Expand All @@ -117,7 +117,7 @@ class DLLExport GetDetOffsetsMultiPeaks: public API::Algorithm
void fitPeaksOffset(const size_t inpnparams, const double minD, const double maxD,
const std::vector<double>& vec_peakPosRef,
const std::vector<double>& vec_peakPosFitted,
const std::vector<double>& vec_fitChi2,
const std::vector<double>& vec_peakHeights,
FitPeakOffsetResult& fitresult);

/// Make a summary on all fit
Expand All @@ -135,7 +135,7 @@ class DLLExport GetDetOffsetsMultiPeaks: public API::Algorithm
double m_maxChiSq;
double m_minPeakHeight;

double maxOffset;
double m_maxOffset;

std::vector<double> m_peakPositions;
std::vector<double> m_fitWindows;
Expand Down

0 comments on commit 14526ef

Please sign in to comment.