added errorbar weighting and filter for nans and inf
This commit is contained in:
parent
21d7516789
commit
4319f4e394
|
@ -1,9 +1,21 @@
|
|||
import lmfit
|
||||
import numpy as np
|
||||
import logging as log
|
||||
|
||||
pv = lmfit.models.PseudoVoigtModel()
|
||||
|
||||
def fitPeak(x,y,autorange=False):
|
||||
def fitPeak(x,y,err=1,autorange=False):
|
||||
if isinstance(err,np.ndarray):
|
||||
if np.all(err==0):
|
||||
err = 1
|
||||
log.warn("Asked to fit peak but all errors are zero, forcing them to 1")
|
||||
elif np.isfinite(err).sum() != len(err):
|
||||
idx = np.isfinite(err)
|
||||
x = x[idx]
|
||||
y = y[idx]
|
||||
err = err[idx]
|
||||
log.warn("Asked to fit peak but some errorbars are infinite or nans,\
|
||||
excluding those points")
|
||||
if autorange:
|
||||
# find fwhm
|
||||
idx = np.ravel(np.argwhere( y<y.max()/2 ))
|
||||
|
@ -16,5 +28,5 @@ def fitPeak(x,y,autorange=False):
|
|||
x = x[idx]
|
||||
y = y[idx]
|
||||
pars = pv.guess(y,x=x)
|
||||
ret = pv.fit(y,x=x,params=pars)
|
||||
ret = pv.fit(y,x=x,weights=1/err,params=pars)
|
||||
return ret
|
||||
|
|
Loading…
Reference in New Issue