Skip to content

Commit

Permalink
Apply extension if not given
Browse files Browse the repository at this point in the history
  • Loading branch information
cailafinn committed May 2, 2024
1 parent 4eb4793 commit f819816
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class MANTID_ALGORITHMS_DLL FlipperEfficiency : public API::Algorithm {
/// Execute the algorithm with the provided properties.
void exec() override;

void saveToFile(API::MatrixWorkspace_sptr const &workspace, std::string const &filepath);
std::map<std::string, std::string> validateInputs() override;

void saveToFile(API::MatrixWorkspace_sptr const &workspace);
};
} // namespace Mantid::Algorithms
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/PolarizationCorrections/FlipperEfficiency.h"
#include <filesystem>

#include "MantidAPI/FileProperty.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAlgorithms/PolarizationCorrections/FlipperEfficiency.h"

namespace {
/// Property Names
namespace PropNames {
std::string const &INPUT_WS{"InputWorkspace"};
std::string const &OUTPUT_WS{"OutputWorkspace"};
std::string const &OUTPUT_FILE{"OutputFilePath"};
std::string const INPUT_WS{"InputWorkspace"};
std::string const OUTPUT_WS{"OutputWorkspace"};
std::string const OUTPUT_FILE{"OutputFilePath"};
} // namespace PropNames

std::string const FILE_EXTENSION{".nxs"};
} // namespace

namespace Mantid::Algorithms {
Expand All @@ -33,21 +37,36 @@ void FlipperEfficiency::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(PropNames::OUTPUT_WS, "", Direction::Output,
PropertyMode::Optional),
"Workspace containing the wavelength-dependent efficiency for the flipper.");
declareProperty(std::make_unique<FileProperty>(PropNames::OUTPUT_FILE, "", FileProperty::OptionalSave, ".nxs"),
declareProperty(std::make_unique<FileProperty>(PropNames::OUTPUT_FILE, "", FileProperty::OptionalSave),
"File name or path for the output to be saved to.");
}

std::map<std::string, std::string> FlipperEfficiency::validateInputs() {
std::map<std::string, std::string> problems;
auto const &outputWs = getPropertyValue(PropNames::OUTPUT_WS);
auto const &outputFile = getPropertyValue(PropNames::OUTPUT_FILE);
if (outputWs.empty() && outputFile.empty()) {
problems[PropNames::OUTPUT_FILE] = "Either an output workspace or output file must be provided.";
problems[PropNames::OUTPUT_WS] = "Either an output workspace or output file must be provided.";
}
return problems;
}

void FlipperEfficiency::exec() {
WorkspaceGroup_sptr const groupWs = getProperty(PropNames::INPUT_WS);
MatrixWorkspace_sptr const firstWs = std::dynamic_pointer_cast<MatrixWorkspace>(groupWs->getItem(0));
auto const filePath = getPropertyValue(PropNames::OUTPUT_FILE);
saveToFile(firstWs, filePath);
saveToFile(firstWs);
}

void FlipperEfficiency::saveToFile(MatrixWorkspace_sptr const &workspace, std::string const &filePath) {
void FlipperEfficiency::saveToFile(MatrixWorkspace_sptr const &workspace) {
std::filesystem::path filePath = getPropertyValue(PropNames::OUTPUT_FILE);
// Add the nexus extension if it's not been applied already.
if (filePath.extension() != FILE_EXTENSION) {
filePath += FILE_EXTENSION;
}
auto saveAlg = createChildAlgorithm("SaveNexus");
saveAlg->initialize();
saveAlg->setProperty("Filename", filePath);
saveAlg->setProperty("Filename", filePath.string());
saveAlg->setProperty("InputWorkspace", workspace);
saveAlg->execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,80 @@
#include "MantidAlgorithms/CreateSampleWorkspace.h"
#include "MantidAlgorithms/GroupWorkspaces.h"
#include "MantidAlgorithms/PolarizationCorrections/FlipperEfficiency.h"
#include "MantidKernel/ConfigService.h"

namespace {} // namespace

using Mantid::Algorithms::CreateSampleWorkspace;
using Mantid::Algorithms::FlipperEfficiency;
using Mantid::Algorithms::GroupWorkspaces;
using Mantid::API::MatrixWorkspace_sptr;
using Mantid::Kernel::ConfigService;

class FlipperEfficiencyTest : public CxxTest::TestSuite {
public:
void setUp() override { m_defaultSaveDirectory = ConfigService::Instance().getString("defaultsave.directory"); }

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

void test_name() {
FlipperEfficiency const alg;
TS_ASSERT_EQUALS(alg.name(), "FlipperEfficiency");
TS_ASSERT_EQUALS(alg.name(), "FlipperEfficiency")
}

void test_version() {
FlipperEfficiency const alg;
TS_ASSERT_EQUALS(alg.version(), 1);
TS_ASSERT_EQUALS(alg.version(), 1)
}

void test_category() {
FlipperEfficiency const alg;
TS_ASSERT_EQUALS(alg.category(), "SANS\\PolarizationCorrections");
TS_ASSERT_EQUALS(alg.category(), "SANS\\PolarizationCorrections")
}

void test_saving_absolute() {
FlipperEfficiency alg;
alg.initialize();
auto temp_filename = std::filesystem::temp_directory_path() /= "something.nxs";
auto group = createTestingWorkspace("testWs");
auto const temp_filename = std::filesystem::temp_directory_path() /= "something.nxs";
auto const &group = createTestingWorkspace("testWs");
alg.setProperty("InputWorkspace", group);
alg.setPropertyValue("OutputFilePath", temp_filename.string());
alg.execute();
TS_ASSERT(std::filesystem::exists(temp_filename))
std::filesystem::remove(temp_filename);
}

void test_saving_relative() {
auto tempDir = std::filesystem::temp_directory_path();
ConfigService::Instance().setString("defaultsave.directory", tempDir);
FlipperEfficiency alg;
alg.initialize();
std::string const &filename = "something.nxs";
auto const &group = createTestingWorkspace("testWs");
alg.setProperty("InputWorkspace", group);
alg.setPropertyValue("OutputFilePath", filename);
alg.execute();
auto savedPath = tempDir /= filename;
TS_ASSERT(std::filesystem::exists(savedPath))
std::filesystem::remove(savedPath);
}

void test_saving_no_ext() {
FlipperEfficiency alg;
alg.initialize();
auto const temp_filename = std::filesystem::temp_directory_path() /= "something";
auto const &group = createTestingWorkspace("testWs");
alg.setProperty("InputWorkspace", group);
alg.setPropertyValue("OutputFilePath", temp_filename.string());
alg.execute();
TS_ASSERT(std::filesystem::exists(temp_filename));
auto savedPath = temp_filename.string() + ".nxs";
TS_ASSERT(std::filesystem::exists(savedPath))
std::filesystem::remove(savedPath);
}

private:
std::string m_defaultSaveDirectory;

Mantid::API::WorkspaceGroup_sptr createTestingWorkspace(std::string const &outName, int const numSpectra = 1,
bool const isMonitor = true, double const binWidth = 0.1) {

Expand Down

0 comments on commit f819816

Please sign in to comment.