Skip to content

Commit

Permalink
Make guess curves in fitting interfaces use workspaces
Browse files Browse the repository at this point in the history
Refs #11036
  • Loading branch information
DanNixon committed Feb 12, 2015
1 parent 27f414f commit 0d34503
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
78 changes: 42 additions & 36 deletions Code/Mantid/MantidQt/CustomInterfaces/src/Indirect/ConvFit.cpp
Expand Up @@ -162,7 +162,7 @@ namespace IDA
bool useTies = m_uiForm.ckTieCentres->isChecked();
QString ties = (useTies ? "True" : "False");

Mantid::API::CompositeFunction_sptr func = createFunction(useTies);
CompositeFunction_sptr func = createFunction(useTies);
std::string function = std::string(func->asString());
QString stX = m_properties["StartX"]->valueText();
QString enX = m_properties["EndX"]->valueText();
Expand Down Expand Up @@ -213,8 +213,6 @@ namespace IDA
*/
bool ConvFit::validate()
{
using Mantid::API::AnalysisDataService;

UserInputValidator uiv;

uiv.checkDataSelectorIsValid("Sample", m_uiForm.dsSampleInput);
Expand Down Expand Up @@ -334,18 +332,18 @@ namespace IDA
*
* @returns the composite fitting function.
*/
Mantid::API::CompositeFunction_sptr ConvFit::createFunction(bool tieCentres)
CompositeFunction_sptr ConvFit::createFunction(bool tieCentres)
{
auto conv = boost::dynamic_pointer_cast<Mantid::API::CompositeFunction>(Mantid::API::FunctionFactory::Instance().createFunction("Convolution"));
Mantid::API::CompositeFunction_sptr comp( new Mantid::API::CompositeFunction );
auto conv = boost::dynamic_pointer_cast<CompositeFunction>(FunctionFactory::Instance().createFunction("Convolution"));
CompositeFunction_sptr comp( new CompositeFunction );

Mantid::API::IFunction_sptr func;
IFunction_sptr func;
size_t index = 0;

// -------------------------------------
// --- Composite / Linear Background ---
// -------------------------------------
func = Mantid::API::FunctionFactory::Instance().createFunction("LinearBackground");
func = FunctionFactory::Instance().createFunction("LinearBackground");
comp->addFunction(func);

const int bgType = m_uiForm.cbBackground->currentIndex(); // 0 = Fixed Flat, 1 = Fit Flat, 2 = Fit all
Expand Down Expand Up @@ -375,35 +373,35 @@ namespace IDA
// --------------------------------------------
// --- Composite / Convolution / Resolution ---
// --------------------------------------------
func = Mantid::API::FunctionFactory::Instance().createFunction("Resolution");
func = FunctionFactory::Instance().createFunction("Resolution");
conv->addFunction(func);

//add resolution file
if (m_uiForm.dsResInput->isFileSelectorVisible())
{
std::string resfilename = m_uiForm.dsResInput->getFullFilePath().toStdString();
Mantid::API::IFunction::Attribute attr(resfilename);
IFunction::Attribute attr(resfilename);
func->setAttribute("FileName", attr);
}
else
{
std::string resWorkspace = m_uiForm.dsResInput->getCurrentDataName().toStdString();
Mantid::API::IFunction::Attribute attr(resWorkspace);
IFunction::Attribute attr(resWorkspace);
func->setAttribute("Workspace", attr);
}

// --------------------------------------------------------
// --- Composite / Convolution / Model / Delta Function ---
// --------------------------------------------------------
Mantid::API::CompositeFunction_sptr model( new Mantid::API::CompositeFunction );
CompositeFunction_sptr model( new CompositeFunction );

bool useDeltaFunc = m_blnManager->value(m_properties["UseDeltaFunc"]);

size_t subIndex = 0;

if ( useDeltaFunc )
{
func = Mantid::API::FunctionFactory::Instance().createFunction("DeltaFunction");
func = FunctionFactory::Instance().createFunction("DeltaFunction");
index = model->addFunction(func);
std::string parName = createParName(index);
populateFunction(func, model, m_properties["DeltaFunction"], parName, false);
Expand All @@ -414,7 +412,7 @@ namespace IDA
// ------------------------------------------------------------

//create temperature correction function to multiply with the lorentzians
Mantid::API::IFunction_sptr tempFunc;
IFunction_sptr tempFunc;
QString temperature = m_uiForm.leTempCorrection->text();
bool useTempCorrection = (!temperature.isEmpty() && m_uiForm.ckTempCorrection->isChecked());

Expand All @@ -431,14 +429,14 @@ namespace IDA
{
//if temperature not included then product is lorentzian * 1
//create product function for temp * lorentzian
auto product = boost::dynamic_pointer_cast<Mantid::API::CompositeFunction>(Mantid::API::FunctionFactory::Instance().createFunction("ProductFunction"));
auto product = boost::dynamic_pointer_cast<CompositeFunction>(FunctionFactory::Instance().createFunction("ProductFunction"));

if(useTempCorrection)
{
createTemperatureCorrection(product);
}

func = Mantid::API::FunctionFactory::Instance().createFunction("Lorentzian");
func = FunctionFactory::Instance().createFunction("Lorentzian");
subIndex = product->addFunction(func);
index = model->addFunction(product);
prefix1 = createParName(index, subIndex);
Expand All @@ -451,14 +449,14 @@ namespace IDA
{
//if temperature not included then product is lorentzian * 1
//create product function for temp * lorentzian
auto product = boost::dynamic_pointer_cast<Mantid::API::CompositeFunction>(Mantid::API::FunctionFactory::Instance().createFunction("ProductFunction"));
auto product = boost::dynamic_pointer_cast<CompositeFunction>(FunctionFactory::Instance().createFunction("ProductFunction"));

if(useTempCorrection)
{
createTemperatureCorrection(product);
}

func = Mantid::API::FunctionFactory::Instance().createFunction("Lorentzian");
func = FunctionFactory::Instance().createFunction("Lorentzian");
subIndex = product->addFunction(func);
index = model->addFunction(product);
prefix2 = createParName(index, subIndex);
Expand All @@ -481,18 +479,18 @@ namespace IDA
return comp;
}

void ConvFit::createTemperatureCorrection(Mantid::API::CompositeFunction_sptr product)
void ConvFit::createTemperatureCorrection(CompositeFunction_sptr product)
{
//create temperature correction function to multiply with the lorentzians
Mantid::API::IFunction_sptr tempFunc;
IFunction_sptr tempFunc;
QString temperature = m_uiForm.leTempCorrection->text();

//create user function for the exponential correction
// (x*temp) / 1-exp(-(x*temp))
tempFunc = Mantid::API::FunctionFactory::Instance().createFunction("UserFunction");
tempFunc = FunctionFactory::Instance().createFunction("UserFunction");
//11.606 is the conversion factor from meV to K
std::string formula = "((x*11.606)/Temp) / (1 - exp(-((x*11.606)/Temp)))";
Mantid::API::IFunction::Attribute att(formula);
IFunction::Attribute att(formula);
tempFunc->setAttribute("Formula", att);
tempFunc->setParameter("Temp", temperature.toDouble());

Expand Down Expand Up @@ -563,7 +561,7 @@ namespace IDA
return lorentzGroup;
}

void ConvFit::populateFunction(Mantid::API::IFunction_sptr func, Mantid::API::IFunction_sptr comp, QtProperty* group, const std::string & pref, bool tie)
void ConvFit::populateFunction(IFunction_sptr func, IFunction_sptr comp, QtProperty* group, const std::string & pref, bool tie)
{
// Get subproperties of group and apply them as parameters on the function object
QList<QtProperty*> props = group->subProperties();
Expand Down Expand Up @@ -723,7 +721,7 @@ namespace IDA
}

bool tieCentres = (m_uiForm.cbFitType->currentIndex() > 1);
Mantid::API::CompositeFunction_sptr function = createFunction(tieCentres);
CompositeFunction_sptr function = createFunction(tieCentres);

if ( m_cfInputWS == NULL )
{
Expand All @@ -750,8 +748,8 @@ namespace IDA
}
}

Mantid::API::FunctionDomain1DVector domain(inputXData);
Mantid::API::FunctionValues outputData(domain);
FunctionDomain1DVector domain(inputXData);
FunctionValues outputData(domain);
function->function(domain, outputData);

QVector<double> dataX, dataY;
Expand All @@ -762,13 +760,21 @@ namespace IDA
dataY.append(outputData.getCalculated(i));
}

removeCurve("CFCalcCurve");
m_curves["CFCalcCurve"] = new QwtPlotCurve();
m_curves["CFCalcCurve"]->setData(dataX, dataY);
QPen fitPen(Qt::red, Qt::SolidLine);
m_curves["CFCalcCurve"]->setPen(fitPen);
m_curves["CFCalcCurve"]->attach(m_plots["ConvFitPlot"]);
m_plots["ConvFitPlot"]->replot();
IAlgorithm_sptr createWsAlg = AlgorithmManager::Instance().create("CreateWorkspace");
createWsAlg->initialize();
createWsAlg->setChild(true);
createWsAlg->setLogging(false);
createWsAlg->setProperty("OutputWorkspace", "__GuessAnon");
createWsAlg->setProperty("NSpec", 1);
createWsAlg->setProperty("DataX", dataX.toStdVector());
createWsAlg->setProperty("DataY", dataY.toStdVector());
createWsAlg->execute();
MatrixWorkspace_sptr guessWs = createWsAlg->getProperty("OutputWorkspace");

plotMiniPlot(guessWs, 0, "ConvFitPlot", "guess");
QPen p(Qt::green, Qt::SolidLine);
m_curves["guess"]->setPen(p);
replot("ConvFitPlot");
}

void ConvFit::singleFit()
Expand All @@ -786,7 +792,7 @@ namespace IDA

m_uiForm.ckPlotGuess->setChecked(false);

Mantid::API::CompositeFunction_sptr function = createFunction(m_uiForm.ckTieCentres->isChecked());
CompositeFunction_sptr function = createFunction(m_uiForm.ckTieCentres->isChecked());

// get output name
QString fitType = fitTypeString();
Expand All @@ -801,7 +807,7 @@ namespace IDA
outputNm += QString("conv_") + fitType + bgType + m_uiForm.spPlotSpectrum->text();
std::string output = outputNm.toStdString();

Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("Fit");
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("Fit");
alg->initialize();
alg->setPropertyValue("Function", function->asString());
alg->setPropertyValue("InputWorkspace", m_cfInputWSName.toStdString());
Expand All @@ -826,7 +832,7 @@ namespace IDA
m_curves["CFCalcCurve"]->setPen(fitPen);
replot("ConvFitPlot");

Mantid::API::IFunction_sptr outputFunc = alg->getProperty("Function");
IFunction_sptr outputFunc = alg->getProperty("Function");

// Get params.
QMap<QString,double> parameters;
Expand Down
22 changes: 14 additions & 8 deletions Code/Mantid/MantidQt/CustomInterfaces/src/Indirect/FuryFit.cpp
Expand Up @@ -690,14 +690,20 @@ namespace IDA
dataX.append(inputXData[i]);
dataY.append(outputData.getCalculated(i));
}

