Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #9436. Adding merge algorithm for POLDI data
- Loading branch information
Showing
5 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
Code/Mantid/Framework/PythonInterface/plugins/algorithms/PoldiMerge.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""*WIKI* | ||
PoldiMerge takes a list of workspace names and adds the counts, resulting in a new workspace. The difference to Plus is that it performs some POLDI-specific tests | ||
that determine whether merging those files is sensible or not. The following requirements have to be fulfilled: | ||
* The time-binning (x-data) of all workspaces must match (offset as well as width of time bins) | ||
* These quantities from the sample log: | ||
** Position of the sample table (x, y and z) | ||
** Rotation speed of the chopper | ||
The algorithm does not perform partial summation - if any of the workspaces does not fulfill the criteria, the intermediate result is discarded. | ||
*WIKI*""" | ||
|
||
from mantid.kernel import StringArrayProperty, Direction | ||
from mantid.simpleapi import * | ||
from mantid.api import * | ||
|
||
import numpy as np | ||
|
||
class PoldiMerge(PythonAlgorithm): | ||
comparedPropertyNames = ["TablePositionX", "TablePositionY", "TablePositionZ", "ChopperSpeed"] | ||
outputWorkspaceName = None | ||
|
||
def category(self): | ||
return "SINQ\\Poldi" | ||
|
||
def name(self): | ||
return "PoldiMerge" | ||
|
||
def PyInit(self): | ||
self.declareProperty(StringArrayProperty(name="WorkspaceNames", | ||
direction=Direction.Input), | ||
doc="List of Workspace names to merge.") | ||
|
||
self.declareProperty(WorkspaceProperty(name="OutputWorkspace", | ||
defaultValue="MergedPoldiWorkspaces", | ||
direction=Direction.Output), | ||
doc="Workspace where all counts from the list workspaces have been added") | ||
|
||
def PyExec(self): | ||
workspaceNames = self.getProperty("WorkspaceNames").value | ||
self.outputWorkspaceName = self.getProperty("OutputWorkspace").valueAsStr | ||
|
||
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 = [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]) | ||
|
||
xdata = workspaces[0].dataX(0) | ||
ydata = np.zeros(len(xdata)) | ||
|
||
for h in range(output.getNumberHistograms()): | ||
output.setX(h, xdata) | ||
output.setY(h, ydata) | ||
|
||
AnalysisDataService.addOrReplace(self.outputWorkspaceName, output) | ||
|
||
while workspaces: | ||
current = workspaces.pop(0) | ||
|
||
try: | ||
if self.canMerge(output, current): | ||
output += current | ||
except RuntimeError as error: | ||
self.handleError(error) | ||
|
||
self.setProperty("OutputWorkspace", output) | ||
|
||
def canMerge(self, leftWorkspace, rightWorkspace): | ||
if not self.timingsMatch(leftWorkspace.dataX(0), rightWorkspace.dataX(0)): | ||
raise RuntimeError("Timings don't match") | ||
|
||
leftRun = leftWorkspace.getRun() | ||
rightRun = rightWorkspace.getRun() | ||
|
||
return self.propertiesMatch(leftRun, rightRun) | ||
|
||
def timingsMatch(self, leftXData, rightXData): | ||
leftDeltaX = leftXData[1] - leftXData[0] | ||
rightDeltaX = rightXData[1] - rightXData[0] | ||
|
||
return abs(leftDeltaX - rightDeltaX) < 1e-4 and abs(rightXData[0] - leftXData[0]) < 1e-4 | ||
|
||
def propertiesMatch(self, leftRun, rightRun): | ||
for propertyName in self.comparedPropertyNames: | ||
if abs(self.getPropertyValue(leftRun.getProperty(propertyName)) - self.getPropertyValue(rightRun.getProperty(propertyName))) > 1e-4: | ||
raise RuntimeError("Property '%s' does not match" % (propertyName)) | ||
|
||
return True | ||
|
||
def getPropertyValue(self, runProperty): | ||
try: | ||
return runProperty.value[0] | ||
except: | ||
return runProperty.value | ||
|
||
def handleError(self, error): | ||
if AnalysisDataService.doesExist(self.outputWorkspaceName): | ||
AnalysisDataService.remove(self.outputWorkspaceName) | ||
|
||
raise RuntimeError("Workspaces can not be merged. %s. Aborting." % (str(error))) | ||
|
||
AlgorithmFactory.subscribe(PoldiMerge) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/PoldiMergeTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import unittest | ||
|
||
from mantid.kernel import * | ||
from mantid.api import * | ||
from mantid.simpleapi import * | ||
|
||
import numpy as np | ||
|
||
class PoldiMergeTest(unittest.TestCase): | ||
def __init__(self, *args): | ||
unittest.TestCase.__init__(self, *args) | ||
properties = ["TablePositionX", "TablePositionY", "TablePositionZ", "ChopperSpeed"] | ||
|
||
leftData = [0.0, 1.0, 2.0, 3.0] | ||
rightDataGood = [0.0, 1.0, 2.0, 3.0] | ||
|
||
rightDataBadOffset = [1.0, 2.0, 3.0, 4.0] | ||
rightDataBadDelta = [0.0, 2.0, 4.0, 6.0] | ||
|
||
ydata = np.ones(len(leftData)) | ||
|
||
self.base = CreateWorkspace(leftData, ydata, OutputWorkspace="Base") | ||
self.goodTiming = CreateWorkspace(rightDataGood, ydata, OutputWorkspace="GoodTiming") | ||
self.goodTimingBadProperties = CreateWorkspace(rightDataGood, ydata, OutputWorkspace="GoodTimingBadProperties") | ||
self.badTimingOffset = CreateWorkspace(rightDataBadOffset, ydata, OutputWorkspace="BadTimingOffset") | ||
self.badTimingDelta = CreateWorkspace(rightDataBadDelta, ydata, OutputWorkspace="BadTimingDelta") | ||
|
||
goodProperty = 10.0 | ||
badProperty = 20.0 | ||
|
||
for p in properties: | ||
self.base.getRun().addProperty(p, goodProperty, True) | ||
self.goodTiming.getRun().addProperty(p, goodProperty, True) | ||
self.badTimingOffset.getRun().addProperty(p, goodProperty, True) | ||
self.badTimingDelta.getRun().addProperty(p, goodProperty, True) | ||
|
||
self.goodTimingBadProperties.getRun().addProperty(p, badProperty, True) | ||
|
||
def __runMerge__(self, workspaceNames): | ||
return PoldiMerge(WorkspaceNames=workspaceNames, OutputWorkspace="PoldiMergeOutput") | ||
|
||
def test_happyCase(self): | ||
output = self.__runMerge__("Base,GoodTiming") | ||
|
||
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")) | ||
|
||
def test_timingOffset(self): | ||
self.assertRaises(RuntimeError, lambda: self.__runMerge__("Base,BadTimingOffset")) | ||
self.assertFalse(AnalysisDataService.doesExist("PoldiMergeOutput")) | ||
|
||
def test_badProperties(self): | ||
self.assertRaises(RuntimeError, lambda: self.__runMerge__("Base,GoodTimingBadProperties")) | ||
self.assertFalse(AnalysisDataService.doesExist("PoldiMergeOutput")) | ||
|
||
def test_badName(self): | ||
self.assertRaises(RuntimeError, lambda: self.__runMerge__("Base,NotExisting")) | ||
self.assertFalse(AnalysisDataService.doesExist("PoldiMergeOutput")) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters