Skip to content

Commit

Permalink
Re #10576. Moved local parameter values to FunctionBrowser.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Mar 26, 2015
1 parent 3ebbaaf commit 171f6e6
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 67 deletions.
Expand Up @@ -84,12 +84,12 @@ class MANTIDQT_CUSTOMINTERFACES_DLL MultiDatasetFit: public API::UserSubWindow
std::pair<double,double> getFittingRange(int i) const;
/// Total number of spectra (datasets).
int getNumberOfSpectra() const;
/// Get value of a local parameter
double getLocalParameterValue(const QString& parName, int i) const;
/// Display info about the plot.
void showPlotInfo();
/// Check that the data sets in the table are valid
void checkDataSets();
/// Get value of a local parameter
double getLocalParameterValue(const QString& parName, int i) const;

public slots:
void setLocalParameterValue(const QString& parName, int i, double value);
Expand All @@ -99,7 +99,6 @@ private slots:
void fit();
void editLocalParameterValues(const QString& parName);
void finishFit(bool);
void updateLocalParameters(int index);
void enableZoom();
void enablePan();
void enableRange();
Expand All @@ -111,7 +110,6 @@ private slots:
private:
void createPlotToolbar();
boost::shared_ptr<Mantid::API::IFunction> createFunction() const;
void initLocalParameter(const QString& parName)const;
void updateParameters(const Mantid::API::IFunction& fun);
void showInfo(const QString& text);
bool eventFilter(QObject *widget, QEvent *evn);
Expand All @@ -134,8 +132,6 @@ private slots:
MantidWidgets::FitOptionsBrowser *m_fitOptionsBrowser;
/// Name of the output workspace
std::string m_outputWorkspaceName;
/// Storage for local paramtere values
mutable QMap<QString,QVector<double>> m_localParameterValues;
/// Fit algorithm runner
boost::shared_ptr<API::AlgorithmRunner> m_fitRunner;
};
Expand Down
76 changes: 20 additions & 56 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp
Expand Up @@ -834,14 +834,14 @@ void MultiDatasetFit::initLayout()
connect(m_dataController,SIGNAL(dataTableUpdated()),m_plotController,SLOT(tableUpdated()));
connect(m_dataController,SIGNAL(dataSetUpdated(int)),m_plotController,SLOT(updateRange(int)));
connect(m_plotController,SIGNAL(fittingRangeChanged(int, double, double)),m_dataController,SLOT(setFittingRange(int, double, double)));
connect(m_plotController,SIGNAL(currentIndexChanged(int)),this,SLOT(updateLocalParameters(int)));

QSplitter* splitter = new QSplitter(Qt::Vertical,this);

m_functionBrowser = new MantidQt::MantidWidgets::FunctionBrowser(NULL, true);
splitter->addWidget( m_functionBrowser );
connect(m_functionBrowser,SIGNAL(localParameterButtonClicked(const QString&)),this,SLOT(editLocalParameterValues(const QString&)));
connect(m_functionBrowser,SIGNAL(functionStructureChanged()),this,SLOT(reset()));
connect(m_plotController,SIGNAL(currentIndexChanged(int)),m_functionBrowser,SLOT(setCurrentDataset(int)));

m_fitOptionsBrowser = new MantidQt::MantidWidgets::FitOptionsBrowser(NULL);
splitter->addWidget( m_fitOptionsBrowser );
Expand Down Expand Up @@ -1112,45 +1112,6 @@ void MultiDatasetFit::editLocalParameterValues(const QString& parName)
dialog.exec();
}

/**
* Get value of a local parameter
* @param parName :: Name of a parameter.
* @param i :: Data set index.
*/
double MultiDatasetFit::getLocalParameterValue(const QString& parName, int i) const
{
if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfSpectra() )
{
initLocalParameter(parName);
}
return m_localParameterValues[parName][i];
}

