Newer
Older
#include "MantidQtCustomInterfaces/Muon/MuonSequentialFitDialog.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/AlgorithmProxy.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceProperty.h"
Federico Montesino Pouzols
committed
#include "MantidGeometry/Instrument.h"
#include "MantidQtCustomInterfaces/Muon/MuonAnalysisFitDataPresenter.h"
#include "MantidQtMantidWidgets/MuonFitPropertyBrowser.h"
namespace MantidQt {
namespace CustomInterfaces {
using namespace Mantid::Kernel;
using namespace Mantid::API;
using MantidQt::CustomInterfaces::MuonAnalysisFitDataPresenter;
using MantidQt::MantidWidgets::MuonFitPropertyBrowser;
namespace {
Logger g_log("MuonSequentialFitDialog");
}
const std::string MuonSequentialFitDialog::SEQUENTIAL_PREFIX("MuonSeqFit_");
/**
* Constructor
* @param fitPropBrowser :: [input] Pointer to fit property browser
* @param dataPresenter :: [input] Pointer to fit data presenter
* @param groupNames :: [input] List of groups/pairs to fit for each run
* @param periods :: [input] List of periods to fit for each run
*/
MuonSequentialFitDialog::MuonSequentialFitDialog(
MuonFitPropertyBrowser *fitPropBrowser,
MuonAnalysisFitDataPresenter *dataPresenter,
const std::vector<std::string> &groupNames,
const std::vector<std::string> &periods)
: QDialog(fitPropBrowser), m_fitPropBrowser(fitPropBrowser),
m_dataPresenter(dataPresenter), m_groups(groupNames), m_periods(periods) {
m_ui.setupUi(this);
setState(Stopped);
// Set initial run to be run number of the workspace selected in fit browser
// when starting seq. fit dialog
const auto fitWS = boost::dynamic_pointer_cast<const MatrixWorkspace>(
m_fitPropBrowser->getWorkspace());
m_ui.runs->setText(QString::number(fitWS->getRunNumber()) + "-");
// Set the file finder to the correct instrument (not Mantid's default
// instrument)
const auto instName = fitWS->getInstrument()->getName();
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
m_ui.runs->setInstrumentOverride(QString(instName.c_str()));
// TODO: find a better initial one, e.g. previously used
m_ui.labelInput->setText("Label");
initDiagnosisTable();
// After initial values are set, update depending elements accordingly. We
// don't rely on
// slot/signal update, as element might be left with default values which
// means these will
// never be called on initialization.
updateLabelError(m_ui.labelInput->text());
updateControlButtonType(m_state);
updateInputEnabled(m_state);
updateControlEnabled(m_state);
updateCursor(m_state);
connect(m_ui.labelInput, SIGNAL(textChanged(const QString &)), this,
SLOT(updateLabelError(const QString &)));
connect(this, SIGNAL(stateChanged(DialogState)), this,
SLOT(updateControlButtonType(DialogState)));
connect(this, SIGNAL(stateChanged(DialogState)), this,
SLOT(updateInputEnabled(DialogState)));
connect(this, SIGNAL(stateChanged(DialogState)), this,
SLOT(updateControlEnabled(DialogState)));
connect(this, SIGNAL(stateChanged(DialogState)), this,
SLOT(updateCursor(DialogState)));
}
/**
* Destructor
*/
MuonSequentialFitDialog::~MuonSequentialFitDialog() {}
/**
* Checks if specified name is valid as a name for the label.
* @param label :: The name to check
* @return Empty string if valid, otherwise an error string
*/
std::string MuonSequentialFitDialog::isValidLabel(const std::string &label) {
if (label.empty())
else
return AnalysisDataService::Instance().isValid(label);
}
/**
* Returns displayable title for the given workspace;
* @param ws :: Workspace to get title from
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
* @return The title, or empty string if unable to get one
*/
std::string MuonSequentialFitDialog::getRunTitle(Workspace_const_sptr ws) {
auto matrixWS = boost::dynamic_pointer_cast<const MatrixWorkspace>(ws);
if (!matrixWS)
return "";
const std::string &instrName = matrixWS->getInstrument()->getName();
const int runNumber = matrixWS->getRunNumber();
if (instrName.empty() || runNumber == 0)
return "";
std::ostringstream runTitle;
runTitle << instrName << runNumber;
return runTitle.str();
}
/**
* Initialize diagnosis table.
*/
void MuonSequentialFitDialog::initDiagnosisTable() {
QStringList headerLabels;
// Add two static columns
headerLabels << "Run"
<< "Fit quality";
// Add remaining columns - one for every fit function parameter
IFunction_sptr fitFunc = m_fitPropBrowser->getFittingFunction();
for (size_t i = 0; i < fitFunc->nParams(); i++) {
QString paramName = QString::fromStdString(fitFunc->parameterName(i));
headerLabels << paramName;
headerLabels << paramName + "_Err";
m_ui.diagnosisTable->setColumnCount(headerLabels.size());
m_ui.diagnosisTable->setHorizontalHeaderLabels(headerLabels);
// Make the table fill all the available space and columns be resized to fit
// contents
m_ui.diagnosisTable->horizontalHeader()->setResizeMode(
QHeaderView::ResizeToContents);
// Make rows alternate bg colors for better user experience
m_ui.diagnosisTable->setAlternatingRowColors(true);
}
/**
* Add a new entry to the diagnosis table.
* @param runTitle :: Title of the run fitted
* @param fitQuality :: Number representing goodness of the fit
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
* @param fittedFunction :: Function containing fitted parameters
*/
void MuonSequentialFitDialog::addDiagnosisEntry(const std::string &runTitle,
double fitQuality,
IFunction_sptr fittedFunction) {
int newRow = m_ui.diagnosisTable->rowCount();
m_ui.diagnosisTable->insertRow(newRow);
QString runTitleDisplay = QString::fromStdString(runTitle);
m_ui.diagnosisTable->setItem(newRow, 0,
createTableWidgetItem(runTitleDisplay));
QString fitQualityDisplay = QString::number(fitQuality);
m_ui.diagnosisTable->setItem(newRow, 1,
createTableWidgetItem(fitQualityDisplay));
for (int i = 2; i < m_ui.diagnosisTable->columnCount(); i += 2) {
std::string paramName =
m_ui.diagnosisTable->horizontalHeaderItem(i)->text().toStdString();
size_t paramIndex = fittedFunction->parameterIndex(paramName);
QString value = QString::number(fittedFunction->getParameter(paramIndex));
QString error = QString::number(fittedFunction->getError(paramIndex));
m_ui.diagnosisTable->setItem(newRow, i, createTableWidgetItem(value));
m_ui.diagnosisTable->setItem(newRow, i + 1, createTableWidgetItem(error));
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
m_ui.diagnosisTable->scrollToBottom();
}
/**
* Helper function to create new item for Diagnosis table.
* @return Created and initialized item with text
*/
QTableWidgetItem *
MuonSequentialFitDialog::createTableWidgetItem(const QString &text) {
auto newItem = new QTableWidgetItem(text);
newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
return newItem;
}
/**
* Updates visibility/tooltip of label error asterisk.
* @param label :: New label as specified by user
*/
void MuonSequentialFitDialog::updateLabelError(const QString &label) {
std::string error = isValidLabel(label.toStdString());
m_ui.labelError->setVisible(!error.empty());
m_ui.labelError->setToolTip(QString::fromStdString(error));
}
/**
* Check if all the input fields are valid.
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
* @return True if everything valid, false otherwise
*/
bool MuonSequentialFitDialog::isInputValid() {
if (!m_ui.runs->isValid())
return false;
std::string label = m_ui.labelInput->text().toStdString();
return isValidLabel(label).empty();
}
/**
* Sets control button to be start/stop depending on new dialog state.
* @param newState :: New state of the dialog
*/
void MuonSequentialFitDialog::updateControlButtonType(DialogState newState) {
// Disconnect everything connected to pressed() signal of the button
disconnect(m_ui.controlButton, SIGNAL(pressed()), 0, 0);
// Connect to appropriate slot
auto buttonSlot = (newState == Running) ? SLOT(stopFit()) : SLOT(startFit());
connect(m_ui.controlButton, SIGNAL(pressed()), this, buttonSlot);
// Set appropriate text
QString buttonText = (newState == Running) ? "Stop" : "Start";
m_ui.controlButton->setText(buttonText);
}
/**
* Updates current state of the dialog.
*/
void MuonSequentialFitDialog::setState(DialogState newState) {
m_state = newState;
emit stateChanged(newState);
}
/**
* Update enabled state of all the input widgets depending on new dialog state.
* @param newState :: New state of the dialog
*/
void MuonSequentialFitDialog::updateInputEnabled(DialogState newState) {
bool enabled = (newState == Stopped);
m_ui.runs->setEnabled(enabled);
m_ui.labelInput->setEnabled(enabled);
foreach (QAbstractButton *button, m_ui.paramTypeGroup->buttons())
button->setEnabled(enabled);
}
/**
* Update control button enabled status depending on the new state.
* Button is disabled in one case only - when preparing for running.
* @param newState :: New state of the dialog
*/
void MuonSequentialFitDialog::updateControlEnabled(DialogState newState) {
m_ui.controlButton->setEnabled(newState != Preparing);
}
/**
* Update cursor depending on the new state of the dialog.
* Waiting cursor is displayed while preparing so that user does know that
* something is happening.
* @param newState :: New state of the dialog
*/
void MuonSequentialFitDialog::updateCursor(DialogState newState) {
switch (newState) {
case Preparing:
setCursor(Qt::WaitCursor);
break;
case Running:
setCursor(Qt::BusyCursor);
break;
default:
unsetCursor();
break;
* Start fitting process by running file search.
* Once search is complete, fit continues in continueFit().
*/
void MuonSequentialFitDialog::startFit() {
if (m_state != Stopped)
throw std::runtime_error("Couldn't start: already running");
setState(Preparing);
// Explicitly run the file search. This might be needed when Start is clicked
// straight after editing the run box. In that case, lost focus event might
// not be processed yet and search might not have been started yet.
// Otherwise, search is not done as the widget sees that it has not been
// changed.
connect(m_ui.runs, SIGNAL(fileInspectionFinished()), this,
SLOT(continueFit()));
if (!m_ui.runs->isSearching()) {
m_ui.runs->findFiles();
}
/**
* Carries out the fitting process once the file search has completed.
* Called when the run control reports that its search has finished.
*/
void MuonSequentialFitDialog::continueFit() {
disconnect(m_ui.runs, SIGNAL(fileInspectionFinished()), this,
SLOT(continueFit()));
// Validate input fields
if (!isInputValid()) {
QMessageBox::critical(this, "Input is not valid",
"One or more input fields are invalid.\n\nInvalid "
"fields are marked with a '*'.");
setState(Stopped);
return;
QStringList runFilenames = m_ui.runs->getFilenames();
const std::string label = m_ui.labelInput->text().toStdString();
const std::string labelGroupName = SEQUENTIAL_PREFIX + label;
AnalysisDataServiceImpl &ads = AnalysisDataService::Instance();
if (ads.doesExist(labelGroupName)) {
QMessageBox::StandardButton answer =
QMessageBox::question(this, "Label already exists",
"Label you specified was used for one of the "
"previous fits. Do you want to overwrite it?",
QMessageBox::Yes | QMessageBox::Cancel);
if (answer != QMessageBox::Yes) {
setState(Stopped);
return;
ads.deepRemoveGroup(labelGroupName);
// Create a group for label
ads.add(labelGroupName, boost::make_shared<WorkspaceGroup>());
// Tell progress bar how many iterations we will need to make and reset it
m_ui.progress->setRange(0, runFilenames.size());
m_ui.progress->setFormat("%p%");
m_ui.progress->setValue(0);
// Clear diagnosis table for new fit
m_ui.diagnosisTable->setRowCount(0);
// Get fit function as specified by user in the fit browser
IFunction_sptr fitFunction = FunctionFactory::Instance().createInitialized(
m_fitPropBrowser->getFittingFunction()->asString());
// Whether we should use initial function for every fit
bool useInitFitFunction =
(m_ui.paramTypeGroup->checkedButton() == m_ui.paramTypeInitial);
setState(Running);
m_stopRequested = false;
for (auto fileIt = runFilenames.constBegin();
fileIt != runFilenames.constEnd(); ++fileIt) {
// Process events (so that Stop button press is processed)
QApplication::processEvents();
// Stop if requested by user
if (m_stopRequested)
break;
MatrixWorkspace_sptr ws;
try {
// If ApplyDeadTimeCorrection is set but no dead time table is set,
// we need to load one from the file.
bool loadDeadTimesFromFile = false;
bool applyDTC = m_processAlg->getProperty("ApplyDeadTimeCorrection");
if (applyDTC) {
if (auto deadTimes =
m_processAlg->getPointerToProperty("DeadTimeTable")) {
if (deadTimes->value() == "") {
// No workspace set for dead time table - we need to load one
loadDeadTimesFromFile = true;
// Use LoadMuonNexus to load the file
auto loadAlg = AlgorithmManager::Instance().create("LoadMuonNexus");
loadAlg->initialize();
loadAlg->setChild(true);
loadAlg->setRethrows(true);
loadAlg->setPropertyValue("Filename", fileIt->toStdString());
loadAlg->setPropertyValue("OutputWorkspace", "__NotUsed");
if (loadDeadTimesFromFile) {
loadAlg->setPropertyValue("DeadTimeTable", "__DeadTimes");
}
loadAlg->execute();
Workspace_sptr loadedWS = loadAlg->getProperty("OutputWorkspace");
double loadedTimeZero = loadAlg->getProperty("TimeZero");
// then use MuonProcess to process it
auto process = AlgorithmManager::Instance().create("MuonProcess");
process->initialize();
process->setChild(true);
process->setRethrows(true);
process->updatePropertyValues(*m_processAlg);
process->setProperty("InputWorkspace", loadedWS);
process->setProperty("LoadedTimeZero", loadedTimeZero);
process->setPropertyValue("OutputWorkspace", "__YouDontSeeMeIAmNinja");
if (m_fitPropBrowser->rawData()) // TODO: or vice verca?
process->setPropertyValue("RebinParams", "");
if (loadDeadTimesFromFile) {
Workspace_sptr deadTimes = loadAlg->getProperty("DeadTimeTable");
ITableWorkspace_sptr deadTimesTable;
if (auto table =
boost::dynamic_pointer_cast<ITableWorkspace>(deadTimes)) {
deadTimesTable = table;
} else if (auto group =
boost::dynamic_pointer_cast<WorkspaceGroup>(deadTimes)) {
deadTimesTable =
boost::dynamic_pointer_cast<ITableWorkspace>(group->getItem(0));
process->setProperty("DeadTimeTable", deadTimesTable);
process->execute();
Workspace_sptr outputWS = process->getProperty("OutputWorkspace");
ws = boost::dynamic_pointer_cast<MatrixWorkspace>(outputWS);
} catch (const std::exception &ex) {
g_log.error(ex.what());
QMessageBox::critical(
this, "Loading failed",
"Unable to load one of the files.\n\nCheck log for details");
} catch (...) {
QMessageBox::critical(
this, "Loading failed",
"Unable to load one of the files.\n\nCheck log for details");
break;
}
const std::string runTitle = getRunTitle(ws);
const std::string wsBaseName = labelGroupName + "_" + runTitle;
IFunction_sptr functionToFit;
if (useInitFitFunction)
// Create a copy so that the original function is not changed
functionToFit = FunctionFactory::Instance().createInitialized(
fitFunction->asString());
else
// Use the same function over and over, so that previous fitted params are
// used for the next fit
functionToFit = fitFunction;
IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit");
fit->setRethrows(true);
// Set function. Gets updated when fit is done.
fit->setProperty("Function", functionToFit);
fit->setProperty("InputWorkspace", ws);
fit->setProperty("Output", wsBaseName);
// We should have one spectrum only in the workspace, so use the first
// one.
fit->setProperty("WorkspaceIndex", 0);
// Various properties from the fit prop. browser
fit->setProperty("StartX", m_fitPropBrowser->startX());
fit->setProperty("EndX", m_fitPropBrowser->endX());
fit->setProperty("Minimizer", m_fitPropBrowser->minimizer());
fit->setProperty("CostFunction", m_fitPropBrowser->costFunction());
fit->execute();
} catch (...) {
QMessageBox::critical(
this, "Fitting failed",
"Unable to fit one of the files.\n\nCheck log for details");
break;
}
// Make sure created fit workspaces end up in the group
// TODO: this really should use loop
ads.addToGroup(labelGroupName, wsBaseName + "_NormalisedCovarianceMatrix");
ads.addToGroup(labelGroupName, wsBaseName + "_Parameters");
ads.addToGroup(labelGroupName, wsBaseName + "_Workspace");
// Copy log values
auto fitWs = ads.retrieveWS<MatrixWorkspace>(wsBaseName + "_Workspace");
fitWs->copyExperimentInfoFrom(ws.get());
// Add information about the fit to the diagnosis table
addDiagnosisEntry(runTitle, fit->getProperty("OutputChi2OverDof"),
functionToFit);
// Update progress
m_ui.progress->setFormat("%p% - " + QString::fromStdString(runTitle));
m_ui.progress->setValue(m_ui.progress->value() + 1);
setState(Stopped);
}
/**
* Stop fitting process.
*/
void MuonSequentialFitDialog::stopFit() {
if (m_state == Stopped)
throw std::runtime_error("Couldn't stop: is not running");
m_stopRequested = true;
}
} // namespace CustomInterfaces