Hello All,
First of all, I really like this library, which I am using to fit synchrotron X-ray Emission spectra.
I am trying to use the library to fit multiple species, where each species is adequately represented by a double pseudo-voigt peak (kalpha1 and kalpha2 fluorescence). I managed to fit a single species by a CompositeModel generated according to the documentation, but now I would like to use the fit parameters and the composite model to fit multiple species in a linear combination, but I am having trouble making lmfit work. namely, it is complaining when I try to read a composite model in a function and then add two composite models together.
here below the functions I defined:
def pseudo_voigt_doublet(x, data, comp=1, cal_shift=17.5, **kwargs):
pv1 = PseudoVoigtModel(prefix=f'comp{comp}_ka1_')
pars = pv1.guess(data, x=x)
pv2 = PseudoVoigtModel(prefix=f'comp{comp}_ka2_')
pars.update(pv2.make_params())
# change the center to be shifted to the right by (self.e_diff*pix_cal/2)
pars.add(f'comp{comp}_shift', value=cal_shift, min=12, max=25)
pars[f'comp{comp}_ka2_center'].set(
expr=f'comp{comp}_ka1_center - comp{comp}_shift')
pars.add(f'comp{comp}_amp_ratio', value=2, min=1.7, max=2.3)
pars[f'comp{comp}_ka2_amplitude'].set(expr=f'comp{comp}_ka1_amplitude / comp{comp}_amp_ratio')
pars[f'comp{comp}_ka2_sigma'].set(value=pars[f'comp{comp}_ka1_sigma'], vary=False)
if 'sigma' in kwargs:
pars[f'comp{comp}_ka1_sigma'].set(value=kwargs.get('sigma'), vary=False)
mod = pv1 + pv2
return mod, pars
def two_components_1(model, fit_params, width):
mod1 = model
mod1.prefix('comp1_')
pars = mod1.make_params(**fit_params.valuesdict())
mod2 = model
mod2.prefix('comp2_')
pars.update(mod2.make_params(**fit_params.valuesdict()))
# set all params to not vary
for param in pars:
param.set(vary=False)
pars.add('width', value=width, min=width / 2, max=width * 2) # arbitrary values for width bounds
pars['comp1_ka1_center'].set(expr='comp1_ka1_center + comp1_ka1_center/width', vary=True)
pars['comp2_ka1_center'].set(expr='comp2_ka1_center - comp2_ka1_center/width', vary=True)
sum_mod = mod1 + mod2
return sum_mod, pars
My main issue is how to provide the variable "model" to the second function so that it can be read in as a model and then combined in the following steps.
There is also probably an issue in assigning the parameters, in that I have not tested yet the **fit_params.valuesdict() as an input to the make_params method.
thanks and apologies if the issue is trivial, but I feel I am in a "dog biting it's tail" kind of situation...
Francesco