-
Notifications
You must be signed in to change notification settings - Fork 122
/
ApplyCorr.cpp
386 lines (315 loc) · 12.2 KB
/
ApplyCorr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
183
184
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
211
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "MantidQtCustomInterfaces/Indirect/ApplyCorr.h"
#include "MantidQtCustomInterfaces/UserInputValidator.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/TextAxis.h"
#include <QStringList>
using namespace Mantid::API;
namespace
{
Mantid::Kernel::Logger g_log("ApplyCorr");
}
namespace MantidQt
{
namespace CustomInterfaces
{
namespace IDA
{
ApplyCorr::ApplyCorr(QWidget * parent) :
IDATab(parent)
{
m_uiForm.setupUi(parent);
connect(m_batchAlgoRunner, SIGNAL(batchComplete(bool)), this, SLOT(algorithmComplete(bool)));
connect(m_uiForm.cbGeometry, SIGNAL(currentIndexChanged(int)), this, SLOT(handleGeometryChange(int)));
connect(m_uiForm.dsSample, SIGNAL(dataReady(const QString&)), this, SLOT(newData(const QString&)));
connect(m_uiForm.spPreviewSpec, SIGNAL(valueChanged(int)), this, SLOT(plotPreview(int)));
m_uiForm.spPreviewSpec->setMinimum(0);
m_uiForm.spPreviewSpec->setMaximum(0);
}
void ApplyCorr::setup()
{
}
/**
* Disables corrections when using S(Q, w) as input data.
*
* @param dataName Name of new data source
*/
void ApplyCorr::newData(const QString &dataName)
{
const MatrixWorkspace_sptr sampleWs = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(dataName.toStdString());
m_uiForm.spPreviewSpec->setMaximum(static_cast<int>(sampleWs->getNumberHistograms()) - 1);
// Plot the sample curve
m_uiForm.ppPreview->clear();
m_uiForm.ppPreview->addSpectrum("Sample", sampleWs, 0, Qt::black);
}
void ApplyCorr::run()
{
API::BatchAlgorithmRunner::AlgorithmRuntimeProps absCorProps;
IAlgorithm_sptr applyCorrAlg = AlgorithmManager::Instance().create("ApplyPaalmanPingsCorrection");
applyCorrAlg->initialize();
QString sampleWsName = m_uiForm.dsSample->getCurrentDataName();
MatrixWorkspace_sptr sampleWs = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(sampleWsName.toStdString());
Mantid::Kernel::Unit_sptr sampleXUnit = sampleWs->getAxis(0)->unit();
// If not in wavelength then do conversion
if(sampleXUnit->caption() != "Wavelength")
{
g_log.information("Sample workspace not in wavelength, need to convert to continue.");
absCorProps["SampleWorkspace"] = addUnitConversionStep(sampleWs);
}
else
{
absCorProps["SampleWorkspace"] = sampleWsName.toStdString();
}
bool useCan = m_uiForm.ckUseCan->isChecked();
if(useCan)
{
QString canWsName = m_uiForm.dsContainer->getCurrentDataName();
MatrixWorkspace_sptr canWs = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(canWsName.toStdString());
// If not in wavelength then do conversion
Mantid::Kernel::Unit_sptr canXUnit = canWs->getAxis(0)->unit();
if(canXUnit->caption() != "Wavelength")
{
g_log.information("Container workspace not in wavelength, need to convert to continue.");
absCorProps["CanWorkspace"] = addUnitConversionStep(canWs);
}
else
{
absCorProps["CanWorkspace"] = canWsName.toStdString();
}
bool useCanScale = m_uiForm.ckScaleCan->isChecked();
if(useCanScale)
{
double canScaleFactor = m_uiForm.spCanScale->value();
applyCorrAlg->setProperty("CanScaleFactor", canScaleFactor);
}
}
bool useCorrections = m_uiForm.ckUseCorrections->isChecked();
if(useCorrections)
{
QString correctionsWsName = m_uiForm.dsCorrections->getCurrentDataName();
WorkspaceGroup_sptr corrections = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(correctionsWsName.toStdString());
bool interpolateAll = false;
for(size_t i = 0; i < corrections->size(); i++)
{
MatrixWorkspace_sptr factorWs = boost::dynamic_pointer_cast<MatrixWorkspace>(corrections->getItem(i));
// Check for matching binning
if(sampleWs && (sampleWs->blocksize() != factorWs->blocksize()))
{
int result;
if(interpolateAll)
{
result = QMessageBox::Yes;
}
else
{
QString text = "Number of bins on sample and "
+ QString::fromStdString(factorWs->name())
+ " workspace does not match.\n"
+ "Would you like to interpolate this workspace to match the sample?";
result = QMessageBox::question(NULL, tr("Interpolate corrections?"), tr(text),
QMessageBox::YesToAll, QMessageBox::Yes, QMessageBox::No);
}
switch(result)
{
case QMessageBox::YesToAll:
interpolateAll = true;
case QMessageBox::Yes:
addInterpolationStep(factorWs, absCorProps["SampleWorkspace"]);
break;
default:
m_batchAlgoRunner->clearQueue();
g_log.error("ApplyCorr cannot run with corrections that do not match sample binning.");
return;
}
}
}
applyCorrAlg->setProperty("CorrectionsWorkspace", correctionsWsName.toStdString());
}
// Generate output workspace name
int nameCutIndex = sampleWsName.lastIndexOf("_");
if(nameCutIndex == -1)
nameCutIndex = sampleWsName.length();
QString correctionType;
switch(m_uiForm.cbGeometry->currentIndex())
{
case 0:
correctionType = "flt";
break;
case 1:
correctionType = "cyl";
break;
}
QString outputWsName = sampleWsName.left(nameCutIndex) + + "_" + correctionType + "_Corrected";
applyCorrAlg->setProperty("OutputWorkspace", outputWsName.toStdString());
// Run the corrections algorithm
m_batchAlgoRunner->addAlgorithm(applyCorrAlg, absCorProps);
m_batchAlgoRunner->executeBatchAsync();
// Set the result workspace for Python script export
m_pythonExportWsName = outputWsName.toStdString();
}
/**
* Adds a unit converstion step to the batch algorithm queue.
*
* @param ws Pointer to the workspace to convert
* @return Name of output workspace
*/
std::string ApplyCorr::addUnitConversionStep(MatrixWorkspace_sptr ws)
{
std::string outputName = ws->name() + "_inWavelength";
IAlgorithm_sptr convertAlg = AlgorithmManager::Instance().create("ConvertUnits");
convertAlg->initialize();
convertAlg->setProperty("InputWorkspace", ws->name());
convertAlg->setProperty("OutputWorkspace", outputName);
convertAlg->setProperty("Target", "Wavelength");
convertAlg->setProperty("EMode", getEMode(ws));
convertAlg->setProperty("EFixed", getEFixed(ws));
m_batchAlgoRunner->addAlgorithm(convertAlg);
return outputName;
}
/**
* Adds a spline interpolation as a step in the calculation for using legacy correction factor
* workspaces.
*
* @param toInterpolate Pointer to the workspace to interpolate
* @param toMatch Name of the workspace to match
*/
void ApplyCorr::addInterpolationStep(MatrixWorkspace_sptr toInterpolate, std::string toMatch)
{
API::BatchAlgorithmRunner::AlgorithmRuntimeProps interpolationProps;
interpolationProps["WorkspaceToMatch"] = toMatch;
IAlgorithm_sptr interpolationAlg = AlgorithmManager::Instance().create("SplineInterpolation");
interpolationAlg->initialize();
interpolationAlg->setProperty("WorkspaceToInterpolate", toInterpolate->name());
interpolationAlg->setProperty("OutputWorkspace", toInterpolate->name());
m_batchAlgoRunner->addAlgorithm(interpolationAlg, interpolationProps);
}
/**
* Handles completion of the algorithm.
*
* @param error True if algorithm failed.
*/
void ApplyCorr::algorithmComplete(bool error)
{
if(error)
{
emit showMessageBox("Unable to apply corrections.\nSee Results Log for more details.");
return;
}
m_outputWs = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_pythonExportWsName);
plotPreview(m_uiForm.spPreviewSpec->value());
}
bool ApplyCorr::validate()
{
UserInputValidator uiv;
uiv.checkDataSelectorIsValid("Sample", m_uiForm.dsSample);
MatrixWorkspace_sptr sampleWs;
bool useCan = m_uiForm.ckUseCan->isChecked();
bool useCorrections = m_uiForm.ckUseCorrections->isChecked();
if(!(useCan || useCorrections))
uiv.addErrorMessage("Must use either container subtraction or corrections");
if(useCan)
{
uiv.checkDataSelectorIsValid("Container", m_uiForm.dsContainer);
// Check can and sample workspaces are the same "type" (reduced or S(Q, w))
QString sample = m_uiForm.dsSample->getCurrentDataName();
QString sampleType = sample.right(sample.length() - sample.lastIndexOf("_"));
QString container = m_uiForm.dsContainer->getCurrentDataName();
QString containerType = container.right(container.length() - container.lastIndexOf("_"));
g_log.debug() << "Sample type is: " << sampleType.toStdString() << std::endl;
g_log.debug() << "Can type is: " << containerType.toStdString() << std::endl;
if(containerType != sampleType)
uiv.addErrorMessage("Sample and can workspaces must contain the same type of data.");
}
if(useCorrections)
{
uiv.checkDataSelectorIsValid("Corrections", m_uiForm.dsCorrections);
QString correctionsWsName = m_uiForm.dsCorrections->getCurrentDataName();
WorkspaceGroup_sptr corrections = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(correctionsWsName.toStdString());
for(size_t i = 0; i < corrections->size(); i++)
{
// Check it is a MatrixWorkspace
MatrixWorkspace_sptr factorWs = boost::dynamic_pointer_cast<MatrixWorkspace>(corrections->getItem(i));
if(!factorWs)
{
QString msg = "Correction factor workspace "
+ QString::number(i)
+ " is not a MatrixWorkspace";
uiv.addErrorMessage(msg);
continue;
}
// Check X unit is wavelength
Mantid::Kernel::Unit_sptr xUnit = factorWs->getAxis(0)->unit();
if(xUnit->caption() != "Wavelength")
{
QString msg = "Correction factor workspace "
+ QString::fromStdString(factorWs->name())
+ " is not in wavelength";
uiv.addErrorMessage(msg);
}
}
}
// Show errors if there are any
if(!uiv.isAllInputValid())
emit showMessageBox(uiv.generateErrorMessage());
return uiv.isAllInputValid();
}
void ApplyCorr::loadSettings(const QSettings & settings)
{
m_uiForm.dsCorrections->readSettings(settings.group());
m_uiForm.dsContainer->readSettings(settings.group());
m_uiForm.dsSample->readSettings(settings.group());
}
/**
* Handles when the type of geometry changes
*
* Updates the file extension to search for
*/
void ApplyCorr::handleGeometryChange(int index)
{
QString ext("");
switch(index)
{
case 0:
// Geomtry is flat
ext = "_flt_abs";
m_uiForm.dsCorrections->setWSSuffixes(QStringList(ext));
m_uiForm.dsCorrections->setFBSuffixes(QStringList(ext + ".nxs"));
break;
case 1:
// Geomtry is cylinder
ext = "_cyl_abs";
m_uiForm.dsCorrections->setWSSuffixes(QStringList(ext));
m_uiForm.dsCorrections->setFBSuffixes(QStringList(ext + ".nxs"));
break;
}
}
/**
* Replots the preview plot.
*
* @param specIndex Spectrum index to plot
*/
void ApplyCorr::plotPreview(int specIndex)
{
bool useCan = m_uiForm.ckUseCan->isChecked();
m_uiForm.ppPreview->clear();
// Plot sample
const QString sample = m_uiForm.dsSample->getCurrentDataName();
if(AnalysisDataService::Instance().doesExist(sample.toStdString()))
{
m_uiForm.ppPreview->addSpectrum("Sample", sample, specIndex, Qt::black);
}
// Plot result
if(m_outputWs)
{
m_uiForm.ppPreview->addSpectrum("Corrected", m_outputWs, specIndex, Qt::green);
}
// Plot can
if(useCan)
{
QString container = m_uiForm.dsContainer->getCurrentDataName();
const MatrixWorkspace_sptr canWs = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(container.toStdString());
m_uiForm.ppPreview->addSpectrum("Can", canWs, specIndex, Qt::red);
}
}
} // namespace IDA
} // namespace CustomInterfaces
} // namespace MantidQt