Skip to content

Commit

Permalink
Add option to save to file
Browse files Browse the repository at this point in the history
You can now either have an output workspace, or save to a file, or both.
  • Loading branch information
jclarkeSTFC committed May 2, 2024
1 parent 2d718ea commit 4eba049
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
Expand Up @@ -39,6 +39,7 @@ class MANTID_ALGORITHMS_DLL PolarizerEfficiency final : public API::Algorithm {
void validateGroupInput();
void calculatePolarizerEfficiency();
MatrixWorkspace_sptr convertToHistIfNecessary(const MatrixWorkspace_sptr ws);
void PolarizerEfficiency::saveToFile(MatrixWorkspace_sptr const &workspace, std::string const &filePathStr);
};

} // namespace Algorithms
Expand Down
Expand Up @@ -7,6 +7,7 @@

#include "MantidAlgorithms/PolarizationCorrections/PolarizerEfficiency.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
Expand All @@ -18,6 +19,7 @@
#include "MantidKernel/ListValidator.h"

#include <boost/algorithm/string/join.hpp>
#include <filesystem>

namespace Mantid::Algorithms {
// Register the algorithm into the algorithm factory
Expand All @@ -31,6 +33,7 @@ static const std::string INPUT_WORKSPACE = "InputWorkspace";
static const std::string ANALYSER_EFFICIENCY = "AnalyserEfficiency";
static const std::string SPIN_STATES = "SpinStates";
static const std::string OUTPUT_WORKSPACE = "OutputWorkspace";
static const std::string OUTPUT_FILE_PATH = "OutputFilePath";
} // namespace PropertyNames

void PolarizerEfficiency::init() {
Expand All @@ -45,13 +48,16 @@ void PolarizerEfficiency::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(PropertyNames::ANALYSER_EFFICIENCY, "",
Direction::Input, wavelengthValidator),
"Analyser efficiency as a function of wavelength");
declareProperty(
std::make_unique<WorkspaceProperty<MatrixWorkspace>>(PropertyNames::OUTPUT_WORKSPACE, "", Direction::Output),
"Polarizer efficiency as a function of wavelength");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(PropertyNames::OUTPUT_WORKSPACE, "",
Direction::Output, PropertyMode::Optional),
"Polarizer efficiency as a function of wavelength");

const auto &spinValidator = std::make_shared<SpinStateValidator>(std::unordered_set<int>{4});
declareProperty(PropertyNames::SPIN_STATES, "11,10,01,00", spinValidator,
"Order of individual spin states in the input group workspace, e.g. \"01,11,00,10\"");

declareProperty(std::make_unique<FileProperty>(PropertyNames::OUTPUT_FILE_PATH, "", FileProperty::OptionalSave),
"File name or path for the output to be saved to.");
}

