From bf80fa0ce81733b929af4f7d303c1e455f2832e3 Mon Sep 17 00:00:00 2001 From: Samuel Jackson Date: Mon, 14 Apr 2014 11:15:37 +0100 Subject: [PATCH] Refs #7860 Modify algorithm to just take a workspace as input. Also do some light refactoring at the same time. --- .../WorkflowAlgorithms/Symmetrise.py | 164 +++++++++--------- 1 file changed, 85 insertions(+), 79 deletions(-) diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/Symmetrise.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/Symmetrise.py index 29d93d5701bd..902d58eed6d4 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/Symmetrise.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/Symmetrise.py @@ -1,13 +1,13 @@ """*WIKI* Symmetrise takes an asymmetric S(Q,w) - i.e. one in which the moduli of xmin & xmax are different. Typically xmax is > mod(xmin). -A negative value of x is chosen (Xcut) so that the curve for mod(Xcut) to xmax is reflected and inserted for x less than the Xcut. +A negative value of x is chosen (XCut) so that the curve for mod(XCut) to xmax is reflected and inserted for x less than the XCut. *WIKI*""" from mantid import config, logger, mtd -from mantid.api import PythonAlgorithm, AlgorithmFactory -from mantid.kernel import StringListValidator, StringMandatoryValidator +from mantid.api import PythonAlgorithm, AlgorithmFactory, WorkspaceProperty +from mantid.kernel import StringListValidator, StringMandatoryValidator, Direction from mantid.simpleapi import * import sys, platform, math, os.path, numpy as np @@ -17,126 +17,132 @@ def category(self): return "Workflow\\MIDAS;PythonAlgorithms" def PyInit(self): - self.setOptionalMessage("Takes and asymmetric S(Q,w) and makes it symmetric") - self.setWikiSummary("Takes and asymmetric S(Q,w) and makes it symmetric") - - self.declareProperty(name='InputType',defaultValue='File',validator=StringListValidator(['File','Workspace']), doc='Origin of data input - File (_red.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='SamNumber',defaultValue='',validator=StringMandatoryValidator(), doc='Sample run number') - self.declareProperty(name='Xcut',defaultValue='',validator=StringMandatoryValidator(), doc='X cutoff value') - self.declareProperty('Verbose',defaultValue=True, doc='Switch Verbose Off/On') - self.declareProperty('Plot',defaultValue=True, doc='Switch Plot Off/On') - self.declareProperty('Save',defaultValue=False, doc='Switch Save result to nxs file Off/On') + self.setOptionalMessage("Takes an asymmetric S(Q,w) and makes it symmetric") + self.setWikiSummary("Takes an asymmetric S(Q,w) and makes it symmetric") + + self.declareProperty(WorkspaceProperty("Sample", "", Direction.Input), doc='Sample to run with') + self.declareProperty('XCut', 0.0, doc='X cut off value') + + self.declareProperty('Verbose',defaultValue=True, doc='Switch verbose output Off/On') + self.declareProperty('Plot',defaultValue=True, doc='Switch plotting Off/On') + self.declareProperty('Save',defaultValue=False, doc='Switch saving result to nxs file Off/On') + self.declareProperty(WorkspaceProperty("OutputWorkspace", "", Direction.Output), doc='Name to call the output workspace.') + def PyExec(self): + from IndirectCommon import CheckHistZero, StartTime, EndTime, getDefaultWorkingDirectory - self.log().information('Symmetrise') - inType = self.getPropertyValue('InputType') - prefix = self.getPropertyValue('Instrument') - ana = self.getPropertyValue('Analyser') - sn = self.getPropertyValue('SamNumber') - sam = prefix+sn+'_'+ana+'_red' - cut = self.getPropertyValue('Xcut') - cut = float(cut) - - verbOp = self.getProperty('Verbose').value - plotOp = self.getProperty('Plot').value - saveOp = self.getProperty('Save').value + StartTime('Symmetrise') + self._setup() + num_spectra, npt = CheckHistZero(self._sample) - self.SymmStart(inType,sam,cut,verbOp,plotOp,saveOp) + sample_x = mtd[self._sample].readX(0) + if math.fabs(self._x_cut) < 1e-5: + raise ValueError('XCut point is Zero') + + delta_x = sample_x[1]-sample_x[0] + # diff = np.absolute(sample_x - self._x_cut) + # ineg = np.where(diff < delta_x)[0] - def SymmRun(self, sample,cut,Verbose,Plot,Save): - workdir = config['defaultsave.directory'] - symWS = sample[:-3] + 'sym' - hist,npt = CheckHistZero(sample) - Xin = mtd[sample].readX(0) - delx = Xin[1]-Xin[0] - if math.fabs(cut) < 1e-5: - error = 'Cut point is Zero' - logger.notice('ERROR *** ' + error) - sys.exit(error) - for n in range(0,npt): - x = Xin[n]-cut - if math.fabs(x) < delx: + for n in range(npt): + x = sample_x[n]-self._x_cut + if math.fabs(x) < delta_x: ineg = n + if ineg <= 0: error = 'Negative point('+str(ineg)+') < 0' logger.notice('ERROR *** ' + error) sys.exit(error) + if ineg >= npt: error = type + 'Negative point('+str(ineg)+') > '+str(npt) logger.notice('ERROR *** ' + error) sys.exit(error) - for n in range(0,npt): - x = Xin[n]+Xin[ineg] - if math.fabs(x) < delx: + + for n in range(npt): + x = sample_x[n]+sample_x[ineg] + if math.fabs(x) < delta_x: ipos = n + if ipos <= 0: error = 'Positive point('+str(ipos)+') < 0' logger.notice('ERROR *** ' + error) sys.exit(error) + if ipos >= npt: error = type + 'Positive point('+str(ipos)+') > '+str(npt) logger.notice('ERROR *** ' + error) sys.exit(error) + ncut = npt-ipos+1 - if Verbose: + + if self._verbose: logger.notice('No. points = '+str(npt)) - logger.notice('Negative : at i ='+str(ineg)+' ; x = '+str(Xin[ineg])) - logger.notice('Positive : at i ='+str(ipos)+' ; x = '+str(Xin[ipos])) - logger.notice('Copy points = '+str(ncut)) - for m in range(0,hist): - Xin = mtd[sample].readX(m) - Yin = mtd[sample].readY(m) - Ein = mtd[sample].readE(m) + logger.notice('Negative : at i ='+str(ineg)+' ; x = '+str(sample_x[ineg])) + logger.notice('Positive : at i ='+str(ipos)+' ; x = '+str(sample_x[ipos])) + logger.notice('Copy points = '+str(xcut)) + + for m in range(num_spectra): + sample_x = mtd[self._sample].readX(m) + Yin = mtd[self._sample].readY(m) + Ein = mtd[self._sample].readE(m) Xout = [] Yout = [] Eout = [] for n in range(0,ncut): icut = npt-n-1 - Xout.append(-Xin[icut]) + Xout.append(-sample_x[icut]) Yout.append(Yin[icut]) Eout.append(Ein[icut]) for n in range(ncut,npt): - Xout.append(Xin[n]) + Xout.append(sample_x[n]) Yout.append(Yin[n]) Eout.append(Ein[n]) + if m == 0: - CreateWorkspace(OutputWorkspace=symWS, DataX=Xout, DataY=Yout, DataE=Eout, + CreateWorkspace(OutputWorkspace=self._output_workspace, DataX=Xout, DataY=Yout, DataE=Eout, Nspec=1, UnitX='DeltaE') else: CreateWorkspace(OutputWorkspace='__tmp', DataX=Xout, DataY=Yout, DataE=Eout, Nspec=1, UnitX='DeltaE') - ConjoinWorkspaces(InputWorkspace1=symWS, InputWorkspace2='__tmp',CheckOverlapping=False) - if Save: - path = os.path.join(workdir,symWS+'.nxs') - SaveNexusProcessed(InputWorkspace=symWS, Filename=path) - if Verbose: - logger.notice('Output file : ' + path) - if Plot: - self.plotSymm(symWS,sample) - - def SymmStart(self, inType,sname,cut,Verbose,Plot,Save): - from IndirectCommon import CheckHistZero, StartTime, EndTime, getDefaultWorkingDirectory - from IndirectImport import import_mantidplot + ConjoinWorkspaces(InputWorkspace1=self._output_workspace, InputWorkspace2='__tmp',CheckOverlapping=False) - StartTime('Symmetrise') - workdir = config['defaultsave.directory'] - if inType == 'File': - spath = os.path.join(workdir, sname+'.nxs') # path name for sample nxs file - LoadNexusProcessed(FileName=spath, OutputWorkspace=sname) - message = 'Input from File : '+spath - else: - message = 'Input from Workspace : '+sname - if Verbose: - logger.notice(message) - self.SymmRun(sname,cut,Verbose,Plot,Save) + + if self._save: + workdir = getDefaultWorkingDirectory() + file_path = os.path.join(workdir,self._output_workspace+'.nxs') + SaveNexusProcessed(InputWorkspace=self._output_workspace, Filename=file_path) + + if self._verbose: + logger.notice('Output file : ' + file_path) + + if self._plot: + self._plotSymmetrise() + + self.setProperty("OutputWorkspace", self._output_workspace) EndTime('Symmetrise') - def plotSymm(self, sym,sample): + + def _setup(self): + """ + Get the algorithm properties. + """ + self._sample = self.getPropertyValue('Sample') + self._x_cut = self.getProperty('XCut').value + + self._verbose = self.getProperty('Verbose').value + self._plot = self.getProperty('Plot').value + self._save = self.getProperty('Save').value + + self._output_workspace = self.getPropertyValue('OutputWorkspace') + + def _plotSymmetrise(self): + """ + Plot the first spectrum of the input and output workspace together + """ + from IndirectImport import import_mantidplot mp = import_mantidplot() - tot_plot=mp.plotSpectrum([sym,sample],0) + tot_plot = mp.plotSpectrum([self._output_workspace, self._sample],0) AlgorithmFactory.subscribe(Symmetrise) # Register algorithm with Mantid