Skip to content

Commit

Permalink
Re #10576. Local parameter fixing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 2, 2015
1 parent 458c060 commit 1f53b67
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 43 deletions.
Expand Up @@ -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<EditLocalParameterDialog*>(parent());}
Expand Down
39 changes: 10 additions & 29 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp
Expand Up @@ -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):
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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<const Mantid::API::CompositeFunction*>( &fun );
if ( cfun && cfun->nFunctions() > 0 )
{
auto qLocalParameters = m_functionBrowser->getLocalParameters();
std::vector<std::string> localParameters;
foreach(QString par, qLocalParameters)
{
localParameters.push_back( par.toStdString() );
}
size_t currentIndex = static_cast<size_t>( 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<int>(i), sfun->getParameter(localParameters[j]) );
}
}
}
else
{
m_functionBrowser->updateParameters( fun );
}
m_functionBrowser->updateMultiDatasetParameters( fun );
}

/**
Expand Down
Expand Up @@ -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)
Expand Down
55 changes: 41 additions & 14 deletions Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp
Expand Up @@ -1627,6 +1627,7 @@ void FunctionBrowser::updateParameters(const Mantid::API::IFunction& fun)
}
}


/**
* Return FunctionFactory function string
*/
Expand Down Expand Up @@ -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<Mantid::API::MultiDomainFunction> multiFun = boost::dynamic_pointer_cast<Mantid::API::MultiDomainFunction>( fun );
if ( !multiFun )
{
Expand All @@ -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<std::string>( 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<std::string>( getLocalParameterValue(parName,i) ));
}
else
Expand Down Expand Up @@ -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<const Mantid::API::CompositeFunction*>( &fun );
if ( cfun && cfun->nFunctions() > 0 )
{
auto qLocalParameters = getLocalParameters();
std::vector<std::string> localParameters;
foreach(QString par, qLocalParameters)
{
localParameters.push_back( par.toStdString() );
}
size_t currentIndex = static_cast<size_t>( 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<int>(i), sfun->getParameter(localParameters[j]) );
}
}
}
else
{
updateParameters( fun );
}
}

} // MantidWidgets
} // MantidQt

0 comments on commit 1f53b67

Please sign in to comment.