-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a32892a
commit db43a6f
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
Code/Mantid/Framework/PythonInterface/plugins/algorithms/Stitch1DMany.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,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()) |
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
44 changes: 44 additions & 0 deletions
44
Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/Stitch1DManyTest.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,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() |