From 4f837f2f9c9bbc43069bcd8c30b5c32955b9d8a3 Mon Sep 17 00:00:00 2001 From: Michael Wedel Date: Thu, 7 Aug 2014 13:18:06 +0200 Subject: [PATCH] Refs #9711. Refactored PoldiMerge to use MergeRuns Consequently, WorkspaceGroups can be handled now as well. --- .../plugins/algorithms/PoldiMerge.py | 54 +++++++++++-------- .../plugins/algorithms/PoldiMergeTest.py | 19 +++++++ 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/PoldiMerge.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/PoldiMerge.py index 6c19116f6395..75ae00ed6b1c 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/PoldiMerge.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/PoldiMerge.py @@ -13,10 +13,10 @@ def category(self): def name(self): return "PoldiMerge" - + def summary(self): return "PoldiMerge takes a list of workspace names and adds the counts, resulting in a new workspace." - + def PyInit(self): self.declareProperty(StringArrayProperty(name="WorkspaceNames", direction=Direction.Input), @@ -33,32 +33,28 @@ def PyExec(self): self.log().information("Workspaces to merge: %i" % (len(workspaceNames))) - if False in [AnalysisDataService.doesExist(x) for x in workspaceNames]: - raise KeyError("Not all strings in the input list are valid workspace names.") + workspaces = [] - workspaces = [AnalysisDataService.retrieve(x) for x in workspaceNames] - - # Create a target workspace for the summation. It inherits the log of - # the first workspace used in the summation. - output = WorkspaceFactory.create(workspaces[0]) + for wsName in workspaceNames: + if not AnalysisDataService.doesExist(wsName): + raise KeyError("Not all strings in the input list are valid workspace names.") - xdata = workspaces[0].dataX(0) - ydata = np.zeros(len(xdata)) + ws = AnalysisDataService.retrieve(wsName) + workspaces += self.getWorkspacesRecursive(ws) - for h in range(output.getNumberHistograms()): - output.setX(h, xdata) - output.setY(h, ydata) - AnalysisDataService.addOrReplace(self.outputWorkspaceName, output) + workspaceCount = len(workspaces) - while workspaces: - current = workspaces.pop(0) + for i in range(workspaceCount): + currentWorkspace = workspaces[i] + for j in range(workspaceCount): + if i != j: + try: + self.canMerge(currentWorkspace, workspaces[j]) + except RuntimeError as error: + self.handleError(error) - try: - if self.canMerge(output, current): - output += current - except RuntimeError as error: - self.handleError(error) + output = MergeRuns(workspaceNames) self.setProperty("OutputWorkspace", output) @@ -96,4 +92,18 @@ def handleError(self, error): raise RuntimeError("Workspaces can not be merged. %s. Aborting." % (str(error))) + def getWorkspacesRecursive(self, workspace): + returnList = [] + if isinstance(workspace, WorkspaceGroup): + for i in range(workspace.getNumberOfEntries()): + returnList += self.getWorkspacesRecursive(workspace.getItem(i)) + + elif isinstance(workspace, MatrixWorkspace): + returnList.append(workspace) + + else: + raise RuntimeError("Can only merge MatrixWorkspaces, this is " + type(workspace)) + + return returnList + AlgorithmFactory.subscribe(PoldiMerge) diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/PoldiMergeTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/PoldiMergeTest.py index 97fdde8f1946..520a7cb7bf1d 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/PoldiMergeTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/PoldiMergeTest.py @@ -25,6 +25,8 @@ def __init__(self, *args): self.badTimingOffset = CreateWorkspace(rightDataBadOffset, ydata, OutputWorkspace="BadTimingOffset") self.badTimingDelta = CreateWorkspace(rightDataBadDelta, ydata, OutputWorkspace="BadTimingDelta") + self.groupGood = GroupWorkspaces(["Base", "GoodTiming"], OutputWorkspace="GoodGroup") + goodProperty = 10.0 badProperty = 20.0 @@ -56,6 +58,23 @@ def test_happyCase(self): DeleteWorkspace("PoldiMergeOutput") + def test_workspaceGroup(self): + output = self.__runMerge__("GoodGroup") + + self.assertTrue(isinstance(output, MatrixWorkspace)) + + dataX = output.dataX(0) + self.assertEqual(dataX[0], 0.0) + self.assertEqual(dataX[-1], 3.0) + self.assertEqual(len(dataX), 4) + + dataY = output.dataY(0) + self.assertEqual(dataY[0], 2.0) + self.assertEqual(dataY[1], 2.0) + self.assertEqual(len(dataY), 4) + + DeleteWorkspace("PoldiMergeOutput") + def test_timingDelta(self): self.assertRaises(RuntimeError, lambda: self.__runMerge__("Base,BadTimingDelta")) self.assertFalse(AnalysisDataService.doesExist("PoldiMergeOutput"))