From 1f53b678e715f99b008930025c81e38927acef8b Mon Sep 17 00:00:00 2001 From: Roman Tolchenov Date: Thu, 2 Apr 2015 16:33:17 +0100 Subject: [PATCH] Re #10576. Local parameter fixing. --- .../MultiDatasetFit.h | 2 + .../CustomInterfaces/src/MultiDatasetFit.cpp | 39 ++++--------- .../MantidQtMantidWidgets/FunctionBrowser.h | 2 + .../MantidWidgets/src/FunctionBrowser.cpp | 55 ++++++++++++++----- 4 files changed, 55 insertions(+), 43 deletions(-) diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h index 3e6d1a9ad486..eeb022438dc5 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h @@ -285,6 +285,8 @@ class LocalParameterItemDelegate: public QStyledItemDelegate signals: void setAllValues(double); void fixParameter(int,bool); +protected: + void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const; private: bool eventFilter(QObject * obj, QEvent * ev); EditLocalParameterDialog *owner() const {return static_cast(parent());} diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp index 9e543990ff4f..5977860f20e8 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp @@ -742,6 +742,13 @@ bool LocalParameterItemDelegate::eventFilter(QObject * obj, QEvent * ev) return QStyledItemDelegate::eventFilter(obj,ev); } +void LocalParameterItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const +{ + auto newOption = option; + //newOption.rect.adjust(10,-1,0,-1); + QStyledItemDelegate::paint(painter, newOption, index); +} + /*==========================================================================================*/ EditLocalParameterDialog::EditLocalParameterDialog(MultiDatasetFit *multifit, const QString &parName): @@ -962,6 +969,7 @@ void MultiDatasetFit::fit() try { auto fun = createFunction(); + std::cerr << fun->asString() << std::endl; auto fit = Mantid::API::AlgorithmManager::Instance().create("Fit"); fit->initialize(); fit->setProperty("Function", fun ); @@ -1081,38 +1089,11 @@ void MultiDatasetFit::finishFit(bool error) } /** - * Update the interface to have the sametparameter values as in a function. + * Update the interface to have the same parameter values as in a function. */ void MultiDatasetFit::updateParameters(const Mantid::API::IFunction& fun) { - m_functionBrowser->resetLocalParameters(); - auto cfun = dynamic_cast( &fun ); - if ( cfun && cfun->nFunctions() > 0 ) - { - auto qLocalParameters = m_functionBrowser->getLocalParameters(); - std::vector localParameters; - foreach(QString par, qLocalParameters) - { - localParameters.push_back( par.toStdString() ); - } - size_t currentIndex = static_cast( m_plotController->getCurrentIndex() ); - for(size_t i = 0; i < cfun->nFunctions(); ++i) - { - auto sfun = cfun->getFunction(i); - if ( i == currentIndex ) - { - m_functionBrowser->updateParameters( *sfun ); - } - for(int j = 0; j < qLocalParameters.size(); ++j) - { - m_functionBrowser->setLocalParameterValue( qLocalParameters[j], static_cast(i), sfun->getParameter(localParameters[j]) ); - } - } - } - else - { - m_functionBrowser->updateParameters( fun ); - } + m_functionBrowser->updateMultiDatasetParameters( fun ); } /** diff --git a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h index 7852656e5462..e7b19630bdfa 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h +++ b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h @@ -134,6 +134,8 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget void setLocalParameterFixed(const QString& parName, int i, bool fixed); /// Return the multidomain function if number of datasets is greater than 1 Mantid::API::IFunction_sptr getGlobalFunction(); + /// Update parameter values in the browser to match those of a function. + void updateMultiDatasetParameters(const Mantid::API::IFunction& fun); signals: /// User selects a different function (or one of it's sub-properties) diff --git a/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp b/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp index 10e9b42ad5aa..4129c90f94c7 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp +++ b/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp @@ -1627,6 +1627,7 @@ void FunctionBrowser::updateParameters(const Mantid::API::IFunction& fun) } } + /** * Return FunctionFactory function string */ @@ -2060,8 +2061,7 @@ Mantid::API::IFunction_sptr FunctionBrowser::getGlobalFunction() } // create the multi-domain function - std::string tmpStr = multiFunStr.toStdString(); - auto fun = Mantid::API::FunctionFactory::Instance().createInitialized( tmpStr ); + auto fun = Mantid::API::FunctionFactory::Instance().createInitialized( multiFunStr.toStdString() ); boost::shared_ptr multiFun = boost::dynamic_pointer_cast( fun ); if ( !multiFun ) { @@ -2082,20 +2082,14 @@ Mantid::API::IFunction_sptr FunctionBrowser::getGlobalFunction() auto tie = fun1->getTie(j); if ( tie ) { - // if a local parameter has a constant tie (is fixed) set tie's value to - // the value of the local parameter - if ( tie->isConstant() ) - { - std::string expr = boost::lexical_cast( getLocalParameterValue(parName,i) ); - tie->set( expr ); - } - else - { - g_log.error() << "Complex ties are not implemented yet in multi-dataset fitting." << std::endl; - } + // If parameter has a tie at this stage then it gets it form the currently + // displayed function. But the i-th local parameters may not have this tie, + // so remove it + fun1->removeTie(j); } - else if ( isLocalParameterFixed(parName,i) ) + if ( isLocalParameterFixed(parName,i) ) { + // Fix this particular local parameter fun1->tie(parName.toStdString(),boost::lexical_cast( getLocalParameterValue(parName,i) )); } else @@ -2149,5 +2143,38 @@ bool FunctionBrowser::isLocalParameterFixed(const QString& parName, int i) const return m_localParameterValues[parName][i].fixed; } +/// Update the interface to have the same parameter values as in a function. +/// @param fun :: A function to get parameter values from. +void FunctionBrowser::updateMultiDatasetParameters(const Mantid::API::IFunction& fun) +{ + auto cfun = dynamic_cast( &fun ); + if ( cfun && cfun->nFunctions() > 0 ) + { + auto qLocalParameters = getLocalParameters(); + std::vector localParameters; + foreach(QString par, qLocalParameters) + { + localParameters.push_back( par.toStdString() ); + } + size_t currentIndex = static_cast( m_currentDataset ); + for(size_t i = 0; i < cfun->nFunctions(); ++i) + { + auto sfun = cfun->getFunction(i); + if ( i == currentIndex ) + { + updateParameters( *sfun ); + } + for(int j = 0; j < qLocalParameters.size(); ++j) + { + setLocalParameterValue( qLocalParameters[j], static_cast(i), sfun->getParameter(localParameters[j]) ); + } + } + } + else + { + updateParameters( fun ); + } +} + } // MantidWidgets } // MantidQt