void MultiDatasetFit::setLocalParameterValue(const QString& parName, int i, double value)
{
if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfSpectra() )
{
initLocalParameter(parName);
}
m_localParameterValues[parName][i] = value;
}

/**
* Init a local parameter. Define initial values for all datasets.
* @param parName :: Name of parametere to init.
*/
void MultiDatasetFit::initLocalParameter(const QString& parName)const
{
double value = m_functionBrowser->getParameter(parName);
QVector<double> values( static_cast<int>(getNumberOfSpectra()), value );
m_localParameterValues[parName] = values;
}

void MultiDatasetFit::reset()
{
m_localParameterValues.clear();
}

/**
* Slot called on completion of the Fit algorithm.
* @param error :: Set to true if Fit finishes with an error.
Expand All @@ -1171,7 +1132,7 @@ void MultiDatasetFit::finishFit(bool error)
*/
void MultiDatasetFit::updateParameters(const Mantid::API::IFunction& fun)
{
m_localParameterValues.clear();
m_functionBrowser->resetLocalParameters();
auto cfun = dynamic_cast<const Mantid::API::CompositeFunction*>( &fun );
if ( cfun && cfun->nFunctions() > 0 )
{
Expand All @@ -1191,7 +1152,7 @@ void MultiDatasetFit::updateParameters(const Mantid::API::IFunction& fun)
}
for(int j = 0; j < qLocalParameters.size(); ++j)
{
setLocalParameterValue( qLocalParameters[j], static_cast<int>(i), sfun->getParameter(localParameters[j]) );
m_functionBrowser->setLocalParameterValue( qLocalParameters[j], static_cast<int>(i), sfun->getParameter(localParameters[j]) );
}
}
}
Expand All @@ -1201,20 +1162,6 @@ void MultiDatasetFit::updateParameters(const Mantid::API::IFunction& fun)
}
}

/**
* Update the local parameters in the function browser to show values corresponding
* to a particular dataset.
* @param index :: Index of a dataset.
*/
void MultiDatasetFit::updateLocalParameters(int index)
{
auto localParameters = m_functionBrowser->getLocalParameters();
foreach(QString par, localParameters)
{
m_functionBrowser->setParameter( par, getLocalParameterValue( par, index ) );
}
}

/**
* Show a message in the info bar at the bottom of the interface.
*/
Expand Down Expand Up @@ -1335,6 +1282,23 @@ void MultiDatasetFit::enableRange()
m_uiForm.toolOptions->setCurrentIndex(rangeToolPage);
}

void MultiDatasetFit::setLocalParameterValue(const QString& parName, int i, double value)
{
m_functionBrowser->setLocalParameterValue(parName, i, value);
}

/// Get value of a local parameter
double MultiDatasetFit::getLocalParameterValue(const QString& parName, int i) const
{
return m_functionBrowser->getLocalParameterValue(parName, i);
}

void MultiDatasetFit::reset()
{
m_functionBrowser->resetLocalParameters();
m_functionBrowser->setNumberOfDatasets( getNumberOfSpectra() );
}

/*==========================================================================================*/
/* DataController */
/*==========================================================================================*/
Expand Down
Expand Up @@ -95,10 +95,6 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget
Mantid::API::IFunction_sptr getFunction(QtProperty* prop = NULL, bool attributesOnly = false);
/// Check if a function is set
bool hasFunction() const;
/// Get a list of names of global parameters
QStringList getGlobalParameters() const;
/// Get a list of names of local parameters
QStringList getLocalParameters() const;

/// Return a function with specified index
Mantid::API::IFunction_sptr getFunctionByIndex(const QString& index);
Expand All @@ -117,6 +113,15 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget
/// Update parameter values in the browser to match those of a function.
void updateParameters(const Mantid::API::IFunction& fun);

/// Get a list of names of global parameters
QStringList getGlobalParameters() const;
/// Get a list of names of local parameters
QStringList getLocalParameters() const;
/// Get the number of datasets
size_t getNumberOfDatasets() const;
/// Get value of a local parameter
double getLocalParameterValue(const QString& parName, int i) const;

