-
Notifications
You must be signed in to change notification settings - Fork 122
/
SetMDFrame.cpp
208 lines (181 loc) · 8.16 KB
/
SetMDFrame.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
// 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 "MantidMDAlgorithms/SetMDFrame.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/IMDWorkspace.h"
#include "MantidGeometry/MDGeometry/GeneralFrame.h"
#include "MantidGeometry/MDGeometry/HKL.h"
#include "MantidGeometry/MDGeometry/MDFrameFactory.h"
#include "MantidGeometry/MDGeometry/MDHistoDimension.h"
#include "MantidGeometry/MDGeometry/QLab.h"
#include "MantidGeometry/MDGeometry/QSample.h"
#include "MantidGeometry/MDGeometry/UnknownFrame.h"
#include "MantidKernel/ArrayBoundedValidator.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MDAxisValidator.h"
#include <boost/pointer_cast.hpp>
#include <map>
#include <memory>
namespace Mantid {
namespace MDAlgorithms {
using Mantid::API::WorkspaceProperty;
using Mantid::Kernel::Direction;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SetMDFrame)
const std::string SetMDFrame::mdFrameSpecifier = "MDFrame";
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string SetMDFrame::name() const { return "SetMDFrame"; }
/// Algorithm's version for identification. @see Algorithm::version
int SetMDFrame::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string SetMDFrame::category() const {
return "MDAlgorithms";
;
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string SetMDFrame::summary() const {
return "Sets a new MDFrame type for a selection of axes for legacy MDHisto "
"and MDEvent "
"workspaces.";
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SetMDFrame::init() {
declareProperty(
std::make_unique<WorkspaceProperty<Mantid::API::IMDWorkspace>>(
"InputWorkspace", "", Direction::InOut),
"The workspace for which the MDFrames are to be changed. "
"Note that only MDHisto and MDEvent workspaces can be "
"altered by this algorithm.");
// Options for the MDFrames
std::vector<std::string> mdFrames;
mdFrames.emplace_back(Mantid::Geometry::GeneralFrame::GeneralFrameName);
mdFrames.emplace_back(Mantid::Geometry::QSample::QSampleName);
mdFrames.emplace_back(Mantid::Geometry::QLab::QLabName);
mdFrames.emplace_back(Mantid::Geometry::HKL::HKLName);
mdFrames.emplace_back(Mantid::Geometry::UnknownFrame::UnknownFrameName);
// Create a selection of MDFrames and units for each dimension
std::string propName = mdFrameSpecifier;
declareProperty(
propName, Mantid::Geometry::GeneralFrame::GeneralFrameName,
std::make_shared<Mantid::Kernel::StringListValidator>(mdFrames),
"MDFrame type selection.\n");
auto axisValidator =
std::make_shared<Mantid::Kernel::ArrayBoundedValidator<int>>();
axisValidator->setLower(0);
declareProperty(
std::make_unique<Kernel::ArrayProperty<int>>(
"Axes", std::vector<int>(0), axisValidator, Direction::Input),
"Selects the axes which are going to be set to the new MDFrame type.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SetMDFrame::exec() {
Mantid::API::IMDWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
std::vector<int> axesInts = this->getProperty("Axes");
std::vector<size_t> axes(axesInts.begin(), axesInts.end());
// If no axes were specified, then don't do anything
if (axes.empty()) {
return;
}
for (auto &axe : axes) {
// Get associated dimension
auto dimension = inputWorkspace->getDimension(axe);
// Provide a new MDFrame
std::string frameSelection = getProperty(mdFrameSpecifier);
const auto &mdFrame = dimension->getMDFrame();
auto newMDFrame = createMDFrame(frameSelection, mdFrame);
// Set the new MDFrame. We need to alter the MDFrame information
// of the MDHistoDimension which we only get as a const -- hence
// we need a const-cast at this point.
auto mdHistoDimension =
std::const_pointer_cast<Mantid::Geometry::MDHistoDimension>(
std::dynamic_pointer_cast<const Mantid::Geometry::MDHistoDimension>(
dimension));
if (!mdHistoDimension) {
throw std::runtime_error("SetMDFrame: Cannot convert to MDHistDimension");
}
mdHistoDimension->setMDFrame(*newMDFrame);
}
}
/**
* Check the inputs for invalid values
* @returns A map with validation warnings.
*/
std::map<std::string, std::string> SetMDFrame::validateInputs() {
std::map<std::string, std::string> invalidProperties;
Mantid::API::IMDWorkspace_sptr ws = getProperty("InputWorkspace");
if (!std::dynamic_pointer_cast<Mantid::API::IMDEventWorkspace>(ws) &&
!std::dynamic_pointer_cast<Mantid::API::IMDHistoWorkspace>(ws)) {
invalidProperties.insert(
std::make_pair("InputWorkspace", "The input workspace has to be either "
"an MDEvent or MDHisto Workspace."));
}
std::vector<int> axesInts = this->getProperty("Axes");
Kernel::MDAxisValidator axisChecker(axesInts, ws->getNumDims(), true);
auto axisErrors = axisChecker.validate();
for (auto &axisError : axisErrors) {
invalidProperties.insert(axisError);
}
return invalidProperties;
}
/**
* Creates an MDFrame based on the users selection
* @param frameSelection :: the selected frame type
* @param oldFrame :: a refrence to the frame we want to replace
* @returns :: a unique pointer to the new MDFrame
*/
Mantid::Geometry::MDFrame_uptr
SetMDFrame::createMDFrame(const std::string &frameSelection,
const Mantid::Geometry::MDFrame &oldFrame) const {
auto mdFrameFactory = Mantid::Geometry::makeMDFrameFactoryChain();
if (frameSelection == Mantid::Geometry::GeneralFrame::GeneralFrameName) {
Mantid::Geometry::MDFrameArgument argument(
Mantid::Geometry::GeneralFrame::GeneralFrameName,
oldFrame.getUnitLabel());
return mdFrameFactory->create(argument);
} else if (frameSelection == Mantid::Geometry::QSample::QSampleName) {
Mantid::Geometry::MDFrameArgument argument(
Mantid::Geometry::QSample::QSampleName);
return mdFrameFactory->create(argument);
} else if (frameSelection == Mantid::Geometry::QLab::QLabName) {
Mantid::Geometry::MDFrameArgument argument(
Mantid::Geometry::QLab::QLabName);
return mdFrameFactory->create(argument);
} else if (frameSelection == Mantid::Geometry::HKL::HKLName) {
Mantid::Geometry::MDFrameArgument argument(Mantid::Geometry::HKL::HKLName,
oldFrame.getUnitLabel());
// We want to make sure here that we get an HKL MDFrame, hence we need to
// make sure that the HKL frame accepts the units
Mantid::Geometry::HKLFrameFactory hklFrameFactory;
auto canInterpret = hklFrameFactory.canInterpret(argument);
if (!canInterpret) {
throw std::invalid_argument("SetMDFrame: " + frameSelection +
" does not have units which are compatible "
"with an HKL frame. Please contact the "
"Mantid team if you believe that the units "
"should be compatible.");
}
return mdFrameFactory->create(argument);
} else if (frameSelection ==
Mantid::Geometry::UnknownFrame::UnknownFrameName) {
Mantid::Geometry::MDFrameArgument argument(
Mantid::Geometry::UnknownFrame::UnknownFrameName,
oldFrame.getUnitLabel());
return mdFrameFactory->create(argument);
} else {
throw std::invalid_argument(
"SetMDFrame: The selected MDFrame does not seem to be supported");
}
}
} // namespace MDAlgorithms
} // namespace Mantid