Skip to content

Commit

Permalink
Refs #9711. Refactored PoldiMerge to use MergeRuns
Browse files Browse the repository at this point in the history
Consequently, WorkspaceGroups can be handled now as well.
  • Loading branch information
Michael Wedel committed Aug 7, 2014
1 parent e9cc7a9 commit 4f837f2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
Expand Up @@ -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),
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Up @@ -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

Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit 4f837f2

Please sign in to comment.