signals:
/// User selects a different function (or one of it's sub-properties)
void currentFunctionChanged();
Expand All @@ -131,6 +136,13 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget

void functionStructureChanged();

public slots:

void setNumberOfDatasets(size_t n);
void setLocalParameterValue(const QString& parName, int i, double value);
void resetLocalParameters();
void setCurrentDataset(int i);

protected:
/// Create the Qt property browser
void createBrowser();
Expand Down Expand Up @@ -205,6 +217,8 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget
/// Check if a parameter property has a upper bound
bool hasUpperBound(QtProperty* prop) const;

void initLocalParameter(const QString& parName)const;

protected slots:
/// Show the context menu
void popupMenu(const QPoint &);
Expand Down Expand Up @@ -319,6 +333,12 @@ protected slots:

/// Set true if the constructed function is intended to be used in a multi-dataset fit
bool m_multiDataset;
/// Number of datasets this function is used for
size_t m_numberOfDatasets;
/// Storage for local paramtere values
mutable QMap<QString,QVector<double>> m_localParameterValues;
/// Index of a dataset for which the parameters are currently displayed
int m_currentDataset;

friend class CreateAttributePropertyForFunctionBrowser;
friend class SetAttributeFromProperty;
Expand Down
80 changes: 79 additions & 1 deletion Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp
Expand Up @@ -82,7 +82,10 @@ namespace MantidWidgets
* @param multi :: Option to use the browser for multi-dataset fitting.
*/
FunctionBrowser::FunctionBrowser(QWidget *parent, bool multi)
:QWidget(parent),m_multiDataset(multi)
:QWidget(parent),m_multiDataset(multi),
m_numberOfDatasets(0),
m_currentDataset(0)

{
// create m_browser
createBrowser();
Expand Down Expand Up @@ -1829,5 +1832,80 @@ bool FunctionBrowser::hasFunction() const
return ! m_functionManager->properties().isEmpty();
}

/// Get the number of datasets
size_t FunctionBrowser::getNumberOfDatasets() const
{
return m_numberOfDatasets;
}

/// Set new number of the datasets
/// @param n :: New value for the number of datasets.
void FunctionBrowser::setNumberOfDatasets(size_t n)
{
if ( !m_multiDataset )
{
throw std::runtime_error("Function browser wasn't set up for multi-dataset fitting.");
}
m_numberOfDatasets = n;
}

/**
* Get value of a local parameter
* @param parName :: Name of a parameter.
* @param i :: Data set index.
*/
double FunctionBrowser::getLocalParameterValue(const QString& parName, int i) const
{
if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfDatasets() )
{
initLocalParameter(parName);
}
return m_localParameterValues[parName][i];
}

void FunctionBrowser::setLocalParameterValue(const QString& parName, int i, double value)
{
if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfDatasets() )
{
initLocalParameter(parName);
}
m_localParameterValues[parName][i] = value;
if ( i == m_currentDataset )
{
setParameter( parName, value );
}
}

/**
* Init a local parameter. Define initial values for all datasets.
* @param parName :: Name of parametere to init.
*/
void FunctionBrowser::initLocalParameter(const QString& parName)const
{
double value = getParameter(parName);
QVector<double> values( static_cast<int>(getNumberOfDatasets()), value );
m_localParameterValues[parName] = values;
}

void FunctionBrowser::resetLocalParameters()
{
m_localParameterValues.clear();
}

/// Set current dataset.
void FunctionBrowser::setCurrentDataset(int i)
{
m_currentDataset = i;
if ( m_currentDataset >= m_numberOfDatasets )
{
throw std::runtime_error("Dataset index is outside the range");
}
auto localParameters = getLocalParameters();
foreach(QString par, localParameters)
{
setParameter( par, getLocalParameterValue( par, m_currentDataset ) );
}
}

} // MantidWidgets
} // MantidQt

0 comments on commit 171f6e6

Please sign in to comment.