-
Notifications
You must be signed in to change notification settings - Fork 122
/
WorkspaceUtils.cpp
254 lines (222 loc) · 9 KB
/
WorkspaceUtils.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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2019 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 "MantidQtWidgets/Common/WorkspaceUtils.h"
#include "MantidKernel/Logger.h"
#include "MantidQtWidgets/Common/TableWidgetValidators.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/TextAxis.h"
#include "MantidGeometry/Instrument.h"
#include <boost/algorithm/string.hpp>
#include <regex>
using namespace Mantid::API;
namespace {
Mantid::Kernel::Logger g_log("WorkspaceUtils");
double roundToPrecision(double value, double precision) { return value - std::remainder(value, precision); }
QPair<double, double> roundRangeToPrecision(double rangeStart, double rangeEnd, double precision) {
return QPair<double, double>(roundToPrecision(rangeStart, precision) + precision,
roundToPrecision(rangeEnd, precision) - precision);
}
auto const regDigits = std::regex("\\d+");
} // namespace
namespace MantidQt {
namespace MantidWidgets {
namespace WorkspaceUtils {
/**
* Gets the maximum number of histograms for a 2D Workspace
*
* @param workspace Workspace pointer
* @return size_t if index is found or null pointer if not found.
*/
std::optional<std::size_t> maximumIndex(const Mantid::API::MatrixWorkspace_sptr &workspace) {
if (workspace) {
const auto numberOfHistograms = workspace->getNumberHistograms();
if (numberOfHistograms > 0)
return numberOfHistograms - 1;
}
return std::nullopt;
}
std::string getIndexString(const MatrixWorkspace_sptr &workspace) {
const auto maximum = maximumIndex(workspace);
if (maximum)
return "0-" + std::to_string(*maximum);
return "";
}
std::string getIndexString(const std::string &workspaceName) { return getIndexString(getADSWorkspace(workspaceName)); }
/**
* Gets the suffix of a workspace (i.e. part after last underscore (red, sqw)).
*
* @param wsName Name of workspace
* @return Suffix, or empty string if no underscore
*/
std::string getWorkspaceSuffix(const std::string &wsName) {
auto const lastUnderscoreIndex = wsName.find_last_of("_");
if (lastUnderscoreIndex == wsName.npos)
return std::string();
return wsName.substr(lastUnderscoreIndex + 1);
}
/**
* Returns the basename of a workspace (i.e. the part before the last
*underscore)
*
* e.g. basename of irs26176_graphite002_red is irs26176_graphite002
*
* @param wsName Name of workspace
* @return Base name, or wsName if no underscore
*/
std::string getWorkspaceBasename(const std::string &wsName) {
auto lastUnderscoreIndex = wsName.find_last_of("_");
if (lastUnderscoreIndex == wsName.npos)
return wsName;
return wsName.substr(0, lastUnderscoreIndex);
}
/* Extracts the labels from the axis at the specified index in the
* specified workspace.
*
* @param workspace Constant reference to the matrix workspace
* @param axisIndex Index of the selected axis
* @return map of label, index pairs of labels of selected axis.
*/
std::unordered_map<std::string, size_t> extractAxisLabels(const Mantid::API::MatrixWorkspace_const_sptr &workspace,
const size_t &axisIndex) {
Axis *axis = workspace->getAxis(axisIndex);
if (!axis->isText())
return std::unordered_map<std::string, size_t>();
auto *textAxis = static_cast<TextAxis *>(axis);
std::unordered_map<std::string, size_t> labels;
for (size_t i = 0; i < textAxis->length(); ++i)
labels[textAxis->label(i)] = i;
return labels;
}
/**
* Gets the energy mode from a workspace based on the X unit.
*
* Units of dSpacing typically denote diffraction, hence Elastic.
* All other units default to spectroscopy, therefore Indirect.
*
* @param ws Pointer to the workspace
* @return Energy mode
*/
std::string getEMode(const Mantid::API::MatrixWorkspace_sptr &ws) {
Mantid::Kernel::Unit_sptr xUnit = ws->getAxis(0)->unit();
std::string xUnitName = xUnit->caption();
g_log.debug() << "X unit name is: " << xUnitName << '\n';
if (boost::algorithm::find_first(xUnitName, "d-Spacing"))
return "Elastic";
return "Indirect";
}
/**
* Gets the eFixed value from the workspace using the instrument parameters.
*
* @param ws Pointer to the workspace
* @return eFixed value
*/
std::optional<double> getEFixed(const Mantid::API::MatrixWorkspace_sptr &ws) {
Mantid::Geometry::Instrument_const_sptr inst = ws->getInstrument();
if (!inst)
return std::nullopt;
// Try to get it from the analyser component
if (inst->hasParameter("analyser")) {
auto const analyserName = inst->getStringParameter("analyser")[0];
auto const analyserComp = inst->getComponentByName(analyserName != "fmica" ? analyserName : "mica");
if (analyserComp && analyserComp->hasParameter("Efixed"))
return analyserComp->getNumberParameter("Efixed")[0];
}
// Try to get the parameter form the base instrument
if (inst->hasParameter("Efixed"))
return inst->getNumberParameter("Efixed")[0];
return std::nullopt;
}
/**
* Checks the workspace's instrument for a resolution parameter to use as
* a default for the energy range on the mini plot
*
* @param workspace :: Name of the workspace to use
* @param res :: The retrieved values for the resolution parameter (if one was
*found)
*/
bool getResolutionRangeFromWs(const std::string &workspace, QPair<double, double> &res) {
auto const ws =
Mantid::API::AnalysisDataService::Instance().retrieveWS<const Mantid::API::MatrixWorkspace>(workspace);
return getResolutionRangeFromWs(ws, res);
}
/**
* Checks the workspace's instrument for a resolution parameter to use as
* a default for the energy range on the mini plot
*
* @param ws :: Name of the workspace to use
* @param res :: The retrieved values for the resolution parameter (if one was
*found)
*/
bool getResolutionRangeFromWs(const Mantid::API::MatrixWorkspace_const_sptr &workspace, QPair<double, double> &res) {
if (workspace) {
auto const instrument = workspace->getInstrument();
if (instrument && instrument->hasParameter("analyser")) {
auto const analyser = instrument->getStringParameter("analyser");
if (analyser.size() > 0) {
auto comp = instrument->getComponentByName(analyser[0]);
if (comp) {
auto params = comp->getNumberParameter("resolution", true);
// set the default instrument resolution
if (params.size() > 0) {
res = qMakePair(-params[0], params[0]);
return true;
}
}
}
}
}
return false;
}
QPair<double, double> getXRangeFromWorkspace(std::string const &workspaceName, double precision) {
auto const &ads = AnalysisDataService::Instance();
if (ads.doesExist(workspaceName))
return getXRangeFromWorkspace(ads.retrieveWS<MatrixWorkspace>(workspaceName), precision);
return QPair<double, double>(0.0, 0.0);
}
QPair<double, double> getXRangeFromWorkspace(const Mantid::API::MatrixWorkspace_const_sptr &workspace,
double precision) {
auto const &xValues = workspace->x(0);
return roundRangeToPrecision(xValues.front(), xValues.back(), precision);
}
bool doesExistInADS(std::string const &workspaceName) {
return AnalysisDataService::Instance().doesExist(workspaceName);
}
std::vector<std::string> attachPrefix(std::vector<std::string> const &strings, std::string const &prefix) {
return transformElements(strings.begin(), strings.end(), [&prefix](std::string const &str) { return prefix + str; });
}
/**
* Checks the name of the input workspace against a regexp for prefixes in the form `instrName#runNumber_...`, where
* #runNumber is a number indicating the specific instrument run number that the workspace refers to.
* As output, it returns a single string with format 'instrName#firstRunNumber-#finalRunNumber_...', if run numbers are
* found.
*
* @param workspaceNames :: a vector of strings containing a series of workspace names
*/
std::string parseRunNumbers(std::vector<std::string> const &workspaceNames) {
auto names = WorkspaceUtils::transformElements(workspaceNames.begin(), workspaceNames.end(),
[](auto &name) { return name.substr(0, name.find_first_of('_')); });
std::smatch match;
std::vector<int> runNumbers;
std::string prefix;
std::string suffix = workspaceNames[0].substr(workspaceNames[0].find_first_of('_'));
for (auto const &name : names)
if (std::regex_search(name, match, regDigits)) {
runNumbers.push_back(std::stoi(match.str(0)));
if (prefix.empty())
prefix = match.prefix().str();
}
if (runNumbers.empty() || runNumbers.size() == 1)
return workspaceNames[0];
else {
auto [min, max] = std::minmax_element(runNumbers.cbegin(), runNumbers.cend());
return prefix + std::to_string(*min) + "-" + std::to_string(*max) + suffix;
}
}
} // namespace WorkspaceUtils
} // namespace MantidWidgets
} // namespace MantidQt