Skip to content

Commit

Permalink
Re #4158. Added custom FitDialog which needs more improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 2, 2012
1 parent 637f9eb commit 53b5dc5
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,21 @@ namespace Mantid
/// A friend that can create instances of this class
friend class Fit;
/// Constructor
MultiDomainCreator(API::Algorithm* fit):IDomainCreator(fit){}
MultiDomainCreator(API::Algorithm* fit, size_t n):
IDomainCreator(fit),
m_creators(n),
m_workspacePropertyNames(n)
{
}

/// Create a domain from the input workspace
virtual void createDomain(
const std::vector<std::string>& workspacePropetyNames,
boost::shared_ptr<API::FunctionDomain>& domain,
boost::shared_ptr<API::IFunctionValues>& values, size_t i0);

void addCreator(const std::string& workspacePropetyName,IDomainCreator* creator);
void setCreator(size_t i, const std::string& workspacePropetyName,IDomainCreator* creator);
bool hasCreator(size_t i) const;

/// Vector of creators.
std::vector< boost::shared_ptr<IDomainCreator> > m_creators;
Expand Down
44 changes: 33 additions & 11 deletions Code/Mantid/Framework/CurveFitting/src/Fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,24 @@ namespace CurveFitting
auto mdf = boost::dynamic_pointer_cast<API::MultiDomainFunction>(m_function);
if (mdf)
{
m_workspacePropertyNames.resize(mdf->nFunctions());
m_workspacePropertyNames[0] = "InputWorkspace";
for(size_t i = 1; i < mdf->nFunctions(); ++i)
{
declareProperty(
new API::WorkspaceProperty<API::Workspace>("InputWorkspace_"+boost::lexical_cast<std::string>(i),"",Kernel::Direction::Input),
"Name of the input Workspace");
std::string workspacePropertyName = "InputWorkspace_"+boost::lexical_cast<std::string>(i);
m_workspacePropertyNames[i] = workspacePropertyName;
if (!existsProperty(workspacePropertyName))
{
declareProperty(
new API::WorkspaceProperty<API::Workspace>(workspacePropertyName,"",Kernel::Direction::Input),
"Name of the input Workspace");
}
}
}
else
{
m_workspacePropertyNames.resize(1,"InputWorkspace");
}
}

void Fit::addWorkspace(const std::string& workspacePropertyName, bool addProperties)
Expand All @@ -101,6 +112,7 @@ namespace CurveFitting
//m_function->setWorkspace(ws);
const size_t n = std::string("InputWorkspace").size();
const std::string suffix = (workspacePropertyName.size() > n)? workspacePropertyName.substr(n) : "";
const size_t index = suffix.empty() ? 0 : boost::lexical_cast<size_t>(suffix.substr(1));

API::IFunction_sptr fun = getProperty("Function");
IDomainCreator* creator = nullptr;
Expand All @@ -118,20 +130,25 @@ namespace CurveFitting
{// don't know what to do with this workspace
throw std::invalid_argument("Unsupported workspace type" + ws->id());
}
creator->declareDatasetProperties(suffix,addProperties);
m_workspacePropertyNames.push_back(workspacePropertyName);

