Skip to content

Commit

Permalink
Added spin state order handling to PolarizationCorrectionFredrikze Co…
Browse files Browse the repository at this point in the history
…rrection Algorithm

This is a squashed version of mantidproject#37672

- Added spin state order handling to PolarizationCorrectionFredrikze algorithm

- Implemented InputSpinStateOrder and OutputSpinStateOrder properties.
- Updated execPA functions to dynamically handle different spin state orders.
- Extracted input and output spin state mapping logic into separate functions for better code organization and reusability.
- Ensured backward compatibility by setting default spin state orders.
- Added validation to ensure input workspace count matches the spin state entered

- Added spin state order handling to PolarizationCorrectionFredrikze algorithm

- Implemented InputSpinStateOrder and OutputSpinStateOrder properties.
- Updated execPA functions to dynamically handle different spin state orders.
- Extracted input and output spin state mapping logic into separate functions for better code organization and reusability.
- Ensured backward compatibility by setting default spin state orders.
- Added validation to ensure input workspace count matches the spin state entered

- Included unit tests to verify correct functionality and validation of spin state orders.

- splitted the spin state order string to a string of vectors using the polarization correction helper splitSpinStateString

added p and a to spin states

- modified unit tests to test spin state orders

- updated the documentation to describe the spin states and its usage

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

Refactor Fredrikze Algorithm SpinStates Implementation

- Renamed the input and output spin states
- refactored tests to eliminate code duplication
- added an helper function to fix crash which happens when user enter pp, aa as spinstates
- improved code based on PR review suggestions
- updated unit tests to handle crash which happens when user enter pp, aa as spinstates
- updated documentation

--amend-no-edit

[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
  • Loading branch information
yusufjimoh authored and peterfpeterson committed Jan 16, 2025
1 parent 95a7a9f commit adc8f9a
Show file tree
Hide file tree
Showing 9 changed files with 546 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include "MantidAPI/Algorithm.h"
#include "MantidAPI/WorkspaceGroup_fwd.h"
#include "MantidAlgorithms/DllConfig.h"
#include <memory>
#include <optional>
Expand Down Expand Up @@ -36,11 +37,16 @@ class MANTID_ALGORITHMS_DLL PolarizationCorrectionFredrikze final : public API::
void init() override;
void exec() override;
std::shared_ptr<Mantid::API::MatrixWorkspace> getEfficiencyWorkspace(const std::string &label);
std::shared_ptr<Mantid::API::WorkspaceGroup> execPA(const std::shared_ptr<Mantid::API::WorkspaceGroup> &inWS);
std::shared_ptr<Mantid::API::WorkspaceGroup> execPNR(const std::shared_ptr<Mantid::API::WorkspaceGroup> &inWS);
std::shared_ptr<Mantid::API::MatrixWorkspace> add(std::shared_ptr<Mantid::API::MatrixWorkspace> &lhsWS,
std::shared_ptr<Mantid::API::WorkspaceGroup> execPA(const std::shared_ptr<Mantid::API::WorkspaceGroup> &inWS,
const std::vector<std::string> &inputOrder,
const std::vector<std::string> &outputOrder);

std::shared_ptr<Mantid::API::WorkspaceGroup> execPNR(const std::shared_ptr<Mantid::API::WorkspaceGroup> &inWS,
const std::vector<std::string> &inputOrder,
const std::vector<std::string> &outputOrder);
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(std::shared_ptr<Mantid::API::MatrixWorkspace> &lhsWS,
std::shared_ptr<Mantid::API::MatrixWorkspace> multiply(const std::shared_ptr<Mantid::API::MatrixWorkspace> &lhsWS,
const double &rhs);
};

Expand Down
228 changes: 177 additions & 51 deletions Framework/Algorithms/src/PolarizationCorrectionFredrikze.cpp

Large diffs are not rendered by default.

60 changes: 54 additions & 6 deletions 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 OUTPUT_WILDES_SPIN_STATES{"SpinStatesOutWildes"};
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 @@ -107,7 +111,7 @@ void PolarizationEfficiencyCor::init() {

const auto spinStateValidator =
std::make_shared<SpinStateValidator>(std::unordered_set<int>{0, 2, 4}, true, '+', '-', true);
declareProperty(Prop::SPIN_STATES, "", spinStateValidator,
declareProperty(Prop::OUTPUT_WILDES_SPIN_STATES, "", spinStateValidator,
"The order of the spin states in the output workspace. (Wildes method only).");

std::vector<std::string> propOptions{"", "PA", "PNR"};
Expand All @@ -117,9 +121,39 @@ 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::OUTPUT_WILDES_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 @@ -147,8 +181,8 @@ void PolarizationEfficiencyCor::execWildes() {
if (!isDefault(Prop::FLIPPERS)) {
alg->setPropertyValue("Flippers", getPropertyValue(Prop::FLIPPERS));
}
if (!isDefault(Prop::SPIN_STATES)) {
alg->setPropertyValue("SpinStates", getPropertyValue(Prop::SPIN_STATES));
if (!isDefault(Prop::OUTPUT_WILDES_SPIN_STATES)) {
alg->setPropertyValue("SpinStates", getPropertyValue(Prop::OUTPUT_WILDES_SPIN_STATES));
}
auto out = getPropertyValue(Prop::OUTPUT_WORKSPACES);
alg->setPropertyValue("OutputWorkspace", out);
Expand All @@ -169,6 +203,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("InputSpinStates", getPropertyValue(Prop::INPUT_FRED_SPIN_STATES));
}
if (!isDefault(Prop::OUTPUT_FRED_SPIN_STATES)) {
alg->setPropertyValue("OutputSpinStates", 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 +239,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 All @@ -210,8 +258,8 @@ void PolarizationEfficiencyCor::checkFredrikzeProperties() const {
if (!isDefault(Prop::FLIPPERS)) {
throw std::invalid_argument("Property Flippers cannot be used with the Fredrikze method.");
}
if (!isDefault(Prop::SPIN_STATES)) {
throw std::invalid_argument("Property SpinStates cannot be used with the Fredrikze method.");
if (!isDefault(Prop::OUTPUT_WILDES_SPIN_STATES)) {
throw std::invalid_argument("Property SpinStatesOutWildes cannot be used with the Fredrikze method.");
}
}

Expand Down
Loading

0 comments on commit adc8f9a

Please sign in to comment.