Skip to content

Commit

Permalink
Tried to resolve conflict. Refs #7001.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Jun 18, 2013
2 parents e0d1efb + 64566bf commit 9bfce63
Show file tree
Hide file tree
Showing 10 changed files with 3,471 additions and 1,695 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IPeakFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class MANTID_API_DLL IPeakFunction : public IFunctionWithLocation
static int s_peakRadius;
};

typedef boost::shared_ptr<IPeakFunction> IPeakFunction_sptr;
typedef boost::shared_ptr<const IPeakFunction> IPeakFunction_const_sptr;

} // namespace API
} // namespace Mantid

Expand Down
153 changes: 133 additions & 20 deletions Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/FindPeaks.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidDataObjects/TableWorkspace.h"

namespace Mantid
Expand Down Expand Up @@ -51,6 +52,32 @@ namespace Algorithms
File change history is stored at: <https://github.com/mantidproject/mantid>
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 All @@ -71,43 +98,117 @@ class DLLExport FindPeaks : public API::Algorithm
void init();
void exec();

/// Process algorithm's properties
void processAlgorithmProperties();

/// Find peaks by searching peak position using Mariscotti algorithm
void findPeaksUsingMariscotti();

/// Find peaks according to given peak positions
void findPeaksGivenStartingPoints(const std::vector<double> &peakcentres, const std::vector<double> &fitwindows);

/// Methods searving for findPeaksUsingMariscotti()
API::MatrixWorkspace_sptr calculateSecondDifference(const API::MatrixWorkspace_const_sptr &input);
void smoothData(API::MatrixWorkspace_sptr &WS, const int &w);
void calculateStandardDeviation(const API::MatrixWorkspace_const_sptr &input, const API::MatrixWorkspace_sptr &smoothed, const int &w);
long long computePhi(const int& w) const;

int getCentreIndex(const MantidVec &X, double centre);
void fitPeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const int i0, const int i2, const int i4);
/// Fit peak confined in a given window (x-min, x-max)
void fitPeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const double centre, const double xmin, const double xmax);

/// Fit peak by given/guessed FWHM
void fitPeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const double center_guess, const int FWHM_guess);
void fitPeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const double centre, const double left, const double right);
void findPeaksUsingMariscotti();
void findPeaksGivenStartingPoints(const std::vector<double> &peakCentres, const std::vector<double> &fitWindows);

void fitPeakHighBackground(const API::MatrixWorkspace_sptr &input, const int spectrum, const int& i0, const int& i2, const int& i4,
const unsigned int& i_min, const unsigned int& i_max,
const double& in_bg0, const double& in_bg1, const double& in_bg2);
/// Fit peak
void fitPeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const int i_min, const int i_max, const int i_centre, bool changeflag);


int getVectorIndex(const MantidVec &vecX, double x);

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

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);

void addRow(const int spectrum, const std::vector<double> &params, const std::vector<double> &paramsRaw, const double mincost, bool error);
void addInfoRow(const size_t spectrum, const std::vector<double> &params, const std::vector<double> &paramsRaw, const double mincost, bool error);

/// 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);

std::string createTies(const double height, const double centre, const double sigma, const double a0, const double a1, const double a2, const bool withPeak);
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 backgroundOrder();
int getBackgroundOrder();
/// Create a background function
API::IFunction_sptr createBackgroundFunction(const double a0, const double a1, const double a2);

/// 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);

/// 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);

void estimateLinearBackground(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, size_t specdb);

void estimateFlatBackground(const MantidVec& Y, const size_t i_min, const size_t i_max,
double& out_bg0, double& out_bg1, double& out_bg2);

bool estimatePeakParameters(const MantidVec& vecX, const MantidVec& vecY,
size_t i_min, size_t i_max, double& centre, double& height, double& fwhm, std::string &error);

/// 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);

///
API::MatrixWorkspace_sptr createOutputDataWorkspace();

/// 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);

/// Get best result from a set of fitting result
int getBestResult(std::vector<double> vecRwp);

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);

/// The number of smoothing iterations. Set to 5, the optimum value according to Mariscotti.
static const int g_z = 5;

/// Storage of the peak data
API::ITableWorkspace_sptr m_peaks;
API::ITableWorkspace_sptr m_outPeakTableWS;
/// Progress reporting
API::Progress* m_progress;

//Properties saved in the algo.
API::MatrixWorkspace_sptr inputWS; ///<workspace to check for peaks
int fwhm; ///<holder for the requested peak FWHM
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
bool singleSpectrum; ///<flag for if only a single spectrum is present
bool m_highBackground; ///<flag for find relatively weak peak in high background
Expand All @@ -117,20 +218,32 @@ class DLLExport FindPeaks : public API::Algorithm
std::string m_peakFuncType; //< The name of the peak function to fit
std::string m_backgroundType; //< The type of background to fit

// Peaks positions
std::vector<double> m_vecPeakCentre;
std::vector<double> m_vecFitWindows;

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

unsigned int minGuessedPeakWidth;
unsigned int maxGuessedPeakWidth;
unsigned int stepGuessedPeakWidth;
int m_minGuessedPeakWidth;
int m_maxGuessedPeakWidth;
int m_stepGuessedPeakWidth;

bool m_usePeakPositionTolerance;
double peakPositionTolerance;
double m_peakPositionTolerance;

bool m_usePeakHeightTolerance;
double peakHeightTolerance;
double m_peakHeightTolerance;

std::vector<API::IFunction_sptr> m_fitFunctions;
std::vector<size_t> m_peakLeftIndexes;
std::vector<size_t> m_peakRightIndexes;

std::string m_minimizer;

/// Minimum peak height
double m_minHeight;

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace Algorithms
// Implement abstract Algorithm methods
void exec();

API::MatrixWorkspace_sptr createOutputWorkspace(std::set<specid_t> spectra, std::vector<double> mBinParameters);
API::MatrixWorkspace_sptr createDataWorkspace(std::set<specid_t> spectra, std::vector<double> binparameters);

API::IFunction_sptr createFunction(const std::string &peakFuncType, const std::vector<std::string> &colNames,
const bool isRaw, const bool withBackground,
Expand All @@ -74,6 +74,9 @@ namespace Algorithms

double getTableValue(DataObjects::TableWorkspace_const_sptr tableWS, std::string colname, size_t index);

/// Get the IPeakFunction part in the input function
API::IPeakFunction_sptr getPeakFunction(API::IFunction_sptr infunction);

std::map<specid_t, specid_t> mSpectrumMap;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "MantidAPI/Algorithm.h"
#include "MantidKernel/System.h"
#include "MantidDataObjects/OffsetsWorkspace.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include <gsl/gsl_blas.h>
#include <gsl/gsl_multifit_nlin.h>
Expand Down Expand Up @@ -67,6 +68,9 @@ class DLLExport GetDetOffsetsMultiPeaks: public API::Algorithm
std::string m_backType;
std::string m_peakType;
double m_maxChiSq;

DataObjects::TableWorkspace_sptr m_infoTableWS;
DataObjects::TableWorkspace_sptr m_peakOffsetTableWS;
};

} // namespace Algorithm
Expand Down

0 comments on commit 9bfce63

Please sign in to comment.