Skip to content
Snippets Groups Projects
Commit b657e5cb authored by Marina Ganeva's avatar Marina Ganeva
Browse files

Small changes. Pylint warning fixed.

parent d51717b9
No related branches found
No related tags found
No related merge requests found
...@@ -71,13 +71,13 @@ class ComputeCalibrationCoefVan(PythonAlgorithm): ...@@ -71,13 +71,13 @@ class ComputeCalibrationCoefVan(PythonAlgorithm):
run = self.vanaws.getRun() run = self.vanaws.getRun()
if not run.hasProperty('temperature'): if not run.hasProperty('temperature'):
self.log().warning("Temperature sample log is not present in " + self.vanaws.getName() + self.log().warning("Temperature sample log is not present in " + self.vanaws.getName() +
"T=293K is assumed for Debye-Waller factor.") " T=293K is assumed for Debye-Waller factor.")
return self.defaultT return self.defaultT
try: try:
temperature = float(run.getProperty('temperature').value) temperature = float(run.getProperty('temperature').value)
except ValueError, err: except ValueError, err:
self.log().warning("Error of getting temperature: " + err + self.log().warning("Error of getting temperature: " + err +
"T=293K is assumed for Debye-Waller factor.") " T=293K is assumed for Debye-Waller factor.")
return self.defaultT return self.defaultT
return temperature return temperature
...@@ -103,7 +103,7 @@ class ComputeCalibrationCoefVan(PythonAlgorithm): ...@@ -103,7 +103,7 @@ class ComputeCalibrationCoefVan(PythonAlgorithm):
dataY = self.vanaws.readY(idx) dataY = self.vanaws.readY(idx)
if np.max(dataY) != 0: if np.max(dataY) != 0:
dataE = self.vanaws.readE(idx) dataE = self.vanaws.readE(idx)
peak_centre, sigma = mlzutils.do_fit_gaussian(self.vanaws, idx, self.log(), cleanup_fit=False) peak_centre, sigma = mlzutils.do_fit_gaussian(self.vanaws, idx, self.log())
fwhm = sigma*2.*np.sqrt(2.*np.log(2.)) fwhm = sigma*2.*np.sqrt(2.*np.log(2.))
idxmin = (np.fabs(dataX-peak_centre+3.*fwhm)).argmin() idxmin = (np.fabs(dataX-peak_centre+3.*fwhm)).argmin()
idxmax = (np.fabs(dataX-peak_centre-3.*fwhm)).argmin() idxmax = (np.fabs(dataX-peak_centre-3.*fwhm)).argmin()
...@@ -144,7 +144,8 @@ class ComputeCalibrationCoefVan(PythonAlgorithm): ...@@ -144,7 +144,8 @@ class ComputeCalibrationCoefVan(PythonAlgorithm):
for i in range(nhist): for i in range(nhist):
det = instrument.getDetector(i + detID_offset) det = instrument.getDetector(i + detID_offset)
thetasort[i] = 0.5*self.vanaws.detectorSignedTwoTheta(det) thetasort[i] = 0.5*np.sign(np.cos(det.getPhi()))*self.vanaws.detectorTwoTheta(det)
# thetasort[i] = 0.5*self.vanaws.detectorSignedTwoTheta(det) # gives opposite sign for detectors 0-24
temperature = self.get_temperature() # T in K temperature = self.get_temperature() # T in K
wlength = float(run.getLogData('wavelength').value) # Wavelength, Angstrom wlength = float(run.getLogData('wavelength').value) # Wavelength, Angstrom
......
# pylint: disable=assignment-from-none
import mantid.simpleapi as api import mantid.simpleapi as api
from mantid.api import AlgorithmManager
import numpy as np import numpy as np
...@@ -158,19 +158,12 @@ def compare_mandatory(wslist, plist, logger, tolerance=0.01): ...@@ -158,19 +158,12 @@ def compare_mandatory(wslist, plist, logger, tolerance=0.01):
raise RuntimeError(message) raise RuntimeError(message)
def remove_fit_workspaces(prefix): def do_fit_gaussian(workspace, index, logger):
ws1 = prefix + '_Parameters'
ws2 = prefix + '_NormalisedCovarianceMatrix'
cleanup([ws1, ws2])
def do_fit_gaussian(workspace, index, logger, cleanup_fit=True):
""" """
Calculates guess values on peak centre, sigma and peak height. Calculates guess values on peak centre, sigma and peak height.
Uses them as an input to run a fit algorithm Uses them as an input to run a fit algorithm
@ param workspace --- input workspace @ param workspace --- input workspace
@ param index --- the spectrum with which WorkspaceIndex to fit @ param index --- the spectrum with which WorkspaceIndex to fit
@ param cleanup --- if False, the intermediate workspaces created by Fit algorithm will not be removed
@ returns peak_centre --- fitted peak centre @ returns peak_centre --- fitted peak centre
@ returns sigma --- fitted sigma @ returns sigma --- fitted sigma
""" """
...@@ -206,32 +199,27 @@ def do_fit_gaussian(workspace, index, logger, cleanup_fit=True): ...@@ -206,32 +199,27 @@ def do_fit_gaussian(workspace, index, logger, cleanup_fit=True):
fwhm = np.fabs(x_values[indices[nentries - 1, 0]] - x_values[indices[0, 0]]) fwhm = np.fabs(x_values[indices[nentries - 1, 0]] - x_values[indices[0, 0]])
sigma = fwhm/(2.*np.sqrt(2.*np.log(2.))) sigma = fwhm/(2.*np.sqrt(2.*np.log(2.)))
# execute Fit algorithm # create and execute Fit algorithm
myfunc = 'name=Gaussian, Height='+str(height)+', PeakCentre='+str(try_centre)+', Sigma='+str(sigma) myfunc = 'name=Gaussian, Height='+str(height)+', PeakCentre='+str(try_centre)+', Sigma='+str(sigma)
startX = try_centre - 3.0*fwhm startX = try_centre - 3.0*fwhm
endX = try_centre + 3.0*fwhm endX = try_centre + 3.0*fwhm
prefix = "Fit" + workspace.getName() + str(index) prefix = "Fit" + workspace.getName() + str(index)
retvals = api.Fit(InputWorkspace=workspace.getName(), WorkspaceIndex=index, StartX=startX, EndX=endX, fit_alg = AlgorithmManager.createUnmanaged('Fit')
Function=myfunc, Output=prefix, OutputParametersOnly=True) fit_alg.initialize()
if not retvals or len(retvals) < 4: fit_alg.setChild(True)
api.set_properties(fit_alg, Function=myfunc, InputWorkspace=workspace, CreateOutput=True, Output=prefix)
fit_alg.setProperty('StartX', startX)
fit_alg.setProperty('EndX', endX)
fit_alg.setProperty('WorkspaceIndex', index)
fit_successful = fit_alg.execute()
param_table = fit_alg.getProperty('OutputParameters').value
if not fit_successful:
message = "For detector " + str(index) + " in workspace " + workspace.getName() +\ message = "For detector " + str(index) + " in workspace " + workspace.getName() +\
" failed to retrieve fit results. Input guess parameters are " + str(myfunc) "fit was not successful. Input guess parameters are " + str(myfunc)
logger.error(message) logger.error(message)
if cleanup_fit:
remove_fit_workspaces(prefix)
raise RuntimeError(message) raise RuntimeError(message)
fitStatus = retvals[0] result = param_table.column(1)[1:3]
paramTable = retvals[3]
if fitStatus != 'success':
message = "For detector " + str(index) + " in workspace " + workspace.getName() +\
"fit has been terminated with status " + fitStatus + ". Input guess parameters are " + str(myfunc)
logger.error(message)
if cleanup_fit:
remove_fit_workspaces(prefix)
raise RuntimeError(message)
result = paramTable.column(1)[1:3]
if cleanup_fit:
remove_fit_workspaces(prefix)
# return list: [peak_centre, sigma] # return list: [peak_centre, sigma]
return result return result
...@@ -65,10 +65,10 @@ class ComputeCalibrationCoefVanTest(unittest.TestCase): ...@@ -65,10 +65,10 @@ class ComputeCalibrationCoefVanTest(unittest.TestCase):
# check dwf calculation # check dwf calculation
y_sum = sum(self._input_ws.readY(1)[27:75]) y_sum = sum(self._input_ws.readY(1)[27:75])
det2 = self._input_ws.getInstrument().getDetector(2) # det2 = self._input_ws.getInstrument().getDetector(2)
mvan = 0.001*50.942/N_A mvan = 0.001*50.942/N_A
Bcoef = 4.736767162094296*1e+20*hbar*hbar/(2.0*mvan*k*389.0) Bcoef = 4.736767162094296*1e+20*hbar*hbar/(2.0*mvan*k*389.0)
dwf = np.exp(-1.0*Bcoef*(4.0*np.pi*np.sin(0.5*self._input_ws.detectorSignedTwoTheta(det2))/4.0)**2) dwf = np.exp(-1.0*Bcoef*(4.0*np.pi*np.sin(0.5*np.radians(15.0))/4.0)**2)
self.assertAlmostEqual(y_sum*dwf, wsoutput.readY(1)[0]) self.assertAlmostEqual(y_sum*dwf, wsoutput.readY(1)[0])
DeleteWorkspace(wsoutput) DeleteWorkspace(wsoutput)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment