Skip to content Skip to sidebar Skip to footer

Different Constraints For Fit Parameter In Lmfit Model

I am trying to create a multible voigt/Gaussian/Lorentizan-peak fit function with lmfit. Therefore, I wrote the following Function: def apply_fit_mix_multy(data,modelPeak,peakPos,a

Solution 1:

If I understand your long question (it would be helpful to remove the extraneous stuff - and there is quite a lot of it), you want to create a Model with multiple peaks, allowing sigma from the 1st peak to vary freely, and constraining sigma for the other peaks to depend on this.

To do that, you can either use parameter hints (as you use in your make_model() function) or set expressions for the parameters after the Parameters object is created. For the first approach, something like this

defmake_model(num,amplitud,center,mod):
    pref = "peak{0}_".format(num)
    model = mod(prefix = pref)
    model.set_param_hint(pref+'amplitud', value=amplitud[num], min=0, max=5*amplitud[num])
    model.set_param_hint(pref+'center', value=center[num], min=center[num]-0.5, max=center[num]+0.5)
    if(num==0):
        model.set_param_hint(pref+'sigma', value=0.3, min=0.01, max=1)
    else:
        ## instead of # model.set_param_hint(pref+'sigma', value=0.3, min=0.01, max=1)## set peakN_sigma == peak0_sigma
        model.set_param_hint(pref+'sigma', expr='peak0_sigma') 
        ## or maybe set peakN_sigma == N * peak0_sigma 
        model.set_param_hint(pref+'sigma', expr='%d*peak0_sigma' % num)
    return model

You could also make the full model (simplified somewhat from your code, but the same idea):

model = (VoigtModel(prefix='peak0_') + VoigtModel(prefix='peak1_') +
         VoigtModel(prefix='peak2_') + LinearModel(prefix='const_'))

# create parameters with default valuesparams = model.make_params(peak0_amplitude=10, peak0_sigma=2, ....)

# set constraints for `sigma` params:params['peak1_sigma'].expr = 'peak0_sigma'params['peak2_sigma'].expr = 'peak0_sigma'# similarly, set bounds as needed:params['peak1_sigma'].min = 0params['peak1_amplitude'].min = 0

Hope that helps...

Post a Comment for "Different Constraints For Fit Parameter In Lmfit Model"