-
Notifications
You must be signed in to change notification settings - Fork 122
/
Fury.cpp
342 lines (273 loc) · 11.3 KB
/
Fury.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
#include "MantidQtCustomInterfaces/Indirect/Fury.h"
#include "MantidQtCustomInterfaces/UserInputValidator.h"
#include "MantidQtMantidWidgets/RangeSelector.h"
#include <QFileInfo>
#include <qwt_plot.h>
#include <boost/lexical_cast.hpp>
#include <cmath>
namespace
{
Mantid::Kernel::Logger g_log("Fury");
}
using namespace Mantid::API;
namespace MantidQt
{
namespace CustomInterfaces
{
namespace IDA
{
Fury::Fury(QWidget * parent) : IDATab(parent),
m_furTree(NULL),
m_furyResFileType()
{
m_uiForm.setupUi(parent);
}
void Fury::setup()
{
m_furTree = new QtTreePropertyBrowser();
m_uiForm.properties->addWidget(m_furTree);
// Create and configure properties
m_properties["ELow"] = m_dblManager->addProperty("ELow");
m_dblManager->setDecimals(m_properties["ELow"], NUM_DECIMALS);
m_properties["EWidth"] = m_dblManager->addProperty("EWidth");
m_dblManager->setDecimals(m_properties["EWidth"], NUM_DECIMALS);
m_properties["EWidth"]->setEnabled(false);
m_properties["EHigh"] = m_dblManager->addProperty("EHigh");
m_dblManager->setDecimals(m_properties["EHigh"], NUM_DECIMALS);
m_properties["SampleBinning"] = m_dblManager->addProperty("SampleBinning");
m_dblManager->setDecimals(m_properties["SampleBinning"], 0);
m_properties["SampleBins"] = m_dblManager->addProperty("SampleBins");
m_dblManager->setDecimals(m_properties["SampleBins"], 0);
m_properties["SampleBins"]->setEnabled(false);
m_properties["ResolutionBins"] = m_dblManager->addProperty("ResolutionBins");
m_dblManager->setDecimals(m_properties["ResolutionBins"], 0);
m_properties["ResolutionBins"]->setEnabled(false);
m_furTree->addProperty(m_properties["ELow"]);
m_furTree->addProperty(m_properties["EWidth"]);
m_furTree->addProperty(m_properties["EHigh"]);
m_furTree->addProperty(m_properties["SampleBinning"]);
m_furTree->addProperty(m_properties["SampleBins"]);
m_furTree->addProperty(m_properties["ResolutionBins"]);
m_dblManager->setValue(m_properties["SampleBinning"], 10);
m_furTree->setFactoryForManager(m_dblManager, m_dblEdFac);
auto xRangeSelector = m_uiForm.ppPlot->addRangeSelector("FuryRange");
// signals / slots & validators
connect(xRangeSelector, SIGNAL(selectionChangedLazy(double, double)), this, SLOT(rsRangeChangedLazy(double, double)));
connect(m_dblManager, SIGNAL(valueChanged(QtProperty*, double)), this, SLOT(updateRS(QtProperty*, double)));
connect(m_dblManager, SIGNAL(valueChanged(QtProperty*, double)), this, SLOT(updatePropertyValues(QtProperty*, double)));
connect(m_uiForm.dsInput, SIGNAL(dataReady(const QString&)), this, SLOT(plotInput(const QString&)));
connect(m_uiForm.dsResolution, SIGNAL(dataReady(const QString&)), this, SLOT(calculateBinning()));
}
void Fury::run()
{
using namespace Mantid::API;
calculateBinning();
QString wsName = m_uiForm.dsInput->getCurrentDataName();
QString resName = m_uiForm.dsResolution->getCurrentDataName();
double energyMin = m_dblManager->value(m_properties["ELow"]);
double energyMax = m_dblManager->value(m_properties["EHigh"]);
double numBins = m_dblManager->value(m_properties["SampleBinning"]);
bool plot = m_uiForm.ckPlot->isChecked();
bool save = m_uiForm.ckSave->isChecked();
IAlgorithm_sptr furyAlg = AlgorithmManager::Instance().create("TransformToIqt", -1);
furyAlg->initialize();
furyAlg->setProperty("SampleWorkspace", wsName.toStdString());
furyAlg->setProperty("ResolutionWorkspace", resName.toStdString());
furyAlg->setProperty("EnergyMin", energyMin);
furyAlg->setProperty("EnergyMax", energyMax);
furyAlg->setProperty("BinReductionFactor", numBins);
furyAlg->setProperty("Plot", plot);
furyAlg->setProperty("Save", save);
furyAlg->setProperty("DryRun", false);
runAlgorithm(furyAlg);
// Set the result workspace for Python script export
QString sampleName = m_uiForm.dsInput->getCurrentDataName();
m_pythonExportWsName = sampleName.left(sampleName.lastIndexOf("_")).toStdString() + "_iqt";
}
/**
* Ensure we have present and valid file/ws inputs.
*
* The underlying Fourier transform of Fury
* also means we must enforce several rules on the parameters.
*/
bool Fury::validate()
{
UserInputValidator uiv;
uiv.checkDataSelectorIsValid("Sample", m_uiForm.dsInput);
uiv.checkDataSelectorIsValid("Resolution", m_uiForm.dsResolution);
QString message = uiv.generateErrorMessage();
showMessageBox(message);
return message.isEmpty();
}
/**
* Ensures that absolute min and max energy are equal.
*
* @param prop Qt property that was changed
* @param val New value of that property
*/
void Fury::updatePropertyValues(QtProperty *prop, double val)
{
disconnect(m_dblManager, SIGNAL(valueChanged(QtProperty*, double)), this, SLOT(updatePropertyValues(QtProperty*, double)));
if(prop == m_properties["EHigh"])
{
// If the user enters a negative value for EHigh assume they did not mean to add a -
if(val < 0)
{
val = -val;
m_dblManager->setValue(m_properties["EHigh"], val);
}
m_dblManager->setValue(m_properties["ELow"], -val);
}
else if(prop == m_properties["ELow"])
{
// If the user enters a positive value for ELow, assume they ment to add a
if(val > 0)
{
val = -val;
m_dblManager->setValue(m_properties["ELow"], val);
}
m_dblManager->setValue(m_properties["EHigh"], -val);
}
connect(m_dblManager, SIGNAL(valueChanged(QtProperty*, double)), this, SLOT(updatePropertyValues(QtProperty*, double)));
calculateBinning();
}
/**
* Calculates binning parameters.
*/
void Fury::calculateBinning()
{
using namespace Mantid::API;
disconnect(m_dblManager, SIGNAL(valueChanged(QtProperty*, double)), this, SLOT(updatePropertyValues(QtProperty*, double)));
QString wsName = m_uiForm.dsInput->getCurrentDataName();
QString resName = m_uiForm.dsResolution->getCurrentDataName();
if(wsName.isEmpty() || resName.isEmpty())
return;
double energyMin = m_dblManager->value(m_properties["ELow"]);
double energyMax = m_dblManager->value(m_properties["EHigh"]);
double numBins = m_dblManager->value(m_properties["SampleBinning"]);
if(numBins == 0)
return;
IAlgorithm_sptr furyAlg = AlgorithmManager::Instance().create("TransformToIqt");
furyAlg->initialize();
furyAlg->setProperty("SampleWorkspace", wsName.toStdString());
furyAlg->setProperty("ResolutionWorkspace", resName.toStdString());
furyAlg->setProperty("ParameterWorkspace", "__FuryProperties_temp");
furyAlg->setProperty("EnergyMin", energyMin);
furyAlg->setProperty("EnergyMax", energyMax);
furyAlg->setProperty("BinReductionFactor", numBins);
furyAlg->setProperty("Plot", false);
furyAlg->setProperty("Save", false);
furyAlg->setProperty("DryRun", true);
furyAlg->execute();
// Get property table from algorithm
ITableWorkspace_sptr propsTable = AnalysisDataService::Instance().retrieveWS<ITableWorkspace>("__FuryProperties_temp");
// Get data from property table
double energyWidth = propsTable->getColumn("EnergyWidth")->cell<float>(0);
int sampleBins = propsTable->getColumn("SampleOutputBins")->cell<int>(0);
int resolutionBins = propsTable->getColumn("ResolutionBins")->cell<int>(0);
// Update data in property editor
m_dblManager->setValue(m_properties["EWidth"], energyWidth);
m_dblManager->setValue(m_properties["ResolutionBins"], resolutionBins);
m_dblManager->setValue(m_properties["SampleBins"], sampleBins);
connect(m_dblManager, SIGNAL(valueChanged(QtProperty*, double)), this, SLOT(updatePropertyValues(QtProperty*, double)));
// Warn for low number of resolution bins
int numResolutionBins = static_cast<int>(m_dblManager->value(m_properties["ResolutionBins"]));
if(numResolutionBins < 5)
showMessageBox("Number of resolution bins is less than 5.\nResults may be inaccurate.");
}
void Fury::loadSettings(const QSettings & settings)
{
m_uiForm.dsInput->readSettings(settings.group());
m_uiForm.dsResolution->readSettings(settings.group());
}
void Fury::plotInput(const QString& wsname)
{
MatrixWorkspace_sptr workspace;
try
{
workspace = Mantid::API::AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(wsname.toStdString());
}
catch(Mantid::Kernel::Exception::NotFoundError&)
{
showMessageBox(QString("Unable to retrieve workspace: " + wsname));
return;
}
m_uiForm.ppPlot->clear();
m_uiForm.ppPlot->addSpectrum("Sample", workspace, 0);
auto xRangeSelector = m_uiForm.ppPlot->getRangeSelector("FuryRange");
try
{
QPair<double, double> range = m_uiForm.ppPlot->getCurveRange("Sample");
double rounded_min(range.first);
double rounded_max(range.second);
const std::string instrName(workspace->getInstrument()->getName());
if(instrName == "BASIS")
{
xRangeSelector->setRange(range.first, range.second);
m_dblManager->setValue(m_properties["ELow"], rounded_min);
m_dblManager->setValue(m_properties["EHigh"], rounded_max);
m_dblManager->setValue(m_properties["EWidth"], 0.0004);
m_dblManager->setValue(m_properties["SampleBinning"], 1);
}
else
{
rounded_min = floor(rounded_min * 10 + 0.5) / 10.0;
rounded_max = floor(rounded_max * 10 + 0.5) / 10.0;
//corrections for if nearest value is outside of range
if (rounded_max > range.second)
{
rounded_max -= 0.1;
}
if(rounded_min < range.first)
{
rounded_min += 0.1;
}
//check incase we have a really small range
if (fabs(rounded_min) > 0 && fabs(rounded_max) > 0)
{
xRangeSelector->setRange(rounded_min, rounded_max);
m_dblManager->setValue(m_properties["ELow"], rounded_min);
m_dblManager->setValue(m_properties["EHigh"], rounded_max);
}
else
{
xRangeSelector->setRange(range.first, range.second);
m_dblManager->setValue(m_properties["ELow"], range.first);
m_dblManager->setValue(m_properties["EHigh"], range.second);
}
//set default value for width
m_dblManager->setValue(m_properties["EWidth"], 0.005);
}
}
catch(std::invalid_argument & exc)
{
showMessageBox(exc.what());
}
calculateBinning();
}
/**
* Updates the range selectors and properties when range selector is moved.
*
* @param min Range selector min value
* @param max Range selector amx value
*/
void Fury::rsRangeChangedLazy(double min, double max)
{
double oldMin = m_dblManager->value(m_properties["ELow"]);
double oldMax = m_dblManager->value(m_properties["EHigh"]);
if(fabs(oldMin - min) > 0.0000001)
m_dblManager->setValue(m_properties["ELow"], min);
if(fabs(oldMax - max) > 0.0000001)
m_dblManager->setValue(m_properties["EHigh"], max);
}
void Fury::updateRS(QtProperty* prop, double val)
{
auto xRangeSelector = m_uiForm.ppPlot->getRangeSelector("FuryRange");
if(prop == m_properties["ELow"])
xRangeSelector->setMinimum(val);
else if(prop == m_properties["EHigh"])
xRangeSelector->setMaximum(val);
}
} // namespace IDA
} // namespace CustomInterfaces
} // namespace MantidQt