Skip to content

Commit 2c7eef5

Browse files
committed
Add Input and Output SpinStates for FredrikzeAlgorithm in PolarizationEfficiencyCor
- added input and output spin states for fredrikze algorithm in polarizationefficiencycor algorithm - updated unit tests to test spin states in both algorithms - updated polarizationefficiencycor documentation - added a release note for the update
1 parent 732c037 commit 2c7eef5

File tree

8 files changed

+162
-36
lines changed

8 files changed

+162
-36
lines changed

Framework/Algorithms/inc/MantidAlgorithms/PolarizationCorrectionFredrikze.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MANTID_ALGORITHMS_DLL PolarizationCorrectionFredrikze final : public API::
4747
std::shared_ptr<Mantid::API::MatrixWorkspace> add(const std::shared_ptr<Mantid::API::MatrixWorkspace> &lhsWS,
4848
const double &rhs);
4949
std::shared_ptr<Mantid::API::MatrixWorkspace> multiply(const std::shared_ptr<Mantid::API::MatrixWorkspace> &lhsWS,
50-
const double &rhs);
50+
double &rhs);
5151
};
5252

5353
} // namespace Algorithms

Framework/Algorithms/src/PolarizationCorrectionFredrikze.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "MantidAPI/WorkspaceGroup.h"
1212
#include "MantidAPI/WorkspaceHistory.h"
1313
#include "MantidAlgorithms/PolarizationCorrections/PolarizationCorrectionsHelpers.h"
14+
#include "MantidAlgorithms/PolarizationCorrections/SpinStateValidator.h"
1415
#include "MantidDataObjects/WorkspaceSingleValue.h"
1516
#include "MantidGeometry/Instrument.h"
1617
#include "MantidKernel/ListValidator.h"
@@ -151,7 +152,7 @@ const std::string PolarizationCorrectionFredrikze::summary() const {
151152
* @param rhs : WS to multiply by (constant value)
152153
* @return Multiplied Workspace.
153154
*/
154-
MatrixWorkspace_sptr PolarizationCorrectionFredrikze::multiply(const MatrixWorkspace_sptr &lhsWS, const double &rhs) {
155+
MatrixWorkspace_sptr PolarizationCorrectionFredrikze::multiply(const MatrixWorkspace_sptr &lhsWS, double &rhs) {
155156
auto multiply = this->createChildAlgorithm("Multiply");
156157
auto rhsWS = std::make_shared<DataObjects::WorkspaceSingleValue>(rhs);
157158
multiply->initialize();
@@ -244,13 +245,16 @@ void PolarizationCorrectionFredrikze::init() {
244245
std::make_unique<WorkspaceProperty<Mantid::API::WorkspaceGroup>>("OutputWorkspace", "", Direction::Output),
245246
"An output workspace.");
246247

248+
const auto spinStateValidator =
249+
std::make_shared<SpinStateValidator>(std::unordered_set<int>{2, 4}, true, 'p', 'a', true);
250+
247251
declareProperty(
248-
std::make_unique<PropertyWithValue<std::string>>(inputSpinStateOrderLabel, "", Direction::Input),
252+
inputSpinStateOrderLabel, "", spinStateValidator,
249253
"The order of spin states in the input workspace group. TThe possible values are 'pp,pa,ap,aa' or 'p,a'.");
250254

251255
declareProperty(
252-
std::make_unique<PropertyWithValue<std::string>>(outputSpinStateOrderLabel, "", Direction::Input),
253-
"The order of spin states in the output workspace group. The possible values are 'pp,pa,ap,aa' or 'p,a'");
256+
outputSpinStateOrderLabel, "", spinStateValidator,
257+
"The order of spin states in the input workspace group. TThe possible values are 'pp,pa,ap,aa' or 'p,a'.");
254258
}
255259

256260
WorkspaceGroup_sptr PolarizationCorrectionFredrikze::execPA(const WorkspaceGroup_sptr &inWS,
@@ -412,8 +416,6 @@ void PolarizationCorrectionFredrikze::exec() {
412416
const std::string inputOrderStr = getProperty(inputSpinStateOrderLabel);
413417
const std::string outputOrderStr = getProperty(outputSpinStateOrderLabel);
414418

415-
g_log.warning(inputOrderStr);
416-
417419
const std::vector<std::string> inputOrder = PolarizationCorrectionsHelpers::splitSpinStateString(inputOrderStr);
418420
const std::vector<std::string> outputOrder = PolarizationCorrectionsHelpers::splitSpinStateString(outputOrderStr);
419421

@@ -433,6 +435,7 @@ void PolarizationCorrectionFredrikze::exec() {
433435
outWS = execPNR(inWS, inputOrder, outputOrder);
434436
g_log.notice("PNR polarization correction");
435437
}
438+
436439
this->setProperty("OutputWorkspace", outWS);
437440
}
438441

Framework/Algorithms/src/PolarizationEfficiencyCor.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "MantidDataObjects/Workspace2D.h"
1616
#include "MantidDataObjects/WorkspaceCreation.h"
1717
#include "MantidKernel/ArrayProperty.h"
18+
#include "MantidKernel/EnabledWhenProperty.h"
1819
#include "MantidKernel/ListValidator.h"
1920
#include "MantidKernel/StringTokenizer.h"
2021

@@ -26,13 +27,16 @@ namespace {
2627
/// Property names.
2728
namespace Prop {
2829
static const std::string FLIPPERS{"Flippers"};
29-
static const std::string SPIN_STATES{"SpinStates"};
30+
static const std::string SPIN_STATES{"SpinStatesOutWildes"};
3031
static const std::string POLARIZATION_ANALYSIS{"PolarizationAnalysis"};
3132
static const std::string EFFICIENCIES{"Efficiencies"};
3233
static const std::string INPUT_WORKSPACES{"InputWorkspaces"};
3334
static const std::string INPUT_WORKSPACE_GROUP{"InputWorkspaceGroup"};
3435
static const std::string OUTPUT_WORKSPACES{"OutputWorkspace"};
3536
static const std::string CORRECTION_METHOD{"CorrectionMethod"};
37+
static const std::string INPUT_FRED_SPIN_STATES{"SpinStatesInFredrikze"};
38+
static const std::string OUTPUT_FRED_SPIN_STATES{"SpinStatesOutFredrikze"};
39+
3640
} // namespace Prop
3741

3842
namespace CorrectionMethod {
@@ -117,9 +121,38 @@ void PolarizationEfficiencyCor::init() {
117121
"PA: Full Polarization Analysis PNR-PA "
118122
"(Fredrikze method only)");
119123

124+
const auto fredrikzeSpinStateValidator =
125+
std::make_shared<SpinStateValidator>(std::unordered_set<int>{2, 4}, true, 'p', 'a', true);
126+
127+
declareProperty(Prop::INPUT_FRED_SPIN_STATES, "", fredrikzeSpinStateValidator,
128+
"The order of spin states in the input workspace group. The possible values are 'pp,pa,ap,aa' or "
129+
"'p,a'. (Fredrikze method only).");
130+
131+
declareProperty(Prop::OUTPUT_FRED_SPIN_STATES, "", fredrikzeSpinStateValidator,
132+
"The order of spin states in the output workspace group. The possible values are 'pp,pa,ap,aa' or "
133+
"'p,a'. (Fredrikze method only).");
134+
120135
declareProperty(
121136
std::make_unique<WorkspaceProperty<WorkspaceGroup>>(Prop::OUTPUT_WORKSPACES, "", Kernel::Direction::Output),
122137
"A group of polarization efficiency corrected workspaces.");
138+
139+
setPropertySettings(Prop::SPIN_STATES, std::make_unique<EnabledWhenProperty>(
140+
Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::WILDES));
141+
142+
setPropertySettings(Prop::FLIPPERS, std::make_unique<EnabledWhenProperty>(
143+
Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::WILDES));
144+
145+
setPropertySettings(
146+
Prop::INPUT_FRED_SPIN_STATES,
147+
std::make_unique<EnabledWhenProperty>(Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::FREDRIKZE));
148+
149+
setPropertySettings(
150+
Prop::OUTPUT_FRED_SPIN_STATES,
151+
std::make_unique<EnabledWhenProperty>(Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::FREDRIKZE));
152+
153+
setPropertySettings(
154+
"PolarizationAnalysis",
155+
std::make_unique<EnabledWhenProperty>(Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::FREDRIKZE));
123156
}
124157

125158
//----------------------------------------------------------------------------------------------
@@ -169,6 +202,12 @@ void PolarizationEfficiencyCor::execFredrikze() {
169202
if (!isDefault(Prop::POLARIZATION_ANALYSIS)) {
170203
alg->setPropertyValue("PolarizationAnalysis", getPropertyValue(Prop::POLARIZATION_ANALYSIS));
171204
}
205+
if (!isDefault(Prop::INPUT_FRED_SPIN_STATES)) {
206+
alg->setPropertyValue("InputSpinStateOrder", getPropertyValue(Prop::INPUT_FRED_SPIN_STATES));
207+
}
208+
if (!isDefault(Prop::OUTPUT_FRED_SPIN_STATES)) {
209+
alg->setPropertyValue("OutputSpinStateOrder", getPropertyValue(Prop::OUTPUT_FRED_SPIN_STATES));
210+
}
172211
alg->setPropertyValue("OutputWorkspace", getPropertyValue(Prop::OUTPUT_WORKSPACES));
173212
alg->execute();
174213
API::WorkspaceGroup_sptr outWS = alg->getProperty("OutputWorkspace");
@@ -199,6 +238,14 @@ void PolarizationEfficiencyCor::checkWildesProperties() const {
199238
if (!isDefault(Prop::POLARIZATION_ANALYSIS)) {
200239
throw std::invalid_argument("Property PolarizationAnalysis cannot be used with the Wildes method.");
201240
}
241+
242+
if (!isDefault(Prop::INPUT_FRED_SPIN_STATES)) {
243+
throw std::invalid_argument("Property SpinStatesInFredrikze cannot be used with the Wildes method.");
244+
}
245+
246+
if (!isDefault(Prop::OUTPUT_FRED_SPIN_STATES)) {
247+
throw std::invalid_argument("Property SpinStatesOutFredrikze cannot be used with the Wildes method.");
248+
}
202249
}
203250

204251
//----------------------------------------------------------------------------------------------

Framework/Algorithms/test/PolarizationCorrectionFredrikzeTest.h

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -380,33 +380,35 @@ class PolarizationCorrectionFredrikzeTest : public CxxTest::TestSuite {
380380
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
381381
alg.setProperty("Efficiencies", efficiencies);
382382
alg.setProperty("InputSpinStateOrder", "pp,pa,ap,aa");
383-
alg.setProperty("OutputSpinStateOrder", "xx,pa,ap,aa");
384-
// Ensure that the algorithm executes without throwing any errors
383+
alg.setProperty("OutputSpinStateOrder", "pa,pp,ap,aa");
385384
TS_ASSERT_THROWS_NOTHING(alg.execute());
386385
}
387386

388-
// void test_invalid_spinstate_order_does_not_match_input_workspaces() {
389-
// auto groupWS = std::make_shared<WorkspaceGroup>(); // Create group workspace
390-
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
391-
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
392-
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
393-
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
394-
// auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,1,1,1", "1,1,1,1", "1,1,1,1", "1,1,1,1");
395-
//
396-
// PolarizationCorrectionFredrikze alg;
397-
// alg.setChild(true);
398-
// alg.setRethrows(true);
399-
// alg.initialize();
400-
// alg.setProperty("InputWorkspace", groupWS);
401-
// alg.setPropertyValue("OutputWorkspace", "dummy");
402-
// alg.setProperty("PolarizationAnalysis", "PA");
403-
// alg.setProperty("Efficiencies", efficiencies);
404-
// alg.setProperty("InputSpinStateOrder", "pp,pa,ap");
405-
// alg.setProperty("OutputSpinStateOrder", "pp,pa,ap");
406-
//
407-
// TSM_ASSERT_THROWS("Spin state order count does not match input workspaces count, should throw", alg.execute(),
408-
// std::invalid_argument &);
409-
// }
387+
void test_invalid_input_and_output_spin_state_order_for_PA() {
388+
auto groupWS = std::make_shared<WorkspaceGroup>();
389+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
390+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
391+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
392+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
393+
394+
PolarizationCorrectionFredrikze alg;
395+
alg.setChild(true);
396+
alg.setRethrows(true);
397+
alg.initialize();
398+
alg.setProperty("InputWorkspace", groupWS);
399+
alg.setPropertyValue("OutputWorkspace", "dummy");
400+
alg.setProperty("PolarizationAnalysis", "PA");
401+
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
402+
alg.setProperty("Efficiencies", efficiencies);
403+
TSM_ASSERT_THROWS("Invalid value for property InputSpinStateOrder (string) from string pu,pa,ap,aa: When setting "
404+
"value of property InputSpinStateOrder: The spin states must either be one or two characters, "
405+
"with each being either a p or a",
406+
alg.setProperty("InputSpinStateOrder", "pu,pa,ap,aa"), std::invalid_argument &);
407+
TSM_ASSERT_THROWS("Invalid value for property OutputSpinStateOrder (string) from string pu,pa,ap,aa: When setting "
408+
"value of property OutputSpinStateOrder: The spin states must either be one or two characters, "
409+
"with each being either a p or a",
410+
alg.setProperty("OutputSpinStateOrder", "xx,pp,ap,aa"), std::invalid_argument &);
411+
}
410412

411413
// Test various combinations of spin state orders
412414
void test_various_spin_state_orders_for_PA() {
@@ -438,4 +440,47 @@ class PolarizationCorrectionFredrikzeTest : public CxxTest::TestSuite {
438440
}
439441
}
440442
}
443+
444+
void test_invalid_spin_state_order_for_PNR() {
445+
auto groupWS = std::make_shared<WorkspaceGroup>();
446+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
447+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
448+
449+
PolarizationCorrectionFredrikze alg;
450+
alg.setChild(true);
451+
alg.setRethrows(true);
452+
alg.initialize();
453+
alg.setProperty("InputWorkspace", groupWS);
454+
alg.setPropertyValue("OutputWorkspace", "dummy");
455+
alg.setProperty("PolarizationAnalysis", "PNR");
456+
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
457+
alg.setProperty("Efficiencies", efficiencies);
458+
TSM_ASSERT_THROWS(
459+
"Invalid value for property InputSpinStateOrder (string) from string x, y: When setting value of property "
460+
"InputSpinStateOrder: The spin states must either be one or two characters, with each being either a p or a",
461+
alg.setProperty("InputSpinStateOrder", "x, y"), std::invalid_argument &);
462+
TSM_ASSERT_THROWS(
463+
"Invalid value for property OutputSpinStateOrder (string) from string y, x: When setting value of property "
464+
"OutputSpinStateOrder: The spin states must either be one or two characters, with each being either a p or a",
465+
alg.setProperty("OutputSpinStateOrder", "y, x"), std::invalid_argument &);
466+
}
467+
468+
void test_valid_spin_state_order_for_PNR() {
469+
auto groupWS = std::make_shared<WorkspaceGroup>();
470+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
471+
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
472+
473+
PolarizationCorrectionFredrikze alg;
474+
alg.setChild(true);
475+
alg.setRethrows(true);
476+
alg.initialize();
477+
alg.setProperty("InputWorkspace", groupWS);
478+
alg.setPropertyValue("OutputWorkspace", "dummy");
479+
alg.setProperty("PolarizationAnalysis", "PNR");
480+
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
481+
alg.setProperty("Efficiencies", efficiencies);
482+
alg.setProperty("InputSpinStateOrder", "p, a");
483+
alg.setProperty("OutputSpinStateOrder", "a, p");
484+
TS_ASSERT_THROWS_NOTHING(alg.execute());
485+
}
441486
};

Framework/Algorithms/test/PolarizationEfficiencyCorTest.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,36 @@ class PolarizationEfficiencyCorTest : public CxxTest::TestSuite {
325325
WorkspaceGroup_sptr out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("out");
326326
TS_ASSERT_EQUALS(out->size(), 4);
327327
}
328+
void test_polarization_analysis_pa_with_spinstates() {
329+
PolarizationEfficiencyCor alg;
330+
alg.setRethrows(true);
331+
alg.initialize();
332+
alg.setProperty("OutputWorkspace", "out");
333+
alg.setProperty("InputWorkspaceGroup", createWorkspaceGroup(4));
334+
alg.setProperty("CorrectionMethod", "Fredrikze");
335+
alg.setProperty("Efficiencies", createEfficiencies("Fredrikze"));
336+
alg.setProperty("PolarizationAnalysis", "PA");
337+
alg.setProperty("SpinStatesInFredrikze", "pp,pa,ap,aa");
338+
alg.setProperty("SpinStatesOutFredrikze", "pa,pp,ap,aa");
339+
alg.execute();
340+
WorkspaceGroup_sptr out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("out");
341+
TS_ASSERT_EQUALS(out->size(), 4);
342+
}
343+
void test_polarization_analysis_pnr_with_spinstates() {
344+
PolarizationEfficiencyCor alg;
345+
alg.setRethrows(true);
346+
alg.initialize();
347+
alg.setProperty("OutputWorkspace", "out");
348+
alg.setProperty("InputWorkspaceGroup", createWorkspaceGroup(2));
349+
alg.setProperty("CorrectionMethod", "Fredrikze");
350+
alg.setProperty("Efficiencies", createEfficiencies("Fredrikze"));
351+
alg.setProperty("PolarizationAnalysis", "PNR");
352+
alg.setProperty("SpinStatesInFredrikze", "p, a");
353+
alg.setProperty("SpinStatesOutFredrikze", "a, p");
354+
alg.execute();
355+
WorkspaceGroup_sptr out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("out");
356+
TS_ASSERT_EQUALS(out->size(), 2);
357+
}
328358
void test_polarization_analysis_wrong_group_size() {
329359
PolarizationEfficiencyCor alg;
330360
alg.setRethrows(true);

buildconfig/CMake/CppCheck_Suppressions.txt.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/MergeRuns.cp
291291
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/RecordPythonScript.cpp:83
292292
knownConditionTrueFalse:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/PaalmanPingsAbsorptionCorrection.cpp:452
293293
constParameterReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/PolarizationCorrectionFredrikze.cpp:138
294-
constParameterReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/PolarizationCorrectionFredrikze.cpp:155
295294
missingOverride:${CMAKE_SOURCE_DIR}/Framework/Algorithms/inc/MantidAlgorithms/Rebin2D.h:26
296295
duplicateConditionalAssign:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/ReadGroupsFromFile.cpp:134
297296
missingOverride:${CMAKE_SOURCE_DIR}/Framework/Algorithms/inc/MantidAlgorithms/RebinByTimeBase.h:24

docs/source/algorithms/PolarizationEfficiencyCor-v1.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ to select between the two. The default is Wildes.
1616
The input workspaces can be passed in either via `InputWorkspaces` or
1717
`InputWorkspaceGroup` property but not both. An attempt to set both properties will result in an error.
1818

19-
The default values for the ``Flippers``, ``SpinStates`` and ``PolarizationAnalysis`` properties are empty strings and
20-
correspond to the actual defaults of the child algorithms: ``00, 01, 10, 11``/``++, +-, -+, --`` for Wildes and `PA`
21-
for Fredrikze.
19+
The default values for the ``Flippers``, ``SpinStates``, and ``PolarizationAnalysis`` properties are empty strings, and
20+
correspond to the actual defaults of the child algorithms: ``00, 01, 10, 11`` / ``++, +-, -+, --`` for Wildes,
21+
and ``pp, pa, ap, aa`` / ``p, a`` for ``PA`` and ``PNR`` in Fredrikze.
2222

2323
.. categories::
2424

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- A new ``InputSpinStates`` and ``OutputSpinStates`` property has been added to :ref:`algm-PolarizationCorrectionFredrikze` and
2+
:ref:`algm-PolarizationEfficiencyCor` to allow the order of the workspaces in the input and output Workspace Groups to be set.

0 commit comments

Comments
 (0)