diff --git a/Framework/PythonInterface/mantid/api/src/Exports/ADSValidator.cpp b/Framework/PythonInterface/mantid/api/src/Exports/ADSValidator.cpp index 07d4ae5cae4eb71ff9dc324ec38b06eea737c18d..2746f3785167d83ddcb41fbdfb1dd76e7e6b968a 100644 --- a/Framework/PythonInterface/mantid/api/src/Exports/ADSValidator.cpp +++ b/Framework/PythonInterface/mantid/api/src/Exports/ADSValidator.cpp @@ -16,11 +16,11 @@ using namespace boost::python; /// This is the base TypedValidator for most of the WorkspaceValidators void export_ADSValidator() { - TypedValidatorExporter<std::vector<std::string>> - ::define("StringTypedValidator"); + TypedValidatorExporter<std::vector<std::string>>::define( + "StringTypedValidator"); - class_<ADSValidator, bases<TypedValidator<std::vector<std::string>>>, boost::noncopyable>( - "ADSValidator", init<>("Default constructor")) + class_<ADSValidator, bases<TypedValidator<std::vector<std::string>>>, + boost::noncopyable>("ADSValidator", init<>("Default constructor")) .def(init<const bool, const bool>( "Constructor setting allowMultiple and isOptional.", args("allowMultipleSelection", "isOptional"))) diff --git a/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimits.py b/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MatchAndMergeWorkspaces.py similarity index 94% rename from Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimits.py rename to Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MatchAndMergeWorkspaces.py index eacd90f3955b25ca258983f166b1186f9a5186cc..2560619a0bf523701ccfbe6014ed7d3a8e239b15 100644 --- a/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimits.py +++ b/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MatchAndMergeWorkspaces.py @@ -7,18 +7,17 @@ from __future__ import (absolute_import, division, print_function) from mantid.simpleapi import (AnalysisDataService, CloneWorkspace, ConjoinWorkspaces, CropWorkspaceRagged, - DeleteWorkspace, GroupWorkspaces, MatchSpectra, Rebin, SumSpectra) -from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, WorkspaceProperty, WorkspaceGroup, - WorkspaceGroupProperty, ADSValidator) + DeleteWorkspace, MatchSpectra, Rebin, SumSpectra) +from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, WorkspaceProperty, WorkspaceGroup, ADSValidator) from mantid.dataobjects import Workspace2D from mantid.kernel import (Direction, FloatArrayProperty, StringArrayProperty) import numpy as np -class MergeWorkspacesWithLimits(DataProcessorAlgorithm): +class MatchAndMergeWorkspaces(DataProcessorAlgorithm): def name(self): - return 'MergeWorkspacesWithLimits' + return 'MatchAndMergeWorkspaces' def category(self): return 'Workflow\\Diffraction' @@ -114,7 +113,8 @@ class MergeWorkspacesWithLimits(DataProcessorAlgorithm): DeleteWorkspace(ws_conjoined) self.setProperty('OutputWorkspace', merged_ws) - def unwrap_groups(self, inputs): + @staticmethod + def unwrap_groups(inputs): output = [] for name_in_list in inputs: ws_in_list = AnalysisDataService.retrieve(name_in_list) @@ -160,4 +160,4 @@ class MergeWorkspacesWithLimits(DataProcessorAlgorithm): # Register algorithm with Mantid -AlgorithmFactory.subscribe(MergeWorkspacesWithLimits) +AlgorithmFactory.subscribe(MatchAndMergeWorkspaces) diff --git a/Framework/PythonInterface/test/python/plugins/algorithms/WorkflowAlgorithms/MatchAndMergeWorkspacesTest.py b/Framework/PythonInterface/test/python/plugins/algorithms/WorkflowAlgorithms/MatchAndMergeWorkspacesTest.py new file mode 100644 index 0000000000000000000000000000000000000000..be1e574a2784ce5dfaa628d428afbca7c822390c --- /dev/null +++ b/Framework/PythonInterface/test/python/plugins/algorithms/WorkflowAlgorithms/MatchAndMergeWorkspacesTest.py @@ -0,0 +1,74 @@ +# Mantid Repository : https://github.com/mantidproject/mantid +# +# Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI, +# NScD Oak Ridge National Laboratory, European Spallation Source +# & Institut Laue - Langevin +# SPDX - License - Identifier: GPL - 3.0 + +from __future__ import (absolute_import, division, print_function) + +import unittest +import numpy as np +from mantid.api import MatrixWorkspace +from mantid.simpleapi import (AnalysisDataService, ConjoinWorkspaces, CreateWorkspace, MatchAndMergeWorkspaces, + GroupWorkspaces, DeleteWorkspace) + + +class MatchAndMergeWorkspacesTest(unittest.TestCase): + + def setUp(self): + ws_list = [] + for i in range(5): + ws_name = 'ws_' + str(i+1) + data_x = np.arange(i, (i+1)*10+0.1, 0.1) + data_y = np.arange(i, (i+1)*10, 0.1) + data_e = np.arange(i, (i+1)*10, 0.1) + CreateWorkspace(OutputWorkspace=ws_name, DataX=data_x, DataY=data_y, DataE=data_e) + ws_list.append(ws_name) + GroupWorkspaces(InputWorkspaces=ws_list, OutputWorkspace='ws_group') + + def test_MatchAndMergeWorkspaces_executes(self): + x_min = np.array([0, 5, 10, 15, 20]) + x_max = np.array([10, 20, 30, 40, 50]) + ws_merged = MatchAndMergeWorkspaces(InputWorkspaces='ws_group', XMin=x_min, XMax=x_max) + self.assertIsInstance(ws_merged, MatrixWorkspace) + self.assertEqual(ws_merged.getNumberHistograms(), 1) + self.assertAlmostEqual(min(ws_merged.dataX(0)), 0, places=0) + self.assertAlmostEqual(max(ws_merged.dataX(0)), 50, places=0) + + def test_MatchAndMergeWorkspaces_produces_correct_range(self): + x_min = np.array([2, 5, 10, 15, 20]) + x_max = np.array([10, 20, 30, 40, 45]) + ws_merged = MatchAndMergeWorkspaces(InputWorkspaces='ws_group', XMin=x_min, XMax=x_max) + self.assertIsInstance(ws_merged, MatrixWorkspace) + self.assertEqual(ws_merged.getNumberHistograms(), 1) + self.assertAlmostEqual(min(ws_merged.dataX(0)), 2, places=0) + self.assertAlmostEqual(max(ws_merged.dataX(0)), 45, places=0) + + def test_MatchAndMergeWorkspaces_accepts_a_list_of_workspaces(self): + x_min = np.array([2, 5, 10]) + x_max = np.array([10, 20, 30]) + ws_group = AnalysisDataService.retrieve('ws_group') + ws_list = [ws_group[0], ws_group[1], ws_group[2]] + ws_merged = MatchAndMergeWorkspaces(InputWorkspaces=ws_list, XMin=x_min, XMax=x_max) + self.assertIsInstance(ws_merged, MatrixWorkspace) + self.assertEqual(ws_merged.getNumberHistograms(), 1) + self.assertAlmostEqual(min(ws_merged.dataX(0)), 2, places=0) + self.assertAlmostEqual(max(ws_merged.dataX(0)), 30, places=0) + + def test_MatchAndMergeWorkspaces_accepts_a_mixture_of_ws_size(self): + x_min = np.array([2, 5, 10, 15, 20]) + x_max = np.array([10, 20, 30, 40, 45]) + ws_group = AnalysisDataService.retrieve('ws_group') + ConjoinWorkspaces(InputWorkspace1=ws_group[3], + InputWorkspace2=ws_group[4], + CheckOverlapping=False) + ws_list = [ws_group[0], ws_group[1], ws_group[2], ws_group[3]] + ws_merged = MatchAndMergeWorkspaces(InputWorkspaces=ws_list, XMin=x_min, XMax=x_max) + self.assertIsInstance(ws_merged, MatrixWorkspace) + self.assertEqual(ws_merged.getNumberHistograms(), 1) + self.assertAlmostEqual(min(ws_merged.dataX(0)), 2, places=0) + self.assertAlmostEqual(max(ws_merged.dataX(0)), 45, places=0) + + +if __name__ == "__main__": + unittest.main() diff --git a/Framework/PythonInterface/test/python/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimitsTest.py b/Framework/PythonInterface/test/python/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimitsTest.py deleted file mode 100644 index cab4568c08f29ffa7ed7f226a435801f500ee5f0..0000000000000000000000000000000000000000 --- a/Framework/PythonInterface/test/python/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimitsTest.py +++ /dev/null @@ -1,48 +0,0 @@ -# Mantid Repository : https://github.com/mantidproject/mantid -# -# Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI, -# NScD Oak Ridge National Laboratory, European Spallation Source -# & Institut Laue - Langevin -# SPDX - License - Identifier: GPL - 3.0 + -from __future__ import (absolute_import, division, print_function) - -import unittest -import numpy as np -from mantid.api import MatrixWorkspace -from mantid.simpleapi import CreateWorkspace, MergeWorkspacesWithLimits, GroupWorkspaces, DeleteWorkspace - - -class MergeWorkspacesWithLimitsTest(unittest.TestCase): - - def setUp(self): - ws_list = [] - for i in range(5): - ws_name = 'ws_' + str(i+1) - data_x = np.arange(i, (i+1)*10+0.1, 0.1) - data_y = np.arange(i, (i+1)*10, 0.1) - data_e = np.arange(i, (i+1)*10, 0.1) - CreateWorkspace(OutputWorkspace=ws_name, DataX=data_x, DataY=data_y, DataE=data_e) - ws_list.append(ws_name) - GroupWorkspaces(InputWorkspaces=ws_list, OutputWorkspace='ws_group') - - def test_MergeWorkspacesWithLimits_executes(self): - x_min = np.array([0, 5, 10, 15, 20]) - x_max = np.array([10, 20, 30, 40, 50]) - ws_merged = MergeWorkspacesWithLimits(WorkspaceGroup='ws_group', XMin=x_min, XMax=x_max) - self.assertIsInstance(ws_merged, MatrixWorkspace) - self.assertEqual(ws_merged.getNumberHistograms(), 1) - self.assertAlmostEqual(min(ws_merged.dataX(0)), 0, places=0) - self.assertAlmostEqual(max(ws_merged.dataX(0)), 50, places=0) - - def test_MergeWorkspacesWithLimits_produces_correct_range(self): - x_min = np.array([2, 5, 10, 15, 20]) - x_max = np.array([10, 20, 30, 40, 45]) - ws_merged = MergeWorkspacesWithLimits(WorkspaceGroup='ws_group', XMin=x_min, XMax=x_max) - self.assertIsInstance(ws_merged, MatrixWorkspace) - self.assertEqual(ws_merged.getNumberHistograms(), 1) - self.assertAlmostEqual(min(ws_merged.dataX(0)), 2, places=0) - self.assertAlmostEqual(max(ws_merged.dataX(0)), 45, places=0) - - -if __name__ == "__main__": - unittest.main() diff --git a/docs/source/algorithms/MergeWorkspacesWithLimits-v1.rst b/docs/source/algorithms/MatchAndMergeWorkspaces-v1.rst similarity index 82% rename from docs/source/algorithms/MergeWorkspacesWithLimits-v1.rst rename to docs/source/algorithms/MatchAndMergeWorkspaces-v1.rst index 5b72e49f4aa8e7f94700d3cdb08c32d090eb4a6e..b6e64b2a8409e535b3f5d000a1f4991fc001787e 100644 --- a/docs/source/algorithms/MergeWorkspacesWithLimits-v1.rst +++ b/docs/source/algorithms/MatchAndMergeWorkspaces-v1.rst @@ -9,8 +9,9 @@ Description ----------- -This is a workflow algorithm that merges down a workspace group -and sums the spectra using weighted mean and Ranges for each +This is a workflow algorithm that merges down a workspace, workspace +group, or list of workspaces, `MatchSpectra <algm-MatchSpectra>` +and sums the spectra using weighted mean from ranges for each spectra. For each workspace an XMin and XMax is supplied, the workspace is then cropped to that range and all workspaces are Rebinned to have common bin edges. The output is the mean value @@ -49,7 +50,7 @@ Usage polaris.create_total_scattering_pdf(run_number="98533", merge_banks=False) x_min = np.array([0.5, 3, 4, 6, 7]) x_max = np.array([3.5, 5, 7, 11, 20]) - merged_ws = MergeWorkspacesWithLimits(WorkspaceGroup='focused_ws', XMin=x_min, XMax=x_max) + merged_ws = MatchAndMergeWorkspaces(WorkspaceGroup='focused_ws', XMin=x_min, XMax=x_max, CalculateScale=False) fig, ax = plt.subplots(subplot_kw={'projection':'mantid'}) ax.plot(merged_ws) @@ -58,9 +59,9 @@ Usage This will produce a plot that looks like this: -.. figure:: images/MergeWorkspacesWithLimits.png +.. figure:: ../images/MatchAndMergeWorkspaces.png Workflow ######## -.. diagram:: MergeWorkspacesWithLimits-v1_wkflw.dot +.. diagram:: MatchAndMergeWorkspaces-v1_wkflw.dot diff --git a/docs/source/diagrams/MergeWorkspacesWithLimits-v1_wkflw.dot b/docs/source/diagrams/MatchAndMergeWorkspaces-v1_wkflw.dot similarity index 80% rename from docs/source/diagrams/MergeWorkspacesWithLimits-v1_wkflw.dot rename to docs/source/diagrams/MatchAndMergeWorkspaces-v1_wkflw.dot index 1a027789cf4925366d69ec3d64ed3260c7ab7533..99d01bd1ed07cecc8aae80d6e4b786cd99a8d7f1 100644 --- a/docs/source/diagrams/MergeWorkspacesWithLimits-v1_wkflw.dot +++ b/docs/source/diagrams/MatchAndMergeWorkspaces-v1_wkflw.dot @@ -1,19 +1,21 @@ -digraph MergeWorkspacesWithLimits { -label = "MergeWorkspacesWithLimits Workflow Diagram" +digraph MatchAndMergeWorkspaces { +label = "MatchAndMergeWorkspaces Workflow Diagram" $global_style subgraph params { $param_style - inputWorkspace [label="WorkspaceGroup"] + inputWorkspace [label="InputWorkspaces"] outputWorkspace [label="OutputWorkspace"] xMin [label="XMin"] xMax [label="XMax"] + calculateScale [label="CalculateScale"] + calculateOffset [label="CalculateOffset"] matchSpectraIndex [label="Index of spectra with largest X range"] } subgraph decisions { $decision_style - isGroupWsNotConjoined [label="Are there\n un-conjoined workspaces\n in the WorkspaceGroup"] + isGroupWsNotConjoined [label="Are there\n un-conjoined workspaces\n in the workspace list"] } subgraph algorithms { @@ -39,6 +41,8 @@ isGroupWsNotConjoined -> ConjoinWorkspaces [label="Yes"] isGroupWsNotConjoined -> MatchSpectra [label="No"] ConjoinWorkspaces -> isGroupWsNotConjoined matchSpectraIndex -> MatchSpectra +calculateScale -> MatchSpectra +calculateOffset -> MatchSpectra MatchSpectra -> CropWorkspaceRagged xMin -> CropWorkspaceRagged xMax -> CropWorkspaceRagged diff --git a/docs/source/images/MergeWorkspacesWithLimits.png b/docs/source/images/MatchAndMergeWorkspaces.png similarity index 100% rename from docs/source/images/MergeWorkspacesWithLimits.png rename to docs/source/images/MatchAndMergeWorkspaces.png diff --git a/docs/source/release/v4.3.0/framework.rst b/docs/source/release/v4.3.0/framework.rst index 3764cfb83c0fcb51d21eab906378e29843cc2fc2..2b574ab1541c110c45bf84f762d12ab568934ce5 100644 --- a/docs/source/release/v4.3.0/framework.rst +++ b/docs/source/release/v4.3.0/framework.rst @@ -19,7 +19,7 @@ Algorithms ---------- - :ref:`TotScatCalculateSelfScattering <algm-TotScatCalculateSelfScattering>` will calculate a normalized self scattering correction for foccues total scattering data. -- :ref:`MergeWorkspacesWithLimits <algm-MergeWorkspacesWithLimits>` will merge workspaces in a workspace group withing weighting from a set of limits for each workspace. +- :ref:`MatchAndMergeWorkspaces <algm-MatchAndMergeWorkspaces>` will merge workspaces in a workspace group withing weighting from a set of limits for each workspace and using `MatchSpectra <algm-MatchSpectra>`. Data Objects ------------ diff --git a/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py b/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py index 6fa87c21b96ce1ebb1118d31f8a81e3e9e5e0e3e..8cf5287de594446ed236ea15dec0dd359d5139cb 100644 --- a/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py +++ b/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py @@ -105,8 +105,8 @@ def generate_ts_pdf(run_number, focus_file_path, merge_banks=False, q_lims=None, if merge_banks: q_min, q_max = _load_qlims(q_lims) - merged_ws = mantid.MergeWorkspacesWithLimits(InputWorkspaces=focused_ws, XMin=q_min, XMax=q_max, - CalculateScale=False) + merged_ws = mantid.MatchAndMergeWorkspaces(InputWorkspaces=focused_ws, XMin=q_min, XMax=q_max, + CalculateScale=False) pdf_output = mantid.PDFFourierTransform(Inputworkspace=merged_ws, InputSofQType="S(Q)-1", PDFType="G(r)", Filter=True) else: