diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/JumpFit.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/JumpFit.py index bfca95de9b2c..fa1b06eb6874 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/JumpFit.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/JumpFit.py @@ -1,6 +1,6 @@ from mantid.kernel import * from mantid.api import * -import os.path +import os class JumpFit(PythonAlgorithm): @@ -30,47 +30,33 @@ def PyInit(self): self.declareProperty(name='Output', defaultValue='', direction=Direction.InOut, doc='Output name') - self.declareProperty(name='Verbose', defaultValue=False, doc='Output more verbose message to log') - self.declareProperty(name='Plot', defaultValue=False, doc='Plot result workspace') - self.declareProperty(name='Save', defaultValue=False, doc='Save result workspace to nexus file in the default save directory') + self.declareProperty(name='Verbose', defaultValue=False, + doc='Output more verbose message to log') + self.declareProperty(name='Plot', defaultValue=False, + doc='Plot result workspace') + self.declareProperty(name='Save', defaultValue=False, + doc='Save result workspace to nexus file in the default save directory') def PyExec(self): - from mantid.simpleapi import * + from mantid.simpleapi import ExtractSingleSpectrum, Scale, Fit, CopyLogs, AddSampleLog, DeleteWorkspace from mantid import logger, mtd - from IndirectCommon import * - from IndirectImport import import_mantidplot + from IndirectCommon import StartTime, EndTime - in_ws = self.getPropertyValue('InputWorkspace') - out_name = self.getPropertyValue('Output') + self._setup() - jump_function = self.getProperty('Function').value - width = self.getProperty('Width').value - q_min = self.getProperty('QMin').value - q_max = self.getProperty('QMax').value - - verbose = self.getProperty('Verbose').value - plot = self.getProperty('Plot').value - save = self.getProperty('Save').value - - workdir = getDefaultWorkingDirectory() - - StartTime('Jump fit : ' + jump_function + ' ; ') + StartTime('Jump fit : ' + self._jump_function + ' ; ') # Select the width we wish to fit - spectrum_ws = "__" + in_ws - ExtractSingleSpectrum(InputWorkspace=in_ws, OutputWorkspace=spectrum_ws, WorkspaceIndex=width) + spectrum_ws = "__" + self._in_ws + ExtractSingleSpectrum(InputWorkspace=self._in_ws, OutputWorkspace=spectrum_ws, WorkspaceIndex=self._width) # Convert to HWHM Scale(InputWorkspace=spectrum_ws, Factor=0.5, OutputWorkspace=spectrum_ws) - # Crop the workspace between the given ranges - if verbose: - logger.notice('Cropping from Q= ' + str(q_min) + ' to ' + str(q_max)) - - # Give the user some extra infromation if required - if verbose: - in_run = mtd[in_ws].getRun() + if self._verbose: + logger.notice('Cropping from Q= ' + str(self._q_min) + ' to ' + str(self._q_max)) + in_run = mtd[self._in_ws].getRun() try: log = in_run.getLogData('fit_program') if log: @@ -80,13 +66,13 @@ def PyExec(self): # If we couldn't find the fit program, just pass pass - logger.notice('Parameters in ' + in_ws) + logger.notice('Parameters in ' + self._in_ws) - x_data = mtd[in_ws].readX(0) + x_data = mtd[self._in_ws].readX(0) m_max = x_data[-1] # Select fit function to use - if jump_function == 'ChudleyElliot': + if self._jump_function == 'ChudleyElliot': # Chudley-Elliott: HWHM=(1-sin*(Q*L)/(Q*L))/Tau # for Q->0 W=Q^2*L^2/(6*Tau) @@ -94,7 +80,7 @@ def PyExec(self): l_val = 1.5 function = 'name=ChudleyElliot, Tau=' + str(t_val) + ', L=' + str(l_val) - elif jump_function == 'HallRoss': + elif self._jump_function == 'HallRoss': # Hall-Ross: HWHM=(1-exp(-L*Q^2))/Tau # for Q->0 W=A*Q^2*r @@ -102,14 +88,14 @@ def PyExec(self): l_val = 1.5 function = 'name=HallRoss, Tau=' + str(t_val) + ', L=' + str(l_val) - elif jump_function == 'FickDiffusion': + elif self._jump_function == 'FickDiffusion': # Fick: HWHM=D*Q^2 - y_data = mtd[in_ws].readY(0) + y_data = mtd[self._in_ws].readY(0) diff = (y_data[2] - y_data[0]) / ((x_data[2] - x_data[0]) * (x_data[2] - x_data[0])) function = 'name=FickDiffusion, D=' + str(diff) - elif jump_function == 'TeixeiraWater': + elif self._jump_function == 'TeixeiraWater': # Teixeira: HWHM=Q^2*L/((1+Q^2*L)*tau) # for Q->0 W= @@ -118,36 +104,55 @@ def PyExec(self): function = 'name=TeixeiraWater, Tau=' + str(t_val) + ', L=' + str(l_val) # Run fit function - if out_name is "": - out_name = in_ws[:-10] + '_' + jump_function + 'fit' + if self._out_name is "": + self._out_name = self._in_ws[:-10] + '_' + self._jump_function + 'fit' - Fit(Function=function, InputWorkspace=spectrum_ws, CreateOutput=True, Output=out_name, StartX=q_min, EndX=q_max) - fit_workspace = out_name + '_Workspace' + Fit(Function=function, InputWorkspace=spectrum_ws, CreateOutput=True, Output=self._out_name, StartX=self._q_min, EndX=self._q_max) + fit_workspace = self._out_name + '_Workspace' # Populate sample logs - CopyLogs(InputWorkspace=in_ws, OutputWorkspace=fit_workspace) - AddSampleLog(Workspace=fit_workspace, LogName="jump_function", LogType="String", LogText=jump_function) - AddSampleLog(Workspace=fit_workspace, LogName="q_min", LogType="Number", LogText=str(q_min)) - AddSampleLog(Workspace=fit_workspace, LogName="q_max", LogType="Number", LogText=str(q_max)) + CopyLogs(InputWorkspace=self._in_ws, OutputWorkspace=fit_workspace) + AddSampleLog(Workspace=fit_workspace, LogName="jump_function", LogType="String", LogText=self._jump_function) + AddSampleLog(Workspace=fit_workspace, LogName="q_min", LogType="Number", LogText=str(self._q_min)) + AddSampleLog(Workspace=fit_workspace, LogName="q_max", LogType="Number", LogText=str(self._q_max)) - # Process output options - if save: - fit_path = os.path.join(workdir, fit_workspace + '.nxs') - SaveNexusProcessed(InputWorkspace=fit_workspace, Filename=fit_path) + self._process_output(fit_workspace) - if verbose: - logger.notice('Fit file is ' + fit_path) + self.setProperty('Output', self._out_name) - if plot: - mtd_plot = import_mantidplot() - mtd_plot.plotSpectrum(fit_workspace, [0, 1, 2], True) + DeleteWorkspace(Workspace=spectrum_ws) - self.setProperty('Output', out_name) + EndTime('Jump fit : ' + self._jump_function + ' ; ') - DeleteWorkspace(Workspace=spectrum_ws) - EndTime('Jump fit : ' + jump_function + ' ; ') + def _setup(self): + self._in_ws = self.getPropertyValue('InputWorkspace') + self._out_name = self.getPropertyValue('Output') + + self._jump_function = self.getProperty('Function').value + self._width = self.getProperty('Width').value + self._q_min = self.getProperty('QMin').value + self._q_max = self.getProperty('QMax').value + + self._verbose = self.getProperty('Verbose').value + self._plot = self.getProperty('Plot').value + self._save = self.getProperty('Save').value + + def _process_output(self, workspace): + if self._save: + from mantid.simpleapi import SaveNexusProcessed + from IndirectCommon import getDefaultWorkingDirectory + workdir = getDefaultWorkingDirectory() + fit_path = os.path.join(workdir, workspace + '.nxs') + SaveNexusProcessed(InputWorkspace=workspace, Filename=fit_path) + if self._verbose: + logger.notice('Fit file is ' + fit_path) + + if self._plot: + from IndirectImport import import_mantidplot + mtd_plot = import_mantidplot() + mtd_plot.plotSpectrum(workspace, [0, 1, 2], True) # Register algorithm with Mantid AlgorithmFactory.subscribe(JumpFit)