-
Notifications
You must be signed in to change notification settings - Fork 122
/
ExtractQENSMembers.cpp
337 lines (297 loc) · 12.4 KB
/
ExtractQENSMembers.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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidWorkflowAlgorithms/ExtractQENSMembers.h"
#include "MantidAPI/ADSValidator.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidKernel/ArrayProperty.h"
#include <boost/numeric/conversion/cast.hpp>
namespace {
Mantid::Kernel::Logger g_log("ExtractQENSMembers");
}
namespace Mantid {
namespace Algorithms {
using namespace API;
using namespace Kernel;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ExtractQENSMembers)
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string ExtractQENSMembers::name() const {
return "ExtractQENSMembers";
}
/// Algorithm's version for identification. @see Algorithm::version
int ExtractQENSMembers::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string ExtractQENSMembers::category() const {
return "Workflow\\MIDAS";
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string ExtractQENSMembers::summary() const {
return "Extracts the fit members from a QENS fit";
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ExtractQENSMembers::init() {
declareProperty(
std::make_unique<WorkspaceProperty<>>(
"InputWorkspace", "", Direction::Input, PropertyMode::Optional),
"The input workspace used in the fit. Ignored if 'InputWorkspaces' "
"property is provided.");
declareProperty(
std::make_unique<ArrayProperty<std::string>>("InputWorkspaces", ""),
"List of the workspaces used in the fit.");
declareProperty(std::make_unique<WorkspaceProperty<WorkspaceGroup>>(
"ResultWorkspace", "", Direction::Input),
"The result group workspace produced in a QENS fit.");
declareProperty("RenameConvolvedMembers", false,
"If true, renames the n-th 'Convolution' member, to the n-th "
"supplied name in the ConvolvedMembers property.");
declareProperty(
std::make_unique<ArrayProperty<std::string>>("ConvolvedMembers"),
"A list of the names of the members which were convolved "
"before being output by the fit routine. These must be "
"provided in the same order as originally provided to the "
"fit.");
declareProperty(std::make_unique<WorkspaceProperty<WorkspaceGroup>>(
"OutputWorkspace", "", Direction::Output),
"The output workspace group, containing the fit members.");
}
std::map<std::string, std::string> ExtractQENSMembers::validateInputs() {
std::map<std::string, std::string> errors;
std::vector<std::string> workspaceNames = getProperty("InputWorkspaces");
MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
if (workspaceNames.empty() && !inputWorkspace)
errors["InputWorkspace"] = "Neither the InputWorkspace or InputWorkspaces "
"property have been defined.";
return errors;
}
void ExtractQENSMembers::exec() {
auto inputWorkspaces = getInputWorkspaces();
WorkspaceGroup_sptr resultWS = getProperty("ResultWorkspace");
MatrixWorkspace_sptr initialWS =
std::dynamic_pointer_cast<MatrixWorkspace>(resultWS->getItem(0));
const auto qValues = getQValues(inputWorkspaces);
auto members = getAxisLabels(initialWS, 1);
bool renameConvolved = getProperty("RenameConvolvedMembers");
if (renameConvolved)
members = renameConvolvedMembers(members, getProperty("ConvolvedMembers"));
auto memberWorkspaces = createMembersWorkspaces(initialWS, members);
for (auto i = 1u; i < resultWS->size(); ++i)
appendToMembers(
std::dynamic_pointer_cast<MatrixWorkspace>(resultWS->getItem(i)),
memberWorkspaces);
setNumericAxis(memberWorkspaces, qValues, 1);
std::string outputWSName = getProperty("OutputWorkspace");
auto workspaceNames =
addMembersToADS(members, memberWorkspaces, outputWSName);
setProperty("OutputWorkspace", groupWorkspaces(workspaceNames));
}
std::vector<MatrixWorkspace_sptr>
ExtractQENSMembers::getInputWorkspaces() const {
const std::vector<std::string> workspaceNames =
getProperty("InputWorkspaces");
std::vector<MatrixWorkspace_sptr> workspaces;
if (!workspaceNames.empty()) {
workspaces.reserve(workspaceNames.size());
auto &ADS = AnalysisDataService::Instance();
for (const auto &name : workspaceNames)
workspaces.emplace_back(ADS.retrieveWS<MatrixWorkspace>(name));
} else
workspaces.emplace_back(getProperty("InputWorkspace"));
return workspaces;
}
/**
* Extracts the Q-Values from the specified workspace.
*
* @param workspaces The workspaces whose Q-Values to extract.
* @return The extracted Q-Values.
*/
std::vector<double> ExtractQENSMembers::getQValues(
const std::vector<MatrixWorkspace_sptr> &workspaces) {
std::vector<double> qValues;
for (const auto &workspace : workspaces) {
auto getQs = createChildAlgorithm("GetQsInQENSData", -1.0, -1.0, false);
getQs->setProperty("InputWorkspace", workspace);
getQs->executeAsChildAlg();
const std::vector<double> values = getQs->getProperty("Qvalues");
qValues.insert(std::end(qValues), std::begin(values), std::end(values));
}
return qValues;
}
/**
* Retrieves the axis labels from the axis with the specified index, in the
* specified workspace.
*
* @param workspace The workspace whose axis labels to retrieve.
* @param axisIndex The index of the axis whose labels retrieve.
* @return The retrieved axis labels.
*/
std::vector<std::string>
ExtractQENSMembers::getAxisLabels(const MatrixWorkspace_sptr &workspace,
size_t axisIndex) const {
auto axis = workspace->getAxis(axisIndex);
std::vector<std::string> labels;
labels.reserve(axis->length());
for (auto i = 0u; i < axis->length(); ++i)
labels.emplace_back(axis->label(i));
return labels;
}
/**
* Renames the convolved members in the specified vector of members, to the
* respective names in the specified new names vector.
*
* @param members A vector of the members.
* @param newNames The names to use in renaming.
* @return A vector of the members, with the convolved members renamed.
*/
std::vector<std::string> ExtractQENSMembers::renameConvolvedMembers(
const std::vector<std::string> &members,
const std::vector<std::string> &newNames) const {
std::vector<std::string> newMembers;
newMembers.reserve(members.size());
auto index = 0u;
for (const auto &member : members) {
if (member == "Convolution" && index < newNames.size())
newMembers.emplace_back(newNames[index++]);
else
newMembers.emplace_back(member);
}
return newMembers;
}
/**
* Extracts the specified spectrum from the specified spectrum.
*
* @param inputWS The workspace whose spectrum to extract.
* @param spectrum The spectrum to extract.
* @return A workspace containing the extracted spectrum.
*/
MatrixWorkspace_sptr
ExtractQENSMembers::extractSpectrum(const MatrixWorkspace_sptr &inputWS,
size_t spectrum) {
auto extractAlg = createChildAlgorithm("ExtractSpectra", -1.0, -1.0, false);
extractAlg->setProperty("InputWorkspace", inputWS);
extractAlg->setProperty("OutputWorkspace", "__extracted");
extractAlg->setProperty("StartWorkspaceIndex",
boost::numeric_cast<int>(spectrum));
extractAlg->setProperty("EndWorkspaceIndex",
boost::numeric_cast<int>(spectrum));
extractAlg->executeAsChildAlg();
return extractAlg->getProperty("OutputWorkspace");
}
/**
* Appends the spectra of a specified workspace to another specified input
* workspace.
*
* @param inputWS The input workspace to append to.
* @param spectraWorkspace The workspace whose spectra to append.
*/
MatrixWorkspace_sptr ExtractQENSMembers::appendSpectra(
const MatrixWorkspace_sptr &inputWS,
const MatrixWorkspace_sptr &spectraWorkspace) {
auto appendAlg = createChildAlgorithm("AppendSpectra", -1.0, -1.0, false);
appendAlg->setProperty("InputWorkspace1", inputWS);
appendAlg->setProperty("InputWorkspace2", spectraWorkspace);
appendAlg->setProperty("OutputWorkspace", inputWS);
appendAlg->executeAsChildAlg();
return appendAlg->getProperty("OutputWorkspace");
}
/**
* Groups the workspaces with the specified name.
*
* @param workspaceNames A vector of the names of the workspaces to group.
* @return The group workspace.
*/
WorkspaceGroup_sptr ExtractQENSMembers::groupWorkspaces(
const std::vector<std::string> &workspaceNames) {
auto groupAlg = createChildAlgorithm("GroupWorkspaces", -1.0, -1.0, false);
groupAlg->setProperty("InputWorkspaces", workspaceNames);
groupAlg->setProperty("OutputWorkspace", "__grouped");
groupAlg->execute();
return groupAlg->getProperty("OutputWorkspace");
}
/**
* Creates the member workspaces from an initial result workspace and the member
* names.
*
* @param initialWS The initial result workspace.
* @param members A vector of the member names.
* @return A vector of the created members workspaces.
*/
std::vector<MatrixWorkspace_sptr> ExtractQENSMembers::createMembersWorkspaces(
const MatrixWorkspace_sptr &initialWS,
const std::vector<std::string> &members) {
std::vector<MatrixWorkspace_sptr> memberWorkspaces;
memberWorkspaces.reserve(members.size());
for (auto i = 0u; i < members.size(); ++i)
memberWorkspaces.emplace_back(extractSpectrum(initialWS, i));
return memberWorkspaces;
}
/**
* Appends the n-th spectra in the specified result workspace to the n-th
* specified member workspace.
*
* @param resultWS The result workspace.
* @param members A vector containing the member workspaces.
*/
void ExtractQENSMembers::appendToMembers(
const MatrixWorkspace_sptr &resultWS,
std::vector<Mantid::API::MatrixWorkspace_sptr> &members) {
for (auto i = 0u; i < members.size(); ++i)
members[i] = appendSpectra(members[i], extractSpectrum(resultWS, i));
}
/**
* Creates and sets a numeric axis, filled with the specified values, on each of
* the specified workspaces at the specified axis index.
*
* @param workspaces The workspaces whose axis to set.
* @param values The values used to create the numeric axis.
* @param axisIndex The index of the axis to set.
*/
void ExtractQENSMembers::setNumericAxis(
const std::vector<MatrixWorkspace_sptr> &workspaces,
const std::vector<double> &values, size_t axisIndex) const {
auto qAxis = NumericAxis(values.size());
for (auto i = 0u; i < values.size(); ++i)
qAxis.setValue(i, values[i]);
for (auto &workspace : workspaces) {
workspace->replaceAxis(axisIndex, std::make_unique<NumericAxis>(qAxis));
workspace->setYUnitLabel("MomentumTransfer");
}
}
/**
* Adds the specified member workspaces to the analysis data service.
*
* @param members The name of the members; corresponds to each member
* workspace.
* @param memberWorkspaces The member workspaces.
* @param outputWSName The name of the output workspace; used as a prefix.
* @return A vector containing the names of the added
* workspaces.
*/
std::vector<std::string> ExtractQENSMembers::addMembersToADS(
const std::vector<std::string> &members,
const std::vector<Mantid::API::MatrixWorkspace_sptr> &memberWorkspaces,
const std::string &outputWSName) {
std::unordered_map<std::string, size_t> nameCounts;
std::vector<std::string> workspaceNames;
workspaceNames.reserve(members.size());
for (auto i = 0u; i < members.size(); ++i) {
std::string count = "";
if (nameCounts.find(members[i]) == nameCounts.end())
nameCounts[members[i]] = 1;
else
count = std::to_string(++nameCounts[members[i]]);
const auto name = outputWSName + "_" + members[i] + count;
AnalysisDataService::Instance().addOrReplace(name, memberWorkspaces[i]);
workspaceNames.emplace_back(name);
}
return workspaceNames;
}
} // namespace Algorithms
} // namespace Mantid