Skip to content

Commit

Permalink
refs #8042. Check combine two WS.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Oct 7, 2013
1 parent db43a6f commit b56c3b7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def PyInit(self):

self.declareProperty(name="StartOverlap", defaultValue=-1.0, validator=overlap_validator, doc="Overlap in Q.")
self.declareProperty(name="EndOverlap", defaultValue=-1.0, validator=overlap_validator, doc="End overlap in Q.")
self.declareProperty(name="Params", defaultValue="0.1", doc="Rebinning Parameters. See Rebin for format.")
self.declareProperty(FloatArrayProperty(name="Params", values=[0.1]), doc="Rebinning Parameters. See Rebin for format.")
self.declareProperty(name="ScaleRHSWorkspace", defaultValue=True, doc="Scaling either with respect to workspace 1 or workspace 2.")
self.declareProperty(name="UseManualScaleFactor", defaultValue=False, doc="True to use a provided value for the scale factor.")
self.declareProperty(name="ManualScaleFactor", defaultValue=1.0, doc="Provided value for the scale factor.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
Stitches single histogram [[MatrixWorkspace|Matrix Workspaces]] together outputing a stitched Matrix Workspace. This algorithm is a wrapper over [[Stitch1DMD]].
*WIKI*"""
from mantid.simpleapi import *
#from mantid.simpleapi import *

from mantid.simpleapi import *
from mantid.api import *
from mantid.kernel import *
import numpy as np

class Stitch1D(PythonAlgorithm):
class Stitch1DMany(PythonAlgorithm):

def category(self):
return "Reflectometry\\ISIS;PythonAlgorithms"
Expand All @@ -18,14 +19,17 @@ def name(self):
return "Stitch1D"

def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty("InputWorkspaces", "", Direction.Input), "Input workspaces")

input_validator = StringMandatoryValidator()

self.declareProperty(name="InputWorkspaces", defaultValue="", direction=Direction.Input, validator=input_validator, doc="Input workspaces")
self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", "", Direction.Output), "Output stitched workspace")

overlap_validator = FloatMandatoryValidator()
overlap_validator = FloatArrayMandatoryValidator()

self.declareProperty(name="StartOverlaps", defaultValue=-1.0, validator=overlap_validator, doc="Overlap in Q.")
self.declareProperty(name="EndOverlaps", defaultValue=-1.0, validator=overlap_validator, doc="End overlap in Q.")
self.declareProperty(name="Params", defaultValue="0.1", doc="Rebinning Parameters. See Rebin for format.")
self.declareProperty(FloatArrayProperty(name="StartOverlaps", values=[], validator=overlap_validator), doc="Overlap in Q.")
self.declareProperty(FloatArrayProperty(name="EndOverlaps", values=[], validator=overlap_validator), doc="End overlap in Q.")
self.declareProperty(FloatArrayProperty(name="Params", values=[0.1]), doc="Rebinning Parameters. See Rebin for format.")
self.declareProperty(name="ScaleRHSWorkspace", defaultValue=True, doc="Scaling either with respect to workspace 1 or workspace 2.")
self.declareProperty(name="UseManualScaleFactor", defaultValue=False, doc="True to use a provided value for the scale factor.")
self.declareProperty(name="ManualScaleFactor", defaultValue=1.0, doc="Provided value for the scale factor.")
Expand All @@ -38,33 +42,37 @@ def has_non_zero_errors(self, ws):

def PyExec(self):

inputWorkspaces = self.getProperty("InputWorkspaces")
inputWorkspaces = self.getProperty("InputWorkspaces").value
# Just forward the other properties on.
startOverlaps = self.getProperty('StartOverlaps').value
endOverlaps = self.getProperty('EndOverlaps').value
scaleRHSWorkspace = self.getProperty('ScaleRHSWorkspace').value
useManualScaleFactor = self.getProperty('UseManualScaleFactor').value
manualScaleFactor = self.getProperty('ManualScaleFactor').value
outScaleFactor = self.getProperty('OutScaleFactor').value
params = self.getProperty("Params").value

numberOfWorkspaces = len(inputWorkspaces)
inputWorkspaces = inputWorkspaces.split(',')
startOverlaps = startOverlaps.split(',')
endOverlaps = endOverlaps.split(',')
numberOfWorkspaces = len(inputWorkspaces)
if not numberOfWorkspaces > 1:
raise ValueError("Too few workspaces to stitch")
if not (len(startOverlaps) == len(endOverlaps)):
raise ValueError("StartOverlaps and EndOverlaps are different lengths")
if not (len(startOverlaps) == (numberOfWorkspaces- 1)):
raise ValueError("Wrong number of StartOverlaps, should be %i" % (numberOfWorkspaces - 1))
raise ValueError("Wrong number of StartOverlaps, should be %i not %i" % (numberOfWorkspaces - 1, startOverlaps))

print "INPUT WORKSPACES ", inputWorkspaces
print "START OVERLAPS ", startOverlaps
lhsWS = mtd[inputWorkspaces[0]]
for i in range(1, numberOfWorkspaces):
rhsWS = mtd[inputWorkspaces[i].strip()]
lhsWS, scaleFactor = Stitch1D(LHSWorkspace=lhsWS, RHSWorkspace=rhsWS, StartOverlap=startOverlaps[i-1], EndOverlap=endOverlaps[i-1], Params=params, ScaleRHSWorkspace=scaleRHSWorkspace, UseManualScaleFactor=useManualScaleFactor, ManualScaleFactor=manualScaleFactor)

self.setProperty('OutputWorkspace', result)
self.setProperty('OutScaleFactor', scalefactor)
self.setProperty('OutputWorkspace', lhsWS)
self.setProperty('OutScaleFactor', scaleFactor)

return None


#############################################################################################

AlgorithmFactory.subscribe(Stitch1D())
AlgorithmFactory.subscribe(Stitch1DMany)
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ def setUp(self):
e.fill(0)
self.e = e
self.x = x
a = CreateWorkspace(UnitX="1/q", DataX=x, DataY=[0,0,0,3,3,3,3,3,3,3], NSpec=1, DataE=e)
b = CreateWorkspace(UnitX="1/q", DataX=x, DataY=[2,2,2,2,2,2,2,0,0,0], NSpec=1, DataE=e)
c = CreateWorkspace(UnitX="1/q", DataX=x, DataY=[2,2,2,2,2,2,2,0,0,0], NSpec=1, DataE=e)
a = CreateWorkspace(UnitX="1/q", DataX=x, DataY=[0.0,0.0,0.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0], NSpec=1, DataE=e)
b = CreateWorkspace(UnitX="1/q", DataX=x, DataY=[2.0,2.0,2.0,2.0,2.0,2.0,2.0,0.0,0.0,0.0], NSpec=1, DataE=e)
c = CreateWorkspace(UnitX="1/q", DataX=x, DataY=[2.0,2.0,2.0,2.0,2.0,2.0,2.0,0.0,0.0,0.0], NSpec=1, DataE=e)
self.a = a
self.b = b
self.c = c



def tearDown(self):
# Cleanup
DeleteWorkspace(self.a)
Expand All @@ -35,10 +33,41 @@ def tearDown(self):

def test_stitch_throws_with_too_few_workspaces(self):
try:
stitched = Stitch1DMany(InputWorkspaces='a', StartOverlaps=[-0.5], EndOverlaps=[0.5], Params='0.1')
stitched = Stitch1DMany(InputWorkspaces='a', StartOverlaps=[-0.5], EndOverlaps=[0.5], Params=[0.1])
self.fail("Only one workspace. Should have thrown.")
except ValueError:
except RuntimeError:
pass

def test_stitch_throws_with_wrong_number_of_Start_overlaps(self):
try:
stitched = Stitch1DMany(InputWorkspaces='a, b', StartOverlaps=[-0.5, -0.6], EndOverlaps=[0.5], Params=[0.1])
self.fail("Two start overlaps, but only two workspaces. Should have thrown.")
except RuntimeError:
pass

def test_stitch_throws_with_wrong_number_of_End_overlaps(self):
try:
stitched = Stitch1DMany(InputWorkspaces='a, b', StartOverlaps=[-0.5], EndOverlaps=[0.5, 0.6], Params=[0.1])
self.fail("Two end overlaps, but only two workspaces. Should have thrown.")
except RuntimeError:
pass

'''
Cross-check that the result of using Stitch1DMany with two workspaces is the same as using Stitch1D.
'''
def test_stitches_two(self):
stitchedViaStitchMany, scaleFactorMany = Stitch1DMany(InputWorkspaces='a, b', StartOverlaps=[-0.4], EndOverlaps=[0.4], Params=[0.2])
stitchedViaStitchTwo, scaleFactorTwo = Stitch1D(LHSWorkspace=self.a, RHSWorkspace=self.b, StartOverlap=-0.4, EndOverlap=0.4, Params=[0.2])
self.assertEquals(scaleFactorTwo, scaleFactorMany)

yDataRounded = [ round(elem, 4) for elem in stitchedViaStitchMany.readY(0) ]
expectedYData = [0,0,0,3,3,3,3,0,0,0]
same = all([(x == y) for x,y in zip(yDataRounded, expectedYData)])
self.assertTrue(same)

# Do cross compare
isSuccess = CheckWorkspacesMatch(Workspace1=stitchedViaStitchMany, Workspace2=stitchedViaStitchTwo)
self.assertEquals("Success!", isSuccess);

if __name__ == '__main__':
unittest.main()

0 comments on commit b56c3b7

Please sign in to comment.