From 37c9fb98f782b261486def18aff8934bd7a5179e Mon Sep 17 00:00:00 2001 From: Samuel Jones <samjones714@gmail.com> Date: Thu, 26 Jul 2018 09:29:28 +0100 Subject: [PATCH] Re #22515 Made changes based on recommendations --- .../inc/MantidAlgorithms/SortXAxis2.h | 30 +++---- Framework/Algorithms/src/SortXAxis2.cpp | 44 +++++----- .../plugins/algorithms/SortXAxis.py | 81 ------------------- .../plugins/algorithms/SortXAxisTest.py | 6 +- 4 files changed, 40 insertions(+), 121 deletions(-) delete mode 100644 Framework/PythonInterface/plugins/algorithms/SortXAxis.py diff --git a/Framework/Algorithms/inc/MantidAlgorithms/SortXAxis2.h b/Framework/Algorithms/inc/MantidAlgorithms/SortXAxis2.h index b920f97ee7e..03d064c4b09 100644 --- a/Framework/Algorithms/inc/MantidAlgorithms/SortXAxis2.h +++ b/Framework/Algorithms/inc/MantidAlgorithms/SortXAxis2.h @@ -2,11 +2,6 @@ #define MANTID_ALGORITHMS_SORTXAXIS_H_ #include "MantidAPI/Algorithm.h" -#include "MantidAPI/MatrixWorkspace.h" -#include "MantidAPI/WorkspaceProperty.h" -#include "MantidKernel/ListValidator.h" -#include "MantidKernel/System.h" -#include "MantidKernel/make_unique.h" namespace Mantid { namespace Algorithms { @@ -57,28 +52,27 @@ private: void sortIndicesByX(std::vector<std::size_t> &workspaceIndecies, std::string order, - Mantid::API::MatrixWorkspace_const_sptr inputWorkspace, + const Mantid::API::MatrixWorkspace *inputWorkspace, unsigned int specNum); - void copyYandEToOutputWorkspace( - std::vector<std::size_t> &workspaceIndecies, - Mantid::API::MatrixWorkspace_const_sptr inputWorkspace, - Mantid::API::MatrixWorkspace_sptr outputWorkspace, unsigned int SpecNum, - bool isAProperHistogram); + void + copyYandEToOutputWorkspace(std::vector<std::size_t> &workspaceIndecies, + const Mantid::API::MatrixWorkspace *inputWorkspace, + Mantid::API::MatrixWorkspace_sptr outputWorkspace, + unsigned int SpecNum, bool isAProperHistogram); void copyXandDxToOutputWorkspace( std::vector<std::size_t> &workspaceIndecies, - Mantid::API::MatrixWorkspace_const_sptr inputWorkspace, + const Mantid::API::MatrixWorkspace *inputWorkspace, Mantid::API::MatrixWorkspace_sptr outputWorkspace, unsigned int specNum); - void - copyToOutputWorkspace(std::vector<std::size_t> &workspaceIndecies, - Mantid::API::MatrixWorkspace_const_sptr inputWorkspace, - Mantid::API::MatrixWorkspace_sptr outputWorkspace, - unsigned int specNum, bool isAProperHistogram); + void copyToOutputWorkspace(std::vector<std::size_t> &workspaceIndecies, + const Mantid::API::MatrixWorkspace *inputWorkspace, + Mantid::API::MatrixWorkspace_sptr outputWorkspace, + unsigned int specNum, bool isAProperHistogram); bool determineIfHistogramIsValid( - Mantid::API::MatrixWorkspace_const_sptr inputWorkspace); + const Mantid::API::MatrixWorkspace *inputWorkspace); }; } // namespace Algorithms diff --git a/Framework/Algorithms/src/SortXAxis2.cpp b/Framework/Algorithms/src/SortXAxis2.cpp index de20a45d213..561df9be973 100644 --- a/Framework/Algorithms/src/SortXAxis2.cpp +++ b/Framework/Algorithms/src/SortXAxis2.cpp @@ -1,5 +1,10 @@ #include "MantidAlgorithms/SortXAxis2.h" -#include <iostream> +#include "MantidAPI/Algorithm.h" +#include "MantidAPI/MatrixWorkspace.h" +#include "MantidAPI/WorkspaceProperty.h" +#include "MantidKernel/ListValidator.h" +#include "MantidKernel/System.h" +#include "MantidKernel/make_unique.h" using namespace Mantid::Kernel; using namespace Mantid::API; @@ -44,23 +49,24 @@ void SortXAxis::exec() { MatrixWorkspace_sptr outputWorkspace = inputWorkspace->clone(); // Check if it is a valid histogram here - bool isAProperHistogram = determineIfHistogramIsValid(inputWorkspace); + bool isAProperHistogram = determineIfHistogramIsValid(inputWorkspace.get()); // Define everything you can outside of the for loop // Assume that all spec are the same size const auto sizeOfX = inputWorkspace->x(0).size(); - PARALLEL_FOR_IF(Kernel::threadSafe(*inputWorkspace, *outputWorkspace)) + PARALLEL_FOR_IF( + Kernel::threadSafe(*inputWorkspace.get(), *outputWorkspace.get())) for (int specNum = 0u; specNum < (int)inputWorkspace->getNumberHistograms(); specNum++) { PARALLEL_START_INTERUPT_REGION auto workspaceIndicies = createIndexes(sizeOfX); - sortIndicesByX(workspaceIndicies, getProperty("Ordering"), inputWorkspace, - specNum); + sortIndicesByX(workspaceIndicies, getProperty("Ordering"), + inputWorkspace.get(), specNum); - copyToOutputWorkspace(workspaceIndicies, inputWorkspace, outputWorkspace, - specNum, isAProperHistogram); + copyToOutputWorkspace(workspaceIndicies, inputWorkspace.get(), + outputWorkspace, specNum, isAProperHistogram); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION @@ -95,7 +101,7 @@ std::vector<std::size_t> SortXAxis::createIndexes(const size_t sizeOfX) { */ template <typename Comparator> void sortByXValue(std::vector<std::size_t> &workspaceIndicies, - MatrixWorkspace_const_sptr inputWorkspace, + const Mantid::API::MatrixWorkspace *inputWorkspace, unsigned int specNum, Comparator const &compare) { std::sort(workspaceIndicies.begin(), workspaceIndicies.end(), [&](std::size_t lhs, std::size_t rhs) -> bool { @@ -104,10 +110,9 @@ void sortByXValue(std::vector<std::size_t> &workspaceIndicies, }); } -void SortXAxis::sortIndicesByX(std::vector<std::size_t> &workspaceIndicies, - std::string order, - MatrixWorkspace_const_sptr inputWorkspace, - unsigned int specNum) { +void SortXAxis::sortIndicesByX( + std::vector<std::size_t> &workspaceIndicies, std::string order, + const Mantid::API::MatrixWorkspace *inputWorkspace, unsigned int specNum) { if (order == "Ascending") { sortByXValue(workspaceIndicies, inputWorkspace, specNum, std::less<double>()); @@ -129,7 +134,7 @@ void SortXAxis::sortIndicesByX(std::vector<std::size_t> &workspaceIndicies, */ void SortXAxis::copyXandDxToOutputWorkspace( std::vector<std::size_t> &workspaceIndicies, - MatrixWorkspace_const_sptr inputWorkspace, + const Mantid::API::MatrixWorkspace *inputWorkspace, MatrixWorkspace_sptr outputWorkspace, unsigned int specNum) { // Move an ordered X to the output workspace for (auto workspaceIndex = 0u; @@ -164,7 +169,7 @@ void SortXAxis::copyXandDxToOutputWorkspace( */ void SortXAxis::copyYandEToOutputWorkspace( std::vector<std::size_t> &workspaceIndicies, - MatrixWorkspace_const_sptr inputWorkspace, + const Mantid::API::MatrixWorkspace *inputWorkspace, MatrixWorkspace_sptr outputWorkspace, unsigned int specNum, bool isAProperHistogram) { // If Histogram data find the biggest index value and remove it from @@ -193,7 +198,7 @@ void SortXAxis::copyYandEToOutputWorkspace( void SortXAxis::copyToOutputWorkspace( std::vector<std::size_t> &workspaceIndicies, - MatrixWorkspace_const_sptr inputWorkspace, + const Mantid::API::MatrixWorkspace *inputWorkspace, MatrixWorkspace_sptr outputWorkspace, unsigned int specNum, bool isAProperHistogram) { copyXandDxToOutputWorkspace(workspaceIndicies, inputWorkspace, @@ -215,13 +220,14 @@ void SortXAxis::copyToOutputWorkspace( */ template <typename Comparator> bool isItSorted(Comparator const &compare, - MatrixWorkspace_const_sptr inputWorkspace) { + const Mantid::API::MatrixWorkspace *inputWorkspace) { for (auto specNum = 0u; specNum < inputWorkspace->getNumberHistograms(); specNum++) { if (!std::is_sorted(inputWorkspace->x(specNum).begin(), inputWorkspace->x(specNum).end(), - [&](double lhs, double rhs) - -> bool { return compare(lhs, rhs); })) { + [&](double lhs, double rhs) -> bool { + return compare(lhs, rhs); + })) { return false; } } @@ -236,7 +242,7 @@ bool isItSorted(Comparator const &compare, * @return false if it is not a histogram, and is thus point data */ bool SortXAxis::determineIfHistogramIsValid( - MatrixWorkspace_const_sptr inputWorkspace) { + const Mantid::API::MatrixWorkspace *inputWorkspace) { // Assuming all X and Ys are the same, if X is not the same size as y, assume // it is a histogram if (inputWorkspace->x(0).size() != inputWorkspace->y(0).size()) { diff --git a/Framework/PythonInterface/plugins/algorithms/SortXAxis.py b/Framework/PythonInterface/plugins/algorithms/SortXAxis.py deleted file mode 100644 index 9255d00c331..00000000000 --- a/Framework/PythonInterface/plugins/algorithms/SortXAxis.py +++ /dev/null @@ -1,81 +0,0 @@ -#pylint: disable=no-init,invalid-name -from __future__ import (absolute_import, division, print_function) - -from mantid.api import AlgorithmFactory, MatrixWorkspaceProperty, PythonAlgorithm -from mantid.kernel import Direction, StringListValidator -import numpy as np - - -class SortXAxis(PythonAlgorithm): - - def category(self): - return "Transforms\\Axes;Utility\\Sorting" - - def seeAlso(self): - return [ "SortDetectors" ] - - def version(self): - return 1 - - def name(self): - return "SortXAxis" - - def summary(self): - return "Clones the input MatrixWorkspace(s) and orders the x-axis in an ascending fashion." - - def PyInit(self): - self.declareProperty(MatrixWorkspaceProperty("InputWorkspace", defaultValue="", direction=Direction.Input), - doc="Input workspace") - self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", defaultValue="", direction=Direction.Output), - doc="Sorted Output Workspace") - self.declareProperty("Ordering", - defaultValue="Ascending", - validator=StringListValidator(["Ascending", "Descending"]), - direction=Direction.Input, - doc="Ascending or descending sorting") - - def PyExec(self): - input_ws = self.getProperty('InputWorkspace').value - output_ws = self.getPropertyValue('OutputWorkspace') - - num_specs = input_ws.getNumberHistograms() - - clone_alg = self.createChildAlgorithm("CloneWorkspace", enableLogging=False) - clone_alg.setProperty("InputWorkspace", input_ws) - clone_alg.setProperty("OutputWorkspace", output_ws) - clone_alg.execute() - output_ws = clone_alg.getProperty("OutputWorkspace").value - - for i in range(0, num_specs): - x_data = input_ws.readX(i) - y_data = input_ws.readY(i) - e_data = input_ws.readE(i) - - indexes = x_data.argsort() - - if self.getPropertyValue("Ordering") == "Descending": - self.log().information("Sort descending") - indexes = indexes[::-1] - - x_ordered = x_data[indexes] - - if input_ws.isHistogramData(): - max_index = np.argmax(indexes) - indexes = np.delete(indexes, max_index) - - y_ordered = y_data[indexes] - e_ordered = e_data[indexes] - - output_ws.setX(i, x_ordered) - output_ws.setY(i, y_ordered) - output_ws.setE(i, e_ordered) - - if input_ws.hasDx(i): - dx = input_ws.readDx(i) - dx_ordered = dx[indexes] - output_ws.setDx(i, dx_ordered) - - self.setProperty('OutputWorkspace', output_ws) - - -AlgorithmFactory.subscribe(SortXAxis) diff --git a/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py b/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py index cc938dbc341..9b43e770f95 100644 --- a/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py +++ b/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py @@ -80,9 +80,9 @@ class SortXAxisTest(unittest.TestCase): sortedY = sortedws.readY(0) sortedE = sortedws.readE(0) # Check the resulting data values. Sorting operation should have resulted in no changes - #self.assertEqual(dataX, sortedX.tolist()) - #self.assertEqual(dataY, sortedY.tolist()) - #self.assertEqual(dataE, sortedE.tolist()) + self.assertEqual(dataX, sortedX.tolist()) + self.assertEqual(dataY, sortedY.tolist()) + self.assertEqual(dataE, sortedE.tolist()) DeleteWorkspace(unsortedws) DeleteWorkspace(sortedws) -- GitLab