用高斯和多指数衰减卷积进行曲线拟合的Python代码
我正在开发一个代码,用一个模型来拟合数据,这个模型是两个高斯函数与多指数衰减的卷积expAx+expBx+。。。。基本上,仅使用高斯和/或高斯修正的拟合在Lmfit中工作得非常好,但使用内置卷积,即如果使用两个函数的np卷积,Lmfit不起作用 我在互联网上试过很多例子,到目前为止,我意识到我的函数返回inf或nan值,而且数据的间隔也不相等,无法用于卷积。通过使用卷积的数学表达式和scipy.optimize.curve_fit,我找到了解决这个问题的一条捷径。但这是一个非常笨拙和耗时的过程,我想找到一种方法,通过使用两个函数的卷积和使用lmfit,使其更复杂和通用,我可以更轻松地控制参数 该数据集也包含在注释中,作为您的参考用高斯和多指数衰减卷积进行曲线拟合的Python代码,python,curve-fitting,convolution,gaussian,exponential,Python,Curve Fitting,Convolution,Gaussian,Exponential,我正在开发一个代码,用一个模型来拟合数据,这个模型是两个高斯函数与多指数衰减的卷积expAx+expBx+。。。。基本上,仅使用高斯和/或高斯修正的拟合在Lmfit中工作得非常好,但使用内置卷积,即如果使用两个函数的np卷积,Lmfit不起作用 我在互联网上试过很多例子,到目前为止,我意识到我的函数返回inf或nan值,而且数据的间隔也不相等,无法用于卷积。通过使用卷积的数学表达式和scipy.optimize.curve_fit,我找到了解决这个问题的一条捷径。但这是一个非常笨拙和耗时的过程,
w=0.1 # is constant
def CONVSum(x,w,*p):
n=np.int(len(p)/3)
A=p[:n]
B=p[n:2*n]
C=p[2*n:3*n]
# =======================================================================
# below formula is derived as mathematical expression of convoluted multi exponential components with a gaussian distribution based on the instruction given in http://www.np.ph.bham.ac.uk/research_resources/programs/halflife/gauss_exp_conv.pdf
# ======================================================================
fnct=sum(np.float64([A[i]*np.exp(-B[i]*((x-C[i])-(0.5*np.square(w)*B[i])))*(1+scipy.special.erf(((x-C[i])-(np.square(w)*B[i]))/(np.sqrt(2)*w))) for i in range(n)]))
fnct[np.isnan(fnct)]=0
fnct[fnct<1e-12]=0
return fnct
N=4 #number of exponential functions to be fitted
params = np.linspace(1, 0.0001, N*3); #parameters for a multiple exponential
popt,pcov = curve_fit(CONVSum,x,y,p0=params,
bounds=((0,0,0,0,-np.inf,-np.inf,-np.inf,-np.inf,-3,-3,-3,-3),
(1,1,1,1, np.inf, np.inf, np.inf, np.inf, 3, 3, 3, 3)),
maxfev = 1000000)
非常感谢关于高斯和多重指数衰减卷积拟合的任何帮助或提示,我更喜欢使用lmfit,因为我可以很好地识别参数,并将它们相互关联
理想情况下,我想用参数来拟合我的数据,其中一些参数在数据集之间共享,一些参数是延迟+关闭的。好吧,你的脚本有点难读,并且有很多与你的问题无关的内容。你的exgauss函数不是在防范无穷大。x>~710的np.expx将给出Inf,拟合将无法继续 这是所给出的固化配件代码的等值。我通过在和中使用非常好的指导和信息,成功地创建了这个。但它仍然需要发展
# =============================================================================
# below formula is drived as mathematical expresion of convoluted multi exponential components with a gausian distribution based on the instruction given in http://www.np.ph.bham.ac.uk/research_resources/programs/halflife/gauss_exp_conv.pdf
# =============================================================================
def CONVSum(x,params):
fnct=sum(
np.float64([
(params['amp%s_%s'%(n,i)].value)*np.exp(-(params['dec%s_%s'%(n,i)].value)*((x-(params['cen%s_%s'%(n,i)].value))-
(0.5*np.square((params['sig%s_%s'%(n,i)].value))*(params['dec%s_%s'%(n,i)].value))))*
(1+scipy.special.erf(((x-(params['cen%s_%s'%(n,i)].value))-(np.square((params['sig%s_%s'%(n,i)].value))*
(params['dec%s_%s'%(n,i)].value)))/(np.sqrt(2)*(params['sig%s_%s'%(n,i)].value)))) for n in range(N) for i in wav
])
)
fnct=fnct/fnct.max()
return fnct
# =============================================================================
# this global fit were adapted from https://stackoverflow.com/questions/20339234/python-and-lmfit-how-to-fit-multiple-datasets-with-shared-parameters/20341726#20341726
# it is of very important thet we can identify the shared parameteres for datasets
# =============================================================================
def objective(params, x, data):
""" calculate total residual for fits to several data sets"""
ndata = data.shape[0]
resid = 0.0*data[:]
# make residual per data set
resid = data- CONVSum(x,params)
# now flatten this to a 1D array, as minimize() needs
return resid.flatten()
# selec datasets
x = df[949].index
data =df[949].values
# create required sets of parameters, one per data set
N=4 #number of exponential decays
wav=[949] #the desired data to be fitted
fit_params = Parameters()
for i in wav:
for n in range(N):
fit_params.add( 'amp%s_%s'%(n,i), value=1, min=0.0, max=1)
fit_params.add( 'dec%s_%s'%(n,i), value=0.5, min=-1e10, max=1e10)
fit_params.add( 'cen%s_%s'%(n,i), value=0.1, min=-3.0, max=1000)
fit_params.add( 'sig%s_%s'%(n,i), value=0.1, min=0.05, max=0.5)
# now we constrain some values to have the same value
# for example assigning sig_2, sig_3, .. sig_5 to be equal to sig_1
for i in wav:
for n in (1,2,3):
print(n,i)
fit_params['sig%s_%s'%(n,i)].expr='sig0_949'
fit_params['cen%s_%s'%(n,i)].expr='cen0_949'
# it will run the global fit to all the data sets
result = minimize(objective, fit_params, args=(x,data))
report_fit(result.params)
# plot the data sets and fits
plt.close('all')
plt.figure()
for i in wav:
y_fit = CONVSum(x,result.params)
plt.plot(x, data, 'o-', x, y_fit, '-')
plt.xscale('symlog')
plt.show()
不幸的是,拟合结果不是很令人满意,我仍在寻求一些改进建议。这是我的示例数据文件亲爱的@M Newville感谢您的回复,使用np.isnan消除nan和inf解决了问题,但拟合操作系统仍然不起作用。我确实找到了编辑我的问题的方法,但我对它做了一点修改。这很难阅读,而且显然是不完整的。。。如果您想帮助清理并理解如何使其更好,请发布一个完整的示例,包括导入和读取数据。事实上,根本不可能知道你在做什么。。。这对任何人都没有帮助。