From 2aa835f7bec52611af443fe6c1f66990d3cfea24 Mon Sep 17 00:00:00 2001 From: Verena Reimund <reimund@ill.eu> Date: Tue, 3 Apr 2018 15:53:52 +0200 Subject: [PATCH] Enable ascending / descending odering of sorted data Refs #22217 --- .../plugins/algorithms/SortXAxis.py | 13 ++++- .../plugins/algorithms/SortXAxisTest.py | 56 ++++++++++++------- docs/source/algorithms/SortXAxis-v1.rst | 2 +- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/Framework/PythonInterface/plugins/algorithms/SortXAxis.py b/Framework/PythonInterface/plugins/algorithms/SortXAxis.py index 6a5c8dbb4d7..3f52326f699 100644 --- a/Framework/PythonInterface/plugins/algorithms/SortXAxis.py +++ b/Framework/PythonInterface/plugins/algorithms/SortXAxis.py @@ -2,10 +2,9 @@ from __future__ import (absolute_import, division, print_function) from mantid.api import AlgorithmFactory, MatrixWorkspaceProperty, PythonAlgorithm -from mantid.kernel import Direction +from mantid.kernel import Direction, StringListValidator import numpy as np - class SortXAxis(PythonAlgorithm): def category(self): @@ -22,6 +21,11 @@ class SortXAxis(PythonAlgorithm): 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 @@ -42,7 +46,12 @@ class SortXAxis(PythonAlgorithm): indexes = x_data.argsort() + if self.getPropertyValue("Ordering") == "Descending": + self.log().notice("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) diff --git a/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py b/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py index aba5747cecc..9b43e770f95 100644 --- a/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py +++ b/Framework/PythonInterface/test/python/plugins/algorithms/SortXAxisTest.py @@ -8,9 +8,9 @@ class SortXAxisTest(unittest.TestCase): def test_x_ascending(self): - dataX = [1, 2, 3] # In ascending order, so y and e will need to be reversed. - dataY = [1, 2, 3] - dataE = [1, 2, 3] + dataX = [1., 2., 3.] # In ascending order, so y and e will need to be reversed. + dataY = [1., 2., 3.] + dataE = [1., 2., 3.] unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=True) # Run the algorithm sortedws = SortXAxis(InputWorkspace=unsortedws) @@ -25,9 +25,9 @@ class SortXAxisTest(unittest.TestCase): DeleteWorkspace(sortedws) def test_x_descending(self): - dataX = [3, 2, 1] # In descending order, so y and e will need to be reversed. - dataY = [1, 2, 3] - dataE = [1, 2, 3] + dataX = [3., 2., 1.] # In descending order, so y and e will need to be reversed. + dataY = [1., 2., 3.] + dataE = [1., 2., 3.] unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=True) # Run the algorithm sortedws = SortXAxis(InputWorkspace=unsortedws) @@ -44,9 +44,9 @@ class SortXAxisTest(unittest.TestCase): DeleteWorkspace(sortedws) def test_on_multiple_spectrum(self): - dataX = [3, 2, 1, 3, 2, 1] # In descending order, so y and e will need to be reversed. - dataY = [1, 2, 3, 1, 2, 3] - dataE = [1, 2, 3, 1, 2, 3] + dataX = [3., 2., 1., 3., 2., 1.] # In descending order, so y and e will need to be reversed. + dataY = [1., 2., 3., 1., 2., 3.] + dataE = [1., 2., 3., 1., 2., 3.] unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=True, NSpec=2) dataY.reverse() dataE.reverse() @@ -70,9 +70,9 @@ class SortXAxisTest(unittest.TestCase): DeleteWorkspace(sortedws) def test_sorts_x_histogram_ascending(self): - dataX = [1, 2, 3, 4] - dataY = [1, 2, 3] - dataE = [1, 2, 3] + dataX = [1., 2., 3., 4.] + dataY = [1., 2., 3.] + dataE = [1., 2., 3.] unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=False) # Run the algorithm sortedws = SortXAxis(InputWorkspace=unsortedws) @@ -88,9 +88,9 @@ class SortXAxisTest(unittest.TestCase): DeleteWorkspace(sortedws) def test_sorts_x_histogram_descending(self): - dataX = [4, 3, 2, 1] - dataY = [1, 2, 3] - dataE = [1, 2, 3] + dataX = [4., 3., 2., 1.] + dataY = [1., 2., 3.] + dataE = [1., 2., 3.] unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=False) # Run the algorithm sortedws = SortXAxis(InputWorkspace=unsortedws) @@ -111,9 +111,9 @@ class SortXAxisTest(unittest.TestCase): # Create unsorted workspace parent = AlgorithmManager.createUnmanaged('Load') create_ws_alg = parent.createChildAlgorithm("CreateWorkspace") - dataX = [4, 3, 2, 1] - dataY = [1, 2, 3] - dataE = [1, 2, 3] + dataX = [4., 3., 2., 1.] + dataY = [1., 2., 3.] + dataE = [1., 2., 3.] create_ws_alg.setProperty("DataX", dataX) create_ws_alg.setProperty("DataY", dataY) create_ws_alg.setProperty("DataE", dataE) @@ -153,9 +153,9 @@ class SortXAxisTest(unittest.TestCase): DeleteWorkspace(sortedws) def test_dx_histogram_ascending(self): - dataX = [1, 2, 3, 4] - dataY = [1, 2, 3] - dx = [1, 2, 3] + dataX = [1., 2., 3., 4.] + dataY = [1., 2., 3.] + dx = [1., 2., 3.] unsortedws = CreateWorkspace(DataX=dataX, DataY=dataY, Dx=dx, UnitX='TOF', Distribution=False) # Run the algorithm sortedws = SortXAxis(InputWorkspace=unsortedws) @@ -165,5 +165,19 @@ class SortXAxisTest(unittest.TestCase): DeleteWorkspace(unsortedws) DeleteWorkspace(sortedws) + def test_sort_descending(self): + dataX = [1., 2., 3., 4.] + dataY = [1., 2., 3.] + unsortedws = CreateWorkspace(DataX=dataX, DataY=dataY, UnitX='TOF', Distribution=False) + # Run the algorithm + sortedws = SortXAxis(InputWorkspace=unsortedws, Ordering="Descending") + sortedX = sortedws.readX(0) + sortedY = sortedws.readY(0) + # Check the resulting data values. Sorting operation should have resulted in no changes + self.assertEqual([4., 3., 2., 1.], sortedX.tolist()) + self.assertEqual([3., 2., 1.], sortedY.tolist()) + DeleteWorkspace(unsortedws) + DeleteWorkspace(sortedws) + if __name__ == '__main__': unittest.main() diff --git a/docs/source/algorithms/SortXAxis-v1.rst b/docs/source/algorithms/SortXAxis-v1.rst index 7d732196c1d..a02d7ec28ed 100644 --- a/docs/source/algorithms/SortXAxis-v1.rst +++ b/docs/source/algorithms/SortXAxis-v1.rst @@ -10,7 +10,7 @@ Description ----------- Clones the input :ref:`Matrix Workspaces <MatrixWorkspace>` and orders the -x-axis in an ascending fashion. Ensures that the y-axis and error data as well as optional Dx data +x-axis in an ascending or descending fashion. Ensures that the y-axis and error data as well as optional Dx data are sorted in a consistent way with the x-axis. This algorithm is for use with small workspaces loaded. It is -- GitLab