Skip to content

Commit

Permalink
add option to generate table workspace, re #10967
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeMPouzols committed May 28, 2015
1 parent 6979e1d commit 1b1f0cf
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pylint: disable=no-init,invalid-name
from mantid.kernel import *
from mantid.api import *
from mantid.simpleapi import CreateEmptyTableWorkspace


class EnginXCalibrate(PythonAlgorithm):
Expand All @@ -26,6 +27,12 @@ def PyInit(self):
Direction.Input, PropertyMode.Optional),\
"Calibrated detector positions. If not specified, default ones are used.")

self.declareProperty('OutputParametersTableName', '', direction=Direction.Input,
doc = 'Name for a table workspace with the calibration parameters calculated by '
'from this algorithm: difc and zero parameters for GSAS. At the moment '
'these two parameters are added as two columns in a single row. If not given, no'
'table is generated.')

self.declareProperty("Difc", 0.0, direction = Direction.Output,\
doc = "Calibrated Difc value for the bank")

Expand All @@ -36,16 +43,33 @@ def PyExec(self):

ws = self._focusRun()

difc, zero = self._fitParams(ws)

self._produceOutputs(difc, zero)

def _fitParams(self, focusedWS):
"""
Fit the GSAS parameters that this algorithm produces: difc and zero
@param focusedWS: focused workspace to do the fitting on
@returns a pair of parameters: difc and zero
"""
fitPeaksAlg = self.createChildAlgorithm('EnginXFitPeaks')
fitPeaksAlg.setProperty('InputWorkspace', ws)
fitPeaksAlg.setProperty('InputWorkspace', focusedWS)
fitPeaksAlg.setProperty('WorkspaceIndex', 0) # There should be only one index anyway
fitPeaksAlg.setProperty('ExpectedPeaks', self.getProperty('ExpectedPeaks').value)
fitPeaksAlg.execute()

self.setProperty('Difc', fitPeaksAlg.getProperty('Difc').value)
self.setProperty('Zero', fitPeaksAlg.getProperty('Zero').value)
difc = fitPeaksAlg.getProperty('Difc').value
zero = fitPeaksAlg.getProperty('Zero').value

return difc, zero

def _focusRun(self):
"""
Focuses the input workspace by running EnginXFocus which will produce a single spectrum workspace.
"""
alg = self.createChildAlgorithm('EnginXFocus')
alg.setProperty('Filename', self.getProperty('Filename').value)
alg.setProperty('Bank', self.getProperty('Bank').value)
Expand All @@ -58,5 +82,32 @@ def _focusRun(self):

return alg.getProperty('OutputWorkspace').value

def _produceOutputs(self, difc, zero):
"""
Just fills in the output properties as requested
@param difc :: the difc GSAS parameter as fitted here
@param zero :: the zero GSAS parameter as fitted here
"""
self.setProperty('Difc', difc)
self.setProperty('Zero', zero)

# make output table if requested
tblName = self.getPropertyValue("OutputParametersTableName")
if '' != tblName:
self._generateOutputParTable(tblName)

def _generateOutputParTable(self, name):
"""
Produces a table workspace with the two calibration parameters
@param name :: the name to use for the table workspace that is created here
"""
tbl = CreateEmptyTableWorkspace(OutputWorkspace=name)
tbl.addColumn('double', 'difc')
tbl.addColumn('double', 'zero')
tbl.addRow([float(self.getPropertyValue('difc')), float(self.getPropertyValue('zero'))])

self.log().information("Output parameters added into a table workspace: %s" % name)


AlgorithmFactory.subscribe(EnginXCalibrate)
38 changes: 31 additions & 7 deletions Code/Mantid/docs/source/algorithms/EnginXCalibrate-v1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,23 @@ Description
removed without a notification, should instrument scientists decide to do so.


Utilises :ref:`algm-EnginXFocus` which performs a TOF to dSpacing conversion using calibrated pixel positions, focuses the values in dSpacing and then converts them back to TOF.
Then calls :ref:`algm-EnginXFitPeaks` which through a sequence of peak fits determines a linear relationship between dSpacing and measured TOF values in terms of DIFC and ZERO values and provides the these parameters to the Calibrate algorithm.
This algorithm provides an indirect calibration of the sample position, that is, a calibration returned in terms of Difc and Zero rather than an actual new sample position (hence the reason for 'indirect').

Utilises :ref:`algm-EnginXFocus` which performs a TOF to dSpacing
conversion using calibrated pixel positions, focuses the values in
dSpacing and then converts them back to TOF.
Then calls :ref:`algm-EnginXFitPeaks` which through a sequence of peak
fits determines a linear relationship between dSpacing and measured
TOF values in terms of DIFC and ZERO values and provides the these
parameters to the Calibrate algorithm.

This algorithm provides an indirect calibration of the sample
position, that is, a calibration returned in terms of Difc and Zero
rather than an actual new sample position (hence the reason for
'indirect').

The parameters DIFC and ZERO are returned and can be retrieved as
output properties as well. If a name is given in
OutputParametersTableName the algorithm also produces a table
workspace with that name, containing the two output parameters.

.. categories::

Expand All @@ -29,17 +42,28 @@ Usage

**Example - Calculate Difc and Zero:**

.. testcode:: Ex
.. testcode:: ExampleCalib

out_tbl_name = 'out_params'
Difc, Zero = EnginXCalibrate(Filename="ENGINX00213855.nxs",
ExpectedPeaks=[1.097, 2.1], Bank=1)
ExpectedPeaks=[1.097, 2.1], Bank=1,
OutputParametersTableName=out_tbl_name)

print "Difc: %.2f" % (Difc)
print "Zero: %.2f" % (Zero)
tbl = mtd[out_tbl_name]
print "The output table has %d row(s)" % tbl.rowCount()
print "Parameters from the table, Difc: %.2f, Zero: %.2f" % (tbl.cell(0,0), tbl.cell(0,1))

.. testcleanup:: ExampleCalib

DeleteWorkspace(out_tbl_name)

Output:

.. testoutput:: Ex
.. testoutput:: ExampleCalib

Difc: 18404.93
Zero: -10.89
The output table has 1 row(s)
Parameters from the table, Difc: 18404.93, Zero: -10.89

0 comments on commit 1b1f0cf

Please sign in to comment.