// Create the curve
removeCurve("FF_FitCurve");
m_curves["FF_FitCurve"] = new QwtPlotCurve();
m_curves["FF_FitCurve"]->setData(dataX, dataY);
m_curves["FF_FitCurve"]->attach(m_plots["FuryFitPlot"]);
QPen fitPen(Qt::red, Qt::SolidLine);
m_curves["FF_FitCurve"]->setPen(fitPen);
IAlgorithm_sptr createWsAlg = AlgorithmManager::Instance().create("CreateWorkspace");
createWsAlg->initialize();
createWsAlg->setChild(true);
createWsAlg->setLogging(false);
createWsAlg->setProperty("OutputWorkspace", "__GuessAnon");
createWsAlg->setProperty("NSpec", 1);
createWsAlg->setProperty("DataX", dataX.toStdVector());
createWsAlg->setProperty("DataY", dataY.toStdVector());
createWsAlg->execute();
MatrixWorkspace_sptr guessWs = createWsAlg->getProperty("OutputWorkspace");

plotMiniPlot(guessWs, 0, "FuryFitPlot", "guess");
QPen p(Qt::green, Qt::SolidLine);
m_curves["guess"]->setPen(p);
replot("FuryFitPlot");
}

Expand Down

0 comments on commit 0d34503

Please sign in to comment.