if (!m_domainCreator)
{
if (boost::dynamic_pointer_cast<API::MultiDomainFunction>(fun))
if (m_workspacePropertyNames.empty())
{
setFunction();
}
auto multiFun = boost::dynamic_pointer_cast<API::MultiDomainFunction>(fun);
if (multiFun)
{
auto multiCreator = new MultiDomainCreator(this);
multiCreator->addCreator(workspacePropertyName,creator);
auto multiCreator = new MultiDomainCreator(this,m_workspacePropertyNames.size());
multiCreator->setCreator(index,workspacePropertyName,creator);
m_domainCreator.reset(multiCreator);
creator->declareDatasetProperties(suffix,addProperties);
}
else
{
m_domainCreator.reset(creator);
creator->declareDatasetProperties(suffix,addProperties);
}
}
else
Expand All @@ -142,7 +159,11 @@ namespace CurveFitting
{
throw std::runtime_error(std::string("MultiDomainCreator expected, found ") + typeid(*m_domainCreator.get()).name());
}
multiCreator->addCreator(workspacePropertyName,creator);
if (!multiCreator->hasCreator(index))
{
creator->declareDatasetProperties(suffix,addProperties);
}
multiCreator->setCreator(index,workspacePropertyName,creator);
}

}
Expand All @@ -155,7 +176,7 @@ namespace CurveFitting
auto multiFun = boost::dynamic_pointer_cast<API::MultiDomainFunction>(m_function);
if (multiFun)
{
m_domainCreator.reset(new MultiDomainCreator(this));
m_domainCreator.reset(new MultiDomainCreator(this,m_workspacePropertyNames.size()));
}
auto props = getProperties();
for(auto prop = props.begin(); prop != props.end(); ++prop)
Expand All @@ -180,6 +201,7 @@ namespace CurveFitting
}
const size_t n = std::string("InputWorkspace").size();
const std::string suffix = (workspacePropertyName.size() > n)? workspacePropertyName.substr(n) : "";
const size_t index = suffix.empty() ? 0 : boost::lexical_cast<size_t>(suffix.substr(1));
creator->declareDatasetProperties(suffix,false);
m_workspacePropertyNames.push_back(workspacePropertyName);
if (!m_domainCreator)
Expand All @@ -189,7 +211,7 @@ namespace CurveFitting
auto multiCreator = boost::dynamic_pointer_cast<MultiDomainCreator>(m_domainCreator);
if (multiCreator)
{
multiCreator->addCreator(workspacePropertyName,creator);
multiCreator->setCreator(index,workspacePropertyName,creator);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/CurveFitting/src/FitMW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace
m_startXPropertyName = "StartX" + suffix;
m_endXPropertyName = "EndX" + suffix;

if (addProp)
if (addProp && !m_fit->existsProperty(m_workspaceIndexPropertyName))
{
BoundedValidator<int> *mustBePositive = new BoundedValidator<int>();
mustBePositive->setLower(0);
Expand Down
15 changes: 12 additions & 3 deletions Code/Mantid/Framework/CurveFitting/src/MultiDomainCreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ namespace Mantid
namespace CurveFitting
{

void MultiDomainCreator::addCreator(const std::string& workspacePropetyName,IDomainCreator* creator)
void MultiDomainCreator::setCreator(size_t i, const std::string& workspacePropetyName,IDomainCreator* creator)
{
m_workspacePropertyNames.push_back(workspacePropetyName);
m_creators.push_back(boost::shared_ptr<IDomainCreator>(creator));
m_workspacePropertyNames[i] = workspacePropetyName;
m_creators[i] = boost::shared_ptr<IDomainCreator>(creator);
}

bool MultiDomainCreator::hasCreator(size_t i) const
{
return static_cast<bool>(m_creators[i]);
}

/// Create a domain from the input workspace
Expand All @@ -31,6 +36,10 @@ namespace CurveFitting
i0 = 0;
for(auto c = m_creators.begin(); c != m_creators.end(); ++c)
{
if (!(*c))
{
throw std::runtime_error("Missing domain creator");
}
auto i = static_cast<size_t>(c - m_creators.begin());
API::FunctionDomain_sptr domain;
(**c).createDomain(std::vector<std::string>(1,workspacePropetyNames[i]),domain,values,i0);
Expand Down
100 changes: 59 additions & 41 deletions Code/Mantid/Framework/CurveFitting/test/MultiDomainFunctionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "MantidAPI/JointDomain.h"
#include "MantidAPI/IFunction1D.h"
#include "MantidAPI/ParamFunction.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidCurveFitting/CostFuncLeastSquares.h"
#include "MantidCurveFitting/LevenbergMarquardtMDMinimizer.h"
#include "MantidCurveFitting/Fit.h"
Expand Down Expand Up @@ -59,6 +60,7 @@ class MultiDomainFunctionTest : public CxxTest::TestSuite
public:
MultiDomainFunctionTest()
{
FrameworkManager::Instance();
multi = boost::make_shared<MultiDomainFunction>();
multi->addFunction(boost::make_shared<MultiDomainFunctionTest_Function>());
multi->addFunction(boost::make_shared<MultiDomainFunctionTest_Function>());
Expand All @@ -78,6 +80,47 @@ class MultiDomainFunctionTest : public CxxTest::TestSuite
domain->addDomain(boost::make_shared<FunctionDomain1D>(1,2,10));
domain->addDomain(boost::make_shared<FunctionDomain1D>(2,3,11));

const double A0 = 0, A1 = 1, A2 = 2;
const double B0 = 1, B1 = 2, B2 = 3;

ws1.reset(new WorkspaceTester);
ws1->initialize(1,10,10);
{
Mantid::MantidVec& x = ws1->dataX(0);
Mantid::MantidVec& y = ws1->dataY(0);
Mantid::MantidVec& e = ws1->dataE(0);
for(size_t i = 0; i < ws1->blocksize(); ++i)
{
x[i] = 0.1 * i;
y[i] = A0 + A1 + A2 + (B0 + B1 + B2) * x[i];
}
}

ws2.reset(new WorkspaceTester);
ws2->initialize(1,10,10);
{
Mantid::MantidVec& x = ws2->dataX(0);
Mantid::MantidVec& y = ws2->dataY(0);
Mantid::MantidVec& e = ws2->dataE(0);
for(size_t i = 0; i < ws2->blocksize(); ++i)
{
x[i] = 1 + 0.1 * i;
y[i] = A0 + A1 + (B0 + B1) * x[i];
}
}

ws3.reset(new WorkspaceTester);
ws3->initialize(1,10,10);
{
Mantid::MantidVec& x = ws3->dataX(0);
Mantid::MantidVec& y = ws3->dataY(0);
Mantid::MantidVec& e = ws3->dataE(0);
for(size_t i = 0; i < ws3->blocksize(); ++i)
{
x[i] = 2 + 0.1 * i;
y[i] = A0 + A2 + (B0 + B2) * x[i];
}
}
}

void test_fit()
Expand Down Expand Up @@ -137,47 +180,6 @@ class MultiDomainFunctionTest : public CxxTest::TestSuite

void test_Fit_algorithm()
{
const double A0 = 0, A1 = 1, A2 = 2;
const double B0 = 1, B1 = 2, B2 = 3;

MatrixWorkspace_sptr ws1(new WorkspaceTester);
ws1->initialize(1,10,10);
{
Mantid::MantidVec& x = ws1->dataX(0);
Mantid::MantidVec& y = ws1->dataY(0);
Mantid::MantidVec& e = ws1->dataE(0);
for(size_t i = 0; i < ws1->blocksize(); ++i)
{
x[i] = 0.1 * i;
y[i] = A0 + A1 + A2 + (B0 + B1 + B2) * x[i];
}
}

MatrixWorkspace_sptr ws2(new WorkspaceTester);
ws2->initialize(1,10,10);
{
Mantid::MantidVec& x = ws2->dataX(0);
Mantid::MantidVec& y = ws2->dataY(0);
Mantid::MantidVec& e = ws2->dataE(0);
for(size_t i = 0; i < ws2->blocksize(); ++i)
{
x[i] = 1 + 0.1 * i;
y[i] = A0 + A1 + (B0 + B1) * x[i];
}
}

MatrixWorkspace_sptr ws3(new WorkspaceTester);
ws3->initialize(1,10,10);
{
Mantid::MantidVec& x = ws3->dataX(0);
Mantid::MantidVec& y = ws3->dataY(0);
Mantid::MantidVec& e = ws3->dataE(0);
for(size_t i = 0; i < ws3->blocksize(); ++i)
{
x[i] = 2 + 0.1 * i;
y[i] = A0 + A2 + (B0 + B2) * x[i];
}
}

multi->getFunction(0)->setParameter("A",0);
multi->getFunction(0)->setParameter("B",0);
Expand Down Expand Up @@ -207,9 +209,25 @@ class MultiDomainFunctionTest : public CxxTest::TestSuite

}

void test_Fit_resetting_properties()
{
system("pause");
Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("Fit");
Mantid::API::IAlgorithm& fit = *alg;
fit.initialize();
fit.setProperty("Function",boost::dynamic_pointer_cast<IFunction>(multi));
fit.setProperty("InputWorkspace",ws1);
fit.setProperty("WorkspaceIndex",0);
fit.setProperty("InputWorkspace",ws2);
fit.setProperty("WorkspaceIndex",1);
fit.setProperty("InputWorkspace_1",ws2);
fit.setProperty("InputWorkspace_1",ws1);
}

private:
boost::shared_ptr<MultiDomainFunction> multi;
boost::shared_ptr<JointDomain> domain;
MatrixWorkspace_sptr ws1,ws2,ws3;
};

#endif /*MULTIDOMAINFUNCTIONTEST_H_*/
9 changes: 7 additions & 2 deletions Code/Mantid/MantidQt/API/src/AlgorithmDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,14 @@ void AlgorithmDialog::showValidators()
*/
bool AlgorithmDialog::setPropertyValue(const QString pName, bool validateOthers)
{
Mantid::Kernel::Property *p = getAlgorithmProperty(pName);
//Mantid::Kernel::Property *p = getAlgorithmProperty(pName);
QString value = getInputValue(pName);

std::string error("");
try
{
error = p->setValue(value.toStdString());
//error = p->setValue(value.toStdString());
getAlgorithm()->setPropertyValue(pName.toStdString(),value.toStdString());
}
catch(std::exception & err_details)
{
Expand Down Expand Up @@ -498,6 +499,10 @@ void AlgorithmDialog::untie(const QString & property)
QWidget* AlgorithmDialog::tie(QWidget* widget, const QString & property, QLayout *parent_layout,
bool readHistory)
{
if (property == "WorkspaceIndex")
{
std::cerr << "tie " << property.toStdString() << std::endl;
}
if( m_tied_properties.contains(property) )
m_tied_properties.remove(property);

Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/MantidQt/CustomDialogs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set ( SRC_FILES src/CreateSampleShapeDialog.cpp
src/ConvertTableToMatrixWorkspaceDialog.cpp
src/StartLiveDataDialog.cpp
src/FitDialog.cpp
src/LoadDialog.cpp
src/LoadAsciiDialog.cpp
src/LoadDAEDialog.cpp
Expand All @@ -18,6 +19,7 @@ set ( SRC_UNITY_IGNORE_FILES )
set ( MOC_FILES inc/MantidQtCustomDialogs/CreateSampleShapeDialog.h
inc/MantidQtCustomDialogs/ConvertTableToMatrixWorkspaceDialog.h
inc/MantidQtCustomDialogs/StartLiveDataDialog.h
inc/MantidQtCustomDialogs/FitDialog.h
inc/MantidQtCustomDialogs/LoadDialog.h
inc/MantidQtCustomDialogs/LoadAsciiDialog.h
inc/MantidQtCustomDialogs/LoadDAEDialog.h
Expand All @@ -42,6 +44,7 @@ set ( UI_FILES inc/MantidQtCustomDialogs/CreateSampleShapeDialog.ui
inc/MantidQtCustomDialogs/LoadDialog.ui
inc/MantidQtCustomDialogs/StartLiveDataDialog.ui
inc/MantidQtCustomDialogs/SlicingAlgorithmDialog.ui
inc/MantidQtCustomDialogs/FitDialog.ui
)

include_directories ( inc )
Expand Down

0 comments on commit 53b5dc5

Please sign in to comment.