Skip to content

Commit

Permalink
refs #8042. Fix bug with integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Oct 7, 2013
1 parent b56c3b7 commit f905086
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def has_non_zero_errors(self, ws):
return count > 0

def PyExec(self):
rangeTolerance = 1e-9
# Just forward the other properties on.
startOverlap = self.getProperty('StartOverlap').value
endOverlap = self.getProperty('EndOverlap').value
startOverlap = self.getProperty('StartOverlap').value - rangeTolerance
endOverlap = self.getProperty('EndOverlap').value + rangeTolerance
scaleRHSWorkspace = self.getProperty('ScaleRHSWorkspace').value
useManualScaleFactor = self.getProperty('UseManualScaleFactor').value
manualScaleFactor = self.getProperty('ManualScaleFactor').value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Stitches single histogram [[MatrixWorkspace|Matrix Workspaces]] together outputing a stitched Matrix Workspace. This algorithm is a wrapper over [[Stitch1DMD]].
The algorithm expects pairs of StartOverlaps and EndOverlaps values. The order in which these are provided determines the pairing.
There should be N entries in each of these StartOverlaps and EndOverlaps lists, where N = 1 -(No of workspaces to stitch).
StartOverlaps and EndOverlaps are in the same units as the X-axis for the workspace.
*WIKI*"""
#from mantid.simpleapi import *

Expand Down Expand Up @@ -34,11 +38,9 @@ def PyInit(self):
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.")
self.declareProperty(name="OutScaleFactor", defaultValue=-2.0, direction = Direction.Output, doc="The actual used value for the scaling factor.")

def has_non_zero_errors(self, ws):
errors = ws.extractE()
count = len(errors.nonzero()[0])
return count > 0

def __workspace_from_split_name(self, list_of_names, index):
return mtd[list_of_names[index].strip()]

def PyExec(self):

Expand All @@ -60,16 +62,26 @@ def PyExec(self):
if not (len(startOverlaps) == (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)
scaleFactor = None

self.setProperty('OutputWorkspace', lhsWS)
self.setProperty('OutScaleFactor', scaleFactor)
# Iterate forward through the workspaces
if scaleRHSWorkspace:
lhsWS = self.__workspace_from_split_name(inputWorkspaces, 0)
for i in range(1, numberOfWorkspaces, 1):
rhsWS = self.__workspace_from_split_name(inputWorkspaces, i)
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', lhsWS)
DeleteWorkspace(lhsWS)
# Iterate backwards through the workspaces.
else:
rhsWS = self.__workspace_from_split_name(inputWorkspaces, -1)
for i in range(0, numberOfWorkspaces-1, 1):
lhsWS = self.__workspace_from_split_name(inputWorkspaces, i)
rhsWS, 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', rhsWS)
DeleteWorkspace(rhsWS)

self.setProperty('OutScaleFactor', scaleFactor)
return None


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ def setUp(self):
self.x = x
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)
DeleteWorkspace(self.b)
DeleteWorkspace(self.c)

def test_stitch_throws_with_too_few_workspaces(self):
try:
Expand All @@ -52,6 +49,12 @@ def test_stitch_throws_with_wrong_number_of_End_overlaps(self):
except RuntimeError:
pass


def do_check_ydata(self, expectedYData, targetWS):
yDataRounded = [ round(elem, 4) for elem in targetWS.readY(0) ]
same = all([(x == y) for x,y in zip(yDataRounded, expectedYData)])
self.assertTrue(same)

'''
Cross-check that the result of using Stitch1DMany with two workspaces is the same as using Stitch1D.
'''
Expand All @@ -60,14 +63,37 @@ def test_stitches_two(self):
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)
self.do_check_ydata(expectedYData, stitchedViaStitchMany)

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

DeleteWorkspace(stitchedViaStitchMany)
DeleteWorkspace(stitchedViaStitchTwo)

def test_stitches_three(self):
ws1 = CreateWorkspace(UnitX="1/q", DataX=self.x, DataY=[3.0, 3.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], NSpec=1, DataE=self.e)
ws2 = CreateWorkspace(UnitX="1/q", DataX=self.x, DataY=[0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 0.0], NSpec=1, DataE=self.e)
ws3 = CreateWorkspace(UnitX="1/q", DataX=self.x, DataY=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0], NSpec=1, DataE=self.e)
stitchedViaStitchMany, sf = Stitch1DMany(InputWorkspaces='ws1, ws2, ws3', StartOverlaps=[-0.4,0.2], EndOverlaps=[-0.2,0.4], Params=[0.2])

expectedYData = [3,3,3,3,3,3,3,3,3,3]
self.do_check_ydata(expectedYData, stitchedViaStitchMany)
self.assertEquals(3.0, round(sf, 6))

DeleteWorkspace(ws1)
DeleteWorkspace(ws2)
DeleteWorkspace(ws3)
DeleteWorkspace(stitchedViaStitchMany)

def test_stitches_using_manual_scaling(self):
stitchedViaStitchMany, sf = Stitch1DMany(InputWorkspaces='a, b', StartOverlaps=[-0.4], EndOverlaps=[0.4], Params=[0.2], UseManualScaleFactor=True, ManualScaleFactor=2.0)

self.assertEquals(2.0, round(sf, 6))
DeleteWorkspace(stitchedViaStitchMany)


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

0 comments on commit f905086

Please sign in to comment.