OutputCompositeMembers in Fit with Histogram evaluation type - Integration error

I am writing a simple python code to fit two Lorentzians to my data.

I am facing an error in the Fit function where I have set

OutputCompositeMembers=True

and

EvaluationType=“Histogram”

but this returns the following error:


RuntimeError Traceback (most recent call last)
in ()
33 #using the fit function for each workspace i
34 fit_out = Fit(Function=function,InputWorkspace=“sample”,WorkspaceIndex=i,StartX=-0.29, EndX=1.1,
—> 35 CreateOutput=True,Output=“fit_results”,MaxIterations=500,EvaluationType=‘Histogram’,OutputCompositeMembers=True)
36 #setting the parameters to the values
37 chi_sq = fit_out[1]

/opt/Mantid/bin/mantid/simpleapi.pyc in wrapper(*Function, InputWorkspace, ****kwargs)
365 del kwargs[key]
366 set_properties(algm, **kwargs)
→ 367 algm.execute()
368 return _gather_returns(function_name, lhs, algm, inout=inout)
369 # end

RuntimeError: Integration is not implemented for this function.

But if I use CentrePoint evaluation type, this error is not encountered, but the fitting is quite poor.

PS : This is the whole script, highlighted the relevant values in bold

test = True
counter = 100
#starting values for all four parameters
g1[0] = 0.01 #initial value for g1
g2[0] = 0.1 #initial value for g2
a0[0] = 1e-2
a1[0] = 1e-2
for i in range(0,Qvalues):
test = True
while( test ):
function_composition = “(composite=Convolution,FixResolution=true,NumDeriv=true;”#composite function of convolution type
function_composition += “name=TabulatedFunction,Workspace=res,”#resolution workspace
function_composition += “WorkspaceIndex=”+str(i) #using the appropriate Q-valued resolution function.
function_composition += “,Scaling=1,Shift=0,XScaling=1,ties=(Scaling=1,Shift=0,XScaling=1);”
lorentzian_0 = “(name=Lorentzian”
lorentzian_0 += “,Amplitude=”+str(a0[i])
lorentzian_0 += “,PeakCentre=”+str(0.0)
lorentzian_0 += “,FWHM=”+str(g1[i])
ties_l0 = “” #leaving this open to add any constraints to lorentzian 1
constraints_l0 = “”
lorentzian_1 = “;name=Lorentzian”
lorentzian_1 += “,Amplitude=”+str(a1[i])
lorentzian_1 += “,PeakCentre=”+str(0.0)
lorentzian_1 += “,FWHM=”+str(g2[i])
constraints_l1 = “”
ties_l1 = “)” #leaving this open to add any constraints to lorentzian 1
bg_function_string = “);name=LinearBackground,A0=0,A1=0” #background is chosen linear
function = function_composition+lorentzian_0+ties_l0+lorentzian_1+ties_l1+bg_function_string
#print function
#using the fit function for each workspace i
fit_out = Fit(Function=function,InputWorkspace=“sample”,WorkspaceIndex=i,StartX=-0.29, EndX=1.1,
CreateOutput=True,Output=“fit_results”,MaxIterations=500,EvaluationType=‘Histogram’,OutputCompositeMembers=True)
#setting the parameters to the values
chi_sq = fit_out[1]
a0[i] = fit_out[3].row(3)[“Value”]
g1[i] = fit_out[3].row(5)[“Value”]
a1[i] = fit_out[3].row(6)[“Value”]
g2[i] = fit_out[3].row(8)[“Value”]
if (i <7):
a0[i+1], a1[i+1], g1[i+1], g2[i+1] = a0[i],a1[i],g1[i],g2[i]
if ( chi_sq < 3.0):
test = False
#just printing params after successful fitting
print (“Parameters obtained after “+str(i)+“th Q-value fitting”)
print qs[i],a0[i],a1[i],g1[i],g2[i],” Chi-qs: “,chi_sq
#store the fit of functions in sqw_fit for each Q-value
fit_Evalues = fit_out[4].blocksize()
#print fit_Evalues+1,fit_out[4].readX(1).size
energy_fit[:fit_Evalues+1] = fit_out[4].readX(1)
sqw_fit[i,:fit_Evalues] = fit_out[4].readY(1)
print(”\n”)
print(“Fitting done for all Q-values”)

Hi Harish,

Thanks for getting in touch.

The issue here is that histogram integration does not work with a convolved function (composite=Convolution). In fact, even if it did work it would be technically incorrect, without additional functionality.

Adding this functionality could be a valid request for future versions of Mantid, if it is something that is a priority for users. However, I am afraid that there is no ‘quick fix’ that might make the script above work with the histogram integration.

I am sorry that we could not help you immediately on this occasion. If you would like to have the functionality added to future versions of Mantid, please feel free to contact us either on the forum or via email (Mantid-help@mantidproject.org) and make a case for its inclusion.

All the best,

Keith

Hi Keith,

Thanks for the clarification. Although while using the ConvFit (in Indirect Data Analysis) in Mantid GUI this seems to work (or at least according to the selection shown in the GUI).

Check the EvaluationType in following screenshot. I have also set ExtractMembers to True (not seen in the screenshot).

Regards
Harish

Hi Harish,

Curious! It would be good to know how the Indirect Analysis interface is performing this series of tasks.

