From f8e24c49b6866d9c9a4196b33ebd754eda018d9b Mon Sep 17 00:00:00 2001 From: Oluwaseun Jimoh Date: Wed, 18 Dec 2024 16:51:48 +0000 Subject: [PATCH 1/5] created helper functions to reduce code duplications in PolarizationCorrectionWildestest --- .../test/PolarizationCorrectionWildesTest.h | 293 +++++++++++------- 1 file changed, 175 insertions(+), 118 deletions(-) diff --git a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h index 0ef598449633..f1d2e73c8b34 100644 --- a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h +++ b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h @@ -32,6 +32,71 @@ using namespace Mantid::Kernel; using namespace Mantid::Algorithms; using namespace Mantid::Algorithms::PolarizationCorrectionsHelpers; +namespace { + +MatrixWorkspace_sptr createWorkspace(size_t nHist = 2, std::optional edges = std::nullopt, + std::optional counts = std::nullopt) { + if (!edges) { + edges = BinEdges{0.3, 0.6, 0.9, 1.2}; + } + + const auto yVal = 2.3; + if (!counts) { + counts = Counts{yVal, 4.2 * (yVal), yVal}; + } + + return create(nHist, Histogram(*edges, *counts)); +} + +MatrixWorkspace_sptr createWorkspace(size_t nHist, const Counts &counts) { + return createWorkspace(nHist, std::nullopt, counts); +} + +MatrixWorkspace_sptr createWorkspace(const Counts &counts) { return createWorkspace(2, std::nullopt, counts); } + +std::vector cloneWorkspaces(const MatrixWorkspace_sptr &sourceWorkspace, size_t numClones) { + std::vector clonedWorkspaces; + + clonedWorkspaces.push_back(sourceWorkspace); + for (size_t i = 1; i < numClones; ++i) { + clonedWorkspaces.push_back(sourceWorkspace->clone()); + } + + return clonedWorkspaces; +} + +std::vector generateWorkspaceNames(size_t numClones) { + std::vector wsNames; + + for (size_t i = 0; i < numClones; ++i) { + wsNames.push_back("ws" + std::to_string(i)); + } + + return wsNames; +} +void prepareAndRegisterWorkspaces(std::vector &wsNames, + const std::vector &wsList, const size_t nHist) { + + for (size_t i = 0; i != wsNames.size(); ++i) { + for (size_t j = 0; j != nHist; ++j) { + wsList[i]->mutableY(j) *= static_cast(i + 1); + wsList[i]->mutableE(j) *= static_cast(i + 1); + } + AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); + } +} + +std::vector createWorkspaceList(std::optional counts, size_t numClones) { + auto baseWorkspace = counts.has_value() ? createWorkspace(*counts) : createWorkspace(); + + return cloneWorkspaces(baseWorkspace, numClones); +} + +std::vector createWorkspaceList(size_t numClones) { + return createWorkspaceList(std::nullopt, numClones); +} + +} // namespace class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { public: // This pair of boilerplate methods prevent the suite being created statically @@ -76,22 +141,24 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_IdealCaseTwoInputsWithAnalyzer() { constexpr size_t nBins{3}; constexpr size_t nHist{2}; + constexpr size_t numClones{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, 4.2 * yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{std::initializer_list{"ws00", "ws11"}}; - const std::array wsList{{ws00, ws11}}; + + auto wsList = createWorkspaceList(numClones); + auto wsNames = generateWorkspaceNames(numClones); for (size_t i = 0; i != nHist; ++i) { - ws11->mutableY(i) *= 2.; - ws11->mutableE(i) *= 2.; + wsList[1]->mutableY(i) *= 2.; + wsList[1]->mutableE(i) *= 2.; } AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); + auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "00, 11"); TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) + const std::array POL_DIRS{{"++", "+-", "-+", "--"}}; for (size_t i = 0; i != 4; ++i) { const auto &dir = POL_DIRS[i]; @@ -137,19 +204,23 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { constexpr size_t nHist{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; + constexpr size_t numClones{2}; + // confirm the counts in this method if this is correct Counts counts{yVal, 4.2 * yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{std::initializer_list{"ws00", "ws11"}}; - const std::array wsList{{ws00, ws11}}; + + auto wsList = createWorkspaceList(numClones); + auto wsNames = generateWorkspaceNames(numClones); for (size_t i = 0; i != nHist; ++i) { - ws11->mutableY(i) *= 2.; - ws11->mutableE(i) *= 2.; + wsList[1]->mutableY(i) *= 2.; + wsList[1]->mutableE(i) *= 2.; } + AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); + auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "0, 1"); + compareCorrectionResults( outputWS, {"_++", "_--"}, nHist, nBins, edges, counts, [](size_t wsIndex, double c) { return c * static_cast(wsIndex + 1); }, @@ -162,7 +233,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, 4.2 * yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); + auto const ws00 = createWorkspace(); const std::vector wsNames{{"ws00"}}; AnalysisDataService::Instance().addOrReplace(wsNames.front(), ws00); auto effWS = idealEfficiencies(edges); @@ -188,26 +259,51 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_FullCorrections() { constexpr size_t nHist{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; + constexpr size_t numClones{4}; const double yVal = 2.3; + BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{yVal, yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); - MatrixWorkspace_sptr ws01 = ws00->clone(); - MatrixWorkspace_sptr ws10 = ws00->clone(); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; - const std::array wsList{{ws00, ws01, ws10, ws11}}; - for (size_t i = 0; i != 4; ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + + // auto ws00 = createWorkspace(counts); + // MatrixWorkspace_sptr ws01 = ws00->clone(); + // MatrixWorkspace_sptr ws10 = ws00->clone(); + // MatrixWorkspace_sptr ws11 = ws00->clone(); + // const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; + // const std::array wsList{{ws00, ws01, ws10, ws11}}; + // for (size_t i = 0; i != 4; ++i) { + // for (size_t j = 0; j != nHist; ++j) { + // wsList[i]->mutableY(j) *= static_cast(i + 1); + // wsList[i]->mutableE(j) *= static_cast(i + 1); + // } + // AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); + // } + auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS); + TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) - fullFourInputsResultsCheck(outputWS, ws00, ws01, ws10, ws11, effWS, counts); + fullFourInputsResultsCheck(outputWS, wsList[0], wsList[1], wsList[2], wsList[3], effWS, counts); + } + + void test_FullCorrectionss() { + constexpr size_t nHist{2}; + constexpr size_t numClones{4}; + BinEdges edges{0.3, 0.6, 0.9, 1.2}; + const double yVal = 2.3; + Counts counts{yVal, yVal, yVal}; + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + + auto effWS = efficiencies(edges); + WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS); + + fullFourInputsResultsCheck(outputWS, wsList[0], wsList[1], wsList[2], wsList[3], effWS, counts); } void test_ThreeInputsWithMissing01FlipperConfiguration() { threeInputsTest("01"); } @@ -217,26 +313,22 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_TwoInputsWithAnalyzer() { constexpr size_t nHist{2}; constexpr size_t nBins{3}; + constexpr size_t numClones{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); MatrixWorkspace_sptr ws01 = nullptr; MatrixWorkspace_sptr ws10 = nullptr; - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{std::initializer_list{"ws00", "ws11"}}; - const std::array wsList{{ws00, ws11}}; - for (size_t i = 0; i != 2; ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "00, 11"); + TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) - solveMissingIntensities(ws00, ws01, ws10, ws11, effWS); + solveMissingIntensities(wsList[0], ws01, ws10, wsList[1], effWS); const double F1 = effWS->y(0).front(); const double F1e = effWS->e(0).front(); const double F2 = effWS->y(1).front(); @@ -245,9 +337,9 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const double P1e = effWS->e(2).front(); const double P2 = effWS->y(3).front(); const double P2e = effWS->e(3).front(); - const Eigen::Vector4d y{ws00->y(0).front(), ws01->y(0).front(), ws10->y(0).front(), ws11->y(0).front()}; + const Eigen::Vector4d y{wsList[0]->y(0).front(), ws01->y(0).front(), ws10->y(0).front(), wsList[1]->y(0).front()}; const auto expected = correction(y, F1, F2, P1, P2); - const Eigen::Vector4d e{ws00->e(0).front(), ws01->e(0).front(), ws10->e(0).front(), ws11->e(0).front()}; + const Eigen::Vector4d e{wsList[0]->e(0).front(), ws01->e(0).front(), ws10->e(0).front(), wsList[1]->e(0).front()}; const auto expectedError = error(y, e, F1, F1e, F2, F2e, P1, P1e, P2, P2e); // This test constructs the expected missing I01 and I10 intensities // slightly different from what the algorithm does: I10 is solved @@ -264,30 +356,26 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_TwoInputsWithoutAnalyzer() { constexpr size_t nHist{2}; constexpr size_t nBins{3}; + constexpr size_t numClones{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{std::initializer_list{"ws00", "ws11"}}; - const std::array wsList{{ws00, ws11}}; - for (size_t i = 0; i != 2; ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "0, 1"); + TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 2) const double F1 = effWS->y(0).front(); const double F1e = effWS->e(0).front(); const double P1 = effWS->y(2).front(); const double P1e = effWS->e(2).front(); - const Eigen::Vector2d y{ws00->y(0).front(), ws11->y(0).front()}; + const Eigen::Vector2d y{wsList[0]->y(0).front(), wsList[1]->y(0).front()}; const auto expected = correctionWithoutAnalyzer(y, F1, P1); - const Eigen::Vector2d e{ws00->e(0).front(), ws11->e(0).front()}; + const Eigen::Vector2d e{wsList[0]->e(0).front(), wsList[1]->e(0).front()}; const auto expectedError = errorWithoutAnalyzer(y, e, F1, F1e, P1, P1e); compareCorrectionResults( outputWS, {"_++", "_--"}, nHist, nBins, edges, counts, [&expected](size_t i, double) { return expected[i]; }, @@ -300,7 +388,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); + auto const ws00 = createWorkspace(counts); const std::string wsName{"ws00"}; AnalysisDataService::Instance().addOrReplace(wsName, ws00); auto effWS = efficiencies(edges); @@ -326,7 +414,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_FailureWhenEfficiencyHistogramIsMissing() { BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; - MatrixWorkspace_sptr ws00 = create(1, Histogram(edges, counts)); + auto const ws00 = createWorkspace(1, counts); const std::string wsName{"ws00"}; AnalysisDataService::Instance().addOrReplace(wsName, ws00); auto effWS = idealEfficiencies(edges); @@ -343,7 +431,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_FailureWhenEfficiencyXDataMismatches() { BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; - MatrixWorkspace_sptr ws00 = create(1, Histogram(edges, counts)); + auto const ws00 = createWorkspace(1, counts); const std::string wsName{"ws00"}; AnalysisDataService::Instance().addOrReplace(wsName, ws00); auto effWS = idealEfficiencies(edges); @@ -357,7 +445,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { constexpr size_t nHist{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); + auto const ws00 = createWorkspace(counts); MatrixWorkspace_sptr ws01 = ws00->clone(); MatrixWorkspace_sptr ws10 = create(nHist + 1, Histogram(edges, counts)); MatrixWorkspace_sptr ws11 = ws00->clone(); @@ -371,10 +459,9 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_FailureWhenAnInputWorkspaceIsMissing() { - constexpr size_t nHist{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); + auto const ws00 = createWorkspace(counts); MatrixWorkspace_sptr ws01 = ws00->clone(); MatrixWorkspace_sptr ws11 = ws00->clone(); AnalysisDataService::Instance().addOrReplace("ws00", ws00); @@ -400,22 +487,21 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateOrderInOutputWorkspacegroup() { BinEdges edges{0.3, 0.6, 0.9, 1.2}; auto effWS = idealEfficiencies(edges); - constexpr size_t nHist{2}; Counts counts{2.3, 9.6, 2.3}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); + auto const ws00 = createWorkspace(counts); MatrixWorkspace_sptr ws01 = ws00->clone(); MatrixWorkspace_sptr ws10 = ws00->clone(); MatrixWorkspace_sptr ws11 = ws00->clone(); const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; const std::array wsList{{ws00, ws01, ws10, ws11}}; - for (size_t i = 0; i != 4; ++i) { + for (size_t i = 0; i < 4; ++i) { AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); } WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS); TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) const std::array OUTPUT_ORDER{{"++", "+-", "-+", "--"}}; - for (size_t i = 0; i != 4; ++i) { + for (size_t i = 0; i < 4; ++i) { auto ws = outputWS->getItem(i); TS_ASSERT(ws) const std::string expectedName = m_outputWSName + std::string("_") + OUTPUT_ORDER[i]; @@ -425,30 +511,24 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateAddedToSampleLogWhenRequested() { constexpr size_t nHist{2}; + constexpr size_t numClones{4}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); - MatrixWorkspace_sptr ws01 = ws00->clone(); - MatrixWorkspace_sptr ws10 = ws00->clone(); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; - const std::array wsList{{ws00, ws01, ws10, ws11}}; - for (size_t i = 0; i != 4; ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + const auto effWS = efficiencies(edges); auto alg = createWildesAlg(wsNames, effWS); alg->setProperty("AddSpinStateToLog", true); + const auto outputWS = runAlg(std::move(alg)); TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) const std::array EXPECTED_LOG_VALUES{ {SpinStatesORSO::PP, SpinStatesORSO::PM, SpinStatesORSO::MP, SpinStatesORSO::MM}}; - for (size_t i = 0; i != 4; ++i) { + for (size_t i = 0; i < 4; ++i) { MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); TS_ASSERT(ws) const auto &run = ws->run(); @@ -459,27 +539,22 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateAddedToSampleLogWhenRequestedNoAnalyser() { constexpr size_t nHist{2}; + constexpr size_t numClones{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{std::initializer_list{"ws00", "ws11"}}; - const std::array wsList{{ws00, ws11}}; - for (size_t i = 0; i != 2; ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + const auto effWS = efficiencies(edges); auto alg = createWildesAlg(wsNames, effWS, "0, 1"); alg->setProperty("AddSpinStateToLog", true); const auto outputWS = runAlg(std::move(alg)); TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 2) const std::array EXPECTED_LOG_VALUES{{SpinStatesORSO::PO, SpinStatesORSO::MO}}; - for (size_t i = 0; i != 2; ++i) { + for (size_t i = 0; i < 2; ++i) { MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); TS_ASSERT(ws) const auto &run = ws->run(); @@ -492,20 +567,14 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { constexpr size_t nHist{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; + // confrim nuymber of clones of 4 or2 + constexpr size_t numClones{4}; Counts counts{yVal, yVal, yVal}; - MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); - MatrixWorkspace_sptr ws01 = ws00->clone(); - MatrixWorkspace_sptr ws10 = ws00->clone(); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; - const std::array wsList{{ws00, ws01, ws10, ws11}}; - for (size_t i = 0; i != 4; ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + const auto effWS = efficiencies(edges); const auto outputWS = runCorrectionWildes(wsNames, effWS); TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) @@ -519,18 +588,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { private: const std::string m_outputWSName{"output"}; - void setupWorkspaceData(std::vector &wsNames, - const std::vector &wsList, const size_t nHist) { - - for (size_t i = 0; i != wsNames.size(); ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } - } - std::unique_ptr createWildesAlg(const std::vector &inputWorkspaces, Mantid::API::MatrixWorkspace_sptr effWs, const std::string &flippers = "", @@ -693,7 +750,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { std::vector wsNames{"ws00", "ws01", "ws10", "ws11"}; const std::vector wsList{ws00, ws01, ws10, ws11}; - setupWorkspaceData(wsNames, wsList, nHist); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); // Re-order the input workspace names to match the input flipper configuration const auto &flipperConfigVec = splitSpinStateString(flipperConfig); @@ -733,7 +790,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const std::string presentFlipperConf = missingFlipperConf == "01" ? "10" : "01"; std::vector wsNames{"ws00", "wsXX", "ws11"}; const std::vector wsList{ws00, wsXX, ws11}; - setupWorkspaceData(wsNames, wsList, nHist); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); // Re-order the input workspace names to match the input flipper configuration const auto &flipperConfigVec = splitSpinStateString(flipperConfig); From 7044921ecaab20668fad938544f934ecde2a717f Mon Sep 17 00:00:00 2001 From: Oluwaseun Jimoh Date: Wed, 18 Dec 2024 19:10:34 +0000 Subject: [PATCH 2/5] refactor methods with spinstates, added more helper methods to reduce code duplication --- .../test/PolarizationCorrectionWildesTest.h | 286 +++++++++--------- 1 file changed, 139 insertions(+), 147 deletions(-) diff --git a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h index f1d2e73c8b34..2fab3d274225 100644 --- a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h +++ b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h @@ -54,7 +54,7 @@ MatrixWorkspace_sptr createWorkspace(size_t nHist, const Counts &counts) { MatrixWorkspace_sptr createWorkspace(const Counts &counts) { return createWorkspace(2, std::nullopt, counts); } -std::vector cloneWorkspaces(const MatrixWorkspace_sptr &sourceWorkspace, size_t numClones) { +std::vector cloneWorkspaces(const MatrixWorkspace_sptr &sourceWorkspace, const size_t numClones) { std::vector clonedWorkspaces; clonedWorkspaces.push_back(sourceWorkspace); @@ -65,7 +65,7 @@ std::vector cloneWorkspaces(const MatrixWorkspace_sptr &so return clonedWorkspaces; } -std::vector generateWorkspaceNames(size_t numClones) { +std::vector generateWorkspaceNames(const size_t numClones) { std::vector wsNames; for (size_t i = 0; i < numClones; ++i) { @@ -74,6 +74,17 @@ std::vector generateWorkspaceNames(size_t numClones) { return wsNames; } +void addWorkspacesToService(const std::vector &wsNames, const std::vector &wsList, + const size_t numWorkspaces) { + for (size_t i = 0; i < numWorkspaces; ++i) { + AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); + } +} + +void addWorkspaceToService(const std::string &wsName, const MatrixWorkspace_sptr &ws) { + AnalysisDataService::Instance().addOrReplace(wsName, ws); +} + void prepareAndRegisterWorkspaces(std::vector &wsNames, const std::vector &wsList, const size_t nHist) { @@ -82,20 +93,55 @@ void prepareAndRegisterWorkspaces(std::vector &wsNames, wsList[i]->mutableY(j) *= static_cast(i + 1); wsList[i]->mutableE(j) *= static_cast(i + 1); } - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); + addWorkspacesToService(wsNames, wsList, wsNames.size()); } } -std::vector createWorkspaceList(std::optional counts, size_t numClones) { +std::vector createWorkspaceList(const std::optional counts, const size_t numClones) { auto baseWorkspace = counts.has_value() ? createWorkspace(*counts) : createWorkspace(); return cloneWorkspaces(baseWorkspace, numClones); } -std::vector createWorkspaceList(size_t numClones) { +std::vector createWorkspaceList(const size_t numClones) { return createWorkspaceList(std::nullopt, numClones); } +void setupWorkspacesForIdealCasesTwoInput(const std::vector &wsNames, + const std::vector &wsList, + const size_t nHist) { + + for (size_t i = 0; i != nHist; ++i) { + wsList[1]->mutableY(i) *= 2.; + wsList[1]->mutableE(i) *= 2.; + } + + AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); + AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); +} + +void validateNumberOfEntries(const WorkspaceGroup_sptr workspaceGroup, const size_t expectedEntries) { + TS_ASSERT_EQUALS(workspaceGroup->getNumberOfEntries(), expectedEntries); +} + +void validateSpinStateLogs(const WorkspaceGroup_sptr &outputWS, const std::vector &expectedLogValues) { + const auto logName = SpinStatesORSO::LOG_NAME; + for (size_t i = 0; i < expectedLogValues.size(); ++i) { + MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); + TS_ASSERT(ws); + const auto &run = ws->run(); + TS_ASSERT(run.hasProperty(logName)); + TS_ASSERT_EQUALS(run.getPropertyValueAsType(logName), expectedLogValues[i]); + } +} + +void validateNoSpinStateLogs(const WorkspaceGroup_sptr &outputWS, size_t numClones) { + for (size_t i = 0; i < numClones; ++i) { + MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); + TS_ASSERT(ws); + TS_ASSERT(!ws->run().hasProperty(SpinStatesORSO::LOG_NAME)); + } +} } // namespace class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { public: @@ -148,16 +194,11 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { auto wsList = createWorkspaceList(numClones); auto wsNames = generateWorkspaceNames(numClones); - for (size_t i = 0; i != nHist; ++i) { - wsList[1]->mutableY(i) *= 2.; - wsList[1]->mutableE(i) *= 2.; - } - AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); - AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); + setupWorkspacesForIdealCasesTwoInput(wsNames, wsList, nHist); auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "00, 11"); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) + validateNumberOfEntries(outputWS, 4); const std::array POL_DIRS{{"++", "+-", "-+", "--"}}; for (size_t i = 0; i != 4; ++i) { @@ -210,13 +251,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { auto wsList = createWorkspaceList(numClones); auto wsNames = generateWorkspaceNames(numClones); - for (size_t i = 0; i != nHist; ++i) { - wsList[1]->mutableY(i) *= 2.; - wsList[1]->mutableE(i) *= 2.; - } - - AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); - AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); + setupWorkspacesForIdealCasesTwoInput(wsNames, wsList, nHist); auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "0, 1"); @@ -233,12 +268,15 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, 4.2 * yVal, yVal}; + auto const ws00 = createWorkspace(); - const std::vector wsNames{{"ws00"}}; - AnalysisDataService::Instance().addOrReplace(wsNames.front(), ws00); + const std::string wsName{"ws00"}; + addWorkspaceToService(wsName, ws00); + auto effWS = idealEfficiencies(edges); - WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "0"); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 1) + WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsName, effWS, "0"); + + validateNumberOfEntries(outputWS, 1); MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(m_outputWSName + std::string("_++"))); TS_ASSERT(ws) @@ -258,38 +296,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_FullCorrections() { - constexpr size_t nHist{2}; - constexpr size_t numClones{4}; - const double yVal = 2.3; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - Counts counts{yVal, yVal, yVal}; - - auto wsList = createWorkspaceList(counts, numClones); - auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); - - // auto ws00 = createWorkspace(counts); - // MatrixWorkspace_sptr ws01 = ws00->clone(); - // MatrixWorkspace_sptr ws10 = ws00->clone(); - // MatrixWorkspace_sptr ws11 = ws00->clone(); - // const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; - // const std::array wsList{{ws00, ws01, ws10, ws11}}; - // for (size_t i = 0; i != 4; ++i) { - // for (size_t j = 0; j != nHist; ++j) { - // wsList[i]->mutableY(j) *= static_cast(i + 1); - // wsList[i]->mutableE(j) *= static_cast(i + 1); - // } - // AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - // } - - auto effWS = efficiencies(edges); - WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS); - - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) - fullFourInputsResultsCheck(outputWS, wsList[0], wsList[1], wsList[2], wsList[3], effWS, counts); - } - - void test_FullCorrectionss() { constexpr size_t nHist{2}; constexpr size_t numClones{4}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; @@ -327,7 +333,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "00, 11"); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) + validateNumberOfEntries(outputWS, 4); solveMissingIntensities(wsList[0], ws01, ws10, wsList[1], effWS); const double F1 = effWS->y(0).front(); const double F1e = effWS->e(0).front(); @@ -368,7 +374,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "0, 1"); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 2) + validateNumberOfEntries(outputWS, 2); const double F1 = effWS->y(0).front(); const double F1e = effWS->e(0).front(); const double P1 = effWS->y(2).front(); @@ -388,12 +394,15 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; + auto const ws00 = createWorkspace(counts); const std::string wsName{"ws00"}; - AnalysisDataService::Instance().addOrReplace(wsName, ws00); + addWorkspaceToService(wsName, ws00); + auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsName, effWS, "0"); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 1) + validateNumberOfEntries(outputWS, 1); + const auto P1 = effWS->y(2).front(); const auto P1e = effWS->e(2).front(); const auto P2 = effWS->y(3).front(); @@ -412,48 +421,42 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_FailureWhenEfficiencyHistogramIsMissing() { - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - Counts counts{0., 0., 0.}; - auto const ws00 = createWorkspace(1, counts); const std::string wsName{"ws00"}; - AnalysisDataService::Instance().addOrReplace(wsName, ws00); - auto effWS = idealEfficiencies(edges); - // Rename F1 to something else. + auto [ws00, effWS] = setupFailureWorkspaceAndEfficiencies(); + + // Rename F1 to something else auto axis = std::make_unique(4); axis->setLabel(0, "__wrong_histogram_label"); axis->setLabel(1, "F2"); axis->setLabel(2, "P1"); axis->setLabel(3, "P2"); effWS->replaceAxis(1, std::move(axis)); + runCorrectionWildes(wsName, effWS, "0", "", false); } - void test_FailureWhenEfficiencyXDataMismatches() { - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - Counts counts{0., 0., 0.}; - auto const ws00 = createWorkspace(1, counts); - const std::string wsName{"ws00"}; - AnalysisDataService::Instance().addOrReplace(wsName, ws00); - auto effWS = idealEfficiencies(edges); - // Change a bin edge of one of the histograms. + void test_FailureWhenEfficiencyXDataMismatchess() { + auto [ws00, effWS] = setupFailureWorkspaceAndEfficiencies(); + auto &xs = effWS->mutableX(0); xs[xs.size() / 2] *= 1.01; - runCorrectionWildes(wsName, effWS, "0", "", false); + + runCorrectionWildes("ws00", effWS, "0", "", false); } void test_FailureWhenNumberOfHistogramsInInputWorkspacesMismatch() { constexpr size_t nHist{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; + auto const ws00 = createWorkspace(counts); MatrixWorkspace_sptr ws01 = ws00->clone(); MatrixWorkspace_sptr ws10 = create(nHist + 1, Histogram(edges, counts)); MatrixWorkspace_sptr ws11 = ws00->clone(); const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; - const std::array wsList{{ws00, ws01, ws10, ws11}}; - for (size_t i = 0; i != 4; ++i) { - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + std::vector wsList = {ws00, ws01, ws10, ws11}; + addWorkspacesToService(wsNames, wsList, 4); + auto effWS = idealEfficiencies(edges); runCorrectionWildes(wsNames, effWS, "", "", false); } @@ -461,12 +464,12 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_FailureWhenAnInputWorkspaceIsMissing() { BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; - auto const ws00 = createWorkspace(counts); - MatrixWorkspace_sptr ws01 = ws00->clone(); - MatrixWorkspace_sptr ws11 = ws00->clone(); - AnalysisDataService::Instance().addOrReplace("ws00", ws00); - AnalysisDataService::Instance().addOrReplace("ws01", ws01); - AnalysisDataService::Instance().addOrReplace("ws11", ws11); + constexpr size_t numClones{3}; + + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + addWorkspacesToService(wsNames, wsList, numClones); + PolarizationCorrectionWildes alg; alg.setChild(true); alg.setRethrows(true); @@ -484,22 +487,19 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { idealCaseFullCorrectionsTest(edges, crossPolarizationEffWS, POL_DIRS); } - void test_SpinStateOrderInOutputWorkspacegroup() { + void test_SpinStateOrderInOutputWorkspaceGroup() { BinEdges edges{0.3, 0.6, 0.9, 1.2}; - auto effWS = idealEfficiencies(edges); Counts counts{2.3, 9.6, 2.3}; - auto const ws00 = createWorkspace(counts); - MatrixWorkspace_sptr ws01 = ws00->clone(); - MatrixWorkspace_sptr ws10 = ws00->clone(); - MatrixWorkspace_sptr ws11 = ws00->clone(); - const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; - const std::array wsList{{ws00, ws01, ws10, ws11}}; - for (size_t i = 0; i < 4; ++i) { - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } + constexpr size_t numClones{4}; + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + addWorkspacesToService(wsNames, wsList, numClones); + + auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) + + validateNumberOfEntries(outputWS, 4); const std::array OUTPUT_ORDER{{"++", "+-", "-+", "--"}}; for (size_t i = 0; i < 4; ++i) { auto ws = outputWS->getItem(i); @@ -510,79 +510,35 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_SpinStateAddedToSampleLogWhenRequested() { - constexpr size_t nHist{2}; constexpr size_t numClones{4}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - auto wsList = createWorkspaceList(counts, numClones); - auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + const std::vector expectedLogValues = {SpinStatesORSO::PP, SpinStatesORSO::PM, SpinStatesORSO::MP, + SpinStatesORSO::MM}; - const auto effWS = efficiencies(edges); - auto alg = createWildesAlg(wsNames, effWS); - alg->setProperty("AddSpinStateToLog", true); - - const auto outputWS = runAlg(std::move(alg)); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) - const std::array EXPECTED_LOG_VALUES{ - {SpinStatesORSO::PP, SpinStatesORSO::PM, SpinStatesORSO::MP, SpinStatesORSO::MM}}; - for (size_t i = 0; i < 4; ++i) { - MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); - TS_ASSERT(ws) - const auto &run = ws->run(); - TS_ASSERT(run.hasProperty(SpinStatesORSO::LOG_NAME)) - TS_ASSERT_EQUALS(run.getPropertyValueAsType(SpinStatesORSO::LOG_NAME), EXPECTED_LOG_VALUES[i]) - } + prepareWorkspacesAndRunCorrectionWithSampleState(counts, edges, numClones, true, expectedLogValues); } void test_SpinStateAddedToSampleLogWhenRequestedNoAnalyser() { - constexpr size_t nHist{2}; constexpr size_t numClones{2}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - auto wsList = createWorkspaceList(counts, numClones); - auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + const std::vector expectedLogValues = {SpinStatesORSO::PO, SpinStatesORSO::MO}; - const auto effWS = efficiencies(edges); - auto alg = createWildesAlg(wsNames, effWS, "0, 1"); - alg->setProperty("AddSpinStateToLog", true); - const auto outputWS = runAlg(std::move(alg)); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 2) - const std::array EXPECTED_LOG_VALUES{{SpinStatesORSO::PO, SpinStatesORSO::MO}}; - for (size_t i = 0; i < 2; ++i) { - MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); - TS_ASSERT(ws) - const auto &run = ws->run(); - TS_ASSERT(run.hasProperty(SpinStatesORSO::LOG_NAME)) - TS_ASSERT_EQUALS(run.getPropertyValueAsType(SpinStatesORSO::LOG_NAME), EXPECTED_LOG_VALUES[i]) - } + prepareWorkspacesAndRunCorrectionWithSampleState(counts, edges, numClones, true, expectedLogValues, "0, 1"); } void test_SpinStateNotAddedToSampleLogByDefault() { - constexpr size_t nHist{2}; + constexpr size_t numClones{4}; BinEdges edges{0.3, 0.6, 0.9, 1.2}; const double yVal = 2.3; - // confrim nuymber of clones of 4 or2 - constexpr size_t numClones{4}; Counts counts{yVal, yVal, yVal}; - auto wsList = createWorkspaceList(counts, numClones); - auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); - - const auto effWS = efficiencies(edges); - const auto outputWS = runCorrectionWildes(wsNames, effWS); - TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) - for (size_t i = 0; i != 4; ++i) { - MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); - TS_ASSERT(ws) - TS_ASSERT(!ws->run().hasProperty(SpinStatesORSO::LOG_NAME)) - } + prepareWorkspacesAndRunCorrectionWithSampleState(counts, edges, numClones, false, {}); } private: @@ -1532,6 +1488,42 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } throw std::invalid_argument("Unknown polarization string: " + pol); } + + std::pair setupFailureWorkspaceAndEfficiencies() { + BinEdges edges{0.3, 0.6, 0.9, 1.2}; + Counts counts{0., 0., 0.}; + + auto const ws00 = createWorkspace(1, counts); + const std::string wsName{"ws00"}; + addWorkspaceToService(wsName, ws00); + + auto effWS = idealEfficiencies(edges); + + return {ws00, effWS}; + } + + void prepareWorkspacesAndRunCorrectionWithSampleState(const Counts &counts, const BinEdges &edges, size_t numClones, + bool addSpinStateToLog, + const std::vector &expectedLogValues, + const std::string &grouping = "") { + constexpr size_t nHist{2}; + auto wsList = createWorkspaceList(counts, numClones); + auto wsNames = generateWorkspaceNames(numClones); + prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + + const auto effWS = efficiencies(edges); + auto alg = createWildesAlg(wsNames, effWS, grouping); + alg->setProperty("AddSpinStateToLog", addSpinStateToLog); + + const auto outputWS = runAlg(std::move(alg)); + validateNumberOfEntries(outputWS, numClones); + + if (addSpinStateToLog) { + validateSpinStateLogs(outputWS, expectedLogValues); + } else { + validateNoSpinStateLogs(outputWS, numClones); + } + } }; class PolarizationCorrectionWildesTestPerformance : public CxxTest::TestSuite { From 3597c207f2cbd334fdd5a80c625368d50b5ef95a Mon Sep 17 00:00:00 2001 From: Jimoh Yusuf Date: Wed, 8 Jan 2025 12:24:34 +0000 Subject: [PATCH 3/5] removed helper methods from namespace to class as private methods in PolarizationCorrectionWildesTest --- .../test/PolarizationCorrectionWildesTest.h | 222 +++++++++--------- 1 file changed, 111 insertions(+), 111 deletions(-) diff --git a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h index 2fab3d274225..68416734c634 100644 --- a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h +++ b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h @@ -32,117 +32,7 @@ using namespace Mantid::Kernel; using namespace Mantid::Algorithms; using namespace Mantid::Algorithms::PolarizationCorrectionsHelpers; -namespace { - -MatrixWorkspace_sptr createWorkspace(size_t nHist = 2, std::optional edges = std::nullopt, - std::optional counts = std::nullopt) { - if (!edges) { - edges = BinEdges{0.3, 0.6, 0.9, 1.2}; - } - - const auto yVal = 2.3; - if (!counts) { - counts = Counts{yVal, 4.2 * (yVal), yVal}; - } - - return create(nHist, Histogram(*edges, *counts)); -} - -MatrixWorkspace_sptr createWorkspace(size_t nHist, const Counts &counts) { - return createWorkspace(nHist, std::nullopt, counts); -} - -MatrixWorkspace_sptr createWorkspace(const Counts &counts) { return createWorkspace(2, std::nullopt, counts); } - -std::vector cloneWorkspaces(const MatrixWorkspace_sptr &sourceWorkspace, const size_t numClones) { - std::vector clonedWorkspaces; - - clonedWorkspaces.push_back(sourceWorkspace); - for (size_t i = 1; i < numClones; ++i) { - clonedWorkspaces.push_back(sourceWorkspace->clone()); - } - - return clonedWorkspaces; -} - -std::vector generateWorkspaceNames(const size_t numClones) { - std::vector wsNames; - - for (size_t i = 0; i < numClones; ++i) { - wsNames.push_back("ws" + std::to_string(i)); - } - - return wsNames; -} -void addWorkspacesToService(const std::vector &wsNames, const std::vector &wsList, - const size_t numWorkspaces) { - for (size_t i = 0; i < numWorkspaces; ++i) { - AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); - } -} - -void addWorkspaceToService(const std::string &wsName, const MatrixWorkspace_sptr &ws) { - AnalysisDataService::Instance().addOrReplace(wsName, ws); -} - -void prepareAndRegisterWorkspaces(std::vector &wsNames, - const std::vector &wsList, const size_t nHist) { - - for (size_t i = 0; i != wsNames.size(); ++i) { - for (size_t j = 0; j != nHist; ++j) { - wsList[i]->mutableY(j) *= static_cast(i + 1); - wsList[i]->mutableE(j) *= static_cast(i + 1); - } - addWorkspacesToService(wsNames, wsList, wsNames.size()); - } -} - -std::vector createWorkspaceList(const std::optional counts, const size_t numClones) { - auto baseWorkspace = counts.has_value() ? createWorkspace(*counts) : createWorkspace(); - - return cloneWorkspaces(baseWorkspace, numClones); -} - -std::vector createWorkspaceList(const size_t numClones) { - return createWorkspaceList(std::nullopt, numClones); -} - -void setupWorkspacesForIdealCasesTwoInput(const std::vector &wsNames, - const std::vector &wsList, - const size_t nHist) { - - for (size_t i = 0; i != nHist; ++i) { - wsList[1]->mutableY(i) *= 2.; - wsList[1]->mutableE(i) *= 2.; - } - - AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); - AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); -} - -void validateNumberOfEntries(const WorkspaceGroup_sptr workspaceGroup, const size_t expectedEntries) { - TS_ASSERT_EQUALS(workspaceGroup->getNumberOfEntries(), expectedEntries); -} - -void validateSpinStateLogs(const WorkspaceGroup_sptr &outputWS, const std::vector &expectedLogValues) { - const auto logName = SpinStatesORSO::LOG_NAME; - for (size_t i = 0; i < expectedLogValues.size(); ++i) { - MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); - TS_ASSERT(ws); - const auto &run = ws->run(); - TS_ASSERT(run.hasProperty(logName)); - TS_ASSERT_EQUALS(run.getPropertyValueAsType(logName), expectedLogValues[i]); - } -} - -void validateNoSpinStateLogs(const WorkspaceGroup_sptr &outputWS, size_t numClones) { - for (size_t i = 0; i < numClones; ++i) { - MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); - TS_ASSERT(ws); - TS_ASSERT(!ws->run().hasProperty(SpinStatesORSO::LOG_NAME)); - } -} -} // namespace +// namespace class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { public: // This pair of boilerplate methods prevent the suite being created statically @@ -1524,6 +1414,116 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { validateNoSpinStateLogs(outputWS, numClones); } } + + MatrixWorkspace_sptr createWorkspace(size_t nHist = 2, std::optional edges = std::nullopt, + std::optional counts = std::nullopt) { + if (!edges) { + edges = BinEdges{0.3, 0.6, 0.9, 1.2}; + } + + const auto yVal = 2.3; + if (!counts) { + counts = Counts{yVal, 4.2 * (yVal), yVal}; + } + + return create(nHist, Histogram(*edges, *counts)); + } + + MatrixWorkspace_sptr createWorkspace(size_t nHist, const Counts &counts) { + return createWorkspace(nHist, std::nullopt, counts); + } + + MatrixWorkspace_sptr createWorkspace(const Counts &counts) { return createWorkspace(2, std::nullopt, counts); } + + std::vector cloneWorkspaces(const MatrixWorkspace_sptr &sourceWorkspace, + const size_t numClones) { + std::vector clonedWorkspaces; + + clonedWorkspaces.push_back(sourceWorkspace); + for (size_t i = 1; i < numClones; ++i) { + clonedWorkspaces.push_back(sourceWorkspace->clone()); + } + + return clonedWorkspaces; + } + + std::vector generateWorkspaceNames(const size_t numClones) { + std::vector wsNames; + + for (size_t i = 0; i < numClones; ++i) { + wsNames.push_back("ws" + std::to_string(i)); + } + + return wsNames; + } + void addWorkspacesToService(const std::vector &wsNames, const std::vector &wsList, + const size_t numWorkspaces) { + for (size_t i = 0; i < numWorkspaces; ++i) { + AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); + } + } + + void addWorkspaceToService(const std::string &wsName, const MatrixWorkspace_sptr &ws) { + AnalysisDataService::Instance().addOrReplace(wsName, ws); + } + + void prepareAndRegisterWorkspaces(std::vector &wsNames, + const std::vector &wsList, const size_t nHist) { + + for (size_t i = 0; i != wsNames.size(); ++i) { + for (size_t j = 0; j != nHist; ++j) { + wsList[i]->mutableY(j) *= static_cast(i + 1); + wsList[i]->mutableE(j) *= static_cast(i + 1); + } + addWorkspacesToService(wsNames, wsList, wsNames.size()); + } + } + + std::vector createWorkspaceList(const std::optional counts, const size_t numClones) { + auto baseWorkspace = counts.has_value() ? createWorkspace(*counts) : createWorkspace(); + + return cloneWorkspaces(baseWorkspace, numClones); + } + + std::vector createWorkspaceList(const size_t numClones) { + return createWorkspaceList(std::nullopt, numClones); + } + + void setupWorkspacesForIdealCasesTwoInput(const std::vector &wsNames, + const std::vector &wsList, + const size_t nHist) { + + for (size_t i = 0; i != nHist; ++i) { + wsList[1]->mutableY(i) *= 2.; + wsList[1]->mutableE(i) *= 2.; + } + + AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); + AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); + } + + void validateNumberOfEntries(const WorkspaceGroup_sptr workspaceGroup, const size_t expectedEntries) { + TS_ASSERT_EQUALS(workspaceGroup->getNumberOfEntries(), expectedEntries); + } + + void validateSpinStateLogs(const WorkspaceGroup_sptr &outputWS, const std::vector &expectedLogValues) { + const auto logName = SpinStatesORSO::LOG_NAME; + for (size_t i = 0; i < expectedLogValues.size(); ++i) { + MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); + TS_ASSERT(ws); + const auto &run = ws->run(); + TS_ASSERT(run.hasProperty(logName)); + TS_ASSERT_EQUALS(run.getPropertyValueAsType(logName), expectedLogValues[i]); + } + } + + void validateNoSpinStateLogs(const WorkspaceGroup_sptr &outputWS, size_t numClones) { + for (size_t i = 0; i < numClones; ++i) { + MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(i)); + TS_ASSERT(ws); + TS_ASSERT(!ws->run().hasProperty(SpinStatesORSO::LOG_NAME)); + } + } }; class PolarizationCorrectionWildesTestPerformance : public CxxTest::TestSuite { From 0eb7e30380bc22a4fae8fe51892f439b5db7fa44 Mon Sep 17 00:00:00 2001 From: Jimoh Yusuf Date: Mon, 13 Jan 2025 14:22:25 +0000 Subject: [PATCH 4/5] - refactor and improve tests based on suggestions from code review --- .../test/PolarizationCorrectionWildesTest.h | 124 ++++++------------ 1 file changed, 39 insertions(+), 85 deletions(-) diff --git a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h index 68416734c634..59fd74d10f55 100644 --- a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h +++ b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h @@ -75,11 +75,8 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_IdealCaseTwoInputsWithAnalyzer() { - constexpr size_t nBins{3}; constexpr size_t nHist{2}; constexpr size_t numClones{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, 4.2 * yVal, yVal}; auto wsList = createWorkspaceList(numClones); @@ -131,10 +128,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_IdealCaseTwoInputsNoAnalyzer() { - constexpr size_t nBins{3}; constexpr size_t nHist{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; constexpr size_t numClones{2}; // confirm the counts in this method if this is correct Counts counts{yVal, 4.2 * yVal, yVal}; @@ -153,10 +147,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_IdealCaseDirectBeamCorrections() { - constexpr size_t nBins{3}; constexpr size_t nHist{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, 4.2 * yVal, yVal}; auto const ws00 = createWorkspace(); @@ -188,13 +179,12 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_FullCorrections() { constexpr size_t nHist{2}; constexpr size_t numClones{4}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - auto wsList = createWorkspaceList(counts, numClones); + auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + setWorkspacesTestData(wsNames, wsList, nHist); + addWorkspacesToService(wsNames, wsList); auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS); @@ -208,15 +198,13 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_TwoInputsWithAnalyzer() { constexpr size_t nHist{2}; - constexpr size_t nBins{3}; constexpr size_t numClones{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - auto wsList = createWorkspaceList(counts, numClones); + auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + setWorkspacesTestData(wsNames, wsList, nHist); + addWorkspacesToService(wsNames, wsList); MatrixWorkspace_sptr ws01 = nullptr; MatrixWorkspace_sptr ws10 = nullptr; @@ -251,15 +239,13 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_TwoInputsWithoutAnalyzer() { constexpr size_t nHist{2}; - constexpr size_t nBins{3}; constexpr size_t numClones{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - auto wsList = createWorkspaceList(counts, numClones); + auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + setWorkspacesTestData(wsNames, wsList, nHist); + addWorkspacesToService(wsNames, wsList); auto effWS = efficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "0, 1"); @@ -280,12 +266,9 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_directBeamOnlyInput() { constexpr size_t nHist{2}; - constexpr size_t nBins{3}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; - auto const ws00 = createWorkspace(counts); + auto const ws00 = createWorkspace(nHist, counts); const std::string wsName{"ws00"}; addWorkspaceToService(wsName, ws00); @@ -336,29 +319,27 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_FailureWhenNumberOfHistogramsInInputWorkspacesMismatch() { constexpr size_t nHist{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; - auto const ws00 = createWorkspace(counts); + auto const ws00 = createWorkspace(nHist, counts); MatrixWorkspace_sptr ws01 = ws00->clone(); MatrixWorkspace_sptr ws10 = create(nHist + 1, Histogram(edges, counts)); MatrixWorkspace_sptr ws11 = ws00->clone(); const std::vector wsNames{{"ws00", "ws01", "ws10", "ws11"}}; std::vector wsList = {ws00, ws01, ws10, ws11}; - addWorkspacesToService(wsNames, wsList, 4); + addWorkspacesToService(wsNames, wsList); auto effWS = idealEfficiencies(edges); runCorrectionWildes(wsNames, effWS, "", "", false); } void test_FailureWhenAnInputWorkspaceIsMissing() { - BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; constexpr size_t numClones{3}; - auto wsList = createWorkspaceList(counts, numClones); + auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - addWorkspacesToService(wsNames, wsList, numClones); + addWorkspacesToService(wsNames, wsList); PolarizationCorrectionWildes alg; alg.setChild(true); @@ -369,7 +350,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_IdealCrossPolarizationCaseFullCorrections() { - BinEdges edges{0.3, 0.6, 0.9, 1.2}; auto crossPolarizationEffWS = idealEfficiencies(edges, false); // Cross polarized ideal efficiencies should give us the same ouput as for ideal efficiencies but in the reverse // order @@ -378,13 +358,12 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_SpinStateOrderInOutputWorkspaceGroup() { - BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{2.3, 9.6, 2.3}; constexpr size_t numClones{4}; - auto wsList = createWorkspaceList(counts, numClones); + auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - addWorkspacesToService(wsNames, wsList, numClones); + addWorkspacesToService(wsNames, wsList); auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS); @@ -401,8 +380,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateAddedToSampleLogWhenRequested() { constexpr size_t numClones{4}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; const std::vector expectedLogValues = {SpinStatesORSO::PP, SpinStatesORSO::PM, SpinStatesORSO::MP, @@ -413,8 +390,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateAddedToSampleLogWhenRequestedNoAnalyser() { constexpr size_t numClones{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; const std::vector expectedLogValues = {SpinStatesORSO::PO, SpinStatesORSO::MO}; @@ -424,8 +399,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateNotAddedToSampleLogByDefault() { constexpr size_t numClones{4}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; prepareWorkspacesAndRunCorrectionWithSampleState(counts, edges, numClones, false, {}); @@ -433,6 +406,9 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { private: const std::string m_outputWSName{"output"}; + const double yVal{2.3}; + const BinEdges edges{0.3, 0.6, 0.9, 1.2}; + static constexpr size_t nBins{3}; std::unique_ptr createWildesAlg(const std::vector &inputWorkspaces, Mantid::API::MatrixWorkspace_sptr effWs, @@ -585,9 +561,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const std::array &outputSpinStates, const std::string &flipperConfig = "00,01,10,11", const std::string &spinStates = "") { - constexpr size_t nBins{3}; constexpr size_t nHist{2}; - const double yVal = 2.3; Counts counts{yVal, 4.2 * yVal, yVal}; MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); MatrixWorkspace_sptr ws01 = ws00->clone(); @@ -596,7 +570,8 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { std::vector wsNames{"ws00", "ws01", "ws10", "ws11"}; const std::vector wsList{ws00, ws01, ws10, ws11}; - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + setWorkspacesTestData(wsNames, wsList, nHist); + addWorkspacesToService(wsNames, wsList); // Re-order the input workspace names to match the input flipper configuration const auto &flipperConfigVec = splitSpinStateString(flipperConfig); @@ -624,10 +599,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void idealThreeInputsTest(const std::string &missingFlipperConf, const std::string &flipperConfig, const std::vector &outputWsOrder, const bool useSpinStates = false) { - constexpr size_t nBins{3}; constexpr size_t nHist{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, 4.2 * yVal, yVal}; MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); MatrixWorkspace_sptr wsXX = ws00->clone(); @@ -636,7 +608,8 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const std::string presentFlipperConf = missingFlipperConf == "01" ? "10" : "01"; std::vector wsNames{"ws00", "wsXX", "ws11"}; const std::vector wsList{ws00, wsXX, ws11}; - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + setWorkspacesTestData(wsNames, wsList, nHist); + addWorkspacesToService(wsNames, wsList); // Re-order the input workspace names to match the input flipper configuration const auto &flipperConfigVec = splitSpinStateString(flipperConfig); @@ -700,8 +673,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void threeInputsTest(const std::string &missingFlipperConf) { constexpr size_t nHist{2}; - BinEdges edges{0.3, 0.6, 0.9, 1.2}; - const double yVal = 2.3; Counts counts{yVal, yVal, yVal}; MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); MatrixWorkspace_sptr ws01 = missingFlipperConf == "01" ? nullptr : ws00->clone(); @@ -1361,7 +1332,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void runIdealCaseFullCorrections(const std::string &flipperConfig, const std::array &outputOrder, const bool useSpinStates = false) { - BinEdges edges{0.3, 0.6, 0.9, 1.2}; auto effWS = idealEfficiencies(edges); std::string spinStates = ""; if (useSpinStates) { @@ -1380,7 +1350,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } std::pair setupFailureWorkspaceAndEfficiencies() { - BinEdges edges{0.3, 0.6, 0.9, 1.2}; Counts counts{0., 0., 0.}; auto const ws00 = createWorkspace(1, counts); @@ -1397,9 +1366,10 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const std::vector &expectedLogValues, const std::string &grouping = "") { constexpr size_t nHist{2}; - auto wsList = createWorkspaceList(counts, numClones); + auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - prepareAndRegisterWorkspaces(wsNames, wsList, nHist); + setWorkspacesTestData(wsNames, wsList, nHist); + addWorkspacesToService(wsNames, wsList); const auto effWS = efficiencies(edges); auto alg = createWildesAlg(wsNames, effWS, grouping); @@ -1415,26 +1385,14 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } } - MatrixWorkspace_sptr createWorkspace(size_t nHist = 2, std::optional edges = std::nullopt, - std::optional counts = std::nullopt) { - if (!edges) { - edges = BinEdges{0.3, 0.6, 0.9, 1.2}; - } - - const auto yVal = 2.3; + MatrixWorkspace_sptr createWorkspace(size_t nHist = 2, std::optional counts = std::nullopt) { if (!counts) { counts = Counts{yVal, 4.2 * (yVal), yVal}; } - return create(nHist, Histogram(*edges, *counts)); - } - - MatrixWorkspace_sptr createWorkspace(size_t nHist, const Counts &counts) { - return createWorkspace(nHist, std::nullopt, counts); + return create(nHist, Histogram(edges, *counts)); } - MatrixWorkspace_sptr createWorkspace(const Counts &counts) { return createWorkspace(2, std::nullopt, counts); } - std::vector cloneWorkspaces(const MatrixWorkspace_sptr &sourceWorkspace, const size_t numClones) { std::vector clonedWorkspaces; @@ -1456,9 +1414,9 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { return wsNames; } - void addWorkspacesToService(const std::vector &wsNames, const std::vector &wsList, - const size_t numWorkspaces) { - for (size_t i = 0; i < numWorkspaces; ++i) { + void addWorkspacesToService(const std::vector &wsNames, + const std::vector &wsList) { + for (size_t i = 0; i < wsNames.size(); ++i) { AnalysisDataService::Instance().addOrReplace(wsNames[i], wsList[i]); } } @@ -1467,28 +1425,24 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { AnalysisDataService::Instance().addOrReplace(wsName, ws); } - void prepareAndRegisterWorkspaces(std::vector &wsNames, - const std::vector &wsList, const size_t nHist) { + void setWorkspacesTestData(std::vector &wsNames, + const std::vector &wsList, const size_t nHist) { for (size_t i = 0; i != wsNames.size(); ++i) { for (size_t j = 0; j != nHist; ++j) { wsList[i]->mutableY(j) *= static_cast(i + 1); wsList[i]->mutableE(j) *= static_cast(i + 1); } - addWorkspacesToService(wsNames, wsList, wsNames.size()); } } - std::vector createWorkspaceList(const std::optional counts, const size_t numClones) { - auto baseWorkspace = counts.has_value() ? createWorkspace(*counts) : createWorkspace(); + std::vector createWorkspaceList(const size_t numClones, + const std::optional &counts = std::nullopt) { + auto baseWorkspace = counts.has_value() ? createWorkspace(2, *counts) : createWorkspace(); return cloneWorkspaces(baseWorkspace, numClones); } - std::vector createWorkspaceList(const size_t numClones) { - return createWorkspaceList(std::nullopt, numClones); - } - void setupWorkspacesForIdealCasesTwoInput(const std::vector &wsNames, const std::vector &wsList, const size_t nHist) { @@ -1498,11 +1452,11 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { wsList[1]->mutableE(i) *= 2.; } - AnalysisDataService::Instance().addOrReplace(wsNames.front(), wsList.front()); - AnalysisDataService::Instance().addOrReplace(wsNames.back(), wsList.back()); + addWorkspaceToService(wsNames.front(), wsList.front()); + addWorkspaceToService(wsNames.back(), wsList.back()); } - void validateNumberOfEntries(const WorkspaceGroup_sptr workspaceGroup, const size_t expectedEntries) { + void validateNumberOfEntries(const WorkspaceGroup_sptr &workspaceGroup, const size_t expectedEntries) { TS_ASSERT_EQUALS(workspaceGroup->getNumberOfEntries(), expectedEntries); } From 79e809cb985ac1b2e1f9f122bda1ff10a6b709a1 Mon Sep 17 00:00:00 2001 From: Jimoh Yusuf Date: Tue, 14 Jan 2025 21:02:13 +0000 Subject: [PATCH 5/5] improve tests based on suggestions from code review --- .../test/PolarizationCorrectionWildesTest.h | 90 ++++++++----------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h index 59fd74d10f55..6c934a0cf4f2 100644 --- a/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h +++ b/Framework/Algorithms/test/PolarizationCorrectionWildesTest.h @@ -32,6 +32,14 @@ using namespace Mantid::Kernel; using namespace Mantid::Algorithms; using namespace Mantid::Algorithms::PolarizationCorrectionsHelpers; +namespace { +const double yVal{2.3}; +const BinEdges edges{0.3, 0.6, 0.9, 1.2}; +constexpr size_t nBins{3}; +constexpr size_t nHist{2}; +const std::string outputWSName{"output"}; +} // namespace + // namespace class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { public: @@ -75,13 +83,14 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_IdealCaseTwoInputsWithAnalyzer() { - constexpr size_t nHist{2}; constexpr size_t numClones{2}; Counts counts{yVal, 4.2 * yVal, yVal}; auto wsList = createWorkspaceList(numClones); auto wsNames = generateWorkspaceNames(numClones); - setupWorkspacesForIdealCasesTwoInput(wsNames, wsList, nHist); + setupWorkspacesForIdealCasesTwoInput(wsList[1]); + addWorkspaceToService(wsNames.front(), wsList.front()); + addWorkspaceToService(wsNames.back(), wsList.back()); auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "00, 11"); @@ -90,7 +99,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const std::array POL_DIRS{{"++", "+-", "-+", "--"}}; for (size_t i = 0; i != 4; ++i) { const auto &dir = POL_DIRS[i]; - const std::string wsName = m_outputWSName + std::string("_") + dir; + const std::string wsName = outputWSName + std::string("_") + dir; MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(wsName)); TS_ASSERT(ws) TS_ASSERT_EQUALS(ws->getNumberHistograms(), nHist) @@ -128,14 +137,15 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_IdealCaseTwoInputsNoAnalyzer() { - constexpr size_t nHist{2}; constexpr size_t numClones{2}; // confirm the counts in this method if this is correct Counts counts{yVal, 4.2 * yVal, yVal}; auto wsList = createWorkspaceList(numClones); auto wsNames = generateWorkspaceNames(numClones); - setupWorkspacesForIdealCasesTwoInput(wsNames, wsList, nHist); + setupWorkspacesForIdealCasesTwoInput(wsList[1]); + addWorkspaceToService(wsNames.front(), wsList.front()); + addWorkspaceToService(wsNames.back(), wsList.back()); auto effWS = idealEfficiencies(edges); WorkspaceGroup_sptr outputWS = runCorrectionWildes(wsNames, effWS, "0, 1"); @@ -147,7 +157,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_IdealCaseDirectBeamCorrections() { - constexpr size_t nHist{2}; Counts counts{yVal, 4.2 * yVal, yVal}; auto const ws00 = createWorkspace(); @@ -159,7 +168,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { validateNumberOfEntries(outputWS, 1); MatrixWorkspace_sptr ws = - std::dynamic_pointer_cast(outputWS->getItem(m_outputWSName + std::string("_++"))); + std::dynamic_pointer_cast(outputWS->getItem(outputWSName + std::string("_++"))); TS_ASSERT(ws) TS_ASSERT_EQUALS(ws->getNumberHistograms(), nHist) for (size_t i = 0; i != nHist; ++i) { @@ -177,13 +186,12 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_FullCorrections() { - constexpr size_t nHist{2}; constexpr size_t numClones{4}; Counts counts{yVal, yVal, yVal}; auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - setWorkspacesTestData(wsNames, wsList, nHist); + setWorkspacesTestData(wsList, nHist); addWorkspacesToService(wsNames, wsList); auto effWS = efficiencies(edges); @@ -197,13 +205,12 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_ThreeInputsWithMissing10FlipperConfiguration() { threeInputsTest("10"); } void test_TwoInputsWithAnalyzer() { - constexpr size_t nHist{2}; constexpr size_t numClones{2}; Counts counts{yVal, yVal, yVal}; auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - setWorkspacesTestData(wsNames, wsList, nHist); + setWorkspacesTestData(wsList, nHist); addWorkspacesToService(wsNames, wsList); MatrixWorkspace_sptr ws01 = nullptr; MatrixWorkspace_sptr ws10 = nullptr; @@ -238,13 +245,12 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_TwoInputsWithoutAnalyzer() { - constexpr size_t nHist{2}; constexpr size_t numClones{2}; Counts counts{yVal, yVal, yVal}; auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - setWorkspacesTestData(wsNames, wsList, nHist); + setWorkspacesTestData(wsList, nHist); addWorkspacesToService(wsNames, wsList); auto effWS = efficiencies(edges); @@ -265,8 +271,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_directBeamOnlyInput() { - constexpr size_t nHist{2}; - Counts counts{yVal, yVal, yVal}; + Counts counts(3, yVal); auto const ws00 = createWorkspace(nHist, counts); const std::string wsName{"ws00"}; @@ -318,8 +323,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_FailureWhenNumberOfHistogramsInInputWorkspacesMismatch() { - constexpr size_t nHist{2}; - Counts counts{0., 0., 0.}; + Counts counts(3, 0.); auto const ws00 = createWorkspace(nHist, counts); MatrixWorkspace_sptr ws01 = ws00->clone(); @@ -334,7 +338,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void test_FailureWhenAnInputWorkspaceIsMissing() { - Counts counts{0., 0., 0.}; + Counts counts(3, 0.); constexpr size_t numClones{3}; auto wsList = createWorkspaceList(numClones, counts); @@ -373,14 +377,14 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { for (size_t i = 0; i < 4; ++i) { auto ws = outputWS->getItem(i); TS_ASSERT(ws) - const std::string expectedName = m_outputWSName + std::string("_") + OUTPUT_ORDER[i]; + const std::string expectedName = outputWSName + std::string("_") + OUTPUT_ORDER[i]; TS_ASSERT_EQUALS(ws->getName(), expectedName) } } void test_SpinStateAddedToSampleLogWhenRequested() { constexpr size_t numClones{4}; - Counts counts{yVal, yVal, yVal}; + Counts counts(3, yVal); const std::vector expectedLogValues = {SpinStatesORSO::PP, SpinStatesORSO::PM, SpinStatesORSO::MP, SpinStatesORSO::MM}; @@ -390,7 +394,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateAddedToSampleLogWhenRequestedNoAnalyser() { constexpr size_t numClones{2}; - Counts counts{yVal, yVal, yVal}; + Counts counts(3, yVal); const std::vector expectedLogValues = {SpinStatesORSO::PO, SpinStatesORSO::MO}; @@ -399,17 +403,12 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void test_SpinStateNotAddedToSampleLogByDefault() { constexpr size_t numClones{4}; - Counts counts{yVal, yVal, yVal}; + Counts counts(3, yVal); prepareWorkspacesAndRunCorrectionWithSampleState(counts, edges, numClones, false, {}); } private: - const std::string m_outputWSName{"output"}; - const double yVal{2.3}; - const BinEdges edges{0.3, 0.6, 0.9, 1.2}; - static constexpr size_t nBins{3}; - std::unique_ptr createWildesAlg(const std::vector &inputWorkspaces, Mantid::API::MatrixWorkspace_sptr effWs, const std::string &flippers = "", @@ -426,7 +425,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { TS_ASSERT_THROWS_NOTHING(alg->setProperty("InputWorkspaces", inputWorkspaces)) } - TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("OutputWorkspace", m_outputWSName)) + TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("OutputWorkspace", outputWSName)) if (!flippers.empty()) { alg->setProperty("Flippers", flippers); @@ -486,7 +485,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { for (size_t wsIndex = 0; wsIndex < wsGrp->size(); ++wsIndex) { MatrixWorkspace_sptr ws = - std::dynamic_pointer_cast(wsGrp->getItem(m_outputWSName + pols[wsIndex])); + std::dynamic_pointer_cast(wsGrp->getItem(outputWSName + pols[wsIndex])); TS_ASSERT(ws) TS_ASSERT_EQUALS(ws->getNumberHistograms(), nHist) for (size_t histIndex = 0; histIndex < nHist; ++histIndex) { @@ -561,7 +560,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const std::array &outputSpinStates, const std::string &flipperConfig = "00,01,10,11", const std::string &spinStates = "") { - constexpr size_t nHist{2}; Counts counts{yVal, 4.2 * yVal, yVal}; MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); MatrixWorkspace_sptr ws01 = ws00->clone(); @@ -570,7 +568,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { std::vector wsNames{"ws00", "ws01", "ws10", "ws11"}; const std::vector wsList{ws00, ws01, ws10, ws11}; - setWorkspacesTestData(wsNames, wsList, nHist); + setWorkspacesTestData(wsList, nHist); addWorkspacesToService(wsNames, wsList); // Re-order the input workspace names to match the input flipper configuration @@ -599,7 +597,6 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { void idealThreeInputsTest(const std::string &missingFlipperConf, const std::string &flipperConfig, const std::vector &outputWsOrder, const bool useSpinStates = false) { - constexpr size_t nHist{2}; Counts counts{yVal, 4.2 * yVal, yVal}; MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); MatrixWorkspace_sptr wsXX = ws00->clone(); @@ -608,7 +605,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { const std::string presentFlipperConf = missingFlipperConf == "01" ? "10" : "01"; std::vector wsNames{"ws00", "wsXX", "ws11"}; const std::vector wsList{ws00, wsXX, ws11}; - setWorkspacesTestData(wsNames, wsList, nHist); + setWorkspacesTestData(wsList, nHist); addWorkspacesToService(wsNames, wsList); // Re-order the input workspace names to match the input flipper configuration @@ -628,7 +625,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { TS_ASSERT_EQUALS(outputWS->getNumberOfEntries(), 4) for (size_t i = 0; i != 4; ++i) { const auto &dir = outputWsOrder[i]; - const std::string wsName = m_outputWSName + std::string("_") + dir; + const std::string wsName = outputWSName + std::string("_") + dir; MatrixWorkspace_sptr ws = std::dynamic_pointer_cast(outputWS->getItem(wsName)); TS_ASSERT(ws) TS_ASSERT_EQUALS(ws->getNumberHistograms(), nHist) @@ -672,8 +669,7 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { } void threeInputsTest(const std::string &missingFlipperConf) { - constexpr size_t nHist{2}; - Counts counts{yVal, yVal, yVal}; + Counts counts(3, yVal); MatrixWorkspace_sptr ws00 = create(nHist, Histogram(edges, counts)); MatrixWorkspace_sptr ws01 = missingFlipperConf == "01" ? nullptr : ws00->clone(); MatrixWorkspace_sptr ws10 = missingFlipperConf == "10" ? nullptr : ws00->clone(); @@ -1365,10 +1361,9 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { bool addSpinStateToLog, const std::vector &expectedLogValues, const std::string &grouping = "") { - constexpr size_t nHist{2}; auto wsList = createWorkspaceList(numClones, counts); auto wsNames = generateWorkspaceNames(numClones); - setWorkspacesTestData(wsNames, wsList, nHist); + setWorkspacesTestData(wsList, nHist); addWorkspacesToService(wsNames, wsList); const auto effWS = efficiencies(edges); @@ -1425,10 +1420,9 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { AnalysisDataService::Instance().addOrReplace(wsName, ws); } - void setWorkspacesTestData(std::vector &wsNames, - const std::vector &wsList, const size_t nHist) { + void setWorkspacesTestData(const std::vector &wsList, const size_t nHist) { - for (size_t i = 0; i != wsNames.size(); ++i) { + for (size_t i = 0; i != wsList.size(); ++i) { for (size_t j = 0; j != nHist; ++j) { wsList[i]->mutableY(j) *= static_cast(i + 1); wsList[i]->mutableE(j) *= static_cast(i + 1); @@ -1443,17 +1437,11 @@ class PolarizationCorrectionWildesTest : public CxxTest::TestSuite { return cloneWorkspaces(baseWorkspace, numClones); } - void setupWorkspacesForIdealCasesTwoInput(const std::vector &wsNames, - const std::vector &wsList, - const size_t nHist) { - - for (size_t i = 0; i != nHist; ++i) { - wsList[1]->mutableY(i) *= 2.; - wsList[1]->mutableE(i) *= 2.; + void setupWorkspacesForIdealCasesTwoInput(const Mantid::API::MatrixWorkspace_sptr &ws) { + for (size_t i = 0; i < ws->getNumberHistograms(); ++i) { + ws->mutableY(i) *= 2.; + ws->mutableE(i) *= 2.; } - - addWorkspaceToService(wsNames.front(), wsList.front()); - addWorkspaceToService(wsNames.back(), wsList.back()); } void validateNumberOfEntries(const WorkspaceGroup_sptr &workspaceGroup, const size_t expectedEntries) {