[SciPy-User] Asymmetric peak fitting

Paweł Kwaśniewski pawel.kw at gmail.com
Thu Mar 19 07:04:17 EDT 2015


Hi,

Ok, now I have a problem. In the attached script I try to fit some example
experimental data I have. The peak is actually a numerical derivative of a
step function - this is why the values are so large. I tried different
models, all except the ExponentialGaussianModel can handle the fit pretty
well using the automatically guessed parameters. I don't really understand
why. Is there something I'm missing here?

Cheers,

Pawel

2015-03-19 10:57 GMT+01:00 Paweł Kwaśniewski <pawel.kw at gmail.com>:

> Matt,
>
> I have one more question: the weights for fitting a model should be
> 1./error_bar or the error_bar? The name "weights" suggests the former, but
> I'd like to be sure.
>
> Cheers,
>
> Paweł
>
> 2015-03-19 9:09 GMT+01:00 Paweł Kwaśniewski <pawel.kw at gmail.com>:
>
>> Jonathan, Matt,
>>
>> Thank you for your answers. I must say that lmfit is already a good
>> friend of mine - I use it for quite some time for fitting. Somehow I didn't
>> think of looking there for asymmetric peak models. Thanks for pointing this
>> out! I suppose this will solve my problem.
>>
>> Cheers,
>>
>> Pawel
>>
>> 2015-03-19 2:16 GMT+01:00 Matt Newville <newville at cars.uchicago.edu>:
>>
>>> Pawel,
>>>
>>> On Wed, Mar 18, 2015 at 10:06 AM, Paweł Kwaśniewski <pawel.kw at gmail.com>
>>> wrote:
>>>
>>>> Hi All,
>>>>
>>>> I'm currently trying to fit some experimental data in the form of
>>>> asymmetric peaks. In principle it's difficult to find a distribution
>>>> describing this kind of data. My goal is to get the full width half maximum
>>>> of the peak and the peak position. I tried to look for some ready solutions
>>>> within scipy but could not find any. I did find a nice paper though:
>>>>
>>>> http://arxiv.org/abs/0711.4449
>>>>
>>>> Since it's not so trivial (at least not for me) to implement this, I'd
>>>> like to ask if anyone in the community has already done this (or something
>>>> similar) and would like to share the code.
>>>>
>>>> Cheers,
>>>>
>>>> Pawel Kwasniewski
>>>>
>>>
>>> You might find the lmfit module (http://lmfit.github.io/lmfit-py/)
>>> useful.  This is a module for least-squares minimization and curve-fitting,
>>> built on top of scipy.optimize.  It includes several built-in Models, a few
>>> representing asymmetric peaks such as an exponentially damped Gaussian (
>>> http://lmfit.github.io/lmfit-py/builtin_models.html#exponentialgaussianmodel).
>>> A quick look at the paper you referenced suggests this function might be
>>> sufficient, but if this or one of the other built-in Models isn't exactly
>>> what you're looking forward, it's very easy to make a new Model class from
>>> a Python function that calculates and returns the model function.  Lmfit
>>> Models can also be added  or multiplied together to make composite models
>>> (say, Gaussian + Step + Quadratic), and allow you to place bounds and/or
>>> constraints on any of the Parameters in the fit.
>>>
>>> Though there are many features you can use to set up models and their
>>> parameters, using these models (either built-in or one that you define
>>> yourself) for curve-fitting data is pretty straightforward.  See
>>> http://lmfit.github.io/lmfit-py/builtin_models.html#example-1-fit-peaked-data-to-gaussian-lorentzian-and-voigt-profiles
>>> for a simple example.   That example uses symmetric peaks, but the use of
>>> any Model for curve-fitting is basically the same.
>>>
>>> Hope that helps,
>>>
>>> --Matt Newville
>>>
>>>
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150319/5c15f423/attachment.html>
-------------- next part --------------
import lmfit
import pylab as pl

data = pl.loadtxt('example_data.txt')

x = data[:,0]
y = data[:,1]
#model = lmfit.models.ExponentialGaussianModel()
model = lmfit.models.GaussianModel()
#model = lmfit.models.SkewedGaussianModel()
#model = lmfit.models.VoigtModel()

pars = model.guess(y,x=x)
initguess = model.eval(params=pars,x=x)
out = model.fit(y,pars,x=x)
print(out.fit_report(min_correl=0.25))
fitres = model.eval(params=out.params,x=x)
fig = pl.figure()
ax = pl.subplot(111)
ax.plot(x,y,'o')
ax.plot(x,fitres,'-',lw=1)
ax.plot(x,initguess,'--k',lw=1)
pl.show()


-------------- next part --------------
-2.000000000000000000e+00 -1.030000023022569076e+05
-1.990000000223520082e+00 -3.850000086055233987e+04
-1.980000000447029951e+00 2.440000054538901168e+04
-1.970000000670550033e+00 -3.200000071526428201e+03
-1.960000000894069894e+00 -9.700000216814485611e+03
-1.950000001117589976e+00 -3.010000067279546420e+04
-1.940000001341100067e+00 4.500000100584039501e+03
-1.930000001564619927e+00 2.190000048950899145e+04
-1.920000001788140009e+00 -5.130000114665804722e+04
-1.910000002011660092e+00 1.200000026822410473e+04
-1.900000002235169960e+00 4.980000111313003435e+04
-1.890000002458690043e+00 -1.590000035539693818e+04
-1.880000002682209903e+00 -1.900000042468816537e+04
-1.870000002905729986e+00 -4.200000093878436928e+03
-1.860000003129240076e+00 -2.090000046715698409e+04
-1.850000003352759936e+00 -1.840000041127696022e+04
-1.840000003576280019e+00 1.870000041798256279e+04
-1.830000003799800101e+00 3.200000071526428201e+03
-1.820000004023309970e+00 -2.460000054985941460e+04
-1.810000004246830052e+00 2.060000046045138151e+04
-1.800000004470349912e+00 -2.900000064820825355e+04
-1.790000004693869995e+00 -3.850000086055233987e+04
-1.780000004917380085e+00 5.070000113324684207e+04
-1.770000005140899946e+00 5.080000113548204536e+04
-1.760000005364420028e+00 1.500000033528013046e+04
-1.750000005587940111e+00 -3.430000076667389658e+04
-1.740000005811449979e+00 -2.500000055880021682e+04
-1.730000006034970061e+00 8.100000181051270374e+03
-1.720000006258489922e+00 8.400000187756873856e+03
-1.710000006482010004e+00 1.350000030175211759e+04
-1.700000006705520095e+00 -2.700000060350423610e+03
-1.690000006929039955e+00 -3.800000084937633346e+03
-1.680000007152560038e+00 4.000000089408035251e+02
-1.670000007376069906e+00 -3.020000067503066384e+04
-1.660000007599589988e+00 -5.000000111760043637e+02
-1.650000007823110071e+00 2.330000052080180467e+04
-1.640000008046629931e+00 -1.900000042468816673e+03
-1.630000008270140022e+00 3.430000076667389658e+04
-1.620000008493660104e+00 7.900000176580869265e+03
-1.610000008717179965e+00 -3.170000070855867671e+04
-1.600000008940700047e+00 -9.100000203403280466e+03
-1.590000009164209915e+00 1.920000042915856829e+04
-1.580000009387729998e+00 1.010000022575528783e+04
-1.570000009611250080e+00 -4.150000092760836560e+04
-1.560000009834769941e+00 1.060000023693129333e+04
-1.550000010058280031e+00 5.070000113324684207e+04
-1.540000010281799891e+00 -1.370000030622252052e+04
-1.530000010505319974e+00 -3.260000072867548442e+04
-1.520000010728840056e+00 2.880000064373785062e+04
-1.510000010952349925e+00 3.610000080690751929e+04
-1.500000011175870007e+00 -1.090000024363689590e+04
-1.490000011399390090e+00 -2.700000060350423519e+04
-1.480000011622909950e+00 -1.820000040680656093e+04
-1.470000011846420040e+00 -1.400000031292812309e+03
-1.460000012069939901e+00 -1.450000032410412678e+04
-1.450000012293459983e+00 -1.190000026598890327e+04
-1.440000012516980066e+00 -3.400000075996829764e+03
-1.430000012740489934e+00 -2.390000053421300981e+04
-1.420000012964010017e+00 1.620000036210254075e+04
-1.410000013187530099e+00 2.700000060350423519e+04
-1.400000013411049959e+00 -1.550000034645613596e+04
-1.390000013634560050e+00 2.350000052527220396e+04
-1.380000013858079910e+00 1.700000037998414882e+03
-1.370000014081599993e+00 -4.020000089855075203e+04
-1.360000014305110083e+00 3.140000070185307413e+04
-1.350000014528629944e+00 1.610000035986734110e+04
-1.340000014752150026e+00 -3.970000088737475016e+04
-1.330000014975670108e+00 -2.990000066832506127e+04
-1.320000015199179977e+00 5.500000122936047774e+03
-1.310000015422700059e+00 3.030000067726586349e+04
-1.300000015646219920e+00 1.310000029281131538e+04
-1.290000015869740002e+00 -1.770000039563055543e+04
-1.280000016093250093e+00 2.400000053644821037e+03
-1.270000016316769953e+00 7.000000156464061547e+02
-1.260000016540290035e+00 1.100000024587209509e+03
-1.250000016763809896e+00 1.000000022352008813e+02
-1.240000016987319986e+00 -3.170000070855867671e+04
-1.230000017210840069e+00 -3.100000069291227192e+03
-1.220000017434359929e+00 2.130000047609778630e+04
-1.210000017657880012e+00 2.300000051409620210e+04
-1.200000017881390102e+00 -1.370000030622252052e+04
-1.190000018104909962e+00 -2.300000051409620028e+03
-1.180000018328430045e+00 -2.170000048503858852e+04
-1.170000018551949905e+00 -2.000000044704017625e+02
-1.160000018775459996e+00 1.860000041574736315e+04
-1.150000018998980078e+00 -1.370000030622252052e+04
-1.140000019222499938e+00 1.620000036210254075e+04
-1.130000019446020021e+00 7.500000167640065229e+03
-1.120000019669529889e+00 -3.000000067056026182e+03
-1.110000019893049972e+00 -9.800000219049685256e+03
-1.100000020116570054e+00 -1.570000035092653707e+04
-1.090000020340089915e+00 1.150000025704810105e+04
-1.080000020563600005e+00 5.200000116230445201e+03
-1.070000020787120087e+00 8.000000178816070502e+02
-1.060000021010639948e+00 4.100000091643235464e+03
-1.050000021234150038e+00 -4.000000089408034910e+03
-1.040000021457669899e+00 2.810000062809144583e+04
-1.030000021681189981e+00 -4.000000089408034910e+03
-1.020000021904710064e+00 -1.420000031739852420e+04
-1.010000022128219932e+00 -1.090000024363689590e+04
-1.000000022351740014e+00 -1.860000041574736315e+04
-9.900000225752589866e-01 1.100000024587209509e+03
-9.800000227987769597e-01 2.480000055432981753e+04
-9.700000230222940445e-01 2.300000051409620210e+04
-9.600000232458110183e-01 -4.750000106172041706e+04
-9.500000234693289913e-01 -3.600000080467231328e+03
-9.400000236928459652e-01 1.630000036433774221e+04
-9.300000239163640492e-01 -5.200000116230445201e+03
-9.200000241398810230e-01 3.050000068173626642e+04
-9.100000243633989960e-01 5.900000131876851810e+03
-9.000000245869159698e-01 -8.900000198932877538e+03
-8.900000248104330547e-01 -1.120000025034249848e+04
-8.800000250339510277e-01 -2.730000061020983776e+04
-8.700000252574680015e-01 1.200000026822410473e+04
-8.600000254809859745e-01 4.210000094101957075e+04
-8.500000257045029484e-01 4.400000098348838037e+03
-8.400000259280200332e-01 -1.330000029728171648e+04
-8.300000261515380062e-01 -3.830000085608193331e+04
-8.200000263750549800e-01 -1.000000022352008818e+04
-8.100000265985729531e-01 2.420000054091861239e+04
-8.000000268220900379e-01 -3.700000082702432337e+03
-7.900000270456080109e-01 -2.880000064373785062e+04
-7.800000272691249847e-01 -2.440000054538901168e+04
-7.700000274926419586e-01 4.480000100136999390e+04
-7.600000277161600426e-01 1.140000025481289958e+04
-7.500000279396770164e-01 -3.520000078679070430e+04
-7.400000281631949894e-01 2.230000049844979367e+04
-7.300000283867119633e-01 2.990000066832506127e+04
-7.200000286102290481e-01 -4.500000100584039319e+04
-7.100000288337470211e-01 -3.880000086725794245e+04
-7.000000290572639949e-01 4.640000103713320277e+04
-6.900000292807819680e-01 2.180000048727379180e+04
-6.800000295042990528e-01 -1.070000023916649297e+04
-6.700000297278170258e-01 -2.850000063703224805e+04
-6.600000299513339996e-01 2.590000057891702818e+04
-6.500000301748509735e-01 3.200000071526427928e+04
-6.400000303983689465e-01 -3.460000077337949915e+04
-6.300000306218860313e-01 -4.300000096113637483e+03
-6.200000308454040043e-01 -2.270000050739059952e+04
-6.100000310689209782e-01 8.500000189992073501e+03
-6.000000312924389512e-01 6.200000138582454383e+03
-5.900000315159560360e-01 -8.200000183286470929e+03
-5.800000317394730098e-01 2.150000048056818923e+04
-5.700000319629909828e-01 7.400000165404864674e+03
-5.600000321865079567e-01 1.420000031739852420e+04
-5.500000324100260407e-01 -3.250000072644028478e+04
-5.400000326335430145e-01 -1.080000024140169444e+04
-5.300000328570599883e-01 1.970000044033457380e+04
-5.200000330805779614e-01 4.200000093878436928e+03
-5.100000333040950462e-01 -7.100000158699262101e+03
-5.000000335276130192e-01 -2.730000061020983776e+04
-4.900000337511299930e-01 1.000000022352008813e+02
-4.800000339746480216e-01 4.910000109748362593e+04
-4.700000341981649954e-01 8.200000183286470929e+03
-4.600000344216820247e-01 -2.100000046939218373e+04
-4.500000346451999977e-01 -1.860000041574736315e+04
-4.400000348687170271e-01 9.100000203403280466e+03
-4.300000350922350001e-01 3.870000086502273916e+04
-4.200000353157519739e-01 -3.600000080467231328e+03
-4.100000355392690032e-01 3.100000069291227192e+03
-4.000000357627869763e-01 1.640000036657294186e+04
-3.900000359863040056e-01 -1.800000040233615800e+04
-3.800000362098219786e-01 -2.850000063703224805e+04
-3.700000364333390079e-01 -8.200000183286470929e+03
-3.600000366568569810e-01 4.700000105054441519e+03
-3.500000368803740103e-01 1.740000038892495286e+04
-3.400000371038909841e-01 8.000000178816069820e+03
-3.300000373274090126e-01 -8.000000178816070502e+02
-3.200000375509259865e-01 1.800000040233615664e+03
-3.100000377744440150e-01 -1.670000037327854443e+04
-3.000000379979609888e-01 -7.800000174345667801e+03
-2.900000382214780181e-01 -5.600000125171249238e+03
-2.800000384449959911e-01 3.250000072644028478e+04
-2.700000386685130205e-01 1.740000038892495286e+04
-2.600000388920309935e-01 -3.820000085384673730e+04
-2.500000391155480228e-01 1.780000039786575508e+04
-2.400000393390659958e-01 2.450000054762421496e+04
-2.300000395625829974e-01 5.700000127406449792e+03
-2.200000397860999990e-01 -1.080000024140169444e+04
-2.100000400096179998e-01 -1.860000041574736315e+04
-2.000000402331350013e-01 1.400000031292812309e+04
-1.900000404566530021e-01 2.900000064820825355e+04
-1.800000406801700037e-01 5.900000131876851810e+03
-1.700000409036870053e-01 -1.210000027045930619e+04
-1.600000411272050060e-01 -5.500000122936047774e+03
-1.500000413507220076e-01 -1.610000035986734110e+04
-1.400000415742400084e-01 2.530000056550582303e+04
-1.300000417977570100e-01 4.420000098795878876e+04
-1.200000420212749969e-01 -1.410000031516332274e+04
-1.100000422447919984e-01 -1.700000037998414882e+03
-1.000000424683090000e-01 3.450000077114430314e+04
-9.000004269182680650e-02 4.850000108407242078e+04
-8.000004291534419665e-02 3.910000087396354502e+04
-7.000004313886169782e-02 4.080000091196195717e+04
-6.000004336237910185e-02 1.392000031113996229e+05
-5.000004358589649894e-02 2.265000050627299934e+05
-4.000004380941390297e-02 2.708000060529239709e+05
-3.000004403293130006e-02 3.696000082613024279e+05
-2.000004425644870062e-02 4.938000110374219366e+05
-1.000004447996620006e-02 5.917000132256835932e+05
-4.470348358154300184e-08 5.609000125372416805e+05
9.999955072999000549e-03 4.315000096448917757e+05
1.999995484948159999e-02 3.461000077360302093e+05
2.999995462596419943e-02 3.046000068084218656e+05
3.999995440244669825e-02 2.439000054516549280e+05
4.999995417892930116e-02 1.664000037193742464e+05
5.999995395541189713e-02 1.268000028342347068e+05
6.999995373189449310e-02 1.371000030644604121e+05
7.999995350837710295e-02 1.217000027202394704e+05
8.999995328485969892e-02 1.015000022687288874e+05
9.999995306134219775e-02 9.250000206756081025e+04
1.099999528378250019e-01 7.800000174345668347e+04
1.199999526143070011e-01 6.680000149311417772e+04
1.299999523907900134e-01 6.350000141935255670e+04
1.399999521672730118e-01 5.590000124947728909e+04
1.499999519437550111e-01 3.580000080020190944e+04
1.599999517202380095e-01 3.710000082925952302e+04
1.699999514967200087e-01 3.850000086055233987e+04
1.799999512732030071e-01 2.210000049397939438e+04
1.899999510496850064e-01 1.980000044256977344e+04
1.999999508261680048e-01 2.490000055656501718e+04
2.099999506026510032e-01 -5.700000127406449792e+03
2.199999503791330024e-01 -1.000000022352008727e+03
2.299999501556160009e-01 2.330000052080180467e+04
2.399999499320980001e-01 7.900000176580869265e+03
2.499999497085809985e-01 -3.700000082702432337e+03
2.599999494850639969e-01 2.700000060350423610e+03
2.699999492615460239e-01 2.080000046492178080e+04
2.799999490380289946e-01 1.410000031516332274e+04
2.899999488145110216e-01 -5.400000120700847219e+03
2.999999485909939922e-01 -1.450000032410412678e+04
3.099999483674760192e-01 4.400000098348838037e+03
3.199999481439589899e-01 1.550000034645613596e+04
3.299999479204420161e-01 -6.300000140817654938e+03
3.399999476969239876e-01 4.200000093878436928e+03
3.499999474734070137e-01 1.090000024363689590e+04
3.599999472498889852e-01 2.300000051409620028e+03
3.699999470263720114e-01 7.900000176580869265e+03
3.799999468028549821e-01 4.100000091643235464e+03
3.899999465793370090e-01 -1.190000026598890327e+04
3.999999463558199797e-01 6.500000145288056956e+03
4.099999461323020067e-01 1.260000028163530988e+04
4.199999459087849774e-01 -6.100000136347252919e+03
4.299999456852670043e-01 -6.000000134112052592e+02
4.399999454617499750e-01 1.020000022799048929e+04
4.499999452382330012e-01 1.200000026822410518e+03
4.599999450147149727e-01 -1.090000024363689590e+04
4.699999447911979988e-01 6.800000151993659529e+03
4.799999445676800258e-01 3.800000084937633346e+03
4.899999443441629965e-01 -1.820000040680656093e+04
4.999999441206460227e-01 7.300000163169664120e+03
5.099999438971279941e-01 2.390000053421300981e+04
5.199999436736110203e-01 5.800000129641650346e+03
5.299999434500930473e-01 -7.700000172110467247e+03
5.399999432265759625e-01 5.600000125171249238e+03
5.499999430030579894e-01 5.400000120700847219e+03
5.599999427795410156e-01 -2.070000046268658116e+04
5.699999425560240418e-01 1.020000022799048929e+04
5.799999423325059578e-01 8.500000189992073501e+03
5.899999421089889839e-01 -2.000000044704017455e+03
5.999999418854710109e-01 9.400000210108883039e+03
6.099999416619540371e-01 -1.130000025257769812e+04
6.199999414384369523e-01 -5.000000111760043637e+02
6.299999412149189792e-01 5.600000125171249238e+03
6.399999409914020054e-01 -6.800000151993659529e+03
6.499999407678840324e-01 -7.000000156464061547e+02
6.599999405443669476e-01 8.800000196697676074e+03
6.699999403208489746e-01 1.510000033751533192e+04
6.799999400973320007e-01 -7.000000156464061547e+03
6.899999398738150269e-01 -1.920000042915856829e+04
6.999999396502970539e-01 1.610000035986734110e+04
7.099999394267799691e-01 2.040000045598097859e+04
7.199999392032619960e-01 -1.380000030845772017e+04
7.299999389797450222e-01 -4.500000100584039501e+03
7.399999387562270492e-01 1.500000033528013046e+04
7.499999385327099644e-01 -6.900000154228860083e+03
7.599999383091929905e-01 -4.500000100584039501e+03
7.699999380856750175e-01 -2.600000058115222600e+03
7.799999378621580437e-01 -4.200000093878436928e+03
7.899999376386399597e-01 4.000000089408035251e+02
7.999999374151229858e-01 4.500000100584039501e+03
8.099999371916060120e-01 1.310000029281131538e+04
8.199999369680880390e-01 6.100000136347252919e+03
8.299999367445709542e-01 -7.300000163169664120e+03
8.399999365210529811e-01 -2.400000053644821037e+03
8.499999362975360073e-01 -1.000000022352008818e+04
8.599999360740180343e-01 -8.600000192227274965e+03
8.699999358505009495e-01 1.060000023693129333e+04
8.799999356269839756e-01 -1.400000031292812309e+03
8.899999354034660026e-01 4.600000102819240055e+03
8.999999351799490288e-01 3.200000071526428201e+03
9.099999349564309448e-01 -3.200000071526428201e+03
9.199999347329139709e-01 9.000000201168078320e+02
9.299999345093969971e-01 1.140000025481289958e+04
9.399999342858790241e-01 1.200000026822410518e+03
9.499999340623620503e-01 -8.000000178816069820e+03
9.599999338388439662e-01 3.300000073761628755e+03
9.699999336153269924e-01 -2.000000044704017625e+02
9.799999333918090194e-01 -3.500000078232030774e+03
9.899999331682920456e-01 -1.400000031292812309e+03
9.999999329447749608e-01 4.000000089408034910e+03


More information about the SciPy-User mailing list