Skip to content

Commit 536fa47

Browse files
committed
Improve validation code efficiency and robustness
1 parent 9a33583 commit 536fa47

File tree

2 files changed

+78
-37
lines changed

2 files changed

+78
-37
lines changed

Framework/Algorithms/src/PolarizationCorrections/PolarizationEfficienciesWildes.cpp

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -128,69 +128,88 @@ void PolarizationEfficienciesWildes::init() {
128128
}
129129

130130
namespace {
131-
132-
void validateMatchingBins(const Mantid::API::MatrixWorkspace_sptr &workspace,
133-
const Mantid::API::MatrixWorkspace_sptr &refWs, const std::string &propertyName,
134-
std::map<std::string, std::string> &problems) {
131+
bool hasMatchingBins(const Mantid::API::MatrixWorkspace_sptr &workspace, const Mantid::API::MatrixWorkspace_sptr &refWs,
132+
const std::string &propertyName, std::map<std::string, std::string> &problems) {
135133
if (!WorkspaceHelpers::matchingBins(*workspace, *refWs, true)) {
136134
problems[propertyName] = "All input workspaces must have the same X values.";
137-
return;
135+
return false;
138136
}
137+
138+
return true;
139139
}
140140

141-
void validateInputWorkspace(const Mantid::API::MatrixWorkspace_sptr &workspace,
142-
const Mantid::API::MatrixWorkspace_sptr &refWs, const std::string &propertyName,
143-
std::map<std::string, std::string> &problems) {
141+
bool isValidInputWorkspace(const Mantid::API::MatrixWorkspace_sptr &workspace,
142+
const Mantid::API::MatrixWorkspace_sptr &refWs, const std::string &propertyName,
143+
std::map<std::string, std::string> &problems) {
144144
if (workspace == nullptr) {
145145
problems[propertyName] = "All input workspaces must be matrix workspaces.";
146-
return;
146+
return false;
147147
}
148148

149149
Kernel::Unit_const_sptr unit = workspace->getAxis(0)->unit();
150150
if (unit->unitID() != "Wavelength") {
151151
problems[propertyName] = "All input workspaces must be in units of Wavelength.";
152-
return;
152+
return false;
153153
}
154154

155155
if (workspace->getNumberHistograms() != 1) {
156156
problems[propertyName] = "All input workspaces must contain only a single spectrum.";
157-
return;
157+
return false;
158158
}
159159

160-
validateMatchingBins(workspace, refWs, propertyName, problems);
160+
return hasMatchingBins(workspace, refWs, propertyName, problems);
161161
}
162162

163-
void validateInputWSGroup(const Mantid::API::WorkspaceGroup_sptr &groupWs, const std::string &propertyName,
164-
std::map<std::string, std::string> &problems) {
163+
bool isValidInputWSGroup(const Mantid::API::WorkspaceGroup_sptr &groupWs, const std::string &propertyName,
164+
std::map<std::string, std::string> &problems) {
165165
if (groupWs == nullptr) {
166166
problems[propertyName] = "The input workspace must be a group workspace.";
167-
return;
167+
return false;
168168
}
169169

170170
if (groupWs->size() != 4) {
171171
problems[propertyName] = "The input group must contain a workspace for all four flipper configurations.";
172-
return;
172+
return false;
173173
}
174174

175175
const MatrixWorkspace_sptr refWs = std::dynamic_pointer_cast<MatrixWorkspace>(groupWs->getItem(0));
176176
for (size_t i = 0; i < groupWs->size(); ++i) {
177177
const MatrixWorkspace_sptr childWs = std::dynamic_pointer_cast<MatrixWorkspace>(groupWs->getItem(i));
178-
validateInputWorkspace(childWs, refWs, propertyName, problems);
178+
if (!isValidInputWorkspace(childWs, refWs, propertyName, problems)) {
179+
return false;
180+
}
179181
}
182+
183+
return true;
180184
}
181185
} // namespace
182186

183187
std::map<std::string, std::string> PolarizationEfficienciesWildes::validateInputs() {
184188
std::map<std::string, std::string> problems;
185189

186-
const WorkspaceGroup_sptr nonMagWsGrp = getProperty(PropNames::INPUT_NON_MAG_WS);
187-
validateInputWSGroup(nonMagWsGrp, PropNames::INPUT_NON_MAG_WS, problems);
188-
const MatrixWorkspace_sptr nonMagRefWs = std::dynamic_pointer_cast<MatrixWorkspace>(nonMagWsGrp->getItem(0));
189-
190190
const bool hasMagWsGrp = !isDefault(PropNames::INPUT_MAG_WS);
191191
const bool hasInputPWs = !isDefault(PropNames::INPUT_P_EFF_WS);
192192
const bool hasInputAWs = !isDefault(PropNames::INPUT_A_EFF_WS);
193193

194+
if (!isDefault(PropNames::OUTPUT_P_EFF_WS) && !hasMagWsGrp && !hasInputPWs && !hasInputAWs) {
195+
problems[PropNames::OUTPUT_P_EFF_WS] = "If output polarizer efficiency is requested then either the magnetic "
196+
"workspace or the known analyser efficiency should be provided.";
197+
}
198+
199+
if (!isDefault(PropNames::OUTPUT_A_EFF_WS) && !hasMagWsGrp && !hasInputPWs && !hasInputAWs) {
200+
problems[PropNames::OUTPUT_A_EFF_WS] = "If output analyser efficiency is requested then either the magnetic "
201+
"workspace or the known polarizer efficiency should be provided.";
202+
}
203+
204+
const WorkspaceGroup_sptr nonMagWsGrp = getProperty(PropNames::INPUT_NON_MAG_WS);
205+
if (!isValidInputWSGroup(nonMagWsGrp, PropNames::INPUT_NON_MAG_WS, problems)) {
206+
// We need to use a child workspace from the nonMag group as a reference for later checks, so stop here if there are
207+
// any issues with this input
208+
return problems;
209+
}
210+
211+
const MatrixWorkspace_sptr nonMagRefWs = std::dynamic_pointer_cast<MatrixWorkspace>(nonMagWsGrp->getItem(0));
212+
194213
// If a magnetic workspace has been provided then we will use that to calculate the polarizer and analyser
195214
// efficiencies, so individual efficiency workspaces should not be provided as well
196215
if (hasMagWsGrp) {
@@ -203,34 +222,23 @@ std::map<std::string, std::string> PolarizationEfficienciesWildes::validateInput
203222
}
204223

205224
const WorkspaceGroup_sptr magWsGrp = getProperty(PropNames::INPUT_MAG_WS);
206-
validateInputWSGroup(magWsGrp, PropNames::INPUT_MAG_WS, problems);
207-
208-
if (problems.find(PropNames::INPUT_MAG_WS) == problems.end()) {
225+
if (isValidInputWSGroup(magWsGrp, PropNames::INPUT_MAG_WS, problems)) {
226+
// Check that bins match between the input mag and nonMag workspace groups
209227
const MatrixWorkspace_sptr magWs = std::dynamic_pointer_cast<MatrixWorkspace>(magWsGrp->getItem(0));
210-
validateMatchingBins(magWs, nonMagRefWs, PropNames::INPUT_MAG_WS, problems);
228+
hasMatchingBins(magWs, nonMagRefWs, PropNames::INPUT_MAG_WS, problems);
211229
}
212230
} else {
213231
if (hasInputPWs) {
214232
const MatrixWorkspace_sptr inputPolEffWs = getProperty(PropNames::INPUT_P_EFF_WS);
215-
validateInputWorkspace(inputPolEffWs, nonMagRefWs, PropNames::INPUT_P_EFF_WS, problems);
233+
isValidInputWorkspace(inputPolEffWs, nonMagRefWs, PropNames::INPUT_P_EFF_WS, problems);
216234
}
217235

218236
if (hasInputAWs) {
219237
const MatrixWorkspace_sptr inputAnaEffWs = getProperty(PropNames::INPUT_A_EFF_WS);
220-
validateInputWorkspace(inputAnaEffWs, nonMagRefWs, PropNames::INPUT_A_EFF_WS, problems);
238+
isValidInputWorkspace(inputAnaEffWs, nonMagRefWs, PropNames::INPUT_A_EFF_WS, problems);
221239
}
222240
}
223241

224-
if (!isDefault(PropNames::OUTPUT_P_EFF_WS) && !hasMagWsGrp && !hasInputPWs && !hasInputAWs) {
225-
problems[PropNames::OUTPUT_P_EFF_WS] = "If output polarizer efficiency is requested then either the magnetic "
226-
"workspace or the known analyser efficiency should be provided.";
227-
}
228-
229-
if (!isDefault(PropNames::OUTPUT_A_EFF_WS) && !hasMagWsGrp && !hasInputPWs && !hasInputAWs) {
230-
problems[PropNames::OUTPUT_A_EFF_WS] = "If output analyser efficiency is requested then either the magnetic "
231-
"workspace or the known polarizer efficiency should be provided.";
232-
}
233-
234242
return problems;
235243
}
236244

Framework/Algorithms/test/PolarizationCorrections/PolarizationEfficienciesWildesTest.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "MantidAPI/AnalysisDataService.h"
1212
#include "MantidAPI/MatrixWorkspace.h"
13+
#include "MantidAPI/WorkspaceFactory.h"
1314
#include "MantidAPI/WorkspaceGroup.h"
1415
#include "MantidAlgorithms/CreateSampleWorkspace.h"
1516
#include "MantidAlgorithms/GroupWorkspaces.h"
@@ -21,6 +22,7 @@ using Mantid::Algorithms::GroupWorkspaces;
2122
using Mantid::Algorithms::PolarizationEfficienciesWildes;
2223
using Mantid::API::AnalysisDataService;
2324
using Mantid::API::MatrixWorkspace_sptr;
25+
using Mantid::API::WorkspaceFactory;
2426
using Mantid::API::WorkspaceGroup_sptr;
2527
using Mantid::DataObjects::TableWorkspace;
2628

@@ -241,6 +243,20 @@ class PolarizationEfficienciesWildesTest : public CxxTest::TestSuite {
241243
assertSetPropertyThrowsInvalidArgumentError<TableWorkspace>(InputPropNames::A_EFF_WS, invalidWsType);
242244
}
243245

246+
void test_input_non_mag_not_matrix_ws_group_throws_error() {
247+
const auto nonMagGrp = createTableWorkspaceGrp("nonMagWs");
248+
const auto magGrp = createMagWSGroup("magWs");
249+
const auto alg = createEfficiencyAlg(nonMagGrp, magGrp);
250+
assertValidationError(alg, InputPropNames::NON_MAG_WS, PropErrors::WS_GRP_CHILD_TYPE_ERROR);
251+
}
252+
253+
void test_input_mag_not_matrix_ws_group_throws_error() {
254+
const auto nonMagGrp = createNonMagWSGroup("nonMagWs");
255+
const auto magGrp = createTableWorkspaceGrp("magWs");
256+
const auto alg = createEfficiencyAlg(nonMagGrp, magGrp);
257+
assertValidationError(alg, InputPropNames::MAG_WS, PropErrors::WS_GRP_CHILD_TYPE_ERROR);
258+
}
259+
244260
/// Validation tests - valid property combinations
245261

246262
void test_providing_both_mag_and_input_polarizer_efficiency_ws_throws_error() {
@@ -459,6 +475,23 @@ class PolarizationEfficienciesWildesTest : public CxxTest::TestSuite {
459475
return alg.getProperty("OutputWorkspace");
460476
}
461477

478+
WorkspaceGroup_sptr createTableWorkspaceGrp(const std::string &outName) {
479+
const std::vector<std::string> &wsNames{outName + "_00", outName + "_01", outName + "_10", outName + "_11"};
480+
for (const auto &wsName : wsNames) {
481+
const auto ws = WorkspaceFactory::Instance().createTable();
482+
AnalysisDataService::Instance().addOrReplace(wsName, ws);
483+
}
484+
485+
GroupWorkspaces groupAlg;
486+
groupAlg.initialize();
487+
groupAlg.setChild(true);
488+
groupAlg.setProperty("InputWorkspaces", wsNames);
489+
groupAlg.setPropertyValue("OutputWorkspace", outName);
490+
groupAlg.execute();
491+
492+
return groupAlg.getProperty("OutputWorkspace");
493+
}
494+
462495
std::unique_ptr<PolarizationEfficienciesWildes> createEfficiencyAlg(const WorkspaceGroup_sptr &nonMagWSGroup,
463496
const WorkspaceGroup_sptr &magWsGroup = nullptr) {
464497
auto alg = std::make_unique<PolarizationEfficienciesWildes>();

0 commit comments

Comments
 (0)