There are a couple of options to investigate this

  1. If you are happy to share the data files (nxs) that you are using we can re-create the process here and diagnose. Obviously these files will be treated in confidence, you could send them to my email (keith.butler@stfc.ac.uk).

  2. If you would rather not send the data, you could send us the history of the workspace that is created by this procedure. By looking at this we should be able to see how the functions and variables are passed around.

Best,

Keith

Thanks for the response, Keith. I will send the sample and resolution nexus files to your mail ID.

Hi Harish,

Thanks for the information.

It looks like the Indirect Interface ConvFit calls Fit under the hood - with EvaluationType set to CentrePoint by default. However, it may result in a better fit than calling fit alone, as there are other settings.

To reproduce something like the screenshot that you sent, you can do the following:

Load(Filename='/Users/keithbutler/Documents/Mantid/Data/Harish/sample.nxs', OutputWorkspace='sample')
ConvolutionFitSequential(InputWorkspace='sample', Function='name=LinearBackground,A0=0.021,A1=-0.006;(composite=Convolution,FixResolution=true,NumDeriv=true;name=Resolution,Workspace=__ConvFit_Resolution,WorkspaceIndex=0,X=(),Y=();(name=Lorentzian,Amplitude=1,PeakCentre=0.45,FWHM=0.0175;name=Lorentzian,Amplitude=1,PeakCentre=0.45,FWHM=0.0175))', PassWSIndexToFunction=True, BackgroundType='Fit Linear', StartX=-0.30000000000000071, EndX=1.1999999999999995, OutputWorkspace='sample_conv_2LFitL_s0_Result'

Hope this helps,

Keith

Hi Keith,

Thanks for the python code segment. I just had two clarifications,

  1. It seems like Background is being set two times, the first time in the Function string and second time in BackgroundType argument. Should this be done both the times?

  2. How do I read the Output workspace that contains the fitted functions and fit curves? Should I use the Load command again?

Harish

Harish

  1. The Background type input is our user friendly method for putting it into the function. When used it appears in the parameter tree so that the user can modify it if required.
  2. All output workspaces are listed in the workspace window. They can be opened for reading & plotting,etc.

Harish
By the way all the QENS programs were developed by me
Sp

1 Like

Hi Harish,

I’ve worked some more to come up with a script that approximates what you show in your script above. There are several variables that do not appear to be declared anywhere before use in your script, so I have just chosen values to initialise them, you probably want to change these.

As Spencer points out, the parameters will be accessible from table workspaces spawned by the fitting process. These appear in the Workspace dialog in the main Mantid window. More on Python and table workspaces is available here Python ITableWorkspace v2 - Mantid Project

The script that I was running:

test = True
counter = 100

g1 = np.zeros(shape=(10))
g2 = np.zeros(shape=(10))
a0 = np.zeros(shape=(10))
a1 = np.zeros(shape=(10))
Qvalues = 1
Load(Filename='/Users/keithbutler/Documents/Mantid/Data/Harish/sample.nxs', 
OutputWorkspace='sample')
Load(Filename='/Users/keithbutler/Documents/Mantid/Data/Harish/resolution.nxs', 
OutputWorkspace='resolution')
#starting values for all four parameters
g1[0] = 0.01 #initial value for g1
g2[0] = 0.1 #initial value for g2
a0[0] = 1e-2
a1[0] = 1e-2
for i in range(0,Qvalues):

    bg = 'name=LinearBackground, A0=0, A1=0;'
    tab = 'name=TabulatedFunction, Workspace=resolution, WorkspaceIndex=0, Scaling=1, Shift=0, XScaling=1, ties=(Scaling=1,Shift=0,XScaling=1);' 
    compos1 = '(composite=Convolution, FixResolution=true, NumDeriv=true; name=Resolution, Workspace=resolution, WorkspaceIndex=0, X=(), Y=();'
    compos2 = '(name=Lorentzian, Amplitude=1, PeakCentre=0.45, FWHM=0.0175))'

    fx = bg + tab + compos1 + compos2

    ConvolutionFitSequential(InputWorkspace='sample', Function=fx, PassWSIndexToFunction=True, BackgroundType='Fit Linear', StartX=-0.3, EndX=1.2, SpecMax=7, OutputWorkspace='conv_fitting_result')

Note that I do not actually use the values of g1 a1 etc in my setup, but you could put them in quite easily.

All the best,

Keith

1 Like

I am using ipython notebook (not using Mantid GUI) for running my fitting in python using mantid libraries. In this scenario, I was wondering how to access the fitted member functions (not just the fit parameters, which I am able to access through the return value of ConvolutionFitSequential).

Thank you so much for the QENS programs :slight_smile: , am trying to carry out everything from reduction to analysis with Mantid, the reason why I am trying the best I can.

Harish

Thank you, Keith, but I think you have used only one Lorentzian in this script (just to clarify), nevertheless I guess I can modify it to my requirement.

As I already pointed out, I am using ipython notebook for the analysis, therefore can’t see the mantid GUI window. However the parameters of the fit of are returned by ‘ConvolutionFitSequential’ as a workspace, I was wondering how to access the Member functions and the fit itself in the notebook.

Thank you
Harish

Hi Harish,

You are welcome.

Yes, this script should be easily extensible and you can add what functions you like.

More on Python and table workspaces is available here Python ITableWorkspace v2 - Mantid Project This tells you how to access properties of workspaces via Python.

Best,

Keith