Skip to content

Commit

Permalink
refs #8042. Code templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Oct 4, 2013
1 parent a32892a commit db43a6f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""*WIKI*
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.api import *
from mantid.kernel import *
import numpy as np

class Stitch1D(PythonAlgorithm):

def category(self):
return "Reflectometry\\ISIS;PythonAlgorithms"

def name(self):
return "Stitch1D"

def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty("InputWorkspaces", "", Direction.Input), "Input workspaces")
self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", "", Direction.Output), "Output stitched workspace")

overlap_validator = FloatMandatoryValidator()

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(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.")
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 PyExec(self):

inputWorkspaces = self.getProperty("InputWorkspaces")
# 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(',')
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))

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

return None


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

AlgorithmFactory.subscribe(Stitch1D())
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set ( TEST_PY_FILES
SANSWideAngleCorrectionTest.py
SortXAxisTest.py
Stitch1DTest.py
Stitch1DManyTest.py
SuggestTibCNCSTest.py
SuggestTibHYSPECTest.py
UpdatePeakParameterTableValueTest.py
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest
import numpy
from mantid.simpleapi import *
from mantid.kernel import *
from mantid.api import *

class Stitch1DManyTest(unittest.TestCase):

a = None
b = None
c = None
x = None
e = None

def setUp(self):
x = numpy.arange(-1, 1.2, 0.2)
e = numpy.arange(-1, 1, 0.2)
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)
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:
stitched = Stitch1DMany(InputWorkspaces='a', StartOverlaps=[-0.5], EndOverlaps=[0.5], Params='0.1')
self.fail("Only one workspace. Should have thrown.")
except ValueError:
pass

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

0 comments on commit db43a6f

Please sign in to comment.