-
Notifications
You must be signed in to change notification settings - Fork 122
/
CreateWorkspace.cpp
292 lines (245 loc) · 11.8 KB
/
CreateWorkspace.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
// 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 "MantidAlgorithms/CreateWorkspace.h"
#include "MantidAPI/BinEdgeAxis.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/TextAxis.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidHistogramData/HistogramBuilder.h"
#include "MantidIndexing/GlobalSpectrumIndex.h"
#include "MantidIndexing/IndexInfo.h"
#include "MantidIndexing/SpectrumIndexSet.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/InvisibleProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/UnitFactory.h"
namespace Mantid {
namespace Algorithms {
using namespace Kernel;
using namespace API;
using namespace DataObjects;
using namespace HistogramData;
using namespace Indexing;
DECLARE_ALGORITHM(CreateWorkspace)
/// Init function
void CreateWorkspace::init() {
std::vector<std::string> unitOptions = UnitFactory::Instance().getKeys();
unitOptions.emplace_back("SpectraNumber");
unitOptions.emplace_back("Text");
declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
"Name to be given to the created workspace.");
auto required = std::make_shared<MandatoryValidator<std::vector<double>>>();
declareProperty(std::make_unique<ArrayProperty<double>>("DataX", required), "X-axis data values for workspace.");
declareProperty(std::make_unique<ArrayProperty<double>>("DataY", required),
"Y-axis data values for workspace (measures).");
declareProperty(std::make_unique<ArrayProperty<double>>("DataE"), "Error values for workspace.");
declareProperty(std::make_unique<PropertyWithValue<int>>("NSpec", 1), "Number of spectra to divide data into.");
declareProperty("UnitX", "", "The unit to assign to the XAxis");
declareProperty("VerticalAxisUnit", "SpectraNumber", std::make_shared<StringListValidator>(unitOptions),
"The unit to assign to the second Axis (leave blank for "
"default Spectra number)");
declareProperty(std::make_unique<ArrayProperty<std::string>>("VerticalAxisValues"), "Values for the VerticalAxis.");
declareProperty(std::make_unique<PropertyWithValue<bool>>("Distribution", false),
"Whether OutputWorkspace should be marked as a distribution.");
declareProperty("YUnitLabel", "", "Label for Y Axis");
declareProperty("WorkspaceTitle", "", "Title for Workspace");
declareProperty(
std::make_unique<WorkspaceProperty<>>("ParentWorkspace", "", Direction::Input, PropertyMode::Optional),
"Name of a parent workspace.");
declareProperty(std::make_unique<ArrayProperty<double>>("Dx"), "X error values for workspace (optional).");
std::vector<std::string> propOptions{Parallel::toString(Parallel::StorageMode::Cloned),
Parallel::toString(Parallel::StorageMode::Distributed),
Parallel::toString(Parallel::StorageMode::MasterOnly)};
declareProperty("ParallelStorageMode", Parallel::toString(Parallel::StorageMode::Cloned),
std::make_shared<StringListValidator>(propOptions),
"The parallel storage mode of the output workspace for MPI builds");
setPropertySettings("ParallelStorageMode", std::make_unique<InvisibleProperty>());
}
/// Input validation
std::map<std::string, std::string> CreateWorkspace::validateInputs() {
std::map<std::string, std::string> issues;
const std::string vUnit = getProperty("VerticalAxisUnit");
const std::vector<std::string> vAxis = getProperty("VerticalAxisValues");
if (vUnit == "SpectraNumber" && !vAxis.empty())
issues["VerticalAxisValues"] = "Axis values cannot be provided when using a spectra axis";
return issues;
}
/// Exec function
void CreateWorkspace::exec() {
// Contortions to get at the vector in the property without copying it
const Property *const dataXprop = getProperty("DataX");
const Property *const dataYprop = getProperty("DataY");
const Property *const dataEprop = getProperty("DataE");
const Property *const errorDxprop = getProperty("Dx");
const ArrayProperty<double> *pCheck = nullptr;
pCheck = dynamic_cast<const ArrayProperty<double> *>(dataXprop);
if (!pCheck)
throw std::invalid_argument("DataX cannot be casted to a double vector");
const std::vector<double> &dataX = *pCheck;
pCheck = dynamic_cast<const ArrayProperty<double> *>(dataYprop);
if (!pCheck)
throw std::invalid_argument("DataY cannot be casted to a double vector");
const std::vector<double> &dataY = *pCheck;
pCheck = dynamic_cast<const ArrayProperty<double> *>(dataEprop);
if (!pCheck)
throw std::invalid_argument("DataE cannot be casted to a double vector");
const std::vector<double> &dataE = *pCheck;
pCheck = dynamic_cast<const ArrayProperty<double> *>(errorDxprop);
if (!pCheck)
throw std::invalid_argument("Dx cannot be casted to a double vector");
const std::vector<double> &dX = *pCheck;
const int nSpec = getProperty("NSpec");
const std::string xUnit = getProperty("UnitX");
const std::string vUnit = getProperty("VerticalAxisUnit");
const std::vector<std::string> vAxis = getProperty("VerticalAxisValues");
// Verify the size of the vertical axis.
const auto vAxisSize = static_cast<int>(vAxis.size());
if (vUnit != "SpectraNumber") {
// In the case of numerical axis, the vertical axis can represent either
// point data or bin edges.
if ((vUnit == "Text" && vAxisSize != nSpec) || (vAxisSize != nSpec && vAxisSize != nSpec + 1)) {
throw std::invalid_argument("The number of vertical axis values doesn't "
"match the number of histograms.");
}
}
// Verify length of vectors makes sense with NSpec
if ((dataY.size() % nSpec) != 0) {
throw std::invalid_argument("Length of DataY must be divisible by NSpec");
}
const std::size_t ySize = dataY.size() / nSpec;
// Check whether the X values provided are to be re-used for (are common to)
// every spectrum
const bool commonX(dataX.size() == ySize || dataX.size() == ySize + 1);
std::size_t xSize{dataX.size()};
HistogramBuilder histogramBuilder;
if (commonX) {
histogramBuilder.setX(dataX);
} else {
if (xSize % nSpec != 0) {
throw std::invalid_argument("Length of DataX must be divisible by NSpec");
}
xSize /= nSpec;
histogramBuilder.setX(xSize);
}
histogramBuilder.setY(ySize);
if (!dX.empty()) {
if (dX.size() != dataY.size())
throw std::runtime_error("Dx must have the same size as DataY");
histogramBuilder.setDx(ySize);
}
histogramBuilder.setDistribution(getProperty("Distribution"));
auto histogram = histogramBuilder.build();
const bool dataE_provided = !dataE.empty();
if (dataE_provided && dataY.size() != dataE.size()) {
throw std::runtime_error("DataE (if provided) must be the same size as DataY");
}
// Create the OutputWorkspace
MatrixWorkspace_const_sptr parentWS = getProperty("ParentWorkspace");
MatrixWorkspace_sptr outputWS;
if (parentWS) {
outputWS = create<HistoWorkspace>(*parentWS, nSpec, histogram);
} else {
auto storageMode = Parallel::fromString(getProperty("ParallelStorageMode"));
IndexInfo indexInfo(nSpec, storageMode, communicator());
outputWS = create<Workspace2D>(indexInfo, histogram);
}
Progress progress(this, 0.0, 1.0, nSpec);
const auto &indexInfo = outputWS->indexInfo();
PARALLEL_FOR_IF(Kernel::threadSafe(*outputWS))
for (int i = 0; i < nSpec; i++) {
PARALLEL_START_INTERUPT_REGION
// In an MPI run the global index i is not necessarily on this rank, i.e.,
// there might not be a corrsponding workspace index.
const auto localIndices = indexInfo.makeIndexSet({static_cast<GlobalSpectrumIndex>(i)});
if (localIndices.empty())
continue;
const std::vector<double>::difference_type xStart = i * xSize;
const std::vector<double>::difference_type xEnd = xStart + xSize;
const std::vector<double>::difference_type yStart = i * ySize;
const std::vector<double>::difference_type yEnd = yStart + ySize;
auto local_i = localIndices[0];
// Just set the pointer if common X bins. Otherwise, copy in the right chunk
// (as we do for Y).
if (!commonX)
outputWS->mutableX(local_i).assign(dataX.begin() + xStart, dataX.begin() + xEnd);
outputWS->mutableY(local_i).assign(dataY.begin() + yStart, dataY.begin() + yEnd);
if (dataE_provided)
outputWS->mutableE(local_i).assign(dataE.begin() + yStart, dataE.begin() + yEnd);
if (!dX.empty())
outputWS->mutableDx(local_i).assign(dX.begin() + yStart, dX.begin() + yEnd);
progress.report();
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
// Set the Unit of the X Axis
try {
outputWS->getAxis(0)->unit() = UnitFactory::Instance().create(xUnit);
} catch (Exception::NotFoundError &) {
outputWS->getAxis(0)->unit() = UnitFactory::Instance().create("Label");
Unit_sptr unit = outputWS->getAxis(0)->unit();
std::shared_ptr<Units::Label> label = std::dynamic_pointer_cast<Units::Label>(unit);
label->setLabel(xUnit, xUnit);
}
// Populate the VerticalAxis. A spectra one is there by default with a 1->N
// mapping
if (vUnit != "SpectraNumber") {
if (vUnit == "Text") {
auto newAxis = std::make_unique<TextAxis>(vAxis.size());
auto newAxisRaw = newAxis.get();
outputWS->replaceAxis(1, std::move(newAxis));
for (size_t i = 0; i < vAxis.size(); i++) {
newAxisRaw->setLabel(i, vAxis[i]);
}
} else {
std::unique_ptr<NumericAxis> newAxis(nullptr);
if (vAxisSize == nSpec)
newAxis = std::make_unique<NumericAxis>(vAxisSize); // treat as points
else if (vAxisSize == nSpec + 1)
newAxis = std::make_unique<BinEdgeAxis>(vAxisSize); // treat as bin edges
else
throw std::range_error("Invalid vertical axis length. It must be the "
"same length as NSpec or 1 longer.");
auto newAxisRaw = newAxis.get();
newAxis->unit() = UnitFactory::Instance().create(vUnit);
outputWS->replaceAxis(1, std::move(newAxis));
for (size_t i = 0; i < vAxis.size(); i++) {
try {
newAxisRaw->setValue(i, boost::lexical_cast<double, std::string>(vAxis[i]));
} catch (boost::bad_lexical_cast &) {
throw std::invalid_argument("CreateWorkspace - YAxisValues property "
"could not be converted to a double.");
}
}
}
}
// Set Y Unit label
if (!parentWS || !getPropertyValue("YUnitLabel").empty()) {
outputWS->setYUnitLabel(getProperty("YUnitLabel"));
}
// Set Workspace Title
if (!parentWS || !getPropertyValue("WorkspaceTitle").empty()) {
outputWS->setTitle(getProperty("WorkspaceTitle"));
}
// Set OutputWorkspace property
setProperty("OutputWorkspace", outputWS);
}
Parallel::ExecutionMode
CreateWorkspace::getParallelExecutionMode(const std::map<std::string, Parallel::StorageMode> &storageModes) const {
const auto storageMode = Parallel::fromString(getProperty("ParallelStorageMode"));
if (!storageModes.empty())
if (storageModes.begin()->second != storageMode)
throw std::invalid_argument("Input workspace storage mode differs from "
"requested output workspace storage mode.");
return Parallel::getCorrespondingExecutionMode(storageMode);
}
} // namespace Algorithms
} // namespace Mantid