-
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.
Re #12541
- Loading branch information
Showing
5 changed files
with
211 additions
and
46 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
Code/Mantid/Framework/PythonInterface/plugins/algorithms/ResNorm.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,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) |
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
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
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
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,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:: |