Skip to content

Commit

Permalink
Keep old version of algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Jun 17, 2015
1 parent c629332 commit c1c5d44
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#pylint: disable=no-init
from mantid.api import PythonAlgorithm, AlgorithmFactory
from mantid.kernel import StringListValidator, StringMandatoryValidator
from mantid.simpleapi import *
from mantid import config, logger
import os


class ResNorm(PythonAlgorithm):

def category(self):
return "Workflow\\MIDAS;PythonAlgorithms"


def summary(self):
return "This algorithm creates a group 'normalisation' file by taking a resolution file and fitting "+\
"it to all the groups in the resolution (vanadium) data file which has the same "+\
"grouping as the sample data of interest."


def PyInit(self):
self.declareProperty(name='InputType', defaultValue='File',
validator=StringListValidator(['File','Workspace']),
doc='Origin of data input - File (.nxs) or Workspace')
self.declareProperty(name='Instrument', defaultValue='iris',
validator=StringListValidator(['irs','iris','osi','osiris']),
doc='Instrument')
self.declareProperty(name='Analyser', defaultValue='graphite002',
validator=StringListValidator(['graphite002','graphite004']),
doc='Analyser & reflection')
self.declareProperty(name='VanNumber', defaultValue='',
validator=StringMandatoryValidator(),
doc='Sample run number')
self.declareProperty(name='ResInputType',defaultValue='File',
validator=StringListValidator(['File','Workspace']),
doc='Origin of res input - File (_res.nxs) or Workspace')
self.declareProperty(name='ResNumber', defaultValue='',
validator=StringMandatoryValidator(),
doc='Resolution run number')
self.declareProperty(name='EnergyMin', defaultValue=-0.2,
doc='Minimum energy for fit. Default=-0.2')
self.declareProperty(name='EnergyMax', defaultValue=0.2,
doc='Maximum energy for fit. Default=0.2')
self.declareProperty(name='VanBinning', defaultValue=1,
doc='Binning value (integer) for sample. Default=1')
self.declareProperty(name='Plot', defaultValue='None',
validator=StringListValidator(['None','Intensity','Stretch','Fit','All']),
doc='Plot options')
self.declareProperty(name='Save', defaultValue=False,
doc='Switch Save result to nxs file Off/On')


def PyExec(self):
from IndirectImport import run_f2py_compatibility_test, is_supported_f2py_platform

if is_supported_f2py_platform():
import IndirectBayes as Main

run_f2py_compatibility_test()

self.log().information('ResNorm input')
inType = self.getPropertyValue('InputType')
prefix = self.getPropertyValue('Instrument')
ana = self.getPropertyValue('Analyser')
van = self.getPropertyValue('VanNumber')
rinType = self.getPropertyValue('ResInputType')
res = self.getPropertyValue('ResNumber')
emin = self.getPropertyValue('EnergyMin')
emax = self.getPropertyValue('EnergyMax')
nbin = self.getPropertyValue('VanBinning')

vname = prefix+van+'_'+ana+ '_red'
rname = prefix+res+'_'+ana+ '_res'
erange = [float(emin), float(emax)]
plotOp = self.getPropertyValue('Plot')
saveOp = self.getProperty('Save').value

workdir = config['defaultsave.directory']
if inType == 'File':
vpath = os.path.join(workdir, vname+'.nxs') # path name for van nxs file
LoadNexusProcessed(Filename=vpath, OutputWorkspace=vname)
Vmessage = 'Vanadium from File : '+vpath
else:
Vmessage = 'Vanadium from Workspace : '+vname
if rinType == 'File':
rpath = os.path.join(workdir, rname+'.nxs') # path name for res nxs file
LoadNexusProcessed(Filename=rpath, OutputWorkspace=rname)
Rmessage = 'Resolution from File : '+rpath
else:
Rmessage = 'Resolution from Workspace : '+rname
logger.information(Vmessage)
logger.information(Rmessage)
Main.ResNormRun(vname,rname,erange,nbin,True,plotOp,saveOp)


# Register algorithm with Mantid
AlgorithmFactory.subscribe(ResNorm)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def summary(self):
and fitting it to all the groups in the resolution (vanadium)
reduction."""

def version(self):
return 2


def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty('ResolutionWorkspace', '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def test_basic(self):
Tests a basic run of ResNorm.
"""
result = ResNorm(ResolutionWorkspace=self._res_ws,
VanadiumWorkspace=self._van_ws)
VanadiumWorkspace=self._van_ws,
Version=2)
self._validate_result(result)


Expand All @@ -53,7 +54,8 @@ def test_with_limits(self):
result = ResNorm(ResolutionWorkspace=self._res_ws,
VanadiumWorkspace=self._van_ws,
EnergyMin=-0.1,
EnergyMax=0.1)
EnergyMax=0.1,
Version=2)
self._validate_result(result)


Expand All @@ -65,7 +67,8 @@ def test_with_bad_limits(self):
ResolutionWorkspace=self._res_ws,
VanadiumWorkspace=self._van_ws,
EnergyMin=0.1,
EnergyMax=-0.1)
EnergyMax=-0.1,
Version=2)


if __name__=="__main__":
Expand Down
80 changes: 37 additions & 43 deletions Code/Mantid/docs/source/algorithms/ResNorm-v1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,48 @@
Description
-----------

