Skip to content

Commit

Permalink
Add Input and Output SpinStates for FredrikzeAlgorithm in Polarizatio…
Browse files Browse the repository at this point in the history
…nEfficiencyCor

- 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
  • Loading branch information
yusufjimoh committed Sep 2, 2024
1 parent ed5463c commit 37d2200
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MANTID_ALGORITHMS_DLL PolarizationCorrectionFredrikze final : public API::
std::shared_ptr<Mantid::API::MatrixWorkspace> add(const std::shared_ptr<Mantid::API::MatrixWorkspace> &lhsWS,
const double &rhs);
std::shared_ptr<Mantid::API::MatrixWorkspace> multiply(const std::shared_ptr<Mantid::API::MatrixWorkspace> &lhsWS,
const double &rhs);
double &rhs);
};

} // namespace Algorithms
Expand Down
15 changes: 9 additions & 6 deletions Framework/Algorithms/src/PolarizationCorrectionFredrikze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceHistory.h"
#include "MantidAlgorithms/PolarizationCorrections/PolarizationCorrectionsHelpers.h"
#include "MantidAlgorithms/PolarizationCorrections/SpinStateValidator.h"
#include "MantidDataObjects/WorkspaceSingleValue.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/ListValidator.h"
Expand Down Expand Up @@ -151,7 +152,7 @@ const std::string PolarizationCorrectionFredrikze::summary() const {
* @param rhs : WS to multiply by (constant value)
* @return Multiplied Workspace.
*/
MatrixWorkspace_sptr PolarizationCorrectionFredrikze::multiply(const MatrixWorkspace_sptr &lhsWS, const double &rhs) {
MatrixWorkspace_sptr PolarizationCorrectionFredrikze::multiply(const MatrixWorkspace_sptr &lhsWS, double &rhs) {
auto multiply = this->createChildAlgorithm("Multiply");
auto rhsWS = std::make_shared<DataObjects::WorkspaceSingleValue>(rhs);
multiply->initialize();
Expand Down Expand Up @@ -244,13 +245,16 @@ void PolarizationCorrectionFredrikze::init() {
std::make_unique<WorkspaceProperty<Mantid::API::WorkspaceGroup>>("OutputWorkspace", "", Direction::Output),
"An output workspace.");

const auto spinStateValidator =
std::make_shared<SpinStateValidator>(std::unordered_set<int>{2, 4}, true, 'p', 'a', true);

declareProperty(
std::make_unique<PropertyWithValue<std::string>>(inputSpinStateOrderLabel, "", Direction::Input),
inputSpinStateOrderLabel, "", spinStateValidator,
"The order of spin states in the input workspace group. TThe possible values are 'pp,pa,ap,aa' or 'p,a'.");

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

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

g_log.warning(inputOrderStr);

const std::vector<std::string> inputOrder = PolarizationCorrectionsHelpers::splitSpinStateString(inputOrderStr);
const std::vector<std::string> outputOrder = PolarizationCorrectionsHelpers::splitSpinStateString(outputOrderStr);

Expand All @@ -433,6 +435,7 @@ void PolarizationCorrectionFredrikze::exec() {
outWS = execPNR(inWS, inputOrder, outputOrder);
g_log.notice("PNR polarization correction");
}

this->setProperty("OutputWorkspace", outWS);
}

Expand Down
49 changes: 48 additions & 1 deletion Framework/Algorithms/src/PolarizationEfficiencyCor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/StringTokenizer.h"

Expand All @@ -26,13 +27,16 @@ namespace {
/// Property names.
namespace Prop {
static const std::string FLIPPERS{"Flippers"};
static const std::string SPIN_STATES{"SpinStates"};
static const std::string SPIN_STATES{"SpinStatesWildes"};
static const std::string POLARIZATION_ANALYSIS{"PolarizationAnalysis"};
static const std::string EFFICIENCIES{"Efficiencies"};
static const std::string INPUT_WORKSPACES{"InputWorkspaces"};
static const std::string INPUT_WORKSPACE_GROUP{"InputWorkspaceGroup"};
static const std::string OUTPUT_WORKSPACES{"OutputWorkspace"};
static const std::string CORRECTION_METHOD{"CorrectionMethod"};
static const std::string INPUT_FRED_SPIN_STATES{"SpinStatesInFredrikze"};
static const std::string OUTPUT_FRED_SPIN_STATES{"SpinStatesOutFredrikze"};

} // namespace Prop

namespace CorrectionMethod {
Expand Down Expand Up @@ -117,9 +121,38 @@ void PolarizationEfficiencyCor::init() {
"PA: Full Polarization Analysis PNR-PA "
"(Fredrikze method only)");

const auto fredrikzeSpinStateValidator =
std::make_shared<SpinStateValidator>(std::unordered_set<int>{2, 4}, true, 'p', 'a', true);

declareProperty(Prop::INPUT_FRED_SPIN_STATES, "", fredrikzeSpinStateValidator,
"The order of spin states in the input workspace group. The possible values are 'pp,pa,ap,aa' or "
"'p,a'. (Fredrikze method only).");

declareProperty(Prop::OUTPUT_FRED_SPIN_STATES, "", fredrikzeSpinStateValidator,
"The order of spin states in the output workspace group. The possible values are 'pp,pa,ap,aa' or "
"'p,a'. (Fredrikze method only).");

declareProperty(
std::make_unique<WorkspaceProperty<WorkspaceGroup>>(Prop::OUTPUT_WORKSPACES, "", Kernel::Direction::Output),
"A group of polarization efficiency corrected workspaces.");

setPropertySettings(Prop::SPIN_STATES, std::make_unique<EnabledWhenProperty>(
Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::WILDES));

setPropertySettings(Prop::FLIPPERS, std::make_unique<EnabledWhenProperty>(
Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::WILDES));

setPropertySettings(
Prop::INPUT_FRED_SPIN_STATES,
std::make_unique<EnabledWhenProperty>(Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::FREDRIKZE));

setPropertySettings(
Prop::OUTPUT_FRED_SPIN_STATES,
std::make_unique<EnabledWhenProperty>(Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::FREDRIKZE));

setPropertySettings(
"PolarizationAnalysis",
std::make_unique<EnabledWhenProperty>(Prop::CORRECTION_METHOD, Kernel::IS_EQUAL_TO, CorrectionMethod::FREDRIKZE));
}

//----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -169,6 +202,12 @@ void PolarizationEfficiencyCor::execFredrikze() {
if (!isDefault(Prop::POLARIZATION_ANALYSIS)) {
alg->setPropertyValue("PolarizationAnalysis", getPropertyValue(Prop::POLARIZATION_ANALYSIS));
}
if (!isDefault(Prop::INPUT_FRED_SPIN_STATES)) {
alg->setPropertyValue("InputSpinStateOrder", getPropertyValue(Prop::INPUT_FRED_SPIN_STATES));
}
if (!isDefault(Prop::OUTPUT_FRED_SPIN_STATES)) {
alg->setPropertyValue("OutputSpinStateOrder", getPropertyValue(Prop::OUTPUT_FRED_SPIN_STATES));
}
alg->setPropertyValue("OutputWorkspace", getPropertyValue(Prop::OUTPUT_WORKSPACES));
alg->execute();
API::WorkspaceGroup_sptr outWS = alg->getProperty("OutputWorkspace");
Expand Down Expand Up @@ -199,6 +238,14 @@ void PolarizationEfficiencyCor::checkWildesProperties() const {
if (!isDefault(Prop::POLARIZATION_ANALYSIS)) {
throw std::invalid_argument("Property PolarizationAnalysis cannot be used with the Wildes method.");
}

if (!isDefault(Prop::INPUT_FRED_SPIN_STATES)) {
throw std::invalid_argument("Property SpinStatesInFredrikze cannot be used with the Wildes method.");
}

if (!isDefault(Prop::OUTPUT_FRED_SPIN_STATES)) {
throw std::invalid_argument("Property SpinStatesOutFredrikze cannot be used with the Wildes method.");
}
}

//----------------------------------------------------------------------------------------------
Expand Down
93 changes: 69 additions & 24 deletions Framework/Algorithms/test/PolarizationCorrectionFredrikzeTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,33 +380,35 @@ class PolarizationCorrectionFredrikzeTest : public CxxTest::TestSuite {
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
alg.setProperty("Efficiencies", efficiencies);
alg.setProperty("InputSpinStateOrder", "pp,pa,ap,aa");
alg.setProperty("OutputSpinStateOrder", "xx,pa,ap,aa");
// Ensure that the algorithm executes without throwing any errors
alg.setProperty("OutputSpinStateOrder", "pa,pp,ap,aa");
TS_ASSERT_THROWS_NOTHING(alg.execute());
}

// void test_invalid_spinstate_order_does_not_match_input_workspaces() {
// auto groupWS = std::make_shared<WorkspaceGroup>(); // Create group workspace
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
// groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
// auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,1,1,1", "1,1,1,1", "1,1,1,1", "1,1,1,1");
//
// PolarizationCorrectionFredrikze alg;
// alg.setChild(true);
// alg.setRethrows(true);
// alg.initialize();
// alg.setProperty("InputWorkspace", groupWS);
// alg.setPropertyValue("OutputWorkspace", "dummy");
// alg.setProperty("PolarizationAnalysis", "PA");
// alg.setProperty("Efficiencies", efficiencies);
// alg.setProperty("InputSpinStateOrder", "pp,pa,ap");
// alg.setProperty("OutputSpinStateOrder", "pp,pa,ap");
//
// TSM_ASSERT_THROWS("Spin state order count does not match input workspaces count, should throw", alg.execute(),
// std::invalid_argument &);
// }
void test_invalid_input_and_output_spin_state_order_for_PA() {
auto groupWS = std::make_shared<WorkspaceGroup>();
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));

PolarizationCorrectionFredrikze alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", groupWS);
alg.setPropertyValue("OutputWorkspace", "dummy");
alg.setProperty("PolarizationAnalysis", "PA");
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
alg.setProperty("Efficiencies", efficiencies);
TSM_ASSERT_THROWS("Invalid value for property InputSpinStateOrder (string) from string pu,pa,ap,aa: When setting "
"value of property InputSpinStateOrder: The spin states must either be one or two characters, "
"with each being either a p or a",
alg.setProperty("InputSpinStateOrder", "pu,pa,ap,aa"), std::invalid_argument &);
TSM_ASSERT_THROWS("Invalid value for property OutputSpinStateOrder (string) from string pu,pa,ap,aa: When setting "
"value of property OutputSpinStateOrder: The spin states must either be one or two characters, "
"with each being either a p or a",
alg.setProperty("OutputSpinStateOrder", "xx,pp,ap,aa"), std::invalid_argument &);
}

// Test various combinations of spin state orders
void test_various_spin_state_orders_for_PA() {
Expand Down Expand Up @@ -438,4 +440,47 @@ class PolarizationCorrectionFredrikzeTest : public CxxTest::TestSuite {
}
}
}

