Mantid
Loading...
Searching...
No Matches
WorkspaceUtils.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10
11#include "MantidAPI/Axis.h"
13#include "MantidAPI/TextAxis.h"
15#include <boost/algorithm/string.hpp>
16#include <regex>
17
18using namespace Mantid::API;
19
20namespace {
21Mantid::Kernel::Logger g_log("WorkspaceUtils");
22
23double roundToPrecision(double value, double precision) { return value - std::remainder(value, precision); }
24
25std::pair<double, double> roundRangeToPrecision(double rangeStart, double rangeEnd, double precision) {
26 return std::pair<double, double>(roundToPrecision(rangeStart, precision) + precision,
27 roundToPrecision(rangeEnd, precision) - precision);
28}
29
30auto const regDigits = std::regex("\\d+");
31} // namespace
32namespace MantidQt {
33namespace MantidWidgets {
34namespace WorkspaceUtils {
35
43std::optional<std::size_t> maximumIndex(const Mantid::API::MatrixWorkspace_sptr &workspace) {
44 if (workspace) {
45 const auto numberOfHistograms = workspace->getNumberHistograms();
46 if (numberOfHistograms > 0)
47 return numberOfHistograms - 1;
48 }
49 return std::nullopt;
50}
51
53 const auto maximum = maximumIndex(workspace);
54 if (maximum)
55 return "0-" + std::to_string(*maximum);
56 return "";
57}
58
59std::string getIndexString(const std::string &workspaceName) { return getIndexString(getADSWorkspace(workspaceName)); }
60
67std::string getWorkspaceSuffix(const std::string &wsName) {
68 auto const lastUnderscoreIndex = wsName.find_last_of("_");
69 if (lastUnderscoreIndex == wsName.npos)
70 return std::string();
71 return wsName.substr(lastUnderscoreIndex + 1);
72}
73
83std::string getWorkspaceBasename(const std::string &wsName) {
84 auto lastUnderscoreIndex = wsName.find_last_of("_");
85 if (lastUnderscoreIndex == wsName.npos)
86 return wsName;
87
88 return wsName.substr(0, lastUnderscoreIndex);
89}
90
91/* Extracts the labels from the axis at the specified index in the
92 * specified workspace.
93 *
94 * @param workspace Constant reference to the matrix workspace
95 * @param axisIndex Index of the selected axis
96 * @return map of label, index pairs of labels of selected axis.
97 */
98std::unordered_map<std::string, size_t> extractAxisLabels(const Mantid::API::MatrixWorkspace_const_sptr &workspace,
99 const size_t &axisIndex) {
100 Axis *axis = workspace->getAxis(axisIndex);
101 if (!axis->isText())
102 return std::unordered_map<std::string, size_t>();
103
104 auto *textAxis = static_cast<TextAxis *>(axis);
105 std::unordered_map<std::string, size_t> labels;
106
107 for (size_t i = 0; i < textAxis->length(); ++i)
108 labels[textAxis->label(i)] = i;
109 return labels;
110}
111
122 Mantid::Kernel::Unit_sptr xUnit = ws->getAxis(0)->unit();
123 std::string xUnitName = xUnit->caption();
124
125 g_log.debug() << "X unit name is: " << xUnitName << '\n';
126 if (boost::algorithm::find_first(xUnitName, "d-Spacing"))
127 return "Elastic";
128
129 return "Indirect";
130}
131
138std::optional<double> getEFixed(const Mantid::API::MatrixWorkspace_sptr &ws) {
139 Mantid::Geometry::Instrument_const_sptr inst = ws->getInstrument();
140 if (!inst)
141 return std::nullopt;
142
143 // Try to get it from the analyser component
144 if (inst->hasParameter("analyser")) {
145 auto const analyserName = inst->getStringParameter("analyser")[0];
146 auto const analyserComp = inst->getComponentByName(analyserName != "fmica" ? analyserName : "mica");
147
148 if (analyserComp && analyserComp->hasParameter("Efixed"))
149 return analyserComp->getNumberParameter("Efixed")[0];
150 }
151
152 // Try to get the parameter form the base instrument
153 if (inst->hasParameter("Efixed"))
154 return inst->getNumberParameter("Efixed")[0];
155 return std::nullopt;
156}
157
166bool getResolutionRangeFromWs(const std::string &workspace, std::pair<double, double> &res) {
167 auto const ws =
168 Mantid::API::AnalysisDataService::Instance().retrieveWS<const Mantid::API::MatrixWorkspace>(workspace);
169 return getResolutionRangeFromWs(ws, res);
170}
180 std::pair<double, double> &res) {
181 if (workspace) {
182 auto const instrument = workspace->getInstrument();
183 if (instrument && instrument->hasParameter("analyser")) {
184 auto const analyser = instrument->getStringParameter("analyser");
185 if (analyser.size() > 0) {
186 auto comp = instrument->getComponentByName(analyser[0]);
187 if (comp) {
188 auto params = comp->getNumberParameter("resolution", true);
189
190 // set the default instrument resolution
191 if (params.size() > 0) {
192 res = std::pair(-params[0], params[0]);
193 return true;
194 }
195 }
196 }
197 }
198 }
199 return false;
200}
201
202std::pair<double, double> getXRangeFromWorkspace(std::string const &workspaceName, double precision) {
203 auto const &ads = AnalysisDataService::Instance();
204 if (ads.doesExist(workspaceName))
205 return getXRangeFromWorkspace(ads.retrieveWS<MatrixWorkspace>(workspaceName), precision);
206 return std::pair<double, double>(0.0, 0.0);
207}
208
210 double precision) {
211 assert(workspace != nullptr);
212 auto const &xValues = workspace->x(0);
213 return roundRangeToPrecision(xValues.front(), xValues.back(), precision);
214}
215
216bool doesExistInADS(std::string const &workspaceName) {
217 return AnalysisDataService::Instance().doesExist(workspaceName);
218}
219
220bool doAllWsExistInADS(std::vector<std::string> const &workspaceNames) {
221 return AnalysisDataService::Instance().doAllWsExist(workspaceNames);
222}
223
224std::vector<std::string> attachPrefix(std::vector<std::string> const &strings, std::string const &prefix) {
225 return transformElements(strings.begin(), strings.end(), [&prefix](std::string const &str) { return prefix + str; });
226}
227
237std::string parseRunNumbers(std::vector<std::string> const &workspaceNames) {
238 auto names = WorkspaceUtils::transformElements(workspaceNames.begin(), workspaceNames.end(),
239 [](auto &name) { return name.substr(0, name.find_first_of('_')); });
240 std::smatch match;
241 std::vector<int> runNumbers;
242 std::string prefix;
243 if (workspaceNames.empty()) {
244 return std::string("");
245 }
246 std::string suffix = workspaceNames[0].substr(workspaceNames[0].find_first_of('_'));
247 for (auto const &name : names)
248 if (std::regex_search(name, match, regDigits)) {
249 runNumbers.push_back(std::stoi(match.str(0)));
250 if (prefix.empty())
251 prefix = match.prefix().str();
252 }
253 if (runNumbers.empty() || runNumbers.size() == 1)
254 return workspaceNames[0];
255 else {
256 auto [min, max] = std::minmax_element(runNumbers.cbegin(), runNumbers.cend());
257 return prefix + std::to_string(*min) + "-" + std::to_string(*max) + suffix;
258 }
259}
260
261} // namespace WorkspaceUtils
262} // namespace MantidWidgets
263} // namespace MantidQt
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
IPeaksWorkspace_sptr workspace
Class to represent the axis of a workspace.
Definition Axis.h:30
virtual bool isText() const
Returns true if the axis is Text.
Definition Axis.h:54
const std::shared_ptr< Kernel::Unit > & unit() const
The unit for this axis.
Definition Axis.cpp:28
Base MatrixWorkspace Abstract Class.
Class to represent a text axis of a workspace.
Definition TextAxis.h:36
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
EXPORT_OPT_MANTIDQT_COMMON std::string getIndexString(const std::string &workspaceName)
EXPORT_OPT_MANTIDQT_COMMON std::pair< double, double > getXRangeFromWorkspace(const Mantid::API::MatrixWorkspace_const_sptr &workspace, double precision=0.00001)
EXPORT_OPT_MANTIDQT_COMMON std::string getWorkspaceBasename(const std::string &wsName)
Returns the basename of a workspace (i.e.
EXPORT_OPT_MANTIDQT_COMMON std::optional< std::size_t > maximumIndex(const Mantid::API::MatrixWorkspace_sptr &workspace)
Gets the maximum number of histograms for a 2D Workspace.
EXPORT_OPT_MANTIDQT_COMMON bool getResolutionRangeFromWs(const std::string &workspace, std::pair< double, double > &res)
Checks the workspace's instrument for a resolution parameter to use as a default for the energy range...
std::shared_ptr< T > getADSWorkspace(std::string const &workspaceName)
EXPORT_OPT_MANTIDQT_COMMON bool doesExistInADS(std::string const &workspaceName)
EXPORT_OPT_MANTIDQT_COMMON std::string getEMode(const Mantid::API::MatrixWorkspace_sptr &ws)
Gets the energy mode from a workspace based on the X unit.
EXPORT_OPT_MANTIDQT_COMMON std::string parseRunNumbers(std::vector< std::string > const &workspaceNames)
Checks the name of the input workspace against a regexp for prefixes in the form instrName\#runNumber...
EXPORT_OPT_MANTIDQT_COMMON std::string getWorkspaceSuffix(const std::string &wsName)
Gets the suffix of a workspace (i.e.
EXPORT_OPT_MANTIDQT_COMMON std::optional< double > getEFixed(const Mantid::API::MatrixWorkspace_sptr &ws)
Gets the eFixed value from the workspace using the instrument parameters.
EXPORT_OPT_MANTIDQT_COMMON std::unordered_map< std::string, size_t > extractAxisLabels(const Mantid::API::MatrixWorkspace_const_sptr &workspace, const size_t &axisIndex)
std::vector< std::string > transformElements(Iterator const fromIter, Iterator const toIter, Functor const &functor)
EXPORT_OPT_MANTIDQT_COMMON bool doAllWsExistInADS(std::vector< std::string > const &workspaceNames)
EXPORT_OPT_MANTIDQT_COMMON std::vector< std::string > attachPrefix(std::vector< std::string > const &strings, std::string const &prefix)
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
Definition Unit.h:194
std::string to_string(const wide_integer< Bits, Signed > &n)