TODO
The routine varies the width of the resolution file to give a 'stretch
factor' and the area provides an intensity normalisation factor. The
fitted parameters are in the group workspace with suffix \_ResNorm with
additional suffices of Intensity & Stretch. The fitted data are in the
workspace ending in \_ResNorm\_Fit.

This routine was originally part of the MODES package.

Usage
-----

**Example - a basic example using ResNorm.**

.. testcode:: ExIRISResNorm

def createSampleWorkspace(name, num_spec=10, random=False):
"""
Creates a sample workspace with a single lorentzian that looks like IRIS data
"""
import os

# Create sample data
function = "name=Lorentzian,Amplitude=8,PeakCentre=5,FWHM=0.7"
ws = CreateSampleWorkspace("Histogram",
Function="User Defined",
UserDefinedFunction=function,
XUnit="DeltaE",
Random=random,
XMin=0,
XMax=10,
BinWidth=0.01)

# Reduce number of spectra
ws = CropWorkspace(ws,
StartWorkspaceIndex=0,
EndWorkspaceIndex=num_spec-1)

ws = ScaleX(ws, -5, "Add")
ws = ScaleX(ws, 0.1, "Multiply")

# Load instrument and instrument parameters
LoadInstrument(ws, InstrumentName='IRIS')
path = os.path.join(config['instrumentDefinition.directory'], 'IRIS_graphite_002_Parameters.xml')
LoadParameterFile(ws, Filename=path)
ws = RenameWorkspace(ws, OutputWorkspace=name)

return ws


van = createSampleWorkspace("irs26173_graphite002_red", random=True)
res = createSampleWorkspace("irs26173_graphite002_res", num_spec=1)

res_norm = ResNorm(ResolutionWorkspace=res,
VanadiumWorkspace=van)
.. code-block:: python
def createSampleWorkspace(name, random=False):
""" Creates a sample workspace with a single lorentzian that looks like IRIS data"""
import os
function = "name=Lorentzian,Amplitude=8,PeakCentre=5,FWHM=0.7"
ws = CreateSampleWorkspace("Histogram", Function="User Defined", UserDefinedFunction=function, XUnit="DeltaE", Random=True, XMin=0, XMax=10, BinWidth=0.01)
ws = CropWorkspace(ws, StartWorkspaceIndex=0, EndWorkspaceIndex=9)
ws = ScaleX(ws, -5, "Add")
ws = ScaleX(ws, 0.1, "Multiply")
#load instrument and instrument parameters
LoadInstrument(ws, InstrumentName='IRIS')
path = os.path.join(config['instrumentDefinition.directory'], 'IRIS_graphite_002_Parameters.xml')
LoadParameterFile(ws, Filename=path)
ws = RenameWorkspace(ws, OutputWorkspace=name)
return ws
ws = createSampleWorkspace("irs26173_graphite002_red", random=True)
res = createSampleWorkspace("irs26173_graphite002_res")
ResNorm(VanNumber='26176',
ResNumber='26173',
InputType='Workspace',
ResInputType='Workspace',
Instrument='irs',
Analyser='graphite002',
Plot='None',
Version=1)
.. categories::
68 changes: 68 additions & 0 deletions Code/Mantid/docs/source/algorithms/ResNorm-v2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.. algorithm::

.. summary::

.. alias::

.. properties::

Description
-----------

This algorithm fits the variation of the width of the resolution file to give a
stretch factor and fits the peak intensities by normalising the peak area to
unity and performing a linear fit with the vanadium.

The outpout workspace is a WorkspaceGroup containing two MatrixWorkspaces named
*_Intensity* and *_Stretch* with the fitted parameters.

Usage
-----

**Example - a basic example using ResNorm.**

.. testcode:: ExIRISResNorm

def createSampleWorkspace(name, num_spec=10, random=False):
"""
Creates a sample workspace with a single lorentzian that looks like IRIS data
"""
import os

# Create sample data
function = "name=Lorentzian,Amplitude=8,PeakCentre=5,FWHM=0.7"
ws = CreateSampleWorkspace("Histogram",
Function="User Defined",
UserDefinedFunction=function,
XUnit="DeltaE",
Random=random,
XMin=0,
XMax=10,
BinWidth=0.01)

# Reduce number of spectra
ws = CropWorkspace(ws,
StartWorkspaceIndex=0,
EndWorkspaceIndex=num_spec-1)

ws = ScaleX(ws, -5, "Add")
ws = ScaleX(ws, 0.1, "Multiply")

# Load instrument and instrument parameters
LoadInstrument(ws, InstrumentName='IRIS')
path = os.path.join(config['instrumentDefinition.directory'], 'IRIS_graphite_002_Parameters.xml')
LoadParameterFile(ws, Filename=path)
ws = RenameWorkspace(ws, OutputWorkspace=name)

return ws


van = createSampleWorkspace("irs26173_graphite002_red", random=True)
res = createSampleWorkspace("irs26173_graphite002_res", num_spec=1)

res_norm = ResNorm(ResolutionWorkspace=res,
VanadiumWorkspace=van,
Version=2)


.. categories::

0 comments on commit c1c5d44

Please sign in to comment.