void test_invalid_spin_state_order_for_PNR() {
auto groupWS = std::make_shared<WorkspaceGroup>();
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));

PolarizationCorrectionFredrikze alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", groupWS);
alg.setPropertyValue("OutputWorkspace", "dummy");
alg.setProperty("PolarizationAnalysis", "PNR");
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
alg.setProperty("Efficiencies", efficiencies);
TSM_ASSERT_THROWS(
"Invalid value for property InputSpinStateOrder (string) from string x, y: When setting value of property "
"InputSpinStateOrder: The spin states must either be one or two characters, with each being either a p or a",
alg.setProperty("InputSpinStateOrder", "x, y"), std::invalid_argument &);
TSM_ASSERT_THROWS(
"Invalid value for property OutputSpinStateOrder (string) from string y, x: When setting value of property "
"OutputSpinStateOrder: The spin states must either be one or two characters, with each being either a p or a",
alg.setProperty("OutputSpinStateOrder", "y, x"), std::invalid_argument &);
}

void test_valid_spin_state_order_for_PNR() {
auto groupWS = std::make_shared<WorkspaceGroup>();
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));

PolarizationCorrectionFredrikze alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", groupWS);
alg.setPropertyValue("OutputWorkspace", "dummy");
alg.setProperty("PolarizationAnalysis", "PNR");
auto efficiencies = makeEfficiencies(create1DWorkspace(4, 1, 1), "1,0,0,0", "1,0,0,0", "1,0,0,0", "1,0,0,0");
alg.setProperty("Efficiencies", efficiencies);
alg.setProperty("InputSpinStateOrder", "p, a");
alg.setProperty("OutputSpinStateOrder", "a, p");
TS_ASSERT_THROWS_NOTHING(alg.execute());
}
};
30 changes: 30 additions & 0 deletions Framework/Algorithms/test/PolarizationEfficiencyCorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,36 @@ class PolarizationEfficiencyCorTest : public CxxTest::TestSuite {
WorkspaceGroup_sptr out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("out");
TS_ASSERT_EQUALS(out->size(), 4);
}
void test_polarization_analysis_pa_with_spinstates() {
PolarizationEfficiencyCor alg;
alg.setRethrows(true);
alg.initialize();
alg.setProperty("OutputWorkspace", "out");
alg.setProperty("InputWorkspaceGroup", createWorkspaceGroup(4));
alg.setProperty("CorrectionMethod", "Fredrikze");
alg.setProperty("Efficiencies", createEfficiencies("Fredrikze"));
alg.setProperty("PolarizationAnalysis", "PA");
alg.setProperty("SpinStatesInFredrikze", "pp,pa,ap,aa");
alg.setProperty("SpinStatesOutFredrikze", "pa,pp,ap,aa");
alg.execute();
WorkspaceGroup_sptr out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("out");
TS_ASSERT_EQUALS(out->size(), 4);
}
void test_polarization_analysis_pnr_with_spinstates() {
PolarizationEfficiencyCor alg;
alg.setRethrows(true);
alg.initialize();
alg.setProperty("OutputWorkspace", "out");
alg.setProperty("InputWorkspaceGroup", createWorkspaceGroup(2));
alg.setProperty("CorrectionMethod", "Fredrikze");
alg.setProperty("Efficiencies", createEfficiencies("Fredrikze"));
alg.setProperty("PolarizationAnalysis", "PNR");
alg.setProperty("SpinStatesInFredrikze", "p, a");
alg.setProperty("SpinStatesOutFredrikze", "a, p");
alg.execute();
WorkspaceGroup_sptr out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("out");
TS_ASSERT_EQUALS(out->size(), 2);
}
void test_polarization_analysis_wrong_group_size() {
PolarizationEfficiencyCor alg;
alg.setRethrows(true);
Expand Down
1 change: 0 additions & 1 deletion buildconfig/CMake/CppCheck_Suppressions.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/MergeRuns.cp
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/RecordPythonScript.cpp:83
knownConditionTrueFalse:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/PaalmanPingsAbsorptionCorrection.cpp:452
constParameterReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/PolarizationCorrectionFredrikze.cpp:138
constParameterReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/PolarizationCorrectionFredrikze.cpp:155
missingOverride:${CMAKE_SOURCE_DIR}/Framework/Algorithms/inc/MantidAlgorithms/Rebin2D.h:26
duplicateConditionalAssign:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/ReadGroupsFromFile.cpp:134
missingOverride:${CMAKE_SOURCE_DIR}/Framework/Algorithms/inc/MantidAlgorithms/RebinByTimeBase.h:24
Expand Down
6 changes: 3 additions & 3 deletions docs/source/algorithms/PolarizationEfficiencyCor-v1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ to select between the two. The default is Wildes.
The input workspaces can be passed in either via `InputWorkspaces` or
`InputWorkspaceGroup` property but not both. An attempt to set both properties will result in an error.

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

.. categories::

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

0 comments on commit 37d2200

Please sign in to comment.