/**
Expand All @@ -78,6 +84,14 @@ std::map<std::string, std::string> PolarizerEfficiency::validateInputs() {
}
}

// Check outputs.
auto const &outputWs = getPropertyValue(PropertyNames::OUTPUT_WORKSPACE);
auto const &outputFile = getPropertyValue(PropertyNames::OUTPUT_FILE_PATH);
if (outputWs.empty() && outputFile.empty()) {
errorList[PropertyNames::OUTPUT_FILE_PATH] = "Either an output workspace or output file must be provided.";
errorList[PropertyNames::OUTPUT_WORKSPACE] = "Either an output workspace or output file must be provided.";
}

return errorList;
}

Expand Down Expand Up @@ -124,7 +138,30 @@ void PolarizerEfficiency::calculatePolarizerEfficiency() {
effCell = rebin->getProperty("OutputWorkspace");

const auto &effPolarizer = (t00Ws - t01Ws) / (4 * (2 * effCell - 1) * (t00Ws + t01Ws)) + 0.5;
setProperty(PropertyNames::OUTPUT_WORKSPACE, effPolarizer);

auto const &filename = getPropertyValue(PropertyNames::OUTPUT_FILE_PATH);
if (!filename.empty()) {
saveToFile(effPolarizer, filename);
}

auto const &outputWsName = getPropertyValue(PropertyNames::OUTPUT_WORKSPACE);
if (!outputWsName.empty()) {
setProperty(PropertyNames::OUTPUT_WORKSPACE, effPolarizer);
}
}

void PolarizerEfficiency::saveToFile(MatrixWorkspace_sptr const &workspace, std::string const &filePathStr) {
std::filesystem::path filePath = filePathStr;
// Add the nexus extension if it's not been applied already.
const std::string fileExtension = ".nxs";
if (filePath.extension() != fileExtension) {
filePath.replace_extension(fileExtension);
}
auto saveAlg = createChildAlgorithm("SaveNexus");
saveAlg->initialize();
saveAlg->setProperty("Filename", filePath.string());
saveAlg->setProperty("InputWorkspace", workspace);
saveAlg->execute();
}

MatrixWorkspace_sptr PolarizerEfficiency::convertToHistIfNecessary(const MatrixWorkspace_sptr ws) {
Expand Down
Expand Up @@ -10,8 +10,10 @@
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAlgorithms/PolarizationCorrections/PolarizerEfficiency.h"
#include "MantidKernel/ConfigService.h"

#include <cxxtest/TestSuite.h>
#include <filesystem>

using namespace Mantid;
using namespace Mantid::Algorithms;
Expand All @@ -22,9 +24,13 @@ class PolarizerEfficiencyTest : public CxxTest::TestSuite {
void setUp() override {
// Use an analyser efficiency of 1 to make test calculations simpler
generateFunctionDefinedWorkspace(ANALYSER_EFFICIENCY_WS_NAME, "1 + x*0");
m_defaultSaveDirectory = Kernel::ConfigService::Instance().getString("defaultsave.directory");
}

void tearDown() override { AnalysisDataService::Instance().clear(); }
void tearDown() override {
AnalysisDataService::Instance().clear();
Kernel::ConfigService::Instance().setString("defaultsave.directory", m_defaultSaveDirectory);
}

void testName() {
PolarizerEfficiency alg;
Expand Down Expand Up @@ -120,8 +126,42 @@ class PolarizerEfficiencyTest : public CxxTest::TestSuite {
}
}

/// Saving Tests

void testSavingAbsolute() {
auto const temp_filename = std::filesystem::temp_directory_path() /= "something.nxs";
auto polarizerEfficiency = createPolarizerEfficiencyAlgorithm(createExampleGroupWorkspace("wsGrp"), false);
polarizerEfficiency->setPropertyValue("OutputFilePath", temp_filename.string());
polarizerEfficiency->execute();
TS_ASSERT(std::filesystem::exists(temp_filename))
std::filesystem::remove(temp_filename);
}

void testSavingRelative() {
auto tempDir = std::filesystem::temp_directory_path();
Kernel::ConfigService::Instance().setString("defaultsave.directory", tempDir.string());
std::string const &filename = "something.nxs";
auto polarizerEfficiency = createPolarizerEfficiencyAlgorithm();
polarizerEfficiency->setPropertyValue("OutputFilePath", filename);
polarizerEfficiency->execute();
auto savedPath = tempDir /= filename;
TS_ASSERT(std::filesystem::exists(savedPath))
std::filesystem::remove(savedPath);
}

void testSavingNoExt() {
auto const temp_filename = std::filesystem::temp_directory_path() /= "something";
auto polarizerEfficiency = createPolarizerEfficiencyAlgorithm();
polarizerEfficiency->setPropertyValue("OutputFilePath", temp_filename.string());
polarizerEfficiency->execute();
auto savedPath = temp_filename.string() + ".nxs";
TS_ASSERT(std::filesystem::exists(savedPath))
std::filesystem::remove(savedPath);
}

private:
const std::string ANALYSER_EFFICIENCY_WS_NAME = "effAnalyser";
std::string m_defaultSaveDirectory;

WorkspaceGroup_sptr createExampleGroupWorkspace(const std::string &name, const std::string &xUnit = "Wavelength",
const size_t numBins = 5) {
Expand Down Expand Up @@ -206,15 +246,18 @@ class PolarizerEfficiencyTest : public CxxTest::TestSuite {
return result;
}

IAlgorithm_sptr createPolarizerEfficiencyAlgorithm(WorkspaceGroup_sptr inputGrp = nullptr) {
IAlgorithm_sptr createPolarizerEfficiencyAlgorithm(WorkspaceGroup_sptr inputGrp = nullptr,
const bool setOutputWs = true) {
if (inputGrp == nullptr) {
inputGrp = createExampleGroupWorkspace("wsGrp");
}
auto polarizerEfficiency = AlgorithmManager::Instance().create("PolarizerEfficiency");
polarizerEfficiency->initialize();
polarizerEfficiency->setProperty("InputWorkspace", inputGrp->getName());
polarizerEfficiency->setProperty("AnalyserEfficiency", ANALYSER_EFFICIENCY_WS_NAME);
polarizerEfficiency->setProperty("OutputWorkspace", "psm");
if (setOutputWs) {
polarizerEfficiency->setProperty("OutputWorkspace", "psm");
}
return polarizerEfficiency;
}
};

0 comments on commit 4eba049

Please sign in to comment.